Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Static Public Member Functions | List of all members
flutter::Base64 Struct Reference

#include <base64.h>

Public Types

enum class  Error { kNone , kBadPadding , kBadChar }
 

Static Public Member Functions

static size_t Encode (const void *src, size_t length, void *dst)
 
static size_t EncodedSize (size_t srcDataLength)
 
static Error Decode (const void *src, size_t srcLength, void *dst, size_t *dstLength)
 

Detailed Description

Definition at line 12 of file base64.h.

Member Enumeration Documentation

◆ Error

enum class flutter::Base64::Error
strong
Enumerator
kNone 
kBadPadding 
kBadChar 

Definition at line 14 of file base64.h.

Member Function Documentation

◆ Decode()

Base64::Error flutter::Base64::Decode ( const void *  src,
size_t  srcLength,
void *  dst,
size_t *  dstLength 
)
static

Base64 decodes src into dst.

This can be called once with 'dst' nullptr to get the required size, then again with an allocated 'dst' pointer to do the actual decoding.

Parameters
dstnullptr or a pointer to a buffer large enough to receive the result
dstLengthassigned the length dst is required to be. Must not be nullptr.

Definition at line 28 of file base64.cc.

31 {
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}
glong glong end
#define FML_DCHECK(condition)
Definition logging.h:103
dst
Definition cp.py:12
#define DecodePad
Definition base64.cc:11
static const signed char kDecodeData[]
Definition base64.cc:19

◆ Encode()

size_t flutter::Base64::Encode ( const void *  src,
size_t  length,
void *  dst 
)
static

Base64 encodes src into dst.

Parameters
dsta pointer to a buffer large enough to receive the result.
Returns
the required length of dst for encoding.

Definition at line 118 of file base64.cc.

118 {
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}
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]
size_t length
#define EncodePad
Definition base64.cc:12
static const char kDefaultEncode[]
Definition base64.cc:14
static size_t EncodedSize(size_t srcDataLength)
Definition base64.h:33

◆ EncodedSize()

static size_t flutter::Base64::EncodedSize ( size_t  srcDataLength)
inlinestatic

Returns the length of the buffer that needs to be allocated to encode srcDataLength bytes.

Definition at line 33 of file base64.h.

33 {
34 // Take the floor of division by 3 to find the number of groups that need to
35 // be encoded. Each group takes 4 bytes to be represented in base64.
36 return ((srcDataLength + 2) / 3) * 4;
37 }

The documentation for this struct was generated from the following files: