Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkBase64.cpp
Go to the documentation of this file.
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 "src/base/SkBase64.h"
10
12
13#include <cstdint>
14
15#define DecodePad -2
16#define EncodePad 64
17
18static const char default_encode[] =
19 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20 "abcdefghijklmnopqrstuvwxyz"
21 "0123456789+/=";
22
23static const signed char decodeData[] = {
24 62, -1, -1, -1, 63,
25 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, DecodePad, -1, -1,
26 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
27 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
28 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
29 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
30};
31
32#if defined _WIN32 // 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* srcv, size_t srcLength, void* dstv, size_t* dstLength){
38 const unsigned char* src = static_cast<const unsigned char*>(srcv);
39 unsigned char* dst = static_cast<unsigned char*>(dstv);
40
41 int i = 0;
42 bool padTwo = false;
43 bool padThree = false;
44 char unsigned const * const end = src + srcLength;
45 while (src < end) {
46 unsigned char bytes[4];
47 int byte = 0;
48 do {
49 unsigned char srcByte = *src++;
50 if (srcByte == 0)
51 goto goHome;
52 if (srcByte <= ' ')
53 continue; // treat as white space
54 if (srcByte < '+' || srcByte > 'z')
55 return kBadCharError;
56 signed char decoded = decodeData[srcByte - '+'];
57 bytes[byte] = decoded;
58 if (decoded < 0) {
59 if (decoded == DecodePad)
60 goto handlePad;
61 return kBadCharError;
62 } else
63 byte++;
64 if (*src)
65 continue;
66 if (byte == 0)
67 goto goHome;
68 if (byte == 4)
69 break;
70handlePad:
71 if (byte < 2)
72 return kPadError;
73 padThree = true;
74 if (byte == 2)
75 padTwo = true;
76 break;
77 } while (byte < 4);
78 int two = 0;
79 int three = 0;
80 if (dst) {
81 int one = (uint8_t) (bytes[0] << 2);
82 two = bytes[1];
83 one |= two >> 4;
84 two = (uint8_t) ((two << 4) & 0xFF);
85 three = bytes[2];
86 two |= three >> 2;
87 three = (uint8_t) ((three << 6) & 0xFF);
88 three |= bytes[3];
89 SkASSERT(one < 256 && two < 256 && three < 256);
90 dst[i] = (unsigned char) one;
91 }
92 i++;
93 if (padTwo)
94 break;
95 if (dst)
96 dst[i] = (unsigned char) two;
97 i++;
98 if (padThree)
99 break;
100 if (dst)
101 dst[i] = (unsigned char) three;
102 i++;
103 }
104goHome:
105 *dstLength = i;
106 return kNoError;
107}
108
109#if defined _WIN32
110#pragma warning ( pop )
111#endif
112
113size_t SkBase64::Encode(const void* srcv, size_t length, void* dstv, const char* encodeMap) {
114 const unsigned char* src = static_cast<const unsigned char*>(srcv);
115 unsigned char* dst = static_cast<unsigned char*>(dstv);
116
117 const char* encode;
118 if (nullptr == encodeMap) {
120 } else {
121 encode = encodeMap;
122 }
123 if (dst) {
124 size_t remainder = length % 3;
125 char unsigned const * const end = &src[length - remainder];
126 while (src < end) {
127 unsigned a = *src++;
128 unsigned b = *src++;
129 unsigned c = *src++;
130 int d = c & 0x3F;
131 c = (c >> 6 | b << 2) & 0x3F;
132 b = (b >> 4 | a << 4) & 0x3F;
133 a = a >> 2;
134 *dst++ = encode[a];
135 *dst++ = encode[b];
136 *dst++ = encode[c];
137 *dst++ = encode[d];
138 }
139 if (remainder > 0) {
140 int k1 = 0;
141 int k2 = EncodePad;
142 int a = (uint8_t) *src++;
143 if (remainder == 2)
144 {
145 int b = *src++;
146 k1 = b >> 4;
147 k2 = (b << 2) & 0x3F;
148 }
149 *dst++ = encode[a >> 2];
150 *dst++ = encode[(k1 | a << 4) & 0x3F];
151 *dst++ = encode[k2];
152 *dst++ = encode[EncodePad];
153 }
154 }
155 return EncodedSize(length);
156}
#define SkASSERT(cond)
Definition SkAssert.h:116
static const signed char decodeData[]
Definition SkBase64.cpp:23
#define DecodePad
Definition SkBase64.cpp:15
static const char default_encode[]
Definition SkBase64.cpp:18
static void encode(uint8_t output[16], const uint32_t input[4])
Definition SkMD5.cpp:240
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static bool b
struct MyStruct a[10]
glong glong end
size_t length
#define EncodePad
Definition base64.cc:12
#define DecodePad
Definition base64.cc:11
static Error Decode(const void *src, size_t srcLength, void *dst, size_t *dstLength)
Definition SkBase64.cpp:37
static size_t EncodedSize(size_t srcDataLength)
Definition SkBase64.h:40
@ kPadError
Definition SkBase64.h:17
@ kBadCharError
Definition SkBase64.h:18
@ kNoError
Definition SkBase64.h:16
static size_t Encode(const void *src, size_t length, void *dst, const char *encode=nullptr)
Definition SkBase64.cpp:113