2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2006
9 Copyright (C) Jeremy Allison 1992-2007
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "lib/util/base64.h"
28 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm.
33 * A DATA_BLOB like {.data = NULL, length = 0} indicates memory error.
35 * Decoding stops at the first invalid character.
37 _PUBLIC_ DATA_BLOB
base64_decode_data_blob_talloc(TALLOC_CTX
*mem_ctx
, const char *s
)
39 int bit_offset
, byte_offset
, idx
, i
, n
;
40 DATA_BLOB decoded
= data_blob_talloc(mem_ctx
, s
, strlen(s
)+1);
41 unsigned char *d
= decoded
.data
;
44 if (decoded
.data
== NULL
) {
51 while (*s
&& (p
=strchr(b64
,*s
))) {
53 byte_offset
= (i
*6)/8;
55 d
[byte_offset
] &= ~((1<<(8-bit_offset
))-1);
57 d
[byte_offset
] |= (idx
<< (2-bit_offset
));
60 d
[byte_offset
] |= (idx
>> (bit_offset
-2));
61 d
[byte_offset
+1] = (idx
<< (8-(bit_offset
-2))) & 0xFF;
67 if ((n
> 0) && (*s
== '=')) {
73 decoded
.data
= talloc_realloc(mem_ctx
, decoded
.data
, uint8_t, n
);
78 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
80 _PUBLIC_ DATA_BLOB
base64_decode_data_blob(const char *s
)
82 return base64_decode_data_blob_talloc(NULL
, s
);
86 * Decode a base64 string in-place - wrapper for the above
88 _PUBLIC_
void base64_decode_inplace(char *s
)
90 DATA_BLOB decoded
= base64_decode_data_blob(s
);
92 if ( decoded
.length
!= 0 ) {
93 memcpy(s
, decoded
.data
, decoded
.length
);
96 s
[decoded
.length
] = '\0';
101 data_blob_free(&decoded
);
105 * Encode a base64 string into a talloc()ed string caller to free.
107 * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
111 _PUBLIC_
char *base64_encode_data_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
)
115 size_t out_cnt
, len
, output_len
;
119 * Note: we return NULL for a zero-length blob, even though it can be
120 * encoded as a zero length string in base64.
122 * FIXME, perhaps, but we need to check carefully before changing
125 if (data
.length
== 0 || data
.data
== NULL
) {
131 output_len
= data
.length
* 2 + 4; /* Account for closing bytes. 4 is
132 * random but should be enough for
134 result
= talloc_array(mem_ctx
, char, output_len
); /* get us plenty of space */
135 if (result
== NULL
) {
140 int c
= (unsigned char) *(data
.data
++);
143 if (char_count
== 3) {
144 result
[out_cnt
++] = b64
[bits
>> 18];
145 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
146 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
147 result
[out_cnt
++] = b64
[bits
& 0x3f];
154 if (char_count
!= 0) {
155 bits
<<= 16 - (8 * char_count
);
156 result
[out_cnt
++] = b64
[bits
>> 18];
157 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
158 if (char_count
== 1) {
159 result
[out_cnt
++] = '=';
160 result
[out_cnt
++] = '=';
162 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
163 result
[out_cnt
++] = '=';
166 result
[out_cnt
] = '\0'; /* terminate */