1 /* $Id: charutil.c,v 1.19 2004/04/28 00:19:03 bluehal Exp $ */
10 #include <sys/types.h>
17 static __inline__
void LeftTrim(char *psValue
)
19 char *psTmp
= psValue
;
21 while (*psTmp
== ' ' || *psTmp
== '\t')
24 /* can't use strcpy here, as the strings must not
25 overlap, at least according to spec. */
26 if (psTmp
> psValue
) {
27 while (*psTmp
!= '\0') {
28 *(psValue
++) = *(psTmp
++);
34 static __inline__
void RightTrim(char *psValue
)
36 long lLength
= strlen(psValue
) - 1;
38 while ((psValue
[lLength
] == ' ' || psValue
[lLength
] == '\t')
43 psValue
[++lLength
] = '\000';
46 void FullTrim(char *psValue
)
52 void Bin2Hex(unsigned char *src
, int length
, char *dst
)
54 static char hex_tbl
[] = "0123456789abcdef";
60 for (i
= 0; i
< length
; i
++) {
61 *ptr
++ = hex_tbl
[*src
>> 4];
62 *ptr
++ = hex_tbl
[*src
++ & 0xf];
71 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\000";
73 /* find char in in alphabet, return offset, -1 otherwise */
80 return a
- ALPHABET
- 1;
85 void Encode_Base64(char *src
, char *dst
)
94 g
= (g
<< 8) | *src
++;
96 *dst
++ = ALPHABET
[0x3f & (g
>> 18)];
97 *dst
++ = ALPHABET
[0x3f & (g
>> 12)];
98 *dst
++ = ALPHABET
[0x3f & (g
>> 6)];
99 *dst
++ = ALPHABET
[0x3f & g
];
106 *dst
++ = ALPHABET
[0x3f & (g
>> 2)];
107 *dst
++ = ALPHABET
[0x3f & (g
<< 4)];
111 *dst
++ = ALPHABET
[0x3f & (g
>> 10)];
112 *dst
++ = ALPHABET
[0x3f & (g
>> 4)];
113 *dst
++ = ALPHABET
[0x3f & (g
<< 2)];
121 void Decode_Base64(char *src
, char *dst
)
131 n
= find_char(*src
++);
147 /* shouldn't happen, but do something anyway */
159 /* helper function for the configuration line parser */
161 copy_substring(char *destination
,
162 int startidx
, int endidx
, const char *source
)
165 strncpy(destination
, source
+ startidx
, endidx
- startidx
);
166 destination
[endidx
- startidx
] = '\0';
170 /* common to Pop3 and Imap4 authentication list grabber. */
171 void grab_authList(const char *source
, char *destination
)
174 /* regex isn't all that helpful for lists of things. */
175 /* but does leave the end of the matched region in regs.end[0] */
176 /* what remains is a list of legal authentication schemes. */
177 if (isalnum(source
[0])) {
178 /* copy, while turning caps into lower case */
179 for (i
= 0; i
< 99 && source
[i
] != '\0'; i
++) {
180 destination
[i
] = tolower(source
[i
]);
182 destination
[i
] = '\0';
184 destination
[0] = '\0';
189 int compile_and_match_regex(const char *regex
, const char *str
, /*@out@ */
190 struct re_registers
*regs
)
195 struct re_pattern_buffer rpbuf
;
197 /* compile the regex pattern */
198 memset(&rpbuf
, 0, sizeof(struct re_pattern_buffer
));
199 /* posix egrep interprets intervals (eg. {1,32}) nicely */
200 re_syntax_options
= RE_SYNTAX_POSIX_EGREP
;
201 errstr
= re_compile_pattern(regex
, strlen(regex
), &rpbuf
);
202 if (errstr
!= NULL
) {
203 fprintf(stderr
, "error in compiling regular expression: %s\n",
208 /* match the regex */
209 regs
->num_regs
= REGS_UNALLOCATED
;
210 matchedchars
= re_match(&rpbuf
, str
, strlen(str
), 0, regs
);
211 /* this can fail (return -1 or 0) without being an error,
212 if we're trying to apply a regex just to see if it
217 for (i
= 1; i
< 6; i
++) {
218 printf("%d %d, (%.*s)\n", regs
.start
[i
], regs
.end
[i
],
219 (regs
.end
[i
] - regs
.start
[i
]),
220 (regs
.start
[i
] >= 0) ? &str
[regs
.start
[i
]] : "");
224 regfree(&rpbuf
); // added 3 jul 02, appeasing valgrind
229 /* like perl chomp(); useful for dealing with input from popen */
232 int l
= strlen(s
) - 1;
233 if (l
>= 0 && s
[l
] == '\n')
237 char *strdup_ordie(const char *c
)
239 char *ret
= strdup(c
);
241 fprintf(stderr
, "ran out of memory\n");
247 void StripComment(char *buf
)
251 /* Strip comments at beginning of line, or after whitespace.
252 a kludgy way of avoiding problems with #'s in passwords. */
255 for (p
= (char *) buf
+ 1; *p
&& !(*p
== '#' && isspace(*(p
- 1)));