pcre2_context.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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-2018 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, /* JIT callback */
  130. NULL, /* JIT callback data */
  131. #endif
  132. NULL, /* Callout function */
  133. NULL, /* Callout data */
  134. NULL, /* Substitute callout function */
  135. NULL, /* Substitute callout data */
  136. PCRE2_UNSET, /* Offset limit */
  137. HEAP_LIMIT,
  138. MATCH_LIMIT,
  139. MATCH_LIMIT_DEPTH };
  140. /* The create function copies the default into the new memory, but must
  141. override the default memory handling functions if a gcontext was provided. */
  142. PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
  143. pcre2_match_context_create(pcre2_general_context *gcontext)
  144. {
  145. pcre2_match_context *mcontext = PRIV(memctl_malloc)(
  146. sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
  147. if (mcontext == NULL) return NULL;
  148. *mcontext = PRIV(default_match_context);
  149. if (gcontext != NULL)
  150. *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
  151. return mcontext;
  152. }
  153. /* A default convert context is set up to save having to initialize at run time
  154. when no context is supplied to the convert function. */
  155. const pcre2_convert_context PRIV(default_convert_context) = {
  156. { default_malloc, default_free, NULL }, /* Default memory handling */
  157. #ifdef _WIN32
  158. CHAR_BACKSLASH, /* Default path separator */
  159. CHAR_GRAVE_ACCENT /* Default escape character */
  160. #else /* Not Windows */
  161. CHAR_SLASH, /* Default path separator */
  162. CHAR_BACKSLASH /* Default escape character */
  163. #endif
  164. };
  165. /* The create function copies the default into the new memory, but must
  166. override the default memory handling functions if a gcontext was provided. */
  167. PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
  168. pcre2_convert_context_create(pcre2_general_context *gcontext)
  169. {
  170. pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
  171. sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
  172. if (ccontext == NULL) return NULL;
  173. *ccontext = PRIV(default_convert_context);
  174. if (gcontext != NULL)
  175. *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
  176. return ccontext;
  177. }
  178. /*************************************************
  179. * Context copy functions *
  180. *************************************************/
  181. PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
  182. pcre2_general_context_copy(pcre2_general_context *gcontext)
  183. {
  184. pcre2_general_context *new =
  185. gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
  186. gcontext->memctl.memory_data);
  187. if (new == NULL) return NULL;
  188. memcpy(new, gcontext, sizeof(pcre2_real_general_context));
  189. return new;
  190. }
  191. PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
  192. pcre2_compile_context_copy(pcre2_compile_context *ccontext)
  193. {
  194. pcre2_compile_context *new =
  195. ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
  196. ccontext->memctl.memory_data);
  197. if (new == NULL) return NULL;
  198. memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
  199. return new;
  200. }
  201. PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
  202. pcre2_match_context_copy(pcre2_match_context *mcontext)
  203. {
  204. pcre2_match_context *new =
  205. mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
  206. mcontext->memctl.memory_data);
  207. if (new == NULL) return NULL;
  208. memcpy(new, mcontext, sizeof(pcre2_real_match_context));
  209. return new;
  210. }
  211. PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
  212. pcre2_convert_context_copy(pcre2_convert_context *ccontext)
  213. {
  214. pcre2_convert_context *new =
  215. ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
  216. ccontext->memctl.memory_data);
  217. if (new == NULL) return NULL;
  218. memcpy(new, ccontext, sizeof(pcre2_real_convert_context));
  219. return new;
  220. }
  221. /*************************************************
  222. * Context free functions *
  223. *************************************************/
  224. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  225. pcre2_general_context_free(pcre2_general_context *gcontext)
  226. {
  227. if (gcontext != NULL)
  228. gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
  229. }
  230. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  231. pcre2_compile_context_free(pcre2_compile_context *ccontext)
  232. {
  233. if (ccontext != NULL)
  234. ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
  235. }
  236. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  237. pcre2_match_context_free(pcre2_match_context *mcontext)
  238. {
  239. if (mcontext != NULL)
  240. mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
  241. }
  242. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  243. pcre2_convert_context_free(pcre2_convert_context *ccontext)
  244. {
  245. if (ccontext != NULL)
  246. ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
  247. }
  248. /*************************************************
  249. * Set values in contexts *
  250. *************************************************/
  251. /* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
  252. data is given. Only some of the functions are able to test the validity of the
  253. data. */
  254. /* ------------ Compile context ------------ */
  255. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  256. pcre2_set_character_tables(pcre2_compile_context *ccontext,
  257. const uint8_t *tables)
  258. {
  259. ccontext->tables = tables;
  260. return 0;
  261. }
  262. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  263. pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
  264. {
  265. switch(value)
  266. {
  267. case PCRE2_BSR_ANYCRLF:
  268. case PCRE2_BSR_UNICODE:
  269. ccontext->bsr_convention = value;
  270. return 0;
  271. default:
  272. return PCRE2_ERROR_BADDATA;
  273. }
  274. }
  275. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  276. pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
  277. {
  278. ccontext->max_pattern_length = length;
  279. return 0;
  280. }
  281. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  282. pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
  283. {
  284. switch(newline)
  285. {
  286. case PCRE2_NEWLINE_CR:
  287. case PCRE2_NEWLINE_LF:
  288. case PCRE2_NEWLINE_CRLF:
  289. case PCRE2_NEWLINE_ANY:
  290. case PCRE2_NEWLINE_ANYCRLF:
  291. case PCRE2_NEWLINE_NUL:
  292. ccontext->newline_convention = newline;
  293. return 0;
  294. default:
  295. return PCRE2_ERROR_BADDATA;
  296. }
  297. }
  298. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  299. pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
  300. {
  301. ccontext->parens_nest_limit = limit;
  302. return 0;
  303. }
  304. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  305. pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
  306. {
  307. ccontext->extra_options = options;
  308. return 0;
  309. }
  310. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  311. pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
  312. int (*guard)(uint32_t, void *), void *user_data)
  313. {
  314. ccontext->stack_guard = guard;
  315. ccontext->stack_guard_data = user_data;
  316. return 0;
  317. }
  318. /* ------------ Match context ------------ */
  319. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  320. pcre2_set_callout(pcre2_match_context *mcontext,
  321. int (*callout)(pcre2_callout_block *, void *), void *callout_data)
  322. {
  323. mcontext->callout = callout;
  324. mcontext->callout_data = callout_data;
  325. return 0;
  326. }
  327. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  328. pcre2_set_substitute_callout(pcre2_match_context *mcontext,
  329. int (*substitute_callout)(pcre2_substitute_callout_block *, void *),
  330. void *substitute_callout_data)
  331. {
  332. mcontext->substitute_callout = substitute_callout;
  333. mcontext->substitute_callout_data = substitute_callout_data;
  334. return 0;
  335. }
  336. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  337. pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
  338. {
  339. mcontext->heap_limit = limit;
  340. return 0;
  341. }
  342. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  343. pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
  344. {
  345. mcontext->match_limit = limit;
  346. return 0;
  347. }
  348. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  349. pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
  350. {
  351. mcontext->depth_limit = limit;
  352. return 0;
  353. }
  354. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  355. pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
  356. {
  357. mcontext->offset_limit = limit;
  358. return 0;
  359. }
  360. /* This function became obsolete at release 10.30. It is kept as a synonym for
  361. backwards compatibility. */
  362. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  363. pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
  364. {
  365. return pcre2_set_depth_limit(mcontext, limit);
  366. }
  367. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  368. pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
  369. void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
  370. void *mydata)
  371. {
  372. (void)mcontext;
  373. (void)mymalloc;
  374. (void)myfree;
  375. (void)mydata;
  376. return 0;
  377. }
  378. /* ------------ Convert context ------------ */
  379. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  380. pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
  381. {
  382. if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
  383. separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
  384. ccontext->glob_separator = separator;
  385. return 0;
  386. }
  387. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  388. pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
  389. {
  390. if (escape > 255 || (escape != 0 && !ispunct(escape)))
  391. return PCRE2_ERROR_BADDATA;
  392. ccontext->glob_escape = escape;
  393. return 0;
  394. }
  395. /* End of pcre2_context.c */