pcre2_context.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #include "pcre2_internal.h"
  37. /*************************************************
  38. * Default malloc/free functions *
  39. *************************************************/
  40. /* Ignore the "user data" argument in each case. */
  41. static void *default_malloc(size_t size, void *data)
  42. {
  43. (void)data;
  44. return malloc(size);
  45. }
  46. static void default_free(void *block, void *data)
  47. {
  48. (void)data;
  49. free(block);
  50. }
  51. /*************************************************
  52. * Get a block and save memory control *
  53. *************************************************/
  54. /* This internal function is called to get a block of memory in which the
  55. memory control data is to be stored at the start for future use.
  56. Arguments:
  57. size amount of memory required
  58. memctl pointer to a memctl block or NULL
  59. Returns: pointer to memory or NULL on failure
  60. */
  61. extern void *
  62. PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
  63. {
  64. pcre2_memctl *newmemctl;
  65. void *yield = (memctl == NULL)? malloc(size) :
  66. memctl->malloc(size, memctl->memory_data);
  67. if (yield == NULL) return NULL;
  68. newmemctl = (pcre2_memctl *)yield;
  69. if (memctl == NULL)
  70. {
  71. newmemctl->malloc = default_malloc;
  72. newmemctl->free = default_free;
  73. newmemctl->memory_data = NULL;
  74. }
  75. else *newmemctl = *memctl;
  76. return yield;
  77. }
  78. /*************************************************
  79. * Create and initialize contexts *
  80. *************************************************/
  81. /* Initializing for compile and match contexts is done in separate, private
  82. functions so that these can be called from functions such as pcre2_compile()
  83. when an external context is not supplied. The initializing functions have an
  84. option to set up default memory management. */
  85. PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
  86. pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
  87. void (*private_free)(void *, void *), void *memory_data)
  88. {
  89. pcre2_general_context *gcontext;
  90. if (private_malloc == NULL) private_malloc = default_malloc;
  91. if (private_free == NULL) private_free = default_free;
  92. gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);
  93. if (gcontext == NULL) return NULL;
  94. gcontext->memctl.malloc = private_malloc;
  95. gcontext->memctl.free = private_free;
  96. gcontext->memctl.memory_data = memory_data;
  97. return gcontext;
  98. }
  99. /* A default compile context is set up to save having to initialize at run time
  100. when no context is supplied to the compile function. */
  101. const pcre2_compile_context PRIV(default_compile_context) = {
  102. { default_malloc, default_free, NULL }, /* Default memory handling */
  103. NULL, /* Stack guard */
  104. NULL, /* Stack guard data */
  105. PRIV(default_tables), /* Character tables */
  106. PCRE2_UNSET, /* Max pattern length */
  107. BSR_DEFAULT, /* Backslash R default */
  108. NEWLINE_DEFAULT, /* Newline convention */
  109. PARENS_NEST_LIMIT, /* As it says */
  110. 0 }; /* Extra options */
  111. /* The create function copies the default into the new memory, but must
  112. override the default memory handling functions if a gcontext was provided. */
  113. PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
  114. pcre2_compile_context_create(pcre2_general_context *gcontext)
  115. {
  116. pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
  117. sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
  118. if (ccontext == NULL) return NULL;
  119. *ccontext = PRIV(default_compile_context);
  120. if (gcontext != NULL)
  121. *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
  122. return ccontext;
  123. }
  124. /* A default match context is set up to save having to initialize at run time
  125. when no context is supplied to a match function. */
  126. const pcre2_match_context PRIV(default_match_context) = {
  127. { default_malloc, default_free, NULL },
  128. #ifdef SUPPORT_JIT
  129. NULL,
  130. NULL,
  131. #endif
  132. NULL,
  133. NULL,
  134. PCRE2_UNSET, /* Offset limit */
  135. HEAP_LIMIT,
  136. MATCH_LIMIT,
  137. MATCH_LIMIT_DEPTH };
  138. /* The create function copies the default into the new memory, but must
  139. override the default memory handling functions if a gcontext was provided. */
  140. PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
  141. pcre2_match_context_create(pcre2_general_context *gcontext)
  142. {
  143. pcre2_match_context *mcontext = PRIV(memctl_malloc)(
  144. sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
  145. if (mcontext == NULL) return NULL;
  146. *mcontext = PRIV(default_match_context);
  147. if (gcontext != NULL)
  148. *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
  149. return mcontext;
  150. }
  151. /* A default convert context is set up to save having to initialize at run time
  152. when no context is supplied to the convert function. */
  153. const pcre2_convert_context PRIV(default_convert_context) = {
  154. { default_malloc, default_free, NULL }, /* Default memory handling */
  155. #ifdef _WIN32
  156. CHAR_BACKSLASH, /* Default path separator */
  157. CHAR_GRAVE_ACCENT /* Default escape character */
  158. #else /* Not Windows */
  159. CHAR_SLASH, /* Default path separator */
  160. CHAR_BACKSLASH /* Default escape character */
  161. #endif
  162. };
  163. /* The create function copies the default into the new memory, but must
  164. override the default memory handling functions if a gcontext was provided. */
  165. PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
  166. pcre2_convert_context_create(pcre2_general_context *gcontext)
  167. {
  168. pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
  169. sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
  170. if (ccontext == NULL) return NULL;
  171. *ccontext = PRIV(default_convert_context);
  172. if (gcontext != NULL)
  173. *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
  174. return ccontext;
  175. }
  176. /*************************************************
  177. * Context copy functions *
  178. *************************************************/
  179. PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
  180. pcre2_general_context_copy(pcre2_general_context *gcontext)
  181. {
  182. pcre2_general_context *new =
  183. gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
  184. gcontext->memctl.memory_data);
  185. if (new == NULL) return NULL;
  186. memcpy(new, gcontext, sizeof(pcre2_real_general_context));
  187. return new;
  188. }
  189. PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
  190. pcre2_compile_context_copy(pcre2_compile_context *ccontext)
  191. {
  192. pcre2_compile_context *new =
  193. ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
  194. ccontext->memctl.memory_data);
  195. if (new == NULL) return NULL;
  196. memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
  197. return new;
  198. }
  199. PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
  200. pcre2_match_context_copy(pcre2_match_context *mcontext)
  201. {
  202. pcre2_match_context *new =
  203. mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
  204. mcontext->memctl.memory_data);
  205. if (new == NULL) return NULL;
  206. memcpy(new, mcontext, sizeof(pcre2_real_match_context));
  207. return new;
  208. }
  209. PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
  210. pcre2_convert_context_copy(pcre2_convert_context *ccontext)
  211. {
  212. pcre2_convert_context *new =
  213. ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
  214. ccontext->memctl.memory_data);
  215. if (new == NULL) return NULL;
  216. memcpy(new, ccontext, sizeof(pcre2_real_convert_context));
  217. return new;
  218. }
  219. /*************************************************
  220. * Context free functions *
  221. *************************************************/
  222. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  223. pcre2_general_context_free(pcre2_general_context *gcontext)
  224. {
  225. if (gcontext != NULL)
  226. gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
  227. }
  228. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  229. pcre2_compile_context_free(pcre2_compile_context *ccontext)
  230. {
  231. if (ccontext != NULL)
  232. ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
  233. }
  234. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  235. pcre2_match_context_free(pcre2_match_context *mcontext)
  236. {
  237. if (mcontext != NULL)
  238. mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
  239. }
  240. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  241. pcre2_convert_context_free(pcre2_convert_context *ccontext)
  242. {
  243. if (ccontext != NULL)
  244. ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
  245. }
  246. /*************************************************
  247. * Set values in contexts *
  248. *************************************************/
  249. /* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
  250. data is given. Only some of the functions are able to test the validity of the
  251. data. */
  252. /* ------------ Compile context ------------ */
  253. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  254. pcre2_set_character_tables(pcre2_compile_context *ccontext,
  255. const unsigned char *tables)
  256. {
  257. ccontext->tables = tables;
  258. return 0;
  259. }
  260. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  261. pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
  262. {
  263. switch(value)
  264. {
  265. case PCRE2_BSR_ANYCRLF:
  266. case PCRE2_BSR_UNICODE:
  267. ccontext->bsr_convention = value;
  268. return 0;
  269. default:
  270. return PCRE2_ERROR_BADDATA;
  271. }
  272. }
  273. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  274. pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
  275. {
  276. ccontext->max_pattern_length = length;
  277. return 0;
  278. }
  279. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  280. pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
  281. {
  282. switch(newline)
  283. {
  284. case PCRE2_NEWLINE_CR:
  285. case PCRE2_NEWLINE_LF:
  286. case PCRE2_NEWLINE_CRLF:
  287. case PCRE2_NEWLINE_ANY:
  288. case PCRE2_NEWLINE_ANYCRLF:
  289. case PCRE2_NEWLINE_NUL:
  290. ccontext->newline_convention = newline;
  291. return 0;
  292. default:
  293. return PCRE2_ERROR_BADDATA;
  294. }
  295. }
  296. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  297. pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
  298. {
  299. ccontext->parens_nest_limit = limit;
  300. return 0;
  301. }
  302. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  303. pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
  304. {
  305. ccontext->extra_options = options;
  306. return 0;
  307. }
  308. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  309. pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
  310. int (*guard)(uint32_t, void *), void *user_data)
  311. {
  312. ccontext->stack_guard = guard;
  313. ccontext->stack_guard_data = user_data;
  314. return 0;
  315. }
  316. /* ------------ Match context ------------ */
  317. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  318. pcre2_set_callout(pcre2_match_context *mcontext,
  319. int (*callout)(pcre2_callout_block *, void *), void *callout_data)
  320. {
  321. mcontext->callout = callout;
  322. mcontext->callout_data = callout_data;
  323. return 0;
  324. }
  325. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  326. pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
  327. {
  328. mcontext->heap_limit = limit;
  329. return 0;
  330. }
  331. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  332. pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
  333. {
  334. mcontext->match_limit = limit;
  335. return 0;
  336. }
  337. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  338. pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
  339. {
  340. mcontext->depth_limit = limit;
  341. return 0;
  342. }
  343. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  344. pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
  345. {
  346. mcontext->offset_limit = limit;
  347. return 0;
  348. }
  349. /* This function became obsolete at release 10.30. It is kept as a synonym for
  350. backwards compatibility. */
  351. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  352. pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
  353. {
  354. return pcre2_set_depth_limit(mcontext, limit);
  355. }
  356. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  357. pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
  358. void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
  359. void *mydata)
  360. {
  361. (void)mcontext;
  362. (void)mymalloc;
  363. (void)myfree;
  364. (void)mydata;
  365. return 0;
  366. }
  367. /* ------------ Convert context ------------ */
  368. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  369. pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
  370. {
  371. if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
  372. separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
  373. ccontext->glob_separator = separator;
  374. return 0;
  375. }
  376. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  377. pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
  378. {
  379. if (escape > 255 || (escape != 0 && !ispunct(escape)))
  380. return PCRE2_ERROR_BADDATA;
  381. ccontext->glob_escape = escape;
  382. return 0;
  383. }
  384. /* End of pcre2_context.c */