]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/base64.c
Add jcr to BSOCK and BDB SpoolAttr NoAttributs finish up multiple simultaneous jobs...
[bacula/bacula] / bacula / src / lib / base64.c
1 /*   
2  *   Generic base 64 input and output routines
3  *
4  *    Written by Kern E. Sibbald, March MM.
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29
30 #include "bacula.h"
31
32 #ifdef TEST_MODE
33 #include <glob.h>
34 #endif
35
36
37 static uint8_t const base64_digits[64] =
38 {
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', '+', '/'
44 };
45
46 static int base64_inited = 0;
47 static uint8_t base64_map[128];
48   
49
50 /* Initialize the Base 64 conversion routines */
51 void
52 base64_init(void)
53 {     
54    int i; 
55    memset(base64_map, 0, sizeof(base64_map));
56    for (i=0; i<64; i++)
57       base64_map[(uint8_t)base64_digits[i]] = i;
58    base64_inited = 1;
59 }
60
61 /* Convert a value to base64 characters.
62  * The result is stored in where, which
63  * must be at least 8 characters long.
64  *
65  * Returns the number of characters
66  * stored (not including the EOS).
67  */
68 int
69 to_base64(intmax_t value, char *where)
70 {
71    uintmax_t val;
72    int i = 0;
73    int n;
74
75    /* Handle negative values */
76    if (value < 0) {
77       where[i++] = '-';
78       value = -value;
79    }
80
81    /* Determine output size */
82    val = value;
83    do {
84       val >>= 6;
85       i++;
86    } while (val);
87    n = i;
88
89    /* Output characters */
90    val = value;
91    where[i] = 0;
92    do {
93       where[--i] = base64_digits[val & (uintmax_t)0x3F];
94       val >>= 6;
95    } while (val);
96    return n;
97 }
98
99 /*
100  * Convert the Base 64 characters in where to
101  * a value. No checking is done on the validity
102  * of the characters!!
103  *
104  * Returns the value.
105  */
106 int
107 from_base64(intmax_t *value, char *where)
108
109    uintmax_t val = 0;
110    int i, neg;
111
112    if (!base64_inited) 
113       base64_init();
114    /* Check if it is negative */
115    i = neg = 0;
116    if (where[i] == '-') {
117       i++;
118       neg = 1;
119    }
120    /* Construct value */
121    while (where[i] != 0 && where[i] != ' ') {
122       val <<= 6;
123       val += base64_map[(uint8_t)where[i++]];
124    }
125          
126    *value = neg ? -(intmax_t)val : (intmax_t)val;
127    return i;
128 }
129
130 /* Encode a stat structure into a base64 character string */
131 void
132 encode_stat(char *buf, struct stat *statp)
133 {
134    char *p = buf;
135    /*
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,
139     * it is not used.   
140     *
141     */
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);
145    *p++ = ' ';
146    p += to_base64((intmax_t)statp->st_mode, p);
147    *p++ = ' ';
148    p += to_base64((intmax_t)statp->st_nlink, p);
149    *p++ = ' ';
150    p += to_base64((intmax_t)statp->st_uid, p);
151    *p++ = ' ';
152    p += to_base64((intmax_t)statp->st_gid, p);
153    *p++ = ' ';
154    p += to_base64((intmax_t)statp->st_rdev, p);
155    *p++ = ' ';
156    p += to_base64((intmax_t)statp->st_size, p);
157    *p++ = ' ';
158    p += to_base64((intmax_t)statp->st_blksize, p);
159    *p++ = ' ';
160    p += to_base64((intmax_t)statp->st_blocks, p);
161    *p++ = ' ';
162    p += to_base64((intmax_t)statp->st_atime, p);
163    *p++ = ' ';
164    p += to_base64((intmax_t)statp->st_mtime, p);
165    *p++ = ' ';
166    p += to_base64((intmax_t)statp->st_ctime, p);
167    *p++ = 0;
168    return;
169 }
170
171
172 /* Decode a stat packet from base64 characters */
173 void
174 decode_stat(char *buf, struct stat *statp)
175 {
176    char *p = buf;
177    intmax_t val;
178
179    p += from_base64(&val, p);
180    statp->st_dev = val;
181    p++;                               /* skip space */
182    p += from_base64(&val, p);
183    statp->st_ino = val;
184    p++;
185    p += from_base64(&val, p);
186    statp->st_mode = val;
187    p++;
188    p += from_base64(&val, p);
189    statp->st_nlink = val;
190    p++;
191    p += from_base64(&val, p);
192    statp->st_uid = val;
193    p++;
194    p += from_base64(&val, p);
195    statp->st_gid = val;
196    p++;
197    p += from_base64(&val, p);
198    statp->st_rdev = val;
199    p++;
200    p += from_base64(&val, p);
201    statp->st_size = val;
202    p++;
203    p += from_base64(&val, p);
204    statp->st_blksize = val;
205    p++;
206    p += from_base64(&val, p);
207    statp->st_blocks = val;
208    p++;
209    p += from_base64(&val, p);
210    statp->st_atime = val;
211    p++;
212    p += from_base64(&val, p);
213    statp->st_mtime = val;
214    p++;
215    p += from_base64(&val, p);
216    statp->st_ctime = val;
217    p++;
218 }
219
220 /*
221  * Encode binary data in bin of len bytes into
222  * buf as base64 characters.
223  *
224  *  Returns: the number of characters stored not
225  *           including the EOS
226  */
227 int
228 bin_to_base64(char *buf, char *bin, int len)
229 {
230    uint32_t reg, save, mask;
231    int rem, i;
232    int j = 0;
233
234    reg = 0;
235    rem = 0;
236    for (i=0; i<len; ) {
237       if (rem < 6) {
238          reg <<= 8;
239          reg |= (int8_t)bin[i++];
240          rem += 8;
241       }
242       save = reg;
243       reg >>= (rem - 6);
244       buf[j++] = base64_digits[reg & (uint32_t)0x3F];
245       reg = save;
246       rem -= 6;
247    }
248    if (rem) {
249       mask = 1;
250       for (i=1; i<rem; i++) {
251          mask = (mask << 1) | 1;
252       }
253       buf[j++] = base64_digits[reg & mask];
254    }
255    buf[j] = 0;
256    return j;
257 }
258
259 #ifdef BIN_TEST
260 int main(int argc, char *argv[])
261 {
262    int xx = 0;
263    int len;
264    char buf[100];
265    char junk[100];
266    int i;
267
268 #ifdef xxxx
269    for (i=0; i < 1000; i++) {
270       bin_to_base64(buf, (char *)&xx, 4);
271       printf("xx=%s\n", buf);
272       xx++;
273    }
274 #endif
275    junk[0] = 0xFF;
276    for (i=1; i<100; i++) {
277       junk[i] = junk[i-1]-1;
278    }
279    len = bin_to_base64(buf, junk, 16);
280    printf("len=%d junk=%s\n", len, buf);
281    return 0;
282 }
283 #endif
284
285 #ifdef TEST_MODE
286 static int errfunc(const char *epath, int eernoo)
287 {
288   Dmsg0(-1, "in errfunc\n");
289   return 1;
290 }
291
292
293 /*
294  * Test the base64 routines by encoding and decoding
295  * lstat() packets.
296  */
297 int main(int argc, char *argv[]) 
298 {
299    char where[500];
300    int i;
301    glob_t my_glob;
302    char *fname;
303    struct stat statp;
304    struct stat statn;
305    int debug_level = 0;
306    char *p;
307    time_t t = 1028712799;
308
309    if (argc > 1 && strcmp(argv[1], "-v") == 0)
310       debug_level++;  
311
312    base64_init();
313
314    my_glob.gl_offs = 0;
315    glob("/etc/grub.conf", GLOB_MARK, errfunc, &my_glob);
316
317    for (i=0; my_glob.gl_pathv[i]; i++) {
318       fname = my_glob.gl_pathv[i];
319       if (lstat(fname, &statp) < 0) {
320          printf("Cannot stat %s: %s\n", fname, strerror(errno));
321          continue;
322       }
323       encode_stat(where, &statp);
324       p = where;
325       p += to_base64((intmax_t)(statp.st_atime), p);
326       *p++ = ' ';
327       p += to_base64((intmax_t)t, p);
328       printf("%s %s\n", fname, where);
329
330 #ifdef xxxx
331       printf("%s %lld\n", "st_dev", (intmax_t)statp.st_dev);
332       printf("%s %lld\n", "st_ino", (intmax_t)statp.st_ino);
333       printf("%s %lld\n", "st_mode", (intmax_t)statp.st_mode);
334       printf("%s %lld\n", "st_nlink", (intmax_t)statp.st_nlink);
335       printf("%s %lld\n", "st_uid", (intmax_t)statp.st_uid);
336       printf("%s %lld\n", "st_gid", (intmax_t)statp.st_gid);
337       printf("%s %lld\n", "st_rdev", (intmax_t)statp.st_rdev);
338       printf("%s %lld\n", "st_size", (intmax_t)statp.st_size);
339       printf("%s %lld\n", "st_blksize", (intmax_t)statp.st_blksize);
340       printf("%s %lld\n", "st_blocks", (intmax_t)statp.st_blocks);
341       printf("%s %lld\n", "st_atime", (intmax_t)statp.st_atime);
342       printf("%s %lld\n", "st_mtime", (intmax_t)statp.st_mtime);
343       printf("%s %lld\n", "st_ctime", (intmax_t)statp.st_ctime);
344 #endif
345
346
347       if (debug_level)
348          printf("%s: len=%d val=%s\n", fname, strlen(where), where);
349       
350       decode_stat(where, &statn);
351
352 #ifdef xxx
353       if (statp.st_dev != statn.st_dev || 
354           statp.st_ino != statn.st_ino ||
355           statp.st_mode != statn.st_mode ||
356           statp.st_nlink != statn.st_nlink ||
357           statp.st_uid != statn.st_uid ||
358           statp.st_gid != statn.st_gid ||
359           statp.st_rdev != statn.st_rdev ||
360           statp.st_size != statn.st_size ||
361           statp.st_blksize != statn.st_blksize ||
362           statp.st_blocks != statn.st_blocks ||
363           statp.st_atime != statn.st_atime ||
364           statp.st_mtime != statn.st_mtime ||
365           statp.st_ctime != statn.st_ctime) {
366
367          printf("%s: %s\n", fname, where);
368          encode_stat(where, &statn);
369          printf("%s: %s\n", fname, where);
370          printf("NOT EQAL\n");
371       }
372 #endif
373
374    }
375    globfree(&my_glob);
376
377    printf("%d files examined\n", i);
378
379    return 0;
380 }   
381 #endif