pcre2_maketables.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2020 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains the external function pcre2_maketables(), which builds
  34. character tables for PCRE2 in the current locale. The file is compiled on its
  35. own as part of the PCRE2 library. It is also included in the compilation of
  36. pcre2_dftables.c as a freestanding program, in which case the macro
  37. PCRE2_DFTABLES is defined. */
  38. #ifndef PCRE2_DFTABLES /* Compiling the library */
  39. # ifdef HAVE_CONFIG_H
  40. # include "config.h"
  41. # endif
  42. # include "pcre2_internal.h"
  43. #endif
  44. /*************************************************
  45. * Create PCRE2 character tables *
  46. *************************************************/
  47. /* This function builds a set of character tables for use by PCRE2 and returns
  48. a pointer to them. They are build using the ctype functions, and consequently
  49. their contents will depend upon the current locale setting. When compiled as
  50. part of the library, the store is obtained via a general context malloc, if
  51. supplied, but when PCRE2_DFTABLES is defined (when compiling the pcre2_dftables
  52. freestanding auxiliary program) malloc() is used, and the function has a
  53. different name so as not to clash with the prototype in pcre2.h.
  54. Arguments: none when PCRE2_DFTABLES is defined
  55. else a PCRE2 general context or NULL
  56. Returns: pointer to the contiguous block of data
  57. else NULL if memory allocation failed
  58. */
  59. #ifdef PCRE2_DFTABLES /* Included in freestanding pcre2_dftables program */
  60. static const uint8_t *maketables(void)
  61. {
  62. uint8_t *yield = (uint8_t *)malloc(TABLES_LENGTH);
  63. #else /* Not PCRE2_DFTABLES, that is, compiling the library */
  64. PCRE2_EXP_DEFN const uint8_t * PCRE2_CALL_CONVENTION
  65. pcre2_maketables(pcre2_general_context *gcontext)
  66. {
  67. uint8_t *yield = (uint8_t *)((gcontext != NULL)?
  68. gcontext->memctl.malloc(TABLES_LENGTH, gcontext->memctl.memory_data) :
  69. malloc(TABLES_LENGTH));
  70. #endif /* PCRE2_DFTABLES */
  71. int i;
  72. uint8_t *p;
  73. if (yield == NULL) return NULL;
  74. p = yield;
  75. /* First comes the lower casing table */
  76. for (i = 0; i < 256; i++) *p++ = tolower(i);
  77. /* Next the case-flipping table */
  78. for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
  79. /* Then the character class tables. Don't try to be clever and save effort on
  80. exclusive ones - in some locales things may be different.
  81. Note that the table for "space" includes everything "isspace" gives, including
  82. VT in the default locale. This makes it work for the POSIX class [:space:].
  83. From PCRE1 release 8.34 and for all PCRE2 releases it is also correct for Perl
  84. space, because Perl added VT at release 5.18.
  85. Note also that it is possible for a character to be alnum or alpha without
  86. being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the
  87. fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must
  88. test for alnum specially. */
  89. memset(p, 0, cbit_length);
  90. for (i = 0; i < 256; i++)
  91. {
  92. if (isdigit(i)) p[cbit_digit + i/8] |= 1u << (i&7);
  93. if (isupper(i)) p[cbit_upper + i/8] |= 1u << (i&7);
  94. if (islower(i)) p[cbit_lower + i/8] |= 1u << (i&7);
  95. if (isalnum(i)) p[cbit_word + i/8] |= 1u << (i&7);
  96. if (i == '_') p[cbit_word + i/8] |= 1u << (i&7);
  97. if (isspace(i)) p[cbit_space + i/8] |= 1u << (i&7);
  98. if (isxdigit(i)) p[cbit_xdigit + i/8] |= 1u << (i&7);
  99. if (isgraph(i)) p[cbit_graph + i/8] |= 1u << (i&7);
  100. if (isprint(i)) p[cbit_print + i/8] |= 1u << (i&7);
  101. if (ispunct(i)) p[cbit_punct + i/8] |= 1u << (i&7);
  102. if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1u << (i&7);
  103. }
  104. p += cbit_length;
  105. /* Finally, the character type table. In this, we used to exclude VT from the
  106. white space chars, because Perl didn't recognize it as such for \s and for
  107. comments within regexes. However, Perl changed at release 5.18, so PCRE1
  108. changed at release 8.34 and it's always been this way for PCRE2. */
  109. for (i = 0; i < 256; i++)
  110. {
  111. int x = 0;
  112. if (isspace(i)) x += ctype_space;
  113. if (isalpha(i)) x += ctype_letter;
  114. if (islower(i)) x += ctype_lcletter;
  115. if (isdigit(i)) x += ctype_digit;
  116. if (isalnum(i) || i == '_') x += ctype_word;
  117. *p++ = x;
  118. }
  119. return yield;
  120. }
  121. #ifndef PCRE2_DFTABLES /* Compiling the library */
  122. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  123. pcre2_maketables_free(pcre2_general_context *gcontext, const uint8_t *tables)
  124. {
  125. if (gcontext)
  126. gcontext->memctl.free((void *)tables, gcontext->memctl.memory_data);
  127. else
  128. free((void *)tables);
  129. }
  130. #endif
  131. /* End of pcre2_maketables.c */