fbabd2464f6ba7f9ca083b431b465bb36b649888
[platform/upstream/grpc.git] / tools / codegen / core / gen_legal_metadata_characters.cc
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
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
37 static void dump(void) {
38   int i;
39
40   printf("static const uint8_t legal_header_bits[256/8] = ");
41   for (i = 0; i < 256 / 8; i++)
42     printf("%c 0x%02x", i ? ',' : '{', legal_bits[i]);
43   printf(" };\n");
44 }
45
46 static void clear(void) { memset(legal_bits, 0, sizeof(legal_bits)); }
47
48 int main(void) {
49   int i;
50
51   clear();
52   for (i = 'a'; i <= 'z'; i++) legal(i);
53   for (i = '0'; i <= '9'; i++) legal(i);
54   legal('-');
55   legal('_');
56   legal('.');
57   dump();
58
59   clear();
60   for (i = 32; i <= 126; i++) {
61     legal(i);
62   }
63   dump();
64
65   return 0;
66 }