s3:libsmb: use cli_state_remote_sockaddr() in smb2cli_tcon_send()
[Samba/gebeck_regimport.git] / lib / util / base64.c
blob19ce2d1b850f23ec38b0e251f1d607cef3d9cbff
1 /*
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/>.
25 #include "includes.h"
27 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 /**
30 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
31 **/
32 _PUBLIC_ DATA_BLOB base64_decode_data_blob(const char *s)
34 int bit_offset, byte_offset, idx, i, n;
35 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
36 unsigned char *d = decoded.data;
37 char *p;
39 n=i=0;
41 while (*s && (p=strchr_m(b64,*s))) {
42 idx = (int)(p - b64);
43 byte_offset = (i*6)/8;
44 bit_offset = (i*6)%8;
45 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
46 if (bit_offset < 3) {
47 d[byte_offset] |= (idx << (2-bit_offset));
48 n = byte_offset+1;
49 } else {
50 d[byte_offset] |= (idx >> (bit_offset-2));
51 d[byte_offset+1] = 0;
52 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
53 n = byte_offset+2;
55 s++; i++;
58 if ((n > 0) && (*s == '=')) {
59 n -= 1;
62 /* fix up length */
63 decoded.length = n;
64 return decoded;
67 /**
68 * Decode a base64 string in-place - wrapper for the above
69 **/
70 _PUBLIC_ void base64_decode_inplace(char *s)
72 DATA_BLOB decoded = base64_decode_data_blob(s);
74 if ( decoded.length != 0 ) {
75 memcpy(s, decoded.data, decoded.length);
77 /* null terminate */
78 s[decoded.length] = '\0';
79 } else {
80 *s = '\0';
83 data_blob_free(&decoded);
86 /**
87 * Encode a base64 string into a talloc()ed string caller to free.
89 * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
90 * with adjustments
91 **/
93 _PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
95 int bits = 0;
96 int char_count = 0;
97 size_t out_cnt, len, output_len;
98 char *result;
100 if (!data.length || !data.data)
101 return NULL;
103 out_cnt = 0;
104 len = data.length;
105 output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is
106 * random but should be enough for
107 * the = and \0 */
108 result = talloc_array(mem_ctx, char, output_len); /* get us plenty of space */
109 SMB_ASSERT(result != NULL);
111 while (len--) {
112 int c = (unsigned char) *(data.data++);
113 bits += c;
114 char_count++;
115 if (char_count == 3) {
116 result[out_cnt++] = b64[bits >> 18];
117 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
118 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
119 result[out_cnt++] = b64[bits & 0x3f];
120 bits = 0;
121 char_count = 0;
122 } else {
123 bits <<= 8;
126 if (char_count != 0) {
127 bits <<= 16 - (8 * char_count);
128 result[out_cnt++] = b64[bits >> 18];
129 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
130 if (char_count == 1) {
131 result[out_cnt++] = '=';
132 result[out_cnt++] = '=';
133 } else {
134 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
135 result[out_cnt++] = '=';
138 result[out_cnt] = '\0'; /* terminate */
139 return result;