svn commit: samba r7740 - in branches/SAMBA_4_0/source: lib lib/ldb/common lib/ldb/include utils

tridge at samba.org tridge at samba.org
Sun Jun 19 01:31:28 GMT 2005


Author: tridge
Date: 2005-06-19 01:31:27 +0000 (Sun, 19 Jun 2005)
New Revision: 7740

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=7740

Log:
get rid of our duplicate base64 routines





Modified:
   branches/SAMBA_4_0/source/lib/ldb/common/ldb_ldif.c
   branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
   branches/SAMBA_4_0/source/lib/ldb/include/ldb_private.h
   branches/SAMBA_4_0/source/lib/util_str.c
   branches/SAMBA_4_0/source/utils/ntlm_auth.c


Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb_ldif.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb_ldif.c	2005-06-19 01:17:29 UTC (rev 7739)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb_ldif.c	2005-06-19 01:31:27 UTC (rev 7740)
@@ -46,7 +46,7 @@
   this base64 decoder was taken from jitterbug (written by tridge).
   we might need to replace it with a new version
 */
-static int base64_decode(char *s)
+int ldb_base64_decode(char *s)
 {
 	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 	int bit_offset, byte_offset, idx, i, n;
@@ -92,7 +92,7 @@
   encode as base64
   caller frees
 */
-char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len)
+char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
 {
 	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 	int bit_offset, byte_offset, idx, i;
@@ -100,7 +100,7 @@
 	int bytes = (len*8 + 5)/6;
 	char *out;
 
-	out = talloc_array(ldb, char, bytes+2);
+	out = talloc_array(mem_ctx, char, bytes+2);
 	if (!out) return NULL;
 
 	for (i=0;i<bytes;i++) {
@@ -402,7 +402,7 @@
 	}
 
 	if (base64_encoded) {
-		int len = base64_decode(value->data);
+		int len = ldb_base64_decode(value->data);
 		if (len == -1) {
 			/* it wasn't valid base64 data */
 			return -1;

Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb.h	2005-06-19 01:17:29 UTC (rev 7739)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb.h	2005-06-19 01:31:27 UTC (rev 7740)
@@ -270,6 +270,8 @@
 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f);
 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s);
 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
+char *ldb_base64_encode(void *mem_ctx, const char *buf, int len);
+int ldb_base64_decode(char *s);
 
 
 /* useful functions for ldb_message structure manipulation */

Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb_private.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb_private.h	2005-06-19 01:17:29 UTC (rev 7739)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb_private.h	2005-06-19 01:31:27 UTC (rev 7740)
@@ -116,7 +116,6 @@
 void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
 
 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */
-char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len);
 int ldb_should_b64_encode(const struct ldb_val *val);
 
 int ltdb_connect(struct ldb_context *ldb, const char *url, 

Modified: branches/SAMBA_4_0/source/lib/util_str.c
===================================================================
--- branches/SAMBA_4_0/source/lib/util_str.c	2005-06-19 01:17:29 UTC (rev 7739)
+++ branches/SAMBA_4_0/source/lib/util_str.c	2005-06-19 01:31:27 UTC (rev 7740)
@@ -24,6 +24,7 @@
 #include "includes.h"
 #include "system/iconv.h"
 #include "pstring.h"
+#include "lib/ldb/include/ldb.h"
 
 /**
  * @file
@@ -895,37 +896,11 @@
 /**
  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
  **/
-DATA_BLOB base64_decode_data_blob(const char *s)
+DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
 {
-	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-	int bit_offset, byte_offset, idx, i, n;
-	DATA_BLOB decoded = data_blob(s, strlen(s)+1);
-	uint8_t *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++;
-	}
-
-	/* fix up length */
-	decoded.length = n;
-	return decoded;
+	DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
+	ret.length = ldb_base64_decode(ret.data);
+	return ret;
 }
 
 /**
@@ -933,58 +908,15 @@
  **/
 void base64_decode_inplace(char *s)
 {
-	DATA_BLOB decoded = base64_decode_data_blob(s);
-	memcpy(s, decoded.data, decoded.length);
-	data_blob_free(&decoded);
-
-	/* null terminate */
-	s[decoded.length] = '\0';
+	ldb_base64_decode(s);
 }
 
 /**
- * Encode a base64 string into a malloc()ed string caller to free.
- *
- *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
+ * Encode a base64 string into a talloc()ed string caller to free.
  **/
-char * base64_encode_data_blob(DATA_BLOB data)
+char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
 {
-	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-	int bits = 0;
-	int char_count = 0;
-	size_t out_cnt = 0;
-	size_t len = data.length;
-	size_t output_len = data.length * 2;
-	char *result = malloc(output_len); /* get us plenty of space */
-
-	while (len-- && out_cnt < (data.length * 2) - 5) {
-		int c = (uint8_t) *(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;
+	return ldb_base64_encode(mem_ctx, data.data, data.length);
 }
 
 #ifdef VALGRIND

Modified: branches/SAMBA_4_0/source/utils/ntlm_auth.c
===================================================================
--- branches/SAMBA_4_0/source/utils/ntlm_auth.c	2005-06-19 01:17:29 UTC (rev 7739)
+++ branches/SAMBA_4_0/source/utils/ntlm_auth.c	2005-06-19 01:31:27 UTC (rev 7740)
@@ -244,7 +244,7 @@
 	}
 
 	if (strlen(buf) > 3) {
-		in = base64_decode_data_blob(buf + 3);
+		in = base64_decode_data_blob(NULL, buf + 3);
 	} else {
 		in = data_blob(NULL, 0);
 	}
@@ -309,7 +309,7 @@
 	}
 
 	if (strlen(buf) > 3) {
-		in = base64_decode_data_blob(buf + 3);
+		in = base64_decode_data_blob(NULL, buf + 3);
 	} else {
 		in = data_blob(NULL, 0);
 	}
@@ -450,7 +450,7 @@
 	nt_status = auth_nt_status_squash(nt_status);
 
 	if (out.length) {
-		out_base64 = base64_encode_data_blob(out);
+		out_base64 = base64_encode_data_blob(NULL, out);
 	} else {
 		out_base64 = NULL;
 	}



More information about the samba-cvs mailing list