Avoid signed overflow
[openssl.git] / crypto / asn1 / charmap.pl
1 #! /usr/bin/env perl
2 # Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use strict;
10
11 my ($i, @arr);
12
13 # Set up an array with the type of ASCII characters
14 # Each set bit represents a character property.
15
16 # RFC2253 character properties
17 my $RFC2253_ESC = 1;    # Character escaped with \
18 my $ESC_CTRL    = 2;    # Escaped control character
19 # These are used with RFC1779 quoting using "
20 my $NOESC_QUOTE = 8;    # Not escaped if quoted
21 my $PSTRING_CHAR = 0x10;        # Valid PrintableString character
22 my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
23 my $RFC2253_LAST_ESC = 0x40;  # Escaped with \ if last character
24 my $RFC2254_ESC = 0x400;        # Character escaped \XX
25
26 for($i = 0; $i < 128; $i++) {
27         # Set the RFC2253 escape characters (control)
28         $arr[$i] = 0;
29         if(($i < 32) || ($i > 126)) {
30                 $arr[$i] |= $ESC_CTRL;
31         }
32
33         # Some PrintableString characters
34         if(                ( ( $i >= ord("a")) && ( $i <= ord("z")) )
35                         || (  ( $i >= ord("A")) && ( $i <= ord("Z")) )
36                         || (  ( $i >= ord("0")) && ( $i <= ord("9")) )  ) {
37                 $arr[$i] |= $PSTRING_CHAR;
38         }
39 }
40
41 # Now setup the rest
42
43 # Remaining RFC2253 escaped characters
44
45 $arr[ord(" ")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC | $RFC2253_LAST_ESC;
46 $arr[ord("#")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC;
47
48 $arr[ord(",")] |= $NOESC_QUOTE | $RFC2253_ESC;
49 $arr[ord("+")] |= $NOESC_QUOTE | $RFC2253_ESC;
50 $arr[ord("\"")] |= $RFC2253_ESC;
51 $arr[ord("\\")] |= $RFC2253_ESC;
52 $arr[ord("<")] |= $NOESC_QUOTE | $RFC2253_ESC;
53 $arr[ord(">")] |= $NOESC_QUOTE | $RFC2253_ESC;
54 $arr[ord(";")] |= $NOESC_QUOTE | $RFC2253_ESC;
55
56 # Remaining RFC2254 characters
57
58 $arr[0] |= $RFC2254_ESC;
59 $arr[ord("(")] |= $RFC2254_ESC;
60 $arr[ord(")")] |= $RFC2254_ESC;
61 $arr[ord("*")] |= $RFC2254_ESC;
62 $arr[ord("\\")] |= $RFC2254_ESC;
63
64 # Remaining PrintableString characters
65
66 $arr[ord(" ")] |= $PSTRING_CHAR;
67 $arr[ord("'")] |= $PSTRING_CHAR;
68 $arr[ord("(")] |= $PSTRING_CHAR;
69 $arr[ord(")")] |= $PSTRING_CHAR;
70 $arr[ord("+")] |= $PSTRING_CHAR;
71 $arr[ord(",")] |= $PSTRING_CHAR;
72 $arr[ord("-")] |= $PSTRING_CHAR;
73 $arr[ord(".")] |= $PSTRING_CHAR;
74 $arr[ord("/")] |= $PSTRING_CHAR;
75 $arr[ord(":")] |= $PSTRING_CHAR;
76 $arr[ord("=")] |= $PSTRING_CHAR;
77 $arr[ord("?")] |= $PSTRING_CHAR;
78
79 # Now generate the C code
80
81 print <<EOF;
82 /*
83  * WARNING: do not edit!
84  * Generated by crypto/asn1/charmap.pl
85  *
86  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
87  *
88  * Licensed under the OpenSSL license (the "License").  You may not use
89  * this file except in compliance with the License.  You can obtain a copy
90  * in the file LICENSE in the source distribution or at
91  * https://www.openssl.org/source/license.html
92  */
93
94 /*
95  * Mask of various character properties
96  */
97
98 static const unsigned short char_type[] = {
99 EOF
100
101 print "   ";
102 for($i = 0; $i < 128; $i++) {
103         print("\n   ") if($i && (($i % 16) == 0));
104         printf(" %2d", $arr[$i]);
105         print(",") if ($i != 127);
106 }
107 print("\n};\n");
108