Browse code

Use a full table for base64 decode.

Also encodes error or end marker into table.
About 20% faster.
decode: 466491 -> 374139 decicycles
syntax check: 236955 -> 161182 decicycles

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>

Reimar Döffinger authored on 2012/01/21 20:22:19
Showing 1 changed files
... ...
@@ -30,32 +30,59 @@
30 30
 #include "intreadwrite.h"
31 31
 
32 32
 /* ---------------- private code */
33
-static const uint8_t map2[] =
33
+static const uint8_t map2[256] =
34 34
 {
35
+    0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40
+    0xff, 0xff, 0xff,
41
+
35 42
     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
36 43
     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
37
-    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
44
+    0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0x00, 0x01,
38 45
     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
39 46
     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
40 47
     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
41 48
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
42 49
     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
43 50
     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
44
-    0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
51
+    0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
52
+
53
+                      0xff, 0xff, 0xff, 0xff, 0xff,
54
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 70
 };
46 71
 
47
-int av_base64_decode(uint8_t *out, const char *in, int out_size)
72
+int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
48 73
 {
49 74
     int i, v;
50 75
     uint8_t *dst = out;
51 76
     uint8_t *end = out + out_size;
77
+    // no sign extension
78
+    const uint8_t *in = in_str;
52 79
 
53 80
     v = 0;
54 81
     for (i = 0; ; i++) {
55
-        unsigned int index= in[i]-43;
56
-        if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
57
-            return in[i] && in[i] != '=' ? -1 : dst - out;
58
-        v = (v << 6) + map2[index];
82
+        unsigned bits = map2[in[i]];
83
+        if (bits & 0x80)
84
+            return bits & 1 ? -1 : dst - out;
85
+        v = (v << 6) + bits;
59 86
         if (i & 3) {
60 87
             if (dst < end) {
61 88
                 *dst++ = v >> (6 - 2 * (i & 3));