2 * Generic base 64 input and output routines
4 * Written by Kern E. Sibbald, March MM.
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 static char const base64_digits[64] =
37 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
38 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
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 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
44 static int base64_inited = 0;
45 static char base64_map[128];
48 /* Initialize the Base 64 conversion routines */
53 memset(base64_map, 0, sizeof(base64_map));
55 base64_map[(int)base64_digits[i]] = i;
59 /* Convert a value to base64 characters.
60 * The result is stored in where, which
61 * must be at least 8 characters long.
63 * Returns the number of characters
64 * stored (not including the EOS).
67 to_base64(intmax_t value, char *where)
73 /* Handle negative values */
79 /* Determine output size */
87 /* Output characters */
91 where[--i] = base64_digits[val & (unsigned)0x3F];
98 * Convert the Base 64 characters in where to
99 * a value. No checking is done on the validity
100 * of the characters!!
105 from_base64(intmax_t *value, char *where)
112 /* Check if it is negative */
114 if (where[i] == '-') {
118 /* Construct value */
119 while (where[i] != 0 && where[i] != ' ') {
121 val += base64_map[(int)where[i++]];
124 *value = neg ? -(intmax_t)val : (intmax_t)val;
128 /* Encode a stat structure into a base64 character string */
130 encode_stat(char *buf, struct stat *statp)
134 * NOTE: we should use rdev as major and minor device if
135 * it is a block or char device (S_ISCHR(statp->st_mode)
136 * or S_ISBLK(statp->st_mode)). In all other cases,
140 p += to_base64((intmax_t)statp->st_dev, p);
141 *p++ = ' '; /* separate fields with a space */
142 p += to_base64((intmax_t)statp->st_ino, p);
144 p += to_base64((intmax_t)statp->st_mode, p);
146 p += to_base64((intmax_t)statp->st_nlink, p);
148 p += to_base64((intmax_t)statp->st_uid, p);
150 p += to_base64((intmax_t)statp->st_gid, p);
152 p += to_base64((intmax_t)statp->st_rdev, p);
154 p += to_base64((intmax_t)statp->st_size, p);
156 p += to_base64((intmax_t)statp->st_blksize, p);
158 p += to_base64((intmax_t)statp->st_blocks, p);
160 p += to_base64((intmax_t)statp->st_atime, p);
162 p += to_base64((intmax_t)statp->st_mtime, p);
164 p += to_base64((intmax_t)statp->st_ctime, p);
170 /* Decode a stat packet from base64 characters */
172 decode_stat(char *buf, struct stat *statp)
177 p += from_base64(&val, p);
179 p++; /* skip space */
180 p += from_base64(&val, p);
183 p += from_base64(&val, p);
184 statp->st_mode = val;
186 p += from_base64(&val, p);
187 statp->st_nlink = val;
189 p += from_base64(&val, p);
192 p += from_base64(&val, p);
195 p += from_base64(&val, p);
196 statp->st_rdev = val;
198 p += from_base64(&val, p);
199 statp->st_size = val;
201 p += from_base64(&val, p);
202 statp->st_blksize = val;
204 p += from_base64(&val, p);
205 statp->st_blocks = val;
207 p += from_base64(&val, p);
208 statp->st_atime = val;
210 p += from_base64(&val, p);
211 statp->st_mtime = val;
213 p += from_base64(&val, p);
214 statp->st_ctime = val;
219 * Encode binary data in bin of len bytes into
220 * buf as base64 characters.
222 * Returns: the number of characters stored not
226 bin_to_base64(char *buf, char *bin, int len)
228 unsigned int reg, save, mask;
242 buf[j++] = base64_digits[reg & (unsigned)0x3F];
248 for (i=1; i<rem; i++) {
249 mask = (mask << 1) | 1;
251 buf[j++] = base64_digits[reg & mask];
258 int main(int argc, char *argv[])
266 for (i=0; i < 100; i++) {
267 bin_to_base64(buf, (char *)&xx, 4);
268 printf("xx=%s\n", buf);
271 len = bin_to_base64(buf, junk, 16);
272 printf("len=%d junk=%s\n", len, buf);
278 static int errfunc(const char *epath, int eernoo)
280 Dmsg0(-1, "in errfunc\n");
286 * Test the base64 routines by encoding and decoding
289 int main(int argc, char *argv[])
299 if (argc > 1 && strcmp(argv[1], "-v") == 0)
305 glob("/etc/*", GLOB_MARK, errfunc, &my_glob);
307 for (i=0; my_glob.gl_pathv[i]; i++) {
308 fname = my_glob.gl_pathv[i];
309 if (lstat(fname, &statp) < 0) {
310 printf("Cannot stat %s: %s\n", fname, strerror(errno));
313 encode_stat(where, &statp);
316 printf("%s: len=%d val=%s\n", fname, strlen(where), where);
318 decode_stat(where, &statn);
320 if (statp.st_dev != statn.st_dev ||
321 statp.st_ino != statn.st_ino ||
322 statp.st_mode != statn.st_mode ||
323 statp.st_nlink != statn.st_nlink ||
324 statp.st_uid != statn.st_uid ||
325 statp.st_gid != statn.st_gid ||
326 statp.st_rdev != statn.st_rdev ||
327 statp.st_size != statn.st_size ||
328 statp.st_blksize != statn.st_blksize ||
329 statp.st_blocks != statn.st_blocks ||
330 statp.st_atime != statn.st_atime ||
331 statp.st_mtime != statn.st_mtime ||
332 statp.st_ctime != statn.st_ctime) {
334 printf("%s: %s\n", fname, where);
335 encode_stat(where, &statn);
336 printf("%s: %s\n", fname, where);
337 printf("NOT EQAL\n");
343 printf("%d files examined\n", i);