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