]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/base64.c
fix encoding of flac private data
[ffmpeg] / libavutil / base64.c
index 73f41bbcc27ea665a108af1b6bc93e6f4ef9e9ca..8567c9924bcacf0ea3763b7feb083efc644a5195 100644 (file)
@@ -29,7 +29,7 @@
 #include "base64.h"
 
 /* ---------------- private code */
-static uint8_t map2[] =
+static const uint8_t map2[] =
 {
     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
@@ -70,37 +70,34 @@ int av_base64_decode(uint8_t * out, const char *in, int out_length)
 * fixed edge cases and made it work from data (vs. strings) by ryan.
 *****************************************************************************/
 
-char *av_base64_encode(char *out, int out_len, uint8_t * src, int len)
+char *av_base64_encode(char * buf, int buf_len, const uint8_t * src, int len)
 {
     static const char b64[] =
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-    char *dst;
+    char *ret, *dst;
     unsigned i_bits = 0;
     int i_shift = 0;
     int bytes_remaining = len;
 
-    if (len < UINT_MAX / 4 && out_len > (len * 4 / 3 + 12) && out) {
-        dst = out;
-    } else
+    if (len >= UINT_MAX / 4 ||
+        buf_len < len * 4 / 3 + 12)
         return NULL;
-
-    if (len) {                  // special edge case, what should we really do here?
-        while (bytes_remaining) {
-            i_bits = (i_bits << 8) + *src++;
-            bytes_remaining--;
-            i_shift += 8;
-
-            do {
-                *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
-                i_shift -= 6;
-            } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0));
-        }
-        while ((dst - out) & 3)
-            *dst++ = '=';
+    ret = dst = buf;
+    while (bytes_remaining) {
+        i_bits = (i_bits << 8) + *src++;
+        bytes_remaining--;
+        i_shift += 8;
+
+        do {
+            *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
+            i_shift -= 6;
+        } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0));
     }
+    while ((dst - ret) & 3)
+        *dst++ = '=';
     *dst = '\0';
 
-    return out;
+    return ret;
 }
 
 // #define TEST_BASE64
@@ -131,12 +128,10 @@ int b64test()
     };
     for (t = tests; t->data; t++) {
         char *str;
-        int ret;
 
         av_log(NULL, AV_LOG_ERROR, "Encoding %s...\n", (char *) t->data);
-        str = av_malloc(t->len * 4 / 3 + 12);
-        ret = av_base64_encode(str, t->len * 4 / 3 + 12, t->data, t->len);
-        if (ret > 0) {
+        str = av_base64_encode(t->data, t->len);
+        if (str) {
             av_log(NULL, AV_LOG_ERROR, "Encoded to %s...\n", str);
             if (strcmp(str, t->result) != 0) {
                 av_log(NULL, AV_LOG_ERROR, "failed test %d: %s != %s\n",
@@ -170,9 +165,9 @@ int b64test()
         srand(123141);          // time(NULL));
         for (test_count = 0; test_count < 100; test_count++) {
             int size = rand() % 1024;
-            int ii, ret;
+            int ii;
             uint8_t *data;
-            char *encoded_result = av_malloc(size * 4 / 3 + 12);
+            char *encoded_result;
 
             av_log(NULL, AV_LOG_ERROR, "Test %d: Size %d bytes...",
                    test_count, size);
@@ -181,9 +176,8 @@ int b64test()
                 data[ii] = rand() % 255;
             }
 
-            ret = av_base64_encode(encoded_result, size * 4 / 3 + 12,
-                                   data, size);
-            if (ret > 0) {
+            encoded_result = av_base64_encode(data, size);
+            if (encoded_result) {
                 int decode_buffer_size = size + 10;     // try without 10 as well
                 uint8_t *decode_buffer = av_malloc(decode_buffer_size);
                 if (decode_buffer) {