xref: /openssl/crypto/asn1/charmap.pl (revision c37b9479)
1#! /usr/bin/env perl
2# Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (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
9use strict;
10use FindBin;
11use lib "$FindBin::Bin/../../util/perl";
12use OpenSSL::copyright;
13
14my ($i, @arr);
15
16# Set up an array with the type of ASCII characters
17# Each set bit represents a character property.
18
19# RFC2253 character properties
20my $RFC2253_ESC = 1;	# Character escaped with \
21my $ESC_CTRL	= 2;	# Escaped control character
22# These are used with RFC1779 quoting using "
23my $NOESC_QUOTE	= 8;	# Not escaped if quoted
24my $PSTRING_CHAR = 0x10;	# Valid PrintableString character
25my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
26my $RFC2253_LAST_ESC = 0x40;  # Escaped with \ if last character
27my $RFC2254_ESC = 0x400;	# Character escaped \XX
28my $HOST_ANY = 0x1000;      # Valid hostname character anywhere in label
29my $HOST_DOT = 0x2000;  # Dot: hostname label separator
30my $HOST_HYPHEN = 0x4000; # Hyphen: not valid at start or end.
31my $HOST_WILD = 0x8000; # Wildcard character
32
33for($i = 0; $i < 128; $i++) {
34	# Set the RFC2253 escape characters (control)
35	$arr[$i] = 0;
36	if(($i < 32) || ($i > 126)) {
37		$arr[$i] |= $ESC_CTRL;
38	}
39
40	# Some PrintableString characters
41	if(		   ( ( $i >= ord("a")) && ( $i <= ord("z")) )
42			|| (  ( $i >= ord("A")) && ( $i <= ord("Z")) )
43			|| (  ( $i >= ord("0")) && ( $i <= ord("9")) )  ) {
44		$arr[$i] |= $PSTRING_CHAR | $HOST_ANY;
45	}
46}
47
48# Now setup the rest
49
50# Remaining RFC2253 escaped characters
51
52$arr[ord(" ")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC | $RFC2253_LAST_ESC;
53$arr[ord("#")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC;
54
55$arr[ord(",")] |= $NOESC_QUOTE | $RFC2253_ESC;
56$arr[ord("+")] |= $NOESC_QUOTE | $RFC2253_ESC;
57$arr[ord("\"")] |= $RFC2253_ESC;
58$arr[ord("\\")] |= $RFC2253_ESC;
59$arr[ord("<")] |= $NOESC_QUOTE | $RFC2253_ESC;
60$arr[ord(">")] |= $NOESC_QUOTE | $RFC2253_ESC;
61$arr[ord(";")] |= $NOESC_QUOTE | $RFC2253_ESC;
62
63# Remaining RFC2254 characters
64
65$arr[0] |= $RFC2254_ESC;
66$arr[ord("(")] |= $RFC2254_ESC;
67$arr[ord(")")] |= $RFC2254_ESC;
68$arr[ord("*")] |= $RFC2254_ESC | $HOST_WILD;
69$arr[ord("\\")] |= $RFC2254_ESC;
70
71# Remaining PrintableString characters
72
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$arr[ord(",")] |= $PSTRING_CHAR;
79$arr[ord("-")] |= $PSTRING_CHAR | $HOST_HYPHEN;
80$arr[ord(".")] |= $PSTRING_CHAR | $HOST_DOT;
81$arr[ord("/")] |= $PSTRING_CHAR;
82$arr[ord(":")] |= $PSTRING_CHAR;
83$arr[ord("=")] |= $PSTRING_CHAR;
84$arr[ord("?")] |= $PSTRING_CHAR;
85
86# Now generate the C code
87
88# Year the file was generated.
89my $YEAR = OpenSSL::copyright::year_of($0);
90
91print <<EOF;
92/*
93 * WARNING: do not edit!
94 * Generated by crypto/asn1/charmap.pl
95 *
96 * Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
97 *
98 * Licensed under the Apache License 2.0 (the "License").  You may not use
99 * this file except in compliance with the License.  You can obtain a copy
100 * in the file LICENSE in the source distribution or at
101 * https://www.openssl.org/source/license.html
102 */
103
104#define CHARTYPE_HOST_ANY $HOST_ANY
105#define CHARTYPE_HOST_DOT $HOST_DOT
106#define CHARTYPE_HOST_HYPHEN $HOST_HYPHEN
107#define CHARTYPE_HOST_WILD $HOST_WILD
108
109/*
110 * Mask of various character properties
111 */
112
113static const unsigned short char_type[] = {
114EOF
115
116print "   ";
117for($i = 0; $i < 128; $i++) {
118	print("\n   ") if($i && (($i % 12) == 0));
119	printf(" %4d", $arr[$i]);
120	print(",") if ($i != 127);
121}
122print("\n};\n");
123
124