pcre_get.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. Copyright (c) 1997-2012 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains some convenience functions for extracting substrings
  33. from the subject string after a regex match has succeeded. The original idea
  34. for these functions came from Scott Wimer. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "pcre_config.h"
  37. #endif
  38. #include "pcre_internal.h"
  39. /*************************************************
  40. * Find number for named string *
  41. *************************************************/
  42. /* This function is used by the get_first_set() function below, as well
  43. as being generally available. It assumes that names are unique.
  44. Arguments:
  45. code the compiled regex
  46. stringname the name whose number is required
  47. Returns: the number of the named parentheses, or a negative number
  48. (PCRE_ERROR_NOSUBSTRING) if not found
  49. */
  50. #if defined COMPILE_PCRE8
  51. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  52. pcre_get_stringnumber(const pcre *code, const char *stringname)
  53. #elif defined COMPILE_PCRE16
  54. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  55. pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
  56. #elif defined COMPILE_PCRE32
  57. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  58. pcre32_get_stringnumber(const pcre32 *code, PCRE_SPTR32 stringname)
  59. #endif
  60. {
  61. int rc;
  62. int entrysize;
  63. int top, bot;
  64. pcre_uchar *nametable;
  65. #ifdef COMPILE_PCRE8
  66. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  67. return rc;
  68. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  69. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  70. return rc;
  71. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  72. return rc;
  73. #endif
  74. #ifdef COMPILE_PCRE16
  75. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  76. return rc;
  77. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  78. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  79. return rc;
  80. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  81. return rc;
  82. #endif
  83. #ifdef COMPILE_PCRE32
  84. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  85. return rc;
  86. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  87. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  88. return rc;
  89. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  90. return rc;
  91. #endif
  92. bot = 0;
  93. while (top > bot)
  94. {
  95. int mid = (top + bot) / 2;
  96. pcre_uchar *entry = nametable + entrysize*mid;
  97. int c = STRCMP_UC_UC((pcre_uchar *)stringname,
  98. (pcre_uchar *)(entry + IMM2_SIZE));
  99. if (c == 0) return GET2(entry, 0);
  100. if (c > 0) bot = mid + 1; else top = mid;
  101. }
  102. return PCRE_ERROR_NOSUBSTRING;
  103. }
  104. /*************************************************
  105. * Find (multiple) entries for named string *
  106. *************************************************/
  107. /* This is used by the get_first_set() function below, as well as being
  108. generally available. It is used when duplicated names are permitted.
  109. Arguments:
  110. code the compiled regex
  111. stringname the name whose entries required
  112. firstptr where to put the pointer to the first entry
  113. lastptr where to put the pointer to the last entry
  114. Returns: the length of each entry, or a negative number
  115. (PCRE_ERROR_NOSUBSTRING) if not found
  116. */
  117. #if defined COMPILE_PCRE8
  118. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  119. pcre_get_stringtable_entries(const pcre *code, const char *stringname,
  120. char **firstptr, char **lastptr)
  121. #elif defined COMPILE_PCRE16
  122. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  123. pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
  124. PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
  125. #elif defined COMPILE_PCRE32
  126. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  127. pcre32_get_stringtable_entries(const pcre32 *code, PCRE_SPTR32 stringname,
  128. PCRE_UCHAR32 **firstptr, PCRE_UCHAR32 **lastptr)
  129. #endif
  130. {
  131. int rc;
  132. int entrysize;
  133. int top, bot;
  134. pcre_uchar *nametable, *lastentry;
  135. #ifdef COMPILE_PCRE8
  136. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  137. return rc;
  138. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  139. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  140. return rc;
  141. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  142. return rc;
  143. #endif
  144. #ifdef COMPILE_PCRE16
  145. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  146. return rc;
  147. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  148. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  149. return rc;
  150. if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  151. return rc;
  152. #endif
  153. #ifdef COMPILE_PCRE32
  154. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  155. return rc;
  156. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  157. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  158. return rc;
  159. if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  160. return rc;
  161. #endif
  162. lastentry = nametable + entrysize * (top - 1);
  163. bot = 0;
  164. while (top > bot)
  165. {
  166. int mid = (top + bot) / 2;
  167. pcre_uchar *entry = nametable + entrysize*mid;
  168. int c = STRCMP_UC_UC((pcre_uchar *)stringname,
  169. (pcre_uchar *)(entry + IMM2_SIZE));
  170. if (c == 0)
  171. {
  172. pcre_uchar *first = entry;
  173. pcre_uchar *last = entry;
  174. while (first > nametable)
  175. {
  176. if (STRCMP_UC_UC((pcre_uchar *)stringname,
  177. (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
  178. first -= entrysize;
  179. }
  180. while (last < lastentry)
  181. {
  182. if (STRCMP_UC_UC((pcre_uchar *)stringname,
  183. (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
  184. last += entrysize;
  185. }
  186. #if defined COMPILE_PCRE8
  187. *firstptr = (char *)first;
  188. *lastptr = (char *)last;
  189. #elif defined COMPILE_PCRE16
  190. *firstptr = (PCRE_UCHAR16 *)first;
  191. *lastptr = (PCRE_UCHAR16 *)last;
  192. #elif defined COMPILE_PCRE32
  193. *firstptr = (PCRE_UCHAR32 *)first;
  194. *lastptr = (PCRE_UCHAR32 *)last;
  195. #endif
  196. return entrysize;
  197. }
  198. if (c > 0) bot = mid + 1; else top = mid;
  199. }
  200. return PCRE_ERROR_NOSUBSTRING;
  201. }
  202. /*************************************************
  203. * Find first set of multiple named strings *
  204. *************************************************/
  205. /* This function allows for duplicate names in the table of named substrings.
  206. It returns the number of the first one that was set in a pattern match.
  207. Arguments:
  208. code the compiled regex
  209. stringname the name of the capturing substring
  210. ovector the vector of matched substrings
  211. stringcount number of captured substrings
  212. Returns: the number of the first that is set,
  213. or the number of the last one if none are set,
  214. or a negative number on error
  215. */
  216. #if defined COMPILE_PCRE8
  217. static int
  218. get_first_set(const pcre *code, const char *stringname, int *ovector,
  219. int stringcount)
  220. #elif defined COMPILE_PCRE16
  221. static int
  222. get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector,
  223. int stringcount)
  224. #elif defined COMPILE_PCRE32
  225. static int
  226. get_first_set(const pcre32 *code, PCRE_SPTR32 stringname, int *ovector,
  227. int stringcount)
  228. #endif
  229. {
  230. const REAL_PCRE *re = (const REAL_PCRE *)code;
  231. int entrysize;
  232. pcre_uchar *entry;
  233. #if defined COMPILE_PCRE8
  234. char *first, *last;
  235. #elif defined COMPILE_PCRE16
  236. PCRE_UCHAR16 *first, *last;
  237. #elif defined COMPILE_PCRE32
  238. PCRE_UCHAR32 *first, *last;
  239. #endif
  240. #if defined COMPILE_PCRE8
  241. if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
  242. return pcre_get_stringnumber(code, stringname);
  243. entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
  244. #elif defined COMPILE_PCRE16
  245. if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
  246. return pcre16_get_stringnumber(code, stringname);
  247. entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
  248. #elif defined COMPILE_PCRE32
  249. if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
  250. return pcre32_get_stringnumber(code, stringname);
  251. entrysize = pcre32_get_stringtable_entries(code, stringname, &first, &last);
  252. #endif
  253. if (entrysize <= 0) return entrysize;
  254. for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
  255. {
  256. int n = GET2(entry, 0);
  257. if (n < stringcount && ovector[n*2] >= 0) return n;
  258. }
  259. return GET2(entry, 0);
  260. }
  261. /*************************************************
  262. * Copy captured string to given buffer *
  263. *************************************************/
  264. /* This function copies a single captured substring into a given buffer.
  265. Note that we use memcpy() rather than strncpy() in case there are binary zeros
  266. in the string.
  267. Arguments:
  268. subject the subject string that was matched
  269. ovector pointer to the offsets table
  270. stringcount the number of substrings that were captured
  271. (i.e. the yield of the pcre_exec call, unless
  272. that was zero, in which case it should be 1/3
  273. of the offset table size)
  274. stringnumber the number of the required substring
  275. buffer where to put the substring
  276. size the size of the buffer
  277. Returns: if successful:
  278. the length of the copied string, not including the zero
  279. that is put on the end; can be zero
  280. if not successful:
  281. PCRE_ERROR_NOMEMORY (-6) buffer too small
  282. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  283. */
  284. #if defined COMPILE_PCRE8
  285. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  286. pcre_copy_substring(const char *subject, int *ovector, int stringcount,
  287. int stringnumber, char *buffer, int size)
  288. #elif defined COMPILE_PCRE16
  289. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  290. pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
  291. int stringnumber, PCRE_UCHAR16 *buffer, int size)
  292. #elif defined COMPILE_PCRE32
  293. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  294. pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
  295. int stringnumber, PCRE_UCHAR32 *buffer, int size)
  296. #endif
  297. {
  298. int yield;
  299. if (stringnumber < 0 || stringnumber >= stringcount)
  300. return PCRE_ERROR_NOSUBSTRING;
  301. stringnumber *= 2;
  302. yield = ovector[stringnumber+1] - ovector[stringnumber];
  303. if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
  304. memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
  305. buffer[yield] = 0;
  306. return yield;
  307. }
  308. /*************************************************
  309. * Copy named captured string to given buffer *
  310. *************************************************/
  311. /* This function copies a single captured substring into a given buffer,
  312. identifying it by name. If the regex permits duplicate names, the first
  313. substring that is set is chosen.
  314. Arguments:
  315. code the compiled regex
  316. subject the subject string that was matched
  317. ovector pointer to the offsets table
  318. stringcount the number of substrings that were captured
  319. (i.e. the yield of the pcre_exec call, unless
  320. that was zero, in which case it should be 1/3
  321. of the offset table size)
  322. stringname the name of the required substring
  323. buffer where to put the substring
  324. size the size of the buffer
  325. Returns: if successful:
  326. the length of the copied string, not including the zero
  327. that is put on the end; can be zero
  328. if not successful:
  329. PCRE_ERROR_NOMEMORY (-6) buffer too small
  330. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  331. */
  332. #if defined COMPILE_PCRE8
  333. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  334. pcre_copy_named_substring(const pcre *code, const char *subject,
  335. int *ovector, int stringcount, const char *stringname,
  336. char *buffer, int size)
  337. #elif defined COMPILE_PCRE16
  338. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  339. pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
  340. int *ovector, int stringcount, PCRE_SPTR16 stringname,
  341. PCRE_UCHAR16 *buffer, int size)
  342. #elif defined COMPILE_PCRE32
  343. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  344. pcre32_copy_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
  345. int *ovector, int stringcount, PCRE_SPTR32 stringname,
  346. PCRE_UCHAR32 *buffer, int size)
  347. #endif
  348. {
  349. int n = get_first_set(code, stringname, ovector, stringcount);
  350. if (n <= 0) return n;
  351. #if defined COMPILE_PCRE8
  352. return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
  353. #elif defined COMPILE_PCRE16
  354. return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
  355. #elif defined COMPILE_PCRE32
  356. return pcre32_copy_substring(subject, ovector, stringcount, n, buffer, size);
  357. #endif
  358. }
  359. /*************************************************
  360. * Copy all captured strings to new store *
  361. *************************************************/
  362. /* This function gets one chunk of store and builds a list of pointers and all
  363. of the captured substrings in it. A NULL pointer is put on the end of the list.
  364. Arguments:
  365. subject the subject string that was matched
  366. ovector pointer to the offsets table
  367. stringcount the number of substrings that were captured
  368. (i.e. the yield of the pcre_exec call, unless
  369. that was zero, in which case it should be 1/3
  370. of the offset table size)
  371. listptr set to point to the list of pointers
  372. Returns: if successful: 0
  373. if not successful:
  374. PCRE_ERROR_NOMEMORY (-6) failed to get store
  375. */
  376. #if defined COMPILE_PCRE8
  377. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  378. pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
  379. const char ***listptr)
  380. #elif defined COMPILE_PCRE16
  381. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  382. pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
  383. PCRE_SPTR16 **listptr)
  384. #elif defined COMPILE_PCRE32
  385. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  386. pcre32_get_substring_list(PCRE_SPTR32 subject, int *ovector, int stringcount,
  387. PCRE_SPTR32 **listptr)
  388. #endif
  389. {
  390. int i;
  391. int size = sizeof(pcre_uchar *);
  392. int double_count = stringcount * 2;
  393. pcre_uchar **stringlist;
  394. pcre_uchar *p;
  395. for (i = 0; i < double_count; i += 2)
  396. {
  397. size += sizeof(pcre_uchar *) + IN_UCHARS(1);
  398. if (ovector[i+1] > ovector[i]) size += IN_UCHARS(ovector[i+1] - ovector[i]);
  399. }
  400. stringlist = (pcre_uchar **)(PUBL(malloc))(size);
  401. if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
  402. #if defined COMPILE_PCRE8
  403. *listptr = (const char **)stringlist;
  404. #elif defined COMPILE_PCRE16
  405. *listptr = (PCRE_SPTR16 *)stringlist;
  406. #elif defined COMPILE_PCRE32
  407. *listptr = (PCRE_SPTR32 *)stringlist;
  408. #endif
  409. p = (pcre_uchar *)(stringlist + stringcount + 1);
  410. for (i = 0; i < double_count; i += 2)
  411. {
  412. int len = (ovector[i+1] > ovector[i])? (ovector[i+1] - ovector[i]) : 0;
  413. memcpy(p, subject + ovector[i], IN_UCHARS(len));
  414. *stringlist++ = p;
  415. p += len;
  416. *p++ = 0;
  417. }
  418. *stringlist = NULL;
  419. return 0;
  420. }
  421. /*************************************************
  422. * Free store obtained by get_substring_list *
  423. *************************************************/
  424. /* This function exists for the benefit of people calling PCRE from non-C
  425. programs that can call its functions, but not free() or (PUBL(free))()
  426. directly.
  427. Argument: the result of a previous pcre_get_substring_list()
  428. Returns: nothing
  429. */
  430. #if defined COMPILE_PCRE8
  431. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  432. pcre_free_substring_list(const char **pointer)
  433. #elif defined COMPILE_PCRE16
  434. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  435. pcre16_free_substring_list(PCRE_SPTR16 *pointer)
  436. #elif defined COMPILE_PCRE32
  437. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  438. pcre32_free_substring_list(PCRE_SPTR32 *pointer)
  439. #endif
  440. {
  441. (PUBL(free))((void *)pointer);
  442. }
  443. /*************************************************
  444. * Copy captured string to new store *
  445. *************************************************/
  446. /* This function copies a single captured substring into a piece of new
  447. store
  448. Arguments:
  449. subject the subject string that was matched
  450. ovector pointer to the offsets table
  451. stringcount the number of substrings that were captured
  452. (i.e. the yield of the pcre_exec call, unless
  453. that was zero, in which case it should be 1/3
  454. of the offset table size)
  455. stringnumber the number of the required substring
  456. stringptr where to put a pointer to the substring
  457. Returns: if successful:
  458. the length of the string, not including the zero that
  459. is put on the end; can be zero
  460. if not successful:
  461. PCRE_ERROR_NOMEMORY (-6) failed to get store
  462. PCRE_ERROR_NOSUBSTRING (-7) substring not present
  463. */
  464. #if defined COMPILE_PCRE8
  465. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  466. pcre_get_substring(const char *subject, int *ovector, int stringcount,
  467. int stringnumber, const char **stringptr)
  468. #elif defined COMPILE_PCRE16
  469. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  470. pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
  471. int stringnumber, PCRE_SPTR16 *stringptr)
  472. #elif defined COMPILE_PCRE32
  473. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  474. pcre32_get_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
  475. int stringnumber, PCRE_SPTR32 *stringptr)
  476. #endif
  477. {
  478. int yield;
  479. pcre_uchar *substring;
  480. if (stringnumber < 0 || stringnumber >= stringcount)
  481. return PCRE_ERROR_NOSUBSTRING;
  482. stringnumber *= 2;
  483. yield = ovector[stringnumber+1] - ovector[stringnumber];
  484. substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
  485. if (substring == NULL) return PCRE_ERROR_NOMEMORY;
  486. memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
  487. substring[yield] = 0;
  488. #if defined COMPILE_PCRE8
  489. *stringptr = (const char *)substring;
  490. #elif defined COMPILE_PCRE16
  491. *stringptr = (PCRE_SPTR16)substring;
  492. #elif defined COMPILE_PCRE32
  493. *stringptr = (PCRE_SPTR32)substring;
  494. #endif
  495. return yield;
  496. }
  497. /*************************************************
  498. * Copy named captured string to new store *
  499. *************************************************/
  500. /* This function copies a single captured substring, identified by name, into
  501. new store. If the regex permits duplicate names, the first substring that is
  502. set is chosen.
  503. Arguments:
  504. code the compiled regex
  505. subject the subject string that was matched
  506. ovector pointer to the offsets table
  507. stringcount the number of substrings that were captured
  508. (i.e. the yield of the pcre_exec call, unless
  509. that was zero, in which case it should be 1/3
  510. of the offset table size)
  511. stringname the name of the required substring
  512. stringptr where to put the pointer
  513. Returns: if successful:
  514. the length of the copied string, not including the zero
  515. that is put on the end; can be zero
  516. if not successful:
  517. PCRE_ERROR_NOMEMORY (-6) couldn't get memory
  518. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  519. */
  520. #if defined COMPILE_PCRE8
  521. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  522. pcre_get_named_substring(const pcre *code, const char *subject,
  523. int *ovector, int stringcount, const char *stringname,
  524. const char **stringptr)
  525. #elif defined COMPILE_PCRE16
  526. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  527. pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
  528. int *ovector, int stringcount, PCRE_SPTR16 stringname,
  529. PCRE_SPTR16 *stringptr)
  530. #elif defined COMPILE_PCRE32
  531. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  532. pcre32_get_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
  533. int *ovector, int stringcount, PCRE_SPTR32 stringname,
  534. PCRE_SPTR32 *stringptr)
  535. #endif
  536. {
  537. int n = get_first_set(code, stringname, ovector, stringcount);
  538. if (n <= 0) return n;
  539. #if defined COMPILE_PCRE8
  540. return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
  541. #elif defined COMPILE_PCRE16
  542. return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
  543. #elif defined COMPILE_PCRE32
  544. return pcre32_get_substring(subject, ovector, stringcount, n, stringptr);
  545. #endif
  546. }
  547. /*************************************************
  548. * Free store obtained by get_substring *
  549. *************************************************/
  550. /* This function exists for the benefit of people calling PCRE from non-C
  551. programs that can call its functions, but not free() or (PUBL(free))()
  552. directly.
  553. Argument: the result of a previous pcre_get_substring()
  554. Returns: nothing
  555. */
  556. #if defined COMPILE_PCRE8
  557. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  558. pcre_free_substring(const char *pointer)
  559. #elif defined COMPILE_PCRE16
  560. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  561. pcre16_free_substring(PCRE_SPTR16 pointer)
  562. #elif defined COMPILE_PCRE32
  563. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  564. pcre32_free_substring(PCRE_SPTR32 pointer)
  565. #endif
  566. {
  567. (PUBL(free))((void *)pointer);
  568. }
  569. /* End of pcre_get.c */