pcre2_dfa_match.c 136 KB

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