PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-1.0.1/source/hbpcre/pcreget.c

#
C | 465 lines | 174 code | 71 blank | 220 comment | 50 complexity | a113f1b3d2ee16efa1e4444ab0da60be MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  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-2008 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. #if 2875
  36. #include "_hbconf.h"
  37. #endif
  38. #include "pcreinal.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. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  51. pcre_get_stringnumber(const pcre *code, const char *stringname)
  52. {
  53. int rc;
  54. int entrysize;
  55. int top, bot;
  56. uschar *nametable;
  57. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  58. return rc;
  59. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  60. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  61. return rc;
  62. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  63. return rc;
  64. bot = 0;
  65. while (top > bot)
  66. {
  67. int mid = (top + bot) / 2;
  68. uschar *entry = nametable + entrysize*mid;
  69. int c = strcmp(stringname, (char *)(entry + 2));
  70. if (c == 0) return (entry[0] << 8) + entry[1];
  71. if (c > 0) bot = mid + 1; else top = mid;
  72. }
  73. return PCRE_ERROR_NOSUBSTRING;
  74. }
  75. /*************************************************
  76. * Find (multiple) entries for named string *
  77. *************************************************/
  78. /* This is used by the get_first_set() function below, as well as being
  79. generally available. It is used when duplicated names are permitted.
  80. Arguments:
  81. code the compiled regex
  82. stringname the name whose entries required
  83. firstptr where to put the pointer to the first entry
  84. lastptr where to put the pointer to the last entry
  85. Returns: the length of each entry, or a negative number
  86. (PCRE_ERROR_NOSUBSTRING) if not found
  87. */
  88. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  89. pcre_get_stringtable_entries(const pcre *code, const char *stringname,
  90. char **firstptr, char **lastptr)
  91. {
  92. int rc;
  93. int entrysize;
  94. int top, bot;
  95. uschar *nametable, *lastentry;
  96. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  97. return rc;
  98. if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  99. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  100. return rc;
  101. if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  102. return rc;
  103. lastentry = nametable + entrysize * (top - 1);
  104. bot = 0;
  105. while (top > bot)
  106. {
  107. int mid = (top + bot) / 2;
  108. uschar *entry = nametable + entrysize*mid;
  109. int c = strcmp(stringname, (char *)(entry + 2));
  110. if (c == 0)
  111. {
  112. uschar *first = entry;
  113. uschar *last = entry;
  114. while (first > nametable)
  115. {
  116. if (strcmp(stringname, (char *)(first - entrysize + 2)) != 0) break;
  117. first -= entrysize;
  118. }
  119. while (last < lastentry)
  120. {
  121. if (strcmp(stringname, (char *)(last + entrysize + 2)) != 0) break;
  122. last += entrysize;
  123. }
  124. *firstptr = (char *)first;
  125. *lastptr = (char *)last;
  126. return entrysize;
  127. }
  128. if (c > 0) bot = mid + 1; else top = mid;
  129. }
  130. return PCRE_ERROR_NOSUBSTRING;
  131. }
  132. /*************************************************
  133. * Find first set of multiple named strings *
  134. *************************************************/
  135. /* This function allows for duplicate names in the table of named substrings.
  136. It returns the number of the first one that was set in a pattern match.
  137. Arguments:
  138. code the compiled regex
  139. stringname the name of the capturing substring
  140. ovector the vector of matched substrings
  141. Returns: the number of the first that is set,
  142. or the number of the last one if none are set,
  143. or a negative number on error
  144. */
  145. static int
  146. get_first_set(const pcre *code, const char *stringname, int *ovector)
  147. {
  148. const real_pcre *re = (const real_pcre *)code;
  149. int entrysize;
  150. char *first, *last;
  151. uschar *entry;
  152. if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
  153. return pcre_get_stringnumber(code, stringname);
  154. entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
  155. if (entrysize <= 0) return entrysize;
  156. for (entry = (uschar *)first; entry <= (uschar *)last; entry += entrysize)
  157. {
  158. int n = (entry[0] << 8) + entry[1];
  159. if (ovector[n*2] >= 0) return n;
  160. }
  161. return (first[0] << 8) + first[1];
  162. }
  163. /*************************************************
  164. * Copy captured string to given buffer *
  165. *************************************************/
  166. /* This function copies a single captured substring into a given buffer.
  167. Note that we use memcpy() rather than strncpy() in case there are binary zeros
  168. in the string.
  169. Arguments:
  170. subject the subject string that was matched
  171. ovector pointer to the offsets table
  172. stringcount the number of substrings that were captured
  173. (i.e. the yield of the pcre_exec call, unless
  174. that was zero, in which case it should be 1/3
  175. of the offset table size)
  176. stringnumber the number of the required substring
  177. buffer where to put the substring
  178. size the size of the buffer
  179. Returns: if successful:
  180. the length of the copied string, not including the zero
  181. that is put on the end; can be zero
  182. if not successful:
  183. PCRE_ERROR_NOMEMORY (-6) buffer too small
  184. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  185. */
  186. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  187. pcre_copy_substring(const char *subject, int *ovector, int stringcount,
  188. int stringnumber, char *buffer, int size)
  189. {
  190. int yield;
  191. if (stringnumber < 0 || stringnumber >= stringcount)
  192. return PCRE_ERROR_NOSUBSTRING;
  193. stringnumber *= 2;
  194. yield = ovector[stringnumber+1] - ovector[stringnumber];
  195. if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
  196. memcpy(buffer, subject + ovector[stringnumber], yield);
  197. buffer[yield] = 0;
  198. return yield;
  199. }
  200. /*************************************************
  201. * Copy named captured string to given buffer *
  202. *************************************************/
  203. /* This function copies a single captured substring into a given buffer,
  204. identifying it by name. If the regex permits duplicate names, the first
  205. substring that is set is chosen.
  206. Arguments:
  207. code the compiled regex
  208. subject the subject string that was matched
  209. ovector pointer to the offsets table
  210. stringcount the number of substrings that were captured
  211. (i.e. the yield of the pcre_exec call, unless
  212. that was zero, in which case it should be 1/3
  213. of the offset table size)
  214. stringname the name of the required substring
  215. buffer where to put the substring
  216. size the size of the buffer
  217. Returns: if successful:
  218. the length of the copied string, not including the zero
  219. that is put on the end; can be zero
  220. if not successful:
  221. PCRE_ERROR_NOMEMORY (-6) buffer too small
  222. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  223. */
  224. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  225. pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,
  226. int stringcount, const char *stringname, char *buffer, int size)
  227. {
  228. int n = get_first_set(code, stringname, ovector);
  229. if (n <= 0) return n;
  230. return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
  231. }
  232. /*************************************************
  233. * Copy all captured strings to new store *
  234. *************************************************/
  235. /* This function gets one chunk of store and builds a list of pointers and all
  236. of the captured substrings in it. A NULL pointer is put on the end of the list.
  237. Arguments:
  238. subject the subject string that was matched
  239. ovector pointer to the offsets table
  240. stringcount the number of substrings that were captured
  241. (i.e. the yield of the pcre_exec call, unless
  242. that was zero, in which case it should be 1/3
  243. of the offset table size)
  244. listptr set to point to the list of pointers
  245. Returns: if successful: 0
  246. if not successful:
  247. PCRE_ERROR_NOMEMORY (-6) failed to get store
  248. */
  249. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  250. pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
  251. const char ***listptr)
  252. {
  253. int i;
  254. int size = sizeof(char *);
  255. int double_count = stringcount * 2;
  256. char **stringlist;
  257. char *p;
  258. for (i = 0; i < double_count; i += 2)
  259. size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
  260. stringlist = (char **)(pcre_malloc)(size);
  261. if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
  262. *listptr = (const char **)stringlist;
  263. p = (char *)(stringlist + stringcount + 1);
  264. for (i = 0; i < double_count; i += 2)
  265. {
  266. int len = ovector[i+1] - ovector[i];
  267. memcpy(p, subject + ovector[i], len);
  268. *stringlist++ = p;
  269. p += len;
  270. *p++ = 0;
  271. }
  272. *stringlist = NULL;
  273. return 0;
  274. }
  275. /*************************************************
  276. * Free store obtained by get_substring_list *
  277. *************************************************/
  278. /* This function exists for the benefit of people calling PCRE from non-C
  279. programs that can call its functions, but not free() or (pcre_free)() directly.
  280. Argument: the result of a previous pcre_get_substring_list()
  281. Returns: nothing
  282. */
  283. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  284. pcre_free_substring_list(const char **pointer)
  285. {
  286. (pcre_free)((void *)pointer);
  287. }
  288. /*************************************************
  289. * Copy captured string to new store *
  290. *************************************************/
  291. /* This function copies a single captured substring into a piece of new
  292. store
  293. Arguments:
  294. subject the subject string that was matched
  295. ovector pointer to the offsets table
  296. stringcount the number of substrings that were captured
  297. (i.e. the yield of the pcre_exec call, unless
  298. that was zero, in which case it should be 1/3
  299. of the offset table size)
  300. stringnumber the number of the required substring
  301. stringptr where to put a pointer to the substring
  302. Returns: if successful:
  303. the length of the string, not including the zero that
  304. is put on the end; can be zero
  305. if not successful:
  306. PCRE_ERROR_NOMEMORY (-6) failed to get store
  307. PCRE_ERROR_NOSUBSTRING (-7) substring not present
  308. */
  309. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  310. pcre_get_substring(const char *subject, int *ovector, int stringcount,
  311. int stringnumber, const char **stringptr)
  312. {
  313. int yield;
  314. char *substring;
  315. if (stringnumber < 0 || stringnumber >= stringcount)
  316. return PCRE_ERROR_NOSUBSTRING;
  317. stringnumber *= 2;
  318. yield = ovector[stringnumber+1] - ovector[stringnumber];
  319. substring = (char *)(pcre_malloc)(yield + 1);
  320. if (substring == NULL) return PCRE_ERROR_NOMEMORY;
  321. memcpy(substring, subject + ovector[stringnumber], yield);
  322. substring[yield] = 0;
  323. *stringptr = substring;
  324. return yield;
  325. }
  326. /*************************************************
  327. * Copy named captured string to new store *
  328. *************************************************/
  329. /* This function copies a single captured substring, identified by name, into
  330. new store. If the regex permits duplicate names, the first substring that is
  331. set is chosen.
  332. Arguments:
  333. code the compiled regex
  334. subject the subject string that was matched
  335. ovector pointer to the offsets table
  336. stringcount the number of substrings that were captured
  337. (i.e. the yield of the pcre_exec call, unless
  338. that was zero, in which case it should be 1/3
  339. of the offset table size)
  340. stringname the name of the required substring
  341. stringptr where to put the pointer
  342. Returns: if successful:
  343. the length of the copied string, not including the zero
  344. that is put on the end; can be zero
  345. if not successful:
  346. PCRE_ERROR_NOMEMORY (-6) couldn't get memory
  347. PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
  348. */
  349. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  350. pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,
  351. int stringcount, const char *stringname, const char **stringptr)
  352. {
  353. int n = get_first_set(code, stringname, ovector);
  354. if (n <= 0) return n;
  355. return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
  356. }
  357. /*************************************************
  358. * Free store obtained by get_substring *
  359. *************************************************/
  360. /* This function exists for the benefit of people calling PCRE from non-C
  361. programs that can call its functions, but not free() or (pcre_free)() directly.
  362. Argument: the result of a previous pcre_get_substring()
  363. Returns: nothing
  364. */
  365. PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
  366. pcre_free_substring(const char *pointer)
  367. {
  368. (pcre_free)((void *)pointer);
  369. }
  370. /* End of pcre_get.c */