lib/util Move base64 functions into lib/util/base64.c
authorAndrew Bartlett <abartlet@samba.org>
Wed, 30 Mar 2011 06:49:01 +0000 (17:49 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 30 Mar 2011 10:17:05 +0000 (12:17 +0200)
Andrew Bartlett

lib/util/base64.c [new file with mode: 0644]
lib/util/util.h
lib/util/util_str.c
lib/util/wscript_build
source3/Makefile.in
source3/include/proto.h
source3/lib/util_str.c
source4/utils/ntlm_auth.c

diff --git a/lib/util/base64.c b/lib/util/base64.c
new file mode 100644 (file)
index 0000000..19ce2d1
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+
+   Copyright (C) Andrew Tridgell 1992-2001
+   Copyright (C) Simo Sorce      2001-2002
+   Copyright (C) Martin Pool     2003
+   Copyright (C) James Peach    2006
+   Copyright (C) Jeremy Allison  1992-2007
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+
+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+/**
+ * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
+ **/
+_PUBLIC_ DATA_BLOB base64_decode_data_blob(const char *s)
+{
+       int bit_offset, byte_offset, idx, i, n;
+       DATA_BLOB decoded = data_blob(s, strlen(s)+1);
+       unsigned char *d = decoded.data;
+       char *p;
+
+       n=i=0;
+
+       while (*s && (p=strchr_m(b64,*s))) {
+               idx = (int)(p - b64);
+               byte_offset = (i*6)/8;
+               bit_offset = (i*6)%8;
+               d[byte_offset] &= ~((1<<(8-bit_offset))-1);
+               if (bit_offset < 3) {
+                       d[byte_offset] |= (idx << (2-bit_offset));
+                       n = byte_offset+1;
+               } else {
+                       d[byte_offset] |= (idx >> (bit_offset-2));
+                       d[byte_offset+1] = 0;
+                       d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
+                       n = byte_offset+2;
+               }
+               s++; i++;
+       }
+
+       if ((n > 0) && (*s == '=')) {
+               n -= 1;
+       }
+
+       /* fix up length */
+       decoded.length = n;
+       return decoded;
+}
+
+/**
+ * Decode a base64 string in-place - wrapper for the above
+ **/
+_PUBLIC_ void base64_decode_inplace(char *s)
+{
+       DATA_BLOB decoded = base64_decode_data_blob(s);
+
+       if ( decoded.length != 0 ) {
+               memcpy(s, decoded.data, decoded.length);
+
+               /* null terminate */
+               s[decoded.length] = '\0';
+       } else {
+               *s = '\0';
+       }
+
+       data_blob_free(&decoded);
+}
+
+/**
+ * Encode a base64 string into a talloc()ed string caller to free.
+ *
+ * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
+ * with adjustments
+ **/
+
+_PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
+{
+       int bits = 0;
+       int char_count = 0;
+       size_t out_cnt, len, output_len;
+       char *result;
+
+        if (!data.length || !data.data)
+               return NULL;
+
+       out_cnt = 0;
+       len = data.length;
+       output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is
+                                          * random but should be enough for
+                                          * the = and \0 */
+       result = talloc_array(mem_ctx, char, output_len); /* get us plenty of space */
+       SMB_ASSERT(result != NULL);
+
+       while (len--) {
+               int c = (unsigned char) *(data.data++);
+               bits += c;
+               char_count++;
+               if (char_count == 3) {
+                       result[out_cnt++] = b64[bits >> 18];
+                       result[out_cnt++] = b64[(bits >> 12) & 0x3f];
+                       result[out_cnt++] = b64[(bits >> 6) & 0x3f];
+                       result[out_cnt++] = b64[bits & 0x3f];
+                       bits = 0;
+                       char_count = 0;
+               } else {
+                       bits <<= 8;
+               }
+       }
+       if (char_count != 0) {
+               bits <<= 16 - (8 * char_count);
+               result[out_cnt++] = b64[bits >> 18];
+               result[out_cnt++] = b64[(bits >> 12) & 0x3f];
+               if (char_count == 1) {
+                       result[out_cnt++] = '=';
+                       result[out_cnt++] = '=';
+               } else {
+                       result[out_cnt++] = b64[(bits >> 6) & 0x3f];
+                       result[out_cnt++] = '=';
+               }
+       }
+       result[out_cnt] = '\0'; /* terminate */
+       return result;
+}
+
index 48d6566dd09ddcba253a7284c62b190c54c10609..45779912f3105f08c4bac7bbae818d5fc36cd934 100644 (file)
@@ -409,6 +409,19 @@ _PUBLIC_ int strwicmp(const char *psz1, const char *psz2);
 **/
 _PUBLIC_ void string_replace(char *s, char oldc, char newc);
 
+/**
+ Base64 decode a string, place into a data blob.  Caller to data_blob_free() the result.
+**/
+_PUBLIC_ DATA_BLOB base64_decode_data_blob(const char *s);
+/**
+ Base64 decode a string, inplace
+**/
+_PUBLIC_ void base64_decode_inplace(char *s);
+/**
+ Base64 encode a binary data blob into a string
+**/
+_PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data);
+
 /**
  * Compare 2 strings.
  *
index 8695266655c30ce5973d17084c15aba0778e1d38..41183ff57048b79fe15140b5f42fb7c909fb8d8b 100644 (file)
@@ -320,5 +320,3 @@ _PUBLIC_ void string_replace(char *s, char oldc, char newc)
                s++;
        }
 }
-
-
index 6aaf04c96e0b4e7b26f443447758cc11f275c013..e39af54c0ffd887ca1a9dbd925705608f824b882 100755 (executable)
@@ -7,7 +7,7 @@ bld.SAMBA_LIBRARY('samba-util-common',
                   util_file.c time.c rbtree.c rfc1738.c select.c
                   genrand.c fsusage.c blocking.c become_daemon.c
                   signal.c system.c params.c util.c util_id.c util_net.c
-                  util_strlist.c idtree.c debug.c fault.c''',
+                  util_strlist.c idtree.c debug.c fault.c base64.c''',
                   public_deps='talloc pthread LIBCRYPTO',
                   # until we get all the dependencies in this library in common
                   # we need to allow this library to be built with unresolved symbols
index 15279ee039ec51912b839becc7d2d53bf4af41d8..05843efd63c005ed5c599d304c96a030f329fba0 100644 (file)
@@ -450,7 +450,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
          lib/access.o lib/smbrun.o \
          lib/bitmap.o lib/dprintf.o $(UTIL_REG_OBJ) \
          lib/wins_srv.o \
-         lib/util_str.o lib/util_sid.o \
+         lib/util_str.o ../lib/util/base64.o lib/util_sid.o \
          lib/util_unistr.o ../lib/util/charset/codepoints.o ../lib/util/charset/util_str.o lib/util_file.o \
          lib/util.o lib/util_names.o \
          lib/util_sock.o lib/sock_exec.o lib/util_sec.o \
index a565f93462defa0b9fcb2837618ea0bf21109909..91ff45d7b975423def5c80e3f71f612f8243845a 100644 (file)
@@ -1026,9 +1026,6 @@ char *ipstr_list_make(char **ipstr_list,
                        int ip_count);
 int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list);
 void ipstr_list_free(char* ipstr_list);
-DATA_BLOB base64_decode_data_blob(const char *s);
-void base64_decode_inplace(char *s);
-char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data);
 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr);
 SMB_OFF_T conv_str_size(const char * str);
 void string_append(char **left, const char *right);
index 7b5071746370ee86deae0c938c9cf64f91b637b1..0f75f45bf12adb99a1249703db578602b76b16b5 100644 (file)
@@ -1515,121 +1515,6 @@ void ipstr_list_free(char* ipstr_list)
        SAFE_FREE(ipstr_list);
 }
 
-static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-/**
- * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
- **/
-DATA_BLOB base64_decode_data_blob(const char *s)
-{
-       int bit_offset, byte_offset, idx, i, n;
-       DATA_BLOB decoded = data_blob(s, strlen(s)+1);
-       unsigned char *d = decoded.data;
-       char *p;
-
-       n=i=0;
-
-       while (*s && (p=strchr_m(b64,*s))) {
-               idx = (int)(p - b64);
-               byte_offset = (i*6)/8;
-               bit_offset = (i*6)%8;
-               d[byte_offset] &= ~((1<<(8-bit_offset))-1);
-               if (bit_offset < 3) {
-                       d[byte_offset] |= (idx << (2-bit_offset));
-                       n = byte_offset+1;
-               } else {
-                       d[byte_offset] |= (idx >> (bit_offset-2));
-                       d[byte_offset+1] = 0;
-                       d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
-                       n = byte_offset+2;
-               }
-               s++; i++;
-       }
-
-       if ((n > 0) && (*s == '=')) {
-               n -= 1;
-       }
-
-       /* fix up length */
-       decoded.length = n;
-       return decoded;
-}
-
-/**
- * Decode a base64 string in-place - wrapper for the above
- **/
-void base64_decode_inplace(char *s)
-{
-       DATA_BLOB decoded = base64_decode_data_blob(s);
-
-       if ( decoded.length != 0 ) {
-               memcpy(s, decoded.data, decoded.length);
-
-               /* null terminate */
-               s[decoded.length] = '\0';
-       } else {
-               *s = '\0';
-       }
-
-       data_blob_free(&decoded);
-}
-
-/**
- * Encode a base64 string into a talloc()ed string caller to free.
- *
- * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
- * with adjustments
- **/
-
-char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
-{
-       int bits = 0;
-       int char_count = 0;
-       size_t out_cnt, len, output_len;
-       char *result;
-
-        if (!data.length || !data.data)
-               return NULL;
-
-       out_cnt = 0;
-       len = data.length;
-       output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is
-                                          * random but should be enough for
-                                          * the = and \0 */
-       result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */
-       SMB_ASSERT(result != NULL);
-
-       while (len--) {
-               int c = (unsigned char) *(data.data++);
-               bits += c;
-               char_count++;
-               if (char_count == 3) {
-                       result[out_cnt++] = b64[bits >> 18];
-                       result[out_cnt++] = b64[(bits >> 12) & 0x3f];
-                       result[out_cnt++] = b64[(bits >> 6) & 0x3f];
-                       result[out_cnt++] = b64[bits & 0x3f];
-                       bits = 0;
-                       char_count = 0;
-               } else {
-                       bits <<= 8;
-               }
-       }
-       if (char_count != 0) {
-               bits <<= 16 - (8 * char_count);
-               result[out_cnt++] = b64[bits >> 18];
-               result[out_cnt++] = b64[(bits >> 12) & 0x3f];
-               if (char_count == 1) {
-                       result[out_cnt++] = '=';
-                       result[out_cnt++] = '=';
-               } else {
-                       result[out_cnt++] = b64[(bits >> 6) & 0x3f];
-                       result[out_cnt++] = '=';
-               }
-       }
-       result[out_cnt] = '\0'; /* terminate */
-       return result;
-}
-
 /* read a SMB_BIG_UINT from a string */
 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
 {
index 34f79715ff225be10c30ba2469f4fcc3f57c92e6..a3701f88224e3394c934b4e52f46a32c1cb8af14 100644 (file)
@@ -142,33 +142,6 @@ static bool parse_ntlm_auth_domain_user(const char *domuser, char **domain,
        return true;
 }
 
-/**
- * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
- **/
-static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
-{
-       DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
-       ret.length = ldb_base64_decode((char *)ret.data);
-       return ret;
-}
-
-/**
- * Encode a base64 string into a talloc()ed string caller to free.
- **/
-static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
-{
-       return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
-}
-
-/**
- * Decode a base64 string in-place - wrapper for the above
- **/
-static void base64_decode_inplace(char *s)
-{
-       ldb_base64_decode(s);
-}
-
-
 
 /* Authenticate a user with a plaintext password */
 
@@ -291,7 +264,7 @@ static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mod
        }
 
        if (strlen(buf) > 3) {
-               in = base64_decode_data_blob(NULL, buf + 3);
+               in = base64_decode_data_blob(buf + 3);
        } else {
                in = data_blob(NULL, 0);
        }
@@ -433,7 +406,7 @@ static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
                        mux_printf(mux_id, "OK\n");
                        return;
                }
-               in = base64_decode_data_blob(NULL, buf + 3);
+               in = base64_decode_data_blob(buf + 3);
        } else {
                in = data_blob(NULL, 0);
        }