Line data Source code
1 : /*
2 : Bacula® - The Network Backup Solution
3 :
4 : Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5 :
6 : The main author of Bacula is Kern Sibbald, with contributions from
7 : many others, a complete list can be found in the file AUTHORS.
8 : This program is Free Software; you can redistribute it and/or
9 : modify it under the terms of version three of the GNU Affero General Public
10 : License as published by the Free Software Foundation and included
11 : in the file LICENSE.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU Affero General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : Bacula® is a registered trademark of Kern Sibbald.
24 : The licensor of Bacula is the Free Software Foundation Europe
25 : (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 : Switzerland, email:ftf@fsfeurope.org.
27 : */
28 : /*
29 : * Generic base 64 input and output routines
30 : *
31 : * Written by Kern E. Sibbald, March MM.
32 : */
33 : /*
34 : * Originally from bacula-5.0.3:src/lib/base64.c, with minor formatting
35 : * changes.
36 : * Graham Keeling, 2014.
37 : */
38 :
39 : #include "burp.h"
40 : #include "base64.h"
41 :
42 : static uint8_t const base64_digits[64]=
43 : {
44 : 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
45 : 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
46 : 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
47 : 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
48 : '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
49 : };
50 :
51 : static uint8_t base64_map[128];
52 :
53 : /* Initialize the Base 64 conversion routines */
54 302 : void base64_init(void)
55 : {
56 : int i;
57 : memset(base64_map, 0, sizeof(base64_map));
58 19630 : for(i=0; i<64; i++)
59 19328 : base64_map[(uint8_t)base64_digits[i]]=i;
60 302 : }
61 :
62 : /*
63 : * Convert a value to base64 characters. The result is stored in where, which
64 : * must be at least 13 bytes long.
65 : *
66 : * Returns the number of characters stored (not including the EOS).
67 : */
68 1690013 : int to_base64(int64_t value, char *where)
69 : {
70 : uint64_t val;
71 1690013 : int i=0;
72 : int n;
73 :
74 : /* Handle negative values */
75 1690013 : if(value<0)
76 : {
77 200630 : where[i++]='-';
78 200630 : value=-value;
79 : }
80 :
81 : /* Determine output size */
82 1690013 : val=value;
83 : do
84 : {
85 6396786 : val>>=6;
86 6396786 : i++;
87 6396786 : } while(val);
88 1690013 : n=i;
89 :
90 : /* Output characters */
91 1690013 : val=value;
92 1690013 : where[i]=0;
93 : do
94 : {
95 6396786 : where[--i]=base64_digits[val & (uint64_t)0x3F];
96 6396786 : val>>=6;
97 6396786 : } while(val);
98 1690013 : return n;
99 : }
100 :
101 : /*
102 : * Convert the Base 64 characters in where to a value.
103 : *
104 : * Returns the number of characters converted.
105 : */
106 1423332 : int from_base64(int64_t *value, const char *where)
107 : {
108 1423332 : uint64_t val=0;
109 1423332 : int i=0;
110 1423332 : int neg=0;
111 :
112 : /* Check if it is negative */
113 1423332 : if(where[i]=='-')
114 : {
115 186038 : i++;
116 186038 : neg=1;
117 : }
118 : /* Construct value */
119 7026322 : for(char c=where[i]; c && c!=' '; c=where[++i])
120 : {
121 5602990 : if(!isalnum((unsigned char)c) && c!='+' && c!='/')
122 27 : continue;
123 5602963 : val<<=6;
124 5602963 : val+=base64_map[(uint8_t)c];
125 : }
126 :
127 1423332 : *value=neg?-(int64_t)val:(int64_t)val;
128 1423332 : return i;
129 : }
130 :
131 4 : uint64_t base64_to_uint64(const char *buf)
132 : {
133 : int64_t val;
134 4 : from_base64(&val, buf);
135 4 : return (uint64_t)val;
136 : }
137 :
138 17055 : void base64_from_uint64(uint64_t src, char *buf)
139 : {
140 17055 : char *p=buf;
141 17055 : p+=to_base64(src, p);
142 17055 : *p=0;
143 17055 : }
|