• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <grpc/support/port_platform.h>
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 
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/slice/slice_internal.h"
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 
grpc_base64_encode(const void * vdata,size_t data_size,int url_safe,int multiline)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, url_safe, 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 
grpc_base64_estimate_encoded_size(size_t data_size,int url_safe,int multiline)68 size_t grpc_base64_estimate_encoded_size(size_t data_size, int url_safe,
69                                          int multiline) {
70   size_t result_projected_size =
71       4 * ((data_size + 3) / 3) +
72       2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS))
73                      : 0) +
74       1;
75   return result_projected_size;
76 }
77 
grpc_base64_encode_core(char * result,const void * vdata,size_t data_size,int url_safe,int multiline)78 void grpc_base64_encode_core(char* result, const void* vdata, size_t data_size,
79                              int url_safe, int multiline) {
80   const unsigned char* data = static_cast<const unsigned char*>(vdata);
81   const char* base64_chars =
82       url_safe ? base64_url_safe_chars : base64_url_unsafe_chars;
83   const size_t result_projected_size =
84       grpc_base64_estimate_encoded_size(data_size, url_safe, multiline);
85 
86   char* current = result;
87   size_t num_blocks = 0;
88   size_t i = 0;
89 
90   /* Encode each block. */
91   while (data_size >= 3) {
92     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
93     *current++ =
94         base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
95     *current++ =
96         base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
97     *current++ = base64_chars[data[i + 2] & 0x3F];
98 
99     data_size -= 3;
100     i += 3;
101     if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) {
102       *current++ = '\r';
103       *current++ = '\n';
104       num_blocks = 0;
105     }
106   }
107 
108   /* Take care of the tail. */
109   if (data_size == 2) {
110     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
111     *current++ =
112         base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
113     *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
114     *current++ = GRPC_BASE64_PAD_CHAR;
115   } else if (data_size == 1) {
116     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
117     *current++ = base64_chars[(data[i] & 0x03) << 4];
118     *current++ = GRPC_BASE64_PAD_CHAR;
119     *current++ = GRPC_BASE64_PAD_CHAR;
120   }
121 
122   GPR_ASSERT(current >= result);
123   GPR_ASSERT((uintptr_t)(current - result) < result_projected_size);
124   result[current - result] = '\0';
125 }
126 
grpc_base64_decode(const char * b64,int url_safe)127 grpc_slice grpc_base64_decode(const char* b64, int url_safe) {
128   return grpc_base64_decode_with_len(b64, strlen(b64), url_safe);
129 }
130 
decode_one_char(const unsigned char * codes,unsigned char * result,size_t * result_offset)131 static void decode_one_char(const unsigned char* codes, unsigned char* result,
132                             size_t* result_offset) {
133   uint32_t packed = (static_cast<uint32_t>(codes[0]) << 2) |
134                     (static_cast<uint32_t>(codes[1]) >> 4);
135   result[(*result_offset)++] = static_cast<unsigned char>(packed);
136 }
137 
decode_two_chars(const unsigned char * codes,unsigned char * result,size_t * result_offset)138 static void decode_two_chars(const unsigned char* codes, unsigned char* result,
139                              size_t* result_offset) {
140   uint32_t packed = (static_cast<uint32_t>(codes[0]) << 10) |
141                     (static_cast<uint32_t>(codes[1]) << 4) |
142                     (static_cast<uint32_t>(codes[2]) >> 2);
143   result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8);
144   result[(*result_offset)++] = static_cast<unsigned char>(packed);
145 }
146 
decode_group(const unsigned char * codes,size_t num_codes,unsigned char * result,size_t * result_offset)147 static int decode_group(const unsigned char* codes, size_t num_codes,
148                         unsigned char* result, size_t* result_offset) {
149   GPR_ASSERT(num_codes <= 4);
150 
151   /* Short end groups that may not have padding. */
152   if (num_codes == 1) {
153     gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes.");
154     return 0;
155   }
156   if (num_codes == 2) {
157     decode_one_char(codes, result, result_offset);
158     return 1;
159   }
160   if (num_codes == 3) {
161     decode_two_chars(codes, result, result_offset);
162     return 1;
163   }
164 
165   /* Regular 4 byte groups with padding or not. */
166   GPR_ASSERT(num_codes == 4);
167   if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) {
168     gpr_log(GPR_ERROR, "Invalid padding detected.");
169     return 0;
170   }
171   if (codes[2] == GRPC_BASE64_PAD_BYTE) {
172     if (codes[3] == GRPC_BASE64_PAD_BYTE) {
173       decode_one_char(codes, result, result_offset);
174     } else {
175       gpr_log(GPR_ERROR, "Invalid padding detected.");
176       return 0;
177     }
178   } else if (codes[3] == GRPC_BASE64_PAD_BYTE) {
179     decode_two_chars(codes, result, result_offset);
180   } else {
181     /* No padding. */
182     uint32_t packed = (static_cast<uint32_t>(codes[0]) << 18) |
183                       (static_cast<uint32_t>(codes[1]) << 12) |
184                       (static_cast<uint32_t>(codes[2]) << 6) | codes[3];
185     result[(*result_offset)++] = static_cast<unsigned char>(packed >> 16);
186     result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8);
187     result[(*result_offset)++] = static_cast<unsigned char>(packed);
188   }
189   return 1;
190 }
191 
grpc_base64_decode_with_len(const char * b64,size_t b64_len,int url_safe)192 grpc_slice grpc_base64_decode_with_len(const char* b64, size_t b64_len,
193                                        int url_safe) {
194   grpc_slice result = GRPC_SLICE_MALLOC(b64_len);
195   unsigned char* current = GRPC_SLICE_START_PTR(result);
196   size_t result_size = 0;
197   unsigned char codes[4];
198   size_t num_codes = 0;
199 
200   while (b64_len--) {
201     unsigned char c = static_cast<unsigned char>(*b64++);
202     signed char code;
203     if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue;
204     if (url_safe) {
205       if (c == '+' || c == '/') {
206         gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c);
207         goto fail;
208       }
209       if (c == '-') {
210         c = '+';
211       } else if (c == '_') {
212         c = '/';
213       }
214     }
215     code = base64_bytes[c];
216     if (code == -1) {
217       if (c != '\r' && c != '\n') {
218         gpr_log(GPR_ERROR, "Invalid character %c", c);
219         goto fail;
220       }
221     } else {
222       codes[num_codes++] = static_cast<unsigned char>(code);
223       if (num_codes == 4) {
224         if (!decode_group(codes, num_codes, current, &result_size)) goto fail;
225         num_codes = 0;
226       }
227     }
228   }
229 
230   if (num_codes != 0 &&
231       !decode_group(codes, num_codes, current, &result_size)) {
232     goto fail;
233   }
234   GRPC_SLICE_SET_LENGTH(result, result_size);
235   return result;
236 
237 fail:
238   grpc_slice_unref_internal(result);
239   return grpc_empty_slice();
240 }
241