xref: /freebsd/crypto/openssl/crypto/asn1/charmap.pl (revision dbd5678d)
1#! /usr/bin/env perl
2# Copyright 2000-2022 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
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);
90print <<EOF;
91/*
92 * WARNING: do not edit!
93 * Generated by crypto/asn1/charmap.pl
94 *
95 * Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
96 *
97 * Licensed under the OpenSSL license (the "License").  You may not use
98 * this file except in compliance with the License.  You can obtain a copy
99 * in the file LICENSE in the source distribution or at
100 * https://www.openssl.org/source/license.html
101 */
102
103#define CHARTYPE_HOST_ANY $HOST_ANY
104#define CHARTYPE_HOST_DOT $HOST_DOT
105#define CHARTYPE_HOST_HYPHEN $HOST_HYPHEN
106#define CHARTYPE_HOST_WILD $HOST_WILD
107
108/*
109 * Mask of various character properties
110 */
111
112static const unsigned short char_type[] = {
113EOF
114
115print "   ";
116for($i = 0; $i < 128; $i++) {
117	print("\n   ") if($i && (($i % 12) == 0));
118	printf(" %4d", $arr[$i]);
119	print(",") if ($i != 127);
120}
121print("\n};\n");
122
123