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)
78 /* Stu. Fixed this to deal with the possibility that somebody's
79 going to send a token bigger than SIZ, which is possible
80 with a bigger input buffer. */
86 if (strlen(source)==0) {
90 for (i=0; i<strlen(source); ++i) {
91 if (source[i]==separator) {
94 else if (curr_parm == parmnum) {
95 if (len < SIZ) { /* stu 2/8/2001 */
97 dest[len++] = source[i];
106 * remove_token() - a tokenizer that kills, maims, and destroys
108 void remove_token(char *source, int parmnum, char separator)
120 if (strlen(source)==0) {
124 for (i=0; i<strlen(source); ++i) {
125 if ( (start < 0) && (curr_parm == parmnum) ) {
129 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
133 if (source[i]==separator) {
138 if (end < 0) end = strlen(source);
140 printf("%d .. %d\n", start, end);
142 strcpy(&source[start], &source[end]);
149 * extract_int() - extract an int parm w/o supplying a buffer
151 int extract_int(char *source, int parmnum)
155 extract_token(buf, source, parmnum, '|');
160 * extract_long() - extract an long parm w/o supplying a buffer
162 long extract_long(char *source, long int parmnum)
166 extract_token(buf, source, parmnum, '|');
173 * decode_base64() and encode_base64() are adaptations of code by
174 * John Walker, found in full in the file "base64.c" included with this
175 * distribution. The difference between those functions and these is that
176 * these are intended to encode/decode small string buffers, and those are
177 * intended to encode/decode entire MIME parts.
180 void encode_base64(char *dest, char *source)
182 int i, hiteof = FALSE;
186 /* Fill dtable with character encodings. */
188 for (i = 0; i < 26; i++) {
190 dtable[26 + i] = 'a' + i;
192 for (i = 0; i < 10; i++) {
193 dtable[52 + i] = '0' + i;
199 byte igroup[3], ogroup[4];
202 igroup[0] = igroup[1] = igroup[2] = 0;
203 for (n = 0; n < 3; n++) {
209 igroup[n] = (byte) c;
212 ogroup[0] = dtable[igroup[0] >> 2];
213 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
214 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
215 ogroup[3] = dtable[igroup[2] & 0x3F];
217 /* Replace characters in output stream with "=" pad
218 characters if fewer than three characters were
219 read from the end of the input stream. */
227 for (i = 0; i < 4; i++) {
228 dest[dpos++] = ogroup[i];
237 void decode_base64(char *dest, char *source)
243 for (i = 0; i < 255; i++) {
246 for (i = 'A'; i <= 'Z'; i++) {
247 dtable[i] = 0 + (i - 'A');
249 for (i = 'a'; i <= 'z'; i++) {
250 dtable[i] = 26 + (i - 'a');
252 for (i = '0'; i <= '9'; i++) {
253 dtable[i] = 52 + (i - '0');
259 /*CONSTANTCONDITION*/
261 byte a[4], b[4], o[3];
263 for (i = 0; i < 4; i++) {
264 int c = source[spos++];
272 if (dtable[c] & 0x80) {
273 /* Ignoring errors: discard invalid character. */
278 b[i] = (byte) dtable[c];
280 o[0] = (b[0] << 2) | (b[1] >> 4);
281 o[1] = (b[1] << 4) | (b[2] >> 2);
282 o[2] = (b[2] << 6) | b[3];
283 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
284 if (i>=1) dest[dpos++] = o[0];
285 if (i>=2) dest[dpos++] = o[1];
286 if (i>=3) dest[dpos++] = o[2];
297 * Strip leading and trailing spaces from a string
299 void striplt(char *buf)
301 while ((strlen(buf) > 0) && (isspace(buf[0])))
302 strcpy(buf, &buf[1]);
303 while (isspace(buf[strlen(buf) - 1]))
304 buf[strlen(buf) - 1] = 0;
312 * Return the number of occurances of character ch in string st
314 int haschar(char *st, int ch)
318 for (a = 0; a < strlen(st); ++a)
328 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
330 int collapsed_strcmp(char *s1, char *s2) {
334 c1 = malloc(strlen(s1)+1);
335 c2 = malloc(strlen(s2)+1);
340 for (i=0; i<strlen(s1); ++i) {
341 if (isalnum(s1[i])) {
342 c1[pos] = tolower(s1[i]);
348 for (i=0; i<strlen(s2); ++i) {
349 if (isalnum(s2[i])) {
350 c2[pos] = tolower(s2[i]);
355 ret = strcmp(c1, c2);
364 * Format a date/time stamp for output
366 void fmt_date(char *buf, time_t thetime) {
370 char *ascmonths[] = {
371 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
372 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
376 tm = localtime(&thetime);
379 if (hour == 0) hour = 12;
380 else if (hour > 12) hour = hour - 12;
382 sprintf(buf, "%s %d %4d %d:%02d%s",
383 ascmonths[tm->tm_mon],
388 ( (tm->tm_hour >= 12) ? "pm" : "am" )