• 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 /* generates constant table for metadata.cc */
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 static unsigned char legal_bits[256 / 8];
25 
legal(int x)26 static void legal(int x) {
27   int byte = x / 8;
28   int bit = x % 8;
29   /* NB: the following integer arithmetic operation needs to be in its
30    * expanded form due to the "integral promotion" performed (see section
31    * 3.2.1.1 of the C89 draft standard). A cast to the smaller container type
32    * is then required to avoid the compiler warning */
33   legal_bits[byte] =
34       (unsigned char)((legal_bits[byte] | (unsigned char)(1 << bit)));
35 }
36 
dump(const char * name)37 static void dump(const char *name) {
38   int i;
39 
40   printf("const uint8_t %s[256/8] = ", name);
41   for (i = 0; i < 256 / 8; i++)
42     printf("%c 0x%02x", i ? ',' : '{', legal_bits[i]);
43   printf(" };\n");
44 }
45 
clear(void)46 static void clear(void) { memset(legal_bits, 0, sizeof(legal_bits)); }
47 
main(void)48 int main(void) {
49   int i;
50 
51   clear();
52   for (i = 'a'; i <= 'z'; i++) legal(i);
53   for (i = 'A'; i <= 'Z'; i++) legal(i);
54   for (i = '0'; i <= '9'; i++) legal(i);
55   legal('-');
56   legal('_');
57   legal('.');
58   legal('~');
59   dump("grpc_url_percent_encoding_unreserved_bytes");
60 
61   clear();
62   for (i = 32; i <= 126; i++) {
63     if (i == '%') continue;
64     legal(i);
65   }
66   dump("grpc_compatible_percent_encoding_unreserved_bytes");
67 
68   return 0;
69 }
70