23 #include "absl/base/attributes.h"
31 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
32 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
33 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
34 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 62, 0x40, 0x40, 0x40, 63,
35 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x40, 0x40,
36 0x40, 0x40, 0x40, 0x40, 0x40, 0, 1, 2, 3, 4, 5, 6,
37 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
38 19, 20, 21, 22, 23, 24, 25, 0x40, 0x40, 0x40, 0x40, 0x40,
39 0x40, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
40 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
41 49, 50, 51, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
42 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
43 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
44 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
45 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
46 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
47 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
48 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
49 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
50 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
51 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
52 0x40, 0x40, 0x40, 0x40};
62 "Base64 decoding failed, invalid character '%c' in base64 "
64 static_cast<char>(*input_ptr));
71 #define COMPOSE_OUTPUT_BYTE_0(input_ptr) \
72 (uint8_t)((decode_table[(input_ptr)[0]] << 2) | \
73 (decode_table[(input_ptr)[1]] >> 4))
75 #define COMPOSE_OUTPUT_BYTE_1(input_ptr) \
76 (uint8_t)((decode_table[(input_ptr)[1]] << 4) | \
77 (decode_table[(input_ptr)[2]] >> 2))
79 #define COMPOSE_OUTPUT_BYTE_2(input_ptr) \
80 (uint8_t)((decode_table[(input_ptr)[2]] << 6) | decode_table[(input_ptr)[3]])
93 "Base64 decoding failed. Input has more than 2 paddings.");
96 size_t tuples =
len / 4;
97 size_t tail_case =
len % 4;
100 "Base64 decoding failed. Input has a length of %zu (without"
101 " padding), which is invalid.\n",
105 return tuples * 3 +
tail_xtra[tail_case];
111 if (
ctx->input_cur >
ctx->input_end ||
ctx->output_cur >
ctx->output_end) {
116 while (
ctx->input_end >=
ctx->input_cur + 4 &&
117 ctx->output_end >=
ctx->output_cur + 3) {
122 ctx->output_cur += 3;
127 input_tail =
static_cast<size_t>(
ctx->input_end -
ctx->input_cur);
128 if (input_tail == 4) {
130 if (
ctx->input_cur[3] ==
'=') {
131 if (
ctx->input_cur[2] ==
'=' &&
ctx->output_end >=
ctx->output_cur + 1) {
135 }
else if (
ctx->output_end >=
ctx->output_cur + 2) {
144 }
else if (
ctx->contains_tail && input_tail > 1) {
148 switch (input_tail) {
156 ctx->input_cur += input_tail;
165 size_t output_length = input_length / 4 * 3;
171 "Base64 decoding failed, input of "
172 "grpc_chttp2_base64_decode has a length of %d, which is not a "
174 static_cast<int>(input_length));
178 if (input_length > 0) {
193 ctx.contains_tail =
false;
208 size_t output_length) {
216 "Base64 decoding failed, input of "
217 "grpc_chttp2_base64_decode_with_length has a length of %d, which "
218 "has a tail of 1 byte.\n",
219 static_cast<int>(input_length));
225 input_length / 4 * 3 +
tail_xtra[input_length % 4])) {
228 "Base64 decoding failed, output_length %d is longer "
229 "than the max possible output length %d.\n",
230 static_cast<int>(output_length),
231 static_cast<int>(input_length / 4 * 3 +
tail_xtra[input_length % 4]));
240 ctx.contains_tail =
true;