]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/base64.c
Use the command line utility dropdb instead of the psql command
[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
131 /*
132  * Encode binary data in bin of len bytes into
133  * buf as base64 characters.
134  *
135  *  Returns: the number of characters stored not
136  *           including the EOS
137  */
138 int
139 bin_to_base64(char *buf, char *bin, int len)
140 {
141    uint32_t reg, save, mask;
142    int rem, i;
143    int j = 0;
144
145    reg = 0;
146    rem = 0;
147    for (i=0; i<len; ) {
148       if (rem < 6) {
149          reg <<= 8;
150          reg |= (int8_t)bin[i++];
151          rem += 8;
152       }
153       save = reg;
154       reg >>= (rem - 6);
155       buf[j++] = base64_digits[reg & (uint32_t)0x3F];
156       reg = save;
157       rem -= 6;
158    }
159    if (rem) {
160       mask = 1;
161       for (i=1; i<rem; i++) {
162          mask = (mask << 1) | 1;
163       }
164       buf[j++] = base64_digits[reg & mask];
165    }
166    buf[j] = 0;
167    return j;
168 }
169
170 #ifdef BIN_TEST
171 int main(int argc, char *argv[])
172 {
173    int xx = 0;
174    int len;
175    char buf[100];
176    char junk[100];
177    int i;
178
179 #ifdef xxxx
180    for (i=0; i < 1000; i++) {
181       bin_to_base64(buf, (char *)&xx, 4);
182       printf("xx=%s\n", buf);
183       xx++;
184    }
185 #endif
186    junk[0] = 0xFF;
187    for (i=1; i<100; i++) {
188       junk[i] = junk[i-1]-1;
189    }
190    len = bin_to_base64(buf, junk, 16);
191    printf("len=%d junk=%s\n", len, buf);
192    return 0;
193 }
194 #endif
195
196 #ifdef TEST_MODE
197 static int errfunc(const char *epath, int eernoo)
198 {
199   printf("in errfunc\n");
200   return 1;
201 }
202
203
204 /*
205  * Test the base64 routines by encoding and decoding
206  * lstat() packets.
207  */
208 int main(int argc, char *argv[]) 
209 {
210    char where[500];
211    int i;
212    glob_t my_glob;
213    char *fname;
214    struct stat statp;
215    struct stat statn;
216    int debug_level = 0;
217    char *p;
218    time_t t = 1028712799;
219
220    if (argc > 1 && strcmp(argv[1], "-v") == 0)
221       debug_level++;  
222
223    base64_init();
224
225    my_glob.gl_offs = 0;
226    glob("/etc/grub.conf", GLOB_MARK, errfunc, &my_glob);
227
228    for (i=0; my_glob.gl_pathv[i]; i++) {
229       fname = my_glob.gl_pathv[i];
230       if (lstat(fname, &statp) < 0) {
231          printf("Cannot stat %s: %s\n", fname, strerror(errno));
232          continue;
233       }
234       encode_stat(where, &statp);
235
236       printf("Encoded stat=%s\n", where);
237      
238 #ifdef xxx
239       p = where;
240       p += to_base64((intmax_t)(statp.st_atime), p);
241       *p++ = ' ';
242       p += to_base64((intmax_t)t, p);
243       printf("%s %s\n", fname, where);
244
245       printf("%s %lld\n", "st_dev", (intmax_t)statp.st_dev);
246       printf("%s %lld\n", "st_ino", (intmax_t)statp.st_ino);
247       printf("%s %lld\n", "st_mode", (intmax_t)statp.st_mode);
248       printf("%s %lld\n", "st_nlink", (intmax_t)statp.st_nlink);
249       printf("%s %lld\n", "st_uid", (intmax_t)statp.st_uid);
250       printf("%s %lld\n", "st_gid", (intmax_t)statp.st_gid);
251       printf("%s %lld\n", "st_rdev", (intmax_t)statp.st_rdev);
252       printf("%s %lld\n", "st_size", (intmax_t)statp.st_size);
253       printf("%s %lld\n", "st_blksize", (intmax_t)statp.st_blksize);
254       printf("%s %lld\n", "st_blocks", (intmax_t)statp.st_blocks);
255       printf("%s %lld\n", "st_atime", (intmax_t)statp.st_atime);
256       printf("%s %lld\n", "st_mtime", (intmax_t)statp.st_mtime);
257       printf("%s %lld\n", "st_ctime", (intmax_t)statp.st_ctime);
258 #endif
259
260       if (debug_level)
261          printf("%s: len=%d val=%s\n", fname, strlen(where), where);
262       
263       decode_stat(where, &statn);
264
265       if (statp.st_dev != statn.st_dev || 
266           statp.st_ino != statn.st_ino ||
267           statp.st_mode != statn.st_mode ||
268           statp.st_nlink != statn.st_nlink ||
269           statp.st_uid != statn.st_uid ||
270           statp.st_gid != statn.st_gid ||
271           statp.st_rdev != statn.st_rdev ||
272           statp.st_size != statn.st_size ||
273           statp.st_blksize != statn.st_blksize ||
274           statp.st_blocks != statn.st_blocks ||
275           statp.st_atime != statn.st_atime ||
276           statp.st_mtime != statn.st_mtime ||
277           statp.st_ctime != statn.st_ctime) {
278
279          printf("%s: %s\n", fname, where);
280          encode_stat(where, &statn);
281          printf("%s: %s\n", fname, where);
282          printf("NOT EQAL\n");
283       }
284
285    }
286    globfree(&my_glob);
287
288    printf("%d files examined\n", i);
289
290    to_base64(UINT32_MAX, where);
291    printf("UINT32_MAX=%s\n", where);
292
293    return 0;
294 }   
295 #endif