Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
base64.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/common/base64.h"
6
7#include "flutter/fml/logging.h"
8
9#include <cstdint>
10
11#define DecodePad -2
12#define EncodePad 64
13
14static const char kDefaultEncode[] =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16 "abcdefghijklmnopqrstuvwxyz"
17 "0123456789+/=";
18
19static const signed char kDecodeData[] = {
20 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
21 -1, -1, DecodePad, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
22 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
23 -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
24 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
25
26namespace flutter {
27
29 size_t srcLength,
30 void* dstv,
31 size_t* dstLength) {
32 const unsigned char* src = static_cast<const unsigned char*>(srcv);
33 unsigned char* dst = static_cast<unsigned char*>(dstv);
34
35 int i = 0;
36 bool padTwo = false;
37 bool padThree = false;
38 char unsigned const* const end = src + srcLength;
39 while (src < end) {
40 unsigned char bytes[4] = {0, 0, 0, 0};
41 int byte = 0;
42 do {
43 unsigned char srcByte = *src++;
44 if (srcByte == 0) {
45 *dstLength = i;
46 return Error::kNone;
47 }
48 if (srcByte <= ' ') {
49 continue; // treat as white space
50 }
51 if (srcByte < '+' || srcByte > 'z') {
52 return Error::kBadChar;
53 }
54 signed char decoded = kDecodeData[srcByte - '+'];
55 bytes[byte] = decoded;
56 if (decoded != DecodePad) {
57 if (decoded < 0) {
58 return Error::kBadChar;
59 }
60 byte++;
61 if (*src) {
62 continue;
63 }
64 if (byte == 0) {
65 *dstLength = i;
66 return Error::kNone;
67 }
68 if (byte == 4) {
69 break;
70 }
71 }
72 // As an optimization, if we find an equals sign
73 // we assume all future bytes to read are the
74 // appropriate number of padding equals signs.
75 if (byte < 2) {
76 return Error::kBadPadding;
77 }
78 padThree = true;
79 if (byte == 2) {
80 padTwo = true;
81 }
82 break;
83 } while (byte < 4);
84 int two = 0;
85 int three = 0;
86 if (dst) {
87 int one = (uint8_t)(bytes[0] << 2);
88 two = bytes[1];
89 one |= two >> 4;
90 two = (uint8_t)((two << 4) & 0xFF);
91 three = bytes[2];
92 two |= three >> 2;
93 three = (uint8_t)((three << 6) & 0xFF);
94 three |= bytes[3];
95 FML_DCHECK(one < 256 && two < 256 && three < 256);
96 dst[i] = (unsigned char)one;
97 }
98 i++;
99 if (padTwo) {
100 break;
101 }
102 if (dst) {
103 dst[i] = (unsigned char)two;
104 }
105 i++;
106 if (padThree) {
107 break;
108 }
109 if (dst) {
110 dst[i] = (unsigned char)three;
111 }
112 i++;
113 }
114 *dstLength = i;
115 return Error::kNone;
116}
117
118size_t Base64::Encode(const void* srcv, size_t length, void* dstv) {
119 FML_DCHECK(dstv);
120 const unsigned char* src = static_cast<const unsigned char*>(srcv);
121 unsigned char* dst = static_cast<unsigned char*>(dstv);
122
123 const char* encode = kDefaultEncode;
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 // NOLINTBEGIN(clang-analyzer-core.NullDereference)
135 *dst++ = encode[a];
136 *dst++ = encode[b];
137 *dst++ = encode[c];
138 *dst++ = encode[d];
139 // NOLINTEND(clang-analyzer-core.NullDereference)
140 }
141 if (remainder > 0) {
142 int k1 = 0;
143 int k2 = EncodePad;
144 int a = (uint8_t)*src++;
145 if (remainder == 2) {
146 int b = *src++;
147 k1 = b >> 4;
148 k2 = (b << 2) & 0x3F;
149 }
150 // NOLINTBEGIN(clang-analyzer-core.NullDereference)
151 *dst++ = encode[a >> 2];
152 *dst++ = encode[(k1 | a << 4) & 0x3F];
153 *dst++ = encode[k2];
154 *dst++ = encode[EncodePad];
155 // NOLINTEND(clang-analyzer-core.NullDereference)
156 }
157 return EncodedSize(length);
158}
159
160} // namespace flutter
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
#define FML_DCHECK(condition)
Definition logging.h:103
size_t length
#define EncodePad
Definition base64.cc:12
static const char kDefaultEncode[]
Definition base64.cc:14
#define DecodePad
Definition base64.cc:11
static const signed char kDecodeData[]
Definition base64.cc:19
static size_t Encode(const void *src, size_t length, void *dst)
Definition base64.cc:118
static Error Decode(const void *src, size_t srcLength, void *dst, size_t *dstLength)
Definition base64.cc:28
static size_t EncodedSize(size_t srcDataLength)
Definition base64.h:33