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_parms() - discover number of parameters...
35 int num_parms(char *source)
40 for (a=0; a<strlen(source); ++a)
41 if (source[a]=='|') ++count;
46 * extract() - 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];
74 * extract_int() - extract an int parm w/o supplying a buffer
76 int extract_int(char *source, int parmnum)
80 extract_token(buf, source, parmnum, '|');
85 * extract_long() - extract an long parm w/o supplying a buffer
87 long extract_long(char *source, long int parmnum)
91 extract_token(buf, source, parmnum, '|');
98 * decode_base64() and encode_base64() are adaptations of code by
99 * John Walker, found in full in the file "base64.c" included with this
100 * distribution. The difference between those functions and these is that
101 * these are intended to encode/decode small string buffers, and those are
102 * intended to encode/decode entire MIME parts.
105 void encode_base64(char *dest, char *source)
107 int i, hiteof = FALSE;
111 /* Fill dtable with character encodings. */
113 for (i = 0; i < 26; i++) {
115 dtable[26 + i] = 'a' + i;
117 for (i = 0; i < 10; i++) {
118 dtable[52 + i] = '0' + i;
124 byte igroup[3], ogroup[4];
127 igroup[0] = igroup[1] = igroup[2] = 0;
128 for (n = 0; n < 3; n++) {
134 igroup[n] = (byte) c;
137 ogroup[0] = dtable[igroup[0] >> 2];
138 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
139 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
140 ogroup[3] = dtable[igroup[2] & 0x3F];
142 /* Replace characters in output stream with "=" pad
143 characters if fewer than three characters were
144 read from the end of the input stream. */
152 for (i = 0; i < 4; i++) {
153 dest[dpos++] = ogroup[i];
162 void decode_base64(char *dest, char *source)
168 for (i = 0; i < 255; i++) {
171 for (i = 'A'; i <= 'Z'; i++) {
172 dtable[i] = 0 + (i - 'A');
174 for (i = 'a'; i <= 'z'; i++) {
175 dtable[i] = 26 + (i - 'a');
177 for (i = '0'; i <= '9'; i++) {
178 dtable[i] = 52 + (i - '0');
184 /*CONSTANTCONDITION*/
186 byte a[4], b[4], o[3];
188 for (i = 0; i < 4; i++) {
189 int c = source[spos++];
197 if (dtable[c] & 0x80) {
198 /* Ignoring errors: discard invalid character. */
203 b[i] = (byte) dtable[c];
205 o[0] = (b[0] << 2) | (b[1] >> 4);
206 o[1] = (b[1] << 4) | (b[2] >> 2);
207 o[2] = (b[2] << 6) | b[3];
208 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
209 if (i>=1) dest[dpos++] = o[0];
210 if (i>=2) dest[dpos++] = o[1];
211 if (i>=3) dest[dpos++] = o[2];
222 * Strip leading and trailing spaces from a string
224 void striplt(char *buf)
226 while ((strlen(buf) > 0) && (buf[0] == 32))
227 strcpy(buf, &buf[1]);
228 while (buf[strlen(buf) - 1] == 32)
229 buf[strlen(buf) - 1] = 0;
237 * Return the number of occurances of character ch in string st
239 int haschar(char *st, int ch)
243 for (a = 0; a < strlen(st); ++a)
253 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
255 int collapsed_strcmp(char *s1, char *s2) {
262 for (i=0; i<strlen(c1); ++i) {
263 while (!isalnum(c1[i])) strcpy(&c1[i], &c1[i+1]);
264 if (isupper(c1[i])) c1[i]=tolower(c1[i]);
267 for (i=0; i<strlen(c2); ++i) {
268 while (!isalnum(c2[i])) strcpy(&c2[i], &c2[i+1]);
269 if (isupper(c2[i])) c2[i]=tolower(c2[i]);
272 ret = strcmp(c1, c2);