• 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, 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 multiline)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 
grpc_base64_encode_core(char * result,const void * vdata,size_t data_size,int url_safe,int multiline)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 =
81       url_safe ? base64_url_safe_chars : base64_url_unsafe_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 
grpc_base64_decode(const char * b64,int url_safe)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 
decode_one_char(const unsigned char * codes,unsigned char * result,size_t * result_offset)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 
decode_two_chars(const unsigned char * codes,unsigned char * result,size_t * result_offset)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 
decode_group(const unsigned char * codes,size_t num_codes,unsigned char * result,size_t * result_offset)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);
166   if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) {
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 
grpc_base64_decode_with_len(const char * b64,size_t b64_len,int url_safe)191 grpc_slice grpc_base64_decode_with_len(const char* b64, size_t b64_len,
192                                        int url_safe) {
193   grpc_slice result = GRPC_SLICE_MALLOC(b64_len);
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:
237   grpc_slice_unref_internal(result);
238   return grpc_empty_slice();
239 }
240