pcre_maketables.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2008 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains the external function pcre_maketables(), which builds
  33. character tables for PCRE in the current locale. The file is compiled on its
  34. own as part of the PCRE library. However, it is also included in the
  35. compilation of dftables.c, in which case the macro DFTABLES is defined. */
  36. #ifndef DFTABLES
  37. # ifdef HAVE_CONFIG_H
  38. # include "config.h"
  39. # endif
  40. # include "pcre_internal.h"
  41. #endif
  42. /*************************************************
  43. * Create PCRE character tables *
  44. *************************************************/
  45. /* This function builds a set of character tables for use by PCRE and returns
  46. a pointer to them. They are build using the ctype functions, and consequently
  47. their contents will depend upon the current locale setting. When compiled as
  48. part of the library, the store is obtained via pcre_malloc(), but when compiled
  49. inside dftables, use malloc().
  50. Arguments: none
  51. Returns: pointer to the contiguous block of data
  52. */
  53. const unsigned char *
  54. pcre_maketables(void)
  55. {
  56. unsigned char *yield, *p;
  57. int i;
  58. #ifndef DFTABLES
  59. yield = (unsigned char*)(pcre_malloc)(tables_length);
  60. #else
  61. yield = (unsigned char*)malloc(tables_length);
  62. #endif
  63. if (yield == NULL) return NULL;
  64. p = yield;
  65. /* First comes the lower casing table */
  66. for (i = 0; i < 256; i++) *p++ = tolower(i);
  67. /* Next the case-flipping table */
  68. for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
  69. /* Then the character class tables. Don't try to be clever and save effort on
  70. exclusive ones - in some locales things may be different. Note that the table
  71. for "space" includes everything "isspace" gives, including VT in the default
  72. locale. This makes it work for the POSIX class [:space:]. Note also that it is
  73. possible for a character to be alnum or alpha without being lower or upper,
  74. such as "male and female ordinals" (\xAA and \xBA) in the fr_FR locale (at
  75. least under Debian Linux's locales as of 12/2005). So we must test for alnum
  76. specially. */
  77. memset(p, 0, cbit_length);
  78. for (i = 0; i < 256; i++)
  79. {
  80. if (isdigit(i)) p[cbit_digit + i/8] |= 1 << (i&7);
  81. if (isupper(i)) p[cbit_upper + i/8] |= 1 << (i&7);
  82. if (islower(i)) p[cbit_lower + i/8] |= 1 << (i&7);
  83. if (isalnum(i)) p[cbit_word + i/8] |= 1 << (i&7);
  84. if (i == '_') p[cbit_word + i/8] |= 1 << (i&7);
  85. if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7);
  86. if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
  87. if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7);
  88. if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7);
  89. if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7);
  90. if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7);
  91. }
  92. p += cbit_length;
  93. /* Finally, the character type table. In this, we exclude VT from the white
  94. space chars, because Perl doesn't recognize it as such for \s and for comments
  95. within regexes. */
  96. for (i = 0; i < 256; i++)
  97. {
  98. int x = 0;
  99. if (i != 0x0b && isspace(i)) x += ctype_space;
  100. if (isalpha(i)) x += ctype_letter;
  101. if (isdigit(i)) x += ctype_digit;
  102. if (isxdigit(i)) x += ctype_xdigit;
  103. if (isalnum(i) || i == '_') x += ctype_word;
  104. /* Note: strchr includes the terminating zero in the characters it considers.
  105. In this instance, that is ok because we want binary zero to be flagged as a
  106. meta-character, which in this sense is any character that terminates a run
  107. of data characters. */
  108. if (strchr("\\*+?{^.$|()[", i) != 0) x += ctype_meta;
  109. *p++ = x;
  110. }
  111. return yield;
  112. }
  113. /* End of pcre_maketables.c */