]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/base64.c
Add and use a public API for RC4 and DES, analogous to the AES API.
[ffmpeg] / libavutil / base64.c
index 73f41bbcc27ea665a108af1b6bc93e6f4ef9e9ca..06fe5407e2870fc5ec4155fa7ed44b6070be906a 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * Base64.c
  * Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com)
  *
  * This file is part of FFmpeg.
@@ -20,8 +19,8 @@
  */
 
 /**
-* @file base64.c
- * @brief Base64 Encode/Decode
+ * @file libavutil/base64.c
+ * @brief Base64 encode/decode
  * @author Ryan Martell <rdm4@martellventures.com> (with lots of Michael)
  */
 
@@ -29,7 +28,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 +50,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,54 +60,50 @@ int av_base64_decode(uint8_t * out, const char *in, int out_length)
         }
     }
 
-    return (dst - out);
+    return dst - out;
 }
 
 /*****************************************************************************
-* b64_encode: stolen from VLC's http.c
-* simplified by michael
-* fixed edge cases and made it work from data (vs. strings) by ryan.
+* b64_encode: Stolen from VLC's http.c.
+* Simplified by Michael.
+* 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
-
-#ifdef TEST_BASE64
-#include "avutil.h"
+#ifdef TEST
+#include "log.h"
+#include "mem.h"
 
-int b64test()
+int main(void)
 {
     int numerr = 0;
     int len;
@@ -131,12 +126,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 +163,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 +174,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) {