4 * Utility functions that are used by both the client and server.
19 typedef unsigned char byte; /* Byte type */
20 static byte dtable[256]; /* base64 encode / decode table */
23 char *safestrncpy(char *dest, const char *src, size_t n)
25 if (dest == NULL || src == NULL) {
26 fprintf(stderr, "safestrncpy: NULL argument\n");
29 strncpy(dest, src, n);
36 #ifndef HAVE_STRNCASECMP
37 int strncasecmp(char *lstr, char *rstr, int len)
42 lc=tolower(lstr[pos]);
43 rc=tolower(rstr[pos]);
44 if ((lc==0)&&(rc==0)) return(0);
45 if (lc<rc) return(-1);
56 * num_tokens() - discover number of parameters/tokens in a string
58 int num_tokens(char *source, char tok) {
62 for (a=0; a<strlen(source); ++a) {
63 if (source[a]==tok) ++count;
69 * extract_token() - a smarter string tokenizer
71 void extract_token(char *dest, char *source, int parmnum, char separator)
81 if (strlen(source)==0) {
85 for (i=0; i<strlen(source); ++i) {
86 if (source[i]==separator) {
89 else if (curr_parm == parmnum) {
91 dest[len++] = source[i];
99 * remove_token() - a tokenizer that kills, maims, and destroys
101 void remove_token(char *source, int parmnum, char separator)
113 if (strlen(source)==0) {
117 for (i=0; i<strlen(source); ++i) {
118 if ( (start < 0) && (curr_parm == parmnum) ) {
122 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
126 if (source[i]==separator) {
131 if (end < 0) end = strlen(source);
133 printf("%d .. %d\n", start, end);
135 strcpy(&source[start], &source[end]);
142 * extract_int() - extract an int parm w/o supplying a buffer
144 int extract_int(char *source, int parmnum)
148 extract_token(buf, source, parmnum, '|');
153 * extract_long() - extract an long parm w/o supplying a buffer
155 long extract_long(char *source, long int parmnum)
159 extract_token(buf, source, parmnum, '|');
166 * decode_base64() and encode_base64() are adaptations of code by
167 * John Walker, found in full in the file "base64.c" included with this
168 * distribution. The difference between those functions and these is that
169 * these are intended to encode/decode small string buffers, and those are
170 * intended to encode/decode entire MIME parts.
173 void encode_base64(char *dest, char *source)
175 int i, hiteof = FALSE;
179 /* Fill dtable with character encodings. */
181 for (i = 0; i < 26; i++) {
183 dtable[26 + i] = 'a' + i;
185 for (i = 0; i < 10; i++) {
186 dtable[52 + i] = '0' + i;
192 byte igroup[3], ogroup[4];
195 igroup[0] = igroup[1] = igroup[2] = 0;
196 for (n = 0; n < 3; n++) {
202 igroup[n] = (byte) c;
205 ogroup[0] = dtable[igroup[0] >> 2];
206 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
207 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
208 ogroup[3] = dtable[igroup[2] & 0x3F];
210 /* Replace characters in output stream with "=" pad
211 characters if fewer than three characters were
212 read from the end of the input stream. */
220 for (i = 0; i < 4; i++) {
221 dest[dpos++] = ogroup[i];
230 void decode_base64(char *dest, char *source)
236 for (i = 0; i < 255; i++) {
239 for (i = 'A'; i <= 'Z'; i++) {
240 dtable[i] = 0 + (i - 'A');
242 for (i = 'a'; i <= 'z'; i++) {
243 dtable[i] = 26 + (i - 'a');
245 for (i = '0'; i <= '9'; i++) {
246 dtable[i] = 52 + (i - '0');
252 /*CONSTANTCONDITION*/
254 byte a[4], b[4], o[3];
256 for (i = 0; i < 4; i++) {
257 int c = source[spos++];
265 if (dtable[c] & 0x80) {
266 /* Ignoring errors: discard invalid character. */
271 b[i] = (byte) dtable[c];
273 o[0] = (b[0] << 2) | (b[1] >> 4);
274 o[1] = (b[1] << 4) | (b[2] >> 2);
275 o[2] = (b[2] << 6) | b[3];
276 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
277 if (i>=1) dest[dpos++] = o[0];
278 if (i>=2) dest[dpos++] = o[1];
279 if (i>=3) dest[dpos++] = o[2];
290 * Strip leading and trailing spaces from a string
292 void striplt(char *buf)
294 while ((strlen(buf) > 0) && (isspace(buf[0])))
295 strcpy(buf, &buf[1]);
296 while (isspace(buf[strlen(buf) - 1]))
297 buf[strlen(buf) - 1] = 0;
305 * Return the number of occurances of character ch in string st
307 int haschar(char *st, int ch)
311 for (a = 0; a < strlen(st); ++a)
321 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
323 int collapsed_strcmp(char *s1, char *s2) {
327 c1 = malloc(strlen(s1)+1);
328 c2 = malloc(strlen(s2)+1);
333 for (i=0; i<strlen(s1); ++i) {
334 if (isalnum(s1[i])) {
335 c1[pos] = tolower(s1[i]);
341 for (i=0; i<strlen(s2); ++i) {
342 if (isalnum(s2[i])) {
343 c2[pos] = tolower(s2[i]);
348 ret = strcmp(c1, c2);
357 * Format a date/time stamp for output
359 void fmt_date(char *buf, time_t thetime) {
363 char *ascmonths[] = {
364 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
365 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
369 tm = localtime(&thetime);
372 if (hour == 0) hour = 12;
373 else if (hour > 12) hour = hour - 12;
375 sprintf(buf, "%s %d %4d %d:%02d%s",
376 ascmonths[tm->tm_mon],
381 ( (tm->tm_hour >= 12) ? "pm" : "am" )