charmap.pl 3.6 KB

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