• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 static const char default_encode[] =
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 
SkBase64()28 SkBase64::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 
decode(const void * srcPtr,size_t size,bool writeDestination)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) {
57                 if (decoded == DecodePad)
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);
76         int two = 0;
77         int three = 0;
78         if (writeDestination) {
79             int one = (uint8_t) (bytes[0] << 2);
80             two = bytes[1];
81             one |= two >> 4;
82             two = (uint8_t) ((two << 4) & 0xFF);
83             three = bytes[2];
84             two |= three >> 2;
85             three = (uint8_t) ((three << 6) & 0xFF);
86             three |= bytes[3];
87             SkASSERT(one < 256 && two < 256 && three < 256);
88             *dst = (unsigned char) one;
89         }
90         dst++;
91         if (padTwo)
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 
107 #if defined _WIN32
108 #pragma warning ( pop )
109 #endif
110 
Encode(const void * srcPtr,size_t length,void * dstPtr,const char * encodeMap)111 size_t SkBase64::Encode(const void* srcPtr, size_t length, void* dstPtr, const char* encodeMap) {
112     const char* encode;
113     if (nullptr == encodeMap) {
114         encode = default_encode;
115     } else {
116         encode = encodeMap;
117     }
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;
128             c = (c >> 6 | b << 2) & 0x3F;
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 
decode(const char * src,size_t len)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 }
164