2 * tools.c -- Miscellaneous routines used by both the client and server.
15 typedef unsigned char byte; /* Byte type */
16 static byte dtable[256]; /* base64 encode / decode table */
19 char *safestrncpy(char *dest, const char *src, size_t n)
21 if (dest == NULL || src == NULL)
23 fprintf(stderr, "safestrncpy: NULL argument\n");
26 strncpy(dest, src, n);
33 * num_tokens() - discover number of parameters/tokens in a string
35 int num_tokens(char *source, char tok) {
39 for (a=0; a<strlen(source); ++a) {
40 if (source[a]==tok) ++count;
46 * extract_token() - a smarter string tokenizer
48 void extract_token(char *dest, char *source, int parmnum, char separator)
58 if (strlen(source)==0) {
62 for (i=0; i<strlen(source); ++i) {
63 if (source[i]==separator) {
66 else if (curr_parm == parmnum) {
68 dest[len++] = source[i];
76 * remove_token() - a tokenizer that kills, maims, and destroys
78 void remove_token(char *source, int parmnum, char separator)
90 if (strlen(source)==0) {
94 for (i=0; i<strlen(source); ++i) {
95 if ( (start < 0) && (curr_parm == parmnum) ) {
99 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
103 if (source[i]==separator) {
108 if (end < 0) end = strlen(source);
110 printf("%d .. %d\n", start, end);
112 strcpy(&source[start], &source[end]);
119 * extract_int() - extract an int parm w/o supplying a buffer
121 int extract_int(char *source, int parmnum)
125 extract_token(buf, source, parmnum, '|');
130 * extract_long() - extract an long parm w/o supplying a buffer
132 long extract_long(char *source, long int parmnum)
136 extract_token(buf, source, parmnum, '|');
143 * decode_base64() and encode_base64() are adaptations of code by
144 * John Walker, found in full in the file "base64.c" included with this
145 * distribution. The difference between those functions and these is that
146 * these are intended to encode/decode small string buffers, and those are
147 * intended to encode/decode entire MIME parts.
150 void encode_base64(char *dest, char *source)
152 int i, hiteof = FALSE;
156 /* Fill dtable with character encodings. */
158 for (i = 0; i < 26; i++) {
160 dtable[26 + i] = 'a' + i;
162 for (i = 0; i < 10; i++) {
163 dtable[52 + i] = '0' + i;
169 byte igroup[3], ogroup[4];
172 igroup[0] = igroup[1] = igroup[2] = 0;
173 for (n = 0; n < 3; n++) {
179 igroup[n] = (byte) c;
182 ogroup[0] = dtable[igroup[0] >> 2];
183 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
184 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
185 ogroup[3] = dtable[igroup[2] & 0x3F];
187 /* Replace characters in output stream with "=" pad
188 characters if fewer than three characters were
189 read from the end of the input stream. */
197 for (i = 0; i < 4; i++) {
198 dest[dpos++] = ogroup[i];
207 void decode_base64(char *dest, char *source)
213 for (i = 0; i < 255; i++) {
216 for (i = 'A'; i <= 'Z'; i++) {
217 dtable[i] = 0 + (i - 'A');
219 for (i = 'a'; i <= 'z'; i++) {
220 dtable[i] = 26 + (i - 'a');
222 for (i = '0'; i <= '9'; i++) {
223 dtable[i] = 52 + (i - '0');
229 /*CONSTANTCONDITION*/
231 byte a[4], b[4], o[3];
233 for (i = 0; i < 4; i++) {
234 int c = source[spos++];
242 if (dtable[c] & 0x80) {
243 /* Ignoring errors: discard invalid character. */
248 b[i] = (byte) dtable[c];
250 o[0] = (b[0] << 2) | (b[1] >> 4);
251 o[1] = (b[1] << 4) | (b[2] >> 2);
252 o[2] = (b[2] << 6) | b[3];
253 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
254 if (i>=1) dest[dpos++] = o[0];
255 if (i>=2) dest[dpos++] = o[1];
256 if (i>=3) dest[dpos++] = o[2];
267 * Strip leading and trailing spaces from a string
269 void striplt(char *buf)
271 while ((strlen(buf) > 0) && (buf[0] == 32))
272 strcpy(buf, &buf[1]);
273 while (buf[strlen(buf) - 1] == 32)
274 buf[strlen(buf) - 1] = 0;
282 * Return the number of occurances of character ch in string st
284 int haschar(char *st, int ch)
288 for (a = 0; a < strlen(st); ++a)
298 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
300 int collapsed_strcmp(char *s1, char *s2) {
304 c1 = malloc(strlen(s1)+1);
305 c2 = malloc(strlen(s2)+1);
310 for (i=0; i<strlen(s1); ++i) {
311 if (isalnum(s1[i])) {
312 c1[pos] = tolower(s1[i]);
318 for (i=0; i<strlen(s2); ++i) {
319 if (isalnum(s2[i])) {
320 c2[pos] = tolower(s2[i]);
325 ret = strcmp(c1, c2);