pcre_study.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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 the external function pcre_study(), along with local
  33. supporting functions. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. #include "pcre_internal.h"
  38. #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
  39. /* Returns from set_start_bits() */
  40. enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
  41. /*************************************************
  42. * Find the minimum subject length for a group *
  43. *************************************************/
  44. /* Scan a parenthesized group and compute the minimum length of subject that
  45. is needed to match it. This is a lower bound; it does not mean there is a
  46. string of that length that matches. In UTF8 mode, the result is in characters
  47. rather than bytes.
  48. Arguments:
  49. re compiled pattern block
  50. code pointer to start of group (the bracket)
  51. startcode pointer to start of the whole pattern's code
  52. options the compiling options
  53. recurses chain of recurse_check to catch mutual recursion
  54. countptr pointer to call count (to catch over complexity)
  55. Returns: the minimum length
  56. -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
  57. -2 internal error (missing capturing bracket)
  58. -3 internal error (opcode not listed)
  59. */
  60. static int
  61. find_minlength(const REAL_PCRE *re, const pcre_uchar *code,
  62. const pcre_uchar *startcode, int options, recurse_check *recurses,
  63. int *countptr)
  64. {
  65. int length = -1;
  66. /* PCRE_UTF16 has the same value as PCRE_UTF8. */
  67. BOOL utf = (options & PCRE_UTF8) != 0;
  68. BOOL had_recurse = FALSE;
  69. recurse_check this_recurse;
  70. register int branchlength = 0;
  71. register pcre_uchar *cc = (pcre_uchar *)code + 1 + LINK_SIZE;
  72. if ((*countptr)++ > 1000) return -1; /* too complex */
  73. if (*code == OP_CBRA || *code == OP_SCBRA ||
  74. *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += IMM2_SIZE;
  75. /* Scan along the opcodes for this branch. If we get to the end of the
  76. branch, check the length against that of the other branches. */
  77. for (;;)
  78. {
  79. int d, min;
  80. pcre_uchar *cs, *ce;
  81. register pcre_uchar op = *cc;
  82. switch (op)
  83. {
  84. case OP_COND:
  85. case OP_SCOND:
  86. /* If there is only one branch in a condition, the implied branch has zero
  87. length, so we don't add anything. This covers the DEFINE "condition"
  88. automatically. */
  89. cs = cc + GET(cc, 1);
  90. if (*cs != OP_ALT)
  91. {
  92. cc = cs + 1 + LINK_SIZE;
  93. break;
  94. }
  95. /* Otherwise we can fall through and treat it the same as any other
  96. subpattern. */
  97. case OP_CBRA:
  98. case OP_SCBRA:
  99. case OP_BRA:
  100. case OP_SBRA:
  101. case OP_CBRAPOS:
  102. case OP_SCBRAPOS:
  103. case OP_BRAPOS:
  104. case OP_SBRAPOS:
  105. case OP_ONCE:
  106. case OP_ONCE_NC:
  107. d = find_minlength(re, cc, startcode, options, recurses, countptr);
  108. if (d < 0) return d;
  109. branchlength += d;
  110. do cc += GET(cc, 1); while (*cc == OP_ALT);
  111. cc += 1 + LINK_SIZE;
  112. break;
  113. /* ACCEPT makes things far too complicated; we have to give up. */
  114. case OP_ACCEPT:
  115. case OP_ASSERT_ACCEPT:
  116. return -1;
  117. /* Reached end of a branch; if it's a ket it is the end of a nested
  118. call. If it's ALT it is an alternation in a nested call. If it is END it's
  119. the end of the outer call. All can be handled by the same code. If an
  120. ACCEPT was previously encountered, use the length that was in force at that
  121. time, and pass back the shortest ACCEPT length. */
  122. case OP_ALT:
  123. case OP_KET:
  124. case OP_KETRMAX:
  125. case OP_KETRMIN:
  126. case OP_KETRPOS:
  127. case OP_END:
  128. if (length < 0 || (!had_recurse && branchlength < length))
  129. length = branchlength;
  130. if (op != OP_ALT) return length;
  131. cc += 1 + LINK_SIZE;
  132. branchlength = 0;
  133. had_recurse = FALSE;
  134. break;
  135. /* Skip over assertive subpatterns */
  136. case OP_ASSERT:
  137. case OP_ASSERT_NOT:
  138. case OP_ASSERTBACK:
  139. case OP_ASSERTBACK_NOT:
  140. do cc += GET(cc, 1); while (*cc == OP_ALT);
  141. /* Fall through */
  142. /* Skip over things that don't match chars */
  143. case OP_REVERSE:
  144. case OP_CREF:
  145. case OP_DNCREF:
  146. case OP_RREF:
  147. case OP_DNRREF:
  148. case OP_DEF:
  149. case OP_CALLOUT:
  150. case OP_SOD:
  151. case OP_SOM:
  152. case OP_EOD:
  153. case OP_EODN:
  154. case OP_CIRC:
  155. case OP_CIRCM:
  156. case OP_DOLL:
  157. case OP_DOLLM:
  158. case OP_NOT_WORD_BOUNDARY:
  159. case OP_WORD_BOUNDARY:
  160. cc += PRIV(OP_lengths)[*cc];
  161. break;
  162. /* Skip over a subpattern that has a {0} or {0,x} quantifier */
  163. case OP_BRAZERO:
  164. case OP_BRAMINZERO:
  165. case OP_BRAPOSZERO:
  166. case OP_SKIPZERO:
  167. cc += PRIV(OP_lengths)[*cc];
  168. do cc += GET(cc, 1); while (*cc == OP_ALT);
  169. cc += 1 + LINK_SIZE;
  170. break;
  171. /* Handle literal characters and + repetitions */
  172. case OP_CHAR:
  173. case OP_CHARI:
  174. case OP_NOT:
  175. case OP_NOTI:
  176. case OP_PLUS:
  177. case OP_PLUSI:
  178. case OP_MINPLUS:
  179. case OP_MINPLUSI:
  180. case OP_POSPLUS:
  181. case OP_POSPLUSI:
  182. case OP_NOTPLUS:
  183. case OP_NOTPLUSI:
  184. case OP_NOTMINPLUS:
  185. case OP_NOTMINPLUSI:
  186. case OP_NOTPOSPLUS:
  187. case OP_NOTPOSPLUSI:
  188. branchlength++;
  189. cc += 2;
  190. #ifdef SUPPORT_UTF
  191. if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
  192. #endif
  193. break;
  194. case OP_TYPEPLUS:
  195. case OP_TYPEMINPLUS:
  196. case OP_TYPEPOSPLUS:
  197. branchlength++;
  198. cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
  199. break;
  200. /* Handle exact repetitions. The count is already in characters, but we
  201. need to skip over a multibyte character in UTF8 mode. */
  202. case OP_EXACT:
  203. case OP_EXACTI:
  204. case OP_NOTEXACT:
  205. case OP_NOTEXACTI:
  206. branchlength += GET2(cc,1);
  207. cc += 2 + IMM2_SIZE;
  208. #ifdef SUPPORT_UTF
  209. if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
  210. #endif
  211. break;
  212. case OP_TYPEEXACT:
  213. branchlength += GET2(cc,1);
  214. cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
  215. || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
  216. break;
  217. /* Handle single-char non-literal matchers */
  218. case OP_PROP:
  219. case OP_NOTPROP:
  220. cc += 2;
  221. /* Fall through */
  222. case OP_NOT_DIGIT:
  223. case OP_DIGIT:
  224. case OP_NOT_WHITESPACE:
  225. case OP_WHITESPACE:
  226. case OP_NOT_WORDCHAR:
  227. case OP_WORDCHAR:
  228. case OP_ANY:
  229. case OP_ALLANY:
  230. case OP_EXTUNI:
  231. case OP_HSPACE:
  232. case OP_NOT_HSPACE:
  233. case OP_VSPACE:
  234. case OP_NOT_VSPACE:
  235. branchlength++;
  236. cc++;
  237. break;
  238. /* "Any newline" might match two characters, but it also might match just
  239. one. */
  240. case OP_ANYNL:
  241. branchlength += 1;
  242. cc++;
  243. break;
  244. /* The single-byte matcher means we can't proceed in UTF-8 mode. (In
  245. non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever
  246. appear, but leave the code, just in case.) */
  247. case OP_ANYBYTE:
  248. #ifdef SUPPORT_UTF
  249. if (utf) return -1;
  250. #endif
  251. branchlength++;
  252. cc++;
  253. break;
  254. /* For repeated character types, we have to test for \p and \P, which have
  255. an extra two bytes of parameters. */
  256. case OP_TYPESTAR:
  257. case OP_TYPEMINSTAR:
  258. case OP_TYPEQUERY:
  259. case OP_TYPEMINQUERY:
  260. case OP_TYPEPOSSTAR:
  261. case OP_TYPEPOSQUERY:
  262. if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
  263. cc += PRIV(OP_lengths)[op];
  264. break;
  265. case OP_TYPEUPTO:
  266. case OP_TYPEMINUPTO:
  267. case OP_TYPEPOSUPTO:
  268. if (cc[1 + IMM2_SIZE] == OP_PROP
  269. || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
  270. cc += PRIV(OP_lengths)[op];
  271. break;
  272. /* Check a class for variable quantification */
  273. case OP_CLASS:
  274. case OP_NCLASS:
  275. #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  276. case OP_XCLASS:
  277. /* The original code caused an unsigned overflow in 64 bit systems,
  278. so now we use a conditional statement. */
  279. if (op == OP_XCLASS)
  280. cc += GET(cc, 1);
  281. else
  282. cc += PRIV(OP_lengths)[OP_CLASS];
  283. #else
  284. cc += PRIV(OP_lengths)[OP_CLASS];
  285. #endif
  286. switch (*cc)
  287. {
  288. case OP_CRPLUS:
  289. case OP_CRMINPLUS:
  290. case OP_CRPOSPLUS:
  291. branchlength++;
  292. /* Fall through */
  293. case OP_CRSTAR:
  294. case OP_CRMINSTAR:
  295. case OP_CRQUERY:
  296. case OP_CRMINQUERY:
  297. case OP_CRPOSSTAR:
  298. case OP_CRPOSQUERY:
  299. cc++;
  300. break;
  301. case OP_CRRANGE:
  302. case OP_CRMINRANGE:
  303. case OP_CRPOSRANGE:
  304. branchlength += GET2(cc,1);
  305. cc += 1 + 2 * IMM2_SIZE;
  306. break;
  307. default:
  308. branchlength++;
  309. break;
  310. }
  311. break;
  312. /* Backreferences and subroutine calls are treated in the same way: we find
  313. the minimum length for the subpattern. A recursion, however, causes an
  314. a flag to be set that causes the length of this branch to be ignored. The
  315. logic is that a recursion can only make sense if there is another
  316. alternation that stops the recursing. That will provide the minimum length
  317. (when no recursion happens). A backreference within the group that it is
  318. referencing behaves in the same way.
  319. If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
  320. matches an empty string (by default it causes a matching failure), so in
  321. that case we must set the minimum length to zero. */
  322. case OP_DNREF: /* Duplicate named pattern back reference */
  323. case OP_DNREFI:
  324. if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
  325. {
  326. int count = GET2(cc, 1+IMM2_SIZE);
  327. pcre_uchar *slot = (pcre_uchar *)re +
  328. re->name_table_offset + GET2(cc, 1) * re->name_entry_size;
  329. d = INT_MAX;
  330. while (count-- > 0)
  331. {
  332. ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(slot, 0));
  333. if (cs == NULL) return -2;
  334. do ce += GET(ce, 1); while (*ce == OP_ALT);
  335. if (cc > cs && cc < ce) /* Simple recursion */
  336. {
  337. d = 0;
  338. had_recurse = TRUE;
  339. break;
  340. }
  341. else
  342. {
  343. recurse_check *r = recurses;
  344. for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
  345. if (r != NULL) /* Mutual recursion */
  346. {
  347. d = 0;
  348. had_recurse = TRUE;
  349. break;
  350. }
  351. else
  352. {
  353. int dd;
  354. this_recurse.prev = recurses;
  355. this_recurse.group = cs;
  356. dd = find_minlength(re, cs, startcode, options, &this_recurse,
  357. countptr);
  358. if (dd < d) d = dd;
  359. }
  360. }
  361. slot += re->name_entry_size;
  362. }
  363. }
  364. else d = 0;
  365. cc += 1 + 2*IMM2_SIZE;
  366. goto REPEAT_BACK_REFERENCE;
  367. case OP_REF: /* Single back reference */
  368. case OP_REFI:
  369. if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
  370. {
  371. ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(cc, 1));
  372. if (cs == NULL) return -2;
  373. do ce += GET(ce, 1); while (*ce == OP_ALT);
  374. if (cc > cs && cc < ce) /* Simple recursion */
  375. {
  376. d = 0;
  377. had_recurse = TRUE;
  378. }
  379. else
  380. {
  381. recurse_check *r = recurses;
  382. for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
  383. if (r != NULL) /* Mutual recursion */
  384. {
  385. d = 0;
  386. had_recurse = TRUE;
  387. }
  388. else
  389. {
  390. this_recurse.prev = recurses;
  391. this_recurse.group = cs;
  392. d = find_minlength(re, cs, startcode, options, &this_recurse,
  393. countptr);
  394. }
  395. }
  396. }
  397. else d = 0;
  398. cc += 1 + IMM2_SIZE;
  399. /* Handle repeated back references */
  400. REPEAT_BACK_REFERENCE:
  401. switch (*cc)
  402. {
  403. case OP_CRSTAR:
  404. case OP_CRMINSTAR:
  405. case OP_CRQUERY:
  406. case OP_CRMINQUERY:
  407. case OP_CRPOSSTAR:
  408. case OP_CRPOSQUERY:
  409. min = 0;
  410. cc++;
  411. break;
  412. case OP_CRPLUS:
  413. case OP_CRMINPLUS:
  414. case OP_CRPOSPLUS:
  415. min = 1;
  416. cc++;
  417. break;
  418. case OP_CRRANGE:
  419. case OP_CRMINRANGE:
  420. case OP_CRPOSRANGE:
  421. min = GET2(cc, 1);
  422. cc += 1 + 2 * IMM2_SIZE;
  423. break;
  424. default:
  425. min = 1;
  426. break;
  427. }
  428. branchlength += min * d;
  429. break;
  430. /* We can easily detect direct recursion, but not mutual recursion. This is
  431. caught by a recursion depth count. */
  432. case OP_RECURSE:
  433. cs = ce = (pcre_uchar *)startcode + GET(cc, 1);
  434. do ce += GET(ce, 1); while (*ce == OP_ALT);
  435. if (cc > cs && cc < ce) /* Simple recursion */
  436. had_recurse = TRUE;
  437. else
  438. {
  439. recurse_check *r = recurses;
  440. for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
  441. if (r != NULL) /* Mutual recursion */
  442. had_recurse = TRUE;
  443. else
  444. {
  445. this_recurse.prev = recurses;
  446. this_recurse.group = cs;
  447. branchlength += find_minlength(re, cs, startcode, options,
  448. &this_recurse, countptr);
  449. }
  450. }
  451. cc += 1 + LINK_SIZE;
  452. break;
  453. /* Anything else does not or need not match a character. We can get the
  454. item's length from the table, but for those that can match zero occurrences
  455. of a character, we must take special action for UTF-8 characters. As it
  456. happens, the "NOT" versions of these opcodes are used at present only for
  457. ASCII characters, so they could be omitted from this list. However, in
  458. future that may change, so we include them here so as not to leave a
  459. gotcha for a future maintainer. */
  460. case OP_UPTO:
  461. case OP_UPTOI:
  462. case OP_NOTUPTO:
  463. case OP_NOTUPTOI:
  464. case OP_MINUPTO:
  465. case OP_MINUPTOI:
  466. case OP_NOTMINUPTO:
  467. case OP_NOTMINUPTOI:
  468. case OP_POSUPTO:
  469. case OP_POSUPTOI:
  470. case OP_NOTPOSUPTO:
  471. case OP_NOTPOSUPTOI:
  472. case OP_STAR:
  473. case OP_STARI:
  474. case OP_NOTSTAR:
  475. case OP_NOTSTARI:
  476. case OP_MINSTAR:
  477. case OP_MINSTARI:
  478. case OP_NOTMINSTAR:
  479. case OP_NOTMINSTARI:
  480. case OP_POSSTAR:
  481. case OP_POSSTARI:
  482. case OP_NOTPOSSTAR:
  483. case OP_NOTPOSSTARI:
  484. case OP_QUERY:
  485. case OP_QUERYI:
  486. case OP_NOTQUERY:
  487. case OP_NOTQUERYI:
  488. case OP_MINQUERY:
  489. case OP_MINQUERYI:
  490. case OP_NOTMINQUERY:
  491. case OP_NOTMINQUERYI:
  492. case OP_POSQUERY:
  493. case OP_POSQUERYI:
  494. case OP_NOTPOSQUERY:
  495. case OP_NOTPOSQUERYI:
  496. cc += PRIV(OP_lengths)[op];
  497. #ifdef SUPPORT_UTF
  498. if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
  499. #endif
  500. break;
  501. /* Skip these, but we need to add in the name length. */
  502. case OP_MARK:
  503. case OP_PRUNE_ARG:
  504. case OP_SKIP_ARG:
  505. case OP_THEN_ARG:
  506. cc += PRIV(OP_lengths)[op] + cc[1];
  507. break;
  508. /* The remaining opcodes are just skipped over. */
  509. case OP_CLOSE:
  510. case OP_COMMIT:
  511. case OP_FAIL:
  512. case OP_PRUNE:
  513. case OP_SET_SOM:
  514. case OP_SKIP:
  515. case OP_THEN:
  516. cc += PRIV(OP_lengths)[op];
  517. break;
  518. /* This should not occur: we list all opcodes explicitly so that when
  519. new ones get added they are properly considered. */
  520. default:
  521. return -3;
  522. }
  523. }
  524. /* Control never gets here */
  525. }
  526. /*************************************************
  527. * Set a bit and maybe its alternate case *
  528. *************************************************/
  529. /* Given a character, set its first byte's bit in the table, and also the
  530. corresponding bit for the other version of a letter if we are caseless. In
  531. UTF-8 mode, for characters greater than 127, we can only do the caseless thing
  532. when Unicode property support is available.
  533. Arguments:
  534. start_bits points to the bit map
  535. p points to the character
  536. caseless the caseless flag
  537. cd the block with char table pointers
  538. utf TRUE for UTF-8 / UTF-16 / UTF-32 mode
  539. Returns: pointer after the character
  540. */
  541. static const pcre_uchar *
  542. set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless,
  543. compile_data *cd, BOOL utf)
  544. {
  545. pcre_uint32 c = *p;
  546. #ifdef COMPILE_PCRE8
  547. SET_BIT(c);
  548. #ifdef SUPPORT_UTF
  549. if (utf && c > 127)
  550. {
  551. GETCHARINC(c, p);
  552. #ifdef SUPPORT_UCP
  553. if (caseless)
  554. {
  555. pcre_uchar buff[6];
  556. c = UCD_OTHERCASE(c);
  557. (void)PRIV(ord2utf)(c, buff);
  558. SET_BIT(buff[0]);
  559. }
  560. #endif /* Not SUPPORT_UCP */
  561. return p;
  562. }
  563. #else /* Not SUPPORT_UTF */
  564. (void)(utf); /* Stops warning for unused parameter */
  565. #endif /* SUPPORT_UTF */
  566. /* Not UTF-8 mode, or character is less than 127. */
  567. if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
  568. return p + 1;
  569. #endif /* COMPILE_PCRE8 */
  570. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  571. if (c > 0xff)
  572. {
  573. c = 0xff;
  574. caseless = FALSE;
  575. }
  576. SET_BIT(c);
  577. #ifdef SUPPORT_UTF
  578. if (utf && c > 127)
  579. {
  580. GETCHARINC(c, p);
  581. #ifdef SUPPORT_UCP
  582. if (caseless)
  583. {
  584. c = UCD_OTHERCASE(c);
  585. if (c > 0xff)
  586. c = 0xff;
  587. SET_BIT(c);
  588. }
  589. #endif /* SUPPORT_UCP */
  590. return p;
  591. }
  592. #else /* Not SUPPORT_UTF */
  593. (void)(utf); /* Stops warning for unused parameter */
  594. #endif /* SUPPORT_UTF */
  595. if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
  596. return p + 1;
  597. #endif
  598. }
  599. /*************************************************
  600. * Set bits for a positive character type *
  601. *************************************************/
  602. /* This function sets starting bits for a character type. In UTF-8 mode, we can
  603. only do a direct setting for bytes less than 128, as otherwise there can be
  604. confusion with bytes in the middle of UTF-8 characters. In a "traditional"
  605. environment, the tables will only recognize ASCII characters anyway, but in at
  606. least one Windows environment, some higher bytes bits were set in the tables.
  607. So we deal with that case by considering the UTF-8 encoding.
  608. Arguments:
  609. start_bits the starting bitmap
  610. cbit type the type of character wanted
  611. table_limit 32 for non-UTF-8; 16 for UTF-8
  612. cd the block with char table pointers
  613. Returns: nothing
  614. */
  615. static void
  616. set_type_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit,
  617. compile_data *cd)
  618. {
  619. register pcre_uint32 c;
  620. for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
  621. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  622. if (table_limit == 32) return;
  623. for (c = 128; c < 256; c++)
  624. {
  625. if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
  626. {
  627. pcre_uchar buff[6];
  628. (void)PRIV(ord2utf)(c, buff);
  629. SET_BIT(buff[0]);
  630. }
  631. }
  632. #endif
  633. }
  634. /*************************************************
  635. * Set bits for a negative character type *
  636. *************************************************/
  637. /* This function sets starting bits for a negative character type such as \D.
  638. In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
  639. otherwise there can be confusion with bytes in the middle of UTF-8 characters.
  640. Unlike in the positive case, where we can set appropriate starting bits for
  641. specific high-valued UTF-8 characters, in this case we have to set the bits for
  642. all high-valued characters. The lowest is 0xc2, but we overkill by starting at
  643. 0xc0 (192) for simplicity.
  644. Arguments:
  645. start_bits the starting bitmap
  646. cbit type the type of character wanted
  647. table_limit 32 for non-UTF-8; 16 for UTF-8
  648. cd the block with char table pointers
  649. Returns: nothing
  650. */
  651. static void
  652. set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit,
  653. compile_data *cd)
  654. {
  655. register pcre_uint32 c;
  656. for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
  657. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  658. if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
  659. #endif
  660. }
  661. /*************************************************
  662. * Create bitmap of starting bytes *
  663. *************************************************/
  664. /* This function scans a compiled unanchored expression recursively and
  665. attempts to build a bitmap of the set of possible starting bytes. As time goes
  666. by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
  667. useful for parenthesized groups in patterns such as (a*)b where the group
  668. provides some optional starting bytes but scanning must continue at the outer
  669. level to find at least one mandatory byte. At the outermost level, this
  670. function fails unless the result is SSB_DONE.
  671. Arguments:
  672. code points to an expression
  673. start_bits points to a 32-byte table, initialized to 0
  674. utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
  675. cd the block with char table pointers
  676. Returns: SSB_FAIL => Failed to find any starting bytes
  677. SSB_DONE => Found mandatory starting bytes
  678. SSB_CONTINUE => Found optional starting bytes
  679. SSB_UNKNOWN => Hit an unrecognized opcode
  680. */
  681. static int
  682. set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf,
  683. compile_data *cd)
  684. {
  685. register pcre_uint32 c;
  686. int yield = SSB_DONE;
  687. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  688. int table_limit = utf? 16:32;
  689. #else
  690. int table_limit = 32;
  691. #endif
  692. #if 0
  693. /* ========================================================================= */
  694. /* The following comment and code was inserted in January 1999. In May 2006,
  695. when it was observed to cause compiler warnings about unused values, I took it
  696. out again. If anybody is still using OS/2, they will have to put it back
  697. manually. */
  698. /* This next statement and the later reference to dummy are here in order to
  699. trick the optimizer of the IBM C compiler for OS/2 into generating correct
  700. code. Apparently IBM isn't going to fix the problem, and we would rather not
  701. disable optimization (in this module it actually makes a big difference, and
  702. the pcre module can use all the optimization it can get). */
  703. volatile int dummy;
  704. /* ========================================================================= */
  705. #endif
  706. do
  707. {
  708. BOOL try_next = TRUE;
  709. const pcre_uchar *tcode = code + 1 + LINK_SIZE;
  710. if (*code == OP_CBRA || *code == OP_SCBRA ||
  711. *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
  712. while (try_next) /* Loop for items in this branch */
  713. {
  714. int rc;
  715. switch(*tcode)
  716. {
  717. /* If we reach something we don't understand, it means a new opcode has
  718. been created that hasn't been added to this code. Hopefully this problem
  719. will be discovered during testing. */
  720. default:
  721. return SSB_UNKNOWN;
  722. /* Fail for a valid opcode that implies no starting bits. */
  723. case OP_ACCEPT:
  724. case OP_ASSERT_ACCEPT:
  725. case OP_ALLANY:
  726. case OP_ANY:
  727. case OP_ANYBYTE:
  728. case OP_CIRC:
  729. case OP_CIRCM:
  730. case OP_CLOSE:
  731. case OP_COMMIT:
  732. case OP_COND:
  733. case OP_CREF:
  734. case OP_DEF:
  735. case OP_DNCREF:
  736. case OP_DNREF:
  737. case OP_DNREFI:
  738. case OP_DNRREF:
  739. case OP_DOLL:
  740. case OP_DOLLM:
  741. case OP_END:
  742. case OP_EOD:
  743. case OP_EODN:
  744. case OP_EXTUNI:
  745. case OP_FAIL:
  746. case OP_MARK:
  747. case OP_NOT:
  748. case OP_NOTEXACT:
  749. case OP_NOTEXACTI:
  750. case OP_NOTI:
  751. case OP_NOTMINPLUS:
  752. case OP_NOTMINPLUSI:
  753. case OP_NOTMINQUERY:
  754. case OP_NOTMINQUERYI:
  755. case OP_NOTMINSTAR:
  756. case OP_NOTMINSTARI:
  757. case OP_NOTMINUPTO:
  758. case OP_NOTMINUPTOI:
  759. case OP_NOTPLUS:
  760. case OP_NOTPLUSI:
  761. case OP_NOTPOSPLUS:
  762. case OP_NOTPOSPLUSI:
  763. case OP_NOTPOSQUERY:
  764. case OP_NOTPOSQUERYI:
  765. case OP_NOTPOSSTAR:
  766. case OP_NOTPOSSTARI:
  767. case OP_NOTPOSUPTO:
  768. case OP_NOTPOSUPTOI:
  769. case OP_NOTPROP:
  770. case OP_NOTQUERY:
  771. case OP_NOTQUERYI:
  772. case OP_NOTSTAR:
  773. case OP_NOTSTARI:
  774. case OP_NOTUPTO:
  775. case OP_NOTUPTOI:
  776. case OP_NOT_HSPACE:
  777. case OP_NOT_VSPACE:
  778. case OP_PRUNE:
  779. case OP_PRUNE_ARG:
  780. case OP_RECURSE:
  781. case OP_REF:
  782. case OP_REFI:
  783. case OP_REVERSE:
  784. case OP_RREF:
  785. case OP_SCOND:
  786. case OP_SET_SOM:
  787. case OP_SKIP:
  788. case OP_SKIP_ARG:
  789. case OP_SOD:
  790. case OP_SOM:
  791. case OP_THEN:
  792. case OP_THEN_ARG:
  793. return SSB_FAIL;
  794. /* A "real" property test implies no starting bits, but the fake property
  795. PT_CLIST identifies a list of characters. These lists are short, as they
  796. are used for characters with more than one "other case", so there is no
  797. point in recognizing them for OP_NOTPROP. */
  798. case OP_PROP:
  799. if (tcode[1] != PT_CLIST) return SSB_FAIL;
  800. {
  801. const pcre_uint32 *p = PRIV(ucd_caseless_sets) + tcode[2];
  802. while ((c = *p++) < NOTACHAR)
  803. {
  804. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  805. if (utf)
  806. {
  807. pcre_uchar buff[6];
  808. (void)PRIV(ord2utf)(c, buff);
  809. c = buff[0];
  810. }
  811. #endif
  812. if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
  813. }
  814. }
  815. try_next = FALSE;
  816. break;
  817. /* We can ignore word boundary tests. */
  818. case OP_WORD_BOUNDARY:
  819. case OP_NOT_WORD_BOUNDARY:
  820. tcode++;
  821. break;
  822. /* If we hit a bracket or a positive lookahead assertion, recurse to set
  823. bits from within the subpattern. If it can't find anything, we have to
  824. give up. If it finds some mandatory character(s), we are done for this
  825. branch. Otherwise, carry on scanning after the subpattern. */
  826. case OP_BRA:
  827. case OP_SBRA:
  828. case OP_CBRA:
  829. case OP_SCBRA:
  830. case OP_BRAPOS:
  831. case OP_SBRAPOS:
  832. case OP_CBRAPOS:
  833. case OP_SCBRAPOS:
  834. case OP_ONCE:
  835. case OP_ONCE_NC:
  836. case OP_ASSERT:
  837. rc = set_start_bits(tcode, start_bits, utf, cd);
  838. if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
  839. if (rc == SSB_DONE) try_next = FALSE; else
  840. {
  841. do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
  842. tcode += 1 + LINK_SIZE;
  843. }
  844. break;
  845. /* If we hit ALT or KET, it means we haven't found anything mandatory in
  846. this branch, though we might have found something optional. For ALT, we
  847. continue with the next alternative, but we have to arrange that the final
  848. result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
  849. return SSB_CONTINUE: if this is the top level, that indicates failure,
  850. but after a nested subpattern, it causes scanning to continue. */
  851. case OP_ALT:
  852. yield = SSB_CONTINUE;
  853. try_next = FALSE;
  854. break;
  855. case OP_KET:
  856. case OP_KETRMAX:
  857. case OP_KETRMIN:
  858. case OP_KETRPOS:
  859. return SSB_CONTINUE;
  860. /* Skip over callout */
  861. case OP_CALLOUT:
  862. tcode += 2 + 2*LINK_SIZE;
  863. break;
  864. /* Skip over lookbehind and negative lookahead assertions */
  865. case OP_ASSERT_NOT:
  866. case OP_ASSERTBACK:
  867. case OP_ASSERTBACK_NOT:
  868. do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
  869. tcode += 1 + LINK_SIZE;
  870. break;
  871. /* BRAZERO does the bracket, but carries on. */
  872. case OP_BRAZERO:
  873. case OP_BRAMINZERO:
  874. case OP_BRAPOSZERO:
  875. rc = set_start_bits(++tcode, start_bits, utf, cd);
  876. if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
  877. /* =========================================================================
  878. See the comment at the head of this function concerning the next line,
  879. which was an old fudge for the benefit of OS/2.
  880. dummy = 1;
  881. ========================================================================= */
  882. do tcode += GET(tcode,1); while (*tcode == OP_ALT);
  883. tcode += 1 + LINK_SIZE;
  884. break;
  885. /* SKIPZERO skips the bracket. */
  886. case OP_SKIPZERO:
  887. tcode++;
  888. do tcode += GET(tcode,1); while (*tcode == OP_ALT);
  889. tcode += 1 + LINK_SIZE;
  890. break;
  891. /* Single-char * or ? sets the bit and tries the next item */
  892. case OP_STAR:
  893. case OP_MINSTAR:
  894. case OP_POSSTAR:
  895. case OP_QUERY:
  896. case OP_MINQUERY:
  897. case OP_POSQUERY:
  898. tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
  899. break;
  900. case OP_STARI:
  901. case OP_MINSTARI:
  902. case OP_POSSTARI:
  903. case OP_QUERYI:
  904. case OP_MINQUERYI:
  905. case OP_POSQUERYI:
  906. tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
  907. break;
  908. /* Single-char upto sets the bit and tries the next */
  909. case OP_UPTO:
  910. case OP_MINUPTO:
  911. case OP_POSUPTO:
  912. tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf);
  913. break;
  914. case OP_UPTOI:
  915. case OP_MINUPTOI:
  916. case OP_POSUPTOI:
  917. tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf);
  918. break;
  919. /* At least one single char sets the bit and stops */
  920. case OP_EXACT:
  921. tcode += IMM2_SIZE;
  922. /* Fall through */
  923. case OP_CHAR:
  924. case OP_PLUS:
  925. case OP_MINPLUS:
  926. case OP_POSPLUS:
  927. (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
  928. try_next = FALSE;
  929. break;
  930. case OP_EXACTI:
  931. tcode += IMM2_SIZE;
  932. /* Fall through */
  933. case OP_CHARI:
  934. case OP_PLUSI:
  935. case OP_MINPLUSI:
  936. case OP_POSPLUSI:
  937. (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
  938. try_next = FALSE;
  939. break;
  940. /* Special spacing and line-terminating items. These recognize specific
  941. lists of characters. The difference between VSPACE and ANYNL is that the
  942. latter can match the two-character CRLF sequence, but that is not
  943. relevant for finding the first character, so their code here is
  944. identical. */
  945. case OP_HSPACE:
  946. SET_BIT(CHAR_HT);
  947. SET_BIT(CHAR_SPACE);
  948. #ifdef SUPPORT_UTF
  949. if (utf)
  950. {
  951. #ifdef COMPILE_PCRE8
  952. SET_BIT(0xC2); /* For U+00A0 */
  953. SET_BIT(0xE1); /* For U+1680, U+180E */
  954. SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
  955. SET_BIT(0xE3); /* For U+3000 */
  956. #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  957. SET_BIT(0xA0);
  958. SET_BIT(0xFF); /* For characters > 255 */
  959. #endif /* COMPILE_PCRE[8|16|32] */
  960. }
  961. else
  962. #endif /* SUPPORT_UTF */
  963. {
  964. #ifndef EBCDIC
  965. SET_BIT(0xA0);
  966. #endif /* Not EBCDIC */
  967. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  968. SET_BIT(0xFF); /* For characters > 255 */
  969. #endif /* COMPILE_PCRE[16|32] */
  970. }
  971. try_next = FALSE;
  972. break;
  973. case OP_ANYNL:
  974. case OP_VSPACE:
  975. SET_BIT(CHAR_LF);
  976. SET_BIT(CHAR_VT);
  977. SET_BIT(CHAR_FF);
  978. SET_BIT(CHAR_CR);
  979. #ifdef SUPPORT_UTF
  980. if (utf)
  981. {
  982. #ifdef COMPILE_PCRE8
  983. SET_BIT(0xC2); /* For U+0085 */
  984. SET_BIT(0xE2); /* For U+2028, U+2029 */
  985. #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  986. SET_BIT(CHAR_NEL);
  987. SET_BIT(0xFF); /* For characters > 255 */
  988. #endif /* COMPILE_PCRE[8|16|32] */
  989. }
  990. else
  991. #endif /* SUPPORT_UTF */
  992. {
  993. SET_BIT(CHAR_NEL);
  994. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  995. SET_BIT(0xFF); /* For characters > 255 */
  996. #endif
  997. }
  998. try_next = FALSE;
  999. break;
  1000. /* Single character types set the bits and stop. Note that if PCRE_UCP
  1001. is set, we do not see these op codes because \d etc are converted to
  1002. properties. Therefore, these apply in the case when only characters less
  1003. than 256 are recognized to match the types. */
  1004. case OP_NOT_DIGIT:
  1005. set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
  1006. try_next = FALSE;
  1007. break;
  1008. case OP_DIGIT:
  1009. set_type_bits(start_bits, cbit_digit, table_limit, cd);
  1010. try_next = FALSE;
  1011. break;
  1012. /* The cbit_space table has vertical tab as whitespace; we no longer
  1013. have to play fancy tricks because Perl added VT to its whitespace at
  1014. release 5.18. PCRE added it at release 8.34. */
  1015. case OP_NOT_WHITESPACE:
  1016. set_nottype_bits(start_bits, cbit_space, table_limit, cd);
  1017. try_next = FALSE;
  1018. break;
  1019. case OP_WHITESPACE:
  1020. set_type_bits(start_bits, cbit_space, table_limit, cd);
  1021. try_next = FALSE;
  1022. break;
  1023. case OP_NOT_WORDCHAR:
  1024. set_nottype_bits(start_bits, cbit_word, table_limit, cd);
  1025. try_next = FALSE;
  1026. break;
  1027. case OP_WORDCHAR:
  1028. set_type_bits(start_bits, cbit_word, table_limit, cd);
  1029. try_next = FALSE;
  1030. break;
  1031. /* One or more character type fudges the pointer and restarts, knowing
  1032. it will hit a single character type and stop there. */
  1033. case OP_TYPEPLUS:
  1034. case OP_TYPEMINPLUS:
  1035. case OP_TYPEPOSPLUS:
  1036. tcode++;
  1037. break;
  1038. case OP_TYPEEXACT:
  1039. tcode += 1 + IMM2_SIZE;
  1040. break;
  1041. /* Zero or more repeats of character types set the bits and then
  1042. try again. */
  1043. case OP_TYPEUPTO:
  1044. case OP_TYPEMINUPTO:
  1045. case OP_TYPEPOSUPTO:
  1046. tcode += IMM2_SIZE; /* Fall through */
  1047. case OP_TYPESTAR:
  1048. case OP_TYPEMINSTAR:
  1049. case OP_TYPEPOSSTAR:
  1050. case OP_TYPEQUERY:
  1051. case OP_TYPEMINQUERY:
  1052. case OP_TYPEPOSQUERY:
  1053. switch(tcode[1])
  1054. {
  1055. default:
  1056. case OP_ANY:
  1057. case OP_ALLANY:
  1058. return SSB_FAIL;
  1059. case OP_HSPACE:
  1060. SET_BIT(CHAR_HT);
  1061. SET_BIT(CHAR_SPACE);
  1062. #ifdef SUPPORT_UTF
  1063. if (utf)
  1064. {
  1065. #ifdef COMPILE_PCRE8
  1066. SET_BIT(0xC2); /* For U+00A0 */
  1067. SET_BIT(0xE1); /* For U+1680, U+180E */
  1068. SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
  1069. SET_BIT(0xE3); /* For U+3000 */
  1070. #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  1071. SET_BIT(0xA0);
  1072. SET_BIT(0xFF); /* For characters > 255 */
  1073. #endif /* COMPILE_PCRE[8|16|32] */
  1074. }
  1075. else
  1076. #endif /* SUPPORT_UTF */
  1077. #ifndef EBCDIC
  1078. SET_BIT(0xA0);
  1079. #endif /* Not EBCDIC */
  1080. break;
  1081. case OP_ANYNL:
  1082. case OP_VSPACE:
  1083. SET_BIT(CHAR_LF);
  1084. SET_BIT(CHAR_VT);
  1085. SET_BIT(CHAR_FF);
  1086. SET_BIT(CHAR_CR);
  1087. #ifdef SUPPORT_UTF
  1088. if (utf)
  1089. {
  1090. #ifdef COMPILE_PCRE8
  1091. SET_BIT(0xC2); /* For U+0085 */
  1092. SET_BIT(0xE2); /* For U+2028, U+2029 */
  1093. #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  1094. SET_BIT(CHAR_NEL);
  1095. SET_BIT(0xFF); /* For characters > 255 */
  1096. #endif /* COMPILE_PCRE16 */
  1097. }
  1098. else
  1099. #endif /* SUPPORT_UTF */
  1100. SET_BIT(CHAR_NEL);
  1101. break;
  1102. case OP_NOT_DIGIT:
  1103. set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
  1104. break;
  1105. case OP_DIGIT:
  1106. set_type_bits(start_bits, cbit_digit, table_limit, cd);
  1107. break;
  1108. /* The cbit_space table has vertical tab as whitespace; we no longer
  1109. have to play fancy tricks because Perl added VT to its whitespace at
  1110. release 5.18. PCRE added it at release 8.34. */
  1111. case OP_NOT_WHITESPACE:
  1112. set_nottype_bits(start_bits, cbit_space, table_limit, cd);
  1113. break;
  1114. case OP_WHITESPACE:
  1115. set_type_bits(start_bits, cbit_space, table_limit, cd);
  1116. break;
  1117. case OP_NOT_WORDCHAR:
  1118. set_nottype_bits(start_bits, cbit_word, table_limit, cd);
  1119. break;
  1120. case OP_WORDCHAR:
  1121. set_type_bits(start_bits, cbit_word, table_limit, cd);
  1122. break;
  1123. }
  1124. tcode += 2;
  1125. break;
  1126. /* Character class where all the information is in a bit map: set the
  1127. bits and either carry on or not, according to the repeat count. If it was
  1128. a negative class, and we are operating with UTF-8 characters, any byte
  1129. with a value >= 0xc4 is a potentially valid starter because it starts a
  1130. character with a value > 255. */
  1131. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  1132. case OP_XCLASS:
  1133. if ((tcode[1 + LINK_SIZE] & XCL_HASPROP) != 0)
  1134. return SSB_FAIL;
  1135. /* All bits are set. */
  1136. if ((tcode[1 + LINK_SIZE] & XCL_MAP) == 0 && (tcode[1 + LINK_SIZE] & XCL_NOT) != 0)
  1137. return SSB_FAIL;
  1138. #endif
  1139. /* Fall through */
  1140. case OP_NCLASS:
  1141. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  1142. if (utf)
  1143. {
  1144. start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
  1145. memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
  1146. }
  1147. #endif
  1148. #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
  1149. SET_BIT(0xFF); /* For characters > 255 */
  1150. #endif
  1151. /* Fall through */
  1152. case OP_CLASS:
  1153. {
  1154. pcre_uint8 *map;
  1155. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  1156. map = NULL;
  1157. if (*tcode == OP_XCLASS)
  1158. {
  1159. if ((tcode[1 + LINK_SIZE] & XCL_MAP) != 0)
  1160. map = (pcre_uint8 *)(tcode + 1 + LINK_SIZE + 1);
  1161. tcode += GET(tcode, 1);
  1162. }
  1163. else
  1164. #endif
  1165. {
  1166. tcode++;
  1167. map = (pcre_uint8 *)tcode;
  1168. tcode += 32 / sizeof(pcre_uchar);
  1169. }
  1170. /* In UTF-8 mode, the bits in a bit map correspond to character
  1171. values, not to byte values. However, the bit map we are constructing is
  1172. for byte values. So we have to do a conversion for characters whose
  1173. value is > 127. In fact, there are only two possible starting bytes for
  1174. characters in the range 128 - 255. */
  1175. #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
  1176. if (map != NULL)
  1177. #endif
  1178. {
  1179. #if defined SUPPORT_UTF && defined COMPILE_PCRE8
  1180. if (utf)
  1181. {
  1182. for (c = 0; c < 16; c++) start_bits[c] |= map[c];
  1183. for (c = 128; c < 256; c++)
  1184. {
  1185. if ((map[c/8] & (1 << (c&7))) != 0)
  1186. {
  1187. int d = (c >> 6) | 0xc0; /* Set bit for this starter */
  1188. start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
  1189. c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
  1190. }
  1191. }
  1192. }
  1193. else
  1194. #endif
  1195. {
  1196. /* In non-UTF-8 mode, the two bit maps are completely compatible. */
  1197. for (c = 0; c < 32; c++) start_bits[c] |= map[c];
  1198. }
  1199. }
  1200. /* Advance past the bit map, and act on what follows. For a zero
  1201. minimum repeat, continue; otherwise stop processing. */
  1202. switch (*tcode)
  1203. {
  1204. case OP_CRSTAR:
  1205. case OP_CRMINSTAR:
  1206. case OP_CRQUERY:
  1207. case OP_CRMINQUERY:
  1208. case OP_CRPOSSTAR:
  1209. case OP_CRPOSQUERY:
  1210. tcode++;
  1211. break;
  1212. case OP_CRRANGE:
  1213. case OP_CRMINRANGE:
  1214. case OP_CRPOSRANGE:
  1215. if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
  1216. else try_next = FALSE;
  1217. break;
  1218. default:
  1219. try_next = FALSE;
  1220. break;
  1221. }
  1222. }
  1223. break; /* End of bitmap class handling */
  1224. } /* End of switch */
  1225. } /* End of try_next loop */
  1226. code += GET(code, 1); /* Advance to next branch */
  1227. }
  1228. while (*code == OP_ALT);
  1229. return yield;
  1230. }
  1231. /*************************************************
  1232. * Study a compiled expression *
  1233. *************************************************/
  1234. /* This function is handed a compiled expression that it must study to produce
  1235. information that will speed up the matching. It returns a pcre[16]_extra block
  1236. which then gets handed back to pcre_exec().
  1237. Arguments:
  1238. re points to the compiled expression
  1239. options contains option bits
  1240. errorptr points to where to place error messages;
  1241. set NULL unless error
  1242. Returns: pointer to a pcre[16]_extra block, with study_data filled in and
  1243. the appropriate flags set;
  1244. NULL on error or if no optimization possible
  1245. */
  1246. #if defined COMPILE_PCRE8
  1247. PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
  1248. pcre_study(const pcre *external_re, int options, const char **errorptr)
  1249. #elif defined COMPILE_PCRE16
  1250. PCRE_EXP_DEFN pcre16_extra * PCRE_CALL_CONVENTION
  1251. pcre16_study(const pcre16 *external_re, int options, const char **errorptr)
  1252. #elif defined COMPILE_PCRE32
  1253. PCRE_EXP_DEFN pcre32_extra * PCRE_CALL_CONVENTION
  1254. pcre32_study(const pcre32 *external_re, int options, const char **errorptr)
  1255. #endif
  1256. {
  1257. int min;
  1258. int count = 0;
  1259. BOOL bits_set = FALSE;
  1260. pcre_uint8 start_bits[32];
  1261. PUBL(extra) *extra = NULL;
  1262. pcre_study_data *study;
  1263. const pcre_uint8 *tables;
  1264. pcre_uchar *code;
  1265. compile_data compile_block;
  1266. const REAL_PCRE *re = (const REAL_PCRE *)external_re;
  1267. *errorptr = NULL;
  1268. if (re == NULL || re->magic_number != MAGIC_NUMBER)
  1269. {
  1270. *errorptr = "argument is not a compiled regular expression";
  1271. return NULL;
  1272. }
  1273. if ((re->flags & PCRE_MODE) == 0)
  1274. {
  1275. #if defined COMPILE_PCRE8
  1276. *errorptr = "argument not compiled in 8 bit mode";
  1277. #elif defined COMPILE_PCRE16
  1278. *errorptr = "argument not compiled in 16 bit mode";
  1279. #elif defined COMPILE_PCRE32
  1280. *errorptr = "argument not compiled in 32 bit mode";
  1281. #endif
  1282. return NULL;
  1283. }
  1284. if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
  1285. {
  1286. *errorptr = "unknown or incorrect option bit(s) set";
  1287. return NULL;
  1288. }
  1289. code = (pcre_uchar *)re + re->name_table_offset +
  1290. (re->name_count * re->name_entry_size);
  1291. /* For an anchored pattern, or an unanchored pattern that has a first char, or
  1292. a multiline pattern that matches only at "line starts", there is no point in
  1293. seeking a list of starting bytes. */
  1294. if ((re->options & PCRE_ANCHORED) == 0 &&
  1295. (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
  1296. {
  1297. int rc;
  1298. /* Set the character tables in the block that is passed around */
  1299. tables = re->tables;
  1300. #if defined COMPILE_PCRE8
  1301. if (tables == NULL)
  1302. (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
  1303. (void *)(&tables));
  1304. #elif defined COMPILE_PCRE16
  1305. if (tables == NULL)
  1306. (void)pcre16_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
  1307. (void *)(&tables));
  1308. #elif defined COMPILE_PCRE32
  1309. if (tables == NULL)
  1310. (void)pcre32_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
  1311. (void *)(&tables));
  1312. #endif
  1313. compile_block.lcc = tables + lcc_offset;
  1314. compile_block.fcc = tables + fcc_offset;
  1315. compile_block.cbits = tables + cbits_offset;
  1316. compile_block.ctypes = tables + ctypes_offset;
  1317. /* See if we can find a fixed set of initial characters for the pattern. */
  1318. memset(start_bits, 0, 32 * sizeof(pcre_uint8));
  1319. rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
  1320. &compile_block);
  1321. bits_set = rc == SSB_DONE;
  1322. if (rc == SSB_UNKNOWN)
  1323. {
  1324. *errorptr = "internal error: opcode not recognized";
  1325. return NULL;
  1326. }
  1327. }
  1328. /* Find the minimum length of subject string. */
  1329. switch(min = find_minlength(re, code, code, re->options, NULL, &count))
  1330. {
  1331. case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
  1332. case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
  1333. default: break;
  1334. }
  1335. /* If a set of starting bytes has been identified, or if the minimum length is
  1336. greater than zero, or if JIT optimization has been requested, or if
  1337. PCRE_STUDY_EXTRA_NEEDED is set, get a pcre[16]_extra block and a
  1338. pcre_study_data block. The study data is put in the latter, which is pointed to
  1339. by the former, which may also get additional data set later by the calling
  1340. program. At the moment, the size of pcre_study_data is fixed. We nevertheless
  1341. save it in a field for returning via the pcre_fullinfo() function so that if it
  1342. becomes variable in the future, we don't have to change that code. */
  1343. if (bits_set || min > 0 || (options & (
  1344. #ifdef SUPPORT_JIT
  1345. PCRE_STUDY_JIT_COMPILE | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE |
  1346. PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE |
  1347. #endif
  1348. PCRE_STUDY_EXTRA_NEEDED)) != 0)
  1349. {
  1350. extra = (PUBL(extra) *)(PUBL(malloc))
  1351. (sizeof(PUBL(extra)) + sizeof(pcre_study_data));
  1352. if (extra == NULL)
  1353. {
  1354. *errorptr = "failed to get memory";
  1355. return NULL;
  1356. }
  1357. study = (pcre_study_data *)((char *)extra + sizeof(PUBL(extra)));
  1358. extra->flags = PCRE_EXTRA_STUDY_DATA;
  1359. extra->study_data = study;
  1360. study->size = sizeof(pcre_study_data);
  1361. study->flags = 0;
  1362. /* Set the start bits always, to avoid unset memory errors if the
  1363. study data is written to a file, but set the flag only if any of the bits
  1364. are set, to save time looking when none are. */
  1365. if (bits_set)
  1366. {
  1367. study->flags |= PCRE_STUDY_MAPPED;
  1368. memcpy(study->start_bits, start_bits, sizeof(start_bits));
  1369. }
  1370. else memset(study->start_bits, 0, 32 * sizeof(pcre_uint8));
  1371. #ifdef PCRE_DEBUG
  1372. if (bits_set)
  1373. {
  1374. pcre_uint8 *ptr = start_bits;
  1375. int i;
  1376. printf("Start bits:\n");
  1377. for (i = 0; i < 32; i++)
  1378. printf("%3d: %02x%s", i * 8, *ptr++, ((i + 1) & 0x7) != 0? " " : "\n");
  1379. }
  1380. #endif
  1381. /* Always set the minlength value in the block, because the JIT compiler
  1382. makes use of it. However, don't set the bit unless the length is greater than
  1383. zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
  1384. checking the zero case. */
  1385. if (min > 0)
  1386. {
  1387. study->flags |= PCRE_STUDY_MINLEN;
  1388. study->minlength = min;
  1389. }
  1390. else study->minlength = 0;
  1391. /* If JIT support was compiled and requested, attempt the JIT compilation.
  1392. If no starting bytes were found, and the minimum length is zero, and JIT
  1393. compilation fails, abandon the extra block and return NULL, unless
  1394. PCRE_STUDY_EXTRA_NEEDED is set. */
  1395. #ifdef SUPPORT_JIT
  1396. extra->executable_jit = NULL;
  1397. if ((options & PCRE_STUDY_JIT_COMPILE) != 0)
  1398. PRIV(jit_compile)(re, extra, JIT_COMPILE);
  1399. if ((options & PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE) != 0)
  1400. PRIV(jit_compile)(re, extra, JIT_PARTIAL_SOFT_COMPILE);
  1401. if ((options & PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE) != 0)
  1402. PRIV(jit_compile)(re, extra, JIT_PARTIAL_HARD_COMPILE);
  1403. if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0 &&
  1404. (options & PCRE_STUDY_EXTRA_NEEDED) == 0)
  1405. {
  1406. #if defined COMPILE_PCRE8
  1407. pcre_free_study(extra);
  1408. #elif defined COMPILE_PCRE16
  1409. pcre16_free_study(extra);
  1410. #elif defined COMPILE_PCRE32
  1411. pcre32_free_study(extra);
  1412. #endif
  1413. extra = NULL;
  1414. }
  1415. #endif
  1416. }
  1417. return extra;
  1418. }
  1419. /*************************************************
  1420. * Free the study data *
  1421. *************************************************/
  1422. /* This function frees the memory that was obtained by pcre_study().
  1423. Argument: a pointer to the pcre[16]_extra block
  1424. Returns: nothing
  1425. */
  1426. #if defined COMPILE_PCRE8
  1427. PCRE_EXP_DEFN void
  1428. pcre_free_study(pcre_extra *extra)
  1429. #elif defined COMPILE_PCRE16
  1430. PCRE_EXP_DEFN void
  1431. pcre16_free_study(pcre16_extra *extra)
  1432. #elif defined COMPILE_PCRE32
  1433. PCRE_EXP_DEFN void
  1434. pcre32_free_study(pcre32_extra *extra)
  1435. #endif
  1436. {
  1437. if (extra == NULL)
  1438. return;
  1439. #ifdef SUPPORT_JIT
  1440. if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
  1441. extra->executable_jit != NULL)
  1442. PRIV(jit_free)(extra->executable_jit);
  1443. #endif
  1444. PUBL(free)(extra);
  1445. }
  1446. /* End of pcre_study.c */