b64.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include "src/core/lib/slice/b64.h"
22 
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 
31 
32 /* --- Constants. --- */
33 
34 static const int8_t base64_bytes[] = {
35  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38  -1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F,
39  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1,
40  -1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
41  0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
42  0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1,
43  -1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
44  0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
45  0x31, 0x32, 0x33, -1, -1, -1, -1, -1};
46 
47 static const char base64_url_unsafe_chars[] =
48  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49 static const char base64_url_safe_chars[] =
50  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
51 
52 #define GRPC_BASE64_PAD_CHAR '='
53 #define GRPC_BASE64_PAD_BYTE 0x7F
54 #define GRPC_BASE64_MULTILINE_LINE_LEN 76
55 #define GRPC_BASE64_MULTILINE_NUM_BLOCKS (GRPC_BASE64_MULTILINE_LINE_LEN / 4)
56 
57 /* --- base64 functions. --- */
58 
59 char* grpc_base64_encode(const void* vdata, size_t data_size, int url_safe,
60  int multiline) {
61  size_t result_projected_size =
62  grpc_base64_estimate_encoded_size(data_size, multiline);
63  char* result = static_cast<char*>(gpr_malloc(result_projected_size));
64  grpc_base64_encode_core(result, vdata, data_size, url_safe, multiline);
65  return result;
66 }
67 
68 size_t grpc_base64_estimate_encoded_size(size_t data_size, int multiline) {
69  size_t result_projected_size =
70  4 * ((data_size + 3) / 3) +
71  2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS))
72  : 0) +
73  1;
74  return result_projected_size;
75 }
76 
77 void grpc_base64_encode_core(char* result, const void* vdata, size_t data_size,
78  int url_safe, int multiline) {
79  const unsigned char* data = static_cast<const unsigned char*>(vdata);
80  const char* base64_chars =
82  const size_t result_projected_size =
83  grpc_base64_estimate_encoded_size(data_size, multiline);
84 
85  char* current = result;
86  size_t num_blocks = 0;
87  size_t i = 0;
88 
89  /* Encode each block. */
90  while (data_size >= 3) {
91  *current++ = base64_chars[(data[i] >> 2) & 0x3F];
92  *current++ =
93  base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
94  *current++ =
95  base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
96  *current++ = base64_chars[data[i + 2] & 0x3F];
97 
98  data_size -= 3;
99  i += 3;
100  if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) {
101  *current++ = '\r';
102  *current++ = '\n';
103  num_blocks = 0;
104  }
105  }
106 
107  /* Take care of the tail. */
108  if (data_size == 2) {
109  *current++ = base64_chars[(data[i] >> 2) & 0x3F];
110  *current++ =
111  base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
112  *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
113  *current++ = GRPC_BASE64_PAD_CHAR;
114  } else if (data_size == 1) {
115  *current++ = base64_chars[(data[i] >> 2) & 0x3F];
116  *current++ = base64_chars[(data[i] & 0x03) << 4];
117  *current++ = GRPC_BASE64_PAD_CHAR;
118  *current++ = GRPC_BASE64_PAD_CHAR;
119  }
120 
121  GPR_ASSERT(current >= result);
122  GPR_ASSERT((uintptr_t)(current - result) < result_projected_size);
123  result[current - result] = '\0';
124 }
125 
126 grpc_slice grpc_base64_decode(const char* b64, int url_safe) {
127  return grpc_base64_decode_with_len(b64, strlen(b64), url_safe);
128 }
129 
130 static void decode_one_char(const unsigned char* codes, unsigned char* result,
131  size_t* result_offset) {
132  uint32_t packed = (static_cast<uint32_t>(codes[0]) << 2) |
133  (static_cast<uint32_t>(codes[1]) >> 4);
134  result[(*result_offset)++] = static_cast<unsigned char>(packed);
135 }
136 
137 static void decode_two_chars(const unsigned char* codes, unsigned char* result,
138  size_t* result_offset) {
139  uint32_t packed = (static_cast<uint32_t>(codes[0]) << 10) |
140  (static_cast<uint32_t>(codes[1]) << 4) |
141  (static_cast<uint32_t>(codes[2]) >> 2);
142  result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8);
143  result[(*result_offset)++] = static_cast<unsigned char>(packed);
144 }
145 
146 static int decode_group(const unsigned char* codes, size_t num_codes,
147  unsigned char* result, size_t* result_offset) {
148  GPR_ASSERT(num_codes <= 4);
149 
150  /* Short end groups that may not have padding. */
151  if (num_codes == 1) {
152  gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes.");
153  return 0;
154  }
155  if (num_codes == 2) {
156  decode_one_char(codes, result, result_offset);
157  return 1;
158  }
159  if (num_codes == 3) {
160  decode_two_chars(codes, result, result_offset);
161  return 1;
162  }
163 
164  /* Regular 4 byte groups with padding or not. */
165  GPR_ASSERT(num_codes == 4);
167  gpr_log(GPR_ERROR, "Invalid padding detected.");
168  return 0;
169  }
170  if (codes[2] == GRPC_BASE64_PAD_BYTE) {
171  if (codes[3] == GRPC_BASE64_PAD_BYTE) {
172  decode_one_char(codes, result, result_offset);
173  } else {
174  gpr_log(GPR_ERROR, "Invalid padding detected.");
175  return 0;
176  }
177  } else if (codes[3] == GRPC_BASE64_PAD_BYTE) {
178  decode_two_chars(codes, result, result_offset);
179  } else {
180  /* No padding. */
181  uint32_t packed = (static_cast<uint32_t>(codes[0]) << 18) |
182  (static_cast<uint32_t>(codes[1]) << 12) |
183  (static_cast<uint32_t>(codes[2]) << 6) | codes[3];
184  result[(*result_offset)++] = static_cast<unsigned char>(packed >> 16);
185  result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8);
186  result[(*result_offset)++] = static_cast<unsigned char>(packed);
187  }
188  return 1;
189 }
190 
191 grpc_slice grpc_base64_decode_with_len(const char* b64, size_t b64_len,
192  int url_safe) {
194  unsigned char* current = GRPC_SLICE_START_PTR(result);
195  size_t result_size = 0;
196  unsigned char codes[4];
197  size_t num_codes = 0;
198 
199  while (b64_len--) {
200  unsigned char c = static_cast<unsigned char>(*b64++);
201  signed char code;
202  if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue;
203  if (url_safe) {
204  if (c == '+' || c == '/') {
205  gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c);
206  goto fail;
207  }
208  if (c == '-') {
209  c = '+';
210  } else if (c == '_') {
211  c = '/';
212  }
213  }
214  code = base64_bytes[c];
215  if (code == -1) {
216  if (c != '\r' && c != '\n') {
217  gpr_log(GPR_ERROR, "Invalid character %c", c);
218  goto fail;
219  }
220  } else {
221  codes[num_codes++] = static_cast<unsigned char>(code);
222  if (num_codes == 4) {
223  if (!decode_group(codes, num_codes, current, &result_size)) goto fail;
224  num_codes = 0;
225  }
226  }
227  }
228 
229  if (num_codes != 0 &&
230  !decode_group(codes, num_codes, current, &result_size)) {
231  goto fail;
232  }
233  GRPC_SLICE_SET_LENGTH(result, result_size);
234  return result;
235 
236 fail:
238  return grpc_empty_slice();
239 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
decode_one_char
static void decode_one_char(const unsigned char *codes, unsigned char *result, size_t *result_offset)
Definition: b64.cc:130
base64_url_unsafe_chars
static const char base64_url_unsafe_chars[]
Definition: b64.cc:47
grpc_base64_encode_core
void grpc_base64_encode_core(char *result, const void *vdata, size_t data_size, int url_safe, int multiline)
Definition: b64.cc:77
string.h
useful.h
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
GRPC_SLICE_SET_LENGTH
#define GRPC_SLICE_SET_LENGTH(slice, newlen)
Definition: include/grpc/impl/codegen/slice.h:107
decode_two_chars
static void decode_two_chars(const unsigned char *codes, unsigned char *result, size_t *result_offset)
Definition: b64.cc:137
GRPC_BASE64_PAD_BYTE
#define GRPC_BASE64_PAD_BYTE
Definition: b64.cc:53
grpc_status._async.code
code
Definition: grpcio_status/grpc_status/_async.py:34
GRPC_BASE64_PAD_CHAR
#define GRPC_BASE64_PAD_CHAR
Definition: b64.cc:52
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_base64_decode_with_len
grpc_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len, int url_safe)
Definition: b64.cc:191
GRPC_BASE64_MULTILINE_NUM_BLOCKS
#define GRPC_BASE64_MULTILINE_NUM_BLOCKS
Definition: b64.cc:55
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_base64_decode
grpc_slice grpc_base64_decode(const char *b64, int url_safe)
Definition: b64.cc:126
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::fail
Poll< absl::StatusOr< std::tuple< T... > > > fail()
Definition: try_join_test.cc:45
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
b64.h
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
stdint.h
grpc_base64_encode
char * grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, int multiline)
Definition: b64.cc:59
grpc_base64_estimate_encoded_size
size_t grpc_base64_estimate_encoded_size(size_t data_size, int multiline)
Definition: b64.cc:68
base64_bytes
static const int8_t base64_bytes[]
Definition: b64.cc:34
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
google::protobuf::compiler::csharp::base64_chars
static const char base64_chars[]
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:437
alloc.h
codes
int codes(struct state *s, const struct huffman *lencode, const struct huffman *distcode)
Definition: bloaty/third_party/zlib/contrib/puff/puff.c:436
slice_refcount.h
decode_group
static int decode_group(const unsigned char *codes, size_t num_codes, unsigned char *result, size_t *result_offset)
Definition: b64.cc:146
int8_t
signed char int8_t
Definition: stdint-msvc2008.h:75
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
base64_url_safe_chars
static const char base64_url_safe_chars[]
Definition: b64.cc:49
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:35