string.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Stig S�ther Bakken <ssb@php.net> |
  17. | Zeev Suraski <zeev@zend.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include <stdio.h>
  22. #include "php.h"
  23. #include "php_rand.h"
  24. #include "php_string.h"
  25. #include "php_variables.h"
  26. #ifdef HAVE_LOCALE_H
  27. # include <locale.h>
  28. #endif
  29. #ifdef HAVE_LANGINFO_H
  30. # include <langinfo.h>
  31. #endif
  32. #ifdef HAVE_MONETARY_H
  33. # include <monetary.h>
  34. #endif
  35. /*
  36. * This define is here because some versions of libintl redefine setlocale
  37. * to point to libintl_setlocale. That's a ridiculous thing to do as far
  38. * as I am concerned, but with this define and the subsequent undef we
  39. * limit the damage to just the actual setlocale() call in this file
  40. * without turning zif_setlocale into zif_libintl_setlocale. -Rasmus
  41. */
  42. #define php_my_setlocale setlocale
  43. #ifdef HAVE_LIBINTL
  44. # include <libintl.h> /* For LC_MESSAGES */
  45. #ifdef setlocale
  46. # undef setlocale
  47. #endif
  48. #endif
  49. #include "scanf.h"
  50. #include "zend_API.h"
  51. #include "zend_execute.h"
  52. #include "php_globals.h"
  53. #include "basic_functions.h"
  54. #include "php_smart_str.h"
  55. #include <Zend/zend_exceptions.h>
  56. #ifdef ZTS
  57. #include "TSRM.h"
  58. #endif
  59. /* For str_getcsv() support */
  60. #include "ext/standard/file.h"
  61. #define STR_PAD_LEFT 0
  62. #define STR_PAD_RIGHT 1
  63. #define STR_PAD_BOTH 2
  64. #define PHP_PATHINFO_DIRNAME 1
  65. #define PHP_PATHINFO_BASENAME 2
  66. #define PHP_PATHINFO_EXTENSION 4
  67. #define PHP_PATHINFO_FILENAME 8
  68. #define PHP_PATHINFO_ALL (PHP_PATHINFO_DIRNAME | PHP_PATHINFO_BASENAME | PHP_PATHINFO_EXTENSION | PHP_PATHINFO_FILENAME)
  69. #define STR_STRSPN 0
  70. #define STR_STRCSPN 1
  71. /* {{{ register_string_constants
  72. */
  73. void register_string_constants(INIT_FUNC_ARGS)
  74. {
  75. REGISTER_LONG_CONSTANT("STR_PAD_LEFT", STR_PAD_LEFT, CONST_CS | CONST_PERSISTENT);
  76. REGISTER_LONG_CONSTANT("STR_PAD_RIGHT", STR_PAD_RIGHT, CONST_CS | CONST_PERSISTENT);
  77. REGISTER_LONG_CONSTANT("STR_PAD_BOTH", STR_PAD_BOTH, CONST_CS | CONST_PERSISTENT);
  78. REGISTER_LONG_CONSTANT("PATHINFO_DIRNAME", PHP_PATHINFO_DIRNAME, CONST_CS | CONST_PERSISTENT);
  79. REGISTER_LONG_CONSTANT("PATHINFO_BASENAME", PHP_PATHINFO_BASENAME, CONST_CS | CONST_PERSISTENT);
  80. REGISTER_LONG_CONSTANT("PATHINFO_EXTENSION", PHP_PATHINFO_EXTENSION, CONST_CS | CONST_PERSISTENT);
  81. REGISTER_LONG_CONSTANT("PATHINFO_FILENAME", PHP_PATHINFO_FILENAME, CONST_CS | CONST_PERSISTENT);
  82. #ifdef HAVE_LOCALECONV
  83. /* If last members of struct lconv equal CHAR_MAX, no grouping is done */
  84. /* This is bad, but since we are going to be hardcoding in the POSIX stuff anyway... */
  85. # ifndef HAVE_LIMITS_H
  86. # define CHAR_MAX 127
  87. # endif
  88. REGISTER_LONG_CONSTANT("CHAR_MAX", CHAR_MAX, CONST_CS | CONST_PERSISTENT);
  89. #endif
  90. #ifdef HAVE_LOCALE_H
  91. REGISTER_LONG_CONSTANT("LC_CTYPE", LC_CTYPE, CONST_CS | CONST_PERSISTENT);
  92. REGISTER_LONG_CONSTANT("LC_NUMERIC", LC_NUMERIC, CONST_CS | CONST_PERSISTENT);
  93. REGISTER_LONG_CONSTANT("LC_TIME", LC_TIME, CONST_CS | CONST_PERSISTENT);
  94. REGISTER_LONG_CONSTANT("LC_COLLATE", LC_COLLATE, CONST_CS | CONST_PERSISTENT);
  95. REGISTER_LONG_CONSTANT("LC_MONETARY", LC_MONETARY, CONST_CS | CONST_PERSISTENT);
  96. REGISTER_LONG_CONSTANT("LC_ALL", LC_ALL, CONST_CS | CONST_PERSISTENT);
  97. # ifdef LC_MESSAGES
  98. REGISTER_LONG_CONSTANT("LC_MESSAGES", LC_MESSAGES, CONST_CS | CONST_PERSISTENT);
  99. # endif
  100. #endif
  101. }
  102. /* }}} */
  103. int php_tag_find(char *tag, int len, char *set);
  104. /* this is read-only, so it's ok */
  105. static char hexconvtab[] = "0123456789abcdef";
  106. /* localeconv mutex */
  107. #ifdef ZTS
  108. static MUTEX_T locale_mutex = NULL;
  109. #endif
  110. /* {{{ php_bin2hex
  111. */
  112. static char *php_bin2hex(const unsigned char *old, const size_t oldlen, size_t *newlen)
  113. {
  114. register unsigned char *result = NULL;
  115. size_t i, j;
  116. if (UNEXPECTED(oldlen * 2 * sizeof(char) > INT_MAX)) {
  117. zend_error(E_ERROR, "String size overflow");
  118. }
  119. result = (unsigned char *) safe_emalloc(oldlen, 2 * sizeof(char), 1);
  120. for (i = j = 0; i < oldlen; i++) {
  121. result[j++] = hexconvtab[old[i] >> 4];
  122. result[j++] = hexconvtab[old[i] & 15];
  123. }
  124. result[j] = '\0';
  125. if (newlen)
  126. *newlen = oldlen * 2 * sizeof(char);
  127. return (char *)result;
  128. }
  129. /* }}} */
  130. /* {{{ php_hex2bin
  131. */
  132. static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen)
  133. {
  134. size_t target_length = oldlen >> 1;
  135. register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1);
  136. size_t i, j;
  137. for (i = j = 0; i < target_length; i++) {
  138. char c = old[j++];
  139. if (c >= '0' && c <= '9') {
  140. str[i] = (c - '0') << 4;
  141. } else if (c >= 'a' && c <= 'f') {
  142. str[i] = (c - 'a' + 10) << 4;
  143. } else if (c >= 'A' && c <= 'F') {
  144. str[i] = (c - 'A' + 10) << 4;
  145. } else {
  146. efree(str);
  147. return NULL;
  148. }
  149. c = old[j++];
  150. if (c >= '0' && c <= '9') {
  151. str[i] |= c - '0';
  152. } else if (c >= 'a' && c <= 'f') {
  153. str[i] |= c - 'a' + 10;
  154. } else if (c >= 'A' && c <= 'F') {
  155. str[i] |= c - 'A' + 10;
  156. } else {
  157. efree(str);
  158. return NULL;
  159. }
  160. }
  161. str[target_length] = '\0';
  162. if (newlen)
  163. *newlen = target_length;
  164. return (char *)str;
  165. }
  166. /* }}} */
  167. #ifdef HAVE_LOCALECONV
  168. /* {{{ localeconv_r
  169. * glibc's localeconv is not reentrant, so lets make it so ... sorta */
  170. PHPAPI struct lconv *localeconv_r(struct lconv *out)
  171. {
  172. # ifdef ZTS
  173. tsrm_mutex_lock( locale_mutex );
  174. # endif
  175. #if defined(PHP_WIN32) && defined(ZTS)
  176. {
  177. /* Even with the enabled per thread locale, localeconv
  178. won't check any locale change in the master thread. */
  179. _locale_t cur = _get_current_locale();
  180. *out = *cur->locinfo->lconv;
  181. _free_locale(cur);
  182. }
  183. #else
  184. /* localeconv doesn't return an error condition */
  185. *out = *localeconv();
  186. #endif
  187. # ifdef ZTS
  188. tsrm_mutex_unlock( locale_mutex );
  189. # endif
  190. return out;
  191. }
  192. /* }}} */
  193. # ifdef ZTS
  194. /* {{{ PHP_MINIT_FUNCTION
  195. */
  196. PHP_MINIT_FUNCTION(localeconv)
  197. {
  198. locale_mutex = tsrm_mutex_alloc();
  199. return SUCCESS;
  200. }
  201. /* }}} */
  202. /* {{{ PHP_MSHUTDOWN_FUNCTION
  203. */
  204. PHP_MSHUTDOWN_FUNCTION(localeconv)
  205. {
  206. tsrm_mutex_free( locale_mutex );
  207. locale_mutex = NULL;
  208. return SUCCESS;
  209. }
  210. /* }}} */
  211. # endif
  212. #endif
  213. /* {{{ proto string bin2hex(string data)
  214. Converts the binary representation of data to hex */
  215. PHP_FUNCTION(bin2hex)
  216. {
  217. char *result, *data;
  218. size_t newlen;
  219. int datalen;
  220. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) {
  221. return;
  222. }
  223. result = php_bin2hex((unsigned char *)data, datalen, &newlen);
  224. if (!result) {
  225. RETURN_FALSE;
  226. }
  227. RETURN_STRINGL(result, newlen, 0);
  228. }
  229. /* }}} */
  230. /* {{{ proto string hex2bin(string data)
  231. Converts the hex representation of data to binary */
  232. PHP_FUNCTION(hex2bin)
  233. {
  234. char *result, *data;
  235. size_t newlen;
  236. int datalen;
  237. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) {
  238. return;
  239. }
  240. if (datalen % 2 != 0) {
  241. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Hexadecimal input string must have an even length");
  242. RETURN_FALSE;
  243. }
  244. result = php_hex2bin((unsigned char *)data, datalen, &newlen);
  245. if (!result) {
  246. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input string must be hexadecimal string");
  247. RETURN_FALSE;
  248. }
  249. RETURN_STRINGL(result, newlen, 0);
  250. }
  251. /* }}} */
  252. static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */
  253. {
  254. char *s11, *s22;
  255. int len1, len2;
  256. long start = 0, len = 0;
  257. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &s11, &len1,
  258. &s22, &len2, &start, &len) == FAILURE) {
  259. return;
  260. }
  261. if (ZEND_NUM_ARGS() < 4) {
  262. len = len1;
  263. }
  264. /* look at substr() function for more information */
  265. if (start < 0) {
  266. start += len1;
  267. if (start < 0) {
  268. start = 0;
  269. }
  270. } else if (start > len1) {
  271. RETURN_FALSE;
  272. }
  273. if (len < 0) {
  274. len += (len1 - start);
  275. if (len < 0) {
  276. len = 0;
  277. }
  278. }
  279. if (len > len1 - start) {
  280. len = len1 - start;
  281. }
  282. if(len == 0) {
  283. RETURN_LONG(0);
  284. }
  285. if (behavior == STR_STRSPN) {
  286. RETURN_LONG(php_strspn(s11 + start /*str1_start*/,
  287. s22 /*str2_start*/,
  288. s11 + start + len /*str1_end*/,
  289. s22 + len2 /*str2_end*/));
  290. } else if (behavior == STR_STRCSPN) {
  291. RETURN_LONG(php_strcspn(s11 + start /*str1_start*/,
  292. s22 /*str2_start*/,
  293. s11 + start + len /*str1_end*/,
  294. s22 + len2 /*str2_end*/));
  295. }
  296. }
  297. /* }}} */
  298. /* {{{ proto int strspn(string str, string mask [, start [, len]])
  299. Finds length of initial segment consisting entirely of characters found in mask. If start or/and length is provided works like strspn(substr($s,$start,$len),$good_chars) */
  300. PHP_FUNCTION(strspn)
  301. {
  302. php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRSPN);
  303. }
  304. /* }}} */
  305. /* {{{ proto int strcspn(string str, string mask [, start [, len]])
  306. Finds length of initial segment consisting entirely of characters not found in mask. If start or/and length is provide works like strcspn(substr($s,$start,$len),$bad_chars) */
  307. PHP_FUNCTION(strcspn)
  308. {
  309. php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRCSPN);
  310. }
  311. /* }}} */
  312. /* {{{ PHP_MINIT_FUNCTION(nl_langinfo) */
  313. #if HAVE_NL_LANGINFO
  314. PHP_MINIT_FUNCTION(nl_langinfo)
  315. {
  316. #define REGISTER_NL_LANGINFO_CONSTANT(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
  317. #ifdef ABDAY_1
  318. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_1);
  319. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_2);
  320. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_3);
  321. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_4);
  322. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_5);
  323. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_6);
  324. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_7);
  325. #endif
  326. #ifdef DAY_1
  327. REGISTER_NL_LANGINFO_CONSTANT(DAY_1);
  328. REGISTER_NL_LANGINFO_CONSTANT(DAY_2);
  329. REGISTER_NL_LANGINFO_CONSTANT(DAY_3);
  330. REGISTER_NL_LANGINFO_CONSTANT(DAY_4);
  331. REGISTER_NL_LANGINFO_CONSTANT(DAY_5);
  332. REGISTER_NL_LANGINFO_CONSTANT(DAY_6);
  333. REGISTER_NL_LANGINFO_CONSTANT(DAY_7);
  334. #endif
  335. #ifdef ABMON_1
  336. REGISTER_NL_LANGINFO_CONSTANT(ABMON_1);
  337. REGISTER_NL_LANGINFO_CONSTANT(ABMON_2);
  338. REGISTER_NL_LANGINFO_CONSTANT(ABMON_3);
  339. REGISTER_NL_LANGINFO_CONSTANT(ABMON_4);
  340. REGISTER_NL_LANGINFO_CONSTANT(ABMON_5);
  341. REGISTER_NL_LANGINFO_CONSTANT(ABMON_6);
  342. REGISTER_NL_LANGINFO_CONSTANT(ABMON_7);
  343. REGISTER_NL_LANGINFO_CONSTANT(ABMON_8);
  344. REGISTER_NL_LANGINFO_CONSTANT(ABMON_9);
  345. REGISTER_NL_LANGINFO_CONSTANT(ABMON_10);
  346. REGISTER_NL_LANGINFO_CONSTANT(ABMON_11);
  347. REGISTER_NL_LANGINFO_CONSTANT(ABMON_12);
  348. #endif
  349. #ifdef MON_1
  350. REGISTER_NL_LANGINFO_CONSTANT(MON_1);
  351. REGISTER_NL_LANGINFO_CONSTANT(MON_2);
  352. REGISTER_NL_LANGINFO_CONSTANT(MON_3);
  353. REGISTER_NL_LANGINFO_CONSTANT(MON_4);
  354. REGISTER_NL_LANGINFO_CONSTANT(MON_5);
  355. REGISTER_NL_LANGINFO_CONSTANT(MON_6);
  356. REGISTER_NL_LANGINFO_CONSTANT(MON_7);
  357. REGISTER_NL_LANGINFO_CONSTANT(MON_8);
  358. REGISTER_NL_LANGINFO_CONSTANT(MON_9);
  359. REGISTER_NL_LANGINFO_CONSTANT(MON_10);
  360. REGISTER_NL_LANGINFO_CONSTANT(MON_11);
  361. REGISTER_NL_LANGINFO_CONSTANT(MON_12);
  362. #endif
  363. #ifdef AM_STR
  364. REGISTER_NL_LANGINFO_CONSTANT(AM_STR);
  365. #endif
  366. #ifdef PM_STR
  367. REGISTER_NL_LANGINFO_CONSTANT(PM_STR);
  368. #endif
  369. #ifdef D_T_FMT
  370. REGISTER_NL_LANGINFO_CONSTANT(D_T_FMT);
  371. #endif
  372. #ifdef D_FMT
  373. REGISTER_NL_LANGINFO_CONSTANT(D_FMT);
  374. #endif
  375. #ifdef T_FMT
  376. REGISTER_NL_LANGINFO_CONSTANT(T_FMT);
  377. #endif
  378. #ifdef T_FMT_AMPM
  379. REGISTER_NL_LANGINFO_CONSTANT(T_FMT_AMPM);
  380. #endif
  381. #ifdef ERA
  382. REGISTER_NL_LANGINFO_CONSTANT(ERA);
  383. #endif
  384. #ifdef ERA_YEAR
  385. REGISTER_NL_LANGINFO_CONSTANT(ERA_YEAR);
  386. #endif
  387. #ifdef ERA_D_T_FMT
  388. REGISTER_NL_LANGINFO_CONSTANT(ERA_D_T_FMT);
  389. #endif
  390. #ifdef ERA_D_FMT
  391. REGISTER_NL_LANGINFO_CONSTANT(ERA_D_FMT);
  392. #endif
  393. #ifdef ERA_T_FMT
  394. REGISTER_NL_LANGINFO_CONSTANT(ERA_T_FMT);
  395. #endif
  396. #ifdef ALT_DIGITS
  397. REGISTER_NL_LANGINFO_CONSTANT(ALT_DIGITS);
  398. #endif
  399. #ifdef INT_CURR_SYMBOL
  400. REGISTER_NL_LANGINFO_CONSTANT(INT_CURR_SYMBOL);
  401. #endif
  402. #ifdef CURRENCY_SYMBOL
  403. REGISTER_NL_LANGINFO_CONSTANT(CURRENCY_SYMBOL);
  404. #endif
  405. #ifdef CRNCYSTR
  406. REGISTER_NL_LANGINFO_CONSTANT(CRNCYSTR);
  407. #endif
  408. #ifdef MON_DECIMAL_POINT
  409. REGISTER_NL_LANGINFO_CONSTANT(MON_DECIMAL_POINT);
  410. #endif
  411. #ifdef MON_THOUSANDS_SEP
  412. REGISTER_NL_LANGINFO_CONSTANT(MON_THOUSANDS_SEP);
  413. #endif
  414. #ifdef MON_GROUPING
  415. REGISTER_NL_LANGINFO_CONSTANT(MON_GROUPING);
  416. #endif
  417. #ifdef POSITIVE_SIGN
  418. REGISTER_NL_LANGINFO_CONSTANT(POSITIVE_SIGN);
  419. #endif
  420. #ifdef NEGATIVE_SIGN
  421. REGISTER_NL_LANGINFO_CONSTANT(NEGATIVE_SIGN);
  422. #endif
  423. #ifdef INT_FRAC_DIGITS
  424. REGISTER_NL_LANGINFO_CONSTANT(INT_FRAC_DIGITS);
  425. #endif
  426. #ifdef FRAC_DIGITS
  427. REGISTER_NL_LANGINFO_CONSTANT(FRAC_DIGITS);
  428. #endif
  429. #ifdef P_CS_PRECEDES
  430. REGISTER_NL_LANGINFO_CONSTANT(P_CS_PRECEDES);
  431. #endif
  432. #ifdef P_SEP_BY_SPACE
  433. REGISTER_NL_LANGINFO_CONSTANT(P_SEP_BY_SPACE);
  434. #endif
  435. #ifdef N_CS_PRECEDES
  436. REGISTER_NL_LANGINFO_CONSTANT(N_CS_PRECEDES);
  437. #endif
  438. #ifdef N_SEP_BY_SPACE
  439. REGISTER_NL_LANGINFO_CONSTANT(N_SEP_BY_SPACE);
  440. #endif
  441. #ifdef P_SIGN_POSN
  442. REGISTER_NL_LANGINFO_CONSTANT(P_SIGN_POSN);
  443. #endif
  444. #ifdef N_SIGN_POSN
  445. REGISTER_NL_LANGINFO_CONSTANT(N_SIGN_POSN);
  446. #endif
  447. #ifdef DECIMAL_POINT
  448. REGISTER_NL_LANGINFO_CONSTANT(DECIMAL_POINT);
  449. #endif
  450. #ifdef RADIXCHAR
  451. REGISTER_NL_LANGINFO_CONSTANT(RADIXCHAR);
  452. #endif
  453. #ifdef THOUSANDS_SEP
  454. REGISTER_NL_LANGINFO_CONSTANT(THOUSANDS_SEP);
  455. #endif
  456. #ifdef THOUSEP
  457. REGISTER_NL_LANGINFO_CONSTANT(THOUSEP);
  458. #endif
  459. #ifdef GROUPING
  460. REGISTER_NL_LANGINFO_CONSTANT(GROUPING);
  461. #endif
  462. #ifdef YESEXPR
  463. REGISTER_NL_LANGINFO_CONSTANT(YESEXPR);
  464. #endif
  465. #ifdef NOEXPR
  466. REGISTER_NL_LANGINFO_CONSTANT(NOEXPR);
  467. #endif
  468. #ifdef YESSTR
  469. REGISTER_NL_LANGINFO_CONSTANT(YESSTR);
  470. #endif
  471. #ifdef NOSTR
  472. REGISTER_NL_LANGINFO_CONSTANT(NOSTR);
  473. #endif
  474. #ifdef CODESET
  475. REGISTER_NL_LANGINFO_CONSTANT(CODESET);
  476. #endif
  477. #undef REGISTER_NL_LANGINFO_CONSTANT
  478. return SUCCESS;
  479. }
  480. /* }}} */
  481. /* {{{ proto string nl_langinfo(int item)
  482. Query language and locale information */
  483. PHP_FUNCTION(nl_langinfo)
  484. {
  485. long item;
  486. char *value;
  487. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &item) == FAILURE) {
  488. return;
  489. }
  490. switch(item) { /* {{{ */
  491. #ifdef ABDAY_1
  492. case ABDAY_1:
  493. case ABDAY_2:
  494. case ABDAY_3:
  495. case ABDAY_4:
  496. case ABDAY_5:
  497. case ABDAY_6:
  498. case ABDAY_7:
  499. #endif
  500. #ifdef DAY_1
  501. case DAY_1:
  502. case DAY_2:
  503. case DAY_3:
  504. case DAY_4:
  505. case DAY_5:
  506. case DAY_6:
  507. case DAY_7:
  508. #endif
  509. #ifdef ABMON_1
  510. case ABMON_1:
  511. case ABMON_2:
  512. case ABMON_3:
  513. case ABMON_4:
  514. case ABMON_5:
  515. case ABMON_6:
  516. case ABMON_7:
  517. case ABMON_8:
  518. case ABMON_9:
  519. case ABMON_10:
  520. case ABMON_11:
  521. case ABMON_12:
  522. #endif
  523. #ifdef MON_1
  524. case MON_1:
  525. case MON_2:
  526. case MON_3:
  527. case MON_4:
  528. case MON_5:
  529. case MON_6:
  530. case MON_7:
  531. case MON_8:
  532. case MON_9:
  533. case MON_10:
  534. case MON_11:
  535. case MON_12:
  536. #endif
  537. #ifdef AM_STR
  538. case AM_STR:
  539. #endif
  540. #ifdef PM_STR
  541. case PM_STR:
  542. #endif
  543. #ifdef D_T_FMT
  544. case D_T_FMT:
  545. #endif
  546. #ifdef D_FMT
  547. case D_FMT:
  548. #endif
  549. #ifdef T_FMT
  550. case T_FMT:
  551. #endif
  552. #ifdef T_FMT_AMPM
  553. case T_FMT_AMPM:
  554. #endif
  555. #ifdef ERA
  556. case ERA:
  557. #endif
  558. #ifdef ERA_YEAR
  559. case ERA_YEAR:
  560. #endif
  561. #ifdef ERA_D_T_FMT
  562. case ERA_D_T_FMT:
  563. #endif
  564. #ifdef ERA_D_FMT
  565. case ERA_D_FMT:
  566. #endif
  567. #ifdef ERA_T_FMT
  568. case ERA_T_FMT:
  569. #endif
  570. #ifdef ALT_DIGITS
  571. case ALT_DIGITS:
  572. #endif
  573. #ifdef INT_CURR_SYMBOL
  574. case INT_CURR_SYMBOL:
  575. #endif
  576. #ifdef CURRENCY_SYMBOL
  577. case CURRENCY_SYMBOL:
  578. #endif
  579. #ifdef CRNCYSTR
  580. case CRNCYSTR:
  581. #endif
  582. #ifdef MON_DECIMAL_POINT
  583. case MON_DECIMAL_POINT:
  584. #endif
  585. #ifdef MON_THOUSANDS_SEP
  586. case MON_THOUSANDS_SEP:
  587. #endif
  588. #ifdef MON_GROUPING
  589. case MON_GROUPING:
  590. #endif
  591. #ifdef POSITIVE_SIGN
  592. case POSITIVE_SIGN:
  593. #endif
  594. #ifdef NEGATIVE_SIGN
  595. case NEGATIVE_SIGN:
  596. #endif
  597. #ifdef INT_FRAC_DIGITS
  598. case INT_FRAC_DIGITS:
  599. #endif
  600. #ifdef FRAC_DIGITS
  601. case FRAC_DIGITS:
  602. #endif
  603. #ifdef P_CS_PRECEDES
  604. case P_CS_PRECEDES:
  605. #endif
  606. #ifdef P_SEP_BY_SPACE
  607. case P_SEP_BY_SPACE:
  608. #endif
  609. #ifdef N_CS_PRECEDES
  610. case N_CS_PRECEDES:
  611. #endif
  612. #ifdef N_SEP_BY_SPACE
  613. case N_SEP_BY_SPACE:
  614. #endif
  615. #ifdef P_SIGN_POSN
  616. case P_SIGN_POSN:
  617. #endif
  618. #ifdef N_SIGN_POSN
  619. case N_SIGN_POSN:
  620. #endif
  621. #ifdef DECIMAL_POINT
  622. case DECIMAL_POINT:
  623. #elif defined(RADIXCHAR)
  624. case RADIXCHAR:
  625. #endif
  626. #ifdef THOUSANDS_SEP
  627. case THOUSANDS_SEP:
  628. #elif defined(THOUSEP)
  629. case THOUSEP:
  630. #endif
  631. #ifdef GROUPING
  632. case GROUPING:
  633. #endif
  634. #ifdef YESEXPR
  635. case YESEXPR:
  636. #endif
  637. #ifdef NOEXPR
  638. case NOEXPR:
  639. #endif
  640. #ifdef YESSTR
  641. case YESSTR:
  642. #endif
  643. #ifdef NOSTR
  644. case NOSTR:
  645. #endif
  646. #ifdef CODESET
  647. case CODESET:
  648. #endif
  649. break;
  650. default:
  651. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Item '%ld' is not valid", item);
  652. RETURN_FALSE;
  653. }
  654. /* }}} */
  655. value = nl_langinfo(item);
  656. if (value == NULL) {
  657. RETURN_FALSE;
  658. } else {
  659. RETURN_STRING(value, 1);
  660. }
  661. }
  662. #endif
  663. /* }}} */
  664. #ifdef HAVE_STRCOLL
  665. /* {{{ proto int strcoll(string str1, string str2)
  666. Compares two strings using the current locale */
  667. PHP_FUNCTION(strcoll)
  668. {
  669. char *s1, *s2;
  670. int s1len, s2len;
  671. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &s1, &s1len, &s2, &s2len) == FAILURE) {
  672. return;
  673. }
  674. RETURN_LONG(strcoll((const char *) s1,
  675. (const char *) s2));
  676. }
  677. /* }}} */
  678. #endif
  679. /* {{{ php_charmask
  680. * Fills a 256-byte bytemask with input. You can specify a range like 'a..z',
  681. * it needs to be incrementing.
  682. * Returns: FAILURE/SUCCESS whether the input was correct (i.e. no range errors)
  683. */
  684. static inline int php_charmask(unsigned char *input, int len, char *mask TSRMLS_DC)
  685. {
  686. unsigned char *end;
  687. unsigned char c;
  688. int result = SUCCESS;
  689. memset(mask, 0, 256);
  690. for (end = input+len; input < end; input++) {
  691. c=*input;
  692. if ((input+3 < end) && input[1] == '.' && input[2] == '.'
  693. && input[3] >= c) {
  694. memset(mask+c, 1, input[3] - c + 1);
  695. input+=3;
  696. } else if ((input+1 < end) && input[0] == '.' && input[1] == '.') {
  697. /* Error, try to be as helpful as possible:
  698. (a range ending/starting with '.' won't be captured here) */
  699. if (end-len >= input) { /* there was no 'left' char */
  700. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, no character to the left of '..'");
  701. result = FAILURE;
  702. continue;
  703. }
  704. if (input+2 >= end) { /* there is no 'right' char */
  705. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, no character to the right of '..'");
  706. result = FAILURE;
  707. continue;
  708. }
  709. if (input[-1] > input[2]) { /* wrong order */
  710. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range, '..'-range needs to be incrementing");
  711. result = FAILURE;
  712. continue;
  713. }
  714. /* FIXME: better error (a..b..c is the only left possibility?) */
  715. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '..'-range");
  716. result = FAILURE;
  717. continue;
  718. } else {
  719. mask[c]=1;
  720. }
  721. }
  722. return result;
  723. }
  724. /* }}} */
  725. /* {{{ php_trim()
  726. * mode 1 : trim left
  727. * mode 2 : trim right
  728. * mode 3 : trim left and right
  729. * what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
  730. */
  731. PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC)
  732. {
  733. register int i;
  734. int trimmed = 0;
  735. char mask[256];
  736. if (what) {
  737. php_charmask((unsigned char*)what, what_len, mask TSRMLS_CC);
  738. } else {
  739. php_charmask((unsigned char*)" \n\r\t\v\0", 6, mask TSRMLS_CC);
  740. }
  741. if (mode & 1) {
  742. for (i = 0; i < len; i++) {
  743. if (mask[(unsigned char)c[i]]) {
  744. trimmed++;
  745. } else {
  746. break;
  747. }
  748. }
  749. len -= trimmed;
  750. c += trimmed;
  751. }
  752. if (mode & 2) {
  753. for (i = len - 1; i >= 0; i--) {
  754. if (mask[(unsigned char)c[i]]) {
  755. len--;
  756. } else {
  757. break;
  758. }
  759. }
  760. }
  761. if (return_value) {
  762. RETVAL_STRINGL(c, len, 1);
  763. } else {
  764. return estrndup(c, len);
  765. }
  766. return "";
  767. }
  768. /* }}} */
  769. /* {{{ php_do_trim
  770. * Base for trim(), rtrim() and ltrim() functions.
  771. */
  772. static void php_do_trim(INTERNAL_FUNCTION_PARAMETERS, int mode)
  773. {
  774. char *str;
  775. char *what = NULL;
  776. int str_len, what_len = 0;
  777. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &what, &what_len) == FAILURE) {
  778. return;
  779. }
  780. php_trim(str, str_len, what, what_len, return_value, mode TSRMLS_CC);
  781. }
  782. /* }}} */
  783. /* {{{ proto string trim(string str [, string character_mask])
  784. Strips whitespace from the beginning and end of a string */
  785. PHP_FUNCTION(trim)
  786. {
  787. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  788. }
  789. /* }}} */
  790. /* {{{ proto string rtrim(string str [, string character_mask])
  791. Removes trailing whitespace */
  792. PHP_FUNCTION(rtrim)
  793. {
  794. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  795. }
  796. /* }}} */
  797. /* {{{ proto string ltrim(string str [, string character_mask])
  798. Strips whitespace from the beginning of a string */
  799. PHP_FUNCTION(ltrim)
  800. {
  801. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  802. }
  803. /* }}} */
  804. /* {{{ proto string wordwrap(string str [, int width [, string break [, boolean cut]]])
  805. Wraps buffer to selected number of characters using string break char */
  806. PHP_FUNCTION(wordwrap)
  807. {
  808. const char *text, *breakchar = "\n";
  809. char *newtext;
  810. int textlen, breakcharlen = 1, chk;
  811. size_t alloced;
  812. size_t current = 0, laststart = 0, lastspace = 0;
  813. long linelength = 75;
  814. zend_bool docut = 0;
  815. size_t newtextlen;
  816. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lsb", &text, &textlen, &linelength, &breakchar, &breakcharlen, &docut) == FAILURE) {
  817. return;
  818. }
  819. if (textlen == 0) {
  820. RETURN_EMPTY_STRING();
  821. }
  822. if (breakcharlen == 0) {
  823. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Break string cannot be empty");
  824. RETURN_FALSE;
  825. }
  826. if (linelength == 0 && docut) {
  827. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't force cut when width is zero");
  828. RETURN_FALSE;
  829. }
  830. if (linelength < 0) {
  831. /* For BC */
  832. linelength = 0;
  833. }
  834. if (linelength > INT_MAX) {
  835. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length should be between 0 and %d", INT_MAX);
  836. RETURN_FALSE;
  837. }
  838. /* Special case for a single-character break as it needs no
  839. additional storage space */
  840. if (breakcharlen == 1 && !docut) {
  841. newtext = estrndup(text, textlen);
  842. laststart = lastspace = 0;
  843. for (current = 0; current < textlen; current++) {
  844. if (text[current] == breakchar[0]) {
  845. laststart = lastspace = current + 1;
  846. } else if (text[current] == ' ') {
  847. if (current - laststart >= linelength) {
  848. newtext[current] = breakchar[0];
  849. laststart = current + 1;
  850. }
  851. lastspace = current;
  852. } else if (current - laststart >= linelength && laststart != lastspace) {
  853. newtext[lastspace] = breakchar[0];
  854. laststart = lastspace + 1;
  855. }
  856. }
  857. RETURN_STRINGL(newtext, textlen, 0);
  858. } else {
  859. /* Multiple character line break or forced cut */
  860. if (linelength > 0) {
  861. chk = (int)(textlen/linelength + 1);
  862. newtext = safe_emalloc(chk, breakcharlen, textlen + 1);
  863. alloced = (size_t)textlen + chk * (size_t)breakcharlen + 1;
  864. } else {
  865. chk = textlen;
  866. alloced = (size_t)textlen * ((size_t)breakcharlen + 1) + 1;
  867. newtext = safe_emalloc(textlen, (breakcharlen + 1), 1);
  868. }
  869. /* now keep track of the actual new text length */
  870. newtextlen = 0;
  871. laststart = lastspace = 0;
  872. for (current = 0; current < textlen; current++) {
  873. if (chk <= 0) {
  874. alloced += (int) (((textlen - current + 1)/linelength + 1) * breakcharlen) + 1;
  875. newtext = erealloc(newtext, alloced);
  876. chk = (int) ((textlen - current)/linelength) + 1;
  877. }
  878. /* when we hit an existing break, copy to new buffer, and
  879. * fix up laststart and lastspace */
  880. if (text[current] == breakchar[0]
  881. && current + breakcharlen < textlen
  882. && !strncmp(text+current, breakchar, breakcharlen)) {
  883. memcpy(newtext+newtextlen, text+laststart, current-laststart+breakcharlen);
  884. newtextlen += current-laststart+breakcharlen;
  885. current += breakcharlen - 1;
  886. laststart = lastspace = current + 1;
  887. chk--;
  888. }
  889. /* if it is a space, check if it is at the line boundary,
  890. * copy and insert a break, or just keep track of it */
  891. else if (text[current] == ' ') {
  892. if (current - laststart >= linelength) {
  893. memcpy(newtext+newtextlen, text+laststart, current-laststart);
  894. newtextlen += current - laststart;
  895. memcpy(newtext+newtextlen, breakchar, breakcharlen);
  896. newtextlen += breakcharlen;
  897. laststart = current + 1;
  898. chk--;
  899. }
  900. lastspace = current;
  901. }
  902. /* if we are cutting, and we've accumulated enough
  903. * characters, and we haven't see a space for this line,
  904. * copy and insert a break. */
  905. else if (current - laststart >= linelength
  906. && docut && laststart >= lastspace) {
  907. memcpy(newtext+newtextlen, text+laststart, current-laststart);
  908. newtextlen += current - laststart;
  909. memcpy(newtext+newtextlen, breakchar, breakcharlen);
  910. newtextlen += breakcharlen;
  911. laststart = lastspace = current;
  912. chk--;
  913. }
  914. /* if the current word puts us over the linelength, copy
  915. * back up until the last space, insert a break, and move
  916. * up the laststart */
  917. else if (current - laststart >= linelength
  918. && laststart < lastspace) {
  919. memcpy(newtext+newtextlen, text+laststart, lastspace-laststart);
  920. newtextlen += lastspace - laststart;
  921. memcpy(newtext+newtextlen, breakchar, breakcharlen);
  922. newtextlen += breakcharlen;
  923. laststart = lastspace = lastspace + 1;
  924. chk--;
  925. }
  926. }
  927. /* copy over any stragglers */
  928. if (laststart != current) {
  929. memcpy(newtext+newtextlen, text+laststart, current-laststart);
  930. newtextlen += current - laststart;
  931. }
  932. newtext[newtextlen] = '\0';
  933. /* free unused memory */
  934. newtext = erealloc(newtext, newtextlen+1);
  935. RETVAL_STRINGL_CHECK(newtext, newtextlen, 0);
  936. }
  937. }
  938. /* }}} */
  939. /* {{{ php_explode
  940. */
  941. PHPAPI void php_explode(zval *delim, zval *str, zval *return_value, long limit)
  942. {
  943. char *p1, *p2, *endp;
  944. endp = Z_STRVAL_P(str) + Z_STRLEN_P(str);
  945. p1 = Z_STRVAL_P(str);
  946. p2 = php_memnstr(Z_STRVAL_P(str), Z_STRVAL_P(delim), Z_STRLEN_P(delim), endp);
  947. if (p2 == NULL) {
  948. add_next_index_stringl(return_value, p1, Z_STRLEN_P(str), 1);
  949. } else {
  950. do {
  951. add_next_index_stringl(return_value, p1, p2 - p1, 1);
  952. p1 = p2 + Z_STRLEN_P(delim);
  953. } while ((p2 = php_memnstr(p1, Z_STRVAL_P(delim), Z_STRLEN_P(delim), endp)) != NULL &&
  954. --limit > 1);
  955. if (p1 <= endp)
  956. add_next_index_stringl(return_value, p1, endp-p1, 1);
  957. }
  958. }
  959. /* }}} */
  960. /* {{{ php_explode_negative_limit
  961. */
  962. PHPAPI void php_explode_negative_limit(zval *delim, zval *str, zval *return_value, long limit)
  963. {
  964. #define EXPLODE_ALLOC_STEP 64
  965. char *p1, *p2, *endp;
  966. endp = Z_STRVAL_P(str) + Z_STRLEN_P(str);
  967. p1 = Z_STRVAL_P(str);
  968. p2 = php_memnstr(Z_STRVAL_P(str), Z_STRVAL_P(delim), Z_STRLEN_P(delim), endp);
  969. if (p2 == NULL) {
  970. /*
  971. do nothing since limit <= -1, thus if only one chunk - 1 + (limit) <= 0
  972. by doing nothing we return empty array
  973. */
  974. } else {
  975. int allocated = EXPLODE_ALLOC_STEP, found = 0;
  976. long i, to_return;
  977. char **positions = emalloc(allocated * sizeof(char *));
  978. positions[found++] = p1;
  979. do {
  980. if (found >= allocated) {
  981. allocated = found + EXPLODE_ALLOC_STEP;/* make sure we have enough memory */
  982. positions = erealloc(positions, allocated*sizeof(char *));
  983. }
  984. positions[found++] = p1 = p2 + Z_STRLEN_P(delim);
  985. } while ((p2 = php_memnstr(p1, Z_STRVAL_P(delim), Z_STRLEN_P(delim), endp)) != NULL);
  986. to_return = limit + found;
  987. /* limit is at least -1 therefore no need of bounds checking : i will be always less than found */
  988. for (i = 0;i < to_return;i++) { /* this checks also for to_return > 0 */
  989. add_next_index_stringl(return_value, positions[i],
  990. (positions[i+1] - Z_STRLEN_P(delim)) - positions[i],
  991. 1
  992. );
  993. }
  994. efree(positions);
  995. }
  996. #undef EXPLODE_ALLOC_STEP
  997. }
  998. /* }}} */
  999. /* {{{ proto array explode(string separator, string str [, int limit])
  1000. Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned. */
  1001. PHP_FUNCTION(explode)
  1002. {
  1003. char *str, *delim;
  1004. int str_len = 0, delim_len = 0;
  1005. long limit = LONG_MAX; /* No limit */
  1006. zval zdelim, zstr;
  1007. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &delim, &delim_len, &str, &str_len, &limit) == FAILURE) {
  1008. return;
  1009. }
  1010. if (delim_len == 0) {
  1011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
  1012. RETURN_FALSE;
  1013. }
  1014. array_init(return_value);
  1015. if (str_len == 0) {
  1016. if (limit >= 0) {
  1017. add_next_index_stringl(return_value, "", sizeof("") - 1, 1);
  1018. }
  1019. return;
  1020. }
  1021. ZVAL_STRINGL(&zstr, str, str_len, 0);
  1022. ZVAL_STRINGL(&zdelim, delim, delim_len, 0);
  1023. if (limit > 1) {
  1024. php_explode(&zdelim, &zstr, return_value, limit);
  1025. } else if (limit < 0) {
  1026. php_explode_negative_limit(&zdelim, &zstr, return_value, limit);
  1027. } else {
  1028. add_index_stringl(return_value, 0, str, str_len, 1);
  1029. }
  1030. }
  1031. /* }}} */
  1032. /* {{{ proto string join(array src, string glue)
  1033. An alias for implode */
  1034. /* }}} */
  1035. /* {{{ php_implode
  1036. */
  1037. PHPAPI void php_implode(zval *delim, zval *arr, zval *return_value TSRMLS_DC)
  1038. {
  1039. zval **tmp;
  1040. HashPosition pos;
  1041. smart_str implstr = {0};
  1042. int numelems, i = 0;
  1043. zval tmp_val;
  1044. int str_len;
  1045. numelems = zend_hash_num_elements(Z_ARRVAL_P(arr));
  1046. if (numelems == 0) {
  1047. RETURN_EMPTY_STRING();
  1048. }
  1049. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
  1050. while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **) &tmp, &pos) == SUCCESS) {
  1051. switch ((*tmp)->type) {
  1052. case IS_STRING:
  1053. smart_str_appendl(&implstr, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
  1054. break;
  1055. case IS_LONG: {
  1056. char stmp[MAX_LENGTH_OF_LONG + 1];
  1057. str_len = slprintf(stmp, sizeof(stmp), "%ld", Z_LVAL_PP(tmp));
  1058. smart_str_appendl(&implstr, stmp, str_len);
  1059. }
  1060. break;
  1061. case IS_BOOL:
  1062. if (Z_LVAL_PP(tmp) == 1) {
  1063. smart_str_appendl(&implstr, "1", sizeof("1")-1);
  1064. }
  1065. break;
  1066. case IS_NULL:
  1067. break;
  1068. case IS_DOUBLE: {
  1069. char *stmp;
  1070. str_len = spprintf(&stmp, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(tmp));
  1071. smart_str_appendl(&implstr, stmp, str_len);
  1072. efree(stmp);
  1073. }
  1074. break;
  1075. case IS_OBJECT: {
  1076. int copy;
  1077. zval expr;
  1078. zend_make_printable_zval(*tmp, &expr, &copy);
  1079. smart_str_appendl(&implstr, Z_STRVAL(expr), Z_STRLEN(expr));
  1080. if (copy) {
  1081. zval_dtor(&expr);
  1082. }
  1083. }
  1084. break;
  1085. default:
  1086. tmp_val = **tmp;
  1087. zval_copy_ctor(&tmp_val);
  1088. convert_to_string(&tmp_val);
  1089. smart_str_appendl(&implstr, Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
  1090. zval_dtor(&tmp_val);
  1091. break;
  1092. }
  1093. if (++i != numelems) {
  1094. smart_str_appendl(&implstr, Z_STRVAL_P(delim), Z_STRLEN_P(delim));
  1095. }
  1096. zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
  1097. }
  1098. smart_str_0(&implstr);
  1099. if (implstr.len) {
  1100. RETURN_STRINGL(implstr.c, implstr.len, 0);
  1101. } else {
  1102. smart_str_free(&implstr);
  1103. RETURN_EMPTY_STRING();
  1104. }
  1105. }
  1106. /* }}} */
  1107. /* {{{ proto string implode([string glue,] array pieces)
  1108. Joins array elements placing glue string between items and return one string */
  1109. PHP_FUNCTION(implode)
  1110. {
  1111. zval **arg1 = NULL, **arg2 = NULL, *delim, *arr;
  1112. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|Z", &arg1, &arg2) == FAILURE) {
  1113. return;
  1114. }
  1115. if (arg2 == NULL) {
  1116. if (Z_TYPE_PP(arg1) != IS_ARRAY) {
  1117. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument must be an array");
  1118. return;
  1119. }
  1120. MAKE_STD_ZVAL(delim);
  1121. #define _IMPL_EMPTY ""
  1122. ZVAL_STRINGL(delim, _IMPL_EMPTY, sizeof(_IMPL_EMPTY) - 1, 0);
  1123. SEPARATE_ZVAL(arg1);
  1124. arr = *arg1;
  1125. } else {
  1126. if (Z_TYPE_PP(arg1) == IS_ARRAY) {
  1127. arr = *arg1;
  1128. convert_to_string_ex(arg2);
  1129. delim = *arg2;
  1130. } else if (Z_TYPE_PP(arg2) == IS_ARRAY) {
  1131. arr = *arg2;
  1132. convert_to_string_ex(arg1);
  1133. delim = *arg1;
  1134. } else {
  1135. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments passed");
  1136. return;
  1137. }
  1138. }
  1139. php_implode(delim, arr, return_value TSRMLS_CC);
  1140. if (arg2 == NULL) {
  1141. FREE_ZVAL(delim);
  1142. }
  1143. }
  1144. /* }}} */
  1145. #define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p]
  1146. /* {{{ proto string strtok([string str,] string token)
  1147. Tokenize a string */
  1148. PHP_FUNCTION(strtok)
  1149. {
  1150. char *str, *tok = NULL;
  1151. int str_len, tok_len = 0;
  1152. zval *zv;
  1153. char *token;
  1154. char *token_end;
  1155. char *p;
  1156. char *pe;
  1157. int skipped = 0;
  1158. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &tok, &tok_len) == FAILURE) {
  1159. return;
  1160. }
  1161. if (ZEND_NUM_ARGS() == 1) {
  1162. tok = str;
  1163. tok_len = str_len;
  1164. } else {
  1165. if (BG(strtok_zval)) {
  1166. zval_ptr_dtor(&BG(strtok_zval));
  1167. }
  1168. MAKE_STD_ZVAL(zv);
  1169. ZVAL_STRINGL(zv, str, str_len, 1);
  1170. BG(strtok_zval) = zv;
  1171. BG(strtok_last) = BG(strtok_string) = Z_STRVAL_P(zv);
  1172. BG(strtok_len) = str_len;
  1173. }
  1174. p = BG(strtok_last); /* Where we start to search */
  1175. pe = BG(strtok_string) + BG(strtok_len);
  1176. if (!p || p >= pe) {
  1177. RETURN_FALSE;
  1178. }
  1179. token = tok;
  1180. token_end = token + tok_len;
  1181. while (token < token_end) {
  1182. STRTOK_TABLE(token++) = 1;
  1183. }
  1184. /* Skip leading delimiters */
  1185. while (STRTOK_TABLE(p)) {
  1186. if (++p >= pe) {
  1187. /* no other chars left */
  1188. BG(strtok_last) = NULL;
  1189. RETVAL_FALSE;
  1190. goto restore;
  1191. }
  1192. skipped++;
  1193. }
  1194. /* We know at this place that *p is no delimiter, so skip it */
  1195. while (++p < pe) {
  1196. if (STRTOK_TABLE(p)) {
  1197. goto return_token;
  1198. }
  1199. }
  1200. if (p - BG(strtok_last)) {
  1201. return_token:
  1202. RETVAL_STRINGL(BG(strtok_last) + skipped, (p - BG(strtok_last)) - skipped, 1);
  1203. BG(strtok_last) = p + 1;
  1204. } else {
  1205. RETVAL_FALSE;
  1206. BG(strtok_last) = NULL;
  1207. }
  1208. /* Restore table -- usually faster then memset'ing the table on every invocation */
  1209. restore:
  1210. token = tok;
  1211. while (token < token_end) {
  1212. STRTOK_TABLE(token++) = 0;
  1213. }
  1214. }
  1215. /* }}} */
  1216. /* {{{ php_strtoupper
  1217. */
  1218. PHPAPI char *php_strtoupper(char *s, size_t len)
  1219. {
  1220. unsigned char *c, *e;
  1221. c = (unsigned char *)s;
  1222. e = (unsigned char *)c+len;
  1223. while (c < e) {
  1224. *c = toupper(*c);
  1225. c++;
  1226. }
  1227. return s;
  1228. }
  1229. /* }}} */
  1230. /* {{{ proto string strtoupper(string str)
  1231. Makes a string uppercase */
  1232. PHP_FUNCTION(strtoupper)
  1233. {
  1234. char *arg;
  1235. int arglen;
  1236. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arglen) == FAILURE) {
  1237. return;
  1238. }
  1239. arg = estrndup(arg, arglen);
  1240. php_strtoupper(arg, arglen);
  1241. RETURN_STRINGL(arg, arglen, 0);
  1242. }
  1243. /* }}} */
  1244. /* {{{ php_strtolower
  1245. */
  1246. PHPAPI char *php_strtolower(char *s, size_t len)
  1247. {
  1248. unsigned char *c, *e;
  1249. c = (unsigned char *)s;
  1250. e = c+len;
  1251. while (c < e) {
  1252. *c = tolower(*c);
  1253. c++;
  1254. }
  1255. return s;
  1256. }
  1257. /* }}} */
  1258. /* {{{ proto string strtolower(string str)
  1259. Makes a string lowercase */
  1260. PHP_FUNCTION(strtolower)
  1261. {
  1262. char *str;
  1263. int arglen;
  1264. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &arglen) == FAILURE) {
  1265. return;
  1266. }
  1267. str = estrndup(str, arglen);
  1268. php_strtolower(str, arglen);
  1269. RETURN_STRINGL(str, arglen, 0);
  1270. }
  1271. /* }}} */
  1272. /* {{{ php_basename
  1273. */
  1274. PHPAPI void php_basename(const char *s, size_t len, char *suffix, size_t sufflen, char **p_ret, size_t *p_len TSRMLS_DC)
  1275. {
  1276. char *ret = NULL, *c, *comp, *cend;
  1277. size_t inc_len, cnt;
  1278. int state;
  1279. c = comp = cend = (char*)s;
  1280. cnt = len;
  1281. state = 0;
  1282. while (cnt > 0) {
  1283. inc_len = (*c == '\0' ? 1: php_mblen(c, cnt));
  1284. switch (inc_len) {
  1285. case -2:
  1286. case -1:
  1287. inc_len = 1;
  1288. php_ignore_value(php_mblen(NULL, 0));
  1289. break;
  1290. case 0:
  1291. goto quit_loop;
  1292. case 1:
  1293. #if defined(PHP_WIN32) || defined(NETWARE)
  1294. if (*c == '/' || *c == '\\') {
  1295. #else
  1296. if (*c == '/') {
  1297. #endif
  1298. if (state == 1) {
  1299. state = 0;
  1300. cend = c;
  1301. }
  1302. #if defined(PHP_WIN32) || defined(NETWARE)
  1303. /* Catch relative paths in c:file.txt style. They're not to confuse
  1304. with the NTFS streams. This part ensures also, that no drive
  1305. letter traversing happens. */
  1306. } else if ((*c == ':' && (c - comp == 1))) {
  1307. if (state == 0) {
  1308. comp = c;
  1309. state = 1;
  1310. } else {
  1311. cend = c;
  1312. state = 0;
  1313. }
  1314. #endif
  1315. } else {
  1316. if (state == 0) {
  1317. comp = c;
  1318. state = 1;
  1319. }
  1320. }
  1321. break;
  1322. default:
  1323. if (state == 0) {
  1324. comp = c;
  1325. state = 1;
  1326. }
  1327. break;
  1328. }
  1329. c += inc_len;
  1330. cnt -= inc_len;
  1331. }
  1332. quit_loop:
  1333. if (state == 1) {
  1334. cend = c;
  1335. }
  1336. if (suffix != NULL && sufflen < (uint)(cend - comp) &&
  1337. memcmp(cend - sufflen, suffix, sufflen) == 0) {
  1338. cend -= sufflen;
  1339. }
  1340. len = cend - comp;
  1341. if (p_ret) {
  1342. ret = emalloc(len + 1);
  1343. memcpy(ret, comp, len);
  1344. ret[len] = '\0';
  1345. *p_ret = ret;
  1346. }
  1347. if (p_len) {
  1348. *p_len = len;
  1349. }
  1350. }
  1351. /* }}} */
  1352. /* {{{ proto string basename(string path [, string suffix])
  1353. Returns the filename component of the path */
  1354. PHP_FUNCTION(basename)
  1355. {
  1356. char *string, *suffix = NULL, *ret;
  1357. int string_len, suffix_len = 0;
  1358. size_t ret_len;
  1359. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &string_len, &suffix, &suffix_len) == FAILURE) {
  1360. return;
  1361. }
  1362. php_basename(string, string_len, suffix, suffix_len, &ret, &ret_len TSRMLS_CC);
  1363. RETURN_STRINGL(ret, (int)ret_len, 0);
  1364. }
  1365. /* }}} */
  1366. /* {{{ php_dirname
  1367. Returns directory name component of path */
  1368. PHPAPI size_t php_dirname(char *path, size_t len)
  1369. {
  1370. return zend_dirname(path, len);
  1371. }
  1372. /* }}} */
  1373. /* {{{ proto string dirname(string path)
  1374. Returns the directory name component of the path */
  1375. PHP_FUNCTION(dirname)
  1376. {
  1377. char *str;
  1378. char *ret;
  1379. int str_len;
  1380. size_t ret_len;
  1381. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1382. return;
  1383. }
  1384. ret = estrndup(str, str_len);
  1385. ret_len = php_dirname(ret, str_len);
  1386. RETURN_STRINGL(ret, ret_len, 0);
  1387. }
  1388. /* }}} */
  1389. /* {{{ proto array pathinfo(string path[, int options])
  1390. Returns information about a certain string */
  1391. PHP_FUNCTION(pathinfo)
  1392. {
  1393. zval *tmp;
  1394. char *path, *ret = NULL;
  1395. int path_len, have_basename;
  1396. size_t ret_len;
  1397. long opt = PHP_PATHINFO_ALL;
  1398. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &path, &path_len, &opt) == FAILURE) {
  1399. return;
  1400. }
  1401. have_basename = ((opt & PHP_PATHINFO_BASENAME) == PHP_PATHINFO_BASENAME);
  1402. MAKE_STD_ZVAL(tmp);
  1403. array_init(tmp);
  1404. if ((opt & PHP_PATHINFO_DIRNAME) == PHP_PATHINFO_DIRNAME) {
  1405. ret = estrndup(path, path_len);
  1406. php_dirname(ret, path_len);
  1407. if (*ret) {
  1408. add_assoc_string(tmp, "dirname", ret, 1);
  1409. }
  1410. efree(ret);
  1411. ret = NULL;
  1412. }
  1413. if (have_basename) {
  1414. php_basename(path, path_len, NULL, 0, &ret, &ret_len TSRMLS_CC);
  1415. add_assoc_stringl(tmp, "basename", ret, ret_len, 0);
  1416. }
  1417. if ((opt & PHP_PATHINFO_EXTENSION) == PHP_PATHINFO_EXTENSION) {
  1418. const char *p;
  1419. int idx;
  1420. if (!have_basename) {
  1421. php_basename(path, path_len, NULL, 0, &ret, &ret_len TSRMLS_CC);
  1422. }
  1423. p = zend_memrchr(ret, '.', ret_len);
  1424. if (p) {
  1425. idx = p - ret;
  1426. add_assoc_stringl(tmp, "extension", ret + idx + 1, ret_len - idx - 1, 1);
  1427. }
  1428. }
  1429. if ((opt & PHP_PATHINFO_FILENAME) == PHP_PATHINFO_FILENAME) {
  1430. const char *p;
  1431. int idx;
  1432. /* Have we already looked up the basename? */
  1433. if (!have_basename && !ret) {
  1434. php_basename(path, path_len, NULL, 0, &ret, &ret_len TSRMLS_CC);
  1435. }
  1436. p = zend_memrchr(ret, '.', ret_len);
  1437. idx = p ? (p - ret) : ret_len;
  1438. add_assoc_stringl(tmp, "filename", ret, idx, 1);
  1439. }
  1440. if (!have_basename && ret) {
  1441. efree(ret);
  1442. }
  1443. if (opt == PHP_PATHINFO_ALL) {
  1444. RETURN_ZVAL(tmp, 0, 1);
  1445. } else {
  1446. zval **element;
  1447. if (zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void **) &element) == SUCCESS) {
  1448. RETVAL_ZVAL(*element, 1, 0);
  1449. } else {
  1450. ZVAL_EMPTY_STRING(return_value);
  1451. }
  1452. }
  1453. zval_ptr_dtor(&tmp);
  1454. }
  1455. /* }}} */
  1456. /* {{{ php_stristr
  1457. case insensitve strstr */
  1458. PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len)
  1459. {
  1460. php_strtolower(s, s_len);
  1461. php_strtolower(t, t_len);
  1462. return php_memnstr(s, t, t_len, s + s_len);
  1463. }
  1464. /* }}} */
  1465. /* {{{ php_strspn
  1466. */
  1467. PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end)
  1468. {
  1469. register const char *p = s1, *spanp;
  1470. register char c = *p;
  1471. cont:
  1472. for (spanp = s2; p != s1_end && spanp != s2_end;) {
  1473. if (*spanp++ == c) {
  1474. c = *(++p);
  1475. goto cont;
  1476. }
  1477. }
  1478. return (p - s1);
  1479. }
  1480. /* }}} */
  1481. /* {{{ php_strcspn
  1482. */
  1483. PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end)
  1484. {
  1485. register const char *p, *spanp;
  1486. register char c = *s1;
  1487. for (p = s1;;) {
  1488. spanp = s2;
  1489. do {
  1490. if (*spanp == c || p == s1_end) {
  1491. return p - s1;
  1492. }
  1493. } while (spanp++ < (s2_end - 1));
  1494. c = *++p;
  1495. }
  1496. /* NOTREACHED */
  1497. }
  1498. /* }}} */
  1499. /* {{{ php_needle_char
  1500. */
  1501. static int php_needle_char(zval *needle, char *target TSRMLS_DC)
  1502. {
  1503. switch (Z_TYPE_P(needle)) {
  1504. case IS_LONG:
  1505. case IS_BOOL:
  1506. *target = (char)Z_LVAL_P(needle);
  1507. return SUCCESS;
  1508. case IS_NULL:
  1509. *target = '\0';
  1510. return SUCCESS;
  1511. case IS_DOUBLE:
  1512. *target = (char)(int)Z_DVAL_P(needle);
  1513. return SUCCESS;
  1514. case IS_OBJECT:
  1515. {
  1516. zval holder = *needle;
  1517. zval_copy_ctor(&(holder));
  1518. convert_to_long(&(holder));
  1519. if(Z_TYPE(holder) != IS_LONG) {
  1520. return FAILURE;
  1521. }
  1522. *target = (char)Z_LVAL(holder);
  1523. return SUCCESS;
  1524. }
  1525. default: {
  1526. php_error_docref(NULL TSRMLS_CC, E_WARNING, "needle is not a string or an integer");
  1527. return FAILURE;
  1528. }
  1529. }
  1530. }
  1531. /* }}} */
  1532. /* {{{ proto string stristr(string haystack, string needle[, bool part])
  1533. Finds first occurrence of a string within another, case insensitive */
  1534. PHP_FUNCTION(stristr)
  1535. {
  1536. zval *needle;
  1537. char *haystack;
  1538. int haystack_len;
  1539. char *found = NULL;
  1540. int found_offset;
  1541. char *haystack_dup;
  1542. char needle_char[2];
  1543. zend_bool part = 0;
  1544. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, &haystack_len, &needle, &part) == FAILURE) {
  1545. return;
  1546. }
  1547. haystack_dup = estrndup(haystack, haystack_len);
  1548. if (Z_TYPE_P(needle) == IS_STRING) {
  1549. char *orig_needle;
  1550. if (!Z_STRLEN_P(needle)) {
  1551. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
  1552. efree(haystack_dup);
  1553. RETURN_FALSE;
  1554. }
  1555. orig_needle = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
  1556. found = php_stristr(haystack_dup, orig_needle, haystack_len, Z_STRLEN_P(needle));
  1557. efree(orig_needle);
  1558. } else {
  1559. if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
  1560. efree(haystack_dup);
  1561. RETURN_FALSE;
  1562. }
  1563. needle_char[1] = 0;
  1564. found = php_stristr(haystack_dup, needle_char, haystack_len, 1);
  1565. }
  1566. if (found) {
  1567. found_offset = found - haystack_dup;
  1568. if (part) {
  1569. RETVAL_STRINGL(haystack, found_offset, 1);
  1570. } else {
  1571. RETVAL_STRINGL(haystack + found_offset, haystack_len - found_offset, 1);
  1572. }
  1573. } else {
  1574. RETVAL_FALSE;
  1575. }
  1576. efree(haystack_dup);
  1577. }
  1578. /* }}} */
  1579. /* {{{ proto string strstr(string haystack, string needle[, bool part])
  1580. Finds first occurrence of a string within another */
  1581. PHP_FUNCTION(strstr)
  1582. {
  1583. zval *needle;
  1584. char *haystack;
  1585. int haystack_len;
  1586. char *found = NULL;
  1587. char needle_char[2];
  1588. long found_offset;
  1589. zend_bool part = 0;
  1590. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, &haystack_len, &needle, &part) == FAILURE) {
  1591. return;
  1592. }
  1593. if (Z_TYPE_P(needle) == IS_STRING) {
  1594. if (!Z_STRLEN_P(needle)) {
  1595. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
  1596. RETURN_FALSE;
  1597. }
  1598. found = php_memnstr(haystack, Z_STRVAL_P(needle), Z_STRLEN_P(needle), haystack + haystack_len);
  1599. } else {
  1600. if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
  1601. RETURN_FALSE;
  1602. }
  1603. needle_char[1] = 0;
  1604. found = php_memnstr(haystack, needle_char, 1, haystack + haystack_len);
  1605. }
  1606. if (found) {
  1607. found_offset = found - haystack;
  1608. if (part) {
  1609. RETURN_STRINGL(haystack, found_offset, 1);
  1610. } else {
  1611. RETURN_STRINGL(found, haystack_len - found_offset, 1);
  1612. }
  1613. }
  1614. RETURN_FALSE;
  1615. }
  1616. /* }}} */
  1617. /* {{{ proto string strchr(string haystack, string needle)
  1618. An alias for strstr */
  1619. /* }}} */
  1620. /* {{{ proto int strpos(string haystack, string needle [, int offset])
  1621. Finds position of first occurrence of a string within another */
  1622. PHP_FUNCTION(strpos)
  1623. {
  1624. zval *needle;
  1625. char *haystack;
  1626. char *found = NULL;
  1627. char needle_char[2];
  1628. long offset = 0;
  1629. int haystack_len;
  1630. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &needle, &offset) == FAILURE) {
  1631. return;
  1632. }
  1633. if (offset < 0 || offset > haystack_len) {
  1634. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string");
  1635. RETURN_FALSE;
  1636. }
  1637. if (Z_TYPE_P(needle) == IS_STRING) {
  1638. if (!Z_STRLEN_P(needle)) {
  1639. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
  1640. RETURN_FALSE;
  1641. }
  1642. found = php_memnstr(haystack + offset,
  1643. Z_STRVAL_P(needle),
  1644. Z_STRLEN_P(needle),
  1645. haystack + haystack_len);
  1646. } else {
  1647. if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
  1648. RETURN_FALSE;
  1649. }
  1650. needle_char[1] = 0;
  1651. found = php_memnstr(haystack + offset,
  1652. needle_char,
  1653. 1,
  1654. haystack + haystack_len);
  1655. }
  1656. if (found) {
  1657. RETURN_LONG(found - haystack);
  1658. } else {
  1659. RETURN_FALSE;
  1660. }
  1661. }
  1662. /* }}} */
  1663. /* {{{ proto int stripos(string haystack, string needle [, int offset])
  1664. Finds position of first occurrence of a string within another, case insensitive */
  1665. PHP_FUNCTION(stripos)
  1666. {
  1667. char *found = NULL;
  1668. char *haystack;
  1669. int haystack_len;
  1670. long offset = 0;
  1671. char *needle_dup = NULL, *haystack_dup;
  1672. char needle_char[2];
  1673. zval *needle;
  1674. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &needle, &offset) == FAILURE) {
  1675. return;
  1676. }
  1677. if (offset < 0 || offset > haystack_len) {
  1678. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string");
  1679. RETURN_FALSE;
  1680. }
  1681. if (haystack_len == 0) {
  1682. RETURN_FALSE;
  1683. }
  1684. haystack_dup = estrndup(haystack, haystack_len);
  1685. php_strtolower(haystack_dup, haystack_len);
  1686. if (Z_TYPE_P(needle) == IS_STRING) {
  1687. if (Z_STRLEN_P(needle) == 0 || Z_STRLEN_P(needle) > haystack_len) {
  1688. efree(haystack_dup);
  1689. RETURN_FALSE;
  1690. }
  1691. needle_dup = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
  1692. php_strtolower(needle_dup, Z_STRLEN_P(needle));
  1693. found = php_memnstr(haystack_dup + offset, needle_dup, Z_STRLEN_P(needle), haystack_dup + haystack_len);
  1694. } else {
  1695. if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
  1696. efree(haystack_dup);
  1697. RETURN_FALSE;
  1698. }
  1699. needle_char[0] = tolower(needle_char[0]);
  1700. needle_char[1] = '\0';
  1701. found = php_memnstr(haystack_dup + offset,
  1702. needle_char,
  1703. sizeof(needle_char) - 1,
  1704. haystack_dup + haystack_len);
  1705. }
  1706. efree(haystack_dup);
  1707. if (needle_dup) {
  1708. efree(needle_dup);
  1709. }
  1710. if (found) {
  1711. RETURN_LONG(found - haystack_dup);
  1712. } else {
  1713. RETURN_FALSE;
  1714. }
  1715. }
  1716. /* }}} */
  1717. /* {{{ proto int strrpos(string haystack, string needle [, int offset])
  1718. Finds position of last occurrence of a string within another string */
  1719. PHP_FUNCTION(strrpos)
  1720. {
  1721. zval *zneedle;
  1722. char *needle, *haystack;
  1723. int needle_len, haystack_len;
  1724. long offset = 0;
  1725. char *p, *e, ord_needle[2];
  1726. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &zneedle, &offset) == FAILURE) {
  1727. RETURN_FALSE;
  1728. }
  1729. if (Z_TYPE_P(zneedle) == IS_STRING) {
  1730. needle = Z_STRVAL_P(zneedle);
  1731. needle_len = Z_STRLEN_P(zneedle);
  1732. } else {
  1733. if (php_needle_char(zneedle, ord_needle TSRMLS_CC) != SUCCESS) {
  1734. RETURN_FALSE;
  1735. }
  1736. ord_needle[1] = '\0';
  1737. needle = ord_needle;
  1738. needle_len = 1;
  1739. }
  1740. if ((haystack_len == 0) || (needle_len == 0)) {
  1741. RETURN_FALSE;
  1742. }
  1743. if (offset >= 0) {
  1744. if (offset > haystack_len) {
  1745. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1746. RETURN_FALSE;
  1747. }
  1748. p = haystack + offset;
  1749. e = haystack + haystack_len - needle_len;
  1750. } else {
  1751. if (offset < -INT_MAX || -offset > haystack_len) {
  1752. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1753. RETURN_FALSE;
  1754. }
  1755. p = haystack;
  1756. if (needle_len > -offset) {
  1757. e = haystack + haystack_len - needle_len;
  1758. } else {
  1759. e = haystack + haystack_len + offset;
  1760. }
  1761. }
  1762. if (needle_len == 1) {
  1763. /* Single character search can shortcut memcmps */
  1764. while (e >= p) {
  1765. if (*e == *needle) {
  1766. RETURN_LONG(e - p + (offset > 0 ? offset : 0));
  1767. }
  1768. e--;
  1769. }
  1770. RETURN_FALSE;
  1771. }
  1772. while (e >= p) {
  1773. if (memcmp(e, needle, needle_len) == 0) {
  1774. RETURN_LONG(e - p + (offset > 0 ? offset : 0));
  1775. }
  1776. e--;
  1777. }
  1778. RETURN_FALSE;
  1779. }
  1780. /* }}} */
  1781. /* {{{ proto int strripos(string haystack, string needle [, int offset])
  1782. Finds position of last occurrence of a string within another string */
  1783. PHP_FUNCTION(strripos)
  1784. {
  1785. zval *zneedle;
  1786. char *needle, *haystack;
  1787. int needle_len, haystack_len;
  1788. long offset = 0;
  1789. char *p, *e, ord_needle[2];
  1790. char *needle_dup, *haystack_dup;
  1791. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &zneedle, &offset) == FAILURE) {
  1792. RETURN_FALSE;
  1793. }
  1794. if (Z_TYPE_P(zneedle) == IS_STRING) {
  1795. needle = Z_STRVAL_P(zneedle);
  1796. needle_len = Z_STRLEN_P(zneedle);
  1797. } else {
  1798. if (php_needle_char(zneedle, ord_needle TSRMLS_CC) != SUCCESS) {
  1799. RETURN_FALSE;
  1800. }
  1801. ord_needle[1] = '\0';
  1802. needle = ord_needle;
  1803. needle_len = 1;
  1804. }
  1805. if ((haystack_len == 0) || (needle_len == 0)) {
  1806. RETURN_FALSE;
  1807. }
  1808. if (needle_len == 1) {
  1809. /* Single character search can shortcut memcmps
  1810. Can also avoid tolower emallocs */
  1811. if (offset >= 0) {
  1812. if (offset > haystack_len) {
  1813. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1814. RETURN_FALSE;
  1815. }
  1816. p = haystack + offset;
  1817. e = haystack + haystack_len - 1;
  1818. } else {
  1819. p = haystack;
  1820. if (offset < -INT_MAX || -offset > haystack_len) {
  1821. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1822. RETURN_FALSE;
  1823. }
  1824. e = haystack + haystack_len + offset;
  1825. }
  1826. /* Borrow that ord_needle buffer to avoid repeatedly tolower()ing needle */
  1827. *ord_needle = tolower(*needle);
  1828. while (e >= p) {
  1829. if (tolower(*e) == *ord_needle) {
  1830. RETURN_LONG(e - p + (offset > 0 ? offset : 0));
  1831. }
  1832. e--;
  1833. }
  1834. RETURN_FALSE;
  1835. }
  1836. needle_dup = estrndup(needle, needle_len);
  1837. php_strtolower(needle_dup, needle_len);
  1838. haystack_dup = estrndup(haystack, haystack_len);
  1839. php_strtolower(haystack_dup, haystack_len);
  1840. if (offset >= 0) {
  1841. if (offset > haystack_len) {
  1842. efree(needle_dup);
  1843. efree(haystack_dup);
  1844. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1845. RETURN_FALSE;
  1846. }
  1847. p = haystack_dup + offset;
  1848. e = haystack_dup + haystack_len - needle_len;
  1849. } else {
  1850. if (offset < -INT_MAX || -offset > haystack_len) {
  1851. efree(needle_dup);
  1852. efree(haystack_dup);
  1853. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  1854. RETURN_FALSE;
  1855. }
  1856. p = haystack_dup;
  1857. if (needle_len > -offset) {
  1858. e = haystack_dup + haystack_len - needle_len;
  1859. } else {
  1860. e = haystack_dup + haystack_len + offset;
  1861. }
  1862. }
  1863. while (e >= p) {
  1864. if (memcmp(e, needle_dup, needle_len) == 0) {
  1865. efree(haystack_dup);
  1866. efree(needle_dup);
  1867. RETURN_LONG(e - p + (offset > 0 ? offset : 0));
  1868. }
  1869. e--;
  1870. }
  1871. efree(haystack_dup);
  1872. efree(needle_dup);
  1873. RETURN_FALSE;
  1874. }
  1875. /* }}} */
  1876. /* {{{ proto string strrchr(string haystack, string needle)
  1877. Finds the last occurrence of a character in a string within another */
  1878. PHP_FUNCTION(strrchr)
  1879. {
  1880. zval *needle;
  1881. char *haystack;
  1882. const char *found = NULL;
  1883. long found_offset;
  1884. int haystack_len;
  1885. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &haystack, &haystack_len, &needle) == FAILURE) {
  1886. return;
  1887. }
  1888. if (Z_TYPE_P(needle) == IS_STRING) {
  1889. found = zend_memrchr(haystack, *Z_STRVAL_P(needle), haystack_len);
  1890. } else {
  1891. char needle_chr;
  1892. if (php_needle_char(needle, &needle_chr TSRMLS_CC) != SUCCESS) {
  1893. RETURN_FALSE;
  1894. }
  1895. found = zend_memrchr(haystack, needle_chr, haystack_len);
  1896. }
  1897. if (found) {
  1898. found_offset = found - haystack;
  1899. RETURN_STRINGL(found, haystack_len - found_offset, 1);
  1900. } else {
  1901. RETURN_FALSE;
  1902. }
  1903. }
  1904. /* }}} */
  1905. /* {{{ php_chunk_split
  1906. */
  1907. static char *php_chunk_split(char *src, int srclen, char *end, int endlen, int chunklen, int *destlen)
  1908. {
  1909. char *dest;
  1910. char *p, *q;
  1911. int chunks; /* complete chunks! */
  1912. int restlen;
  1913. int out_len;
  1914. chunks = srclen / chunklen;
  1915. restlen = srclen - chunks * chunklen; /* srclen % chunklen */
  1916. if(chunks > INT_MAX - 1) {
  1917. return NULL;
  1918. }
  1919. out_len = chunks + 1;
  1920. if(endlen !=0 && out_len > INT_MAX/endlen) {
  1921. return NULL;
  1922. }
  1923. out_len *= endlen;
  1924. if(out_len > INT_MAX - srclen - 1) {
  1925. return NULL;
  1926. }
  1927. out_len += srclen + 1;
  1928. dest = safe_emalloc((int)out_len, sizeof(char), 0);
  1929. for (p = src, q = dest; p < (src + srclen - chunklen + 1); ) {
  1930. memcpy(q, p, chunklen);
  1931. q += chunklen;
  1932. memcpy(q, end, endlen);
  1933. q += endlen;
  1934. p += chunklen;
  1935. }
  1936. if (restlen) {
  1937. memcpy(q, p, restlen);
  1938. q += restlen;
  1939. memcpy(q, end, endlen);
  1940. q += endlen;
  1941. }
  1942. *q = '\0';
  1943. if (destlen) {
  1944. *destlen = q - dest;
  1945. }
  1946. return(dest);
  1947. }
  1948. /* }}} */
  1949. /* {{{ proto string chunk_split(string str [, int chunklen [, string ending]])
  1950. Returns split line */
  1951. PHP_FUNCTION(chunk_split)
  1952. {
  1953. char *str;
  1954. char *result;
  1955. char *end = "\r\n";
  1956. int endlen = 2;
  1957. long chunklen = 76;
  1958. int result_len;
  1959. int str_len;
  1960. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str, &str_len, &chunklen, &end, &endlen) == FAILURE) {
  1961. return;
  1962. }
  1963. if (chunklen <= 0) {
  1964. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Chunk length should be greater than zero");
  1965. RETURN_FALSE;
  1966. }
  1967. if (chunklen > str_len) {
  1968. /* to maintain BC, we must return original string + ending */
  1969. result_len = endlen + str_len;
  1970. result = emalloc(result_len + 1);
  1971. memcpy(result, str, str_len);
  1972. memcpy(result + str_len, end, endlen);
  1973. result[result_len] = '\0';
  1974. RETURN_STRINGL(result, result_len, 0);
  1975. }
  1976. if (!str_len) {
  1977. RETURN_EMPTY_STRING();
  1978. }
  1979. result = php_chunk_split(str, str_len, end, endlen, chunklen, &result_len);
  1980. if (result) {
  1981. RETURN_STRINGL(result, result_len, 0);
  1982. } else {
  1983. RETURN_FALSE;
  1984. }
  1985. }
  1986. /* }}} */
  1987. /* {{{ proto string substr(string str, int start [, int length])
  1988. Returns part of a string */
  1989. PHP_FUNCTION(substr)
  1990. {
  1991. char *str;
  1992. long l = 0, f;
  1993. int str_len;
  1994. int argc = ZEND_NUM_ARGS();
  1995. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &str, &str_len, &f, &l) == FAILURE) {
  1996. return;
  1997. }
  1998. if (argc > 2) {
  1999. if ((l < 0 && -l > str_len)) {
  2000. RETURN_FALSE;
  2001. } else if (l > str_len) {
  2002. l = str_len;
  2003. }
  2004. } else {
  2005. l = str_len;
  2006. }
  2007. if (f > str_len) {
  2008. RETURN_FALSE;
  2009. } else if (f < 0 && -f > str_len) {
  2010. f = 0;
  2011. }
  2012. if (l < 0 && (l + str_len - f) < 0) {
  2013. RETURN_FALSE;
  2014. }
  2015. /* if "from" position is negative, count start position from the end
  2016. * of the string
  2017. */
  2018. if (f < 0) {
  2019. f = str_len + f;
  2020. if (f < 0) {
  2021. f = 0;
  2022. }
  2023. }
  2024. /* if "length" position is negative, set it to the length
  2025. * needed to stop that many chars from the end of the string
  2026. */
  2027. if (l < 0) {
  2028. l = (str_len - f) + l;
  2029. if (l < 0) {
  2030. l = 0;
  2031. }
  2032. }
  2033. if (f >= str_len) {
  2034. RETURN_FALSE;
  2035. }
  2036. if ((f + l) > str_len) {
  2037. l = str_len - f;
  2038. }
  2039. RETURN_STRINGL(str + f, l, 1);
  2040. }
  2041. /* }}} */
  2042. /* {{{ proto mixed substr_replace(mixed str, mixed repl, mixed start [, mixed length])
  2043. Replaces part of a string with another string */
  2044. PHP_FUNCTION(substr_replace)
  2045. {
  2046. zval **str;
  2047. zval **from;
  2048. zval **len = NULL;
  2049. zval **repl;
  2050. char *result;
  2051. int result_len;
  2052. int l = 0;
  2053. int f;
  2054. int argc = ZEND_NUM_ARGS();
  2055. HashPosition pos_str, pos_from, pos_repl, pos_len;
  2056. zval **tmp_str = NULL, **tmp_from = NULL, **tmp_repl = NULL, **tmp_len= NULL;
  2057. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZZ|Z", &str, &repl, &from, &len) == FAILURE) {
  2058. return;
  2059. }
  2060. if (Z_TYPE_PP(str) != IS_ARRAY) {
  2061. if (Z_ISREF_PP(str)) {
  2062. SEPARATE_ZVAL(str);
  2063. }
  2064. convert_to_string_ex(str);
  2065. }
  2066. if (Z_TYPE_PP(repl) != IS_ARRAY) {
  2067. if (Z_ISREF_PP(repl)) {
  2068. SEPARATE_ZVAL(repl);
  2069. }
  2070. convert_to_string_ex(repl);
  2071. }
  2072. if (Z_TYPE_PP(from) != IS_ARRAY) {
  2073. if (Z_ISREF_PP(from)) {
  2074. SEPARATE_ZVAL(from);
  2075. }
  2076. convert_to_long_ex(from);
  2077. }
  2078. if (argc > 3) {
  2079. SEPARATE_ZVAL(len);
  2080. if (Z_TYPE_PP(len) != IS_ARRAY) {
  2081. convert_to_long_ex(len);
  2082. l = Z_LVAL_PP(len);
  2083. }
  2084. } else {
  2085. if (Z_TYPE_PP(str) != IS_ARRAY) {
  2086. l = Z_STRLEN_PP(str);
  2087. }
  2088. }
  2089. if (Z_TYPE_PP(str) == IS_STRING) {
  2090. if (
  2091. (argc == 3 && Z_TYPE_PP(from) == IS_ARRAY) ||
  2092. (argc == 4 && Z_TYPE_PP(from) != Z_TYPE_PP(len))
  2093. ) {
  2094. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'from' and 'len' should be of same type - numerical or array ");
  2095. RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
  2096. }
  2097. if (argc == 4 && Z_TYPE_PP(from) == IS_ARRAY) {
  2098. if (zend_hash_num_elements(Z_ARRVAL_PP(from)) != zend_hash_num_elements(Z_ARRVAL_PP(len))) {
  2099. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'from' and 'len' should have the same number of elements");
  2100. RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
  2101. }
  2102. }
  2103. }
  2104. if (Z_TYPE_PP(str) != IS_ARRAY) {
  2105. if (Z_TYPE_PP(from) != IS_ARRAY) {
  2106. int repl_len = 0;
  2107. f = Z_LVAL_PP(from);
  2108. /* if "from" position is negative, count start position from the end
  2109. * of the string
  2110. */
  2111. if (f < 0) {
  2112. f = Z_STRLEN_PP(str) + f;
  2113. if (f < 0) {
  2114. f = 0;
  2115. }
  2116. } else if (f > Z_STRLEN_PP(str)) {
  2117. f = Z_STRLEN_PP(str);
  2118. }
  2119. /* if "length" position is negative, set it to the length
  2120. * needed to stop that many chars from the end of the string
  2121. */
  2122. if (l < 0) {
  2123. l = (Z_STRLEN_PP(str) - f) + l;
  2124. if (l < 0) {
  2125. l = 0;
  2126. }
  2127. }
  2128. if (f > Z_STRLEN_PP(str) || (f < 0 && -f > Z_STRLEN_PP(str))) {
  2129. RETURN_FALSE;
  2130. } else if (l > Z_STRLEN_PP(str) || (l < 0 && -l > Z_STRLEN_PP(str))) {
  2131. l = Z_STRLEN_PP(str);
  2132. }
  2133. if (f > Z_STRLEN_PP(str) - l) {
  2134. l = Z_STRLEN_PP(str) - f;
  2135. }
  2136. if (Z_TYPE_PP(repl) == IS_ARRAY) {
  2137. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(repl), &pos_repl);
  2138. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(repl), (void **) &tmp_repl, &pos_repl)) {
  2139. convert_to_string_ex(tmp_repl);
  2140. repl_len = Z_STRLEN_PP(tmp_repl);
  2141. }
  2142. } else {
  2143. repl_len = Z_STRLEN_PP(repl);
  2144. }
  2145. result_len = Z_STRLEN_PP(str) - l + repl_len;
  2146. result = safe_emalloc_string(1, result_len, 1);
  2147. memcpy(result, Z_STRVAL_PP(str), f);
  2148. if (repl_len) {
  2149. memcpy((result + f), (Z_TYPE_PP(repl) == IS_ARRAY ? Z_STRVAL_PP(tmp_repl) : Z_STRVAL_PP(repl)), repl_len);
  2150. }
  2151. memcpy((result + f + repl_len), Z_STRVAL_PP(str) + f + l, Z_STRLEN_PP(str) - f - l);
  2152. result[result_len] = '\0';
  2153. RETURN_STRINGL(result, result_len, 0);
  2154. } else {
  2155. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Functionality of 'from' and 'len' as arrays is not implemented");
  2156. RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
  2157. }
  2158. } else { /* str is array of strings */
  2159. char *str_index = NULL;
  2160. uint str_index_len;
  2161. ulong num_index;
  2162. array_init(return_value);
  2163. if (Z_TYPE_PP(from) == IS_ARRAY) {
  2164. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(from), &pos_from);
  2165. }
  2166. if (argc > 3 && Z_TYPE_PP(len) == IS_ARRAY) {
  2167. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(len), &pos_len);
  2168. }
  2169. if (Z_TYPE_PP(repl) == IS_ARRAY) {
  2170. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(repl), &pos_repl);
  2171. }
  2172. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(str), &pos_str);
  2173. while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(str), (void **) &tmp_str, &pos_str) == SUCCESS) {
  2174. zval *orig_str;
  2175. zval dummy;
  2176. ulong refcount;
  2177. int was_ref;
  2178. if(Z_TYPE_PP(tmp_str) != IS_STRING) {
  2179. dummy = **tmp_str;
  2180. orig_str = &dummy;
  2181. zval_copy_ctor(orig_str);
  2182. convert_to_string(orig_str);
  2183. } else {
  2184. orig_str = *tmp_str;
  2185. }
  2186. was_ref = Z_ISREF_P(orig_str);
  2187. Z_UNSET_ISREF_P(orig_str);
  2188. refcount = Z_REFCOUNT_P(orig_str);
  2189. if (Z_TYPE_PP(from) == IS_ARRAY) {
  2190. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(from), (void **) &tmp_from, &pos_from)) {
  2191. if(Z_TYPE_PP(tmp_from) != IS_LONG) {
  2192. zval dummy = **tmp_from;
  2193. zval_copy_ctor(&dummy);
  2194. convert_to_long(&dummy);
  2195. f = Z_LVAL(dummy);
  2196. } else {
  2197. f = Z_LVAL_PP(tmp_from);
  2198. }
  2199. if (f < 0) {
  2200. f = Z_STRLEN_P(orig_str) + f;
  2201. if (f < 0) {
  2202. f = 0;
  2203. }
  2204. } else if (f > Z_STRLEN_P(orig_str)) {
  2205. f = Z_STRLEN_P(orig_str);
  2206. }
  2207. zend_hash_move_forward_ex(Z_ARRVAL_PP(from), &pos_from);
  2208. } else {
  2209. f = 0;
  2210. }
  2211. } else {
  2212. f = Z_LVAL_PP(from);
  2213. if (f < 0) {
  2214. f = Z_STRLEN_P(orig_str) + f;
  2215. if (f < 0) {
  2216. f = 0;
  2217. }
  2218. } else if (f > Z_STRLEN_P(orig_str)) {
  2219. f = Z_STRLEN_P(orig_str);
  2220. }
  2221. }
  2222. if (argc > 3 && Z_TYPE_PP(len) == IS_ARRAY) {
  2223. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(len), (void **) &tmp_len, &pos_len)) {
  2224. if(Z_TYPE_PP(tmp_len) != IS_LONG) {
  2225. zval dummy = **tmp_len;
  2226. zval_copy_ctor(&dummy);
  2227. convert_to_long(&dummy);
  2228. l = Z_LVAL(dummy);
  2229. } else {
  2230. l = Z_LVAL_PP(tmp_len);
  2231. }
  2232. zend_hash_move_forward_ex(Z_ARRVAL_PP(len), &pos_len);
  2233. } else {
  2234. l = Z_STRLEN_P(orig_str);
  2235. }
  2236. } else if (argc > 3) {
  2237. l = Z_LVAL_PP(len);
  2238. } else {
  2239. l = Z_STRLEN_P(orig_str);
  2240. }
  2241. if (l < 0) {
  2242. l = (Z_STRLEN_P(orig_str) - f) + l;
  2243. if (l < 0) {
  2244. l = 0;
  2245. }
  2246. }
  2247. if ((f + l) > Z_STRLEN_P(orig_str)) {
  2248. l = Z_STRLEN_P(orig_str) - f;
  2249. }
  2250. result_len = Z_STRLEN_P(orig_str) - l;
  2251. if (Z_TYPE_PP(repl) == IS_ARRAY) {
  2252. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(repl), (void **) &tmp_repl, &pos_repl)) {
  2253. zval *repl_str;
  2254. zval zrepl;
  2255. if(Z_TYPE_PP(tmp_repl) != IS_STRING) {
  2256. zrepl = **tmp_repl;
  2257. repl_str = &zrepl;
  2258. zval_copy_ctor(repl_str);
  2259. convert_to_string(repl_str);
  2260. } else {
  2261. repl_str = *tmp_repl;
  2262. }
  2263. if(Z_REFCOUNT_P(orig_str) != refcount) {
  2264. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument was modified while replacing");
  2265. if(Z_TYPE_PP(tmp_repl) != IS_STRING) {
  2266. zval_dtor(repl_str);
  2267. }
  2268. break;
  2269. }
  2270. result_len += Z_STRLEN_P(repl_str);
  2271. zend_hash_move_forward_ex(Z_ARRVAL_PP(repl), &pos_repl);
  2272. result = safe_emalloc_string(1, result_len, 1);
  2273. memcpy(result, Z_STRVAL_P(orig_str), f);
  2274. memcpy((result + f), Z_STRVAL_P(repl_str), Z_STRLEN_P(repl_str));
  2275. memcpy((result + f + Z_STRLEN_P(repl_str)), Z_STRVAL_P(orig_str) + f + l, Z_STRLEN_P(orig_str) - f - l);
  2276. if(Z_TYPE_PP(tmp_repl) != IS_STRING) {
  2277. zval_dtor(repl_str);
  2278. }
  2279. } else {
  2280. result = safe_emalloc_string(1, result_len, 1);
  2281. memcpy(result, Z_STRVAL_P(orig_str), f);
  2282. memcpy((result + f), Z_STRVAL_P(orig_str) + f + l, Z_STRLEN_P(orig_str) - f - l);
  2283. }
  2284. } else {
  2285. result_len += Z_STRLEN_PP(repl);
  2286. result = safe_emalloc_string(1, result_len, 1);
  2287. memcpy(result, Z_STRVAL_P(orig_str), f);
  2288. memcpy((result + f), Z_STRVAL_PP(repl), Z_STRLEN_PP(repl));
  2289. memcpy((result + f + Z_STRLEN_PP(repl)), Z_STRVAL_P(orig_str) + f + l, Z_STRLEN_P(orig_str) - f - l);
  2290. }
  2291. result[result_len] = '\0';
  2292. if (zend_hash_get_current_key_ex(Z_ARRVAL_PP(str), &str_index, &str_index_len, &num_index, 0, &pos_str) == HASH_KEY_IS_STRING) {
  2293. add_assoc_stringl_ex(return_value, str_index, str_index_len, result, result_len, 0);
  2294. } else {
  2295. add_index_stringl(return_value, num_index, result, result_len, 0);
  2296. }
  2297. if(Z_TYPE_PP(tmp_str) != IS_STRING) {
  2298. zval_dtor(orig_str);
  2299. } else {
  2300. Z_SET_ISREF_TO_P(orig_str, was_ref);
  2301. }
  2302. zend_hash_move_forward_ex(Z_ARRVAL_PP(str), &pos_str);
  2303. } /*while*/
  2304. } /* if */
  2305. }
  2306. /* }}} */
  2307. /* {{{ proto string quotemeta(string str)
  2308. Quotes meta characters */
  2309. PHP_FUNCTION(quotemeta)
  2310. {
  2311. char *str, *old;
  2312. char *old_end;
  2313. char *p, *q;
  2314. char c;
  2315. int old_len;
  2316. size_t new_len;
  2317. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &old, &old_len) == FAILURE) {
  2318. return;
  2319. }
  2320. old_end = old + old_len;
  2321. if (old == old_end) {
  2322. RETURN_FALSE;
  2323. }
  2324. str = safe_emalloc_string(2, old_len, 1);
  2325. for (p = old, q = str; p != old_end; p++) {
  2326. c = *p;
  2327. switch (c) {
  2328. case '.':
  2329. case '\\':
  2330. case '+':
  2331. case '*':
  2332. case '?':
  2333. case '[':
  2334. case '^':
  2335. case ']':
  2336. case '$':
  2337. case '(':
  2338. case ')':
  2339. *q++ = '\\';
  2340. /* break is missing _intentionally_ */
  2341. default:
  2342. *q++ = c;
  2343. }
  2344. }
  2345. *q = 0;
  2346. new_len = q - str;
  2347. if (UNEXPECTED(new_len > INT_MAX)) {
  2348. efree(str);
  2349. zend_error(E_ERROR, "String size overflow");
  2350. }
  2351. RETURN_STRINGL(erealloc(str, new_len + 1), new_len, 0);
  2352. }
  2353. /* }}} */
  2354. /* {{{ proto int ord(string character)
  2355. Returns ASCII value of character */
  2356. PHP_FUNCTION(ord)
  2357. {
  2358. char *str;
  2359. int str_len;
  2360. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2361. return;
  2362. }
  2363. RETURN_LONG((unsigned char) str[0]);
  2364. }
  2365. /* }}} */
  2366. /* {{{ proto string chr(int ascii)
  2367. Converts ASCII code to a character */
  2368. PHP_FUNCTION(chr)
  2369. {
  2370. long c;
  2371. char temp[2];
  2372. if (ZEND_NUM_ARGS() != 1) {
  2373. WRONG_PARAM_COUNT;
  2374. }
  2375. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "l", &c) == FAILURE) {
  2376. c = 0;
  2377. }
  2378. temp[0] = (char)c;
  2379. temp[1] = '\0';
  2380. RETURN_STRINGL(temp, 1, 1);
  2381. }
  2382. /* }}} */
  2383. /* {{{ php_ucfirst
  2384. Uppercase the first character of the word in a native string */
  2385. static void php_ucfirst(char *str)
  2386. {
  2387. register char *r;
  2388. r = str;
  2389. *r = toupper((unsigned char) *r);
  2390. }
  2391. /* }}} */
  2392. /* {{{ proto string ucfirst(string str)
  2393. Makes a string's first character uppercase */
  2394. PHP_FUNCTION(ucfirst)
  2395. {
  2396. char *str;
  2397. int str_len;
  2398. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2399. return;
  2400. }
  2401. if (!str_len) {
  2402. RETURN_EMPTY_STRING();
  2403. }
  2404. ZVAL_STRINGL(return_value, str, str_len, 1);
  2405. php_ucfirst(Z_STRVAL_P(return_value));
  2406. }
  2407. /* }}} */
  2408. /* {{{
  2409. Lowercase the first character of the word in a native string */
  2410. static void php_lcfirst(char *str)
  2411. {
  2412. register char *r;
  2413. r = str;
  2414. *r = tolower((unsigned char) *r);
  2415. }
  2416. /* }}} */
  2417. /* {{{ proto string lcfirst(string str)
  2418. Make a string's first character lowercase */
  2419. PHP_FUNCTION(lcfirst)
  2420. {
  2421. char *str;
  2422. int str_len;
  2423. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2424. return;
  2425. }
  2426. if (!str_len) {
  2427. RETURN_EMPTY_STRING();
  2428. }
  2429. ZVAL_STRINGL(return_value, str, str_len, 1);
  2430. php_lcfirst(Z_STRVAL_P(return_value));
  2431. }
  2432. /* }}} */
  2433. /* {{{ proto string ucwords(string str [, string delims])
  2434. Uppercase the first character of every word in a string */
  2435. PHP_FUNCTION(ucwords)
  2436. {
  2437. char *str, *delims = " \t\r\n\f\v";
  2438. register char *r, *r_end;
  2439. int str_len, delims_len = 6;
  2440. char mask[256];
  2441. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &delims, &delims_len) == FAILURE) {
  2442. return;
  2443. }
  2444. if (!str_len) {
  2445. RETURN_EMPTY_STRING();
  2446. }
  2447. php_charmask((unsigned char *)delims, delims_len, mask TSRMLS_CC);
  2448. ZVAL_STRINGL(return_value, str, str_len, 1);
  2449. r = Z_STRVAL_P(return_value);
  2450. *r = toupper((unsigned char) *r);
  2451. for (r_end = r + Z_STRLEN_P(return_value) - 1; r < r_end; ) {
  2452. if (mask[(unsigned char)*r++]) {
  2453. *r = toupper((unsigned char) *r);
  2454. }
  2455. }
  2456. }
  2457. /* }}} */
  2458. /* {{{ php_strtr
  2459. */
  2460. PHPAPI char *php_strtr(char *str, int len, char *str_from, char *str_to, int trlen)
  2461. {
  2462. int i;
  2463. unsigned char xlat[256];
  2464. if ((trlen < 1) || (len < 1)) {
  2465. return str;
  2466. }
  2467. for (i = 0; i < 256; xlat[i] = i, i++);
  2468. for (i = 0; i < trlen; i++) {
  2469. xlat[(unsigned char) str_from[i]] = str_to[i];
  2470. }
  2471. for (i = 0; i < len; i++) {
  2472. str[i] = xlat[(unsigned char) str[i]];
  2473. }
  2474. return str;
  2475. }
  2476. /* }}} */
  2477. /* {{{ Definitions for php_strtr_array */
  2478. typedef size_t STRLEN; /* STRLEN should be unsigned */
  2479. typedef uint16_t HASH;
  2480. typedef struct {
  2481. HASH table_mask;
  2482. STRLEN entries[1];
  2483. } SHIFT_TAB;
  2484. typedef struct {
  2485. HASH table_mask;
  2486. int entries[1];
  2487. } HASH_TAB;
  2488. typedef struct {
  2489. const char *s;
  2490. STRLEN l;
  2491. } STR;
  2492. typedef struct _pat_and_repl {
  2493. STR pat;
  2494. STR repl;
  2495. } PATNREPL;
  2496. #define S(a) ((a)->s)
  2497. #define L(a) ((a)->l)
  2498. #define SHIFT_TAB_BITS 13
  2499. #define HASH_TAB_BITS 10 /* should be less than sizeof(HASH) * 8 */
  2500. #define SHIFT_TAB_SIZE (1U << SHIFT_TAB_BITS)
  2501. #define HASH_TAB_SIZE (1U << HASH_TAB_BITS)
  2502. typedef struct {
  2503. int B; /* size of suffixes */
  2504. int Bp; /* size of prefixes */
  2505. STRLEN m; /* minimum pattern length */
  2506. int patnum; /* number of patterns */
  2507. SHIFT_TAB *shift; /* table mapping hash to allowed shift */
  2508. HASH_TAB *hash; /* table mapping hash to int (pair of pointers) */
  2509. HASH *prefix; /* array of hashes of prefixes by pattern suffix hash order */
  2510. PATNREPL *patterns; /* array of prefixes by pattern suffix hash order */
  2511. } PPRES;
  2512. /* }}} */
  2513. /* {{{ php_strtr_hash */
  2514. static inline HASH php_strtr_hash(const char *str, int len)
  2515. {
  2516. HASH res = 0;
  2517. int i;
  2518. for (i = 0; i < len; i++) {
  2519. res = res * 33 + (unsigned char)str[i];
  2520. }
  2521. return res;
  2522. }
  2523. /* }}} */
  2524. /* {{{ php_strtr_populate_shift */
  2525. static inline void php_strtr_populate_shift(PATNREPL *patterns, int patnum, int B, STRLEN m, SHIFT_TAB *shift)
  2526. {
  2527. int i;
  2528. STRLEN j,
  2529. max_shift;
  2530. max_shift = m - B + 1;
  2531. for (i = 0; i < SHIFT_TAB_SIZE; i++) {
  2532. shift->entries[i] = max_shift;
  2533. }
  2534. for (i = 0; i < patnum; i++) {
  2535. for (j = 0; j < m - B + 1; j++) {
  2536. HASH h = php_strtr_hash(&S(&patterns[i].pat)[j], B) & shift->table_mask;
  2537. assert((long long) m - (long long) j - B >= 0);
  2538. shift->entries[h] = MIN(shift->entries[h], m - j - B);
  2539. }
  2540. }
  2541. }
  2542. /* }}} */
  2543. /* {{{ php_strtr_compare_hash_suffix */
  2544. static int php_strtr_compare_hash_suffix(const void *a, const void *b TSRMLS_DC, void *ctx_g)
  2545. {
  2546. const PPRES *res = ctx_g;
  2547. const PATNREPL *pnr_a = a,
  2548. *pnr_b = b;
  2549. HASH hash_a = php_strtr_hash(&S(&pnr_a->pat)[res->m - res->B], res->B)
  2550. & res->hash->table_mask,
  2551. hash_b = php_strtr_hash(&S(&pnr_b->pat)[res->m - res->B], res->B)
  2552. & res->hash->table_mask;
  2553. /* TODO: don't recalculate the hashes all the time */
  2554. if (hash_a > hash_b) {
  2555. return 1;
  2556. } else if (hash_a < hash_b) {
  2557. return -1;
  2558. } else {
  2559. /* longer patterns must be sorted first */
  2560. if (L(&pnr_a->pat) > L(&pnr_b->pat)) {
  2561. return -1;
  2562. } else if (L(&pnr_a->pat) < L(&pnr_b->pat)) {
  2563. return 1;
  2564. } else {
  2565. return 0;
  2566. }
  2567. }
  2568. }
  2569. /* }}} */
  2570. /* {{{ php_strtr_free_strp */
  2571. static void php_strtr_free_strp(void *strp)
  2572. {
  2573. STR_FREE(*(char**)strp);
  2574. }
  2575. /* }}} */
  2576. /* {{{ php_strtr_array_prepare_repls */
  2577. static PATNREPL *php_strtr_array_prepare_repls(int slen, HashTable *pats, zend_llist **allocs, int *outsize)
  2578. {
  2579. PATNREPL *patterns;
  2580. HashPosition hpos;
  2581. zval **entry;
  2582. int num_pats = zend_hash_num_elements(pats),
  2583. i;
  2584. patterns = safe_emalloc(num_pats, sizeof(*patterns), 0);
  2585. *allocs = emalloc(sizeof **allocs);
  2586. zend_llist_init(*allocs, sizeof(void*), &php_strtr_free_strp, 0);
  2587. for (i = 0, zend_hash_internal_pointer_reset_ex(pats, &hpos);
  2588. zend_hash_get_current_data_ex(pats, (void **)&entry, &hpos) == SUCCESS;
  2589. zend_hash_move_forward_ex(pats, &hpos)) {
  2590. char *string_key;
  2591. uint string_key_len;
  2592. ulong num_key;
  2593. zval *tzv = NULL;
  2594. switch (zend_hash_get_current_key_ex(pats, &string_key, &string_key_len, &num_key, 0, &hpos)) {
  2595. case HASH_KEY_IS_LONG:
  2596. string_key_len = 1 + zend_spprintf(&string_key, 0, "%ld", (long)num_key);
  2597. zend_llist_add_element(*allocs, &string_key);
  2598. /* break missing intentionally */
  2599. case HASH_KEY_IS_STRING:
  2600. string_key_len--; /* exclude final '\0' */
  2601. if (string_key_len == 0) { /* empty string given as pattern */
  2602. efree(patterns);
  2603. zend_llist_destroy(*allocs);
  2604. efree(*allocs);
  2605. *allocs = NULL;
  2606. return NULL;
  2607. }
  2608. if (string_key_len > slen) { /* this pattern can never match */
  2609. continue;
  2610. }
  2611. if (Z_TYPE_PP(entry) != IS_STRING) {
  2612. tzv = *entry;
  2613. zval_addref_p(tzv);
  2614. SEPARATE_ZVAL(&tzv);
  2615. convert_to_string(tzv);
  2616. entry = &tzv;
  2617. zend_llist_add_element(*allocs, &Z_STRVAL_PP(entry));
  2618. }
  2619. S(&patterns[i].pat) = string_key;
  2620. L(&patterns[i].pat) = string_key_len;
  2621. S(&patterns[i].repl) = Z_STRVAL_PP(entry);
  2622. L(&patterns[i].repl) = Z_STRLEN_PP(entry);
  2623. i++;
  2624. if (tzv) {
  2625. efree(tzv);
  2626. }
  2627. }
  2628. }
  2629. *outsize = i;
  2630. return patterns;
  2631. }
  2632. /* }}} */
  2633. /* {{{ PPRES *php_strtr_array_prepare(STR *text, PATNREPL *patterns, int patnum, int B, int Bp) */
  2634. static PPRES *php_strtr_array_prepare(STR *text, PATNREPL *patterns, int patnum, int B, int Bp)
  2635. {
  2636. int i;
  2637. PPRES *res = emalloc(sizeof *res);
  2638. res->m = (STRLEN)-1;
  2639. for (i = 0; i < patnum; i++) {
  2640. if (L(&patterns[i].pat) < res->m) {
  2641. res->m = L(&patterns[i].pat);
  2642. }
  2643. }
  2644. assert(res->m > 0 && res->m != (STRLEN)-1);
  2645. res->B = B = MIN(B, res->m);
  2646. res->Bp = Bp = MIN(Bp, res->m);
  2647. res->shift = safe_emalloc(SHIFT_TAB_SIZE, sizeof(*res->shift->entries), sizeof(*res->shift));
  2648. res->shift->table_mask = SHIFT_TAB_SIZE - 1;
  2649. php_strtr_populate_shift(patterns, patnum, B, res->m, res->shift);
  2650. res->hash = safe_emalloc(HASH_TAB_SIZE, sizeof(*res->hash->entries), sizeof(*res->hash));
  2651. res->hash->table_mask = HASH_TAB_SIZE - 1;
  2652. res->patterns = safe_emalloc(patnum, sizeof(*res->patterns), 0);
  2653. memcpy(res->patterns, patterns, sizeof(*patterns) * patnum);
  2654. #ifdef ZTS
  2655. zend_qsort_r(res->patterns, patnum, sizeof(*res->patterns),
  2656. php_strtr_compare_hash_suffix, res, NULL); /* tsrmls not needed */
  2657. #else
  2658. zend_qsort_r(res->patterns, patnum, sizeof(*res->patterns),
  2659. php_strtr_compare_hash_suffix, res);
  2660. #endif
  2661. res->prefix = safe_emalloc(patnum, sizeof(*res->prefix), 0);
  2662. for (i = 0; i < patnum; i++) {
  2663. res->prefix[i] = php_strtr_hash(S(&res->patterns[i].pat), Bp);
  2664. }
  2665. /* Initialize the rest of ->hash */
  2666. for (i = 0; i < HASH_TAB_SIZE; i++) {
  2667. res->hash->entries[i] = -1;
  2668. }
  2669. {
  2670. HASH last_h = -1; /* assumes not all bits are used in res->hash */
  2671. /* res->patterns is already ordered by hash.
  2672. * Make res->hash->entries[h] de index of the first pattern in
  2673. * res->patterns that has hash h */
  2674. for (i = 0; i < patnum; i++) {
  2675. HASH h = php_strtr_hash(&S(&res->patterns[i].pat)[res->m - res->B], res->B)
  2676. & res->hash->table_mask;
  2677. if (h != last_h) {
  2678. res->hash->entries[h] = i;
  2679. last_h = h;
  2680. }
  2681. }
  2682. }
  2683. res->hash->entries[HASH_TAB_SIZE] = patnum; /* OK, we effectively allocated SIZE+1 */
  2684. for (i = HASH_TAB_SIZE - 1; i >= 0; i--) {
  2685. if (res->hash->entries[i] == -1) {
  2686. res->hash->entries[i] = res->hash->entries[i + 1];
  2687. }
  2688. }
  2689. res->patnum = patnum;
  2690. return res;
  2691. }
  2692. /* }}} */
  2693. /* {{{ php_strtr_array_destroy_ppres(PPRES *d) */
  2694. static void php_strtr_array_destroy_ppres(PPRES *d)
  2695. {
  2696. efree(d->shift);
  2697. efree(d->hash);
  2698. efree(d->prefix);
  2699. efree(d->patterns);
  2700. efree(d);
  2701. }
  2702. /* }}} */
  2703. /* {{{ php_strtr_array_do_repl(STR *text, PPRES *d, zval *return_value) */
  2704. static void php_strtr_array_do_repl(STR *text, PPRES *d, zval *return_value)
  2705. {
  2706. STRLEN pos = 0,
  2707. nextwpos = 0,
  2708. lastpos = L(text) - d->m;
  2709. smart_str result = {0};
  2710. while (pos <= lastpos) {
  2711. HASH h = php_strtr_hash(&S(text)[pos + d->m - d->B], d->B) & d->shift->table_mask;
  2712. STRLEN shift = d->shift->entries[h];
  2713. if (shift > 0) {
  2714. pos += shift;
  2715. } else {
  2716. HASH h2 = h & d->hash->table_mask,
  2717. prefix_h = php_strtr_hash(&S(text)[pos], d->Bp);
  2718. int offset_start = d->hash->entries[h2],
  2719. offset_end = d->hash->entries[h2 + 1], /* exclusive */
  2720. i = 0;
  2721. for (i = offset_start; i < offset_end; i++) {
  2722. PATNREPL *pnr;
  2723. if (d->prefix[i] != prefix_h)
  2724. continue;
  2725. pnr = &d->patterns[i];
  2726. if (L(&pnr->pat) > L(text) - pos ||
  2727. memcmp(S(&pnr->pat), &S(text)[pos], L(&pnr->pat)) != 0)
  2728. continue;
  2729. smart_str_appendl(&result, &S(text)[nextwpos], pos - nextwpos);
  2730. smart_str_appendl(&result, S(&pnr->repl), L(&pnr->repl));
  2731. pos += L(&pnr->pat);
  2732. nextwpos = pos;
  2733. goto end_outer_loop;
  2734. }
  2735. pos++;
  2736. end_outer_loop: ;
  2737. }
  2738. }
  2739. smart_str_appendl(&result, &S(text)[nextwpos], L(text) - nextwpos);
  2740. if (result.c != NULL) {
  2741. smart_str_0(&result);
  2742. RETVAL_STRINGL(result.c, result.len, 0);
  2743. } else {
  2744. RETURN_EMPTY_STRING();
  2745. }
  2746. }
  2747. /* }}} */
  2748. /* {{{ php_strtr_array */
  2749. static void php_strtr_array(zval *return_value, char *str, int slen, HashTable *pats)
  2750. {
  2751. PPRES *data;
  2752. STR text;
  2753. PATNREPL *patterns;
  2754. int patterns_len;
  2755. zend_llist *allocs;
  2756. if (zend_hash_num_elements(pats) == 0) {
  2757. RETURN_STRINGL(str, slen, 1);
  2758. }
  2759. S(&text) = str;
  2760. L(&text) = slen;
  2761. patterns = php_strtr_array_prepare_repls(slen, pats, &allocs, &patterns_len);
  2762. if (patterns == NULL) {
  2763. RETURN_FALSE;
  2764. }
  2765. if (patterns_len == 0) {
  2766. efree(patterns);
  2767. zend_llist_destroy(allocs);
  2768. efree(allocs);
  2769. RETURN_STRINGL(str, slen, 1);
  2770. }
  2771. data = php_strtr_array_prepare(&text, patterns, patterns_len, 2, 2);
  2772. efree(patterns);
  2773. php_strtr_array_do_repl(&text, data, return_value);
  2774. php_strtr_array_destroy_ppres(data);
  2775. zend_llist_destroy(allocs);
  2776. efree(allocs);
  2777. }
  2778. /* }}} */
  2779. /* {{{ proto string strtr(string str, string from[, string to])
  2780. Translates characters in str using given translation tables */
  2781. PHP_FUNCTION(strtr)
  2782. {
  2783. zval **from;
  2784. char *str, *to = NULL;
  2785. int str_len, to_len = 0;
  2786. int ac = ZEND_NUM_ARGS();
  2787. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sZ|s", &str, &str_len, &from, &to, &to_len) == FAILURE) {
  2788. return;
  2789. }
  2790. if (ac == 2 && Z_TYPE_PP(from) != IS_ARRAY) {
  2791. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The second argument is not an array");
  2792. RETURN_FALSE;
  2793. }
  2794. /* shortcut for empty string */
  2795. if (str_len == 0) {
  2796. RETURN_EMPTY_STRING();
  2797. }
  2798. if (ac == 2) {
  2799. php_strtr_array(return_value, str, str_len, HASH_OF(*from));
  2800. } else {
  2801. convert_to_string_ex(from);
  2802. ZVAL_STRINGL(return_value, str, str_len, 1);
  2803. php_strtr(Z_STRVAL_P(return_value),
  2804. Z_STRLEN_P(return_value),
  2805. Z_STRVAL_PP(from),
  2806. to,
  2807. MIN(Z_STRLEN_PP(from),
  2808. to_len));
  2809. }
  2810. }
  2811. /* }}} */
  2812. /* {{{ proto string strrev(string str)
  2813. Reverse a string */
  2814. PHP_FUNCTION(strrev)
  2815. {
  2816. char *str;
  2817. char *e, *n, *p;
  2818. int str_len;
  2819. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2820. return;
  2821. }
  2822. n = emalloc(str_len+1);
  2823. p = n;
  2824. e = str + str_len;
  2825. while (--e>=str) {
  2826. *p++ = *e;
  2827. }
  2828. *p = '\0';
  2829. RETVAL_STRINGL(n, str_len, 0);
  2830. }
  2831. /* }}} */
  2832. /* {{{ php_similar_str
  2833. */
  2834. static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)
  2835. {
  2836. char *p, *q;
  2837. char *end1 = (char *) txt1 + len1;
  2838. char *end2 = (char *) txt2 + len2;
  2839. int l;
  2840. *max = 0;
  2841. for (p = (char *) txt1; p < end1; p++) {
  2842. for (q = (char *) txt2; q < end2; q++) {
  2843. for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); l++);
  2844. if (l > *max) {
  2845. *max = l;
  2846. *pos1 = p - txt1;
  2847. *pos2 = q - txt2;
  2848. }
  2849. }
  2850. }
  2851. }
  2852. /* }}} */
  2853. /* {{{ php_similar_char
  2854. */
  2855. static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2)
  2856. {
  2857. int sum;
  2858. int pos1 = 0, pos2 = 0, max;
  2859. php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
  2860. if ((sum = max)) {
  2861. if (pos1 && pos2) {
  2862. sum += php_similar_char(txt1, pos1,
  2863. txt2, pos2);
  2864. }
  2865. if ((pos1 + max < len1) && (pos2 + max < len2)) {
  2866. sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
  2867. txt2 + pos2 + max, len2 - pos2 - max);
  2868. }
  2869. }
  2870. return sum;
  2871. }
  2872. /* }}} */
  2873. /* {{{ proto int similar_text(string str1, string str2 [, float percent])
  2874. Calculates the similarity between two strings */
  2875. PHP_FUNCTION(similar_text)
  2876. {
  2877. char *t1, *t2;
  2878. zval **percent = NULL;
  2879. int ac = ZEND_NUM_ARGS();
  2880. int sim;
  2881. int t1_len, t2_len;
  2882. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|Z", &t1, &t1_len, &t2, &t2_len, &percent) == FAILURE) {
  2883. return;
  2884. }
  2885. if (ac > 2) {
  2886. convert_to_double_ex(percent);
  2887. }
  2888. if (t1_len + t2_len == 0) {
  2889. if (ac > 2) {
  2890. Z_DVAL_PP(percent) = 0;
  2891. }
  2892. RETURN_LONG(0);
  2893. }
  2894. sim = php_similar_char(t1, t1_len, t2, t2_len);
  2895. if (ac > 2) {
  2896. Z_DVAL_PP(percent) = sim * 200.0 / (t1_len + t2_len);
  2897. }
  2898. RETURN_LONG(sim);
  2899. }
  2900. /* }}} */
  2901. /* {{{ php_stripslashes
  2902. *
  2903. * be careful, this edits the string in-place */
  2904. PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC)
  2905. {
  2906. char *s, *t;
  2907. int l;
  2908. if (len != NULL) {
  2909. l = *len;
  2910. } else {
  2911. l = strlen(str);
  2912. }
  2913. s = str;
  2914. t = str;
  2915. while (l > 0) {
  2916. if (*t == '\\') {
  2917. t++; /* skip the slash */
  2918. if (len != NULL) {
  2919. (*len)--;
  2920. }
  2921. l--;
  2922. if (l > 0) {
  2923. if (*t == '0') {
  2924. *s++='\0';
  2925. t++;
  2926. } else {
  2927. *s++ = *t++; /* preserve the next character */
  2928. }
  2929. l--;
  2930. }
  2931. } else {
  2932. *s++ = *t++;
  2933. l--;
  2934. }
  2935. }
  2936. if (s != t) {
  2937. *s = '\0';
  2938. }
  2939. }
  2940. /* }}} */
  2941. /* {{{ proto string addcslashes(string str, string charlist)
  2942. Escapes all chars mentioned in charlist with backslash. It creates octal representations if asked to backslash characters with 8th bit set or with ASCII<32 (except '\n', '\r', '\t' etc...) */
  2943. PHP_FUNCTION(addcslashes)
  2944. {
  2945. char *str, *what;
  2946. int str_len, what_len;
  2947. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &str, &str_len, &what, &what_len) == FAILURE) {
  2948. return;
  2949. }
  2950. if (str_len == 0) {
  2951. RETURN_EMPTY_STRING();
  2952. }
  2953. if (what_len == 0) {
  2954. RETURN_STRINGL(str, str_len, 1);
  2955. }
  2956. Z_STRVAL_P(return_value) = php_addcslashes(str, str_len, &Z_STRLEN_P(return_value), 0, what, what_len TSRMLS_CC);
  2957. RETURN_STRINGL(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), 0);
  2958. }
  2959. /* }}} */
  2960. /* {{{ proto string addslashes(string str)
  2961. Escapes single quote, double quotes and backslash characters in a string with backslashes */
  2962. PHP_FUNCTION(addslashes)
  2963. {
  2964. char *str;
  2965. int str_len;
  2966. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2967. return;
  2968. }
  2969. if (str_len == 0) {
  2970. RETURN_EMPTY_STRING();
  2971. }
  2972. RETURN_STRING(php_addslashes(str,
  2973. str_len,
  2974. &Z_STRLEN_P(return_value), 0
  2975. TSRMLS_CC), 0);
  2976. }
  2977. /* }}} */
  2978. /* {{{ proto string stripcslashes(string str)
  2979. Strips backslashes from a string. Uses C-style conventions */
  2980. PHP_FUNCTION(stripcslashes)
  2981. {
  2982. char *str;
  2983. int str_len;
  2984. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2985. return;
  2986. }
  2987. ZVAL_STRINGL(return_value, str, str_len, 1);
  2988. php_stripcslashes(Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value));
  2989. }
  2990. /* }}} */
  2991. /* {{{ proto string stripslashes(string str)
  2992. Strips backslashes from a string */
  2993. PHP_FUNCTION(stripslashes)
  2994. {
  2995. char *str;
  2996. int str_len;
  2997. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  2998. return;
  2999. }
  3000. ZVAL_STRINGL(return_value, str, str_len, 1);
  3001. php_stripslashes(Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value) TSRMLS_CC);
  3002. }
  3003. /* }}} */
  3004. #ifndef HAVE_STRERROR
  3005. /* {{{ php_strerror
  3006. */
  3007. char *php_strerror(int errnum)
  3008. {
  3009. extern int sys_nerr;
  3010. extern char *sys_errlist[];
  3011. TSRMLS_FETCH();
  3012. if ((unsigned int) errnum < sys_nerr) {
  3013. return(sys_errlist[errnum]);
  3014. }
  3015. (void) snprintf(BG(str_ebuf), sizeof(php_basic_globals.str_ebuf), "Unknown error: %d", errnum);
  3016. return(BG(str_ebuf));
  3017. }
  3018. /* }}} */
  3019. #endif
  3020. /* {{{ php_stripcslashes
  3021. */
  3022. PHPAPI void php_stripcslashes(char *str, int *len)
  3023. {
  3024. char *source, *target, *end;
  3025. int nlen = *len, i;
  3026. char numtmp[4];
  3027. for (source=str, end=str+nlen, target=str; source < end; source++) {
  3028. if (*source == '\\' && source+1 < end) {
  3029. source++;
  3030. switch (*source) {
  3031. case 'n': *target++='\n'; nlen--; break;
  3032. case 'r': *target++='\r'; nlen--; break;
  3033. case 'a': *target++='\a'; nlen--; break;
  3034. case 't': *target++='\t'; nlen--; break;
  3035. case 'v': *target++='\v'; nlen--; break;
  3036. case 'b': *target++='\b'; nlen--; break;
  3037. case 'f': *target++='\f'; nlen--; break;
  3038. case '\\': *target++='\\'; nlen--; break;
  3039. case 'x':
  3040. if (source+1 < end && isxdigit((int)(*(source+1)))) {
  3041. numtmp[0] = *++source;
  3042. if (source+1 < end && isxdigit((int)(*(source+1)))) {
  3043. numtmp[1] = *++source;
  3044. numtmp[2] = '\0';
  3045. nlen-=3;
  3046. } else {
  3047. numtmp[1] = '\0';
  3048. nlen-=2;
  3049. }
  3050. *target++=(char)strtol(numtmp, NULL, 16);
  3051. break;
  3052. }
  3053. /* break is left intentionally */
  3054. default:
  3055. i=0;
  3056. while (source < end && *source >= '0' && *source <= '7' && i<3) {
  3057. numtmp[i++] = *source++;
  3058. }
  3059. if (i) {
  3060. numtmp[i]='\0';
  3061. *target++=(char)strtol(numtmp, NULL, 8);
  3062. nlen-=i;
  3063. source--;
  3064. } else {
  3065. *target++=*source;
  3066. nlen--;
  3067. }
  3068. }
  3069. } else {
  3070. *target++=*source;
  3071. }
  3072. }
  3073. if (nlen != 0) {
  3074. *target='\0';
  3075. }
  3076. *len = nlen;
  3077. }
  3078. /* }}} */
  3079. /* {{{ php_addcslashes
  3080. */
  3081. PHPAPI char *php_addcslashes(const char *str, int length, int *new_length, int should_free, char *what, int wlength TSRMLS_DC)
  3082. {
  3083. char flags[256];
  3084. char *new_str = safe_emalloc(4, (length?length:(length=strlen(str))), 1);
  3085. char *source, *target;
  3086. char *end;
  3087. char c;
  3088. size_t newlen;
  3089. if (!wlength) {
  3090. wlength = strlen(what);
  3091. }
  3092. php_charmask((unsigned char *)what, wlength, flags TSRMLS_CC);
  3093. for (source = (char*)str, end = source + length, target = new_str; source < end; source++) {
  3094. c = *source;
  3095. if (flags[(unsigned char)c]) {
  3096. if ((unsigned char) c < 32 || (unsigned char) c > 126) {
  3097. *target++ = '\\';
  3098. switch (c) {
  3099. case '\n': *target++ = 'n'; break;
  3100. case '\t': *target++ = 't'; break;
  3101. case '\r': *target++ = 'r'; break;
  3102. case '\a': *target++ = 'a'; break;
  3103. case '\v': *target++ = 'v'; break;
  3104. case '\b': *target++ = 'b'; break;
  3105. case '\f': *target++ = 'f'; break;
  3106. default: target += sprintf(target, "%03o", (unsigned char) c);
  3107. }
  3108. continue;
  3109. }
  3110. *target++ = '\\';
  3111. }
  3112. *target++ = c;
  3113. }
  3114. *target = 0;
  3115. newlen = target - new_str;
  3116. if (UNEXPECTED(newlen > INT_MAX)) {
  3117. efree(new_str);
  3118. zend_error(E_ERROR, "String size overflow");
  3119. }
  3120. if (target - new_str < length * 4) {
  3121. new_str = erealloc(new_str, newlen + 1);
  3122. }
  3123. if (new_length) {
  3124. *new_length = (int)newlen;
  3125. }
  3126. if (should_free) {
  3127. STR_FREE((char*)str);
  3128. }
  3129. return new_str;
  3130. }
  3131. /* }}} */
  3132. /* {{{ php_addslashes
  3133. */
  3134. PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free TSRMLS_DC)
  3135. {
  3136. /* maximum string length, worst case situation */
  3137. char *new_str;
  3138. char *source, *target;
  3139. char *end;
  3140. int local_new_length;
  3141. if (!new_length) {
  3142. new_length = &local_new_length;
  3143. }
  3144. if (!str) {
  3145. *new_length = 0;
  3146. return str;
  3147. }
  3148. new_str = (char *) safe_emalloc(2, (length ? length : (length = strlen(str))), 1);
  3149. source = str;
  3150. end = source + length;
  3151. target = new_str;
  3152. while (source < end) {
  3153. switch (*source) {
  3154. case '\0':
  3155. *target++ = '\\';
  3156. *target++ = '0';
  3157. break;
  3158. case '\'':
  3159. case '\"':
  3160. case '\\':
  3161. *target++ = '\\';
  3162. /* break is missing *intentionally* */
  3163. default:
  3164. *target++ = *source;
  3165. break;
  3166. }
  3167. source++;
  3168. }
  3169. *target = 0;
  3170. *new_length = target - new_str;
  3171. if (UNEXPECTED(*new_length < 0)) {
  3172. zend_error(E_ERROR, "String size overflow");
  3173. }
  3174. if (should_free) {
  3175. STR_FREE(str);
  3176. }
  3177. new_str = (char *) erealloc(new_str, *new_length + 1);
  3178. return new_str;
  3179. }
  3180. /* }}} */
  3181. #define _HEB_BLOCK_TYPE_ENG 1
  3182. #define _HEB_BLOCK_TYPE_HEB 2
  3183. #define isheb(c) (((((unsigned char) c) >= 224) && (((unsigned char) c) <= 250)) ? 1 : 0)
  3184. #define _isblank(c) (((((unsigned char) c) == ' ' || ((unsigned char) c) == '\t')) ? 1 : 0)
  3185. #define _isnewline(c) (((((unsigned char) c) == '\n' || ((unsigned char) c) == '\r')) ? 1 : 0)
  3186. /* {{{ php_char_to_str_ex
  3187. */
  3188. PHPAPI int php_char_to_str_ex(char *str, uint len, char from, char *to, int to_len, zval *result, int case_sensitivity, int *replace_count)
  3189. {
  3190. int char_count = 0;
  3191. int replaced = 0;
  3192. char *source, *target, *tmp, *source_end=str+len, *tmp_end = NULL;
  3193. if (case_sensitivity) {
  3194. char *p = str, *e = p + len;
  3195. while ((p = memchr(p, from, (e - p)))) {
  3196. char_count++;
  3197. p++;
  3198. }
  3199. } else {
  3200. for (source = str; source < source_end; source++) {
  3201. if (tolower(*source) == tolower(from)) {
  3202. char_count++;
  3203. }
  3204. }
  3205. }
  3206. if (char_count == 0 && case_sensitivity) {
  3207. ZVAL_STRINGL(result, str, len, 1);
  3208. return 0;
  3209. }
  3210. Z_STRLEN_P(result) = len + (char_count * (to_len - 1));
  3211. if (Z_STRLEN_P(result) < 0) {
  3212. zend_error(E_ERROR, "String size overflow");
  3213. }
  3214. Z_STRVAL_P(result) = target = safe_emalloc_string(char_count, to_len, len + 1);
  3215. Z_TYPE_P(result) = IS_STRING;
  3216. if (case_sensitivity) {
  3217. char *p = str, *e = p + len, *s = str;
  3218. while ((p = memchr(p, from, (e - p)))) {
  3219. memcpy(target, s, (p - s));
  3220. target += p - s;
  3221. memcpy(target, to, to_len);
  3222. target += to_len;
  3223. p++;
  3224. s = p;
  3225. if (replace_count) {
  3226. *replace_count += 1;
  3227. }
  3228. }
  3229. if (s < e) {
  3230. memcpy(target, s, (e - s));
  3231. target += e - s;
  3232. }
  3233. } else {
  3234. for (source = str; source < source_end; source++) {
  3235. if (tolower(*source) == tolower(from)) {
  3236. replaced = 1;
  3237. if (replace_count) {
  3238. *replace_count += 1;
  3239. }
  3240. for (tmp = to, tmp_end = tmp+to_len; tmp < tmp_end; tmp++) {
  3241. *target = *tmp;
  3242. target++;
  3243. }
  3244. } else {
  3245. *target = *source;
  3246. target++;
  3247. }
  3248. }
  3249. }
  3250. *target = 0;
  3251. return replaced;
  3252. }
  3253. /* }}} */
  3254. /* {{{ php_char_to_str
  3255. */
  3256. PHPAPI int php_char_to_str(char *str, uint len, char from, char *to, int to_len, zval *result)
  3257. {
  3258. return php_char_to_str_ex(str, len, from, to, to_len, result, 1, NULL);
  3259. }
  3260. /* }}} */
  3261. /* {{{ php_str_to_str_ex
  3262. */
  3263. PHPAPI char *php_str_to_str_ex(char *haystack, int length,
  3264. char *needle, int needle_len, char *str, int str_len, int *_new_length, int case_sensitivity, int *replace_count)
  3265. {
  3266. char *new_str;
  3267. if (needle_len < length) {
  3268. char *end, *haystack_dup = NULL, *needle_dup = NULL;
  3269. char *e, *s, *p, *r;
  3270. if (needle_len == str_len) {
  3271. new_str = estrndup(haystack, length);
  3272. *_new_length = length;
  3273. if (case_sensitivity) {
  3274. end = new_str + length;
  3275. for (p = new_str; (r = php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  3276. memcpy(r, str, str_len);
  3277. if (replace_count) {
  3278. (*replace_count)++;
  3279. }
  3280. }
  3281. } else {
  3282. haystack_dup = estrndup(haystack, length);
  3283. needle_dup = estrndup(needle, needle_len);
  3284. php_strtolower(haystack_dup, length);
  3285. php_strtolower(needle_dup, needle_len);
  3286. end = haystack_dup + length;
  3287. for (p = haystack_dup; (r = php_memnstr(p, needle_dup, needle_len, end)); p = r + needle_len) {
  3288. memcpy(new_str + (r - haystack_dup), str, str_len);
  3289. if (replace_count) {
  3290. (*replace_count)++;
  3291. }
  3292. }
  3293. efree(haystack_dup);
  3294. efree(needle_dup);
  3295. }
  3296. return new_str;
  3297. } else {
  3298. if (!case_sensitivity) {
  3299. haystack_dup = estrndup(haystack, length);
  3300. needle_dup = estrndup(needle, needle_len);
  3301. php_strtolower(haystack_dup, length);
  3302. php_strtolower(needle_dup, needle_len);
  3303. }
  3304. if (str_len < needle_len) {
  3305. new_str = emalloc(length + 1);
  3306. } else {
  3307. int count = 0;
  3308. char *o, *n, *endp;
  3309. if (case_sensitivity) {
  3310. o = haystack;
  3311. n = needle;
  3312. } else {
  3313. o = haystack_dup;
  3314. n = needle_dup;
  3315. }
  3316. endp = o + length;
  3317. while ((o = php_memnstr(o, n, needle_len, endp))) {
  3318. o += needle_len;
  3319. count++;
  3320. }
  3321. if (count == 0) {
  3322. /* Needle doesn't occur, shortcircuit the actual replacement. */
  3323. if (haystack_dup) {
  3324. efree(haystack_dup);
  3325. }
  3326. if (needle_dup) {
  3327. efree(needle_dup);
  3328. }
  3329. new_str = estrndup(haystack, length);
  3330. if (_new_length) {
  3331. *_new_length = length;
  3332. }
  3333. return new_str;
  3334. } else {
  3335. new_str = safe_emalloc_string(count, str_len - needle_len, length + 1);
  3336. }
  3337. }
  3338. e = s = new_str;
  3339. if (case_sensitivity) {
  3340. end = haystack + length;
  3341. for (p = haystack; (r = php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  3342. memcpy(e, p, r - p);
  3343. e += r - p;
  3344. memcpy(e, str, str_len);
  3345. e += str_len;
  3346. if (replace_count) {
  3347. (*replace_count)++;
  3348. }
  3349. }
  3350. if (p < end) {
  3351. memcpy(e, p, end - p);
  3352. e += end - p;
  3353. }
  3354. } else {
  3355. end = haystack_dup + length;
  3356. for (p = haystack_dup; (r = php_memnstr(p, needle_dup, needle_len, end)); p = r + needle_len) {
  3357. memcpy(e, haystack + (p - haystack_dup), r - p);
  3358. e += r - p;
  3359. memcpy(e, str, str_len);
  3360. e += str_len;
  3361. if (replace_count) {
  3362. (*replace_count)++;
  3363. }
  3364. }
  3365. if (p < end) {
  3366. memcpy(e, haystack + (p - haystack_dup), end - p);
  3367. e += end - p;
  3368. }
  3369. }
  3370. if (haystack_dup) {
  3371. efree(haystack_dup);
  3372. }
  3373. if (needle_dup) {
  3374. efree(needle_dup);
  3375. }
  3376. *e = '\0';
  3377. *_new_length = e - s;
  3378. new_str = erealloc(new_str, *_new_length + 1);
  3379. return new_str;
  3380. }
  3381. } else if (needle_len > length) {
  3382. nothing_todo:
  3383. *_new_length = length;
  3384. new_str = estrndup(haystack, length);
  3385. return new_str;
  3386. } else {
  3387. if (case_sensitivity && memcmp(haystack, needle, length)) {
  3388. goto nothing_todo;
  3389. } else if (!case_sensitivity) {
  3390. char *l_haystack, *l_needle;
  3391. l_haystack = estrndup(haystack, length);
  3392. l_needle = estrndup(needle, length);
  3393. php_strtolower(l_haystack, length);
  3394. php_strtolower(l_needle, length);
  3395. if (memcmp(l_haystack, l_needle, length)) {
  3396. efree(l_haystack);
  3397. efree(l_needle);
  3398. goto nothing_todo;
  3399. }
  3400. efree(l_haystack);
  3401. efree(l_needle);
  3402. }
  3403. *_new_length = str_len;
  3404. new_str = estrndup(str, str_len);
  3405. if (replace_count) {
  3406. (*replace_count)++;
  3407. }
  3408. return new_str;
  3409. }
  3410. }
  3411. /* }}} */
  3412. /* {{{ php_str_to_str
  3413. */
  3414. PHPAPI char *php_str_to_str(char *haystack, int length,
  3415. char *needle, int needle_len, char *str, int str_len, int *_new_length)
  3416. {
  3417. return php_str_to_str_ex(haystack, length, needle, needle_len, str, str_len, _new_length, 1, NULL);
  3418. }
  3419. /* }}} */
  3420. /* {{{ php_str_replace_in_subject
  3421. */
  3422. static void php_str_replace_in_subject(zval *search, zval *replace, zval **subject, zval *result, int case_sensitivity, int *replace_count)
  3423. {
  3424. zval **search_entry,
  3425. **replace_entry = NULL,
  3426. temp_result;
  3427. char *replace_value = NULL;
  3428. int replace_len = 0;
  3429. /* Make sure we're dealing with strings. */
  3430. convert_to_string_ex(subject);
  3431. Z_TYPE_P(result) = IS_STRING;
  3432. if (Z_STRLEN_PP(subject) == 0) {
  3433. ZVAL_STRINGL(result, "", 0, 1);
  3434. return;
  3435. }
  3436. /* If search is an array */
  3437. if (Z_TYPE_P(search) == IS_ARRAY) {
  3438. /* Duplicate subject string for repeated replacement */
  3439. MAKE_COPY_ZVAL(subject, result);
  3440. zend_hash_internal_pointer_reset(Z_ARRVAL_P(search));
  3441. if (Z_TYPE_P(replace) == IS_ARRAY) {
  3442. zend_hash_internal_pointer_reset(Z_ARRVAL_P(replace));
  3443. } else {
  3444. /* Set replacement value to the passed one */
  3445. replace_value = Z_STRVAL_P(replace);
  3446. replace_len = Z_STRLEN_P(replace);
  3447. }
  3448. /* For each entry in the search array, get the entry */
  3449. while (zend_hash_get_current_data(Z_ARRVAL_P(search), (void **) &search_entry) == SUCCESS) {
  3450. /* Make sure we're dealing with strings. */
  3451. SEPARATE_ZVAL(search_entry);
  3452. convert_to_string(*search_entry);
  3453. if (Z_STRLEN_PP(search_entry) == 0) {
  3454. zend_hash_move_forward(Z_ARRVAL_P(search));
  3455. if (Z_TYPE_P(replace) == IS_ARRAY) {
  3456. zend_hash_move_forward(Z_ARRVAL_P(replace));
  3457. }
  3458. continue;
  3459. }
  3460. /* If replace is an array. */
  3461. if (Z_TYPE_P(replace) == IS_ARRAY) {
  3462. /* Get current entry */
  3463. if (zend_hash_get_current_data(Z_ARRVAL_P(replace), (void **)&replace_entry) == SUCCESS) {
  3464. /* Make sure we're dealing with strings. */
  3465. convert_to_string_ex(replace_entry);
  3466. /* Set replacement value to the one we got from array */
  3467. replace_value = Z_STRVAL_PP(replace_entry);
  3468. replace_len = Z_STRLEN_PP(replace_entry);
  3469. zend_hash_move_forward(Z_ARRVAL_P(replace));
  3470. } else {
  3471. /* We've run out of replacement strings, so use an empty one. */
  3472. replace_value = "";
  3473. replace_len = 0;
  3474. }
  3475. }
  3476. if (Z_STRLEN_PP(search_entry) == 1) {
  3477. php_char_to_str_ex(Z_STRVAL_P(result),
  3478. Z_STRLEN_P(result),
  3479. Z_STRVAL_PP(search_entry)[0],
  3480. replace_value,
  3481. replace_len,
  3482. &temp_result,
  3483. case_sensitivity,
  3484. replace_count);
  3485. } else if (Z_STRLEN_PP(search_entry) > 1) {
  3486. Z_STRVAL(temp_result) = php_str_to_str_ex(Z_STRVAL_P(result), Z_STRLEN_P(result),
  3487. Z_STRVAL_PP(search_entry), Z_STRLEN_PP(search_entry),
  3488. replace_value, replace_len, &Z_STRLEN(temp_result), case_sensitivity, replace_count);
  3489. }
  3490. str_efree(Z_STRVAL_P(result));
  3491. Z_STRVAL_P(result) = Z_STRVAL(temp_result);
  3492. Z_STRLEN_P(result) = Z_STRLEN(temp_result);
  3493. if (Z_STRLEN_P(result) == 0) {
  3494. return;
  3495. }
  3496. zend_hash_move_forward(Z_ARRVAL_P(search));
  3497. }
  3498. } else {
  3499. if (Z_STRLEN_P(search) == 1) {
  3500. php_char_to_str_ex(Z_STRVAL_PP(subject),
  3501. Z_STRLEN_PP(subject),
  3502. Z_STRVAL_P(search)[0],
  3503. Z_STRVAL_P(replace),
  3504. Z_STRLEN_P(replace),
  3505. result,
  3506. case_sensitivity,
  3507. replace_count);
  3508. } else if (Z_STRLEN_P(search) > 1) {
  3509. Z_STRVAL_P(result) = php_str_to_str_ex(Z_STRVAL_PP(subject), Z_STRLEN_PP(subject),
  3510. Z_STRVAL_P(search), Z_STRLEN_P(search),
  3511. Z_STRVAL_P(replace), Z_STRLEN_P(replace), &Z_STRLEN_P(result), case_sensitivity, replace_count);
  3512. } else {
  3513. MAKE_COPY_ZVAL(subject, result);
  3514. }
  3515. }
  3516. }
  3517. /* }}} */
  3518. /* {{{ php_str_replace_common
  3519. */
  3520. static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensitivity)
  3521. {
  3522. zval **subject, **search, **replace, **subject_entry, **zcount = NULL;
  3523. zval *result;
  3524. char *string_key;
  3525. uint string_key_len;
  3526. ulong num_key;
  3527. int count = 0;
  3528. int argc = ZEND_NUM_ARGS();
  3529. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZZ|Z", &search, &replace, &subject, &zcount) == FAILURE) {
  3530. return;
  3531. }
  3532. SEPARATE_ZVAL(search);
  3533. SEPARATE_ZVAL(replace);
  3534. SEPARATE_ZVAL(subject);
  3535. /* Make sure we're dealing with strings and do the replacement. */
  3536. if (Z_TYPE_PP(search) != IS_ARRAY) {
  3537. convert_to_string_ex(search);
  3538. convert_to_string_ex(replace);
  3539. } else if (Z_TYPE_PP(replace) != IS_ARRAY) {
  3540. convert_to_string_ex(replace);
  3541. }
  3542. /* if subject is an array */
  3543. if (Z_TYPE_PP(subject) == IS_ARRAY) {
  3544. array_init(return_value);
  3545. zend_hash_internal_pointer_reset(Z_ARRVAL_PP(subject));
  3546. /* For each subject entry, convert it to string, then perform replacement
  3547. and add the result to the return_value array. */
  3548. while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void **)&subject_entry) == SUCCESS) {
  3549. if (Z_TYPE_PP(subject_entry) != IS_ARRAY && Z_TYPE_PP(subject_entry) != IS_OBJECT) {
  3550. MAKE_STD_ZVAL(result);
  3551. SEPARATE_ZVAL(subject_entry);
  3552. php_str_replace_in_subject(*search, *replace, subject_entry, result, case_sensitivity, (argc > 3) ? &count : NULL);
  3553. } else {
  3554. ALLOC_ZVAL(result);
  3555. Z_ADDREF_P(*subject_entry);
  3556. COPY_PZVAL_TO_ZVAL(*result, *subject_entry);
  3557. }
  3558. /* Add to return array */
  3559. switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(subject), &string_key,
  3560. &string_key_len, &num_key, 0, NULL)) {
  3561. case HASH_KEY_IS_STRING:
  3562. add_assoc_zval_ex(return_value, string_key, string_key_len, result);
  3563. break;
  3564. case HASH_KEY_IS_LONG:
  3565. add_index_zval(return_value, num_key, result);
  3566. break;
  3567. }
  3568. zend_hash_move_forward(Z_ARRVAL_PP(subject));
  3569. }
  3570. } else { /* if subject is not an array */
  3571. php_str_replace_in_subject(*search, *replace, subject, return_value, case_sensitivity, (argc > 3) ? &count : NULL);
  3572. }
  3573. if (argc > 3) {
  3574. zval_dtor(*zcount);
  3575. ZVAL_LONG(*zcount, count);
  3576. }
  3577. }
  3578. /* }}} */
  3579. /* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject [, int &replace_count])
  3580. Replaces all occurrences of search in haystack with replace */
  3581. PHP_FUNCTION(str_replace)
  3582. {
  3583. php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3584. }
  3585. /* }}} */
  3586. /* {{{ proto mixed str_ireplace(mixed search, mixed replace, mixed subject [, int &replace_count])
  3587. Replaces all occurrences of search in haystack with replace / case-insensitive */
  3588. PHP_FUNCTION(str_ireplace)
  3589. {
  3590. php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3591. }
  3592. /* }}} */
  3593. /* {{{ php_hebrev
  3594. *
  3595. * Converts Logical Hebrew text (Hebrew Windows style) to Visual text
  3596. * Cheers/complaints/flames - Zeev Suraski <zeev@php.net>
  3597. */
  3598. static void php_hebrev(INTERNAL_FUNCTION_PARAMETERS, int convert_newlines)
  3599. {
  3600. char *str;
  3601. char *heb_str, *tmp, *target, *broken_str;
  3602. int block_start, block_end, block_type, block_length, i;
  3603. long max_chars=0;
  3604. int begin, end, char_count, orig_begin;
  3605. int str_len;
  3606. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &max_chars) == FAILURE) {
  3607. return;
  3608. }
  3609. if (str_len == 0) {
  3610. RETURN_FALSE;
  3611. }
  3612. tmp = str;
  3613. block_start=block_end=0;
  3614. heb_str = (char *) emalloc(str_len+1);
  3615. target = heb_str+str_len;
  3616. *target = 0;
  3617. target--;
  3618. block_length=0;
  3619. if (isheb(*tmp)) {
  3620. block_type = _HEB_BLOCK_TYPE_HEB;
  3621. } else {
  3622. block_type = _HEB_BLOCK_TYPE_ENG;
  3623. }
  3624. do {
  3625. if (block_type == _HEB_BLOCK_TYPE_HEB) {
  3626. while ((isheb((int)*(tmp+1)) || _isblank((int)*(tmp+1)) || ispunct((int)*(tmp+1)) || (int)*(tmp+1)=='\n' ) && block_end<str_len-1) {
  3627. tmp++;
  3628. block_end++;
  3629. block_length++;
  3630. }
  3631. for (i = block_start; i<= block_end; i++) {
  3632. *target = str[i];
  3633. switch (*target) {
  3634. case '(':
  3635. *target = ')';
  3636. break;
  3637. case ')':
  3638. *target = '(';
  3639. break;
  3640. case '[':
  3641. *target = ']';
  3642. break;
  3643. case ']':
  3644. *target = '[';
  3645. break;
  3646. case '{':
  3647. *target = '}';
  3648. break;
  3649. case '}':
  3650. *target = '{';
  3651. break;
  3652. case '<':
  3653. *target = '>';
  3654. break;
  3655. case '>':
  3656. *target = '<';
  3657. break;
  3658. case '\\':
  3659. *target = '/';
  3660. break;
  3661. case '/':
  3662. *target = '\\';
  3663. break;
  3664. default:
  3665. break;
  3666. }
  3667. target--;
  3668. }
  3669. block_type = _HEB_BLOCK_TYPE_ENG;
  3670. } else {
  3671. while (!isheb(*(tmp+1)) && (int)*(tmp+1)!='\n' && block_end < str_len-1) {
  3672. tmp++;
  3673. block_end++;
  3674. block_length++;
  3675. }
  3676. while ((_isblank((int)*tmp) || ispunct((int)*tmp)) && *tmp!='/' && *tmp!='-' && block_end > block_start) {
  3677. tmp--;
  3678. block_end--;
  3679. }
  3680. for (i = block_end; i >= block_start; i--) {
  3681. *target = str[i];
  3682. target--;
  3683. }
  3684. block_type = _HEB_BLOCK_TYPE_HEB;
  3685. }
  3686. block_start=block_end+1;
  3687. } while (block_end < str_len-1);
  3688. broken_str = (char *) emalloc(str_len+1);
  3689. begin=end=str_len-1;
  3690. target = broken_str;
  3691. while (1) {
  3692. char_count=0;
  3693. while ((!max_chars || char_count < max_chars) && begin > 0) {
  3694. char_count++;
  3695. begin--;
  3696. if (begin <= 0 || _isnewline(heb_str[begin])) {
  3697. while (begin > 0 && _isnewline(heb_str[begin-1])) {
  3698. begin--;
  3699. char_count++;
  3700. }
  3701. break;
  3702. }
  3703. }
  3704. if (char_count == max_chars) { /* try to avoid breaking words */
  3705. int new_char_count=char_count, new_begin=begin;
  3706. while (new_char_count > 0) {
  3707. if (_isblank(heb_str[new_begin]) || _isnewline(heb_str[new_begin])) {
  3708. break;
  3709. }
  3710. new_begin++;
  3711. new_char_count--;
  3712. }
  3713. if (new_char_count > 0) {
  3714. begin=new_begin;
  3715. }
  3716. }
  3717. orig_begin=begin;
  3718. if (_isblank(heb_str[begin])) {
  3719. heb_str[begin]='\n';
  3720. }
  3721. while (begin <= end && _isnewline(heb_str[begin])) { /* skip leading newlines */
  3722. begin++;
  3723. }
  3724. for (i = begin; i <= end; i++) { /* copy content */
  3725. *target = heb_str[i];
  3726. target++;
  3727. }
  3728. for (i = orig_begin; i <= end && _isnewline(heb_str[i]); i++) {
  3729. *target = heb_str[i];
  3730. target++;
  3731. }
  3732. begin=orig_begin;
  3733. if (begin <= 0) {
  3734. *target = 0;
  3735. break;
  3736. }
  3737. begin--;
  3738. end=begin;
  3739. }
  3740. efree(heb_str);
  3741. if (convert_newlines) {
  3742. php_char_to_str(broken_str, str_len,'\n', "<br />\n", 7, return_value);
  3743. efree(broken_str);
  3744. } else {
  3745. Z_STRVAL_P(return_value) = broken_str;
  3746. Z_STRLEN_P(return_value) = str_len;
  3747. Z_TYPE_P(return_value) = IS_STRING;
  3748. }
  3749. }
  3750. /* }}} */
  3751. /* {{{ proto string hebrev(string str [, int max_chars_per_line])
  3752. Converts logical Hebrew text to visual text */
  3753. PHP_FUNCTION(hebrev)
  3754. {
  3755. php_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3756. }
  3757. /* }}} */
  3758. /* {{{ proto string hebrevc(string str [, int max_chars_per_line])
  3759. Converts logical Hebrew text to visual text with newline conversion */
  3760. PHP_FUNCTION(hebrevc)
  3761. {
  3762. php_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3763. }
  3764. /* }}} */
  3765. /* {{{ proto string nl2br(string str [, bool is_xhtml])
  3766. Converts newlines to HTML line breaks */
  3767. PHP_FUNCTION(nl2br)
  3768. {
  3769. /* in brief this inserts <br /> or <br> before matched regexp \n\r?|\r\n? */
  3770. char *tmp, *str;
  3771. size_t new_length;
  3772. char *end, *target;
  3773. int repl_cnt = 0;
  3774. int str_len;
  3775. zend_bool is_xhtml = 1;
  3776. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &is_xhtml) == FAILURE) {
  3777. return;
  3778. }
  3779. tmp = str;
  3780. end = str + str_len;
  3781. /* it is really faster to scan twice and allocate mem once instead of scanning once
  3782. and constantly reallocing */
  3783. while (tmp < end) {
  3784. if (*tmp == '\r') {
  3785. if (*(tmp+1) == '\n') {
  3786. tmp++;
  3787. }
  3788. repl_cnt++;
  3789. } else if (*tmp == '\n') {
  3790. if (*(tmp+1) == '\r') {
  3791. tmp++;
  3792. }
  3793. repl_cnt++;
  3794. }
  3795. tmp++;
  3796. }
  3797. if (repl_cnt == 0) {
  3798. RETURN_STRINGL(str, str_len, 1);
  3799. }
  3800. {
  3801. size_t repl_len = is_xhtml ? (sizeof("<br />") - 1) : (sizeof("<br>") - 1);
  3802. new_length = str_len + repl_cnt * repl_len;
  3803. tmp = target = safe_emalloc_string(repl_cnt, repl_len, str_len + 1);
  3804. }
  3805. while (str < end) {
  3806. switch (*str) {
  3807. case '\r':
  3808. case '\n':
  3809. *target++ = '<';
  3810. *target++ = 'b';
  3811. *target++ = 'r';
  3812. if (is_xhtml) {
  3813. *target++ = ' ';
  3814. *target++ = '/';
  3815. }
  3816. *target++ = '>';
  3817. if ((*str == '\r' && *(str+1) == '\n') || (*str == '\n' && *(str+1) == '\r')) {
  3818. *target++ = *str++;
  3819. }
  3820. /* lack of a break; is intentional */
  3821. default:
  3822. *target++ = *str;
  3823. }
  3824. str++;
  3825. }
  3826. *target = '\0';
  3827. RETURN_STRINGL(tmp, new_length, 0);
  3828. }
  3829. /* }}} */
  3830. /* {{{ proto string strip_tags(string str [, string allowable_tags])
  3831. Strips HTML and PHP tags from a string */
  3832. PHP_FUNCTION(strip_tags)
  3833. {
  3834. char *buf;
  3835. char *str;
  3836. zval **allow=NULL;
  3837. char *allowed_tags=NULL;
  3838. int allowed_tags_len=0;
  3839. int str_len;
  3840. size_t retval_len;
  3841. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Z", &str, &str_len, &allow) == FAILURE) {
  3842. return;
  3843. }
  3844. /* To maintain a certain BC, we allow anything for the second parameter and return original string */
  3845. if (allow != NULL) {
  3846. convert_to_string_ex(allow);
  3847. allowed_tags = Z_STRVAL_PP(allow);
  3848. allowed_tags_len = Z_STRLEN_PP(allow);
  3849. }
  3850. buf = estrndup(str, str_len);
  3851. retval_len = php_strip_tags_ex(buf, str_len, NULL, allowed_tags, allowed_tags_len, 0);
  3852. RETURN_STRINGL(buf, retval_len, 0);
  3853. }
  3854. /* }}} */
  3855. /* {{{ proto string setlocale(mixed category, string locale [, string ...])
  3856. Set locale information */
  3857. PHP_FUNCTION(setlocale)
  3858. {
  3859. zval ***args = NULL;
  3860. zval **pcategory, **plocale;
  3861. int num_args, cat, i = 0;
  3862. char *loc, *retval;
  3863. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z+", &pcategory, &args, &num_args) == FAILURE) {
  3864. return;
  3865. }
  3866. #ifdef HAVE_SETLOCALE
  3867. if (Z_TYPE_PP(pcategory) == IS_LONG) {
  3868. convert_to_long_ex(pcategory);
  3869. cat = Z_LVAL_PP(pcategory);
  3870. } else {
  3871. /* FIXME: The following behaviour should be removed. */
  3872. char *category;
  3873. php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "Passing locale category name as string is deprecated. Use the LC_* -constants instead");
  3874. convert_to_string_ex(pcategory);
  3875. category = Z_STRVAL_PP(pcategory);
  3876. if (!strcasecmp("LC_ALL", category)) {
  3877. cat = LC_ALL;
  3878. } else if (!strcasecmp("LC_COLLATE", category)) {
  3879. cat = LC_COLLATE;
  3880. } else if (!strcasecmp("LC_CTYPE", category)) {
  3881. cat = LC_CTYPE;
  3882. #ifdef LC_MESSAGES
  3883. } else if (!strcasecmp("LC_MESSAGES", category)) {
  3884. cat = LC_MESSAGES;
  3885. #endif
  3886. } else if (!strcasecmp("LC_MONETARY", category)) {
  3887. cat = LC_MONETARY;
  3888. } else if (!strcasecmp("LC_NUMERIC", category)) {
  3889. cat = LC_NUMERIC;
  3890. } else if (!strcasecmp("LC_TIME", category)) {
  3891. cat = LC_TIME;
  3892. } else {
  3893. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid locale category name %s, must be one of LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, or LC_TIME", category);
  3894. if (args) {
  3895. efree(args);
  3896. }
  3897. RETURN_FALSE;
  3898. }
  3899. }
  3900. if (Z_TYPE_PP(args[0]) == IS_ARRAY) {
  3901. zend_hash_internal_pointer_reset(Z_ARRVAL_PP(args[0]));
  3902. }
  3903. while (1) {
  3904. if (Z_TYPE_PP(args[0]) == IS_ARRAY) {
  3905. if (!zend_hash_num_elements(Z_ARRVAL_PP(args[0]))) {
  3906. break;
  3907. }
  3908. zend_hash_get_current_data(Z_ARRVAL_PP(args[0]), (void **)&plocale);
  3909. } else {
  3910. plocale = args[i];
  3911. }
  3912. convert_to_string_ex(plocale);
  3913. if (!strcmp ("0", Z_STRVAL_PP(plocale))) {
  3914. loc = NULL;
  3915. } else {
  3916. loc = Z_STRVAL_PP(plocale);
  3917. if (Z_STRLEN_PP(plocale) >= 255) {
  3918. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Specified locale name is too long");
  3919. break;
  3920. }
  3921. }
  3922. retval = php_my_setlocale(cat, loc);
  3923. zend_update_current_locale();
  3924. if (retval) {
  3925. /* Remember if locale was changed */
  3926. if (loc) {
  3927. STR_FREE(BG(locale_string));
  3928. BG(locale_string) = estrdup(retval);
  3929. }
  3930. if (args) {
  3931. efree(args);
  3932. }
  3933. RETURN_STRING(retval, 1);
  3934. }
  3935. if (Z_TYPE_PP(args[0]) == IS_ARRAY) {
  3936. if (zend_hash_move_forward(Z_ARRVAL_PP(args[0])) == FAILURE) break;
  3937. } else {
  3938. if (++i >= num_args) break;
  3939. }
  3940. }
  3941. #endif
  3942. if (args) {
  3943. efree(args);
  3944. }
  3945. RETURN_FALSE;
  3946. }
  3947. /* }}} */
  3948. /* {{{ proto void parse_str(string encoded_string [, array result])
  3949. Parses GET/POST/COOKIE data and sets global variables */
  3950. PHP_FUNCTION(parse_str)
  3951. {
  3952. char *arg;
  3953. zval *arrayArg = NULL;
  3954. char *res = NULL;
  3955. int arglen;
  3956. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &arg, &arglen, &arrayArg) == FAILURE) {
  3957. return;
  3958. }
  3959. res = estrndup(arg, arglen);
  3960. if (arrayArg == NULL) {
  3961. zval tmp;
  3962. if (!EG(active_symbol_table)) {
  3963. zend_rebuild_symbol_table(TSRMLS_C);
  3964. }
  3965. Z_ARRVAL(tmp) = EG(active_symbol_table);
  3966. sapi_module.treat_data(PARSE_STRING, res, &tmp TSRMLS_CC);
  3967. } else {
  3968. zval ret;
  3969. array_init(&ret);
  3970. sapi_module.treat_data(PARSE_STRING, res, &ret TSRMLS_CC);
  3971. /* Clear out the array that was passed in. */
  3972. zval_dtor(arrayArg);
  3973. ZVAL_COPY_VALUE(arrayArg, &ret);
  3974. }
  3975. }
  3976. /* }}} */
  3977. #define PHP_TAG_BUF_SIZE 1023
  3978. /* {{{ php_tag_find
  3979. *
  3980. * Check if tag is in a set of tags
  3981. *
  3982. * states:
  3983. *
  3984. * 0 start tag
  3985. * 1 first non-whitespace char seen
  3986. */
  3987. int php_tag_find(char *tag, int len, char *set) {
  3988. char c, *n, *t;
  3989. int state=0, done=0;
  3990. char *norm;
  3991. if (len <= 0) {
  3992. return 0;
  3993. }
  3994. norm = emalloc(len+1);
  3995. n = norm;
  3996. t = tag;
  3997. c = tolower(*t);
  3998. /*
  3999. normalize the tag removing leading and trailing whitespace
  4000. and turn any <a whatever...> into just <a> and any </tag>
  4001. into <tag>
  4002. */
  4003. while (!done) {
  4004. switch (c) {
  4005. case '<':
  4006. *(n++) = c;
  4007. break;
  4008. case '>':
  4009. done =1;
  4010. break;
  4011. default:
  4012. if (!isspace((int)c)) {
  4013. if (state == 0) {
  4014. state=1;
  4015. }
  4016. if (c != '/') {
  4017. *(n++) = c;
  4018. }
  4019. } else {
  4020. if (state == 1)
  4021. done=1;
  4022. }
  4023. break;
  4024. }
  4025. c = tolower(*(++t));
  4026. }
  4027. *(n++) = '>';
  4028. *n = '\0';
  4029. if (strstr(set, norm)) {
  4030. done=1;
  4031. } else {
  4032. done=0;
  4033. }
  4034. efree(norm);
  4035. return done;
  4036. }
  4037. /* }}} */
  4038. PHPAPI size_t php_strip_tags(char *rbuf, int len, int *stateptr, char *allow, int allow_len) /* {{{ */
  4039. {
  4040. return php_strip_tags_ex(rbuf, len, stateptr, allow, allow_len, 0);
  4041. }
  4042. /* }}} */
  4043. /* {{{ php_strip_tags
  4044. A simple little state-machine to strip out html and php tags
  4045. State 0 is the output state, State 1 means we are inside a
  4046. normal html tag and state 2 means we are inside a php tag.
  4047. The state variable is passed in to allow a function like fgetss
  4048. to maintain state across calls to the function.
  4049. lc holds the last significant character read and br is a bracket
  4050. counter.
  4051. When an allow string is passed in we keep track of the string
  4052. in state 1 and when the tag is closed check it against the
  4053. allow string to see if we should allow it.
  4054. swm: Added ability to strip <?xml tags without assuming it PHP
  4055. code.
  4056. */
  4057. PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, int allow_len, zend_bool allow_tag_spaces)
  4058. {
  4059. char *tbuf, *buf, *p, *tp, *rp, c, lc;
  4060. int br, i=0, depth=0, in_q = 0;
  4061. int state = 0, pos;
  4062. char *allow_free = NULL;
  4063. char is_xml = 0;
  4064. if (stateptr)
  4065. state = *stateptr;
  4066. buf = estrndup(rbuf, len);
  4067. c = *buf;
  4068. lc = '\0';
  4069. p = buf;
  4070. rp = rbuf;
  4071. br = 0;
  4072. if (allow) {
  4073. if (IS_INTERNED(allow)) {
  4074. allow_free = allow = zend_str_tolower_dup(allow, allow_len);
  4075. } else {
  4076. allow_free = NULL;
  4077. php_strtolower(allow, allow_len);
  4078. }
  4079. tbuf = emalloc(PHP_TAG_BUF_SIZE + 1);
  4080. tp = tbuf;
  4081. } else {
  4082. tbuf = tp = NULL;
  4083. }
  4084. while (i < len) {
  4085. switch (c) {
  4086. case '\0':
  4087. break;
  4088. case '<':
  4089. if (in_q) {
  4090. break;
  4091. }
  4092. if (isspace(*(p + 1)) && !allow_tag_spaces) {
  4093. goto reg_char;
  4094. }
  4095. if (state == 0) {
  4096. lc = '<';
  4097. state = 1;
  4098. if (allow) {
  4099. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4100. pos = tp - tbuf;
  4101. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4102. tp = tbuf + pos;
  4103. }
  4104. *(tp++) = '<';
  4105. }
  4106. } else if (state == 1) {
  4107. depth++;
  4108. }
  4109. break;
  4110. case '(':
  4111. if (state == 2) {
  4112. if (lc != '"' && lc != '\'') {
  4113. lc = '(';
  4114. br++;
  4115. }
  4116. } else if (allow && state == 1) {
  4117. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4118. pos = tp - tbuf;
  4119. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4120. tp = tbuf + pos;
  4121. }
  4122. *(tp++) = c;
  4123. } else if (state == 0) {
  4124. *(rp++) = c;
  4125. }
  4126. break;
  4127. case ')':
  4128. if (state == 2) {
  4129. if (lc != '"' && lc != '\'') {
  4130. lc = ')';
  4131. br--;
  4132. }
  4133. } else if (allow && state == 1) {
  4134. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4135. pos = tp - tbuf;
  4136. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4137. tp = tbuf + pos;
  4138. }
  4139. *(tp++) = c;
  4140. } else if (state == 0) {
  4141. *(rp++) = c;
  4142. }
  4143. break;
  4144. case '>':
  4145. if (depth) {
  4146. depth--;
  4147. break;
  4148. }
  4149. if (in_q) {
  4150. break;
  4151. }
  4152. switch (state) {
  4153. case 1: /* HTML/XML */
  4154. lc = '>';
  4155. if (is_xml && *(p -1) == '-') {
  4156. break;
  4157. }
  4158. in_q = state = is_xml = 0;
  4159. if (allow) {
  4160. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4161. pos = tp - tbuf;
  4162. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4163. tp = tbuf + pos;
  4164. }
  4165. *(tp++) = '>';
  4166. *tp='\0';
  4167. if (php_tag_find(tbuf, tp-tbuf, allow)) {
  4168. memcpy(rp, tbuf, tp-tbuf);
  4169. rp += tp-tbuf;
  4170. }
  4171. tp = tbuf;
  4172. }
  4173. break;
  4174. case 2: /* PHP */
  4175. if (!br && lc != '\"' && *(p-1) == '?') {
  4176. in_q = state = 0;
  4177. tp = tbuf;
  4178. }
  4179. break;
  4180. case 3:
  4181. in_q = state = 0;
  4182. tp = tbuf;
  4183. break;
  4184. case 4: /* JavaScript/CSS/etc... */
  4185. if (p >= buf + 2 && *(p-1) == '-' && *(p-2) == '-') {
  4186. in_q = state = 0;
  4187. tp = tbuf;
  4188. }
  4189. break;
  4190. default:
  4191. *(rp++) = c;
  4192. break;
  4193. }
  4194. break;
  4195. case '"':
  4196. case '\'':
  4197. if (state == 4) {
  4198. /* Inside <!-- comment --> */
  4199. break;
  4200. } else if (state == 2 && *(p-1) != '\\') {
  4201. if (lc == c) {
  4202. lc = '\0';
  4203. } else if (lc != '\\') {
  4204. lc = c;
  4205. }
  4206. } else if (state == 0) {
  4207. *(rp++) = c;
  4208. } else if (allow && state == 1) {
  4209. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4210. pos = tp - tbuf;
  4211. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4212. tp = tbuf + pos;
  4213. }
  4214. *(tp++) = c;
  4215. }
  4216. if (state && p != buf && (state == 1 || *(p-1) != '\\') && (!in_q || *p == in_q)) {
  4217. if (in_q) {
  4218. in_q = 0;
  4219. } else {
  4220. in_q = *p;
  4221. }
  4222. }
  4223. break;
  4224. case '!':
  4225. /* JavaScript & Other HTML scripting languages */
  4226. if (state == 1 && *(p-1) == '<') {
  4227. state = 3;
  4228. lc = c;
  4229. } else {
  4230. if (state == 0) {
  4231. *(rp++) = c;
  4232. } else if (allow && state == 1) {
  4233. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4234. pos = tp - tbuf;
  4235. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4236. tp = tbuf + pos;
  4237. }
  4238. *(tp++) = c;
  4239. }
  4240. }
  4241. break;
  4242. case '-':
  4243. if (state == 3 && p >= buf + 2 && *(p-1) == '-' && *(p-2) == '!') {
  4244. state = 4;
  4245. } else {
  4246. goto reg_char;
  4247. }
  4248. break;
  4249. case '?':
  4250. if (state == 1 && *(p-1) == '<') {
  4251. br=0;
  4252. state=2;
  4253. break;
  4254. }
  4255. case 'E':
  4256. case 'e':
  4257. /* !DOCTYPE exception */
  4258. if (state==3 && p > buf+6
  4259. && tolower(*(p-1)) == 'p'
  4260. && tolower(*(p-2)) == 'y'
  4261. && tolower(*(p-3)) == 't'
  4262. && tolower(*(p-4)) == 'c'
  4263. && tolower(*(p-5)) == 'o'
  4264. && tolower(*(p-6)) == 'd') {
  4265. state = 1;
  4266. break;
  4267. }
  4268. /* fall-through */
  4269. case 'l':
  4270. case 'L':
  4271. /* swm: If we encounter '<?xml' then we shouldn't be in
  4272. * state == 2 (PHP). Switch back to HTML.
  4273. */
  4274. if (state == 2 && p > buf+4 && strncasecmp(p-4, "<?xm", 4) == 0) {
  4275. state = 1; is_xml=1;
  4276. break;
  4277. }
  4278. /* fall-through */
  4279. default:
  4280. reg_char:
  4281. if (state == 0) {
  4282. *(rp++) = c;
  4283. } else if (allow && state == 1) {
  4284. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4285. pos = tp - tbuf;
  4286. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4287. tp = tbuf + pos;
  4288. }
  4289. *(tp++) = c;
  4290. }
  4291. break;
  4292. }
  4293. c = *(++p);
  4294. i++;
  4295. }
  4296. if (rp < rbuf + len) {
  4297. *rp = '\0';
  4298. }
  4299. efree(buf);
  4300. if (allow) {
  4301. efree(tbuf);
  4302. if (allow_free) {
  4303. efree(allow_free);
  4304. }
  4305. }
  4306. if (stateptr)
  4307. *stateptr = state;
  4308. return (size_t)(rp - rbuf);
  4309. }
  4310. /* }}} */
  4311. /* {{{ proto array str_getcsv(string input[, string delimiter[, string enclosure[, string escape]]])
  4312. Parse a CSV string into an array */
  4313. PHP_FUNCTION(str_getcsv)
  4314. {
  4315. char *str, delim = ',', enc = '"', esc = '\\';
  4316. char *delim_str = NULL, *enc_str = NULL, *esc_str = NULL;
  4317. int str_len = 0, delim_len = 0, enc_len = 0, esc_len = 0;
  4318. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss", &str, &str_len, &delim_str, &delim_len,
  4319. &enc_str, &enc_len, &esc_str, &esc_len) == FAILURE) {
  4320. return;
  4321. }
  4322. delim = delim_len ? delim_str[0] : delim;
  4323. enc = enc_len ? enc_str[0] : enc;
  4324. esc = esc_len ? esc_str[0] : esc;
  4325. php_fgetcsv(NULL, delim, enc, esc, str_len, str, return_value TSRMLS_CC);
  4326. }
  4327. /* }}} */
  4328. /* {{{ proto string str_repeat(string input, int mult)
  4329. Returns the input string repeat mult times */
  4330. PHP_FUNCTION(str_repeat)
  4331. {
  4332. char *input_str; /* Input string */
  4333. int input_len;
  4334. long mult; /* Multiplier */
  4335. char *result; /* Resulting string */
  4336. size_t result_len; /* Length of the resulting string */
  4337. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &input_str, &input_len, &mult) == FAILURE) {
  4338. return;
  4339. }
  4340. if (mult < 0) {
  4341. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second argument has to be greater than or equal to 0");
  4342. return;
  4343. }
  4344. /* Don't waste our time if it's empty */
  4345. /* ... or if the multiplier is zero */
  4346. if (input_len == 0 || mult == 0)
  4347. RETURN_EMPTY_STRING();
  4348. /* Initialize the result string */
  4349. result_len = input_len * mult;
  4350. if(result_len > INT_MAX) {
  4351. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Result is too big, maximum %d allowed", INT_MAX);
  4352. RETURN_EMPTY_STRING();
  4353. }
  4354. result = (char *)safe_emalloc(input_len, mult, 1);
  4355. /* Heavy optimization for situations where input string is 1 byte long */
  4356. if (input_len == 1) {
  4357. memset(result, *(input_str), mult);
  4358. } else {
  4359. char *s, *e, *ee;
  4360. int l=0;
  4361. memcpy(result, input_str, input_len);
  4362. s = result;
  4363. e = result + input_len;
  4364. ee = result + result_len;
  4365. while (e<ee) {
  4366. l = (e-s) < (ee-e) ? (e-s) : (ee-e);
  4367. memmove(e, s, l);
  4368. e += l;
  4369. }
  4370. }
  4371. result[result_len] = '\0';
  4372. RETURN_STRINGL(result, result_len, 0);
  4373. }
  4374. /* }}} */
  4375. /* {{{ proto mixed count_chars(string input [, int mode])
  4376. Returns info about what characters are used in input */
  4377. PHP_FUNCTION(count_chars)
  4378. {
  4379. char *input;
  4380. int chars[256];
  4381. long mymode=0;
  4382. unsigned char *buf;
  4383. int len, inx;
  4384. char retstr[256];
  4385. int retlen=0;
  4386. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &input, &len, &mymode) == FAILURE) {
  4387. return;
  4388. }
  4389. if (mymode < 0 || mymode > 4) {
  4390. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown mode");
  4391. RETURN_FALSE;
  4392. }
  4393. buf = (unsigned char *) input;
  4394. memset((void*) chars, 0, sizeof(chars));
  4395. while (len > 0) {
  4396. chars[*buf]++;
  4397. buf++;
  4398. len--;
  4399. }
  4400. if (mymode < 3) {
  4401. array_init(return_value);
  4402. }
  4403. for (inx = 0; inx < 256; inx++) {
  4404. switch (mymode) {
  4405. case 0:
  4406. add_index_long(return_value, inx, chars[inx]);
  4407. break;
  4408. case 1:
  4409. if (chars[inx] != 0) {
  4410. add_index_long(return_value, inx, chars[inx]);
  4411. }
  4412. break;
  4413. case 2:
  4414. if (chars[inx] == 0) {
  4415. add_index_long(return_value, inx, chars[inx]);
  4416. }
  4417. break;
  4418. case 3:
  4419. if (chars[inx] != 0) {
  4420. retstr[retlen++] = inx;
  4421. }
  4422. break;
  4423. case 4:
  4424. if (chars[inx] == 0) {
  4425. retstr[retlen++] = inx;
  4426. }
  4427. break;
  4428. }
  4429. }
  4430. if (mymode >= 3 && mymode <= 4) {
  4431. RETURN_STRINGL(retstr, retlen, 1);
  4432. }
  4433. }
  4434. /* }}} */
  4435. /* {{{ php_strnatcmp
  4436. */
  4437. static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
  4438. {
  4439. char *s1, *s2;
  4440. int s1_len, s2_len;
  4441. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &s1, &s1_len, &s2, &s2_len) == FAILURE) {
  4442. return;
  4443. }
  4444. RETURN_LONG(strnatcmp_ex(s1, s1_len,
  4445. s2, s2_len,
  4446. fold_case));
  4447. }
  4448. /* }}} */
  4449. PHPAPI int string_natural_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive TSRMLS_DC) /* {{{ */
  4450. {
  4451. zval op1_copy, op2_copy;
  4452. int use_copy1 = 0, use_copy2 = 0;
  4453. if (Z_TYPE_P(op1) != IS_STRING) {
  4454. zend_make_printable_zval(op1, &op1_copy, &use_copy1);
  4455. }
  4456. if (Z_TYPE_P(op2) != IS_STRING) {
  4457. zend_make_printable_zval(op2, &op2_copy, &use_copy2);
  4458. }
  4459. if (use_copy1) {
  4460. op1 = &op1_copy;
  4461. }
  4462. if (use_copy2) {
  4463. op2 = &op2_copy;
  4464. }
  4465. ZVAL_LONG(result, strnatcmp_ex(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2), case_insensitive));
  4466. if (use_copy1) {
  4467. zval_dtor(op1);
  4468. }
  4469. if (use_copy2) {
  4470. zval_dtor(op2);
  4471. }
  4472. return SUCCESS;
  4473. }
  4474. /* }}} */
  4475. PHPAPI int string_natural_case_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
  4476. {
  4477. return string_natural_compare_function_ex(result, op1, op2, 1 TSRMLS_CC);
  4478. }
  4479. /* }}} */
  4480. PHPAPI int string_natural_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
  4481. {
  4482. return string_natural_compare_function_ex(result, op1, op2, 0 TSRMLS_CC);
  4483. }
  4484. /* }}} */
  4485. /* {{{ proto int strnatcmp(string s1, string s2)
  4486. Returns the result of string comparison using 'natural' algorithm */
  4487. PHP_FUNCTION(strnatcmp)
  4488. {
  4489. php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  4490. }
  4491. /* }}} */
  4492. /* {{{ proto array localeconv(void)
  4493. Returns numeric formatting information based on the current locale */
  4494. PHP_FUNCTION(localeconv)
  4495. {
  4496. zval *grouping, *mon_grouping;
  4497. int len, i;
  4498. /* We don't need no stinkin' parameters... */
  4499. if (zend_parse_parameters_none() == FAILURE) {
  4500. return;
  4501. }
  4502. MAKE_STD_ZVAL(grouping);
  4503. MAKE_STD_ZVAL(mon_grouping);
  4504. array_init(return_value);
  4505. array_init(grouping);
  4506. array_init(mon_grouping);
  4507. #ifdef HAVE_LOCALECONV
  4508. {
  4509. struct lconv currlocdata;
  4510. localeconv_r( &currlocdata );
  4511. /* Grab the grouping data out of the array */
  4512. len = strlen(currlocdata.grouping);
  4513. for (i = 0; i < len; i++) {
  4514. add_index_long(grouping, i, currlocdata.grouping[i]);
  4515. }
  4516. /* Grab the monetary grouping data out of the array */
  4517. len = strlen(currlocdata.mon_grouping);
  4518. for (i = 0; i < len; i++) {
  4519. add_index_long(mon_grouping, i, currlocdata.mon_grouping[i]);
  4520. }
  4521. add_assoc_string(return_value, "decimal_point", currlocdata.decimal_point, 1);
  4522. add_assoc_string(return_value, "thousands_sep", currlocdata.thousands_sep, 1);
  4523. add_assoc_string(return_value, "int_curr_symbol", currlocdata.int_curr_symbol, 1);
  4524. add_assoc_string(return_value, "currency_symbol", currlocdata.currency_symbol, 1);
  4525. add_assoc_string(return_value, "mon_decimal_point", currlocdata.mon_decimal_point, 1);
  4526. add_assoc_string(return_value, "mon_thousands_sep", currlocdata.mon_thousands_sep, 1);
  4527. add_assoc_string(return_value, "positive_sign", currlocdata.positive_sign, 1);
  4528. add_assoc_string(return_value, "negative_sign", currlocdata.negative_sign, 1);
  4529. add_assoc_long( return_value, "int_frac_digits", currlocdata.int_frac_digits );
  4530. add_assoc_long( return_value, "frac_digits", currlocdata.frac_digits );
  4531. add_assoc_long( return_value, "p_cs_precedes", currlocdata.p_cs_precedes );
  4532. add_assoc_long( return_value, "p_sep_by_space", currlocdata.p_sep_by_space );
  4533. add_assoc_long( return_value, "n_cs_precedes", currlocdata.n_cs_precedes );
  4534. add_assoc_long( return_value, "n_sep_by_space", currlocdata.n_sep_by_space );
  4535. add_assoc_long( return_value, "p_sign_posn", currlocdata.p_sign_posn );
  4536. add_assoc_long( return_value, "n_sign_posn", currlocdata.n_sign_posn );
  4537. }
  4538. #else
  4539. /* Ok, it doesn't look like we have locale info floating around, so I guess it
  4540. wouldn't hurt to just go ahead and return the POSIX locale information? */
  4541. add_index_long(grouping, 0, -1);
  4542. add_index_long(mon_grouping, 0, -1);
  4543. add_assoc_string(return_value, "decimal_point", "\x2E", 1);
  4544. add_assoc_string(return_value, "thousands_sep", "", 1);
  4545. add_assoc_string(return_value, "int_curr_symbol", "", 1);
  4546. add_assoc_string(return_value, "currency_symbol", "", 1);
  4547. add_assoc_string(return_value, "mon_decimal_point", "\x2E", 1);
  4548. add_assoc_string(return_value, "mon_thousands_sep", "", 1);
  4549. add_assoc_string(return_value, "positive_sign", "", 1);
  4550. add_assoc_string(return_value, "negative_sign", "", 1);
  4551. add_assoc_long( return_value, "int_frac_digits", CHAR_MAX );
  4552. add_assoc_long( return_value, "frac_digits", CHAR_MAX );
  4553. add_assoc_long( return_value, "p_cs_precedes", CHAR_MAX );
  4554. add_assoc_long( return_value, "p_sep_by_space", CHAR_MAX );
  4555. add_assoc_long( return_value, "n_cs_precedes", CHAR_MAX );
  4556. add_assoc_long( return_value, "n_sep_by_space", CHAR_MAX );
  4557. add_assoc_long( return_value, "p_sign_posn", CHAR_MAX );
  4558. add_assoc_long( return_value, "n_sign_posn", CHAR_MAX );
  4559. #endif
  4560. zend_hash_update(Z_ARRVAL_P(return_value), "grouping", 9, &grouping, sizeof(zval *), NULL);
  4561. zend_hash_update(Z_ARRVAL_P(return_value), "mon_grouping", 13, &mon_grouping, sizeof(zval *), NULL);
  4562. }
  4563. /* }}} */
  4564. /* {{{ proto int strnatcasecmp(string s1, string s2)
  4565. Returns the result of case-insensitive string comparison using 'natural' algorithm */
  4566. PHP_FUNCTION(strnatcasecmp)
  4567. {
  4568. php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  4569. }
  4570. /* }}} */
  4571. /* {{{ proto int substr_count(string haystack, string needle [, int offset [, int length]])
  4572. Returns the number of times a substring occurs in the string */
  4573. PHP_FUNCTION(substr_count)
  4574. {
  4575. char *haystack, *needle;
  4576. long offset = 0, length = 0;
  4577. int ac = ZEND_NUM_ARGS();
  4578. int count = 0;
  4579. int haystack_len, needle_len;
  4580. char *p, *endp, cmp;
  4581. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &haystack, &haystack_len, &needle, &needle_len, &offset, &length) == FAILURE) {
  4582. return;
  4583. }
  4584. if (needle_len == 0) {
  4585. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty substring");
  4586. RETURN_FALSE;
  4587. }
  4588. p = haystack;
  4589. endp = p + haystack_len;
  4590. if (offset < 0) {
  4591. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset should be greater than or equal to 0");
  4592. RETURN_FALSE;
  4593. }
  4594. if (offset > haystack_len) {
  4595. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset value %ld exceeds string length", offset);
  4596. RETURN_FALSE;
  4597. }
  4598. p += offset;
  4599. if (ac == 4) {
  4600. if (length <= 0) {
  4601. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length should be greater than 0");
  4602. RETURN_FALSE;
  4603. }
  4604. if (length > (haystack_len - offset)) {
  4605. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length value %ld exceeds string length", length);
  4606. RETURN_FALSE;
  4607. }
  4608. endp = p + length;
  4609. }
  4610. if (needle_len == 1) {
  4611. cmp = needle[0];
  4612. while ((p = memchr(p, cmp, endp - p))) {
  4613. count++;
  4614. p++;
  4615. }
  4616. } else {
  4617. while ((p = php_memnstr(p, needle, needle_len, endp))) {
  4618. p += needle_len;
  4619. count++;
  4620. }
  4621. }
  4622. RETURN_LONG(count);
  4623. }
  4624. /* }}} */
  4625. /* {{{ proto string str_pad(string input, int pad_length [, string pad_string [, int pad_type]])
  4626. Returns input string padded on the left or right to specified length with pad_string */
  4627. PHP_FUNCTION(str_pad)
  4628. {
  4629. /* Input arguments */
  4630. char *input; /* Input string */
  4631. int input_len;
  4632. long pad_length; /* Length to pad to */
  4633. /* Helper variables */
  4634. size_t num_pad_chars; /* Number of padding characters (total - input size) */
  4635. char *result = NULL; /* Resulting string */
  4636. int result_len = 0; /* Length of the resulting string */
  4637. char *pad_str_val = " "; /* Pointer to padding string */
  4638. int pad_str_len = 1; /* Length of the padding string */
  4639. long pad_type_val = STR_PAD_RIGHT; /* The padding type value */
  4640. int i, left_pad=0, right_pad=0;
  4641. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|sl", &input, &input_len, &pad_length,
  4642. &pad_str_val, &pad_str_len, &pad_type_val) == FAILURE) {
  4643. return;
  4644. }
  4645. /* If resulting string turns out to be shorter than input string,
  4646. we simply copy the input and return. */
  4647. if (pad_length <= 0 || (pad_length - input_len) <= 0) {
  4648. RETURN_STRINGL(input, input_len, 1);
  4649. }
  4650. if (pad_str_len == 0) {
  4651. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Padding string cannot be empty");
  4652. return;
  4653. }
  4654. if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
  4655. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
  4656. return;
  4657. }
  4658. num_pad_chars = pad_length - input_len;
  4659. if (num_pad_chars >= INT_MAX) {
  4660. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Padding length is too long");
  4661. return;
  4662. }
  4663. result = (char *)safe_emalloc_string(1, input_len, num_pad_chars + 1);
  4664. /* We need to figure out the left/right padding lengths. */
  4665. switch (pad_type_val) {
  4666. case STR_PAD_RIGHT:
  4667. left_pad = 0;
  4668. right_pad = num_pad_chars;
  4669. break;
  4670. case STR_PAD_LEFT:
  4671. left_pad = num_pad_chars;
  4672. right_pad = 0;
  4673. break;
  4674. case STR_PAD_BOTH:
  4675. left_pad = num_pad_chars / 2;
  4676. right_pad = num_pad_chars - left_pad;
  4677. break;
  4678. }
  4679. /* First we pad on the left. */
  4680. for (i = 0; i < left_pad; i++)
  4681. result[result_len++] = pad_str_val[i % pad_str_len];
  4682. /* Then we copy the input string. */
  4683. memcpy(result + result_len, input, input_len);
  4684. result_len += input_len;
  4685. /* Finally, we pad on the right. */
  4686. for (i = 0; i < right_pad; i++)
  4687. result[result_len++] = pad_str_val[i % pad_str_len];
  4688. result[result_len] = '\0';
  4689. RETURN_STRINGL(result, result_len, 0);
  4690. }
  4691. /* }}} */
  4692. /* {{{ proto mixed sscanf(string str, string format [, string ...])
  4693. Implements an ANSI C compatible sscanf */
  4694. PHP_FUNCTION(sscanf)
  4695. {
  4696. zval ***args = NULL;
  4697. char *str, *format;
  4698. int str_len, format_len, result, num_args = 0;
  4699. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss*", &str, &str_len, &format, &format_len,
  4700. &args, &num_args) == FAILURE) {
  4701. return;
  4702. }
  4703. result = php_sscanf_internal(str, format, num_args, args, 0, &return_value TSRMLS_CC);
  4704. if (args) {
  4705. efree(args);
  4706. }
  4707. if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
  4708. WRONG_PARAM_COUNT;
  4709. }
  4710. }
  4711. /* }}} */
  4712. static char rot13_from[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4713. static char rot13_to[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
  4714. /* {{{ proto string str_rot13(string str)
  4715. Perform the rot13 transform on a string */
  4716. PHP_FUNCTION(str_rot13)
  4717. {
  4718. char *arg;
  4719. int arglen;
  4720. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arglen) == FAILURE) {
  4721. return;
  4722. }
  4723. RETVAL_STRINGL(arg, arglen, 1);
  4724. php_strtr(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), rot13_from, rot13_to, 52);
  4725. }
  4726. /* }}} */
  4727. static void php_string_shuffle(char *str, long len TSRMLS_DC) /* {{{ */
  4728. {
  4729. long n_elems, rnd_idx, n_left;
  4730. char temp;
  4731. /* The implementation is stolen from array_data_shuffle */
  4732. /* Thus the characteristics of the randomization are the same */
  4733. n_elems = len;
  4734. if (n_elems <= 1) {
  4735. return;
  4736. }
  4737. n_left = n_elems;
  4738. while (--n_left) {
  4739. rnd_idx = php_rand(TSRMLS_C);
  4740. RAND_RANGE(rnd_idx, 0, n_left, PHP_RAND_MAX);
  4741. if (rnd_idx != n_left) {
  4742. temp = str[n_left];
  4743. str[n_left] = str[rnd_idx];
  4744. str[rnd_idx] = temp;
  4745. }
  4746. }
  4747. }
  4748. /* }}} */
  4749. /* {{{ proto void str_shuffle(string str)
  4750. Shuffles string. One permutation of all possible is created */
  4751. PHP_FUNCTION(str_shuffle)
  4752. {
  4753. char *arg;
  4754. int arglen;
  4755. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arglen) == FAILURE) {
  4756. return;
  4757. }
  4758. RETVAL_STRINGL(arg, arglen, 1);
  4759. if (Z_STRLEN_P(return_value) > 1) {
  4760. php_string_shuffle(Z_STRVAL_P(return_value), (long) Z_STRLEN_P(return_value) TSRMLS_CC);
  4761. }
  4762. }
  4763. /* }}} */
  4764. /* {{{ proto mixed str_word_count(string str, [int format [, string charlist]])
  4765. Counts the number of words inside a string. If format of 1 is specified,
  4766. then the function will return an array containing all the words
  4767. found inside the string. If format of 2 is specified, then the function
  4768. will return an associated array where the position of the word is the key
  4769. and the word itself is the value.
  4770. For the purpose of this function, 'word' is defined as a locale dependent
  4771. string containing alphabetic characters, which also may contain, but not start
  4772. with "'" and "-" characters.
  4773. */
  4774. PHP_FUNCTION(str_word_count)
  4775. {
  4776. char *buf, *str, *char_list = NULL, *p, *e, *s, ch[256];
  4777. int str_len, char_list_len = 0, word_count = 0;
  4778. long type = 0;
  4779. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str, &str_len, &type, &char_list, &char_list_len) == FAILURE) {
  4780. return;
  4781. }
  4782. switch(type) {
  4783. case 1:
  4784. case 2:
  4785. array_init(return_value);
  4786. if (!str_len) {
  4787. return;
  4788. }
  4789. break;
  4790. case 0:
  4791. if (!str_len) {
  4792. RETURN_LONG(0);
  4793. }
  4794. /* nothing to be done */
  4795. break;
  4796. default:
  4797. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid format value %ld", type);
  4798. RETURN_FALSE;
  4799. }
  4800. if (char_list) {
  4801. php_charmask((unsigned char *)char_list, char_list_len, ch TSRMLS_CC);
  4802. }
  4803. p = str;
  4804. e = str + str_len;
  4805. /* first character cannot be ' or -, unless explicitly allowed by the user */
  4806. if ((*p == '\'' && (!char_list || !ch['\''])) || (*p == '-' && (!char_list || !ch['-']))) {
  4807. p++;
  4808. }
  4809. /* last character cannot be -, unless explicitly allowed by the user */
  4810. if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
  4811. e--;
  4812. }
  4813. while (p < e) {
  4814. s = p;
  4815. while (p < e && (isalpha((unsigned char)*p) || (char_list && ch[(unsigned char)*p]) || *p == '\'' || *p == '-')) {
  4816. p++;
  4817. }
  4818. if (p > s) {
  4819. switch (type)
  4820. {
  4821. case 1:
  4822. buf = estrndup(s, (p-s));
  4823. add_next_index_stringl(return_value, buf, (p-s), 0);
  4824. break;
  4825. case 2:
  4826. buf = estrndup(s, (p-s));
  4827. add_index_stringl(return_value, (s - str), buf, p-s, 0);
  4828. break;
  4829. default:
  4830. word_count++;
  4831. break;
  4832. }
  4833. }
  4834. p++;
  4835. }
  4836. if (!type) {
  4837. RETURN_LONG(word_count);
  4838. }
  4839. }
  4840. /* }}} */
  4841. #if HAVE_STRFMON
  4842. /* {{{ proto string money_format(string format , float value)
  4843. Convert monetary value(s) to string */
  4844. PHP_FUNCTION(money_format)
  4845. {
  4846. int format_len = 0, str_len;
  4847. char *format, *str, *p, *e;
  4848. double value;
  4849. zend_bool check = 0;
  4850. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd", &format, &format_len, &value) == FAILURE) {
  4851. return;
  4852. }
  4853. p = format;
  4854. e = p + format_len;
  4855. while ((p = memchr(p, '%', (e - p)))) {
  4856. if (*(p + 1) == '%') {
  4857. p += 2;
  4858. } else if (!check) {
  4859. check = 1;
  4860. p++;
  4861. } else {
  4862. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only a single %%i or %%n token can be used");
  4863. RETURN_FALSE;
  4864. }
  4865. }
  4866. str_len = format_len + 1024;
  4867. str = emalloc(str_len);
  4868. if ((str_len = strfmon(str, str_len, format, value)) < 0) {
  4869. efree(str);
  4870. RETURN_FALSE;
  4871. }
  4872. str[str_len] = 0;
  4873. RETURN_STRINGL(erealloc(str, str_len + 1), str_len, 0);
  4874. }
  4875. /* }}} */
  4876. #endif
  4877. /* {{{ proto array str_split(string str [, int split_length])
  4878. Convert a string to an array. If split_length is specified, break the string down into chunks each split_length characters long. */
  4879. PHP_FUNCTION(str_split)
  4880. {
  4881. char *str;
  4882. int str_len;
  4883. long split_length = 1;
  4884. char *p;
  4885. int n_reg_segments;
  4886. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &split_length) == FAILURE) {
  4887. return;
  4888. }
  4889. if (split_length <= 0) {
  4890. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The length of each segment must be greater than zero");
  4891. RETURN_FALSE;
  4892. }
  4893. array_init_size(return_value, ((str_len - 1) / split_length) + 1);
  4894. if (split_length >= str_len) {
  4895. add_next_index_stringl(return_value, str, str_len, 1);
  4896. return;
  4897. }
  4898. n_reg_segments = str_len / split_length;
  4899. p = str;
  4900. while (n_reg_segments-- > 0) {
  4901. add_next_index_stringl(return_value, p, split_length, 1);
  4902. p += split_length;
  4903. }
  4904. if (p != (str + str_len)) {
  4905. add_next_index_stringl(return_value, p, (str + str_len - p), 1);
  4906. }
  4907. }
  4908. /* }}} */
  4909. /* {{{ proto array strpbrk(string haystack, string char_list)
  4910. Search a string for any of a set of characters */
  4911. PHP_FUNCTION(strpbrk)
  4912. {
  4913. char *haystack, *char_list;
  4914. int haystack_len, char_list_len;
  4915. char *haystack_ptr, *cl_ptr;
  4916. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &haystack, &haystack_len, &char_list, &char_list_len) == FAILURE) {
  4917. RETURN_FALSE;
  4918. }
  4919. if (!char_list_len) {
  4920. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The character list cannot be empty");
  4921. RETURN_FALSE;
  4922. }
  4923. for (haystack_ptr = haystack; haystack_ptr < (haystack + haystack_len); ++haystack_ptr) {
  4924. for (cl_ptr = char_list; cl_ptr < (char_list + char_list_len); ++cl_ptr) {
  4925. if (*cl_ptr == *haystack_ptr) {
  4926. RETURN_STRINGL(haystack_ptr, (haystack + haystack_len - haystack_ptr), 1);
  4927. }
  4928. }
  4929. }
  4930. RETURN_FALSE;
  4931. }
  4932. /* }}} */
  4933. /* {{{ proto int substr_compare(string main_str, string str, int offset [, int length [, bool case_sensitivity]])
  4934. Binary safe optionally case insensitive comparison of 2 strings from an offset, up to length characters */
  4935. PHP_FUNCTION(substr_compare)
  4936. {
  4937. char *s1, *s2;
  4938. int s1_len, s2_len;
  4939. long offset, len=0;
  4940. zend_bool cs=0;
  4941. uint cmp_len;
  4942. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|lb", &s1, &s1_len, &s2, &s2_len, &offset, &len, &cs) == FAILURE) {
  4943. RETURN_FALSE;
  4944. }
  4945. if (ZEND_NUM_ARGS() >= 4 && len <= 0) {
  4946. if (len == 0) {
  4947. RETURN_LONG(0L);
  4948. } else {
  4949. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The length must be greater than or equal to zero");
  4950. RETURN_FALSE;
  4951. }
  4952. }
  4953. if (offset < 0) {
  4954. offset = s1_len + offset;
  4955. offset = (offset < 0) ? 0 : offset;
  4956. }
  4957. if (offset >= s1_len) {
  4958. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The start position cannot exceed initial string length");
  4959. RETURN_FALSE;
  4960. }
  4961. cmp_len = (uint) (len ? len : MAX(s2_len, (s1_len - offset)));
  4962. if (!cs) {
  4963. RETURN_LONG(zend_binary_strncmp(s1 + offset, (s1_len - offset), s2, s2_len, cmp_len));
  4964. } else {
  4965. RETURN_LONG(zend_binary_strncasecmp_l(s1 + offset, (s1_len - offset), s2, s2_len, cmp_len));
  4966. }
  4967. }
  4968. /* }}} */
  4969. /*
  4970. * Local variables:
  4971. * tab-width: 4
  4972. * c-basic-offset: 4
  4973. * End:
  4974. * vim600: noet sw=4 ts=4 fdm=marker
  4975. * vim<600: noet sw=4 ts=4
  4976. */