r9671: patch from Kai Blin fixing a bug in our base64 encoder
authorAndrew Tridgell <tridge@samba.org>
Sat, 27 Aug 2005 01:42:20 +0000 (01:42 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:34:38 +0000 (13:34 -0500)
source/lib/ldb/common/ldb_ldif.c

index 6359c9a01494e6b809f9599397696e2d6d1cc1d2..38866d703132af4b404dc53694650fcf56926048 100644 (file)
@@ -155,10 +155,10 @@ char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
        const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        int bit_offset, byte_offset, idx, i;
        const uint8_t *d = (const uint8_t *)buf;
-       int bytes = (len*8 + 5)/6;
+       int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
        char *out;
 
-       out = talloc_array(mem_ctx, char, bytes+2);
+       out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
        if (!out) return NULL;
 
        for (i=0;i<bytes;i++) {
@@ -175,7 +175,8 @@ char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
                out[i] = b64[idx];
        }
 
-       out[i++] = '=';
+       for (;i<bytes+pad_bytes;i++)
+               out[i] = '=';
        out[i] = 0;
 
        return out;