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
8
9#include "include/utils/SkBase64.h"
10
11#define DecodePad -2
12#define EncodePad 64
13
14static const char default_encode[] =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16 "abcdefghijklmnopqrstuvwxyz"
17 "0123456789+/=";
18
19static 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
28SkBase64::SkBase64() : fLength((size_t) -1), fData(nullptr) {
29}
30
31#if defined _WIN32 // disable 'two', etc. may be used without having been initialized
32#pragma warning ( push )
33#pragma warning ( disable : 4701 )
34#endif
35
36SkBase64::Error SkBase64::decode(const void* srcPtr, size_t size, bool writeDestination) {
37 unsigned char* dst = (unsigned char*)fData;
38 int i = 0;
39
40 const unsigned char* src = (const unsigned char*) srcPtr;
41 bool padTwo = false;
42 bool padThree = false;
43 const unsigned char* end = src + size;
44 while (src < end) {
45 unsigned char bytes[4];
46 int byte = 0;
47 do {
48 unsigned char srcByte = *src++;
49 if (srcByte == 0)
50 goto goHome;
51 if (srcByte <= ' ')
52 continue; // treat as white space
53 if (srcByte < '+' || srcByte > 'z')
54 return kBadCharError;
55 signed char decoded = decodeData[srcByte - '+'];
56 bytes[byte] = decoded;
57 if (decoded < 0) {
58 if (decoded == DecodePad)
59 goto handlePad;
60 return kBadCharError;
61 } else
62 byte++;
63 if (*src)
64 continue;
65 if (byte == 0)
66 goto goHome;
67 if (byte == 4)
68 break;
69handlePad:
70 if (byte < 2)
71 return kPadError;
72 padThree = true;
73 if (byte == 2)
74 padTwo = true;
75 break;
76 } while (byte < 4);
77 int two = 0;
78 int three = 0;
79 if (writeDestination) {
80 int one = (uint8_t) (bytes[0] << 2);
81 two = bytes[1];
82 one |= two >> 4;
83 two = (uint8_t) ((two << 4) & 0xFF);
84 three = bytes[2];
85 two |= three >> 2;
86 three = (uint8_t) ((three << 6) & 0xFF);
87 three |= bytes[3];
88 SkASSERT(one < 256 && two < 256 && three < 256);
89 dst[i] = (unsigned char) one;
90 }
91 i++;
92 if (padTwo)
93 break;
94 if (writeDestination)
95 dst[i] = (unsigned char) two;
96 i++;
97 if (padThree)
98 break;
99 if (writeDestination)
100 dst[i] = (unsigned char) three;
101 i++;
102 }
103goHome:
104 fLength = i;
105 return kNoError;
106}
107
108#if defined _WIN32
109#pragma warning ( pop )
110#endif
111
112size_t SkBase64::Encode(const void* srcPtr, size_t length, void* dstPtr, const char* encodeMap) {
113 const char* encode;
114 if (nullptr == encodeMap) {
115 encode = default_encode;
116 } else {
117 encode = encodeMap;
118 }
119 const unsigned char* src = (const unsigned char*) srcPtr;
120 unsigned char* dst = (unsigned char*) dstPtr;
121 if (dst) {
122 size_t remainder = length % 3;
123 const unsigned char* end = &src[length - remainder];
124 while (src < end) {
125 unsigned a = *src++;
126 unsigned b = *src++;
127 unsigned c = *src++;
128 int d = c & 0x3F;
129 c = (c >> 6 | b << 2) & 0x3F;
130 b = (b >> 4 | a << 4) & 0x3F;
131 a = a >> 2;
132 *dst++ = encode[a];
133 *dst++ = encode[b];
134 *dst++ = encode[c];
135 *dst++ = encode[d];
136 }
137 if (remainder > 0) {
138 int k1 = 0;
139 int k2 = EncodePad;
140 int a = (uint8_t) *src++;
141 if (remainder == 2)
142 {
143 int b = *src++;
144 k1 = b >> 4;
145 k2 = (b << 2) & 0x3F;
146 }
147 *dst++ = encode[a >> 2];
148 *dst++ = encode[(k1 | a << 4) & 0x3F];
149 *dst++ = encode[k2];
150 *dst++ = encode[EncodePad];
151 }
152 }
153 return (length + 2) / 3 * 4;
154}
155
156SkBase64::Error SkBase64::decode(const char* src, size_t len) {
157 Error err = decode(src, len, false);
158 SkASSERT(err == kNoError);
159 if (err != kNoError)
160 return err;
161 fData = new char[fLength]; // should use sk_malloc/sk_free
162 decode(src, len, true);
163 return kNoError;
164}
165