charmap.pl 3.6 KB

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