pcre2_config.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. /* Save the configured link size, which is in bytes. In 16-bit and 32-bit modes
  37. its value gets changed by pcre2_intmodedep.h (included by pcre2_internal.h) to
  38. be in code units. */
  39. static int configured_link_size = LINK_SIZE;
  40. #include "pcre2_internal.h"
  41. /* These macros are the standard way of turning unquoted text into C strings.
  42. They allow macros like PCRE2_MAJOR to be defined without quotes, which is
  43. convenient for user programs that want to test their values. */
  44. #define STRING(a) # a
  45. #define XSTRING(s) STRING(s)
  46. /*************************************************
  47. * Return info about what features are configured *
  48. *************************************************/
  49. /* If where is NULL, the length of memory required is returned.
  50. Arguments:
  51. what what information is required
  52. where where to put the information
  53. Returns: 0 if a numerical value is returned
  54. >= 0 if a string value
  55. PCRE2_ERROR_BADOPTION if "where" not recognized
  56. or JIT target requested when JIT not enabled
  57. */
  58. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  59. pcre2_config(uint32_t what, void *where)
  60. {
  61. if (where == NULL) /* Requests a length */
  62. {
  63. switch(what)
  64. {
  65. default:
  66. return PCRE2_ERROR_BADOPTION;
  67. case PCRE2_CONFIG_BSR:
  68. case PCRE2_CONFIG_COMPILED_WIDTHS:
  69. case PCRE2_CONFIG_DEPTHLIMIT:
  70. case PCRE2_CONFIG_HEAPLIMIT:
  71. case PCRE2_CONFIG_JIT:
  72. case PCRE2_CONFIG_LINKSIZE:
  73. case PCRE2_CONFIG_MATCHLIMIT:
  74. case PCRE2_CONFIG_NEVER_BACKSLASH_C:
  75. case PCRE2_CONFIG_NEWLINE:
  76. case PCRE2_CONFIG_PARENSLIMIT:
  77. case PCRE2_CONFIG_STACKRECURSE: /* Obsolete */
  78. case PCRE2_CONFIG_TABLES_LENGTH:
  79. case PCRE2_CONFIG_UNICODE:
  80. return sizeof(uint32_t);
  81. /* These are handled below */
  82. case PCRE2_CONFIG_JITTARGET:
  83. case PCRE2_CONFIG_UNICODE_VERSION:
  84. case PCRE2_CONFIG_VERSION:
  85. break;
  86. }
  87. }
  88. switch (what)
  89. {
  90. default:
  91. return PCRE2_ERROR_BADOPTION;
  92. case PCRE2_CONFIG_BSR:
  93. #ifdef BSR_ANYCRLF
  94. *((uint32_t *)where) = PCRE2_BSR_ANYCRLF;
  95. #else
  96. *((uint32_t *)where) = PCRE2_BSR_UNICODE;
  97. #endif
  98. break;
  99. case PCRE2_CONFIG_COMPILED_WIDTHS:
  100. *((uint32_t *)where) = 0
  101. #ifdef SUPPORT_PCRE2_8
  102. + 1
  103. #endif
  104. #ifdef SUPPORT_PCRE2_16
  105. + 2
  106. #endif
  107. #ifdef SUPPORT_PCRE2_32
  108. + 4
  109. #endif
  110. ;
  111. break;
  112. case PCRE2_CONFIG_DEPTHLIMIT:
  113. *((uint32_t *)where) = MATCH_LIMIT_DEPTH;
  114. break;
  115. case PCRE2_CONFIG_HEAPLIMIT:
  116. *((uint32_t *)where) = HEAP_LIMIT;
  117. break;
  118. case PCRE2_CONFIG_JIT:
  119. #ifdef SUPPORT_JIT
  120. *((uint32_t *)where) = 1;
  121. #else
  122. *((uint32_t *)where) = 0;
  123. #endif
  124. break;
  125. case PCRE2_CONFIG_JITTARGET:
  126. #ifdef SUPPORT_JIT
  127. {
  128. const char *v = PRIV(jit_get_target)();
  129. return (int)(1 + ((where == NULL)?
  130. strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
  131. }
  132. #else
  133. return PCRE2_ERROR_BADOPTION;
  134. #endif
  135. case PCRE2_CONFIG_LINKSIZE:
  136. *((uint32_t *)where) = (uint32_t)configured_link_size;
  137. break;
  138. case PCRE2_CONFIG_MATCHLIMIT:
  139. *((uint32_t *)where) = MATCH_LIMIT;
  140. break;
  141. case PCRE2_CONFIG_NEWLINE:
  142. *((uint32_t *)where) = NEWLINE_DEFAULT;
  143. break;
  144. case PCRE2_CONFIG_NEVER_BACKSLASH_C:
  145. #ifdef NEVER_BACKSLASH_C
  146. *((uint32_t *)where) = 1;
  147. #else
  148. *((uint32_t *)where) = 0;
  149. #endif
  150. break;
  151. case PCRE2_CONFIG_PARENSLIMIT:
  152. *((uint32_t *)where) = PARENS_NEST_LIMIT;
  153. break;
  154. /* This is now obsolete. The stack is no longer used via recursion for
  155. handling backtracking in pcre2_match(). */
  156. case PCRE2_CONFIG_STACKRECURSE:
  157. *((uint32_t *)where) = 0;
  158. break;
  159. case PCRE2_CONFIG_TABLES_LENGTH:
  160. *((uint32_t *)where) = TABLES_LENGTH;
  161. break;
  162. case PCRE2_CONFIG_UNICODE_VERSION:
  163. {
  164. #if defined SUPPORT_UNICODE
  165. const char *v = PRIV(unicode_version);
  166. #else
  167. const char *v = "Unicode not supported";
  168. #endif
  169. return (int)(1 + ((where == NULL)?
  170. strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
  171. }
  172. break;
  173. case PCRE2_CONFIG_UNICODE:
  174. #if defined SUPPORT_UNICODE
  175. *((uint32_t *)where) = 1;
  176. #else
  177. *((uint32_t *)where) = 0;
  178. #endif
  179. break;
  180. /* The hackery in setting "v" below is to cope with the case when
  181. PCRE2_PRERELEASE is set to an empty string (which it is for real releases).
  182. If the second alternative is used in this case, it does not leave a space
  183. before the date. On the other hand, if all four macros are put into a single
  184. XSTRING when PCRE2_PRERELEASE is not empty, an unwanted space is inserted.
  185. There are problems using an "obvious" approach like this:
  186. XSTRING(PCRE2_MAJOR) "." XSTRING(PCRE_MINOR)
  187. XSTRING(PCRE2_PRERELEASE) " " XSTRING(PCRE_DATE)
  188. because, when PCRE2_PRERELEASE is empty, this leads to an attempted expansion
  189. of STRING(). The C standard states: "If (before argument substitution) any
  190. argument consists of no preprocessing tokens, the behavior is undefined." It
  191. turns out the gcc treats this case as a single empty string - which is what
  192. we really want - but Visual C grumbles about the lack of an argument for the
  193. macro. Unfortunately, both are within their rights. As there seems to be no
  194. way to test for a macro's value being empty at compile time, we have to
  195. resort to a runtime test. */
  196. case PCRE2_CONFIG_VERSION:
  197. {
  198. const char *v = (XSTRING(Z PCRE2_PRERELEASE)[1] == 0)?
  199. XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) :
  200. XSTRING(PCRE2_MAJOR.PCRE2_MINOR) XSTRING(PCRE2_PRERELEASE PCRE2_DATE);
  201. return (int)(1 + ((where == NULL)?
  202. strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
  203. }
  204. }
  205. return 0;
  206. }
  207. /* End of pcre2_config.c */