aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/codegen/core/gen_percent_encoding_tables.c
blob: 49ea5ea53b4fdaaadd35da01a97cbbf41b547c63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/* generates constant table for metadata.c */

#include <stdio.h>
#include <string.h>

static unsigned char legal_bits[256 / 8];

static void legal(int x) {
  int byte = x / 8;
  int bit = x % 8;
  /* NB: the following integer arithmetic operation needs to be in its
   * expanded form due to the "integral promotion" performed (see section
   * 3.2.1.1 of the C89 draft standard). A cast to the smaller container type
   * is then required to avoid the compiler warning */
  legal_bits[byte] =
      (unsigned char)((legal_bits[byte] | (unsigned char)(1 << bit)));
}

static void dump(const char *name) {
  int i;

  printf("const uint8_t %s[256/8] = ", name);
  for (i = 0; i < 256 / 8; i++)
    printf("%c 0x%02x", i ? ',' : '{', legal_bits[i]);
  printf(" };\n");
}

static void clear(void) { memset(legal_bits, 0, sizeof(legal_bits)); }

int main(void) {
  int i;

  clear();
  for (i = 'a'; i <= 'z'; i++) legal(i);
  for (i = 'A'; i <= 'Z'; i++) legal(i);
  for (i = '0'; i <= '9'; i++) legal(i);
  legal('-');
  legal('_');
  legal('.');
  legal('~');
  dump("grpc_url_percent_encoding_unreserved_bytes");

  clear();
  for (i = 32; i <= 126; i++) {
    if (i == '%') continue;
    legal(i);
  }
  dump("grpc_compatible_percent_encoding_unreserved_bytes");

  return 0;
}