epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkBase64.h" |
| 10 | |
| 11 | #define DecodePad -2 |
| 12 | #define EncodePad 64 |
| 13 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 14 | static const char default_encode[] = |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 16 | "abcdefghijklmnopqrstuvwxyz" |
| 17 | "0123456789+/="; |
| 18 | |
| 19 | static const signed char decodeData[] = { |
| 20 | 62, -1, -1, -1, 63, |
| 21 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, DecodePad, -1, -1, |
| 22 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 23 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 24 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 25 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
| 26 | }; |
| 27 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | SkBase64::SkBase64() : fLength((size_t) -1), fData(nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | } |
| 30 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 31 | #if defined _WIN32 // disable 'two', etc. may be used without having been initialized |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #pragma warning ( push ) |
| 33 | #pragma warning ( disable : 4701 ) |
| 34 | #endif |
| 35 | |
| 36 | SkBase64::Error SkBase64::decode(const void* srcPtr, size_t size, bool writeDestination) { |
| 37 | unsigned char* dst = (unsigned char*) fData; |
| 38 | const unsigned char* dstStart = (const unsigned char*) fData; |
| 39 | const unsigned char* src = (const unsigned char*) srcPtr; |
| 40 | bool padTwo = false; |
| 41 | bool padThree = false; |
| 42 | const unsigned char* end = src + size; |
| 43 | while (src < end) { |
| 44 | unsigned char bytes[4]; |
| 45 | int byte = 0; |
| 46 | do { |
| 47 | unsigned char srcByte = *src++; |
| 48 | if (srcByte == 0) |
| 49 | goto goHome; |
| 50 | if (srcByte <= ' ') |
| 51 | continue; // treat as white space |
| 52 | if (srcByte < '+' || srcByte > 'z') |
| 53 | return kBadCharError; |
| 54 | signed char decoded = decodeData[srcByte - '+']; |
| 55 | bytes[byte] = decoded; |
| 56 | if (decoded < 0) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | if (decoded == DecodePad) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | goto handlePad; |
| 59 | return kBadCharError; |
| 60 | } else |
| 61 | byte++; |
| 62 | if (*src) |
| 63 | continue; |
| 64 | if (byte == 0) |
| 65 | goto goHome; |
| 66 | if (byte == 4) |
| 67 | break; |
| 68 | handlePad: |
| 69 | if (byte < 2) |
| 70 | return kPadError; |
| 71 | padThree = true; |
| 72 | if (byte == 2) |
| 73 | padTwo = true; |
| 74 | break; |
| 75 | } while (byte < 4); |
tomhudson@google.com | a7ed3cc | 2011-07-29 13:20:06 +0000 | [diff] [blame] | 76 | int two = 0; |
| 77 | int three = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | if (writeDestination) { |
| 79 | int one = (uint8_t) (bytes[0] << 2); |
| 80 | two = bytes[1]; |
| 81 | one |= two >> 4; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 82 | two = (uint8_t) ((two << 4) & 0xFF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | three = bytes[2]; |
| 84 | two |= three >> 2; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 85 | three = (uint8_t) ((three << 6) & 0xFF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | three |= bytes[3]; |
| 87 | SkASSERT(one < 256 && two < 256 && three < 256); |
| 88 | *dst = (unsigned char) one; |
| 89 | } |
| 90 | dst++; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 91 | if (padTwo) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | break; |
| 93 | if (writeDestination) |
| 94 | *dst = (unsigned char) two; |
| 95 | dst++; |
| 96 | if (padThree) |
| 97 | break; |
| 98 | if (writeDestination) |
| 99 | *dst = (unsigned char) three; |
| 100 | dst++; |
| 101 | } |
| 102 | goHome: |
| 103 | fLength = dst - dstStart; |
| 104 | return kNoError; |
| 105 | } |
| 106 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 107 | #if defined _WIN32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | #pragma warning ( pop ) |
| 109 | #endif |
| 110 | |
bungeman@google.com | af5bbf2 | 2012-02-07 20:47:38 +0000 | [diff] [blame] | 111 | size_t SkBase64::Encode(const void* srcPtr, size_t length, void* dstPtr, const char* encodeMap) { |
| 112 | const char* encode; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | if (nullptr == encodeMap) { |
bungeman@google.com | af5bbf2 | 2012-02-07 20:47:38 +0000 | [diff] [blame] | 114 | encode = default_encode; |
| 115 | } else { |
| 116 | encode = encodeMap; |
| 117 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | const unsigned char* src = (const unsigned char*) srcPtr; |
| 119 | unsigned char* dst = (unsigned char*) dstPtr; |
| 120 | if (dst) { |
| 121 | size_t remainder = length % 3; |
| 122 | const unsigned char* end = &src[length - remainder]; |
| 123 | while (src < end) { |
| 124 | unsigned a = *src++; |
| 125 | unsigned b = *src++; |
| 126 | unsigned c = *src++; |
| 127 | int d = c & 0x3F; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | c = (c >> 6 | b << 2) & 0x3F; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | b = (b >> 4 | a << 4) & 0x3F; |
| 130 | a = a >> 2; |
| 131 | *dst++ = encode[a]; |
| 132 | *dst++ = encode[b]; |
| 133 | *dst++ = encode[c]; |
| 134 | *dst++ = encode[d]; |
| 135 | } |
| 136 | if (remainder > 0) { |
| 137 | int k1 = 0; |
| 138 | int k2 = EncodePad; |
| 139 | int a = (uint8_t) *src++; |
| 140 | if (remainder == 2) |
| 141 | { |
| 142 | int b = *src++; |
| 143 | k1 = b >> 4; |
| 144 | k2 = (b << 2) & 0x3F; |
| 145 | } |
| 146 | *dst++ = encode[a >> 2]; |
| 147 | *dst++ = encode[(k1 | a << 4) & 0x3F]; |
| 148 | *dst++ = encode[k2]; |
| 149 | *dst++ = encode[EncodePad]; |
| 150 | } |
| 151 | } |
| 152 | return (length + 2) / 3 * 4; |
| 153 | } |
| 154 | |
| 155 | SkBase64::Error SkBase64::decode(const char* src, size_t len) { |
| 156 | Error err = decode(src, len, false); |
| 157 | SkASSERT(err == kNoError); |
| 158 | if (err != kNoError) |
| 159 | return err; |
| 160 | fData = new char[fLength]; // should use sk_malloc/sk_free |
| 161 | decode(src, len, true); |
| 162 | return kNoError; |
| 163 | } |