blob: 076f6497d8ea5b1761d7299e4329d862af4b8588 [file] [log] [blame]
Derek Sollenberger1cab2922012-01-18 08:56:56 -05001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
The Android Open Source Project09109162009-03-03 19:30:35 -08009
10#include "SkBase64.h"
11
12#define DecodePad -2
13#define EncodePad 64
14
15static const char encode[] =
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17 "abcdefghijklmnopqrstuvwxyz"
18 "0123456789+/=";
19
20static const signed char decodeData[] = {
21 62, -1, -1, -1, 63,
22 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, DecodePad, -1, -1,
23 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
24 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
25 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
26 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
27};
28
29SkBase64::SkBase64() : fLength((size_t) -1), fData(NULL) {
30}
31
32#if defined _WIN32 && _MSC_VER >= 1300 // disable 'two', etc. may be used without having been initialized
33#pragma warning ( push )
34#pragma warning ( disable : 4701 )
35#endif
36
37SkBase64::Error SkBase64::decode(const void* srcPtr, size_t size, bool writeDestination) {
38 unsigned char* dst = (unsigned char*) fData;
39 const unsigned char* dstStart = (const unsigned char*) fData;
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);
Derek Sollenberger1cab2922012-01-18 08:56:56 -050077 int two = 0;
78 int three = 0;
The Android Open Source Project09109162009-03-03 19:30:35 -080079 if (writeDestination) {
80 int one = (uint8_t) (bytes[0] << 2);
81 two = bytes[1];
82 one |= two >> 4;
83 two = (uint8_t) (two << 4);
84 three = bytes[2];
85 two |= three >> 2;
86 three = (uint8_t) (three << 6);
87 three |= bytes[3];
88 SkASSERT(one < 256 && two < 256 && three < 256);
89 *dst = (unsigned char) one;
90 }
91 dst++;
92 if (padTwo)
93 break;
94 if (writeDestination)
95 *dst = (unsigned char) two;
96 dst++;
97 if (padThree)
98 break;
99 if (writeDestination)
100 *dst = (unsigned char) three;
101 dst++;
102 }
103goHome:
104 fLength = dst - dstStart;
105 return kNoError;
106}
107
108#if defined _WIN32 && _MSC_VER >= 1300
109#pragma warning ( pop )
110#endif
111
112size_t SkBase64::Encode(const void* srcPtr, size_t length, void* dstPtr) {
113 const unsigned char* src = (const unsigned char*) srcPtr;
114 unsigned char* dst = (unsigned char*) dstPtr;
115 if (dst) {
116 size_t remainder = length % 3;
117 const unsigned char* end = &src[length - remainder];
118 while (src < end) {
119 unsigned a = *src++;
120 unsigned b = *src++;
121 unsigned c = *src++;
122 int d = c & 0x3F;
123 c = (c >> 6 | b << 2) & 0x3F;
124 b = (b >> 4 | a << 4) & 0x3F;
125 a = a >> 2;
126 *dst++ = encode[a];
127 *dst++ = encode[b];
128 *dst++ = encode[c];
129 *dst++ = encode[d];
130 }
131 if (remainder > 0) {
132 int k1 = 0;
133 int k2 = EncodePad;
134 int a = (uint8_t) *src++;
135 if (remainder == 2)
136 {
137 int b = *src++;
138 k1 = b >> 4;
139 k2 = (b << 2) & 0x3F;
140 }
141 *dst++ = encode[a >> 2];
142 *dst++ = encode[(k1 | a << 4) & 0x3F];
143 *dst++ = encode[k2];
144 *dst++ = encode[EncodePad];
145 }
146 }
147 return (length + 2) / 3 * 4;
148}
149
150SkBase64::Error SkBase64::decode(const char* src, size_t len) {
151 Error err = decode(src, len, false);
152 SkASSERT(err == kNoError);
153 if (err != kNoError)
154 return err;
155 fData = new char[fLength]; // should use sk_malloc/sk_free
156 decode(src, len, true);
157 return kNoError;
158}
159
160#ifdef SK_SUPPORT_UNITTEST
161void SkBase64::UnitTest() {
162 signed char all[256];
163 for (int index = 0; index < 256; index++)
164 all[index] = (signed char) (index + 1);
165 for (int offset = 0; offset < 6; offset++) {
166 size_t length = 256 - offset;
167 size_t encodeLength = Encode(all + offset, length, NULL);
168 char* src = (char*)sk_malloc_throw(encodeLength + 1);
169 Encode(all + offset, length, src);
170 src[encodeLength] = '\0';
171 SkBase64 tryMe;
172 tryMe.decode(src, encodeLength);
173 SkASSERT(length == tryMe.fLength);
174 SkASSERT(strcmp((const char*) (all + offset), tryMe.fData) == 0);
175 sk_free(src);
176 delete[] tryMe.fData;
177 }
178}
179#endif
180
181