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 301 : void base64_init(void)
55 : {
56 : int i;
57 : memset(base64_map, 0, sizeof(base64_map));
58 19565 : for(i=0; i<64; i++)
59 19264 : base64_map[(uint8_t)base64_digits[i]]=i;
60 301 : }
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 1694477 : int to_base64(int64_t value, char *where)
69 : {
70 : uint64_t val;
71 1694477 : int i=0;
72 : int n;
73 :
74 : /* Handle negative values */
75 1694477 : if(value<0)
76 : {
77 200410 : where[i++]='-';
78 200410 : value=-value;
79 : }
80 :
81 : /* Determine output size */
82 1694477 : val=value;
83 6406272 : do
84 : {
85 6406272 : val>>=6;
86 6406272 : i++;
87 : } while(val);
88 1694477 : n=i;
89 :
90 : /* Output characters */
91 1694477 : val=value;
92 1694477 : where[i]=0;
93 6406272 : do
94 : {
95 6406272 : where[--i]=base64_digits[val & (uint64_t)0x3F];
96 6406272 : val>>=6;
97 : } while(val);
98 1694477 : 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 1419772 : int from_base64(int64_t *value, const char *where)
107 : {
108 1419772 : uint64_t val=0;
109 1419772 : int i=0;
110 1419772 : int neg=0;
111 :
112 : /* Check if it is negative */
113 1419772 : if(where[i]=='-')
114 : {
115 185818 : i++;
116 185818 : neg=1;
117 : }
118 : /* Construct value */
119 7013939 : for(char c=where[i]; c && c!=' '; c=where[++i])
120 : {
121 11188334 : if(!isalnum((unsigned char)c) && c!='+' && c!='/')
122 : continue;
123 5594119 : val<<=6;
124 5594119 : val+=base64_map[(uint8_t)c];
125 : }
126 :
127 1419772 : *value=neg?-(int64_t)val:(int64_t)val;
128 1419772 : 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 25079 : void base64_from_uint64(uint64_t src, char *buf)
139 : {
140 25079 : char *p=buf;
141 25079 : p+=to_base64(src, p);
142 25079 : *p=0;
143 25079 : }
|