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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/utils/SkBase64.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 28 | #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] | 29 | #pragma warning ( push ) |
| 30 | #pragma warning ( disable : 4701 ) |
| 31 | #endif |
| 32 | |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 33 | SkBase64::Error SkBase64::Decode(const void* srcv, size_t srcLength, void* dstv, size_t* dstLength){ |
| 34 | const unsigned char* src = static_cast<const unsigned char*>(srcv); |
| 35 | unsigned char* dst = static_cast<unsigned char*>(dstv); |
Mike Klein | c0c0522 | 2020-01-31 11:07:51 -0600 | [diff] [blame] | 36 | |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 37 | int i = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | bool padTwo = false; |
| 39 | bool padThree = false; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 40 | char unsigned const * const end = src + srcLength; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | while (src < end) { |
| 42 | unsigned char bytes[4]; |
| 43 | int byte = 0; |
| 44 | do { |
| 45 | unsigned char srcByte = *src++; |
| 46 | if (srcByte == 0) |
| 47 | goto goHome; |
| 48 | if (srcByte <= ' ') |
| 49 | continue; // treat as white space |
| 50 | if (srcByte < '+' || srcByte > 'z') |
| 51 | return kBadCharError; |
| 52 | signed char decoded = decodeData[srcByte - '+']; |
| 53 | bytes[byte] = decoded; |
| 54 | if (decoded < 0) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | if (decoded == DecodePad) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | goto handlePad; |
| 57 | return kBadCharError; |
| 58 | } else |
| 59 | byte++; |
| 60 | if (*src) |
| 61 | continue; |
| 62 | if (byte == 0) |
| 63 | goto goHome; |
| 64 | if (byte == 4) |
| 65 | break; |
| 66 | handlePad: |
| 67 | if (byte < 2) |
| 68 | return kPadError; |
| 69 | padThree = true; |
| 70 | if (byte == 2) |
| 71 | padTwo = true; |
| 72 | break; |
| 73 | } while (byte < 4); |
tomhudson@google.com | a7ed3cc | 2011-07-29 13:20:06 +0000 | [diff] [blame] | 74 | int two = 0; |
| 75 | int three = 0; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 76 | if (dst) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | int one = (uint8_t) (bytes[0] << 2); |
| 78 | two = bytes[1]; |
| 79 | one |= two >> 4; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 80 | two = (uint8_t) ((two << 4) & 0xFF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | three = bytes[2]; |
| 82 | two |= three >> 2; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 83 | three = (uint8_t) ((three << 6) & 0xFF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | three |= bytes[3]; |
| 85 | SkASSERT(one < 256 && two < 256 && three < 256); |
Mike Klein | c0c0522 | 2020-01-31 11:07:51 -0600 | [diff] [blame] | 86 | dst[i] = (unsigned char) one; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
Mike Klein | c0c0522 | 2020-01-31 11:07:51 -0600 | [diff] [blame] | 88 | i++; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | if (padTwo) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | break; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 91 | if (dst) |
Mike Klein | c0c0522 | 2020-01-31 11:07:51 -0600 | [diff] [blame] | 92 | dst[i] = (unsigned char) two; |
| 93 | i++; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | if (padThree) |
| 95 | break; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 96 | if (dst) |
Mike Klein | c0c0522 | 2020-01-31 11:07:51 -0600 | [diff] [blame] | 97 | dst[i] = (unsigned char) three; |
| 98 | i++; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | goHome: |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 101 | *dstLength = i; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | return kNoError; |
| 103 | } |
| 104 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 105 | #if defined _WIN32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | #pragma warning ( pop ) |
| 107 | #endif |
| 108 | |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 109 | size_t SkBase64::Encode(const void* srcv, size_t length, void* dstv, const char* encodeMap) { |
| 110 | const unsigned char* src = static_cast<const unsigned char*>(srcv); |
| 111 | unsigned char* dst = static_cast<unsigned char*>(dstv); |
| 112 | |
bungeman@google.com | af5bbf2 | 2012-02-07 20:47:38 +0000 | [diff] [blame] | 113 | const char* encode; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | if (nullptr == encodeMap) { |
bungeman@google.com | af5bbf2 | 2012-02-07 20:47:38 +0000 | [diff] [blame] | 115 | encode = default_encode; |
| 116 | } else { |
| 117 | encode = encodeMap; |
| 118 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | if (dst) { |
| 120 | size_t remainder = length % 3; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 121 | char unsigned const * const end = &src[length - remainder]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | while (src < end) { |
| 123 | unsigned a = *src++; |
| 124 | unsigned b = *src++; |
| 125 | unsigned c = *src++; |
| 126 | int d = c & 0x3F; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | c = (c >> 6 | b << 2) & 0x3F; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | b = (b >> 4 | a << 4) & 0x3F; |
| 129 | a = a >> 2; |
| 130 | *dst++ = encode[a]; |
| 131 | *dst++ = encode[b]; |
| 132 | *dst++ = encode[c]; |
| 133 | *dst++ = encode[d]; |
| 134 | } |
| 135 | if (remainder > 0) { |
| 136 | int k1 = 0; |
| 137 | int k2 = EncodePad; |
| 138 | int a = (uint8_t) *src++; |
| 139 | if (remainder == 2) |
| 140 | { |
| 141 | int b = *src++; |
| 142 | k1 = b >> 4; |
| 143 | k2 = (b << 2) & 0x3F; |
| 144 | } |
| 145 | *dst++ = encode[a >> 2]; |
| 146 | *dst++ = encode[(k1 | a << 4) & 0x3F]; |
| 147 | *dst++ = encode[k2]; |
| 148 | *dst++ = encode[EncodePad]; |
| 149 | } |
| 150 | } |
| 151 | return (length + 2) / 3 * 4; |
| 152 | } |