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

#include <SkBase64.h>

Public Types

enum  Error { kNoError , kPadError , kBadCharError }
 

Static Public Member Functions

static size_t Encode (const void *src, size_t length, void *dst, const char *encode=nullptr)
 
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 13 of file SkBase64.h.

Member Enumeration Documentation

◆ Error

Enumerator
kNoError 
kPadError 
kBadCharError 

Definition at line 15 of file SkBase64.h.

15 {
19 };
@ kPadError
Definition SkBase64.h:17
@ kBadCharError
Definition SkBase64.h:18
@ kNoError
Definition SkBase64.h:16

Member Function Documentation

◆ Decode()

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

Base64 decodes src into dst.

Normally this is called once with 'dst' nullptr to get the required size, then again with an allocated 'dst' pointer to do the actual encoding.

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 37 of file SkBase64.cpp.

37 {
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}
#define SkASSERT(cond)
Definition SkAssert.h:116
static const signed char decodeData[]
Definition SkBase64.cpp:23
glong glong end
dst
Definition cp.py:12
#define DecodePad
Definition base64.cc:11

◆ Encode()

size_t SkBase64::Encode ( const void *  src,
size_t  length,
void *  dst,
const char *  encode = nullptr 
)
static

Base64 encodes src into dst.

Normally this is called once with 'dst' nullptr to get the required size, then again with an allocated 'dst' pointer to do the actual encoding.

Parameters
dstnullptr or a pointer to a buffer large enough to receive the result
encodenullptr for default encoding or a pointer to at least 65 chars. encode[64] will be used as the pad character. Encodings other than the default encoding cannot be decoded.
Returns
the required length of dst for encoding.

Definition at line 113 of file SkBase64.cpp.

113 {
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}
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]
size_t length
#define EncodePad
Definition base64.cc:12
static size_t EncodedSize(size_t srcDataLength)
Definition SkBase64.h:40

◆ EncodedSize()

static size_t SkBase64::EncodedSize ( size_t  srcDataLength)
inlinestatic

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

Definition at line 40 of file SkBase64.h.

40 {
41 // Take the floor of division by 3 to find the number of groups that need to be encoded.
42 // Each group takes 4 bytes to be represented in base64.
43 return ((srcDataLength + 2) / 3) * 4;
44 }

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