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 311 : void base64_init(void)
55 : {
56 : int i;
57 311 : memset(base64_map, 0, sizeof(base64_map));
58 20215 : for(i=0; i<64; i++)
59 19904 : base64_map[(uint8_t)base64_digits[i]]=i;
60 311 : }
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 1716862 : int to_base64(int64_t value, char *where)
69 : {
70 : uint64_t val;
71 1733917 : int i=0;
72 : int n;
73 :
74 : /* Handle negative values */
75 1733917 : if(value<0)
76 : {
77 205624 : where[i++]='-';
78 205624 : value=-value;
79 : }
80 :
81 : /* Determine output size */
82 1733917 : val=value;
83 : do
84 : {
85 6437935 : val>>=6;
86 6437935 : i++;
87 6437935 : } while(val);
88 1733917 : n=i;
89 :
90 : /* Output characters */
91 1733917 : val=value;
92 1733917 : where[i]=0;
93 : do
94 : {
95 6437935 : where[--i]=base64_digits[val & (uint64_t)0x3F];
96 6437935 : val>>=6;
97 6437935 : } while(val);
98 1716862 : 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 1423437 : int from_base64(int64_t *value, const char *where)
107 : {
108 1423437 : uint64_t val=0;
109 1423437 : int i=0;
110 1423437 : int neg=0;
111 :
112 1423437 : if(where[i]==' ')
113 1372662 : i++;
114 :
115 : /* Check if it is negative */
116 1423437 : if(where[i]=='-')
117 : {
118 191031 : i++;
119 191031 : neg=1;
120 : }
121 : /* Construct value */
122 7071223 : for(char c=where[i]; c && c!=' '; c=where[++i])
123 : {
124 5647786 : if(!isalnum((unsigned char)c) && c!='+' && c!='/')
125 6 : continue;
126 5647780 : val<<=6;
127 5647780 : val+=base64_map[(uint8_t)c];
128 : }
129 :
130 1423437 : *value=neg?-(int64_t)val:(int64_t)val;
131 1423437 : return i;
132 : }
133 :
134 4 : uint64_t base64_to_uint64(const char *buf)
135 : {
136 4 : int64_t val=0;
137 4 : from_base64(&val, buf);
138 4 : return (uint64_t)val;
139 : }
140 :
141 17055 : void base64_from_uint64(uint64_t src, char *buf)
142 : {
143 17055 : char *p=buf;
144 34110 : p+=to_base64(src, p);
145 17055 : *p=0;
146 17055 : }
|