2 * Generic base 64 input and output routines
4 * Written by Kern E. Sibbald, March MM.
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 static char const base64_digits[64] =
39 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
40 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
41 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
42 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
43 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
46 static int base64_inited = 0;
47 static char base64_map[128];
50 /* Initialize the Base 64 conversion routines */
55 memset(base64_map, 0, sizeof(base64_map));
57 base64_map[(int)base64_digits[i]] = i;
61 /* Convert a value to base64 characters.
62 * The result is stored in where, which
63 * must be at least 8 characters long.
65 * Returns the number of characters
66 * stored (not including the EOS).
69 to_base64(intmax_t value, char *where)
75 /* Handle negative values */
81 /* Determine output size */
89 /* Output characters */
93 where[--i] = base64_digits[val & (unsigned)0x3F];
100 * Convert the Base 64 characters in where to
101 * a value. No checking is done on the validity
102 * of the characters!!
107 from_base64(intmax_t *value, char *where)
114 /* Check if it is negative */
116 if (where[i] == '-') {
120 /* Construct value */
121 while (where[i] != 0 && where[i] != ' ') {
123 val += base64_map[(int)where[i++]];
126 *value = neg ? -(intmax_t)val : (intmax_t)val;
130 /* Encode a stat structure into a base64 character string */
132 encode_stat(char *buf, struct stat *statp)
136 * NOTE: we should use rdev as major and minor device if
137 * it is a block or char device (S_ISCHR(statp->st_mode)
138 * or S_ISBLK(statp->st_mode)). In all other cases,
142 p += to_base64((intmax_t)statp->st_dev, p);
143 *p++ = ' '; /* separate fields with a space */
144 p += to_base64((intmax_t)statp->st_ino, p);
146 p += to_base64((intmax_t)statp->st_mode, p);
148 p += to_base64((intmax_t)statp->st_nlink, p);
150 p += to_base64((intmax_t)statp->st_uid, p);
152 p += to_base64((intmax_t)statp->st_gid, p);
154 p += to_base64((intmax_t)statp->st_rdev, p);
156 p += to_base64((intmax_t)statp->st_size, p);
158 p += to_base64((intmax_t)statp->st_blksize, p);
160 p += to_base64((intmax_t)statp->st_blocks, p);
162 p += to_base64((intmax_t)statp->st_atime, p);
164 p += to_base64((intmax_t)statp->st_mtime, p);
166 p += to_base64((intmax_t)statp->st_ctime, p);
172 /* Decode a stat packet from base64 characters */
174 decode_stat(char *buf, struct stat *statp)
179 p += from_base64(&val, p);
181 p++; /* skip space */
182 p += from_base64(&val, p);
185 p += from_base64(&val, p);
186 statp->st_mode = val;
188 p += from_base64(&val, p);
189 statp->st_nlink = val;
191 p += from_base64(&val, p);
194 p += from_base64(&val, p);
197 p += from_base64(&val, p);
198 statp->st_rdev = val;
200 p += from_base64(&val, p);
201 statp->st_size = val;
203 p += from_base64(&val, p);
204 statp->st_blksize = val;
206 p += from_base64(&val, p);
207 statp->st_blocks = val;
209 p += from_base64(&val, p);
210 statp->st_atime = val;
212 p += from_base64(&val, p);
213 statp->st_mtime = val;
215 p += from_base64(&val, p);
216 statp->st_ctime = val;
221 * Encode binary data in bin of len bytes into
222 * buf as base64 characters.
224 * Returns: the number of characters stored not
228 bin_to_base64(char *buf, char *bin, int len)
230 unsigned int reg, save, mask;
244 buf[j++] = base64_digits[reg & (unsigned)0x3F];
250 for (i=1; i<rem; i++) {
251 mask = (mask << 1) | 1;
253 buf[j++] = base64_digits[reg & mask];
260 int main(int argc, char *argv[])
268 for (i=0; i < 100; i++) {
269 bin_to_base64(buf, (char *)&xx, 4);
270 printf("xx=%s\n", buf);
273 len = bin_to_base64(buf, junk, 16);
274 printf("len=%d junk=%s\n", len, buf);
280 static int errfunc(const char *epath, int eernoo)
282 Dmsg0(-1, "in errfunc\n");
288 * Test the base64 routines by encoding and decoding
291 int main(int argc, char *argv[])
301 if (argc > 1 && strcmp(argv[1], "-v") == 0)
307 glob("/etc/*", GLOB_MARK, errfunc, &my_glob);
309 for (i=0; my_glob.gl_pathv[i]; i++) {
310 fname = my_glob.gl_pathv[i];
311 if (lstat(fname, &statp) < 0) {
312 printf("Cannot stat %s: %s\n", fname, strerror(errno));
315 encode_stat(where, &statp);
318 printf("%s: len=%d val=%s\n", fname, strlen(where), where);
320 decode_stat(where, &statn);
322 if (statp.st_dev != statn.st_dev ||
323 statp.st_ino != statn.st_ino ||
324 statp.st_mode != statn.st_mode ||
325 statp.st_nlink != statn.st_nlink ||
326 statp.st_uid != statn.st_uid ||
327 statp.st_gid != statn.st_gid ||
328 statp.st_rdev != statn.st_rdev ||
329 statp.st_size != statn.st_size ||
330 statp.st_blksize != statn.st_blksize ||
331 statp.st_blocks != statn.st_blocks ||
332 statp.st_atime != statn.st_atime ||
333 statp.st_mtime != statn.st_mtime ||
334 statp.st_ctime != statn.st_ctime) {
336 printf("%s: %s\n", fname, where);
337 encode_stat(where, &statn);
338 printf("%s: %s\n", fname, where);
339 printf("NOT EQAL\n");
345 printf("%d files examined\n", i);