pcre_maketables.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2012 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 "pcre_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 PUBL(malloc)(), but when
  49. compiled inside dftables, use malloc().
  50. Arguments: none
  51. Returns: pointer to the contiguous block of data
  52. */
  53. #if defined COMPILE_PCRE8
  54. const unsigned char *
  55. pcre_maketables(void)
  56. #elif defined COMPILE_PCRE16
  57. const unsigned char *
  58. pcre16_maketables(void)
  59. #elif defined COMPILE_PCRE32
  60. const unsigned char *
  61. pcre32_maketables(void)
  62. #endif
  63. {
  64. unsigned char *yield, *p;
  65. int i;
  66. #ifndef DFTABLES
  67. yield = (unsigned char*)(PUBL(malloc))(tables_length);
  68. #else
  69. yield = (unsigned char*)malloc(tables_length);
  70. #endif
  71. if (yield == NULL) return NULL;
  72. p = yield;
  73. /* First comes the lower casing table */
  74. for (i = 0; i < 256; i++) *p++ = tolower(i);
  75. /* Next the case-flipping table */
  76. for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
  77. /* Then the character class tables. Don't try to be clever and save effort on
  78. exclusive ones - in some locales things may be different.
  79. Note that the table for "space" includes everything "isspace" gives, including
  80. VT in the default locale. This makes it work for the POSIX class [:space:].
  81. From release 8.34 is is also correct for Perl space, because Perl added VT at
  82. release 5.18.
  83. Note also that it is possible for a character to be alnum or alpha without
  84. being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the
  85. fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must
  86. test for alnum specially. */
  87. memset(p, 0, cbit_length);
  88. for (i = 0; i < 256; i++)
  89. {
  90. if (isdigit(i)) p[cbit_digit + i/8] |= 1 << (i&7);
  91. if (isupper(i)) p[cbit_upper + i/8] |= 1 << (i&7);
  92. if (islower(i)) p[cbit_lower + i/8] |= 1 << (i&7);
  93. if (isalnum(i)) p[cbit_word + i/8] |= 1 << (i&7);
  94. if (i == '_') p[cbit_word + i/8] |= 1 << (i&7);
  95. if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7);
  96. if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
  97. if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7);
  98. if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7);
  99. if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7);
  100. if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7);
  101. }
  102. p += cbit_length;
  103. /* Finally, the character type table. In this, we used to exclude VT from the
  104. white space chars, because Perl didn't recognize it as such for \s and for
  105. comments within regexes. However, Perl changed at release 5.18, so PCRE changed
  106. at release 8.34. */
  107. for (i = 0; i < 256; i++)
  108. {
  109. int x = 0;
  110. if (isspace(i)) x += ctype_space;
  111. if (isalpha(i)) x += ctype_letter;
  112. if (isdigit(i)) x += ctype_digit;
  113. if (isxdigit(i)) x += ctype_xdigit;
  114. if (isalnum(i) || i == '_') x += ctype_word;
  115. /* Note: strchr includes the terminating zero in the characters it considers.
  116. In this instance, that is ok because we want binary zero to be flagged as a
  117. meta-character, which in this sense is any character that terminates a run
  118. of data characters. */
  119. if (strchr("\\*+?{^.$|()[", i) != 0) x += ctype_meta;
  120. *p++ = x;
  121. }
  122. return yield;
  123. }
  124. /* End of pcre_maketables.c */