summaryrefslogtreecommitdiff
path: root/source4/lib/util_str.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-06-19 01:31:27 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:18:29 -0500
commit56cc32800036472ebc29362d65e422c0b410e3fc (patch)
treecc590e85fde04ffac6dc296cfdb4a9844126b6e8 /source4/lib/util_str.c
parent814d5a5011038164dd55dd9e593989f69cedef0d (diff)
downloadsamba-56cc32800036472ebc29362d65e422c0b410e3fc.tar.gz
samba-56cc32800036472ebc29362d65e422c0b410e3fc.tar.bz2
samba-56cc32800036472ebc29362d65e422c0b410e3fc.zip
r7740: get rid of our duplicate base64 routines
(This used to be commit cf17f90a83cf04815544c5408eb56d00546b3e88)
Diffstat (limited to 'source4/lib/util_str.c')
-rw-r--r--source4/lib/util_str.c86
1 files changed, 9 insertions, 77 deletions
diff --git a/source4/lib/util_str.c b/source4/lib/util_str.c
index 69a4395cfb..fea7170014 100644
--- a/source4/lib/util_str.c
+++ b/source4/lib/util_str.c
@@ -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 @@ void rfc1738_unescape(char *buf)
/**
* 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 @@ DATA_BLOB base64_decode_data_blob(const char *s)
**/
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