1
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
9
10#include "SkBase64.h"
11
12#define DecodePad -2
13#define EncodePad 64
14
15static const char default_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);
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);
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, const char* encodeMap) {
113    const char* encode;
114    if (NULL == 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
166#ifdef SK_SUPPORT_UNITTEST
167void SkBase64::UnitTest() {
168    signed char all[256];
169    for (int index = 0; index < 256; index++)
170        all[index] = (signed char) (index + 1);
171    for (int offset = 0; offset < 6; offset++) {
172        size_t length = 256 - offset;
173        size_t encodeLength = Encode(all + offset, length, NULL);
174        char* src = (char*)sk_malloc_throw(encodeLength + 1);
175        Encode(all + offset, length, src);
176        src[encodeLength] = '\0';
177        SkBase64 tryMe;
178        tryMe.decode(src, encodeLength);
179        SkASSERT(length == tryMe.fLength);
180        SkASSERT(strcmp((const char*) (all + offset), tryMe.fData) == 0);
181        sk_free(src);
182        delete[] tryMe.fData;
183    }
184}
185#endif
186