• 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 <grpc/byte_buffer_reader.h>
22 #include <string.h>
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/compression.h>
26 #include <grpc/grpc.h>
27 #include <grpc/slice_buffer.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 
31 #include "src/core/lib/compression/message_compress.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/slice/slice_internal.h"
34 
is_compressed(grpc_byte_buffer * buffer)35 static int is_compressed(grpc_byte_buffer* buffer) {
36   switch (buffer->type) {
37     case GRPC_BB_RAW:
38       if (buffer->data.raw.compression == GRPC_COMPRESS_NONE) {
39         return 0 /* GPR_FALSE */;
40       }
41       break;
42   }
43   return 1 /* GPR_TRUE */;
44 }
45 
grpc_byte_buffer_reader_init(grpc_byte_buffer_reader * reader,grpc_byte_buffer * buffer)46 int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader* reader,
47                                  grpc_byte_buffer* buffer) {
48   grpc_core::ExecCtx exec_ctx;
49   grpc_slice_buffer decompressed_slices_buffer;
50   reader->buffer_in = buffer;
51   switch (reader->buffer_in->type) {
52     case GRPC_BB_RAW:
53       grpc_slice_buffer_init(&decompressed_slices_buffer);
54       if (is_compressed(reader->buffer_in)) {
55         if (grpc_msg_decompress(
56 
57                 grpc_compression_algorithm_to_message_compression_algorithm(
58                     reader->buffer_in->data.raw.compression),
59                 &reader->buffer_in->data.raw.slice_buffer,
60                 &decompressed_slices_buffer) == 0) {
61           gpr_log(GPR_ERROR,
62                   "Unexpected error decompressing data for algorithm with enum "
63                   "value '%d'.",
64                   reader->buffer_in->data.raw.compression);
65           memset(reader, 0, sizeof(*reader));
66           return 0;
67         } else { /* all fine */
68           reader->buffer_out =
69               grpc_raw_byte_buffer_create(decompressed_slices_buffer.slices,
70                                           decompressed_slices_buffer.count);
71         }
72         grpc_slice_buffer_destroy_internal(&decompressed_slices_buffer);
73       } else { /* not compressed, use the input buffer as output */
74         reader->buffer_out = reader->buffer_in;
75       }
76       reader->current.index = 0;
77       break;
78   }
79 
80   return 1;
81 }
82 
grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader * reader)83 void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader* reader) {
84   switch (reader->buffer_in->type) {
85     case GRPC_BB_RAW:
86       /* keeping the same if-else structure as in the init function */
87       if (is_compressed(reader->buffer_in)) {
88         grpc_byte_buffer_destroy(reader->buffer_out);
89       }
90       break;
91   }
92 }
93 
grpc_byte_buffer_reader_next(grpc_byte_buffer_reader * reader,grpc_slice * slice)94 int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader* reader,
95                                  grpc_slice* slice) {
96   switch (reader->buffer_in->type) {
97     case GRPC_BB_RAW: {
98       grpc_slice_buffer* slice_buffer;
99       slice_buffer = &reader->buffer_out->data.raw.slice_buffer;
100       if (reader->current.index < slice_buffer->count) {
101         *slice = grpc_slice_ref_internal(
102             slice_buffer->slices[reader->current.index]);
103         reader->current.index += 1;
104         return 1;
105       }
106       break;
107     }
108   }
109   return 0;
110 }
111 
grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader * reader)112 grpc_slice grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader* reader) {
113   grpc_slice in_slice;
114   size_t bytes_read = 0;
115   const size_t input_size = grpc_byte_buffer_length(reader->buffer_out);
116   grpc_slice out_slice = GRPC_SLICE_MALLOC(input_size);
117   uint8_t* const outbuf = GRPC_SLICE_START_PTR(out_slice); /* just an alias */
118 
119   grpc_core::ExecCtx exec_ctx;
120   while (grpc_byte_buffer_reader_next(reader, &in_slice) != 0) {
121     const size_t slice_length = GRPC_SLICE_LENGTH(in_slice);
122     memcpy(&(outbuf[bytes_read]), GRPC_SLICE_START_PTR(in_slice), slice_length);
123     bytes_read += slice_length;
124     grpc_slice_unref_internal(in_slice);
125     GPR_ASSERT(bytes_read <= input_size);
126   }
127 
128   return out_slice;
129 }
130