aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/ext/transport
diff options
context:
space:
mode:
authorGravatar Muxi Yan <mxyan@google.com>2018-02-21 15:26:54 -0800
committerGravatar Muxi Yan <mxyan@google.com>2018-02-21 15:26:54 -0800
commit9ef1f359ca1dec335300067e6d4e3ce4da24b584 (patch)
tree9df9aa80cdb170d5bab304b7e9ec1b9658649f22 /src/core/ext/transport
parent09bd5c0b1b9d5748b8a272e8495ecd0c9fd1602c (diff)
Put infer_length_after_decode in bin_decoder
Diffstat (limited to 'src/core/ext/transport')
-rw-r--r--src/core/ext/transport/chttp2/transport/bin_decoder.cc18
-rw-r--r--src/core/ext/transport/chttp2/transport/bin_decoder.h3
-rw-r--r--src/core/ext/transport/cronet/transport/cronet_transport.cc22
3 files changed, 22 insertions, 21 deletions
diff --git a/src/core/ext/transport/chttp2/transport/bin_decoder.cc b/src/core/ext/transport/chttp2/transport/bin_decoder.cc
index 91980e6974..53b8622259 100644
--- a/src/core/ext/transport/chttp2/transport/bin_decoder.cc
+++ b/src/core/ext/transport/chttp2/transport/bin_decoder.cc
@@ -75,6 +75,24 @@ static bool input_is_valid(uint8_t* input_ptr, size_t length) {
#define COMPOSE_OUTPUT_BYTE_2(input_ptr) \
(uint8_t)((decode_table[input_ptr[2]] << 6) | decode_table[input_ptr[3]])
+size_t grpc_base64_infer_length_after_decode(const grpc_slice& slice) {
+ size_t len = GRPC_SLICE_LENGTH(slice);
+ const uint8_t* bytes = GRPC_SLICE_START_PTR(slice);
+ while (len > 0 && bytes[len - 1] == '=') {
+ len--;
+ }
+ size_t tuples = len / 4;
+ size_t tail_case = len % 4;
+ if (tail_case == 1) {
+ gpr_log(GPR_ERROR,
+ "Base64 decoding failed. Input has a length of %zu (without"
+ " padding), which is invalid.\n",
+ len);
+ tail_case = 0;
+ }
+ return tuples * 3 + tail_xtra[tail_case];
+}
+
bool grpc_base64_decode_partial(struct grpc_base64_decode_context* ctx) {
size_t input_tail;
diff --git a/src/core/ext/transport/chttp2/transport/bin_decoder.h b/src/core/ext/transport/chttp2/transport/bin_decoder.h
index 9cb75ccd81..b5acb3fa91 100644
--- a/src/core/ext/transport/chttp2/transport/bin_decoder.h
+++ b/src/core/ext/transport/chttp2/transport/bin_decoder.h
@@ -48,4 +48,7 @@ grpc_slice grpc_chttp2_base64_decode(grpc_slice input);
grpc_slice grpc_chttp2_base64_decode_with_length(grpc_slice input,
size_t output_length);
+/* Infer the length of decoded data from encoded data. */
+size_t grpc_base64_infer_length_after_decode(const grpc_slice& slice);
+
#endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_BIN_DECODER_H */
diff --git a/src/core/ext/transport/cronet/transport/cronet_transport.cc b/src/core/ext/transport/cronet/transport/cronet_transport.cc
index 6c73c80eda..627082427d 100644
--- a/src/core/ext/transport/cronet/transport/cronet_transport.cc
+++ b/src/core/ext/transport/cronet/transport/cronet_transport.cc
@@ -216,26 +216,6 @@ void grpc_cronet_stream_unref(stream_obj* s) { grpc_stream_unref(s->refcount); }
static enum e_op_result execute_stream_op(struct op_and_state* oas);
-static const size_t tail_xtra[4] = {0, 0, 1, 2};
-
-static size_t infer_length_after_decode(const grpc_slice& slice) {
- size_t len = GRPC_SLICE_LENGTH(slice);
- const uint8_t* bytes = GRPC_SLICE_START_PTR(slice);
- while (len > 0 && bytes[len - 1] == '=') {
- len--;
- }
- size_t tuples = len / 4;
- size_t tail_case = len % 4;
- if (tail_case == 1) {
- gpr_log(GPR_ERROR,
- "Base64 decoding failed. Input has a length of %zu (without"
- " padding), which is invalid.\n",
- len);
- tail_case = 0;
- }
- return tuples * 3 + tail_xtra[tail_case];
-}
-
/*
Utility function to translate enum into string for printing
*/
@@ -427,7 +407,7 @@ static void convert_cronet_array_to_metadata(
if (grpc_is_binary_header(key)) {
value = grpc_slice_from_static_string(header_array->headers[i].value);
value = grpc_slice_intern(grpc_chttp2_base64_decode_with_length(
- value, infer_length_after_decode(value)));
+ value, grpc_base64_infer_length_after_decode(value)));
} else {
value = grpc_slice_intern(
grpc_slice_from_static_string(header_array->headers[i].value));