pcre_dfa_exec.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676
  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 (but see
  6. below for why this module is different).
  7. Written by Philip Hazel
  8. Copyright (c) 1997-2017 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains the external function pcre_dfa_exec(), which is an
  34. alternative matching function that uses a sort of DFA algorithm (not a true
  35. FSM). This is NOT Perl-compatible, but it has advantages in certain
  36. applications. */
  37. /* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved
  38. the performance of his patterns greatly. I could not use it as it stood, as it
  39. was not thread safe, and made assumptions about pattern sizes. Also, it caused
  40. test 7 to loop, and test 9 to crash with a segfault.
  41. The issue is the check for duplicate states, which is done by a simple linear
  42. search up the state list. (Grep for "duplicate" below to find the code.) For
  43. many patterns, there will never be many states active at one time, so a simple
  44. linear search is fine. In patterns that have many active states, it might be a
  45. bottleneck. The suggested code used an indexing scheme to remember which states
  46. had previously been used for each character, and avoided the linear search when
  47. it knew there was no chance of a duplicate. This was implemented when adding
  48. states to the state lists.
  49. I wrote some thread-safe, not-limited code to try something similar at the time
  50. of checking for duplicates (instead of when adding states), using index vectors
  51. on the stack. It did give a 13% improvement with one specially constructed
  52. pattern for certain subject strings, but on other strings and on many of the
  53. simpler patterns in the test suite it did worse. The major problem, I think,
  54. was the extra time to initialize the index. This had to be done for each call
  55. of internal_dfa_exec(). (The supplied patch used a static vector, initialized
  56. only once - I suspect this was the cause of the problems with the tests.)
  57. Overall, I concluded that the gains in some cases did not outweigh the losses
  58. in others, so I abandoned this code. */
  59. #ifdef HAVE_CONFIG_H
  60. #include "pcre_config.h"
  61. #endif
  62. #define NLBLOCK md /* Block containing newline information */
  63. #define PSSTART start_subject /* Field containing processed string start */
  64. #define PSEND end_subject /* Field containing processed string end */
  65. #include "pcre_internal.h"
  66. /* For use to indent debugging output */
  67. #define SP " "
  68. /*************************************************
  69. * Code parameters and static tables *
  70. *************************************************/
  71. /* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
  72. into others, under special conditions. A gap of 20 between the blocks should be
  73. enough. The resulting opcodes don't have to be less than 256 because they are
  74. never stored, so we push them well clear of the normal opcodes. */
  75. #define OP_PROP_EXTRA 300
  76. #define OP_EXTUNI_EXTRA 320
  77. #define OP_ANYNL_EXTRA 340
  78. #define OP_HSPACE_EXTRA 360
  79. #define OP_VSPACE_EXTRA 380
  80. /* This table identifies those opcodes that are followed immediately by a
  81. character that is to be tested in some way. This makes it possible to
  82. centralize the loading of these characters. In the case of Type * etc, the
  83. "character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
  84. small value. Non-zero values in the table are the offsets from the opcode where
  85. the character is to be found. ***NOTE*** If the start of this table is
  86. modified, the three tables that follow must also be modified. */
  87. static const pcre_uint8 coptable[] = {
  88. 0, /* End */
  89. 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */
  90. 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */
  91. 0, 0, 0, /* Any, AllAny, Anybyte */
  92. 0, 0, /* \P, \p */
  93. 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */
  94. 0, /* \X */
  95. 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
  96. 1, /* Char */
  97. 1, /* Chari */
  98. 1, /* not */
  99. 1, /* noti */
  100. /* Positive single-char repeats */
  101. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
  102. 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto, minupto */
  103. 1+IMM2_SIZE, /* exact */
  104. 1, 1, 1, 1+IMM2_SIZE, /* *+, ++, ?+, upto+ */
  105. 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
  106. 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto I, minupto I */
  107. 1+IMM2_SIZE, /* exact I */
  108. 1, 1, 1, 1+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */
  109. /* Negative single-char repeats - only for chars < 256 */
  110. 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
  111. 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto, minupto */
  112. 1+IMM2_SIZE, /* NOT exact */
  113. 1, 1, 1, 1+IMM2_SIZE, /* NOT *+, ++, ?+, upto+ */
  114. 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
  115. 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto I, minupto I */
  116. 1+IMM2_SIZE, /* NOT exact I */
  117. 1, 1, 1, 1+IMM2_SIZE, /* NOT *+I, ++I, ?+I, upto+I */
  118. /* Positive type repeats */
  119. 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
  120. 1+IMM2_SIZE, 1+IMM2_SIZE, /* Type upto, minupto */
  121. 1+IMM2_SIZE, /* Type exact */
  122. 1, 1, 1, 1+IMM2_SIZE, /* Type *+, ++, ?+, upto+ */
  123. /* Character class & ref repeats */
  124. 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */
  125. 0, 0, /* CRRANGE, CRMINRANGE */
  126. 0, 0, 0, 0, /* Possessive *+, ++, ?+, CRPOSRANGE */
  127. 0, /* CLASS */
  128. 0, /* NCLASS */
  129. 0, /* XCLASS - variable length */
  130. 0, /* REF */
  131. 0, /* REFI */
  132. 0, /* DNREF */
  133. 0, /* DNREFI */
  134. 0, /* RECURSE */
  135. 0, /* CALLOUT */
  136. 0, /* Alt */
  137. 0, /* Ket */
  138. 0, /* KetRmax */
  139. 0, /* KetRmin */
  140. 0, /* KetRpos */
  141. 0, /* Reverse */
  142. 0, /* Assert */
  143. 0, /* Assert not */
  144. 0, /* Assert behind */
  145. 0, /* Assert behind not */
  146. 0, 0, /* ONCE, ONCE_NC */
  147. 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
  148. 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
  149. 0, 0, /* CREF, DNCREF */
  150. 0, 0, /* RREF, DNRREF */
  151. 0, /* DEF */
  152. 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
  153. 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
  154. 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
  155. 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */
  156. 0, 0 /* CLOSE, SKIPZERO */
  157. };
  158. /* This table identifies those opcodes that inspect a character. It is used to
  159. remember the fact that a character could have been inspected when the end of
  160. the subject is reached. ***NOTE*** If the start of this table is modified, the
  161. two tables that follow must also be modified. */
  162. static const pcre_uint8 poptable[] = {
  163. 0, /* End */
  164. 0, 0, 0, 1, 1, /* \A, \G, \K, \B, \b */
  165. 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */
  166. 1, 1, 1, /* Any, AllAny, Anybyte */
  167. 1, 1, /* \P, \p */
  168. 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */
  169. 1, /* \X */
  170. 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
  171. 1, /* Char */
  172. 1, /* Chari */
  173. 1, /* not */
  174. 1, /* noti */
  175. /* Positive single-char repeats */
  176. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
  177. 1, 1, 1, /* upto, minupto, exact */
  178. 1, 1, 1, 1, /* *+, ++, ?+, upto+ */
  179. 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
  180. 1, 1, 1, /* upto I, minupto I, exact I */
  181. 1, 1, 1, 1, /* *+I, ++I, ?+I, upto+I */
  182. /* Negative single-char repeats - only for chars < 256 */
  183. 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
  184. 1, 1, 1, /* NOT upto, minupto, exact */
  185. 1, 1, 1, 1, /* NOT *+, ++, ?+, upto+ */
  186. 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
  187. 1, 1, 1, /* NOT upto I, minupto I, exact I */
  188. 1, 1, 1, 1, /* NOT *+I, ++I, ?+I, upto+I */
  189. /* Positive type repeats */
  190. 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
  191. 1, 1, 1, /* Type upto, minupto, exact */
  192. 1, 1, 1, 1, /* Type *+, ++, ?+, upto+ */
  193. /* Character class & ref repeats */
  194. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
  195. 1, 1, /* CRRANGE, CRMINRANGE */
  196. 1, 1, 1, 1, /* Possessive *+, ++, ?+, CRPOSRANGE */
  197. 1, /* CLASS */
  198. 1, /* NCLASS */
  199. 1, /* XCLASS - variable length */
  200. 0, /* REF */
  201. 0, /* REFI */
  202. 0, /* DNREF */
  203. 0, /* DNREFI */
  204. 0, /* RECURSE */
  205. 0, /* CALLOUT */
  206. 0, /* Alt */
  207. 0, /* Ket */
  208. 0, /* KetRmax */
  209. 0, /* KetRmin */
  210. 0, /* KetRpos */
  211. 0, /* Reverse */
  212. 0, /* Assert */
  213. 0, /* Assert not */
  214. 0, /* Assert behind */
  215. 0, /* Assert behind not */
  216. 0, 0, /* ONCE, ONCE_NC */
  217. 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
  218. 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
  219. 0, 0, /* CREF, DNCREF */
  220. 0, 0, /* RREF, DNRREF */
  221. 0, /* DEF */
  222. 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
  223. 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
  224. 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
  225. 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */
  226. 0, 0 /* CLOSE, SKIPZERO */
  227. };
  228. /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
  229. and \w */
  230. static const pcre_uint8 toptable1[] = {
  231. 0, 0, 0, 0, 0, 0,
  232. ctype_digit, ctype_digit,
  233. ctype_space, ctype_space,
  234. ctype_word, ctype_word,
  235. 0, 0 /* OP_ANY, OP_ALLANY */
  236. };
  237. static const pcre_uint8 toptable2[] = {
  238. 0, 0, 0, 0, 0, 0,
  239. ctype_digit, 0,
  240. ctype_space, 0,
  241. ctype_word, 0,
  242. 1, 1 /* OP_ANY, OP_ALLANY */
  243. };
  244. /* Structure for holding data about a particular state, which is in effect the
  245. current data for an active path through the match tree. It must consist
  246. entirely of ints because the working vector we are passed, and which we put
  247. these structures in, is a vector of ints. */
  248. typedef struct stateblock {
  249. int offset; /* Offset to opcode */
  250. int count; /* Count for repeats */
  251. int data; /* Some use extra data */
  252. } stateblock;
  253. #define INTS_PER_STATEBLOCK (int)(sizeof(stateblock)/sizeof(int))
  254. #ifdef PCRE_DEBUG
  255. /*************************************************
  256. * Print character string *
  257. *************************************************/
  258. /* Character string printing function for debugging.
  259. Arguments:
  260. p points to string
  261. length number of bytes
  262. f where to print
  263. Returns: nothing
  264. */
  265. static void
  266. pchars(const pcre_uchar *p, int length, FILE *f)
  267. {
  268. pcre_uint32 c;
  269. while (length-- > 0)
  270. {
  271. if (isprint(c = *(p++)))
  272. fprintf(f, "%c", c);
  273. else
  274. fprintf(f, "\\x{%02x}", c);
  275. }
  276. }
  277. #endif
  278. /*************************************************
  279. * Execute a Regular Expression - DFA engine *
  280. *************************************************/
  281. /* This internal function applies a compiled pattern to a subject string,
  282. starting at a given point, using a DFA engine. This function is called from the
  283. external one, possibly multiple times if the pattern is not anchored. The
  284. function calls itself recursively for some kinds of subpattern.
  285. Arguments:
  286. md the match_data block with fixed information
  287. this_start_code the opening bracket of this subexpression's code
  288. current_subject where we currently are in the subject string
  289. start_offset start offset in the subject string
  290. offsets vector to contain the matching string offsets
  291. offsetcount size of same
  292. workspace vector of workspace
  293. wscount size of same
  294. rlevel function call recursion level
  295. Returns: > 0 => number of match offset pairs placed in offsets
  296. = 0 => offsets overflowed; longest matches are present
  297. -1 => failed to match
  298. < -1 => some kind of unexpected problem
  299. The following macros are used for adding states to the two state vectors (one
  300. for the current character, one for the following character). */
  301. #define ADD_ACTIVE(x,y) \
  302. if (active_count++ < wscount) \
  303. { \
  304. next_active_state->offset = (x); \
  305. next_active_state->count = (y); \
  306. next_active_state++; \
  307. DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  308. } \
  309. else return PCRE_ERROR_DFA_WSSIZE
  310. #define ADD_ACTIVE_DATA(x,y,z) \
  311. if (active_count++ < wscount) \
  312. { \
  313. next_active_state->offset = (x); \
  314. next_active_state->count = (y); \
  315. next_active_state->data = (z); \
  316. next_active_state++; \
  317. DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
  318. } \
  319. else return PCRE_ERROR_DFA_WSSIZE
  320. #define ADD_NEW(x,y) \
  321. if (new_count++ < wscount) \
  322. { \
  323. next_new_state->offset = (x); \
  324. next_new_state->count = (y); \
  325. next_new_state++; \
  326. DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  327. } \
  328. else return PCRE_ERROR_DFA_WSSIZE
  329. #define ADD_NEW_DATA(x,y,z) \
  330. if (new_count++ < wscount) \
  331. { \
  332. next_new_state->offset = (x); \
  333. next_new_state->count = (y); \
  334. next_new_state->data = (z); \
  335. next_new_state++; \
  336. DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d) line %d\n", rlevel*2-2, SP, \
  337. (x), (y), (z), __LINE__)); \
  338. } \
  339. else return PCRE_ERROR_DFA_WSSIZE
  340. /* And now, here is the code */
  341. static int
  342. internal_dfa_exec(
  343. dfa_match_data *md,
  344. const pcre_uchar *this_start_code,
  345. const pcre_uchar *current_subject,
  346. int start_offset,
  347. int *offsets,
  348. int offsetcount,
  349. int *workspace,
  350. int wscount,
  351. int rlevel)
  352. {
  353. stateblock *active_states, *new_states, *temp_states;
  354. stateblock *next_active_state, *next_new_state;
  355. const pcre_uint8 *ctypes, *lcc, *fcc;
  356. const pcre_uchar *ptr;
  357. const pcre_uchar *end_code, *first_op;
  358. dfa_recursion_info new_recursive;
  359. int active_count, new_count, match_count;
  360. /* Some fields in the md block are frequently referenced, so we load them into
  361. independent variables in the hope that this will perform better. */
  362. const pcre_uchar *start_subject = md->start_subject;
  363. const pcre_uchar *end_subject = md->end_subject;
  364. const pcre_uchar *start_code = md->start_code;
  365. #ifdef SUPPORT_UTF
  366. BOOL utf = (md->poptions & PCRE_UTF8) != 0;
  367. #else
  368. BOOL utf = FALSE;
  369. #endif
  370. BOOL reset_could_continue = FALSE;
  371. rlevel++;
  372. offsetcount &= (-2);
  373. wscount -= 2;
  374. wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
  375. (2 * INTS_PER_STATEBLOCK);
  376. DPRINTF(("\n%.*s---------------------\n"
  377. "%.*sCall to internal_dfa_exec f=%d\n",
  378. rlevel*2-2, SP, rlevel*2-2, SP, rlevel));
  379. ctypes = md->tables + ctypes_offset;
  380. lcc = md->tables + lcc_offset;
  381. fcc = md->tables + fcc_offset;
  382. match_count = PCRE_ERROR_NOMATCH; /* A negative number */
  383. active_states = (stateblock *)(workspace + 2);
  384. next_new_state = new_states = active_states + wscount;
  385. new_count = 0;
  386. first_op = this_start_code + 1 + LINK_SIZE +
  387. ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
  388. *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
  389. ? IMM2_SIZE:0);
  390. /* The first thing in any (sub) pattern is a bracket of some sort. Push all
  391. the alternative states onto the list, and find out where the end is. This
  392. makes is possible to use this function recursively, when we want to stop at a
  393. matching internal ket rather than at the end.
  394. If the first opcode in the first alternative is OP_REVERSE, we are dealing with
  395. a backward assertion. In that case, we have to find out the maximum amount to
  396. move back, and set up each alternative appropriately. */
  397. if (*first_op == OP_REVERSE)
  398. {
  399. int max_back = 0;
  400. int gone_back;
  401. end_code = this_start_code;
  402. do
  403. {
  404. int back = GET(end_code, 2+LINK_SIZE);
  405. if (back > max_back) max_back = back;
  406. end_code += GET(end_code, 1);
  407. }
  408. while (*end_code == OP_ALT);
  409. /* If we can't go back the amount required for the longest lookbehind
  410. pattern, go back as far as we can; some alternatives may still be viable. */
  411. #ifdef SUPPORT_UTF
  412. /* In character mode we have to step back character by character */
  413. if (utf)
  414. {
  415. for (gone_back = 0; gone_back < max_back; gone_back++)
  416. {
  417. if (current_subject <= start_subject) break;
  418. current_subject--;
  419. ACROSSCHAR(current_subject > start_subject, *current_subject, current_subject--);
  420. }
  421. }
  422. else
  423. #endif
  424. /* In byte-mode we can do this quickly. */
  425. {
  426. gone_back = (current_subject - max_back < start_subject)?
  427. (int)(current_subject - start_subject) : max_back;
  428. current_subject -= gone_back;
  429. }
  430. /* Save the earliest consulted character */
  431. if (current_subject < md->start_used_ptr)
  432. md->start_used_ptr = current_subject;
  433. /* Now we can process the individual branches. */
  434. end_code = this_start_code;
  435. do
  436. {
  437. int back = GET(end_code, 2+LINK_SIZE);
  438. if (back <= gone_back)
  439. {
  440. int bstate = (int)(end_code - start_code + 2 + 2*LINK_SIZE);
  441. ADD_NEW_DATA(-bstate, 0, gone_back - back);
  442. }
  443. end_code += GET(end_code, 1);
  444. }
  445. while (*end_code == OP_ALT);
  446. }
  447. /* This is the code for a "normal" subpattern (not a backward assertion). The
  448. start of a whole pattern is always one of these. If we are at the top level,
  449. we may be asked to restart matching from the same point that we reached for a
  450. previous partial match. We still have to scan through the top-level branches to
  451. find the end state. */
  452. else
  453. {
  454. end_code = this_start_code;
  455. /* Restarting */
  456. if (rlevel == 1 && (md->moptions & PCRE_DFA_RESTART) != 0)
  457. {
  458. do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
  459. new_count = workspace[1];
  460. if (!workspace[0])
  461. memcpy(new_states, active_states, new_count * sizeof(stateblock));
  462. }
  463. /* Not restarting */
  464. else
  465. {
  466. int length = 1 + LINK_SIZE +
  467. ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
  468. *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
  469. ? IMM2_SIZE:0);
  470. do
  471. {
  472. ADD_NEW((int)(end_code - start_code + length), 0);
  473. end_code += GET(end_code, 1);
  474. length = 1 + LINK_SIZE;
  475. }
  476. while (*end_code == OP_ALT);
  477. }
  478. }
  479. workspace[0] = 0; /* Bit indicating which vector is current */
  480. DPRINTF(("%.*sEnd state = %d\n", rlevel*2-2, SP, (int)(end_code - start_code)));
  481. /* Loop for scanning the subject */
  482. ptr = current_subject;
  483. for (;;)
  484. {
  485. int i, j;
  486. int clen, dlen;
  487. pcre_uint32 c, d;
  488. int forced_fail = 0;
  489. BOOL partial_newline = FALSE;
  490. BOOL could_continue = reset_could_continue;
  491. reset_could_continue = FALSE;
  492. /* Make the new state list into the active state list and empty the
  493. new state list. */
  494. temp_states = active_states;
  495. active_states = new_states;
  496. new_states = temp_states;
  497. active_count = new_count;
  498. new_count = 0;
  499. workspace[0] ^= 1; /* Remember for the restarting feature */
  500. workspace[1] = active_count;
  501. #ifdef PCRE_DEBUG
  502. printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
  503. pchars(ptr, STRLEN_UC(ptr), stdout);
  504. printf("\"\n");
  505. printf("%.*sActive states: ", rlevel*2-2, SP);
  506. for (i = 0; i < active_count; i++)
  507. printf("%d/%d ", active_states[i].offset, active_states[i].count);
  508. printf("\n");
  509. #endif
  510. /* Set the pointers for adding new states */
  511. next_active_state = active_states + active_count;
  512. next_new_state = new_states;
  513. /* Load the current character from the subject outside the loop, as many
  514. different states may want to look at it, and we assume that at least one
  515. will. */
  516. if (ptr < end_subject)
  517. {
  518. clen = 1; /* Number of data items in the character */
  519. #ifdef SUPPORT_UTF
  520. GETCHARLENTEST(c, ptr, clen);
  521. #else
  522. c = *ptr;
  523. #endif /* SUPPORT_UTF */
  524. }
  525. else
  526. {
  527. clen = 0; /* This indicates the end of the subject */
  528. c = NOTACHAR; /* This value should never actually be used */
  529. }
  530. /* Scan up the active states and act on each one. The result of an action
  531. may be to add more states to the currently active list (e.g. on hitting a
  532. parenthesis) or it may be to put states on the new list, for considering
  533. when we move the character pointer on. */
  534. for (i = 0; i < active_count; i++)
  535. {
  536. stateblock *current_state = active_states + i;
  537. BOOL caseless = FALSE;
  538. const pcre_uchar *code;
  539. int state_offset = current_state->offset;
  540. int codevalue, rrc;
  541. int count;
  542. #ifdef PCRE_DEBUG
  543. printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
  544. if (clen == 0) printf("EOL\n");
  545. else if (c > 32 && c < 127) printf("'%c'\n", c);
  546. else printf("0x%02x\n", c);
  547. #endif
  548. /* A negative offset is a special case meaning "hold off going to this
  549. (negated) state until the number of characters in the data field have
  550. been skipped". If the could_continue flag was passed over from a previous
  551. state, arrange for it to passed on. */
  552. if (state_offset < 0)
  553. {
  554. if (current_state->data > 0)
  555. {
  556. DPRINTF(("%.*sSkipping this character\n", rlevel*2-2, SP));
  557. ADD_NEW_DATA(state_offset, current_state->count,
  558. current_state->data - 1);
  559. if (could_continue) reset_could_continue = TRUE;
  560. continue;
  561. }
  562. else
  563. {
  564. current_state->offset = state_offset = -state_offset;
  565. }
  566. }
  567. /* Check for a duplicate state with the same count, and skip if found.
  568. See the note at the head of this module about the possibility of improving
  569. performance here. */
  570. for (j = 0; j < i; j++)
  571. {
  572. if (active_states[j].offset == state_offset &&
  573. active_states[j].count == current_state->count)
  574. {
  575. DPRINTF(("%.*sDuplicate state: skipped\n", rlevel*2-2, SP));
  576. goto NEXT_ACTIVE_STATE;
  577. }
  578. }
  579. /* The state offset is the offset to the opcode */
  580. code = start_code + state_offset;
  581. codevalue = *code;
  582. /* If this opcode inspects a character, but we are at the end of the
  583. subject, remember the fact for use when testing for a partial match. */
  584. if (clen == 0 && poptable[codevalue] != 0)
  585. could_continue = TRUE;
  586. /* If this opcode is followed by an inline character, load it. It is
  587. tempting to test for the presence of a subject character here, but that
  588. is wrong, because sometimes zero repetitions of the subject are
  589. permitted.
  590. We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
  591. argument that is not a data character - but is always one byte long because
  592. the values are small. We have to take special action to deal with \P, \p,
  593. \H, \h, \V, \v and \X in this case. To keep the other cases fast, convert
  594. these ones to new opcodes. */
  595. if (coptable[codevalue] > 0)
  596. {
  597. dlen = 1;
  598. #ifdef SUPPORT_UTF
  599. if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
  600. #endif /* SUPPORT_UTF */
  601. d = code[coptable[codevalue]];
  602. if (codevalue >= OP_TYPESTAR)
  603. {
  604. switch(d)
  605. {
  606. case OP_ANYBYTE: return PCRE_ERROR_DFA_UITEM;
  607. case OP_NOTPROP:
  608. case OP_PROP: codevalue += OP_PROP_EXTRA; break;
  609. case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;
  610. case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;
  611. case OP_NOT_HSPACE:
  612. case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;
  613. case OP_NOT_VSPACE:
  614. case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;
  615. default: break;
  616. }
  617. }
  618. }
  619. else
  620. {
  621. dlen = 0; /* Not strictly necessary, but compilers moan */
  622. d = NOTACHAR; /* if these variables are not set. */
  623. }
  624. /* Now process the individual opcodes */
  625. switch (codevalue)
  626. {
  627. /* ========================================================================== */
  628. /* These cases are never obeyed. This is a fudge that causes a compile-
  629. time error if the vectors coptable or poptable, which are indexed by
  630. opcode, are not the correct length. It seems to be the only way to do
  631. such a check at compile time, as the sizeof() operator does not work
  632. in the C preprocessor. */
  633. case OP_TABLE_LENGTH:
  634. case OP_TABLE_LENGTH +
  635. ((sizeof(coptable) == OP_TABLE_LENGTH) &&
  636. (sizeof(poptable) == OP_TABLE_LENGTH)):
  637. break;
  638. /* ========================================================================== */
  639. /* Reached a closing bracket. If not at the end of the pattern, carry
  640. on with the next opcode. For repeating opcodes, also add the repeat
  641. state. Note that KETRPOS will always be encountered at the end of the
  642. subpattern, because the possessive subpattern repeats are always handled
  643. using recursive calls. Thus, it never adds any new states.
  644. At the end of the (sub)pattern, unless we have an empty string and
  645. PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the
  646. start of the subject, save the match data, shifting up all previous
  647. matches so we always have the longest first. */
  648. case OP_KET:
  649. case OP_KETRMIN:
  650. case OP_KETRMAX:
  651. case OP_KETRPOS:
  652. if (code != end_code)
  653. {
  654. ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
  655. if (codevalue != OP_KET)
  656. {
  657. ADD_ACTIVE(state_offset - GET(code, 1), 0);
  658. }
  659. }
  660. else
  661. {
  662. if (ptr > current_subject ||
  663. ((md->moptions & PCRE_NOTEMPTY) == 0 &&
  664. ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
  665. current_subject > start_subject + md->start_offset)))
  666. {
  667. if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
  668. else if (match_count > 0 && ++match_count * 2 > offsetcount)
  669. match_count = 0;
  670. count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
  671. if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
  672. if (offsetcount >= 2)
  673. {
  674. offsets[0] = (int)(current_subject - start_subject);
  675. offsets[1] = (int)(ptr - start_subject);
  676. DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
  677. offsets[1] - offsets[0], (char *)current_subject));
  678. }
  679. if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
  680. {
  681. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  682. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
  683. match_count, rlevel*2-2, SP));
  684. return match_count;
  685. }
  686. }
  687. }
  688. break;
  689. /* ========================================================================== */
  690. /* These opcodes add to the current list of states without looking
  691. at the current character. */
  692. /*-----------------------------------------------------------------*/
  693. case OP_ALT:
  694. do { code += GET(code, 1); } while (*code == OP_ALT);
  695. ADD_ACTIVE((int)(code - start_code), 0);
  696. break;
  697. /*-----------------------------------------------------------------*/
  698. case OP_BRA:
  699. case OP_SBRA:
  700. do
  701. {
  702. ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
  703. code += GET(code, 1);
  704. }
  705. while (*code == OP_ALT);
  706. break;
  707. /*-----------------------------------------------------------------*/
  708. case OP_CBRA:
  709. case OP_SCBRA:
  710. ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE), 0);
  711. code += GET(code, 1);
  712. while (*code == OP_ALT)
  713. {
  714. ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
  715. code += GET(code, 1);
  716. }
  717. break;
  718. /*-----------------------------------------------------------------*/
  719. case OP_BRAZERO:
  720. case OP_BRAMINZERO:
  721. ADD_ACTIVE(state_offset + 1, 0);
  722. code += 1 + GET(code, 2);
  723. while (*code == OP_ALT) code += GET(code, 1);
  724. ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
  725. break;
  726. /*-----------------------------------------------------------------*/
  727. case OP_SKIPZERO:
  728. code += 1 + GET(code, 2);
  729. while (*code == OP_ALT) code += GET(code, 1);
  730. ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
  731. break;
  732. /*-----------------------------------------------------------------*/
  733. case OP_CIRC:
  734. if (ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0)
  735. { ADD_ACTIVE(state_offset + 1, 0); }
  736. break;
  737. /*-----------------------------------------------------------------*/
  738. case OP_CIRCM:
  739. if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
  740. (ptr != end_subject && WAS_NEWLINE(ptr)))
  741. { ADD_ACTIVE(state_offset + 1, 0); }
  742. break;
  743. /*-----------------------------------------------------------------*/
  744. case OP_EOD:
  745. if (ptr >= end_subject)
  746. {
  747. if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
  748. could_continue = TRUE;
  749. else { ADD_ACTIVE(state_offset + 1, 0); }
  750. }
  751. break;
  752. /*-----------------------------------------------------------------*/
  753. case OP_SOD:
  754. if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
  755. break;
  756. /*-----------------------------------------------------------------*/
  757. case OP_SOM:
  758. if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
  759. break;
  760. /* ========================================================================== */
  761. /* These opcodes inspect the next subject character, and sometimes
  762. the previous one as well, but do not have an argument. The variable
  763. clen contains the length of the current character and is zero if we are
  764. at the end of the subject. */
  765. /*-----------------------------------------------------------------*/
  766. case OP_ANY:
  767. if (clen > 0 && !IS_NEWLINE(ptr))
  768. {
  769. if (ptr + 1 >= md->end_subject &&
  770. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  771. NLBLOCK->nltype == NLTYPE_FIXED &&
  772. NLBLOCK->nllen == 2 &&
  773. c == NLBLOCK->nl[0])
  774. {
  775. could_continue = partial_newline = TRUE;
  776. }
  777. else
  778. {
  779. ADD_NEW(state_offset + 1, 0);
  780. }
  781. }
  782. break;
  783. /*-----------------------------------------------------------------*/
  784. case OP_ALLANY:
  785. if (clen > 0)
  786. { ADD_NEW(state_offset + 1, 0); }
  787. break;
  788. /*-----------------------------------------------------------------*/
  789. case OP_EODN:
  790. if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
  791. could_continue = TRUE;
  792. else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))
  793. { ADD_ACTIVE(state_offset + 1, 0); }
  794. break;
  795. /*-----------------------------------------------------------------*/
  796. case OP_DOLL:
  797. if ((md->moptions & PCRE_NOTEOL) == 0)
  798. {
  799. if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
  800. could_continue = TRUE;
  801. else if (clen == 0 ||
  802. ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
  803. (ptr == end_subject - md->nllen)
  804. ))
  805. { ADD_ACTIVE(state_offset + 1, 0); }
  806. else if (ptr + 1 >= md->end_subject &&
  807. (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 &&
  808. NLBLOCK->nltype == NLTYPE_FIXED &&
  809. NLBLOCK->nllen == 2 &&
  810. c == NLBLOCK->nl[0])
  811. {
  812. if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
  813. {
  814. reset_could_continue = TRUE;
  815. ADD_NEW_DATA(-(state_offset + 1), 0, 1);
  816. }
  817. else could_continue = partial_newline = TRUE;
  818. }
  819. }
  820. break;
  821. /*-----------------------------------------------------------------*/
  822. case OP_DOLLM:
  823. if ((md->moptions & PCRE_NOTEOL) == 0)
  824. {
  825. if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
  826. could_continue = TRUE;
  827. else if (clen == 0 ||
  828. ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
  829. { ADD_ACTIVE(state_offset + 1, 0); }
  830. else if (ptr + 1 >= md->end_subject &&
  831. (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 &&
  832. NLBLOCK->nltype == NLTYPE_FIXED &&
  833. NLBLOCK->nllen == 2 &&
  834. c == NLBLOCK->nl[0])
  835. {
  836. if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
  837. {
  838. reset_could_continue = TRUE;
  839. ADD_NEW_DATA(-(state_offset + 1), 0, 1);
  840. }
  841. else could_continue = partial_newline = TRUE;
  842. }
  843. }
  844. else if (IS_NEWLINE(ptr))
  845. { ADD_ACTIVE(state_offset + 1, 0); }
  846. break;
  847. /*-----------------------------------------------------------------*/
  848. case OP_DIGIT:
  849. case OP_WHITESPACE:
  850. case OP_WORDCHAR:
  851. if (clen > 0 && c < 256 &&
  852. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
  853. { ADD_NEW(state_offset + 1, 0); }
  854. break;
  855. /*-----------------------------------------------------------------*/
  856. case OP_NOT_DIGIT:
  857. case OP_NOT_WHITESPACE:
  858. case OP_NOT_WORDCHAR:
  859. if (clen > 0 && (c >= 256 ||
  860. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
  861. { ADD_NEW(state_offset + 1, 0); }
  862. break;
  863. /*-----------------------------------------------------------------*/
  864. case OP_WORD_BOUNDARY:
  865. case OP_NOT_WORD_BOUNDARY:
  866. {
  867. int left_word, right_word;
  868. if (ptr > start_subject)
  869. {
  870. const pcre_uchar *temp = ptr - 1;
  871. if (temp < md->start_used_ptr) md->start_used_ptr = temp;
  872. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  873. if (utf) { BACKCHAR(temp); }
  874. #endif
  875. GETCHARTEST(d, temp);
  876. #ifdef SUPPORT_UCP
  877. if ((md->poptions & PCRE_UCP) != 0)
  878. {
  879. if (d == '_') left_word = TRUE; else
  880. {
  881. int cat = UCD_CATEGORY(d);
  882. left_word = (cat == ucp_L || cat == ucp_N);
  883. }
  884. }
  885. else
  886. #endif
  887. left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
  888. }
  889. else left_word = FALSE;
  890. if (clen > 0)
  891. {
  892. #ifdef SUPPORT_UCP
  893. if ((md->poptions & PCRE_UCP) != 0)
  894. {
  895. if (c == '_') right_word = TRUE; else
  896. {
  897. int cat = UCD_CATEGORY(c);
  898. right_word = (cat == ucp_L || cat == ucp_N);
  899. }
  900. }
  901. else
  902. #endif
  903. right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
  904. }
  905. else right_word = FALSE;
  906. if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
  907. { ADD_ACTIVE(state_offset + 1, 0); }
  908. }
  909. break;
  910. /*-----------------------------------------------------------------*/
  911. /* Check the next character by Unicode property. We will get here only
  912. if the support is in the binary; otherwise a compile-time error occurs.
  913. */
  914. #ifdef SUPPORT_UCP
  915. case OP_PROP:
  916. case OP_NOTPROP:
  917. if (clen > 0)
  918. {
  919. BOOL OK;
  920. const pcre_uint32 *cp;
  921. const ucd_record * prop = GET_UCD(c);
  922. switch(code[1])
  923. {
  924. case PT_ANY:
  925. OK = TRUE;
  926. break;
  927. case PT_LAMP:
  928. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  929. prop->chartype == ucp_Lt;
  930. break;
  931. case PT_GC:
  932. OK = PRIV(ucp_gentype)[prop->chartype] == code[2];
  933. break;
  934. case PT_PC:
  935. OK = prop->chartype == code[2];
  936. break;
  937. case PT_SC:
  938. OK = prop->script == code[2];
  939. break;
  940. /* These are specials for combination cases. */
  941. case PT_ALNUM:
  942. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  943. PRIV(ucp_gentype)[prop->chartype] == ucp_N;
  944. break;
  945. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  946. which means that Perl space and POSIX space are now identical. PCRE
  947. was changed at release 8.34. */
  948. case PT_SPACE: /* Perl space */
  949. case PT_PXSPACE: /* POSIX space */
  950. switch(c)
  951. {
  952. HSPACE_CASES:
  953. VSPACE_CASES:
  954. OK = TRUE;
  955. break;
  956. default:
  957. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
  958. break;
  959. }
  960. break;
  961. case PT_WORD:
  962. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  963. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  964. c == CHAR_UNDERSCORE;
  965. break;
  966. case PT_CLIST:
  967. cp = PRIV(ucd_caseless_sets) + code[2];
  968. for (;;)
  969. {
  970. if (c < *cp) { OK = FALSE; break; }
  971. if (c == *cp++) { OK = TRUE; break; }
  972. }
  973. break;
  974. case PT_UCNC:
  975. OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  976. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  977. c >= 0xe000;
  978. break;
  979. /* Should never occur, but keep compilers from grumbling. */
  980. default:
  981. OK = codevalue != OP_PROP;
  982. break;
  983. }
  984. if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }
  985. }
  986. break;
  987. #endif
  988. /* ========================================================================== */
  989. /* These opcodes likewise inspect the subject character, but have an
  990. argument that is not a data character. It is one of these opcodes:
  991. OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
  992. OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
  993. case OP_TYPEPLUS:
  994. case OP_TYPEMINPLUS:
  995. case OP_TYPEPOSPLUS:
  996. count = current_state->count; /* Already matched */
  997. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  998. if (clen > 0)
  999. {
  1000. if (d == OP_ANY && ptr + 1 >= md->end_subject &&
  1001. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  1002. NLBLOCK->nltype == NLTYPE_FIXED &&
  1003. NLBLOCK->nllen == 2 &&
  1004. c == NLBLOCK->nl[0])
  1005. {
  1006. could_continue = partial_newline = TRUE;
  1007. }
  1008. else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  1009. (c < 256 &&
  1010. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  1011. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  1012. {
  1013. if (count > 0 && codevalue == OP_TYPEPOSPLUS)
  1014. {
  1015. active_count--; /* Remove non-match possibility */
  1016. next_active_state--;
  1017. }
  1018. count++;
  1019. ADD_NEW(state_offset, count);
  1020. }
  1021. }
  1022. break;
  1023. /*-----------------------------------------------------------------*/
  1024. case OP_TYPEQUERY:
  1025. case OP_TYPEMINQUERY:
  1026. case OP_TYPEPOSQUERY:
  1027. ADD_ACTIVE(state_offset + 2, 0);
  1028. if (clen > 0)
  1029. {
  1030. if (d == OP_ANY && ptr + 1 >= md->end_subject &&
  1031. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  1032. NLBLOCK->nltype == NLTYPE_FIXED &&
  1033. NLBLOCK->nllen == 2 &&
  1034. c == NLBLOCK->nl[0])
  1035. {
  1036. could_continue = partial_newline = TRUE;
  1037. }
  1038. else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  1039. (c < 256 &&
  1040. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  1041. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  1042. {
  1043. if (codevalue == OP_TYPEPOSQUERY)
  1044. {
  1045. active_count--; /* Remove non-match possibility */
  1046. next_active_state--;
  1047. }
  1048. ADD_NEW(state_offset + 2, 0);
  1049. }
  1050. }
  1051. break;
  1052. /*-----------------------------------------------------------------*/
  1053. case OP_TYPESTAR:
  1054. case OP_TYPEMINSTAR:
  1055. case OP_TYPEPOSSTAR:
  1056. ADD_ACTIVE(state_offset + 2, 0);
  1057. if (clen > 0)
  1058. {
  1059. if (d == OP_ANY && ptr + 1 >= md->end_subject &&
  1060. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  1061. NLBLOCK->nltype == NLTYPE_FIXED &&
  1062. NLBLOCK->nllen == 2 &&
  1063. c == NLBLOCK->nl[0])
  1064. {
  1065. could_continue = partial_newline = TRUE;
  1066. }
  1067. else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  1068. (c < 256 &&
  1069. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  1070. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  1071. {
  1072. if (codevalue == OP_TYPEPOSSTAR)
  1073. {
  1074. active_count--; /* Remove non-match possibility */
  1075. next_active_state--;
  1076. }
  1077. ADD_NEW(state_offset, 0);
  1078. }
  1079. }
  1080. break;
  1081. /*-----------------------------------------------------------------*/
  1082. case OP_TYPEEXACT:
  1083. count = current_state->count; /* Number already matched */
  1084. if (clen > 0)
  1085. {
  1086. if (d == OP_ANY && ptr + 1 >= md->end_subject &&
  1087. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  1088. NLBLOCK->nltype == NLTYPE_FIXED &&
  1089. NLBLOCK->nllen == 2 &&
  1090. c == NLBLOCK->nl[0])
  1091. {
  1092. could_continue = partial_newline = TRUE;
  1093. }
  1094. else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  1095. (c < 256 &&
  1096. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  1097. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  1098. {
  1099. if (++count >= (int)GET2(code, 1))
  1100. { ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); }
  1101. else
  1102. { ADD_NEW(state_offset, count); }
  1103. }
  1104. }
  1105. break;
  1106. /*-----------------------------------------------------------------*/
  1107. case OP_TYPEUPTO:
  1108. case OP_TYPEMINUPTO:
  1109. case OP_TYPEPOSUPTO:
  1110. ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0);
  1111. count = current_state->count; /* Number already matched */
  1112. if (clen > 0)
  1113. {
  1114. if (d == OP_ANY && ptr + 1 >= md->end_subject &&
  1115. (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
  1116. NLBLOCK->nltype == NLTYPE_FIXED &&
  1117. NLBLOCK->nllen == 2 &&
  1118. c == NLBLOCK->nl[0])
  1119. {
  1120. could_continue = partial_newline = TRUE;
  1121. }
  1122. else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  1123. (c < 256 &&
  1124. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  1125. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  1126. {
  1127. if (codevalue == OP_TYPEPOSUPTO)
  1128. {
  1129. active_count--; /* Remove non-match possibility */
  1130. next_active_state--;
  1131. }
  1132. if (++count >= (int)GET2(code, 1))
  1133. { ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); }
  1134. else
  1135. { ADD_NEW(state_offset, count); }
  1136. }
  1137. }
  1138. break;
  1139. /* ========================================================================== */
  1140. /* These are virtual opcodes that are used when something like
  1141. OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its
  1142. argument. It keeps the code above fast for the other cases. The argument
  1143. is in the d variable. */
  1144. #ifdef SUPPORT_UCP
  1145. case OP_PROP_EXTRA + OP_TYPEPLUS:
  1146. case OP_PROP_EXTRA + OP_TYPEMINPLUS:
  1147. case OP_PROP_EXTRA + OP_TYPEPOSPLUS:
  1148. count = current_state->count; /* Already matched */
  1149. if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }
  1150. if (clen > 0)
  1151. {
  1152. BOOL OK;
  1153. const pcre_uint32 *cp;
  1154. const ucd_record * prop = GET_UCD(c);
  1155. switch(code[2])
  1156. {
  1157. case PT_ANY:
  1158. OK = TRUE;
  1159. break;
  1160. case PT_LAMP:
  1161. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  1162. prop->chartype == ucp_Lt;
  1163. break;
  1164. case PT_GC:
  1165. OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
  1166. break;
  1167. case PT_PC:
  1168. OK = prop->chartype == code[3];
  1169. break;
  1170. case PT_SC:
  1171. OK = prop->script == code[3];
  1172. break;
  1173. /* These are specials for combination cases. */
  1174. case PT_ALNUM:
  1175. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1176. PRIV(ucp_gentype)[prop->chartype] == ucp_N;
  1177. break;
  1178. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  1179. which means that Perl space and POSIX space are now identical. PCRE
  1180. was changed at release 8.34. */
  1181. case PT_SPACE: /* Perl space */
  1182. case PT_PXSPACE: /* POSIX space */
  1183. switch(c)
  1184. {
  1185. HSPACE_CASES:
  1186. VSPACE_CASES:
  1187. OK = TRUE;
  1188. break;
  1189. default:
  1190. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
  1191. break;
  1192. }
  1193. break;
  1194. case PT_WORD:
  1195. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1196. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  1197. c == CHAR_UNDERSCORE;
  1198. break;
  1199. case PT_CLIST:
  1200. cp = PRIV(ucd_caseless_sets) + code[3];
  1201. for (;;)
  1202. {
  1203. if (c < *cp) { OK = FALSE; break; }
  1204. if (c == *cp++) { OK = TRUE; break; }
  1205. }
  1206. break;
  1207. case PT_UCNC:
  1208. OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  1209. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  1210. c >= 0xe000;
  1211. break;
  1212. /* Should never occur, but keep compilers from grumbling. */
  1213. default:
  1214. OK = codevalue != OP_PROP;
  1215. break;
  1216. }
  1217. if (OK == (d == OP_PROP))
  1218. {
  1219. if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)
  1220. {
  1221. active_count--; /* Remove non-match possibility */
  1222. next_active_state--;
  1223. }
  1224. count++;
  1225. ADD_NEW(state_offset, count);
  1226. }
  1227. }
  1228. break;
  1229. /*-----------------------------------------------------------------*/
  1230. case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
  1231. case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
  1232. case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
  1233. count = current_state->count; /* Already matched */
  1234. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  1235. if (clen > 0)
  1236. {
  1237. int lgb, rgb;
  1238. const pcre_uchar *nptr = ptr + clen;
  1239. int ncount = 0;
  1240. if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)
  1241. {
  1242. active_count--; /* Remove non-match possibility */
  1243. next_active_state--;
  1244. }
  1245. lgb = UCD_GRAPHBREAK(c);
  1246. while (nptr < end_subject)
  1247. {
  1248. dlen = 1;
  1249. if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
  1250. rgb = UCD_GRAPHBREAK(d);
  1251. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  1252. ncount++;
  1253. lgb = rgb;
  1254. nptr += dlen;
  1255. }
  1256. count++;
  1257. ADD_NEW_DATA(-state_offset, count, ncount);
  1258. }
  1259. break;
  1260. #endif
  1261. /*-----------------------------------------------------------------*/
  1262. case OP_ANYNL_EXTRA + OP_TYPEPLUS:
  1263. case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:
  1264. case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:
  1265. count = current_state->count; /* Already matched */
  1266. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  1267. if (clen > 0)
  1268. {
  1269. int ncount = 0;
  1270. switch (c)
  1271. {
  1272. case CHAR_VT:
  1273. case CHAR_FF:
  1274. case CHAR_NEL:
  1275. #ifndef EBCDIC
  1276. case 0x2028:
  1277. case 0x2029:
  1278. #endif /* Not EBCDIC */
  1279. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1280. goto ANYNL01;
  1281. case CHAR_CR:
  1282. if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
  1283. /* Fall through */
  1284. ANYNL01:
  1285. case CHAR_LF:
  1286. if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
  1287. {
  1288. active_count--; /* Remove non-match possibility */
  1289. next_active_state--;
  1290. }
  1291. count++;
  1292. ADD_NEW_DATA(-state_offset, count, ncount);
  1293. break;
  1294. default:
  1295. break;
  1296. }
  1297. }
  1298. break;
  1299. /*-----------------------------------------------------------------*/
  1300. case OP_VSPACE_EXTRA + OP_TYPEPLUS:
  1301. case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:
  1302. case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:
  1303. count = current_state->count; /* Already matched */
  1304. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  1305. if (clen > 0)
  1306. {
  1307. BOOL OK;
  1308. switch (c)
  1309. {
  1310. VSPACE_CASES:
  1311. OK = TRUE;
  1312. break;
  1313. default:
  1314. OK = FALSE;
  1315. break;
  1316. }
  1317. if (OK == (d == OP_VSPACE))
  1318. {
  1319. if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)
  1320. {
  1321. active_count--; /* Remove non-match possibility */
  1322. next_active_state--;
  1323. }
  1324. count++;
  1325. ADD_NEW_DATA(-state_offset, count, 0);
  1326. }
  1327. }
  1328. break;
  1329. /*-----------------------------------------------------------------*/
  1330. case OP_HSPACE_EXTRA + OP_TYPEPLUS:
  1331. case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:
  1332. case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:
  1333. count = current_state->count; /* Already matched */
  1334. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  1335. if (clen > 0)
  1336. {
  1337. BOOL OK;
  1338. switch (c)
  1339. {
  1340. HSPACE_CASES:
  1341. OK = TRUE;
  1342. break;
  1343. default:
  1344. OK = FALSE;
  1345. break;
  1346. }
  1347. if (OK == (d == OP_HSPACE))
  1348. {
  1349. if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)
  1350. {
  1351. active_count--; /* Remove non-match possibility */
  1352. next_active_state--;
  1353. }
  1354. count++;
  1355. ADD_NEW_DATA(-state_offset, count, 0);
  1356. }
  1357. }
  1358. break;
  1359. /*-----------------------------------------------------------------*/
  1360. #ifdef SUPPORT_UCP
  1361. case OP_PROP_EXTRA + OP_TYPEQUERY:
  1362. case OP_PROP_EXTRA + OP_TYPEMINQUERY:
  1363. case OP_PROP_EXTRA + OP_TYPEPOSQUERY:
  1364. count = 4;
  1365. goto QS1;
  1366. case OP_PROP_EXTRA + OP_TYPESTAR:
  1367. case OP_PROP_EXTRA + OP_TYPEMINSTAR:
  1368. case OP_PROP_EXTRA + OP_TYPEPOSSTAR:
  1369. count = 0;
  1370. QS1:
  1371. ADD_ACTIVE(state_offset + 4, 0);
  1372. if (clen > 0)
  1373. {
  1374. BOOL OK;
  1375. const pcre_uint32 *cp;
  1376. const ucd_record * prop = GET_UCD(c);
  1377. switch(code[2])
  1378. {
  1379. case PT_ANY:
  1380. OK = TRUE;
  1381. break;
  1382. case PT_LAMP:
  1383. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  1384. prop->chartype == ucp_Lt;
  1385. break;
  1386. case PT_GC:
  1387. OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
  1388. break;
  1389. case PT_PC:
  1390. OK = prop->chartype == code[3];
  1391. break;
  1392. case PT_SC:
  1393. OK = prop->script == code[3];
  1394. break;
  1395. /* These are specials for combination cases. */
  1396. case PT_ALNUM:
  1397. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1398. PRIV(ucp_gentype)[prop->chartype] == ucp_N;
  1399. break;
  1400. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  1401. which means that Perl space and POSIX space are now identical. PCRE
  1402. was changed at release 8.34. */
  1403. case PT_SPACE: /* Perl space */
  1404. case PT_PXSPACE: /* POSIX space */
  1405. switch(c)
  1406. {
  1407. HSPACE_CASES:
  1408. VSPACE_CASES:
  1409. OK = TRUE;
  1410. break;
  1411. default:
  1412. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
  1413. break;
  1414. }
  1415. break;
  1416. case PT_WORD:
  1417. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1418. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  1419. c == CHAR_UNDERSCORE;
  1420. break;
  1421. case PT_CLIST:
  1422. cp = PRIV(ucd_caseless_sets) + code[3];
  1423. for (;;)
  1424. {
  1425. if (c < *cp) { OK = FALSE; break; }
  1426. if (c == *cp++) { OK = TRUE; break; }
  1427. }
  1428. break;
  1429. case PT_UCNC:
  1430. OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  1431. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  1432. c >= 0xe000;
  1433. break;
  1434. /* Should never occur, but keep compilers from grumbling. */
  1435. default:
  1436. OK = codevalue != OP_PROP;
  1437. break;
  1438. }
  1439. if (OK == (d == OP_PROP))
  1440. {
  1441. if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||
  1442. codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)
  1443. {
  1444. active_count--; /* Remove non-match possibility */
  1445. next_active_state--;
  1446. }
  1447. ADD_NEW(state_offset + count, 0);
  1448. }
  1449. }
  1450. break;
  1451. /*-----------------------------------------------------------------*/
  1452. case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
  1453. case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
  1454. case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:
  1455. count = 2;
  1456. goto QS2;
  1457. case OP_EXTUNI_EXTRA + OP_TYPESTAR:
  1458. case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
  1459. case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:
  1460. count = 0;
  1461. QS2:
  1462. ADD_ACTIVE(state_offset + 2, 0);
  1463. if (clen > 0)
  1464. {
  1465. int lgb, rgb;
  1466. const pcre_uchar *nptr = ptr + clen;
  1467. int ncount = 0;
  1468. if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||
  1469. codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)
  1470. {
  1471. active_count--; /* Remove non-match possibility */
  1472. next_active_state--;
  1473. }
  1474. lgb = UCD_GRAPHBREAK(c);
  1475. while (nptr < end_subject)
  1476. {
  1477. dlen = 1;
  1478. if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
  1479. rgb = UCD_GRAPHBREAK(d);
  1480. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  1481. ncount++;
  1482. lgb = rgb;
  1483. nptr += dlen;
  1484. }
  1485. ADD_NEW_DATA(-(state_offset + count), 0, ncount);
  1486. }
  1487. break;
  1488. #endif
  1489. /*-----------------------------------------------------------------*/
  1490. case OP_ANYNL_EXTRA + OP_TYPEQUERY:
  1491. case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:
  1492. case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:
  1493. count = 2;
  1494. goto QS3;
  1495. case OP_ANYNL_EXTRA + OP_TYPESTAR:
  1496. case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:
  1497. case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:
  1498. count = 0;
  1499. QS3:
  1500. ADD_ACTIVE(state_offset + 2, 0);
  1501. if (clen > 0)
  1502. {
  1503. int ncount = 0;
  1504. switch (c)
  1505. {
  1506. case CHAR_VT:
  1507. case CHAR_FF:
  1508. case CHAR_NEL:
  1509. #ifndef EBCDIC
  1510. case 0x2028:
  1511. case 0x2029:
  1512. #endif /* Not EBCDIC */
  1513. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1514. goto ANYNL02;
  1515. case CHAR_CR:
  1516. if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
  1517. /* Fall through */
  1518. ANYNL02:
  1519. case CHAR_LF:
  1520. if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
  1521. codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
  1522. {
  1523. active_count--; /* Remove non-match possibility */
  1524. next_active_state--;
  1525. }
  1526. ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount);
  1527. break;
  1528. default:
  1529. break;
  1530. }
  1531. }
  1532. break;
  1533. /*-----------------------------------------------------------------*/
  1534. case OP_VSPACE_EXTRA + OP_TYPEQUERY:
  1535. case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:
  1536. case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:
  1537. count = 2;
  1538. goto QS4;
  1539. case OP_VSPACE_EXTRA + OP_TYPESTAR:
  1540. case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:
  1541. case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:
  1542. count = 0;
  1543. QS4:
  1544. ADD_ACTIVE(state_offset + 2, 0);
  1545. if (clen > 0)
  1546. {
  1547. BOOL OK;
  1548. switch (c)
  1549. {
  1550. VSPACE_CASES:
  1551. OK = TRUE;
  1552. break;
  1553. default:
  1554. OK = FALSE;
  1555. break;
  1556. }
  1557. if (OK == (d == OP_VSPACE))
  1558. {
  1559. if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||
  1560. codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)
  1561. {
  1562. active_count--; /* Remove non-match possibility */
  1563. next_active_state--;
  1564. }
  1565. ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
  1566. }
  1567. }
  1568. break;
  1569. /*-----------------------------------------------------------------*/
  1570. case OP_HSPACE_EXTRA + OP_TYPEQUERY:
  1571. case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:
  1572. case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:
  1573. count = 2;
  1574. goto QS5;
  1575. case OP_HSPACE_EXTRA + OP_TYPESTAR:
  1576. case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:
  1577. case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:
  1578. count = 0;
  1579. QS5:
  1580. ADD_ACTIVE(state_offset + 2, 0);
  1581. if (clen > 0)
  1582. {
  1583. BOOL OK;
  1584. switch (c)
  1585. {
  1586. HSPACE_CASES:
  1587. OK = TRUE;
  1588. break;
  1589. default:
  1590. OK = FALSE;
  1591. break;
  1592. }
  1593. if (OK == (d == OP_HSPACE))
  1594. {
  1595. if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||
  1596. codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)
  1597. {
  1598. active_count--; /* Remove non-match possibility */
  1599. next_active_state--;
  1600. }
  1601. ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
  1602. }
  1603. }
  1604. break;
  1605. /*-----------------------------------------------------------------*/
  1606. #ifdef SUPPORT_UCP
  1607. case OP_PROP_EXTRA + OP_TYPEEXACT:
  1608. case OP_PROP_EXTRA + OP_TYPEUPTO:
  1609. case OP_PROP_EXTRA + OP_TYPEMINUPTO:
  1610. case OP_PROP_EXTRA + OP_TYPEPOSUPTO:
  1611. if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
  1612. { ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); }
  1613. count = current_state->count; /* Number already matched */
  1614. if (clen > 0)
  1615. {
  1616. BOOL OK;
  1617. const pcre_uint32 *cp;
  1618. const ucd_record * prop = GET_UCD(c);
  1619. switch(code[1 + IMM2_SIZE + 1])
  1620. {
  1621. case PT_ANY:
  1622. OK = TRUE;
  1623. break;
  1624. case PT_LAMP:
  1625. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
  1626. prop->chartype == ucp_Lt;
  1627. break;
  1628. case PT_GC:
  1629. OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2];
  1630. break;
  1631. case PT_PC:
  1632. OK = prop->chartype == code[1 + IMM2_SIZE + 2];
  1633. break;
  1634. case PT_SC:
  1635. OK = prop->script == code[1 + IMM2_SIZE + 2];
  1636. break;
  1637. /* These are specials for combination cases. */
  1638. case PT_ALNUM:
  1639. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1640. PRIV(ucp_gentype)[prop->chartype] == ucp_N;
  1641. break;
  1642. /* Perl space used to exclude VT, but from Perl 5.18 it is included,
  1643. which means that Perl space and POSIX space are now identical. PCRE
  1644. was changed at release 8.34. */
  1645. case PT_SPACE: /* Perl space */
  1646. case PT_PXSPACE: /* POSIX space */
  1647. switch(c)
  1648. {
  1649. HSPACE_CASES:
  1650. VSPACE_CASES:
  1651. OK = TRUE;
  1652. break;
  1653. default:
  1654. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
  1655. break;
  1656. }
  1657. break;
  1658. case PT_WORD:
  1659. OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
  1660. PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
  1661. c == CHAR_UNDERSCORE;
  1662. break;
  1663. case PT_CLIST:
  1664. cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2];
  1665. for (;;)
  1666. {
  1667. if (c < *cp) { OK = FALSE; break; }
  1668. if (c == *cp++) { OK = TRUE; break; }
  1669. }
  1670. break;
  1671. case PT_UCNC:
  1672. OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
  1673. c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
  1674. c >= 0xe000;
  1675. break;
  1676. /* Should never occur, but keep compilers from grumbling. */
  1677. default:
  1678. OK = codevalue != OP_PROP;
  1679. break;
  1680. }
  1681. if (OK == (d == OP_PROP))
  1682. {
  1683. if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)
  1684. {
  1685. active_count--; /* Remove non-match possibility */
  1686. next_active_state--;
  1687. }
  1688. if (++count >= (int)GET2(code, 1))
  1689. { ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); }
  1690. else
  1691. { ADD_NEW(state_offset, count); }
  1692. }
  1693. }
  1694. break;
  1695. /*-----------------------------------------------------------------*/
  1696. case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
  1697. case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
  1698. case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
  1699. case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:
  1700. if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
  1701. { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
  1702. count = current_state->count; /* Number already matched */
  1703. if (clen > 0)
  1704. {
  1705. int lgb, rgb;
  1706. const pcre_uchar *nptr = ptr + clen;
  1707. int ncount = 0;
  1708. if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)
  1709. {
  1710. active_count--; /* Remove non-match possibility */
  1711. next_active_state--;
  1712. }
  1713. lgb = UCD_GRAPHBREAK(c);
  1714. while (nptr < end_subject)
  1715. {
  1716. dlen = 1;
  1717. if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
  1718. rgb = UCD_GRAPHBREAK(d);
  1719. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  1720. ncount++;
  1721. lgb = rgb;
  1722. nptr += dlen;
  1723. }
  1724. if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0)
  1725. reset_could_continue = TRUE;
  1726. if (++count >= (int)GET2(code, 1))
  1727. { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
  1728. else
  1729. { ADD_NEW_DATA(-state_offset, count, ncount); }
  1730. }
  1731. break;
  1732. #endif
  1733. /*-----------------------------------------------------------------*/
  1734. case OP_ANYNL_EXTRA + OP_TYPEEXACT:
  1735. case OP_ANYNL_EXTRA + OP_TYPEUPTO:
  1736. case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:
  1737. case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:
  1738. if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)
  1739. { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
  1740. count = current_state->count; /* Number already matched */
  1741. if (clen > 0)
  1742. {
  1743. int ncount = 0;
  1744. switch (c)
  1745. {
  1746. case CHAR_VT:
  1747. case CHAR_FF:
  1748. case CHAR_NEL:
  1749. #ifndef EBCDIC
  1750. case 0x2028:
  1751. case 0x2029:
  1752. #endif /* Not EBCDIC */
  1753. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1754. goto ANYNL03;
  1755. case CHAR_CR:
  1756. if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
  1757. /* Fall through */
  1758. ANYNL03:
  1759. case CHAR_LF:
  1760. if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
  1761. {
  1762. active_count--; /* Remove non-match possibility */
  1763. next_active_state--;
  1764. }
  1765. if (++count >= (int)GET2(code, 1))
  1766. { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
  1767. else
  1768. { ADD_NEW_DATA(-state_offset, count, ncount); }
  1769. break;
  1770. default:
  1771. break;
  1772. }
  1773. }
  1774. break;
  1775. /*-----------------------------------------------------------------*/
  1776. case OP_VSPACE_EXTRA + OP_TYPEEXACT:
  1777. case OP_VSPACE_EXTRA + OP_TYPEUPTO:
  1778. case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:
  1779. case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:
  1780. if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)
  1781. { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
  1782. count = current_state->count; /* Number already matched */
  1783. if (clen > 0)
  1784. {
  1785. BOOL OK;
  1786. switch (c)
  1787. {
  1788. VSPACE_CASES:
  1789. OK = TRUE;
  1790. break;
  1791. default:
  1792. OK = FALSE;
  1793. }
  1794. if (OK == (d == OP_VSPACE))
  1795. {
  1796. if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)
  1797. {
  1798. active_count--; /* Remove non-match possibility */
  1799. next_active_state--;
  1800. }
  1801. if (++count >= (int)GET2(code, 1))
  1802. { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
  1803. else
  1804. { ADD_NEW_DATA(-state_offset, count, 0); }
  1805. }
  1806. }
  1807. break;
  1808. /*-----------------------------------------------------------------*/
  1809. case OP_HSPACE_EXTRA + OP_TYPEEXACT:
  1810. case OP_HSPACE_EXTRA + OP_TYPEUPTO:
  1811. case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:
  1812. case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:
  1813. if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)
  1814. { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
  1815. count = current_state->count; /* Number already matched */
  1816. if (clen > 0)
  1817. {
  1818. BOOL OK;
  1819. switch (c)
  1820. {
  1821. HSPACE_CASES:
  1822. OK = TRUE;
  1823. break;
  1824. default:
  1825. OK = FALSE;
  1826. break;
  1827. }
  1828. if (OK == (d == OP_HSPACE))
  1829. {
  1830. if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)
  1831. {
  1832. active_count--; /* Remove non-match possibility */
  1833. next_active_state--;
  1834. }
  1835. if (++count >= (int)GET2(code, 1))
  1836. { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
  1837. else
  1838. { ADD_NEW_DATA(-state_offset, count, 0); }
  1839. }
  1840. }
  1841. break;
  1842. /* ========================================================================== */
  1843. /* These opcodes are followed by a character that is usually compared
  1844. to the current subject character; it is loaded into d. We still get
  1845. here even if there is no subject character, because in some cases zero
  1846. repetitions are permitted. */
  1847. /*-----------------------------------------------------------------*/
  1848. case OP_CHAR:
  1849. if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
  1850. break;
  1851. /*-----------------------------------------------------------------*/
  1852. case OP_CHARI:
  1853. if (clen == 0) break;
  1854. #ifdef SUPPORT_UTF
  1855. if (utf)
  1856. {
  1857. if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
  1858. {
  1859. unsigned int othercase;
  1860. if (c < 128)
  1861. othercase = fcc[c];
  1862. else
  1863. /* If we have Unicode property support, we can use it to test the
  1864. other case of the character. */
  1865. #ifdef SUPPORT_UCP
  1866. othercase = UCD_OTHERCASE(c);
  1867. #else
  1868. othercase = NOTACHAR;
  1869. #endif
  1870. if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
  1871. }
  1872. }
  1873. else
  1874. #endif /* SUPPORT_UTF */
  1875. /* Not UTF mode */
  1876. {
  1877. if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d))
  1878. { ADD_NEW(state_offset + 2, 0); }
  1879. }
  1880. break;
  1881. #ifdef SUPPORT_UCP
  1882. /*-----------------------------------------------------------------*/
  1883. /* This is a tricky one because it can match more than one character.
  1884. Find out how many characters to skip, and then set up a negative state
  1885. to wait for them to pass before continuing. */
  1886. case OP_EXTUNI:
  1887. if (clen > 0)
  1888. {
  1889. int lgb, rgb;
  1890. const pcre_uchar *nptr = ptr + clen;
  1891. int ncount = 0;
  1892. lgb = UCD_GRAPHBREAK(c);
  1893. while (nptr < end_subject)
  1894. {
  1895. dlen = 1;
  1896. if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
  1897. rgb = UCD_GRAPHBREAK(d);
  1898. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  1899. ncount++;
  1900. lgb = rgb;
  1901. nptr += dlen;
  1902. }
  1903. if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0)
  1904. reset_could_continue = TRUE;
  1905. ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
  1906. }
  1907. break;
  1908. #endif
  1909. /*-----------------------------------------------------------------*/
  1910. /* This is a tricky like EXTUNI because it too can match more than one
  1911. character (when CR is followed by LF). In this case, set up a negative
  1912. state to wait for one character to pass before continuing. */
  1913. case OP_ANYNL:
  1914. if (clen > 0) switch(c)
  1915. {
  1916. case CHAR_VT:
  1917. case CHAR_FF:
  1918. case CHAR_NEL:
  1919. #ifndef EBCDIC
  1920. case 0x2028:
  1921. case 0x2029:
  1922. #endif /* Not EBCDIC */
  1923. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1924. case CHAR_LF:
  1925. ADD_NEW(state_offset + 1, 0);
  1926. break;
  1927. case CHAR_CR:
  1928. if (ptr + 1 >= end_subject)
  1929. {
  1930. ADD_NEW(state_offset + 1, 0);
  1931. if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
  1932. reset_could_continue = TRUE;
  1933. }
  1934. else if (UCHAR21TEST(ptr + 1) == CHAR_LF)
  1935. {
  1936. ADD_NEW_DATA(-(state_offset + 1), 0, 1);
  1937. }
  1938. else
  1939. {
  1940. ADD_NEW(state_offset + 1, 0);
  1941. }
  1942. break;
  1943. }
  1944. break;
  1945. /*-----------------------------------------------------------------*/
  1946. case OP_NOT_VSPACE:
  1947. if (clen > 0) switch(c)
  1948. {
  1949. VSPACE_CASES:
  1950. break;
  1951. default:
  1952. ADD_NEW(state_offset + 1, 0);
  1953. break;
  1954. }
  1955. break;
  1956. /*-----------------------------------------------------------------*/
  1957. case OP_VSPACE:
  1958. if (clen > 0) switch(c)
  1959. {
  1960. VSPACE_CASES:
  1961. ADD_NEW(state_offset + 1, 0);
  1962. break;
  1963. default:
  1964. break;
  1965. }
  1966. break;
  1967. /*-----------------------------------------------------------------*/
  1968. case OP_NOT_HSPACE:
  1969. if (clen > 0) switch(c)
  1970. {
  1971. HSPACE_CASES:
  1972. break;
  1973. default:
  1974. ADD_NEW(state_offset + 1, 0);
  1975. break;
  1976. }
  1977. break;
  1978. /*-----------------------------------------------------------------*/
  1979. case OP_HSPACE:
  1980. if (clen > 0) switch(c)
  1981. {
  1982. HSPACE_CASES:
  1983. ADD_NEW(state_offset + 1, 0);
  1984. break;
  1985. default:
  1986. break;
  1987. }
  1988. break;
  1989. /*-----------------------------------------------------------------*/
  1990. /* Match a negated single character casefully. */
  1991. case OP_NOT:
  1992. if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
  1993. break;
  1994. /*-----------------------------------------------------------------*/
  1995. /* Match a negated single character caselessly. */
  1996. case OP_NOTI:
  1997. if (clen > 0)
  1998. {
  1999. pcre_uint32 otherd;
  2000. #ifdef SUPPORT_UTF
  2001. if (utf && d >= 128)
  2002. {
  2003. #ifdef SUPPORT_UCP
  2004. otherd = UCD_OTHERCASE(d);
  2005. #else
  2006. otherd = d;
  2007. #endif /* SUPPORT_UCP */
  2008. }
  2009. else
  2010. #endif /* SUPPORT_UTF */
  2011. otherd = TABLE_GET(d, fcc, d);
  2012. if (c != d && c != otherd)
  2013. { ADD_NEW(state_offset + dlen + 1, 0); }
  2014. }
  2015. break;
  2016. /*-----------------------------------------------------------------*/
  2017. case OP_PLUSI:
  2018. case OP_MINPLUSI:
  2019. case OP_POSPLUSI:
  2020. case OP_NOTPLUSI:
  2021. case OP_NOTMINPLUSI:
  2022. case OP_NOTPOSPLUSI:
  2023. caseless = TRUE;
  2024. codevalue -= OP_STARI - OP_STAR;
  2025. /* Fall through */
  2026. case OP_PLUS:
  2027. case OP_MINPLUS:
  2028. case OP_POSPLUS:
  2029. case OP_NOTPLUS:
  2030. case OP_NOTMINPLUS:
  2031. case OP_NOTPOSPLUS:
  2032. count = current_state->count; /* Already matched */
  2033. if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
  2034. if (clen > 0)
  2035. {
  2036. pcre_uint32 otherd = NOTACHAR;
  2037. if (caseless)
  2038. {
  2039. #ifdef SUPPORT_UTF
  2040. if (utf && d >= 128)
  2041. {
  2042. #ifdef SUPPORT_UCP
  2043. otherd = UCD_OTHERCASE(d);
  2044. #endif /* SUPPORT_UCP */
  2045. }
  2046. else
  2047. #endif /* SUPPORT_UTF */
  2048. otherd = TABLE_GET(d, fcc, d);
  2049. }
  2050. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  2051. {
  2052. if (count > 0 &&
  2053. (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))
  2054. {
  2055. active_count--; /* Remove non-match possibility */
  2056. next_active_state--;
  2057. }
  2058. count++;
  2059. ADD_NEW(state_offset, count);
  2060. }
  2061. }
  2062. break;
  2063. /*-----------------------------------------------------------------*/
  2064. case OP_QUERYI:
  2065. case OP_MINQUERYI:
  2066. case OP_POSQUERYI:
  2067. case OP_NOTQUERYI:
  2068. case OP_NOTMINQUERYI:
  2069. case OP_NOTPOSQUERYI:
  2070. caseless = TRUE;
  2071. codevalue -= OP_STARI - OP_STAR;
  2072. /* Fall through */
  2073. case OP_QUERY:
  2074. case OP_MINQUERY:
  2075. case OP_POSQUERY:
  2076. case OP_NOTQUERY:
  2077. case OP_NOTMINQUERY:
  2078. case OP_NOTPOSQUERY:
  2079. ADD_ACTIVE(state_offset + dlen + 1, 0);
  2080. if (clen > 0)
  2081. {
  2082. pcre_uint32 otherd = NOTACHAR;
  2083. if (caseless)
  2084. {
  2085. #ifdef SUPPORT_UTF
  2086. if (utf && d >= 128)
  2087. {
  2088. #ifdef SUPPORT_UCP
  2089. otherd = UCD_OTHERCASE(d);
  2090. #endif /* SUPPORT_UCP */
  2091. }
  2092. else
  2093. #endif /* SUPPORT_UTF */
  2094. otherd = TABLE_GET(d, fcc, d);
  2095. }
  2096. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  2097. {
  2098. if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)
  2099. {
  2100. active_count--; /* Remove non-match possibility */
  2101. next_active_state--;
  2102. }
  2103. ADD_NEW(state_offset + dlen + 1, 0);
  2104. }
  2105. }
  2106. break;
  2107. /*-----------------------------------------------------------------*/
  2108. case OP_STARI:
  2109. case OP_MINSTARI:
  2110. case OP_POSSTARI:
  2111. case OP_NOTSTARI:
  2112. case OP_NOTMINSTARI:
  2113. case OP_NOTPOSSTARI:
  2114. caseless = TRUE;
  2115. codevalue -= OP_STARI - OP_STAR;
  2116. /* Fall through */
  2117. case OP_STAR:
  2118. case OP_MINSTAR:
  2119. case OP_POSSTAR:
  2120. case OP_NOTSTAR:
  2121. case OP_NOTMINSTAR:
  2122. case OP_NOTPOSSTAR:
  2123. ADD_ACTIVE(state_offset + dlen + 1, 0);
  2124. if (clen > 0)
  2125. {
  2126. pcre_uint32 otherd = NOTACHAR;
  2127. if (caseless)
  2128. {
  2129. #ifdef SUPPORT_UTF
  2130. if (utf && d >= 128)
  2131. {
  2132. #ifdef SUPPORT_UCP
  2133. otherd = UCD_OTHERCASE(d);
  2134. #endif /* SUPPORT_UCP */
  2135. }
  2136. else
  2137. #endif /* SUPPORT_UTF */
  2138. otherd = TABLE_GET(d, fcc, d);
  2139. }
  2140. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  2141. {
  2142. if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)
  2143. {
  2144. active_count--; /* Remove non-match possibility */
  2145. next_active_state--;
  2146. }
  2147. ADD_NEW(state_offset, 0);
  2148. }
  2149. }
  2150. break;
  2151. /*-----------------------------------------------------------------*/
  2152. case OP_EXACTI:
  2153. case OP_NOTEXACTI:
  2154. caseless = TRUE;
  2155. codevalue -= OP_STARI - OP_STAR;
  2156. /* Fall through */
  2157. case OP_EXACT:
  2158. case OP_NOTEXACT:
  2159. count = current_state->count; /* Number already matched */
  2160. if (clen > 0)
  2161. {
  2162. pcre_uint32 otherd = NOTACHAR;
  2163. if (caseless)
  2164. {
  2165. #ifdef SUPPORT_UTF
  2166. if (utf && d >= 128)
  2167. {
  2168. #ifdef SUPPORT_UCP
  2169. otherd = UCD_OTHERCASE(d);
  2170. #endif /* SUPPORT_UCP */
  2171. }
  2172. else
  2173. #endif /* SUPPORT_UTF */
  2174. otherd = TABLE_GET(d, fcc, d);
  2175. }
  2176. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  2177. {
  2178. if (++count >= (int)GET2(code, 1))
  2179. { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
  2180. else
  2181. { ADD_NEW(state_offset, count); }
  2182. }
  2183. }
  2184. break;
  2185. /*-----------------------------------------------------------------*/
  2186. case OP_UPTOI:
  2187. case OP_MINUPTOI:
  2188. case OP_POSUPTOI:
  2189. case OP_NOTUPTOI:
  2190. case OP_NOTMINUPTOI:
  2191. case OP_NOTPOSUPTOI:
  2192. caseless = TRUE;
  2193. codevalue -= OP_STARI - OP_STAR;
  2194. /* Fall through */
  2195. case OP_UPTO:
  2196. case OP_MINUPTO:
  2197. case OP_POSUPTO:
  2198. case OP_NOTUPTO:
  2199. case OP_NOTMINUPTO:
  2200. case OP_NOTPOSUPTO:
  2201. ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0);
  2202. count = current_state->count; /* Number already matched */
  2203. if (clen > 0)
  2204. {
  2205. pcre_uint32 otherd = NOTACHAR;
  2206. if (caseless)
  2207. {
  2208. #ifdef SUPPORT_UTF
  2209. if (utf && d >= 128)
  2210. {
  2211. #ifdef SUPPORT_UCP
  2212. otherd = UCD_OTHERCASE(d);
  2213. #endif /* SUPPORT_UCP */
  2214. }
  2215. else
  2216. #endif /* SUPPORT_UTF */
  2217. otherd = TABLE_GET(d, fcc, d);
  2218. }
  2219. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  2220. {
  2221. if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)
  2222. {
  2223. active_count--; /* Remove non-match possibility */
  2224. next_active_state--;
  2225. }
  2226. if (++count >= (int)GET2(code, 1))
  2227. { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
  2228. else
  2229. { ADD_NEW(state_offset, count); }
  2230. }
  2231. }
  2232. break;
  2233. /* ========================================================================== */
  2234. /* These are the class-handling opcodes */
  2235. case OP_CLASS:
  2236. case OP_NCLASS:
  2237. case OP_XCLASS:
  2238. {
  2239. BOOL isinclass = FALSE;
  2240. int next_state_offset;
  2241. const pcre_uchar *ecode;
  2242. /* For a simple class, there is always just a 32-byte table, and we
  2243. can set isinclass from it. */
  2244. if (codevalue != OP_XCLASS)
  2245. {
  2246. ecode = code + 1 + (32 / sizeof(pcre_uchar));
  2247. if (clen > 0)
  2248. {
  2249. isinclass = (c > 255)? (codevalue == OP_NCLASS) :
  2250. ((((pcre_uint8 *)(code + 1))[c/8] & (1 << (c&7))) != 0);
  2251. }
  2252. }
  2253. /* An extended class may have a table or a list of single characters,
  2254. ranges, or both, and it may be positive or negative. There's a
  2255. function that sorts all this out. */
  2256. else
  2257. {
  2258. ecode = code + GET(code, 1);
  2259. if (clen > 0) isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE, utf);
  2260. }
  2261. /* At this point, isinclass is set for all kinds of class, and ecode
  2262. points to the byte after the end of the class. If there is a
  2263. quantifier, this is where it will be. */
  2264. next_state_offset = (int)(ecode - start_code);
  2265. switch (*ecode)
  2266. {
  2267. case OP_CRSTAR:
  2268. case OP_CRMINSTAR:
  2269. case OP_CRPOSSTAR:
  2270. ADD_ACTIVE(next_state_offset + 1, 0);
  2271. if (isinclass)
  2272. {
  2273. if (*ecode == OP_CRPOSSTAR)
  2274. {
  2275. active_count--; /* Remove non-match possibility */
  2276. next_active_state--;
  2277. }
  2278. ADD_NEW(state_offset, 0);
  2279. }
  2280. break;
  2281. case OP_CRPLUS:
  2282. case OP_CRMINPLUS:
  2283. case OP_CRPOSPLUS:
  2284. count = current_state->count; /* Already matched */
  2285. if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
  2286. if (isinclass)
  2287. {
  2288. if (count > 0 && *ecode == OP_CRPOSPLUS)
  2289. {
  2290. active_count--; /* Remove non-match possibility */
  2291. next_active_state--;
  2292. }
  2293. count++;
  2294. ADD_NEW(state_offset, count);
  2295. }
  2296. break;
  2297. case OP_CRQUERY:
  2298. case OP_CRMINQUERY:
  2299. case OP_CRPOSQUERY:
  2300. ADD_ACTIVE(next_state_offset + 1, 0);
  2301. if (isinclass)
  2302. {
  2303. if (*ecode == OP_CRPOSQUERY)
  2304. {
  2305. active_count--; /* Remove non-match possibility */
  2306. next_active_state--;
  2307. }
  2308. ADD_NEW(next_state_offset + 1, 0);
  2309. }
  2310. break;
  2311. case OP_CRRANGE:
  2312. case OP_CRMINRANGE:
  2313. case OP_CRPOSRANGE:
  2314. count = current_state->count; /* Already matched */
  2315. if (count >= (int)GET2(ecode, 1))
  2316. { ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
  2317. if (isinclass)
  2318. {
  2319. int max = (int)GET2(ecode, 1 + IMM2_SIZE);
  2320. if (*ecode == OP_CRPOSRANGE && count >= (int)GET2(ecode, 1))
  2321. {
  2322. active_count--; /* Remove non-match possibility */
  2323. next_active_state--;
  2324. }
  2325. if (++count >= max && max != 0) /* Max 0 => no limit */
  2326. { ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
  2327. else
  2328. { ADD_NEW(state_offset, count); }
  2329. }
  2330. break;
  2331. default:
  2332. if (isinclass) { ADD_NEW(next_state_offset, 0); }
  2333. break;
  2334. }
  2335. }
  2336. break;
  2337. /* ========================================================================== */
  2338. /* These are the opcodes for fancy brackets of various kinds. We have
  2339. to use recursion in order to handle them. The "always failing" assertion
  2340. (?!) is optimised to OP_FAIL when compiling, so we have to support that,
  2341. though the other "backtracking verbs" are not supported. */
  2342. case OP_FAIL:
  2343. forced_fail++; /* Count FAILs for multiple states */
  2344. break;
  2345. case OP_ASSERT:
  2346. case OP_ASSERT_NOT:
  2347. case OP_ASSERTBACK:
  2348. case OP_ASSERTBACK_NOT:
  2349. {
  2350. int rc;
  2351. int local_offsets[2];
  2352. int local_workspace[1000];
  2353. const pcre_uchar *endasscode = code + GET(code, 1);
  2354. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  2355. rc = internal_dfa_exec(
  2356. md, /* static match data */
  2357. code, /* this subexpression's code */
  2358. ptr, /* where we currently are */
  2359. (int)(ptr - start_subject), /* start offset */
  2360. local_offsets, /* offset vector */
  2361. sizeof(local_offsets)/sizeof(int), /* size of same */
  2362. local_workspace, /* workspace vector */
  2363. sizeof(local_workspace)/sizeof(int), /* size of same */
  2364. rlevel); /* function recursion level */
  2365. if (rc == PCRE_ERROR_DFA_UITEM) return rc;
  2366. if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
  2367. { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
  2368. }
  2369. break;
  2370. /*-----------------------------------------------------------------*/
  2371. case OP_COND:
  2372. case OP_SCOND:
  2373. {
  2374. int local_offsets[1000];
  2375. int local_workspace[1000];
  2376. int codelink = GET(code, 1);
  2377. int condcode;
  2378. /* Because of the way auto-callout works during compile, a callout item
  2379. is inserted between OP_COND and an assertion condition. This does not
  2380. happen for the other conditions. */
  2381. if (code[LINK_SIZE+1] == OP_CALLOUT)
  2382. {
  2383. rrc = 0;
  2384. if (PUBL(callout) != NULL)
  2385. {
  2386. PUBL(callout_block) cb;
  2387. cb.version = 1; /* Version 1 of the callout block */
  2388. cb.callout_number = code[LINK_SIZE+2];
  2389. cb.offset_vector = offsets;
  2390. #if defined COMPILE_PCRE8
  2391. cb.subject = (PCRE_SPTR)start_subject;
  2392. #elif defined COMPILE_PCRE16
  2393. cb.subject = (PCRE_SPTR16)start_subject;
  2394. #elif defined COMPILE_PCRE32
  2395. cb.subject = (PCRE_SPTR32)start_subject;
  2396. #endif
  2397. cb.subject_length = (int)(end_subject - start_subject);
  2398. cb.start_match = (int)(current_subject - start_subject);
  2399. cb.current_position = (int)(ptr - start_subject);
  2400. cb.pattern_position = GET(code, LINK_SIZE + 3);
  2401. cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);
  2402. cb.capture_top = 1;
  2403. cb.capture_last = -1;
  2404. cb.callout_data = md->callout_data;
  2405. cb.mark = NULL; /* No (*MARK) support */
  2406. if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */
  2407. }
  2408. if (rrc > 0) break; /* Fail this thread */
  2409. code += PRIV(OP_lengths)[OP_CALLOUT]; /* Skip callout data */
  2410. }
  2411. condcode = code[LINK_SIZE+1];
  2412. /* Back reference conditions and duplicate named recursion conditions
  2413. are not supported */
  2414. if (condcode == OP_CREF || condcode == OP_DNCREF ||
  2415. condcode == OP_DNRREF)
  2416. return PCRE_ERROR_DFA_UCOND;
  2417. /* The DEFINE condition is always false, and the assertion (?!) is
  2418. converted to OP_FAIL. */
  2419. if (condcode == OP_DEF || condcode == OP_FAIL)
  2420. { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
  2421. /* The only supported version of OP_RREF is for the value RREF_ANY,
  2422. which means "test if in any recursion". We can't test for specifically
  2423. recursed groups. */
  2424. else if (condcode == OP_RREF)
  2425. {
  2426. int value = GET2(code, LINK_SIZE + 2);
  2427. if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
  2428. if (md->recursive != NULL)
  2429. { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }
  2430. else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
  2431. }
  2432. /* Otherwise, the condition is an assertion */
  2433. else
  2434. {
  2435. int rc;
  2436. const pcre_uchar *asscode = code + LINK_SIZE + 1;
  2437. const pcre_uchar *endasscode = asscode + GET(asscode, 1);
  2438. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  2439. rc = internal_dfa_exec(
  2440. md, /* fixed match data */
  2441. asscode, /* this subexpression's code */
  2442. ptr, /* where we currently are */
  2443. (int)(ptr - start_subject), /* start offset */
  2444. local_offsets, /* offset vector */
  2445. sizeof(local_offsets)/sizeof(int), /* size of same */
  2446. local_workspace, /* workspace vector */
  2447. sizeof(local_workspace)/sizeof(int), /* size of same */
  2448. rlevel); /* function recursion level */
  2449. if (rc == PCRE_ERROR_DFA_UITEM) return rc;
  2450. if ((rc >= 0) ==
  2451. (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
  2452. { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
  2453. else
  2454. { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
  2455. }
  2456. }
  2457. break;
  2458. /*-----------------------------------------------------------------*/
  2459. case OP_RECURSE:
  2460. {
  2461. dfa_recursion_info *ri;
  2462. int local_offsets[1000];
  2463. int local_workspace[1000];
  2464. const pcre_uchar *callpat = start_code + GET(code, 1);
  2465. int recno = (callpat == md->start_code)? 0 :
  2466. GET2(callpat, 1 + LINK_SIZE);
  2467. int rc;
  2468. DPRINTF(("%.*sStarting regex recursion\n", rlevel*2-2, SP));
  2469. /* Check for repeating a recursion without advancing the subject
  2470. pointer. This should catch convoluted mutual recursions. (Some simple
  2471. cases are caught at compile time.) */
  2472. for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
  2473. if (recno == ri->group_num && ptr == ri->subject_position)
  2474. return PCRE_ERROR_RECURSELOOP;
  2475. /* Remember this recursion and where we started it so as to
  2476. catch infinite loops. */
  2477. new_recursive.group_num = recno;
  2478. new_recursive.subject_position = ptr;
  2479. new_recursive.prevrec = md->recursive;
  2480. md->recursive = &new_recursive;
  2481. rc = internal_dfa_exec(
  2482. md, /* fixed match data */
  2483. callpat, /* this subexpression's code */
  2484. ptr, /* where we currently are */
  2485. (int)(ptr - start_subject), /* start offset */
  2486. local_offsets, /* offset vector */
  2487. sizeof(local_offsets)/sizeof(int), /* size of same */
  2488. local_workspace, /* workspace vector */
  2489. sizeof(local_workspace)/sizeof(int), /* size of same */
  2490. rlevel); /* function recursion level */
  2491. md->recursive = new_recursive.prevrec; /* Done this recursion */
  2492. DPRINTF(("%.*sReturn from regex recursion: rc=%d\n", rlevel*2-2, SP,
  2493. rc));
  2494. /* Ran out of internal offsets */
  2495. if (rc == 0) return PCRE_ERROR_DFA_RECURSE;
  2496. /* For each successful matched substring, set up the next state with a
  2497. count of characters to skip before trying it. Note that the count is in
  2498. characters, not bytes. */
  2499. if (rc > 0)
  2500. {
  2501. for (rc = rc*2 - 2; rc >= 0; rc -= 2)
  2502. {
  2503. int charcount = local_offsets[rc+1] - local_offsets[rc];
  2504. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  2505. if (utf)
  2506. {
  2507. const pcre_uchar *p = start_subject + local_offsets[rc];
  2508. const pcre_uchar *pp = start_subject + local_offsets[rc+1];
  2509. while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
  2510. }
  2511. #endif
  2512. if (charcount > 0)
  2513. {
  2514. ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0, (charcount - 1));
  2515. }
  2516. else
  2517. {
  2518. ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
  2519. }
  2520. }
  2521. }
  2522. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  2523. }
  2524. break;
  2525. /*-----------------------------------------------------------------*/
  2526. case OP_BRAPOS:
  2527. case OP_SBRAPOS:
  2528. case OP_CBRAPOS:
  2529. case OP_SCBRAPOS:
  2530. case OP_BRAPOSZERO:
  2531. {
  2532. int charcount, matched_count;
  2533. const pcre_uchar *local_ptr = ptr;
  2534. BOOL allow_zero;
  2535. if (codevalue == OP_BRAPOSZERO)
  2536. {
  2537. allow_zero = TRUE;
  2538. codevalue = *(++code); /* Codevalue will be one of above BRAs */
  2539. }
  2540. else allow_zero = FALSE;
  2541. /* Loop to match the subpattern as many times as possible as if it were
  2542. a complete pattern. */
  2543. for (matched_count = 0;; matched_count++)
  2544. {
  2545. int local_offsets[2];
  2546. int local_workspace[1000];
  2547. int rc = internal_dfa_exec(
  2548. md, /* fixed match data */
  2549. code, /* this subexpression's code */
  2550. local_ptr, /* where we currently are */
  2551. (int)(ptr - start_subject), /* start offset */
  2552. local_offsets, /* offset vector */
  2553. sizeof(local_offsets)/sizeof(int), /* size of same */
  2554. local_workspace, /* workspace vector */
  2555. sizeof(local_workspace)/sizeof(int), /* size of same */
  2556. rlevel); /* function recursion level */
  2557. /* Failed to match */
  2558. if (rc < 0)
  2559. {
  2560. if (rc != PCRE_ERROR_NOMATCH) return rc;
  2561. break;
  2562. }
  2563. /* Matched: break the loop if zero characters matched. */
  2564. charcount = local_offsets[1] - local_offsets[0];
  2565. if (charcount == 0) break;
  2566. local_ptr += charcount; /* Advance temporary position ptr */
  2567. }
  2568. /* At this point we have matched the subpattern matched_count
  2569. times, and local_ptr is pointing to the character after the end of the
  2570. last match. */
  2571. if (matched_count > 0 || allow_zero)
  2572. {
  2573. const pcre_uchar *end_subpattern = code;
  2574. int next_state_offset;
  2575. do { end_subpattern += GET(end_subpattern, 1); }
  2576. while (*end_subpattern == OP_ALT);
  2577. next_state_offset =
  2578. (int)(end_subpattern - start_code + LINK_SIZE + 1);
  2579. /* Optimization: if there are no more active states, and there
  2580. are no new states yet set up, then skip over the subject string
  2581. right here, to save looping. Otherwise, set up the new state to swing
  2582. into action when the end of the matched substring is reached. */
  2583. if (i + 1 >= active_count && new_count == 0)
  2584. {
  2585. ptr = local_ptr;
  2586. clen = 0;
  2587. ADD_NEW(next_state_offset, 0);
  2588. }
  2589. else
  2590. {
  2591. const pcre_uchar *p = ptr;
  2592. const pcre_uchar *pp = local_ptr;
  2593. charcount = (int)(pp - p);
  2594. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  2595. if (utf) while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
  2596. #endif
  2597. ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
  2598. }
  2599. }
  2600. }
  2601. break;
  2602. /*-----------------------------------------------------------------*/
  2603. case OP_ONCE:
  2604. case OP_ONCE_NC:
  2605. {
  2606. int local_offsets[2];
  2607. int local_workspace[1000];
  2608. int rc = internal_dfa_exec(
  2609. md, /* fixed match data */
  2610. code, /* this subexpression's code */
  2611. ptr, /* where we currently are */
  2612. (int)(ptr - start_subject), /* start offset */
  2613. local_offsets, /* offset vector */
  2614. sizeof(local_offsets)/sizeof(int), /* size of same */
  2615. local_workspace, /* workspace vector */
  2616. sizeof(local_workspace)/sizeof(int), /* size of same */
  2617. rlevel); /* function recursion level */
  2618. if (rc >= 0)
  2619. {
  2620. const pcre_uchar *end_subpattern = code;
  2621. int charcount = local_offsets[1] - local_offsets[0];
  2622. int next_state_offset, repeat_state_offset;
  2623. do { end_subpattern += GET(end_subpattern, 1); }
  2624. while (*end_subpattern == OP_ALT);
  2625. next_state_offset =
  2626. (int)(end_subpattern - start_code + LINK_SIZE + 1);
  2627. /* If the end of this subpattern is KETRMAX or KETRMIN, we must
  2628. arrange for the repeat state also to be added to the relevant list.
  2629. Calculate the offset, or set -1 for no repeat. */
  2630. repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
  2631. *end_subpattern == OP_KETRMIN)?
  2632. (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
  2633. /* If we have matched an empty string, add the next state at the
  2634. current character pointer. This is important so that the duplicate
  2635. checking kicks in, which is what breaks infinite loops that match an
  2636. empty string. */
  2637. if (charcount == 0)
  2638. {
  2639. ADD_ACTIVE(next_state_offset, 0);
  2640. }
  2641. /* Optimization: if there are no more active states, and there
  2642. are no new states yet set up, then skip over the subject string
  2643. right here, to save looping. Otherwise, set up the new state to swing
  2644. into action when the end of the matched substring is reached. */
  2645. else if (i + 1 >= active_count && new_count == 0)
  2646. {
  2647. ptr += charcount;
  2648. clen = 0;
  2649. ADD_NEW(next_state_offset, 0);
  2650. /* If we are adding a repeat state at the new character position,
  2651. we must fudge things so that it is the only current state.
  2652. Otherwise, it might be a duplicate of one we processed before, and
  2653. that would cause it to be skipped. */
  2654. if (repeat_state_offset >= 0)
  2655. {
  2656. next_active_state = active_states;
  2657. active_count = 0;
  2658. i = -1;
  2659. ADD_ACTIVE(repeat_state_offset, 0);
  2660. }
  2661. }
  2662. else
  2663. {
  2664. #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
  2665. if (utf)
  2666. {
  2667. const pcre_uchar *p = start_subject + local_offsets[0];
  2668. const pcre_uchar *pp = start_subject + local_offsets[1];
  2669. while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
  2670. }
  2671. #endif
  2672. ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
  2673. if (repeat_state_offset >= 0)
  2674. { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
  2675. }
  2676. }
  2677. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  2678. }
  2679. break;
  2680. /* ========================================================================== */
  2681. /* Handle callouts */
  2682. case OP_CALLOUT:
  2683. rrc = 0;
  2684. if (PUBL(callout) != NULL)
  2685. {
  2686. PUBL(callout_block) cb;
  2687. cb.version = 1; /* Version 1 of the callout block */
  2688. cb.callout_number = code[1];
  2689. cb.offset_vector = offsets;
  2690. #if defined COMPILE_PCRE8
  2691. cb.subject = (PCRE_SPTR)start_subject;
  2692. #elif defined COMPILE_PCRE16
  2693. cb.subject = (PCRE_SPTR16)start_subject;
  2694. #elif defined COMPILE_PCRE32
  2695. cb.subject = (PCRE_SPTR32)start_subject;
  2696. #endif
  2697. cb.subject_length = (int)(end_subject - start_subject);
  2698. cb.start_match = (int)(current_subject - start_subject);
  2699. cb.current_position = (int)(ptr - start_subject);
  2700. cb.pattern_position = GET(code, 2);
  2701. cb.next_item_length = GET(code, 2 + LINK_SIZE);
  2702. cb.capture_top = 1;
  2703. cb.capture_last = -1;
  2704. cb.callout_data = md->callout_data;
  2705. cb.mark = NULL; /* No (*MARK) support */
  2706. if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */
  2707. }
  2708. if (rrc == 0)
  2709. { ADD_ACTIVE(state_offset + PRIV(OP_lengths)[OP_CALLOUT], 0); }
  2710. break;
  2711. /* ========================================================================== */
  2712. default: /* Unsupported opcode */
  2713. return PCRE_ERROR_DFA_UITEM;
  2714. }
  2715. NEXT_ACTIVE_STATE: continue;
  2716. } /* End of loop scanning active states */
  2717. /* We have finished the processing at the current subject character. If no
  2718. new states have been set for the next character, we have found all the
  2719. matches that we are going to find. If we are at the top level and partial
  2720. matching has been requested, check for appropriate conditions.
  2721. The "forced_ fail" variable counts the number of (*F) encountered for the
  2722. character. If it is equal to the original active_count (saved in
  2723. workspace[1]) it means that (*F) was found on every active state. In this
  2724. case we don't want to give a partial match.
  2725. The "could_continue" variable is true if a state could have continued but
  2726. for the fact that the end of the subject was reached. */
  2727. if (new_count <= 0)
  2728. {
  2729. if (rlevel == 1 && /* Top level, and */
  2730. could_continue && /* Some could go on, and */
  2731. forced_fail != workspace[1] && /* Not all forced fail & */
  2732. ( /* either... */
  2733. (md->moptions & PCRE_PARTIAL_HARD) != 0 /* Hard partial */
  2734. || /* or... */
  2735. ((md->moptions & PCRE_PARTIAL_SOFT) != 0 && /* Soft partial and */
  2736. match_count < 0) /* no matches */
  2737. ) && /* And... */
  2738. (
  2739. partial_newline || /* Either partial NL */
  2740. ( /* or ... */
  2741. ptr >= end_subject && /* End of subject and */
  2742. ptr > md->start_used_ptr) /* Inspected non-empty string */
  2743. )
  2744. )
  2745. match_count = PCRE_ERROR_PARTIAL;
  2746. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  2747. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, match_count,
  2748. rlevel*2-2, SP));
  2749. break; /* In effect, "return", but see the comment below */
  2750. }
  2751. /* One or more states are active for the next character. */
  2752. ptr += clen; /* Advance to next subject character */
  2753. } /* Loop to move along the subject string */
  2754. /* Control gets here from "break" a few lines above. We do it this way because
  2755. if we use "return" above, we have compiler trouble. Some compilers warn if
  2756. there's nothing here because they think the function doesn't return a value. On
  2757. the other hand, if we put a dummy statement here, some more clever compilers
  2758. complain that it can't be reached. Sigh. */
  2759. return match_count;
  2760. }
  2761. /*************************************************
  2762. * Execute a Regular Expression - DFA engine *
  2763. *************************************************/
  2764. /* This external function applies a compiled re to a subject string using a DFA
  2765. engine. This function calls the internal function multiple times if the pattern
  2766. is not anchored.
  2767. Arguments:
  2768. argument_re points to the compiled expression
  2769. extra_data points to extra data or is NULL
  2770. subject points to the subject string
  2771. length length of subject string (may contain binary zeros)
  2772. start_offset where to start in the subject string
  2773. options option bits
  2774. offsets vector of match offsets
  2775. offsetcount size of same
  2776. workspace workspace vector
  2777. wscount size of same
  2778. Returns: > 0 => number of match offset pairs placed in offsets
  2779. = 0 => offsets overflowed; longest matches are present
  2780. -1 => failed to match
  2781. < -1 => some kind of unexpected problem
  2782. */
  2783. #if defined COMPILE_PCRE8
  2784. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  2785. pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,
  2786. const char *subject, int length, int start_offset, int options, int *offsets,
  2787. int offsetcount, int *workspace, int wscount)
  2788. #elif defined COMPILE_PCRE16
  2789. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  2790. pcre16_dfa_exec(const pcre16 *argument_re, const pcre16_extra *extra_data,
  2791. PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets,
  2792. int offsetcount, int *workspace, int wscount)
  2793. #elif defined COMPILE_PCRE32
  2794. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  2795. pcre32_dfa_exec(const pcre32 *argument_re, const pcre32_extra *extra_data,
  2796. PCRE_SPTR32 subject, int length, int start_offset, int options, int *offsets,
  2797. int offsetcount, int *workspace, int wscount)
  2798. #endif
  2799. {
  2800. REAL_PCRE *re = (REAL_PCRE *)argument_re;
  2801. dfa_match_data match_block;
  2802. dfa_match_data *md = &match_block;
  2803. BOOL utf, anchored, startline, firstline;
  2804. const pcre_uchar *current_subject, *end_subject;
  2805. const pcre_study_data *study = NULL;
  2806. const pcre_uchar *req_char_ptr;
  2807. const pcre_uint8 *start_bits = NULL;
  2808. BOOL has_first_char = FALSE;
  2809. BOOL has_req_char = FALSE;
  2810. pcre_uchar first_char = 0;
  2811. pcre_uchar first_char2 = 0;
  2812. pcre_uchar req_char = 0;
  2813. pcre_uchar req_char2 = 0;
  2814. int newline;
  2815. /* Plausibility checks */
  2816. if ((options & ~PUBLIC_DFA_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
  2817. if (re == NULL || subject == NULL || workspace == NULL ||
  2818. (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
  2819. if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
  2820. if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
  2821. if (length < 0) return PCRE_ERROR_BADLENGTH;
  2822. if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
  2823. /* Check that the first field in the block is the magic number. If it is not,
  2824. return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to
  2825. REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which
  2826. means that the pattern is likely compiled with different endianness. */
  2827. if (re->magic_number != MAGIC_NUMBER)
  2828. return re->magic_number == REVERSED_MAGIC_NUMBER?
  2829. PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC;
  2830. if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
  2831. /* If restarting after a partial match, do some sanity checks on the contents
  2832. of the workspace. */
  2833. if ((options & PCRE_DFA_RESTART) != 0)
  2834. {
  2835. if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 ||
  2836. workspace[1] > (wscount - 2)/INTS_PER_STATEBLOCK)
  2837. return PCRE_ERROR_DFA_BADRESTART;
  2838. }
  2839. /* Set up study, callout, and table data */
  2840. md->tables = re->tables;
  2841. md->callout_data = NULL;
  2842. if (extra_data != NULL)
  2843. {
  2844. unsigned long int flags = extra_data->flags;
  2845. if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
  2846. study = (const pcre_study_data *)extra_data->study_data;
  2847. if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) return PCRE_ERROR_DFA_UMLIMIT;
  2848. if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
  2849. return PCRE_ERROR_DFA_UMLIMIT;
  2850. if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
  2851. md->callout_data = extra_data->callout_data;
  2852. if ((flags & PCRE_EXTRA_TABLES) != 0)
  2853. md->tables = extra_data->tables;
  2854. }
  2855. /* Set some local values */
  2856. current_subject = (const pcre_uchar *)subject + start_offset;
  2857. end_subject = (const pcre_uchar *)subject + length;
  2858. req_char_ptr = current_subject - 1;
  2859. #ifdef SUPPORT_UTF
  2860. /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
  2861. utf = (re->options & PCRE_UTF8) != 0;
  2862. #else
  2863. utf = FALSE;
  2864. #endif
  2865. anchored = (options & (PCRE_ANCHORED|PCRE_DFA_RESTART)) != 0 ||
  2866. (re->options & PCRE_ANCHORED) != 0;
  2867. /* The remaining fixed data for passing around. */
  2868. md->start_code = (const pcre_uchar *)argument_re +
  2869. re->name_table_offset + re->name_count * re->name_entry_size;
  2870. md->start_subject = (const pcre_uchar *)subject;
  2871. md->end_subject = end_subject;
  2872. md->start_offset = start_offset;
  2873. md->moptions = options;
  2874. md->poptions = re->options;
  2875. /* If the BSR option is not set at match time, copy what was set
  2876. at compile time. */
  2877. if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0)
  2878. {
  2879. if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
  2880. md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE);
  2881. #ifdef BSR_ANYCRLF
  2882. else md->moptions |= PCRE_BSR_ANYCRLF;
  2883. #endif
  2884. }
  2885. /* Handle different types of newline. The three bits give eight cases. If
  2886. nothing is set at run time, whatever was used at compile time applies. */
  2887. switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : (pcre_uint32)options) &
  2888. PCRE_NEWLINE_BITS)
  2889. {
  2890. case 0: newline = NEWLINE; break; /* Compile-time default */
  2891. case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
  2892. case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
  2893. case PCRE_NEWLINE_CR+
  2894. PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
  2895. case PCRE_NEWLINE_ANY: newline = -1; break;
  2896. case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
  2897. default: return PCRE_ERROR_BADNEWLINE;
  2898. }
  2899. if (newline == -2)
  2900. {
  2901. md->nltype = NLTYPE_ANYCRLF;
  2902. }
  2903. else if (newline < 0)
  2904. {
  2905. md->nltype = NLTYPE_ANY;
  2906. }
  2907. else
  2908. {
  2909. md->nltype = NLTYPE_FIXED;
  2910. if (newline > 255)
  2911. {
  2912. md->nllen = 2;
  2913. md->nl[0] = (newline >> 8) & 255;
  2914. md->nl[1] = newline & 255;
  2915. }
  2916. else
  2917. {
  2918. md->nllen = 1;
  2919. md->nl[0] = newline;
  2920. }
  2921. }
  2922. /* Check a UTF-8 string if required. Unfortunately there's no way of passing
  2923. back the character offset. */
  2924. #ifdef SUPPORT_UTF
  2925. if (utf && (options & PCRE_NO_UTF8_CHECK) == 0)
  2926. {
  2927. int erroroffset;
  2928. int errorcode = PRIV(valid_utf)((pcre_uchar *)subject, length, &erroroffset);
  2929. if (errorcode != 0)
  2930. {
  2931. if (offsetcount >= 2)
  2932. {
  2933. offsets[0] = erroroffset;
  2934. offsets[1] = errorcode;
  2935. }
  2936. #if defined COMPILE_PCRE8
  2937. return (errorcode <= PCRE_UTF8_ERR5 && (options & PCRE_PARTIAL_HARD) != 0) ?
  2938. PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
  2939. #elif defined COMPILE_PCRE16
  2940. return (errorcode <= PCRE_UTF16_ERR1 && (options & PCRE_PARTIAL_HARD) != 0) ?
  2941. PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16;
  2942. #elif defined COMPILE_PCRE32
  2943. return PCRE_ERROR_BADUTF32;
  2944. #endif
  2945. }
  2946. #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
  2947. if (start_offset > 0 && start_offset < length &&
  2948. NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset]))
  2949. return PCRE_ERROR_BADUTF8_OFFSET;
  2950. #endif
  2951. }
  2952. #endif
  2953. /* If the exec call supplied NULL for tables, use the inbuilt ones. This
  2954. is a feature that makes it possible to save compiled regex and re-use them
  2955. in other programs later. */
  2956. if (md->tables == NULL) md->tables = PRIV(default_tables);
  2957. /* The "must be at the start of a line" flags are used in a loop when finding
  2958. where to start. */
  2959. startline = (re->flags & PCRE_STARTLINE) != 0;
  2960. firstline = (re->options & PCRE_FIRSTLINE) != 0;
  2961. /* Set up the first character to match, if available. The first_byte value is
  2962. never set for an anchored regular expression, but the anchoring may be forced
  2963. at run time, so we have to test for anchoring. The first char may be unset for
  2964. an unanchored pattern, of course. If there's no first char and the pattern was
  2965. studied, there may be a bitmap of possible first characters. */
  2966. if (!anchored)
  2967. {
  2968. if ((re->flags & PCRE_FIRSTSET) != 0)
  2969. {
  2970. has_first_char = TRUE;
  2971. first_char = first_char2 = (pcre_uchar)(re->first_char);
  2972. if ((re->flags & PCRE_FCH_CASELESS) != 0)
  2973. {
  2974. first_char2 = TABLE_GET(first_char, md->tables + fcc_offset, first_char);
  2975. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  2976. if (utf && first_char > 127)
  2977. first_char2 = UCD_OTHERCASE(first_char);
  2978. #endif
  2979. }
  2980. }
  2981. else
  2982. {
  2983. if (!startline && study != NULL &&
  2984. (study->flags & PCRE_STUDY_MAPPED) != 0)
  2985. start_bits = study->start_bits;
  2986. }
  2987. }
  2988. /* For anchored or unanchored matches, there may be a "last known required
  2989. character" set. */
  2990. if ((re->flags & PCRE_REQCHSET) != 0)
  2991. {
  2992. has_req_char = TRUE;
  2993. req_char = req_char2 = (pcre_uchar)(re->req_char);
  2994. if ((re->flags & PCRE_RCH_CASELESS) != 0)
  2995. {
  2996. req_char2 = TABLE_GET(req_char, md->tables + fcc_offset, req_char);
  2997. #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
  2998. if (utf && req_char > 127)
  2999. req_char2 = UCD_OTHERCASE(req_char);
  3000. #endif
  3001. }
  3002. }
  3003. /* Call the main matching function, looping for a non-anchored regex after a
  3004. failed match. If not restarting, perform certain optimizations at the start of
  3005. a match. */
  3006. for (;;)
  3007. {
  3008. int rc;
  3009. if ((options & PCRE_DFA_RESTART) == 0)
  3010. {
  3011. const pcre_uchar *save_end_subject = end_subject;
  3012. /* If firstline is TRUE, the start of the match is constrained to the first
  3013. line of a multiline string. Implement this by temporarily adjusting
  3014. end_subject so that we stop scanning at a newline. If the match fails at
  3015. the newline, later code breaks this loop. */
  3016. if (firstline)
  3017. {
  3018. PCRE_PUCHAR t = current_subject;
  3019. #ifdef SUPPORT_UTF
  3020. if (utf)
  3021. {
  3022. while (t < md->end_subject && !IS_NEWLINE(t))
  3023. {
  3024. t++;
  3025. ACROSSCHAR(t < end_subject, *t, t++);
  3026. }
  3027. }
  3028. else
  3029. #endif
  3030. while (t < md->end_subject && !IS_NEWLINE(t)) t++;
  3031. end_subject = t;
  3032. }
  3033. /* There are some optimizations that avoid running the match if a known
  3034. starting point is not found. However, there is an option that disables
  3035. these, for testing and for ensuring that all callouts do actually occur.
  3036. The option can be set in the regex by (*NO_START_OPT) or passed in
  3037. match-time options. */
  3038. if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
  3039. {
  3040. /* Advance to a known first pcre_uchar (i.e. data item) */
  3041. if (has_first_char)
  3042. {
  3043. if (first_char != first_char2)
  3044. {
  3045. pcre_uchar csc;
  3046. while (current_subject < end_subject &&
  3047. (csc = UCHAR21TEST(current_subject)) != first_char && csc != first_char2)
  3048. current_subject++;
  3049. }
  3050. else
  3051. while (current_subject < end_subject &&
  3052. UCHAR21TEST(current_subject) != first_char)
  3053. current_subject++;
  3054. }
  3055. /* Or to just after a linebreak for a multiline match if possible */
  3056. else if (startline)
  3057. {
  3058. if (current_subject > md->start_subject + start_offset)
  3059. {
  3060. #ifdef SUPPORT_UTF
  3061. if (utf)
  3062. {
  3063. while (current_subject < end_subject &&
  3064. !WAS_NEWLINE(current_subject))
  3065. {
  3066. current_subject++;
  3067. ACROSSCHAR(current_subject < end_subject, *current_subject,
  3068. current_subject++);
  3069. }
  3070. }
  3071. else
  3072. #endif
  3073. while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
  3074. current_subject++;
  3075. /* If we have just passed a CR and the newline option is ANY or
  3076. ANYCRLF, and we are now at a LF, advance the match position by one
  3077. more character. */
  3078. if (UCHAR21TEST(current_subject - 1) == CHAR_CR &&
  3079. (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
  3080. current_subject < end_subject &&
  3081. UCHAR21TEST(current_subject) == CHAR_NL)
  3082. current_subject++;
  3083. }
  3084. }
  3085. /* Advance to a non-unique first pcre_uchar after study */
  3086. else if (start_bits != NULL)
  3087. {
  3088. while (current_subject < end_subject)
  3089. {
  3090. register pcre_uint32 c = UCHAR21TEST(current_subject);
  3091. #ifndef COMPILE_PCRE8
  3092. if (c > 255) c = 255;
  3093. #endif
  3094. if ((start_bits[c/8] & (1 << (c&7))) != 0) break;
  3095. current_subject++;
  3096. }
  3097. }
  3098. }
  3099. /* Restore fudged end_subject */
  3100. end_subject = save_end_subject;
  3101. /* The following two optimizations are disabled for partial matching or if
  3102. disabling is explicitly requested (and of course, by the test above, this
  3103. code is not obeyed when restarting after a partial match). */
  3104. if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 &&
  3105. (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
  3106. {
  3107. /* If the pattern was studied, a minimum subject length may be set. This
  3108. is a lower bound; no actual string of that length may actually match the
  3109. pattern. Although the value is, strictly, in characters, we treat it as
  3110. in pcre_uchar units to avoid spending too much time in this optimization.
  3111. */
  3112. if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
  3113. (pcre_uint32)(end_subject - current_subject) < study->minlength)
  3114. return PCRE_ERROR_NOMATCH;
  3115. /* If req_char is set, we know that that pcre_uchar must appear in the
  3116. subject for the match to succeed. If the first pcre_uchar is set,
  3117. req_char must be later in the subject; otherwise the test starts at the
  3118. match point. This optimization can save a huge amount of work in patterns
  3119. with nested unlimited repeats that aren't going to match. Writing
  3120. separate code for cased/caseless versions makes it go faster, as does
  3121. using an autoincrement and backing off on a match.
  3122. HOWEVER: when the subject string is very, very long, searching to its end
  3123. can take a long time, and give bad performance on quite ordinary
  3124. patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
  3125. string... so we don't do this when the string is sufficiently long. */
  3126. if (has_req_char && end_subject - current_subject < REQ_BYTE_MAX)
  3127. {
  3128. register PCRE_PUCHAR p = current_subject + (has_first_char? 1:0);
  3129. /* We don't need to repeat the search if we haven't yet reached the
  3130. place we found it at last time. */
  3131. if (p > req_char_ptr)
  3132. {
  3133. if (req_char != req_char2)
  3134. {
  3135. while (p < end_subject)
  3136. {
  3137. register pcre_uint32 pp = UCHAR21INCTEST(p);
  3138. if (pp == req_char || pp == req_char2) { p--; break; }
  3139. }
  3140. }
  3141. else
  3142. {
  3143. while (p < end_subject)
  3144. {
  3145. if (UCHAR21INCTEST(p) == req_char) { p--; break; }
  3146. }
  3147. }
  3148. /* If we can't find the required pcre_uchar, break the matching loop,
  3149. which will cause a return or PCRE_ERROR_NOMATCH. */
  3150. if (p >= end_subject) break;
  3151. /* If we have found the required pcre_uchar, save the point where we
  3152. found it, so that we don't search again next time round the loop if
  3153. the start hasn't passed this point yet. */
  3154. req_char_ptr = p;
  3155. }
  3156. }
  3157. }
  3158. } /* End of optimizations that are done when not restarting */
  3159. /* OK, now we can do the business */
  3160. md->start_used_ptr = current_subject;
  3161. md->recursive = NULL;
  3162. rc = internal_dfa_exec(
  3163. md, /* fixed match data */
  3164. md->start_code, /* this subexpression's code */
  3165. current_subject, /* where we currently are */
  3166. start_offset, /* start offset in subject */
  3167. offsets, /* offset vector */
  3168. offsetcount, /* size of same */
  3169. workspace, /* workspace vector */
  3170. wscount, /* size of same */
  3171. 0); /* function recurse level */
  3172. /* Anything other than "no match" means we are done, always; otherwise, carry
  3173. on only if not anchored. */
  3174. if (rc != PCRE_ERROR_NOMATCH || anchored)
  3175. {
  3176. if (rc == PCRE_ERROR_PARTIAL && offsetcount >= 2)
  3177. {
  3178. offsets[0] = (int)(md->start_used_ptr - (PCRE_PUCHAR)subject);
  3179. offsets[1] = (int)(end_subject - (PCRE_PUCHAR)subject);
  3180. if (offsetcount > 2)
  3181. offsets[2] = (int)(current_subject - (PCRE_PUCHAR)subject);
  3182. }
  3183. return rc;
  3184. }
  3185. /* Advance to the next subject character unless we are at the end of a line
  3186. and firstline is set. */
  3187. if (firstline && IS_NEWLINE(current_subject)) break;
  3188. current_subject++;
  3189. #ifdef SUPPORT_UTF
  3190. if (utf)
  3191. {
  3192. ACROSSCHAR(current_subject < end_subject, *current_subject,
  3193. current_subject++);
  3194. }
  3195. #endif
  3196. if (current_subject > end_subject) break;
  3197. /* If we have just passed a CR and we are now at a LF, and the pattern does
  3198. not contain any explicit matches for \r or \n, and the newline option is CRLF
  3199. or ANY or ANYCRLF, advance the match position by one more character. */
  3200. if (UCHAR21TEST(current_subject - 1) == CHAR_CR &&
  3201. current_subject < end_subject &&
  3202. UCHAR21TEST(current_subject) == CHAR_NL &&
  3203. (re->flags & PCRE_HASCRORLF) == 0 &&
  3204. (md->nltype == NLTYPE_ANY ||
  3205. md->nltype == NLTYPE_ANYCRLF ||
  3206. md->nllen == 2))
  3207. current_subject++;
  3208. } /* "Bumpalong" loop */
  3209. return PCRE_ERROR_NOMATCH;
  3210. }
  3211. /* End of pcre_dfa_exec.c */