Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "globals.h" |
| 4 | #include "scoped_ptr.h" |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 5 | |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace art { |
| 9 | |
| 10 | static const byte kMap[256] = { |
| 11 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 12 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 13 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 14 | 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
| 15 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 16 | 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
| 17 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 18 | 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, |
| 19 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 20 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 21 | 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 22 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 23 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 24 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 25 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 26 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 27 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 28 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 29 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 30 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 31 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 32 | 255, 255, 255, 255 |
| 33 | }; |
| 34 | |
Carl Shapiro | a506cb0 | 2011-06-28 22:53:46 -0700 | [diff] [blame] | 35 | byte* DecodeBase64(const char* src, size_t* dst_size) { |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 36 | std::vector<byte> tmp; |
| 37 | unsigned long t = 0, y = 0; |
| 38 | int g = 3; |
Carl Shapiro | a506cb0 | 2011-06-28 22:53:46 -0700 | [diff] [blame] | 39 | for (size_t i = 0; src[i] != '\0'; ++i) { |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 40 | byte c = kMap[src[i] & 0xFF]; |
| 41 | if (c == 255) continue; |
| 42 | // the final = symbols are read and used to trim the remaining bytes |
| 43 | if (c == 254) { |
| 44 | c = 0; |
| 45 | // prevent g < 0 which would potentially allow an overflow later |
| 46 | if (--g < 0) { |
| 47 | return NULL; |
| 48 | } |
| 49 | } else if (g != 3) { |
| 50 | // we only allow = to be at the end |
| 51 | return NULL; |
| 52 | } |
| 53 | t = (t << 6) | c; |
| 54 | if (++y == 4) { |
| 55 | tmp.push_back((t >> 16) & 255); |
| 56 | if (g > 1) { |
| 57 | tmp.push_back((t >> 8) & 255); |
| 58 | } |
| 59 | if (g > 2) { |
| 60 | tmp.push_back(t & 255); |
| 61 | } |
| 62 | y = t = 0; |
| 63 | } |
| 64 | } |
| 65 | if (y != 0) { |
| 66 | return NULL; |
| 67 | } |
| 68 | scoped_ptr<byte> dst(new byte[tmp.size()]); |
| 69 | if (dst_size != NULL) { |
| 70 | *dst_size = tmp.size(); |
| 71 | } |
| 72 | std::copy(tmp.begin(), tmp.end(), dst.get()); |
| 73 | return dst.release(); |
| 74 | } |
| 75 | |
| 76 | }; |