]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/base64.c
Update golomb decoding tables so that get_*_golomb_31() return some
[ffmpeg] / libavutil / base64.c
index 6279244d3be4239f6f48cd0e1c8e6bfa2843fdff..97a77468f4c551146d40cb989b4acd534607cbab 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,
@@ -51,7 +51,7 @@ int av_base64_decode(uint8_t * out, const char *in, int out_length)
     v = 0;
     for (i = 0; in[i] && in[i] != '='; i++) {
         unsigned int index= in[i]-43;
-        if (index>=(sizeof(map2)/sizeof(map2[0])) || map2[index] == 0xff)
+        if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
             return -1;
         v = (v << 6) + map2[index];
         if (i & 3) {
@@ -61,7 +61,7 @@ int av_base64_decode(uint8_t * out, const char *in, int out_length)
         }
     }
 
-    return (dst - out);
+    return dst - out;
 }
 
 /*****************************************************************************
@@ -70,7 +70,7 @@ 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(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+/";
@@ -79,25 +79,22 @@ char *av_base64_encode(uint8_t * src, int len)
     int i_shift = 0;
     int bytes_remaining = len;
 
-    if (len < UINT_MAX / 4) {
-        ret = dst = av_malloc(len * 4 / 3 + 12);
-    } 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 - ret) & 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 ret;