4 * Utility functions that are used by both the client and server.
20 typedef unsigned char byte; /* Byte type */
21 static byte dtable[256]; /* base64 encode / decode table */
24 char *safestrncpy(char *dest, const char *src, size_t n)
26 if (dest == NULL || src == NULL) {
27 fprintf(stderr, "safestrncpy: NULL argument\n");
30 strncpy(dest, src, n);
37 #ifndef HAVE_STRNCASECMP
38 int strncasecmp(char *lstr, char *rstr, int len)
43 lc=tolower(lstr[pos]);
44 rc=tolower(rstr[pos]);
45 if ((lc==0)&&(rc==0)) return(0);
46 if (lc<rc) return(-1);
57 * num_tokens() - discover number of parameters/tokens in a string
59 int num_tokens(char *source, char tok) {
63 for (a=0; a<strlen(source); ++a) {
64 if (source[a]==tok) ++count;
70 * extract_token() - a smarter string tokenizer
72 void extract_token(char *dest, char *source, int parmnum, char separator)
82 if (strlen(source)==0) {
86 for (i=0; i<strlen(source); ++i) {
87 if (source[i]==separator) {
90 else if (curr_parm == parmnum) {
92 dest[len++] = source[i];
100 * remove_token() - a tokenizer that kills, maims, and destroys
102 void remove_token(char *source, int parmnum, char separator)
114 if (strlen(source)==0) {
118 for (i=0; i<strlen(source); ++i) {
119 if ( (start < 0) && (curr_parm == parmnum) ) {
123 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
127 if (source[i]==separator) {
132 if (end < 0) end = strlen(source);
134 printf("%d .. %d\n", start, end);
136 strcpy(&source[start], &source[end]);
143 * extract_int() - extract an int parm w/o supplying a buffer
145 int extract_int(char *source, int parmnum)
149 extract_token(buf, source, parmnum, '|');
154 * extract_long() - extract an long parm w/o supplying a buffer
156 long extract_long(char *source, long int parmnum)
160 extract_token(buf, source, parmnum, '|');
167 * decode_base64() and encode_base64() are adaptations of code by
168 * John Walker, found in full in the file "base64.c" included with this
169 * distribution. The difference between those functions and these is that
170 * these are intended to encode/decode small string buffers, and those are
171 * intended to encode/decode entire MIME parts.
174 void encode_base64(char *dest, char *source)
176 int i, hiteof = FALSE;
180 /* Fill dtable with character encodings. */
182 for (i = 0; i < 26; i++) {
184 dtable[26 + i] = 'a' + i;
186 for (i = 0; i < 10; i++) {
187 dtable[52 + i] = '0' + i;
193 byte igroup[3], ogroup[4];
196 igroup[0] = igroup[1] = igroup[2] = 0;
197 for (n = 0; n < 3; n++) {
203 igroup[n] = (byte) c;
206 ogroup[0] = dtable[igroup[0] >> 2];
207 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
208 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
209 ogroup[3] = dtable[igroup[2] & 0x3F];
211 /* Replace characters in output stream with "=" pad
212 characters if fewer than three characters were
213 read from the end of the input stream. */
221 for (i = 0; i < 4; i++) {
222 dest[dpos++] = ogroup[i];
231 void decode_base64(char *dest, char *source)
237 for (i = 0; i < 255; i++) {
240 for (i = 'A'; i <= 'Z'; i++) {
241 dtable[i] = 0 + (i - 'A');
243 for (i = 'a'; i <= 'z'; i++) {
244 dtable[i] = 26 + (i - 'a');
246 for (i = '0'; i <= '9'; i++) {
247 dtable[i] = 52 + (i - '0');
253 /*CONSTANTCONDITION*/
255 byte a[4], b[4], o[3];
257 for (i = 0; i < 4; i++) {
258 int c = source[spos++];
266 if (dtable[c] & 0x80) {
267 /* Ignoring errors: discard invalid character. */
272 b[i] = (byte) dtable[c];
274 o[0] = (b[0] << 2) | (b[1] >> 4);
275 o[1] = (b[1] << 4) | (b[2] >> 2);
276 o[2] = (b[2] << 6) | b[3];
277 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
278 if (i>=1) dest[dpos++] = o[0];
279 if (i>=2) dest[dpos++] = o[1];
280 if (i>=3) dest[dpos++] = o[2];
291 * Strip leading and trailing spaces from a string
293 void striplt(char *buf)
295 while ((strlen(buf) > 0) && (isspace(buf[0])))
296 strcpy(buf, &buf[1]);
297 while (isspace(buf[strlen(buf) - 1]))
298 buf[strlen(buf) - 1] = 0;
306 * Return the number of occurances of character ch in string st
308 int haschar(char *st, int ch)
312 for (a = 0; a < strlen(st); ++a)
322 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
324 int collapsed_strcmp(char *s1, char *s2) {
328 c1 = malloc(strlen(s1)+1);
329 c2 = malloc(strlen(s2)+1);
334 for (i=0; i<strlen(s1); ++i) {
335 if (isalnum(s1[i])) {
336 c1[pos] = tolower(s1[i]);
342 for (i=0; i<strlen(s2); ++i) {
343 if (isalnum(s2[i])) {
344 c2[pos] = tolower(s2[i]);
349 ret = strcmp(c1, c2);
358 * Format a date/time stamp for output
360 void fmt_date(char *buf, time_t thetime) {
364 char *ascmonths[] = {
365 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
366 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
370 tm = localtime(&thetime);
373 if (hour == 0) hour = 12;
374 else if (hour > 12) hour = hour - 12;
376 sprintf(buf, "%s %d %4d %d:%02d%s",
377 ascmonths[tm->tm_mon],
382 ( (tm->tm_hour >= 12) ? "pm" : "am" )