pcre2_substring.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. * Copy named captured string to given buffer *
  39. *************************************************/
  40. /* This function copies a single captured substring into a given buffer,
  41. identifying it by name. If the regex permits duplicate names, the first
  42. substring that is set is chosen.
  43. Arguments:
  44. match_data points to the match data
  45. stringname the name of the required substring
  46. buffer where to put the substring
  47. sizeptr the size of the buffer, updated to the size of the substring
  48. Returns: if successful: zero
  49. if not successful, a negative error code:
  50. (1) an error from nametable_scan()
  51. (2) an error from copy_bynumber()
  52. (3) PCRE2_ERROR_UNAVAILABLE: no group is in ovector
  53. (4) PCRE2_ERROR_UNSET: all named groups in ovector are unset
  54. */
  55. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  56. pcre2_substring_copy_byname(pcre2_match_data *match_data, PCRE2_SPTR stringname,
  57. PCRE2_UCHAR *buffer, PCRE2_SIZE *sizeptr)
  58. {
  59. PCRE2_SPTR first, last, entry;
  60. int failrc, entrysize;
  61. if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
  62. return PCRE2_ERROR_DFA_UFUNC;
  63. entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
  64. &first, &last);
  65. if (entrysize < 0) return entrysize;
  66. failrc = PCRE2_ERROR_UNAVAILABLE;
  67. for (entry = first; entry <= last; entry += entrysize)
  68. {
  69. uint32_t n = GET2(entry, 0);
  70. if (n < match_data->oveccount)
  71. {
  72. if (match_data->ovector[n*2] != PCRE2_UNSET)
  73. return pcre2_substring_copy_bynumber(match_data, n, buffer, sizeptr);
  74. failrc = PCRE2_ERROR_UNSET;
  75. }
  76. }
  77. return failrc;
  78. }
  79. /*************************************************
  80. * Copy numbered captured string to given buffer *
  81. *************************************************/
  82. /* This function copies a single captured substring into a given buffer,
  83. identifying it by number.
  84. Arguments:
  85. match_data points to the match data
  86. stringnumber the number of the required substring
  87. buffer where to put the substring
  88. sizeptr the size of the buffer, updated to the size of the substring
  89. Returns: if successful: 0
  90. if not successful, a negative error code:
  91. PCRE2_ERROR_NOMEMORY: buffer too small
  92. PCRE2_ERROR_NOSUBSTRING: no such substring
  93. PCRE2_ERROR_UNAVAILABLE: ovector too small
  94. PCRE2_ERROR_UNSET: substring is not set
  95. */
  96. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  97. pcre2_substring_copy_bynumber(pcre2_match_data *match_data,
  98. uint32_t stringnumber, PCRE2_UCHAR *buffer, PCRE2_SIZE *sizeptr)
  99. {
  100. int rc;
  101. PCRE2_SIZE size;
  102. rc = pcre2_substring_length_bynumber(match_data, stringnumber, &size);
  103. if (rc < 0) return rc;
  104. if (size + 1 > *sizeptr) return PCRE2_ERROR_NOMEMORY;
  105. memcpy(buffer, match_data->subject + match_data->ovector[stringnumber*2],
  106. CU2BYTES(size));
  107. buffer[size] = 0;
  108. *sizeptr = size;
  109. return 0;
  110. }
  111. /*************************************************
  112. * Extract named captured string *
  113. *************************************************/
  114. /* This function copies a single captured substring, identified by name, into
  115. new memory. If the regex permits duplicate names, the first substring that is
  116. set is chosen.
  117. Arguments:
  118. match_data pointer to match_data
  119. stringname the name of the required substring
  120. stringptr where to put the pointer to the new memory
  121. sizeptr where to put the length of the substring
  122. Returns: if successful: zero
  123. if not successful, a negative value:
  124. (1) an error from nametable_scan()
  125. (2) an error from get_bynumber()
  126. (3) PCRE2_ERROR_UNAVAILABLE: no group is in ovector
  127. (4) PCRE2_ERROR_UNSET: all named groups in ovector are unset
  128. */
  129. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  130. pcre2_substring_get_byname(pcre2_match_data *match_data,
  131. PCRE2_SPTR stringname, PCRE2_UCHAR **stringptr, PCRE2_SIZE *sizeptr)
  132. {
  133. PCRE2_SPTR first, last, entry;
  134. int failrc, entrysize;
  135. if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
  136. return PCRE2_ERROR_DFA_UFUNC;
  137. entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
  138. &first, &last);
  139. if (entrysize < 0) return entrysize;
  140. failrc = PCRE2_ERROR_UNAVAILABLE;
  141. for (entry = first; entry <= last; entry += entrysize)
  142. {
  143. uint32_t n = GET2(entry, 0);
  144. if (n < match_data->oveccount)
  145. {
  146. if (match_data->ovector[n*2] != PCRE2_UNSET)
  147. return pcre2_substring_get_bynumber(match_data, n, stringptr, sizeptr);
  148. failrc = PCRE2_ERROR_UNSET;
  149. }
  150. }
  151. return failrc;
  152. }
  153. /*************************************************
  154. * Extract captured string to new memory *
  155. *************************************************/
  156. /* This function copies a single captured substring into a piece of new
  157. memory.
  158. Arguments:
  159. match_data points to match data
  160. stringnumber the number of the required substring
  161. stringptr where to put a pointer to the new memory
  162. sizeptr where to put the size of the substring
  163. Returns: if successful: 0
  164. if not successful, a negative error code:
  165. PCRE2_ERROR_NOMEMORY: failed to get memory
  166. PCRE2_ERROR_NOSUBSTRING: no such substring
  167. PCRE2_ERROR_UNAVAILABLE: ovector too small
  168. PCRE2_ERROR_UNSET: substring is not set
  169. */
  170. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  171. pcre2_substring_get_bynumber(pcre2_match_data *match_data,
  172. uint32_t stringnumber, PCRE2_UCHAR **stringptr, PCRE2_SIZE *sizeptr)
  173. {
  174. int rc;
  175. PCRE2_SIZE size;
  176. PCRE2_UCHAR *yield;
  177. rc = pcre2_substring_length_bynumber(match_data, stringnumber, &size);
  178. if (rc < 0) return rc;
  179. yield = PRIV(memctl_malloc)(sizeof(pcre2_memctl) +
  180. (size + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)match_data);
  181. if (yield == NULL) return PCRE2_ERROR_NOMEMORY;
  182. yield = (PCRE2_UCHAR *)(((char *)yield) + sizeof(pcre2_memctl));
  183. memcpy(yield, match_data->subject + match_data->ovector[stringnumber*2],
  184. CU2BYTES(size));
  185. yield[size] = 0;
  186. *stringptr = yield;
  187. *sizeptr = size;
  188. return 0;
  189. }
  190. /*************************************************
  191. * Free memory obtained by get_substring *
  192. *************************************************/
  193. /*
  194. Argument: the result of a previous pcre2_substring_get_byxxx()
  195. Returns: nothing
  196. */
  197. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  198. pcre2_substring_free(PCRE2_UCHAR *string)
  199. {
  200. if (string != NULL)
  201. {
  202. pcre2_memctl *memctl = (pcre2_memctl *)((char *)string - sizeof(pcre2_memctl));
  203. memctl->free(memctl, memctl->memory_data);
  204. }
  205. }
  206. /*************************************************
  207. * Get length of a named substring *
  208. *************************************************/
  209. /* This function returns the length of a named captured substring. If the regex
  210. permits duplicate names, the first substring that is set is chosen.
  211. Arguments:
  212. match_data pointer to match data
  213. stringname the name of the required substring
  214. sizeptr where to put the length
  215. Returns: 0 if successful, else a negative error number
  216. */
  217. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  218. pcre2_substring_length_byname(pcre2_match_data *match_data,
  219. PCRE2_SPTR stringname, PCRE2_SIZE *sizeptr)
  220. {
  221. PCRE2_SPTR first, last, entry;
  222. int failrc, entrysize;
  223. if (match_data->matchedby == PCRE2_MATCHEDBY_DFA_INTERPRETER)
  224. return PCRE2_ERROR_DFA_UFUNC;
  225. entrysize = pcre2_substring_nametable_scan(match_data->code, stringname,
  226. &first, &last);
  227. if (entrysize < 0) return entrysize;
  228. failrc = PCRE2_ERROR_UNAVAILABLE;
  229. for (entry = first; entry <= last; entry += entrysize)
  230. {
  231. uint32_t n = GET2(entry, 0);
  232. if (n < match_data->oveccount)
  233. {
  234. if (match_data->ovector[n*2] != PCRE2_UNSET)
  235. return pcre2_substring_length_bynumber(match_data, n, sizeptr);
  236. failrc = PCRE2_ERROR_UNSET;
  237. }
  238. }
  239. return failrc;
  240. }
  241. /*************************************************
  242. * Get length of a numbered substring *
  243. *************************************************/
  244. /* This function returns the length of a captured substring. If the start is
  245. beyond the end (which can happen when \K is used in an assertion), it sets the
  246. length to zero.
  247. Arguments:
  248. match_data pointer to match data
  249. stringnumber the number of the required substring
  250. sizeptr where to put the length, if not NULL
  251. Returns: if successful: 0
  252. if not successful, a negative error code:
  253. PCRE2_ERROR_NOSUBSTRING: no such substring
  254. PCRE2_ERROR_UNAVAILABLE: ovector is too small
  255. PCRE2_ERROR_UNSET: substring is not set
  256. */
  257. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  258. pcre2_substring_length_bynumber(pcre2_match_data *match_data,
  259. uint32_t stringnumber, PCRE2_SIZE *sizeptr)
  260. {
  261. PCRE2_SIZE left, right;
  262. int count = match_data->rc;
  263. if (count == PCRE2_ERROR_PARTIAL)
  264. {
  265. if (stringnumber > 0) return PCRE2_ERROR_PARTIAL;
  266. count = 0;
  267. }
  268. else if (count < 0) return count; /* Match failed */
  269. if (match_data->matchedby != PCRE2_MATCHEDBY_DFA_INTERPRETER)
  270. {
  271. if (stringnumber > match_data->code->top_bracket)
  272. return PCRE2_ERROR_NOSUBSTRING;
  273. if (stringnumber >= match_data->oveccount)
  274. return PCRE2_ERROR_UNAVAILABLE;
  275. if (match_data->ovector[stringnumber*2] == PCRE2_UNSET)
  276. return PCRE2_ERROR_UNSET;
  277. }
  278. else /* Matched using pcre2_dfa_match() */
  279. {
  280. if (stringnumber >= match_data->oveccount) return PCRE2_ERROR_UNAVAILABLE;
  281. if (count != 0 && stringnumber >= (uint32_t)count) return PCRE2_ERROR_UNSET;
  282. }
  283. left = match_data->ovector[stringnumber*2];
  284. right = match_data->ovector[stringnumber*2+1];
  285. if (sizeptr != NULL) *sizeptr = (left > right)? 0 : right - left;
  286. return 0;
  287. }
  288. /*************************************************
  289. * Extract all captured strings to new memory *
  290. *************************************************/
  291. /* This function gets one chunk of memory and builds a list of pointers and all
  292. the captured substrings in it. A NULL pointer is put on the end of the list.
  293. The substrings are zero-terminated, but also, if the final argument is
  294. non-NULL, a list of lengths is also returned. This allows binary data to be
  295. handled.
  296. Arguments:
  297. match_data points to the match data
  298. listptr set to point to the list of pointers
  299. lengthsptr set to point to the list of lengths (may be NULL)
  300. Returns: if successful: 0
  301. if not successful, a negative error code:
  302. PCRE2_ERROR_NOMEMORY: failed to get memory,
  303. or a match failure code
  304. */
  305. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  306. pcre2_substring_list_get(pcre2_match_data *match_data, PCRE2_UCHAR ***listptr,
  307. PCRE2_SIZE **lengthsptr)
  308. {
  309. int i, count, count2;
  310. PCRE2_SIZE size;
  311. PCRE2_SIZE *lensp;
  312. pcre2_memctl *memp;
  313. PCRE2_UCHAR **listp;
  314. PCRE2_UCHAR *sp;
  315. PCRE2_SIZE *ovector;
  316. if ((count = match_data->rc) < 0) return count; /* Match failed */
  317. if (count == 0) count = match_data->oveccount; /* Ovector too small */
  318. count2 = 2*count;
  319. ovector = match_data->ovector;
  320. size = sizeof(pcre2_memctl) + sizeof(PCRE2_UCHAR *); /* For final NULL */
  321. if (lengthsptr != NULL) size += sizeof(PCRE2_SIZE) * count; /* For lengths */
  322. for (i = 0; i < count2; i += 2)
  323. {
  324. size += sizeof(PCRE2_UCHAR *) + CU2BYTES(1);
  325. if (ovector[i+1] > ovector[i]) size += CU2BYTES(ovector[i+1] - ovector[i]);
  326. }
  327. memp = PRIV(memctl_malloc)(size, (pcre2_memctl *)match_data);
  328. if (memp == NULL) return PCRE2_ERROR_NOMEMORY;
  329. *listptr = listp = (PCRE2_UCHAR **)((char *)memp + sizeof(pcre2_memctl));
  330. lensp = (PCRE2_SIZE *)((char *)listp + sizeof(PCRE2_UCHAR *) * (count + 1));
  331. if (lengthsptr == NULL)
  332. {
  333. sp = (PCRE2_UCHAR *)lensp;
  334. lensp = NULL;
  335. }
  336. else
  337. {
  338. *lengthsptr = lensp;
  339. sp = (PCRE2_UCHAR *)((char *)lensp + sizeof(PCRE2_SIZE) * count);
  340. }
  341. for (i = 0; i < count2; i += 2)
  342. {
  343. size = (ovector[i+1] > ovector[i])? (ovector[i+1] - ovector[i]) : 0;
  344. /* Size == 0 includes the case when the capture is unset. Avoid adding
  345. PCRE2_UNSET to match_data->subject because it overflows, even though with
  346. zero size calling memcpy() is harmless. */
  347. if (size != 0) memcpy(sp, match_data->subject + ovector[i], CU2BYTES(size));
  348. *listp++ = sp;
  349. if (lensp != NULL) *lensp++ = size;
  350. sp += size;
  351. *sp++ = 0;
  352. }
  353. *listp = NULL;
  354. return 0;
  355. }
  356. /*************************************************
  357. * Free memory obtained by substring_list_get *
  358. *************************************************/
  359. /*
  360. Argument: the result of a previous pcre2_substring_list_get()
  361. Returns: nothing
  362. */
  363. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  364. pcre2_substring_list_free(PCRE2_SPTR *list)
  365. {
  366. if (list != NULL)
  367. {
  368. pcre2_memctl *memctl = (pcre2_memctl *)((char *)list - sizeof(pcre2_memctl));
  369. memctl->free(memctl, memctl->memory_data);
  370. }
  371. }
  372. /*************************************************
  373. * Find (multiple) entries for named string *
  374. *************************************************/
  375. /* This function scans the nametable for a given name, using binary chop. It
  376. returns either two pointers to the entries in the table, or, if no pointers are
  377. given, the number of a unique group with the given name. If duplicate names are
  378. permitted, and the name is not unique, an error is generated.
  379. Arguments:
  380. code the compiled regex
  381. stringname the name whose entries required
  382. firstptr where to put the pointer to the first entry
  383. lastptr where to put the pointer to the last entry
  384. Returns: PCRE2_ERROR_NOSUBSTRING if the name is not found
  385. otherwise, if firstptr and lastptr are NULL:
  386. a group number for a unique substring
  387. else PCRE2_ERROR_NOUNIQUESUBSTRING
  388. otherwise:
  389. the length of each entry, having set firstptr and lastptr
  390. */
  391. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  392. pcre2_substring_nametable_scan(const pcre2_code *code, PCRE2_SPTR stringname,
  393. PCRE2_SPTR *firstptr, PCRE2_SPTR *lastptr)
  394. {
  395. uint16_t bot = 0;
  396. uint16_t top = code->name_count;
  397. uint16_t entrysize = code->name_entry_size;
  398. PCRE2_SPTR nametable = (PCRE2_SPTR)((char *)code + sizeof(pcre2_real_code));
  399. while (top > bot)
  400. {
  401. uint16_t mid = (top + bot) / 2;
  402. PCRE2_SPTR entry = nametable + entrysize*mid;
  403. int c = PRIV(strcmp)(stringname, entry + IMM2_SIZE);
  404. if (c == 0)
  405. {
  406. PCRE2_SPTR first;
  407. PCRE2_SPTR last;
  408. PCRE2_SPTR lastentry;
  409. lastentry = nametable + entrysize * (code->name_count - 1);
  410. first = last = entry;
  411. while (first > nametable)
  412. {
  413. if (PRIV(strcmp)(stringname, (first - entrysize + IMM2_SIZE)) != 0) break;
  414. first -= entrysize;
  415. }
  416. while (last < lastentry)
  417. {
  418. if (PRIV(strcmp)(stringname, (last + entrysize + IMM2_SIZE)) != 0) break;
  419. last += entrysize;
  420. }
  421. if (firstptr == NULL) return (first == last)?
  422. (int)GET2(entry, 0) : PCRE2_ERROR_NOUNIQUESUBSTRING;
  423. *firstptr = first;
  424. *lastptr = last;
  425. return entrysize;
  426. }
  427. if (c > 0) bot = mid + 1; else top = mid;
  428. }
  429. return PCRE2_ERROR_NOSUBSTRING;
  430. }
  431. /*************************************************
  432. * Find number for named string *
  433. *************************************************/
  434. /* This function is a convenience wrapper for pcre2_substring_nametable_scan()
  435. when it is known that names are unique. If there are duplicate names, it is not
  436. defined which number is returned.
  437. Arguments:
  438. code the compiled regex
  439. stringname the name whose number is required
  440. Returns: the number of the named parenthesis, or a negative number
  441. PCRE2_ERROR_NOSUBSTRING if not found
  442. PCRE2_ERROR_NOUNIQUESUBSTRING if not unique
  443. */
  444. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  445. pcre2_substring_number_from_name(const pcre2_code *code,
  446. PCRE2_SPTR stringname)
  447. {
  448. return pcre2_substring_nametable_scan(code, stringname, NULL, NULL);
  449. }
  450. /* End of pcre2_substring.c */