pcre2_config.c 7.4 KB

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