2 * tools.c -- Miscellaneous routines used by both the client and server.
14 typedef unsigned char byte; /* Byte type */
15 static byte dtable[256]; /* base64 encode / decode table */
18 char *safestrncpy(char *dest, const char *src, size_t n)
20 if (dest == NULL || src == NULL)
22 fprintf(stderr, "safestrncpy: NULL argument\n");
25 strncpy(dest, src, n);
32 * num_parms() - discover number of parameters...
34 int num_parms(char *source)
39 for (a=0; a<strlen(source); ++a)
40 if (source[a]=='|') ++count;
45 * extract() - a smarter string tokenizer
47 void extract_token(char *dest, char *source, int parmnum, char separator)
57 if (strlen(source)==0) {
61 for (i=0; i<strlen(source); ++i) {
62 if (source[i]==separator) {
65 else if (curr_parm == parmnum) {
67 dest[len++] = source[i];
73 * extract_int() - extract an int parm w/o supplying a buffer
75 int extract_int(char *source, int parmnum)
79 extract_token(buf, source, parmnum, '|');
84 * extract_long() - extract an long parm w/o supplying a buffer
86 long extract_long(char *source, long int parmnum)
90 extract_token(buf, source, parmnum, '|');
97 * decode_base64() and encode_base64() are adaptations of code by
98 * John Walker, found in full in the file "base64.c" included with this
99 * distribution. The difference between those functions and these is that
100 * these are intended to encode/decode small string buffers, and those are
101 * intended to encode/decode entire MIME parts.
104 void encode_base64(char *dest, char *source)
106 int i, hiteof = FALSE;
110 /* Fill dtable with character encodings. */
112 for (i = 0; i < 26; i++) {
114 dtable[26 + i] = 'a' + i;
116 for (i = 0; i < 10; i++) {
117 dtable[52 + i] = '0' + i;
123 byte igroup[3], ogroup[4];
126 igroup[0] = igroup[1] = igroup[2] = 0;
127 for (n = 0; n < 3; n++) {
133 igroup[n] = (byte) c;
136 ogroup[0] = dtable[igroup[0] >> 2];
137 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
138 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
139 ogroup[3] = dtable[igroup[2] & 0x3F];
141 /* Replace characters in output stream with "=" pad
142 characters if fewer than three characters were
143 read from the end of the input stream. */
151 for (i = 0; i < 4; i++) {
152 dest[dpos++] = ogroup[i];
161 void decode_base64(char *dest, char *source)
167 for (i = 0; i < 255; i++) {
170 for (i = 'A'; i <= 'Z'; i++) {
171 dtable[i] = 0 + (i - 'A');
173 for (i = 'a'; i <= 'z'; i++) {
174 dtable[i] = 26 + (i - 'a');
176 for (i = '0'; i <= '9'; i++) {
177 dtable[i] = 52 + (i - '0');
183 /*CONSTANTCONDITION*/
185 byte a[4], b[4], o[3];
187 for (i = 0; i < 4; i++) {
188 int c = source[spos++];
196 if (dtable[c] & 0x80) {
197 /* Ignoring errors: discard invalid character. */
202 b[i] = (byte) dtable[c];
204 o[0] = (b[0] << 2) | (b[1] >> 4);
205 o[1] = (b[1] << 4) | (b[2] >> 2);
206 o[2] = (b[2] << 6) | b[3];
207 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
208 if (i>=1) dest[dpos++] = o[0];
209 if (i>=2) dest[dpos++] = o[1];
210 if (i>=3) dest[dpos++] = o[2];