charmap.pl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/local/bin/perl -w
  2. use strict;
  3. my ($i, @arr);
  4. # Set up an array with the type of ASCII characters
  5. # Each set bit represents a character property.
  6. # RFC2253 character properties
  7. my $RFC2253_ESC = 1; # Character escaped with \
  8. my $ESC_CTRL = 2; # Escaped control character
  9. # These are used with RFC1779 quoting using "
  10. my $NOESC_QUOTE = 8; # Not escaped if quoted
  11. my $PSTRING_CHAR = 0x10; # Valid PrintableString character
  12. my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
  13. my $RFC2253_LAST_ESC = 0x40; # Escaped with \ if last character
  14. for($i = 0; $i < 128; $i++) {
  15. # Set the RFC2253 escape characters (control)
  16. $arr[$i] = 0;
  17. if(($i < 32) || ($i > 126)) {
  18. $arr[$i] |= $ESC_CTRL;
  19. }
  20. # Some PrintableString characters
  21. if( ( ( $i >= ord("a")) && ( $i <= ord("z")) )
  22. || ( ( $i >= ord("A")) && ( $i <= ord("Z")) )
  23. || ( ( $i >= ord("0")) && ( $i <= ord("9")) ) ) {
  24. $arr[$i] |= $PSTRING_CHAR;
  25. }
  26. }
  27. # Now setup the rest
  28. # Remaining RFC2253 escaped characters
  29. $arr[ord(" ")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC | $RFC2253_LAST_ESC;
  30. $arr[ord("#")] |= $NOESC_QUOTE | $RFC2253_FIRST_ESC;
  31. $arr[ord(",")] |= $NOESC_QUOTE | $RFC2253_ESC;
  32. $arr[ord("+")] |= $NOESC_QUOTE | $RFC2253_ESC;
  33. $arr[ord("\"")] |= $RFC2253_ESC;
  34. $arr[ord("\\")] |= $RFC2253_ESC;
  35. $arr[ord("<")] |= $NOESC_QUOTE | $RFC2253_ESC;
  36. $arr[ord(">")] |= $NOESC_QUOTE | $RFC2253_ESC;
  37. $arr[ord(";")] |= $NOESC_QUOTE | $RFC2253_ESC;
  38. # Remaining PrintableString characters
  39. $arr[ord(" ")] |= $PSTRING_CHAR;
  40. $arr[ord("'")] |= $PSTRING_CHAR;
  41. $arr[ord("(")] |= $PSTRING_CHAR;
  42. $arr[ord(")")] |= $PSTRING_CHAR;
  43. $arr[ord("+")] |= $PSTRING_CHAR;
  44. $arr[ord(",")] |= $PSTRING_CHAR;
  45. $arr[ord("-")] |= $PSTRING_CHAR;
  46. $arr[ord(".")] |= $PSTRING_CHAR;
  47. $arr[ord("/")] |= $PSTRING_CHAR;
  48. $arr[ord(":")] |= $PSTRING_CHAR;
  49. $arr[ord("=")] |= $PSTRING_CHAR;
  50. $arr[ord("?")] |= $PSTRING_CHAR;
  51. # Now generate the C code
  52. print <<EOF;
  53. /* Auto generated with chartype.pl script.
  54. * Mask of various character properties
  55. */
  56. static unsigned char char_type[] = {
  57. EOF
  58. for($i = 0; $i < 128; $i++) {
  59. print("\n") if($i && (($i % 16) == 0));
  60. printf("%2d", $arr[$i]);
  61. print(",") if ($i != 127);
  62. }
  63. print("\n};\n\n");