• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2018 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 "test/core/util/fuzzer_util.h"
20 
21 #include <grpc/support/alloc.h>
22 
23 #include "src/core/lib/gpr/useful.h"
24 
25 namespace grpc_core {
26 namespace testing {
27 
grpc_fuzzer_get_next_byte(input_stream * inp)28 uint8_t grpc_fuzzer_get_next_byte(input_stream* inp) {
29   if (inp->cur == inp->end) {
30     return 0;
31   }
32   return *inp->cur++;
33 }
34 
grpc_fuzzer_get_next_string(input_stream * inp,bool * special)35 char* grpc_fuzzer_get_next_string(input_stream* inp, bool* special) {
36   char* str = nullptr;
37   size_t cap = 0;
38   size_t sz = 0;
39   char c;
40   do {
41     if (cap == sz) {
42       cap = GPR_MAX(3 * cap / 2, cap + 8);
43       str = static_cast<char*>(gpr_realloc(str, cap));
44     }
45     c = static_cast<char>(grpc_fuzzer_get_next_byte(inp));
46     str[sz++] = c;
47   } while (c != 0 && c != 1);
48   if (special != nullptr) {
49     *special = (c == 1);
50   }
51   if (c == 1) {
52     str[sz - 1] = 0;
53   }
54   return str;
55 }
56 
grpc_fuzzer_get_next_uint32(input_stream * inp)57 uint32_t grpc_fuzzer_get_next_uint32(input_stream* inp) {
58   uint8_t b = grpc_fuzzer_get_next_byte(inp);
59   uint32_t x = b & 0x7f;
60   if (b & 0x80) {
61     x <<= 7;
62     b = grpc_fuzzer_get_next_byte(inp);
63     x |= b & 0x7f;
64     if (b & 0x80) {
65       x <<= 7;
66       b = grpc_fuzzer_get_next_byte(inp);
67       x |= b & 0x7f;
68       if (b & 0x80) {
69         x <<= 7;
70         b = grpc_fuzzer_get_next_byte(inp);
71         x |= b & 0x7f;
72         if (b & 0x80) {
73           x = (x << 4) | (grpc_fuzzer_get_next_byte(inp) & 0x0f);
74         }
75       }
76     }
77   }
78   return x;
79 }
80 
81 }  // namespace testing
82 }  // namespace grpc_core
83