mbstring.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823
  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. | Author: Tsukada Takuya <tsukada@fminn.nagano.nagano.jp> |
  16. | Rui Hirokawa <hirokawa@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. /*
  21. * PHP 4 Multibyte String module "mbstring"
  22. *
  23. * History:
  24. * 2000.5.19 Release php-4.0RC2_jstring-1.0
  25. * 2001.4.1 Release php4_jstring-1.0.91
  26. * 2001.4.30 Release php4_jstring-1.1 (contribute to The PHP Group)
  27. * 2001.5.1 Renamed from jstring to mbstring (hirokawa@php.net)
  28. */
  29. /*
  30. * PHP3 Internationalization support program.
  31. *
  32. * Copyright (c) 1999,2000 by the PHP3 internationalization team.
  33. * All rights reserved.
  34. *
  35. * See README_PHP3-i18n-ja for more detail.
  36. *
  37. * Authors:
  38. * Hironori Sato <satoh@jpnnet.com>
  39. * Shigeru Kanemoto <sgk@happysize.co.jp>
  40. * Tsukada Takuya <tsukada@fminn.nagano.nagano.jp>
  41. * Rui Hirokawa <rui_hirokawa@ybb.ne.jp>
  42. */
  43. /* {{{ includes */
  44. #ifdef HAVE_CONFIG_H
  45. #include "config.h"
  46. #endif
  47. #include "php.h"
  48. #include "php_ini.h"
  49. #include "php_variables.h"
  50. #include "mbstring.h"
  51. #include "ext/standard/php_string.h"
  52. #include "ext/standard/php_mail.h"
  53. #include "ext/standard/exec.h"
  54. #include "ext/standard/php_smart_str.h"
  55. #include "ext/standard/url.h"
  56. #include "main/php_output.h"
  57. #include "ext/standard/info.h"
  58. #include "libmbfl/mbfl/mbfl_allocators.h"
  59. #include "libmbfl/mbfl/mbfilter_pass.h"
  60. #include "php_variables.h"
  61. #include "php_globals.h"
  62. #include "rfc1867.h"
  63. #include "php_content_types.h"
  64. #include "SAPI.h"
  65. #include "php_unicode.h"
  66. #include "TSRM.h"
  67. #include "mb_gpc.h"
  68. #if HAVE_MBREGEX
  69. #include "php_mbregex.h"
  70. #endif
  71. #include "zend_multibyte.h"
  72. #if HAVE_ONIG
  73. #include "php_onig_compat.h"
  74. #include <oniguruma.h>
  75. #undef UChar
  76. #elif HAVE_PCRE || HAVE_BUNDLED_PCRE
  77. #include "ext/pcre/php_pcre.h"
  78. #endif
  79. /* }}} */
  80. #if HAVE_MBSTRING
  81. /* {{{ prototypes */
  82. ZEND_DECLARE_MODULE_GLOBALS(mbstring)
  83. static PHP_GINIT_FUNCTION(mbstring);
  84. static PHP_GSHUTDOWN_FUNCTION(mbstring);
  85. static void php_mb_populate_current_detect_order_list(TSRMLS_D);
  86. static int php_mb_encoding_translation(TSRMLS_D);
  87. static void php_mb_gpc_get_detect_order(const zend_encoding ***list, size_t *list_size TSRMLS_DC);
  88. static void php_mb_gpc_set_input_encoding(const zend_encoding *encoding TSRMLS_DC);
  89. /* }}} */
  90. /* {{{ php_mb_default_identify_list */
  91. typedef struct _php_mb_nls_ident_list {
  92. enum mbfl_no_language lang;
  93. const enum mbfl_no_encoding *list;
  94. size_t list_size;
  95. } php_mb_nls_ident_list;
  96. static const enum mbfl_no_encoding php_mb_default_identify_list_ja[] = {
  97. mbfl_no_encoding_ascii,
  98. mbfl_no_encoding_jis,
  99. mbfl_no_encoding_utf8,
  100. mbfl_no_encoding_euc_jp,
  101. mbfl_no_encoding_sjis
  102. };
  103. static const enum mbfl_no_encoding php_mb_default_identify_list_cn[] = {
  104. mbfl_no_encoding_ascii,
  105. mbfl_no_encoding_utf8,
  106. mbfl_no_encoding_euc_cn,
  107. mbfl_no_encoding_cp936
  108. };
  109. static const enum mbfl_no_encoding php_mb_default_identify_list_tw_hk[] = {
  110. mbfl_no_encoding_ascii,
  111. mbfl_no_encoding_utf8,
  112. mbfl_no_encoding_euc_tw,
  113. mbfl_no_encoding_big5
  114. };
  115. static const enum mbfl_no_encoding php_mb_default_identify_list_kr[] = {
  116. mbfl_no_encoding_ascii,
  117. mbfl_no_encoding_utf8,
  118. mbfl_no_encoding_euc_kr,
  119. mbfl_no_encoding_uhc
  120. };
  121. static const enum mbfl_no_encoding php_mb_default_identify_list_ru[] = {
  122. mbfl_no_encoding_ascii,
  123. mbfl_no_encoding_utf8,
  124. mbfl_no_encoding_koi8r,
  125. mbfl_no_encoding_cp1251,
  126. mbfl_no_encoding_cp866
  127. };
  128. static const enum mbfl_no_encoding php_mb_default_identify_list_hy[] = {
  129. mbfl_no_encoding_ascii,
  130. mbfl_no_encoding_utf8,
  131. mbfl_no_encoding_armscii8
  132. };
  133. static const enum mbfl_no_encoding php_mb_default_identify_list_tr[] = {
  134. mbfl_no_encoding_ascii,
  135. mbfl_no_encoding_utf8,
  136. mbfl_no_encoding_cp1254,
  137. mbfl_no_encoding_8859_9
  138. };
  139. static const enum mbfl_no_encoding php_mb_default_identify_list_ua[] = {
  140. mbfl_no_encoding_ascii,
  141. mbfl_no_encoding_utf8,
  142. mbfl_no_encoding_koi8u
  143. };
  144. static const enum mbfl_no_encoding php_mb_default_identify_list_neut[] = {
  145. mbfl_no_encoding_ascii,
  146. mbfl_no_encoding_utf8
  147. };
  148. static const php_mb_nls_ident_list php_mb_default_identify_list[] = {
  149. { mbfl_no_language_japanese, php_mb_default_identify_list_ja, sizeof(php_mb_default_identify_list_ja) / sizeof(php_mb_default_identify_list_ja[0]) },
  150. { mbfl_no_language_korean, php_mb_default_identify_list_kr, sizeof(php_mb_default_identify_list_kr) / sizeof(php_mb_default_identify_list_kr[0]) },
  151. { mbfl_no_language_traditional_chinese, php_mb_default_identify_list_tw_hk, sizeof(php_mb_default_identify_list_tw_hk) / sizeof(php_mb_default_identify_list_tw_hk[0]) },
  152. { mbfl_no_language_simplified_chinese, php_mb_default_identify_list_cn, sizeof(php_mb_default_identify_list_cn) / sizeof(php_mb_default_identify_list_cn[0]) },
  153. { mbfl_no_language_russian, php_mb_default_identify_list_ru, sizeof(php_mb_default_identify_list_ru) / sizeof(php_mb_default_identify_list_ru[0]) },
  154. { mbfl_no_language_armenian, php_mb_default_identify_list_hy, sizeof(php_mb_default_identify_list_hy) / sizeof(php_mb_default_identify_list_hy[0]) },
  155. { mbfl_no_language_turkish, php_mb_default_identify_list_tr, sizeof(php_mb_default_identify_list_tr) / sizeof(php_mb_default_identify_list_tr[0]) },
  156. { mbfl_no_language_ukrainian, php_mb_default_identify_list_ua, sizeof(php_mb_default_identify_list_ua) / sizeof(php_mb_default_identify_list_ua[0]) },
  157. { mbfl_no_language_neutral, php_mb_default_identify_list_neut, sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]) }
  158. };
  159. /* }}} */
  160. /* {{{ mb_overload_def mb_ovld[] */
  161. static const struct mb_overload_def mb_ovld[] = {
  162. {MB_OVERLOAD_MAIL, "mail", "mb_send_mail", "mb_orig_mail"},
  163. {MB_OVERLOAD_STRING, "strlen", "mb_strlen", "mb_orig_strlen"},
  164. {MB_OVERLOAD_STRING, "strpos", "mb_strpos", "mb_orig_strpos"},
  165. {MB_OVERLOAD_STRING, "strrpos", "mb_strrpos", "mb_orig_strrpos"},
  166. {MB_OVERLOAD_STRING, "stripos", "mb_stripos", "mb_orig_stripos"},
  167. {MB_OVERLOAD_STRING, "strripos", "mb_strripos", "mb_orig_strripos"},
  168. {MB_OVERLOAD_STRING, "strstr", "mb_strstr", "mb_orig_strstr"},
  169. {MB_OVERLOAD_STRING, "strrchr", "mb_strrchr", "mb_orig_strrchr"},
  170. {MB_OVERLOAD_STRING, "stristr", "mb_stristr", "mb_orig_stristr"},
  171. {MB_OVERLOAD_STRING, "substr", "mb_substr", "mb_orig_substr"},
  172. {MB_OVERLOAD_STRING, "strtolower", "mb_strtolower", "mb_orig_strtolower"},
  173. {MB_OVERLOAD_STRING, "strtoupper", "mb_strtoupper", "mb_orig_strtoupper"},
  174. {MB_OVERLOAD_STRING, "substr_count", "mb_substr_count", "mb_orig_substr_count"},
  175. #if HAVE_MBREGEX
  176. {MB_OVERLOAD_REGEX, "ereg", "mb_ereg", "mb_orig_ereg"},
  177. {MB_OVERLOAD_REGEX, "eregi", "mb_eregi", "mb_orig_eregi"},
  178. {MB_OVERLOAD_REGEX, "ereg_replace", "mb_ereg_replace", "mb_orig_ereg_replace"},
  179. {MB_OVERLOAD_REGEX, "eregi_replace", "mb_eregi_replace", "mb_orig_eregi_replace"},
  180. {MB_OVERLOAD_REGEX, "split", "mb_split", "mb_orig_split"},
  181. #endif
  182. {0, NULL, NULL, NULL}
  183. };
  184. /* }}} */
  185. /* {{{ arginfo */
  186. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_language, 0, 0, 0)
  187. ZEND_ARG_INFO(0, language)
  188. ZEND_END_ARG_INFO()
  189. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_internal_encoding, 0, 0, 0)
  190. ZEND_ARG_INFO(0, encoding)
  191. ZEND_END_ARG_INFO()
  192. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_http_input, 0, 0, 0)
  193. ZEND_ARG_INFO(0, type)
  194. ZEND_END_ARG_INFO()
  195. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_http_output, 0, 0, 0)
  196. ZEND_ARG_INFO(0, encoding)
  197. ZEND_END_ARG_INFO()
  198. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_detect_order, 0, 0, 0)
  199. ZEND_ARG_INFO(0, encoding)
  200. ZEND_END_ARG_INFO()
  201. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_substitute_character, 0, 0, 0)
  202. ZEND_ARG_INFO(0, substchar)
  203. ZEND_END_ARG_INFO()
  204. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_preferred_mime_name, 0, 0, 1)
  205. ZEND_ARG_INFO(0, encoding)
  206. ZEND_END_ARG_INFO()
  207. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_parse_str, 0, 0, 1)
  208. ZEND_ARG_INFO(0, encoded_string)
  209. ZEND_ARG_INFO(1, result)
  210. ZEND_END_ARG_INFO()
  211. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_output_handler, 0, 0, 2)
  212. ZEND_ARG_INFO(0, contents)
  213. ZEND_ARG_INFO(0, status)
  214. ZEND_END_ARG_INFO()
  215. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strlen, 0, 0, 1)
  216. ZEND_ARG_INFO(0, str)
  217. ZEND_ARG_INFO(0, encoding)
  218. ZEND_END_ARG_INFO()
  219. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strpos, 0, 0, 2)
  220. ZEND_ARG_INFO(0, haystack)
  221. ZEND_ARG_INFO(0, needle)
  222. ZEND_ARG_INFO(0, offset)
  223. ZEND_ARG_INFO(0, encoding)
  224. ZEND_END_ARG_INFO()
  225. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strrpos, 0, 0, 2)
  226. ZEND_ARG_INFO(0, haystack)
  227. ZEND_ARG_INFO(0, needle)
  228. ZEND_ARG_INFO(0, offset)
  229. ZEND_ARG_INFO(0, encoding)
  230. ZEND_END_ARG_INFO()
  231. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_stripos, 0, 0, 2)
  232. ZEND_ARG_INFO(0, haystack)
  233. ZEND_ARG_INFO(0, needle)
  234. ZEND_ARG_INFO(0, offset)
  235. ZEND_ARG_INFO(0, encoding)
  236. ZEND_END_ARG_INFO()
  237. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strripos, 0, 0, 2)
  238. ZEND_ARG_INFO(0, haystack)
  239. ZEND_ARG_INFO(0, needle)
  240. ZEND_ARG_INFO(0, offset)
  241. ZEND_ARG_INFO(0, encoding)
  242. ZEND_END_ARG_INFO()
  243. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strstr, 0, 0, 2)
  244. ZEND_ARG_INFO(0, haystack)
  245. ZEND_ARG_INFO(0, needle)
  246. ZEND_ARG_INFO(0, part)
  247. ZEND_ARG_INFO(0, encoding)
  248. ZEND_END_ARG_INFO()
  249. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strrchr, 0, 0, 2)
  250. ZEND_ARG_INFO(0, haystack)
  251. ZEND_ARG_INFO(0, needle)
  252. ZEND_ARG_INFO(0, part)
  253. ZEND_ARG_INFO(0, encoding)
  254. ZEND_END_ARG_INFO()
  255. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_stristr, 0, 0, 2)
  256. ZEND_ARG_INFO(0, haystack)
  257. ZEND_ARG_INFO(0, needle)
  258. ZEND_ARG_INFO(0, part)
  259. ZEND_ARG_INFO(0, encoding)
  260. ZEND_END_ARG_INFO()
  261. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strrichr, 0, 0, 2)
  262. ZEND_ARG_INFO(0, haystack)
  263. ZEND_ARG_INFO(0, needle)
  264. ZEND_ARG_INFO(0, part)
  265. ZEND_ARG_INFO(0, encoding)
  266. ZEND_END_ARG_INFO()
  267. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_substr_count, 0, 0, 2)
  268. ZEND_ARG_INFO(0, haystack)
  269. ZEND_ARG_INFO(0, needle)
  270. ZEND_ARG_INFO(0, encoding)
  271. ZEND_END_ARG_INFO()
  272. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_substr, 0, 0, 2)
  273. ZEND_ARG_INFO(0, str)
  274. ZEND_ARG_INFO(0, start)
  275. ZEND_ARG_INFO(0, length)
  276. ZEND_ARG_INFO(0, encoding)
  277. ZEND_END_ARG_INFO()
  278. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strcut, 0, 0, 2)
  279. ZEND_ARG_INFO(0, str)
  280. ZEND_ARG_INFO(0, start)
  281. ZEND_ARG_INFO(0, length)
  282. ZEND_ARG_INFO(0, encoding)
  283. ZEND_END_ARG_INFO()
  284. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strwidth, 0, 0, 1)
  285. ZEND_ARG_INFO(0, str)
  286. ZEND_ARG_INFO(0, encoding)
  287. ZEND_END_ARG_INFO()
  288. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strimwidth, 0, 0, 3)
  289. ZEND_ARG_INFO(0, str)
  290. ZEND_ARG_INFO(0, start)
  291. ZEND_ARG_INFO(0, width)
  292. ZEND_ARG_INFO(0, trimmarker)
  293. ZEND_ARG_INFO(0, encoding)
  294. ZEND_END_ARG_INFO()
  295. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_convert_encoding, 0, 0, 2)
  296. ZEND_ARG_INFO(0, str)
  297. ZEND_ARG_INFO(0, to)
  298. ZEND_ARG_INFO(0, from)
  299. ZEND_END_ARG_INFO()
  300. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_convert_case, 0, 0, 2)
  301. ZEND_ARG_INFO(0, sourcestring)
  302. ZEND_ARG_INFO(0, mode)
  303. ZEND_ARG_INFO(0, encoding)
  304. ZEND_END_ARG_INFO()
  305. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strtoupper, 0, 0, 1)
  306. ZEND_ARG_INFO(0, sourcestring)
  307. ZEND_ARG_INFO(0, encoding)
  308. ZEND_END_ARG_INFO()
  309. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_strtolower, 0, 0, 1)
  310. ZEND_ARG_INFO(0, sourcestring)
  311. ZEND_ARG_INFO(0, encoding)
  312. ZEND_END_ARG_INFO()
  313. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_detect_encoding, 0, 0, 1)
  314. ZEND_ARG_INFO(0, str)
  315. ZEND_ARG_INFO(0, encoding_list)
  316. ZEND_ARG_INFO(0, strict)
  317. ZEND_END_ARG_INFO()
  318. ZEND_BEGIN_ARG_INFO(arginfo_mb_list_encodings, 0)
  319. ZEND_END_ARG_INFO()
  320. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_encoding_aliases, 0, 0, 1)
  321. ZEND_ARG_INFO(0, encoding)
  322. ZEND_END_ARG_INFO()
  323. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_encode_mimeheader, 0, 0, 1)
  324. ZEND_ARG_INFO(0, str)
  325. ZEND_ARG_INFO(0, charset)
  326. ZEND_ARG_INFO(0, transfer)
  327. ZEND_ARG_INFO(0, linefeed)
  328. ZEND_ARG_INFO(0, indent)
  329. ZEND_END_ARG_INFO()
  330. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_decode_mimeheader, 0, 0, 1)
  331. ZEND_ARG_INFO(0, string)
  332. ZEND_END_ARG_INFO()
  333. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_convert_kana, 0, 0, 1)
  334. ZEND_ARG_INFO(0, str)
  335. ZEND_ARG_INFO(0, option)
  336. ZEND_ARG_INFO(0, encoding)
  337. ZEND_END_ARG_INFO()
  338. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_convert_variables, 0, 0, 3)
  339. ZEND_ARG_INFO(0, to)
  340. ZEND_ARG_INFO(0, from)
  341. ZEND_ARG_VARIADIC_INFO(1, vars)
  342. ZEND_END_ARG_INFO()
  343. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_encode_numericentity, 0, 0, 2)
  344. ZEND_ARG_INFO(0, string)
  345. ZEND_ARG_INFO(0, convmap)
  346. ZEND_ARG_INFO(0, encoding)
  347. ZEND_ARG_INFO(0, is_hex)
  348. ZEND_END_ARG_INFO()
  349. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_decode_numericentity, 0, 0, 2)
  350. ZEND_ARG_INFO(0, string)
  351. ZEND_ARG_INFO(0, convmap)
  352. ZEND_ARG_INFO(0, encoding)
  353. ZEND_END_ARG_INFO()
  354. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_send_mail, 0, 0, 3)
  355. ZEND_ARG_INFO(0, to)
  356. ZEND_ARG_INFO(0, subject)
  357. ZEND_ARG_INFO(0, message)
  358. ZEND_ARG_INFO(0, additional_headers)
  359. ZEND_ARG_INFO(0, additional_parameters)
  360. ZEND_END_ARG_INFO()
  361. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_get_info, 0, 0, 0)
  362. ZEND_ARG_INFO(0, type)
  363. ZEND_END_ARG_INFO()
  364. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_check_encoding, 0, 0, 0)
  365. ZEND_ARG_INFO(0, var)
  366. ZEND_ARG_INFO(0, encoding)
  367. ZEND_END_ARG_INFO()
  368. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_regex_encoding, 0, 0, 0)
  369. ZEND_ARG_INFO(0, encoding)
  370. ZEND_END_ARG_INFO()
  371. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg, 0, 0, 2)
  372. ZEND_ARG_INFO(0, pattern)
  373. ZEND_ARG_INFO(0, string)
  374. ZEND_ARG_INFO(1, registers)
  375. ZEND_END_ARG_INFO()
  376. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_eregi, 0, 0, 2)
  377. ZEND_ARG_INFO(0, pattern)
  378. ZEND_ARG_INFO(0, string)
  379. ZEND_ARG_INFO(1, registers)
  380. ZEND_END_ARG_INFO()
  381. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_replace, 0, 0, 3)
  382. ZEND_ARG_INFO(0, pattern)
  383. ZEND_ARG_INFO(0, replacement)
  384. ZEND_ARG_INFO(0, string)
  385. ZEND_ARG_INFO(0, option)
  386. ZEND_END_ARG_INFO()
  387. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_eregi_replace, 0, 0, 3)
  388. ZEND_ARG_INFO(0, pattern)
  389. ZEND_ARG_INFO(0, replacement)
  390. ZEND_ARG_INFO(0, string)
  391. ZEND_END_ARG_INFO()
  392. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_replace_callback, 0, 0, 3)
  393. ZEND_ARG_INFO(0, pattern)
  394. ZEND_ARG_INFO(0, callback)
  395. ZEND_ARG_INFO(0, string)
  396. ZEND_ARG_INFO(0, option)
  397. ZEND_END_ARG_INFO()
  398. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_split, 0, 0, 2)
  399. ZEND_ARG_INFO(0, pattern)
  400. ZEND_ARG_INFO(0, string)
  401. ZEND_ARG_INFO(0, limit)
  402. ZEND_END_ARG_INFO()
  403. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_match, 0, 0, 2)
  404. ZEND_ARG_INFO(0, pattern)
  405. ZEND_ARG_INFO(0, string)
  406. ZEND_ARG_INFO(0, option)
  407. ZEND_END_ARG_INFO()
  408. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_search, 0, 0, 0)
  409. ZEND_ARG_INFO(0, pattern)
  410. ZEND_ARG_INFO(0, option)
  411. ZEND_END_ARG_INFO()
  412. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_search_pos, 0, 0, 0)
  413. ZEND_ARG_INFO(0, pattern)
  414. ZEND_ARG_INFO(0, option)
  415. ZEND_END_ARG_INFO()
  416. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_search_regs, 0, 0, 0)
  417. ZEND_ARG_INFO(0, pattern)
  418. ZEND_ARG_INFO(0, option)
  419. ZEND_END_ARG_INFO()
  420. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_search_init, 0, 0, 1)
  421. ZEND_ARG_INFO(0, string)
  422. ZEND_ARG_INFO(0, pattern)
  423. ZEND_ARG_INFO(0, option)
  424. ZEND_END_ARG_INFO()
  425. ZEND_BEGIN_ARG_INFO(arginfo_mb_ereg_search_getregs, 0)
  426. ZEND_END_ARG_INFO()
  427. ZEND_BEGIN_ARG_INFO(arginfo_mb_ereg_search_getpos, 0)
  428. ZEND_END_ARG_INFO()
  429. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_ereg_search_setpos, 0, 0, 1)
  430. ZEND_ARG_INFO(0, position)
  431. ZEND_END_ARG_INFO()
  432. ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_regex_set_options, 0, 0, 0)
  433. ZEND_ARG_INFO(0, options)
  434. ZEND_END_ARG_INFO()
  435. /* }}} */
  436. /* {{{ zend_function_entry mbstring_functions[] */
  437. const zend_function_entry mbstring_functions[] = {
  438. PHP_FE(mb_convert_case, arginfo_mb_convert_case)
  439. PHP_FE(mb_strtoupper, arginfo_mb_strtoupper)
  440. PHP_FE(mb_strtolower, arginfo_mb_strtolower)
  441. PHP_FE(mb_language, arginfo_mb_language)
  442. PHP_FE(mb_internal_encoding, arginfo_mb_internal_encoding)
  443. PHP_FE(mb_http_input, arginfo_mb_http_input)
  444. PHP_FE(mb_http_output, arginfo_mb_http_output)
  445. PHP_FE(mb_detect_order, arginfo_mb_detect_order)
  446. PHP_FE(mb_substitute_character, arginfo_mb_substitute_character)
  447. PHP_FE(mb_parse_str, arginfo_mb_parse_str)
  448. PHP_FE(mb_output_handler, arginfo_mb_output_handler)
  449. PHP_FE(mb_preferred_mime_name, arginfo_mb_preferred_mime_name)
  450. PHP_FE(mb_strlen, arginfo_mb_strlen)
  451. PHP_FE(mb_strpos, arginfo_mb_strpos)
  452. PHP_FE(mb_strrpos, arginfo_mb_strrpos)
  453. PHP_FE(mb_stripos, arginfo_mb_stripos)
  454. PHP_FE(mb_strripos, arginfo_mb_strripos)
  455. PHP_FE(mb_strstr, arginfo_mb_strstr)
  456. PHP_FE(mb_strrchr, arginfo_mb_strrchr)
  457. PHP_FE(mb_stristr, arginfo_mb_stristr)
  458. PHP_FE(mb_strrichr, arginfo_mb_strrichr)
  459. PHP_FE(mb_substr_count, arginfo_mb_substr_count)
  460. PHP_FE(mb_substr, arginfo_mb_substr)
  461. PHP_FE(mb_strcut, arginfo_mb_strcut)
  462. PHP_FE(mb_strwidth, arginfo_mb_strwidth)
  463. PHP_FE(mb_strimwidth, arginfo_mb_strimwidth)
  464. PHP_FE(mb_convert_encoding, arginfo_mb_convert_encoding)
  465. PHP_FE(mb_detect_encoding, arginfo_mb_detect_encoding)
  466. PHP_FE(mb_list_encodings, arginfo_mb_list_encodings)
  467. PHP_FE(mb_encoding_aliases, arginfo_mb_encoding_aliases)
  468. PHP_FE(mb_convert_kana, arginfo_mb_convert_kana)
  469. PHP_FE(mb_encode_mimeheader, arginfo_mb_encode_mimeheader)
  470. PHP_FE(mb_decode_mimeheader, arginfo_mb_decode_mimeheader)
  471. PHP_FE(mb_convert_variables, arginfo_mb_convert_variables)
  472. PHP_FE(mb_encode_numericentity, arginfo_mb_encode_numericentity)
  473. PHP_FE(mb_decode_numericentity, arginfo_mb_decode_numericentity)
  474. PHP_FE(mb_send_mail, arginfo_mb_send_mail)
  475. PHP_FE(mb_get_info, arginfo_mb_get_info)
  476. PHP_FE(mb_check_encoding, arginfo_mb_check_encoding)
  477. #if HAVE_MBREGEX
  478. PHP_MBREGEX_FUNCTION_ENTRIES
  479. #endif
  480. PHP_FE_END
  481. };
  482. /* }}} */
  483. /* {{{ zend_module_entry mbstring_module_entry */
  484. zend_module_entry mbstring_module_entry = {
  485. STANDARD_MODULE_HEADER,
  486. "mbstring",
  487. mbstring_functions,
  488. PHP_MINIT(mbstring),
  489. PHP_MSHUTDOWN(mbstring),
  490. PHP_RINIT(mbstring),
  491. PHP_RSHUTDOWN(mbstring),
  492. PHP_MINFO(mbstring),
  493. NO_VERSION_YET,
  494. PHP_MODULE_GLOBALS(mbstring),
  495. PHP_GINIT(mbstring),
  496. PHP_GSHUTDOWN(mbstring),
  497. NULL,
  498. STANDARD_MODULE_PROPERTIES_EX
  499. };
  500. /* }}} */
  501. /* {{{ static sapi_post_entry php_post_entries[] */
  502. static sapi_post_entry php_post_entries[] = {
  503. { DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler },
  504. { MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
  505. { NULL, 0, NULL, NULL }
  506. };
  507. /* }}} */
  508. #ifdef COMPILE_DL_MBSTRING
  509. ZEND_GET_MODULE(mbstring)
  510. #endif
  511. static char *get_internal_encoding(TSRMLS_D) {
  512. if (PG(internal_encoding) && PG(internal_encoding)[0]) {
  513. return PG(internal_encoding);
  514. } else if (SG(default_charset)) {
  515. return SG(default_charset);
  516. }
  517. return "";
  518. }
  519. static char *get_input_encoding(TSRMLS_D) {
  520. if (PG(input_encoding) && PG(input_encoding)[0]) {
  521. return PG(input_encoding);
  522. } else if (SG(default_charset)) {
  523. return SG(default_charset);
  524. }
  525. return "";
  526. }
  527. static char *get_output_encoding(TSRMLS_D) {
  528. if (PG(output_encoding) && PG(output_encoding)[0]) {
  529. return PG(output_encoding);
  530. } else if (SG(default_charset)) {
  531. return SG(default_charset);
  532. }
  533. return "";
  534. }
  535. /* {{{ allocators */
  536. static void *_php_mb_allocators_malloc(unsigned int sz)
  537. {
  538. return emalloc(sz);
  539. }
  540. static void *_php_mb_allocators_realloc(void *ptr, unsigned int sz)
  541. {
  542. return erealloc(ptr, sz);
  543. }
  544. static void *_php_mb_allocators_calloc(unsigned int nelems, unsigned int szelem)
  545. {
  546. return ecalloc(nelems, szelem);
  547. }
  548. static void _php_mb_allocators_free(void *ptr)
  549. {
  550. efree(ptr);
  551. }
  552. static void *_php_mb_allocators_pmalloc(unsigned int sz)
  553. {
  554. return pemalloc(sz, 1);
  555. }
  556. static void *_php_mb_allocators_prealloc(void *ptr, unsigned int sz)
  557. {
  558. return perealloc(ptr, sz, 1);
  559. }
  560. static void _php_mb_allocators_pfree(void *ptr)
  561. {
  562. pefree(ptr, 1);
  563. }
  564. static mbfl_allocators _php_mb_allocators = {
  565. _php_mb_allocators_malloc,
  566. _php_mb_allocators_realloc,
  567. _php_mb_allocators_calloc,
  568. _php_mb_allocators_free,
  569. _php_mb_allocators_pmalloc,
  570. _php_mb_allocators_prealloc,
  571. _php_mb_allocators_pfree
  572. };
  573. /* }}} */
  574. /* {{{ static sapi_post_entry mbstr_post_entries[] */
  575. static sapi_post_entry mbstr_post_entries[] = {
  576. { DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_mb_post_handler },
  577. { MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
  578. { NULL, 0, NULL, NULL }
  579. };
  580. /* }}} */
  581. /* {{{ static int php_mb_parse_encoding_list()
  582. * Return 0 if input contains any illegal encoding, otherwise 1.
  583. * Even if any illegal encoding is detected the result may contain a list
  584. * of parsed encodings.
  585. */
  586. static int
  587. php_mb_parse_encoding_list(const char *value, size_t value_length, const mbfl_encoding ***return_list, size_t *return_size, int persistent TSRMLS_DC)
  588. {
  589. int size, bauto, ret = SUCCESS;
  590. size_t n;
  591. char *p, *p1, *p2, *endp, *tmpstr;
  592. const mbfl_encoding **entry, **list;
  593. list = NULL;
  594. if (value == NULL || value_length <= 0) {
  595. if (return_list) {
  596. *return_list = NULL;
  597. }
  598. if (return_size) {
  599. *return_size = 0;
  600. }
  601. return FAILURE;
  602. } else {
  603. /* copy the value string for work */
  604. if (value[0]=='"' && value[value_length-1]=='"' && value_length>2) {
  605. tmpstr = (char *)estrndup(value+1, value_length-2);
  606. value_length -= 2;
  607. }
  608. else
  609. tmpstr = (char *)estrndup(value, value_length);
  610. if (tmpstr == NULL) {
  611. return FAILURE;
  612. }
  613. /* count the number of listed encoding names */
  614. endp = tmpstr + value_length;
  615. n = 1;
  616. p1 = tmpstr;
  617. while ((p2 = php_memnstr(p1, ",", 1, endp)) != NULL) {
  618. p1 = p2 + 1;
  619. n++;
  620. }
  621. size = n + MBSTRG(default_detect_order_list_size);
  622. /* make list */
  623. list = (const mbfl_encoding **)pecalloc(size, sizeof(mbfl_encoding*), persistent);
  624. if (list != NULL) {
  625. entry = list;
  626. n = 0;
  627. bauto = 0;
  628. p1 = tmpstr;
  629. do {
  630. p2 = p = php_memnstr(p1, ",", 1, endp);
  631. if (p == NULL) {
  632. p = endp;
  633. }
  634. *p = '\0';
  635. /* trim spaces */
  636. while (p1 < p && (*p1 == ' ' || *p1 == '\t')) {
  637. p1++;
  638. }
  639. p--;
  640. while (p > p1 && (*p == ' ' || *p == '\t')) {
  641. *p = '\0';
  642. p--;
  643. }
  644. /* convert to the encoding number and check encoding */
  645. if (strcasecmp(p1, "auto") == 0) {
  646. if (!bauto) {
  647. const enum mbfl_no_encoding *src = MBSTRG(default_detect_order_list);
  648. const size_t identify_list_size = MBSTRG(default_detect_order_list_size);
  649. size_t i;
  650. bauto = 1;
  651. for (i = 0; i < identify_list_size; i++) {
  652. *entry++ = mbfl_no2encoding(*src++);
  653. n++;
  654. }
  655. }
  656. } else {
  657. const mbfl_encoding *encoding = mbfl_name2encoding(p1);
  658. if (encoding) {
  659. *entry++ = encoding;
  660. n++;
  661. } else {
  662. ret = 0;
  663. }
  664. }
  665. p1 = p2 + 1;
  666. } while (n < size && p2 != NULL);
  667. if (n > 0) {
  668. if (return_list) {
  669. *return_list = list;
  670. } else {
  671. pefree(list, persistent);
  672. }
  673. } else {
  674. pefree(list, persistent);
  675. if (return_list) {
  676. *return_list = NULL;
  677. }
  678. ret = 0;
  679. }
  680. if (return_size) {
  681. *return_size = n;
  682. }
  683. } else {
  684. if (return_list) {
  685. *return_list = NULL;
  686. }
  687. if (return_size) {
  688. *return_size = 0;
  689. }
  690. ret = 0;
  691. }
  692. efree(tmpstr);
  693. }
  694. return ret;
  695. }
  696. /* }}} */
  697. /* {{{ static int php_mb_parse_encoding_array()
  698. * Return 0 if input contains any illegal encoding, otherwise 1.
  699. * Even if any illegal encoding is detected the result may contain a list
  700. * of parsed encodings.
  701. */
  702. static int
  703. php_mb_parse_encoding_array(zval *array, const mbfl_encoding ***return_list, size_t *return_size, int persistent TSRMLS_DC)
  704. {
  705. zval **hash_entry;
  706. HashTable *target_hash;
  707. int i, n, size, bauto, ret = SUCCESS;
  708. const mbfl_encoding **list, **entry;
  709. list = NULL;
  710. if (Z_TYPE_P(array) == IS_ARRAY) {
  711. target_hash = Z_ARRVAL_P(array);
  712. zend_hash_internal_pointer_reset(target_hash);
  713. i = zend_hash_num_elements(target_hash);
  714. size = i + MBSTRG(default_detect_order_list_size);
  715. list = (const mbfl_encoding **)pecalloc(size, sizeof(mbfl_encoding*), persistent);
  716. if (list != NULL) {
  717. entry = list;
  718. bauto = 0;
  719. n = 0;
  720. while (i > 0) {
  721. if (zend_hash_get_current_data(target_hash, (void **) &hash_entry) == FAILURE) {
  722. break;
  723. }
  724. convert_to_string_ex(hash_entry);
  725. if (strcasecmp(Z_STRVAL_PP(hash_entry), "auto") == 0) {
  726. if (!bauto) {
  727. const enum mbfl_no_encoding *src = MBSTRG(default_detect_order_list);
  728. const size_t identify_list_size = MBSTRG(default_detect_order_list_size);
  729. size_t j;
  730. bauto = 1;
  731. for (j = 0; j < identify_list_size; j++) {
  732. *entry++ = mbfl_no2encoding(*src++);
  733. n++;
  734. }
  735. }
  736. } else {
  737. const mbfl_encoding *encoding = mbfl_name2encoding(Z_STRVAL_PP(hash_entry));
  738. if (encoding) {
  739. *entry++ = encoding;
  740. n++;
  741. } else {
  742. ret = FAILURE;
  743. }
  744. }
  745. zend_hash_move_forward(target_hash);
  746. i--;
  747. }
  748. if (n > 0) {
  749. if (return_list) {
  750. *return_list = list;
  751. } else {
  752. pefree(list, persistent);
  753. }
  754. } else {
  755. pefree(list, persistent);
  756. if (return_list) {
  757. *return_list = NULL;
  758. }
  759. ret = FAILURE;
  760. }
  761. if (return_size) {
  762. *return_size = n;
  763. }
  764. } else {
  765. if (return_list) {
  766. *return_list = NULL;
  767. }
  768. if (return_size) {
  769. *return_size = 0;
  770. }
  771. ret = FAILURE;
  772. }
  773. }
  774. return ret;
  775. }
  776. /* }}} */
  777. /* {{{ zend_multibyte interface */
  778. static const zend_encoding* php_mb_zend_encoding_fetcher(const char *encoding_name TSRMLS_DC)
  779. {
  780. return (const zend_encoding*)mbfl_name2encoding(encoding_name);
  781. }
  782. static const char *php_mb_zend_encoding_name_getter(const zend_encoding *encoding)
  783. {
  784. return ((const mbfl_encoding *)encoding)->name;
  785. }
  786. static int php_mb_zend_encoding_lexer_compatibility_checker(const zend_encoding *_encoding)
  787. {
  788. const mbfl_encoding *encoding = (const mbfl_encoding*)_encoding;
  789. if (encoding->flag & MBFL_ENCTYPE_SBCS) {
  790. return 1;
  791. }
  792. if ((encoding->flag & (MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE)) == MBFL_ENCTYPE_MBCS) {
  793. return 1;
  794. }
  795. return 0;
  796. }
  797. static const zend_encoding *php_mb_zend_encoding_detector(const unsigned char *arg_string, size_t arg_length, const zend_encoding **list, size_t list_size TSRMLS_DC)
  798. {
  799. mbfl_string string;
  800. if (!list) {
  801. list = (const zend_encoding **)MBSTRG(current_detect_order_list);
  802. list_size = MBSTRG(current_detect_order_list_size);
  803. }
  804. mbfl_string_init(&string);
  805. string.no_language = MBSTRG(language);
  806. string.val = (unsigned char *)arg_string;
  807. string.len = arg_length;
  808. return (const zend_encoding *) mbfl_identify_encoding2(&string, (const mbfl_encoding **)list, list_size, 0);
  809. }
  810. static size_t php_mb_zend_encoding_converter(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length, const zend_encoding *encoding_to, const zend_encoding *encoding_from TSRMLS_DC)
  811. {
  812. mbfl_string string, result;
  813. mbfl_buffer_converter *convd;
  814. int status, loc;
  815. /* new encoding */
  816. /* initialize string */
  817. mbfl_string_init(&string);
  818. mbfl_string_init(&result);
  819. string.no_encoding = ((const mbfl_encoding*)encoding_from)->no_encoding;
  820. string.no_language = MBSTRG(language);
  821. string.val = (unsigned char*)from;
  822. string.len = from_length;
  823. /* initialize converter */
  824. convd = mbfl_buffer_converter_new2((const mbfl_encoding *)encoding_from, (const mbfl_encoding *)encoding_to, string.len);
  825. if (convd == NULL) {
  826. return -1;
  827. }
  828. mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
  829. mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
  830. /* do it */
  831. status = mbfl_buffer_converter_feed2(convd, &string, &loc);
  832. if (status) {
  833. mbfl_buffer_converter_delete(convd);
  834. return (size_t)-1;
  835. }
  836. mbfl_buffer_converter_flush(convd);
  837. if (!mbfl_buffer_converter_result(convd, &result)) {
  838. mbfl_buffer_converter_delete(convd);
  839. return (size_t)-1;
  840. }
  841. *to = result.val;
  842. *to_length = result.len;
  843. mbfl_buffer_converter_delete(convd);
  844. return loc;
  845. }
  846. static int php_mb_zend_encoding_list_parser(const char *encoding_list, size_t encoding_list_len, const zend_encoding ***return_list, size_t *return_size, int persistent TSRMLS_DC)
  847. {
  848. return php_mb_parse_encoding_list(encoding_list, encoding_list_len, (const mbfl_encoding ***)return_list, return_size, persistent TSRMLS_CC);
  849. }
  850. static const zend_encoding *php_mb_zend_internal_encoding_getter(TSRMLS_D)
  851. {
  852. return (const zend_encoding *)MBSTRG(internal_encoding);
  853. }
  854. static int php_mb_zend_internal_encoding_setter(const zend_encoding *encoding TSRMLS_DC)
  855. {
  856. MBSTRG(internal_encoding) = (const mbfl_encoding *)encoding;
  857. return SUCCESS;
  858. }
  859. static zend_multibyte_functions php_mb_zend_multibyte_functions = {
  860. "mbstring",
  861. php_mb_zend_encoding_fetcher,
  862. php_mb_zend_encoding_name_getter,
  863. php_mb_zend_encoding_lexer_compatibility_checker,
  864. php_mb_zend_encoding_detector,
  865. php_mb_zend_encoding_converter,
  866. php_mb_zend_encoding_list_parser,
  867. php_mb_zend_internal_encoding_getter,
  868. php_mb_zend_internal_encoding_setter
  869. };
  870. /* }}} */
  871. static void *_php_mb_compile_regex(const char *pattern TSRMLS_DC);
  872. static int _php_mb_match_regex(void *opaque, const char *str, size_t str_len);
  873. static void _php_mb_free_regex(void *opaque);
  874. #if HAVE_ONIG
  875. /* {{{ _php_mb_compile_regex */
  876. static void *_php_mb_compile_regex(const char *pattern TSRMLS_DC)
  877. {
  878. php_mb_regex_t *retval;
  879. OnigErrorInfo err_info;
  880. int err_code;
  881. if ((err_code = onig_new(&retval,
  882. (const OnigUChar *)pattern,
  883. (const OnigUChar *)pattern + strlen(pattern),
  884. ONIG_OPTION_IGNORECASE | ONIG_OPTION_DONT_CAPTURE_GROUP,
  885. ONIG_ENCODING_ASCII, &OnigSyntaxPerl, &err_info))) {
  886. OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
  887. onig_error_code_to_str(err_str, err_code, err_info);
  888. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: %s", pattern, err_str);
  889. retval = NULL;
  890. }
  891. return retval;
  892. }
  893. /* }}} */
  894. /* {{{ _php_mb_match_regex */
  895. static int _php_mb_match_regex(void *opaque, const char *str, size_t str_len)
  896. {
  897. return onig_search((php_mb_regex_t *)opaque, (const OnigUChar *)str,
  898. (const OnigUChar*)str + str_len, (const OnigUChar *)str,
  899. (const OnigUChar*)str + str_len, NULL, ONIG_OPTION_NONE) >= 0;
  900. }
  901. /* }}} */
  902. /* {{{ _php_mb_free_regex */
  903. static void _php_mb_free_regex(void *opaque)
  904. {
  905. onig_free((php_mb_regex_t *)opaque);
  906. }
  907. /* }}} */
  908. #elif HAVE_PCRE || HAVE_BUNDLED_PCRE
  909. /* {{{ _php_mb_compile_regex */
  910. static void *_php_mb_compile_regex(const char *pattern TSRMLS_DC)
  911. {
  912. pcre *retval;
  913. const char *err_str;
  914. int err_offset;
  915. if (!(retval = pcre_compile(pattern,
  916. PCRE_CASELESS, &err_str, &err_offset, NULL))) {
  917. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (offset=%d): %s", pattern, err_offset, err_str);
  918. }
  919. return retval;
  920. }
  921. /* }}} */
  922. /* {{{ _php_mb_match_regex */
  923. static int _php_mb_match_regex(void *opaque, const char *str, size_t str_len)
  924. {
  925. return pcre_exec((pcre *)opaque, NULL, str, (int)str_len, 0,
  926. 0, NULL, 0) >= 0;
  927. }
  928. /* }}} */
  929. /* {{{ _php_mb_free_regex */
  930. static void _php_mb_free_regex(void *opaque)
  931. {
  932. pcre_free(opaque);
  933. }
  934. /* }}} */
  935. #endif
  936. /* {{{ php_mb_nls_get_default_detect_order_list */
  937. static int php_mb_nls_get_default_detect_order_list(enum mbfl_no_language lang, enum mbfl_no_encoding **plist, size_t *plist_size)
  938. {
  939. size_t i;
  940. *plist = (enum mbfl_no_encoding *) php_mb_default_identify_list_neut;
  941. *plist_size = sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]);
  942. for (i = 0; i < sizeof(php_mb_default_identify_list) / sizeof(php_mb_default_identify_list[0]); i++) {
  943. if (php_mb_default_identify_list[i].lang == lang) {
  944. *plist = (enum mbfl_no_encoding *)php_mb_default_identify_list[i].list;
  945. *plist_size = php_mb_default_identify_list[i].list_size;
  946. return 1;
  947. }
  948. }
  949. return 0;
  950. }
  951. /* }}} */
  952. static char *php_mb_rfc1867_substring_conf(const zend_encoding *encoding, char *start, int len, char quote TSRMLS_DC)
  953. {
  954. char *result = emalloc(len + 2);
  955. char *resp = result;
  956. int i;
  957. for (i = 0; i < len && start[i] != quote; ++i) {
  958. if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) {
  959. *resp++ = start[++i];
  960. } else {
  961. size_t j = php_mb_mbchar_bytes_ex(start+i, (const mbfl_encoding *)encoding);
  962. while (j-- > 0 && i < len) {
  963. *resp++ = start[i++];
  964. }
  965. --i;
  966. }
  967. }
  968. *resp = '\0';
  969. return result;
  970. }
  971. static char *php_mb_rfc1867_getword(const zend_encoding *encoding, char **line, char stop TSRMLS_DC) /* {{{ */
  972. {
  973. char *pos = *line, quote;
  974. char *res;
  975. while (*pos && *pos != stop) {
  976. if ((quote = *pos) == '"' || quote == '\'') {
  977. ++pos;
  978. while (*pos && *pos != quote) {
  979. if (*pos == '\\' && pos[1] && pos[1] == quote) {
  980. pos += 2;
  981. } else {
  982. ++pos;
  983. }
  984. }
  985. if (*pos) {
  986. ++pos;
  987. }
  988. } else {
  989. pos += php_mb_mbchar_bytes_ex(pos, (const mbfl_encoding *)encoding);
  990. }
  991. }
  992. if (*pos == '\0') {
  993. res = estrdup(*line);
  994. *line += strlen(*line);
  995. return res;
  996. }
  997. res = estrndup(*line, pos - *line);
  998. while (*pos == stop) {
  999. pos += php_mb_mbchar_bytes_ex(pos, (const mbfl_encoding *)encoding);
  1000. }
  1001. *line = pos;
  1002. return res;
  1003. }
  1004. /* }}} */
  1005. static char *php_mb_rfc1867_getword_conf(const zend_encoding *encoding, char *str TSRMLS_DC) /* {{{ */
  1006. {
  1007. while (*str && isspace(*(unsigned char *)str)) {
  1008. ++str;
  1009. }
  1010. if (!*str) {
  1011. return estrdup("");
  1012. }
  1013. if (*str == '"' || *str == '\'') {
  1014. char quote = *str;
  1015. str++;
  1016. return php_mb_rfc1867_substring_conf(encoding, str, strlen(str), quote TSRMLS_CC);
  1017. } else {
  1018. char *strend = str;
  1019. while (*strend && !isspace(*(unsigned char *)strend)) {
  1020. ++strend;
  1021. }
  1022. return php_mb_rfc1867_substring_conf(encoding, str, strend - str, 0 TSRMLS_CC);
  1023. }
  1024. }
  1025. /* }}} */
  1026. static char *php_mb_rfc1867_basename(const zend_encoding *encoding, char *filename TSRMLS_DC) /* {{{ */
  1027. {
  1028. char *s, *s2;
  1029. const size_t filename_len = strlen(filename);
  1030. /* The \ check should technically be needed for win32 systems only where
  1031. * it is a valid path separator. However, IE in all it's wisdom always sends
  1032. * the full path of the file on the user's filesystem, which means that unless
  1033. * the user does basename() they get a bogus file name. Until IE's user base drops
  1034. * to nill or problem is fixed this code must remain enabled for all systems. */
  1035. s = php_mb_safe_strrchr_ex(filename, '\\', filename_len, (const mbfl_encoding *)encoding);
  1036. s2 = php_mb_safe_strrchr_ex(filename, '/', filename_len, (const mbfl_encoding *)encoding);
  1037. if (s && s2) {
  1038. if (s > s2) {
  1039. return ++s;
  1040. } else {
  1041. return ++s2;
  1042. }
  1043. } else if (s) {
  1044. return ++s;
  1045. } else if (s2) {
  1046. return ++s2;
  1047. } else {
  1048. return filename;
  1049. }
  1050. }
  1051. /* }}} */
  1052. /* {{{ php.ini directive handler */
  1053. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_language) */
  1054. static PHP_INI_MH(OnUpdate_mbstring_language)
  1055. {
  1056. enum mbfl_no_language no_language;
  1057. no_language = mbfl_name2no_language(new_value);
  1058. if (no_language == mbfl_no_language_invalid) {
  1059. MBSTRG(language) = mbfl_no_language_neutral;
  1060. return FAILURE;
  1061. }
  1062. MBSTRG(language) = no_language;
  1063. php_mb_nls_get_default_detect_order_list(no_language, &MBSTRG(default_detect_order_list), &MBSTRG(default_detect_order_list_size));
  1064. return SUCCESS;
  1065. }
  1066. /* }}} */
  1067. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_detect_order) */
  1068. static PHP_INI_MH(OnUpdate_mbstring_detect_order)
  1069. {
  1070. const mbfl_encoding **list;
  1071. size_t size;
  1072. if (!new_value) {
  1073. if (MBSTRG(detect_order_list)) {
  1074. pefree(MBSTRG(detect_order_list), 1);
  1075. }
  1076. MBSTRG(detect_order_list) = NULL;
  1077. MBSTRG(detect_order_list_size) = 0;
  1078. return SUCCESS;
  1079. }
  1080. if (FAILURE == php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
  1081. return FAILURE;
  1082. }
  1083. if (MBSTRG(detect_order_list)) {
  1084. pefree(MBSTRG(detect_order_list), 1);
  1085. }
  1086. MBSTRG(detect_order_list) = list;
  1087. MBSTRG(detect_order_list_size) = size;
  1088. return SUCCESS;
  1089. }
  1090. /* }}} */
  1091. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_input) */
  1092. static PHP_INI_MH(OnUpdate_mbstring_http_input)
  1093. {
  1094. const mbfl_encoding **list;
  1095. size_t size;
  1096. if (!new_value || !new_value_length) {
  1097. if (MBSTRG(http_input_list)) {
  1098. pefree(MBSTRG(http_input_list), 1);
  1099. }
  1100. if (SUCCESS == php_mb_parse_encoding_list(get_input_encoding(TSRMLS_C), strlen(get_input_encoding(TSRMLS_C))+1, &list, &size, 1 TSRMLS_CC)) {
  1101. MBSTRG(http_input_list) = list;
  1102. MBSTRG(http_input_list_size) = size;
  1103. return SUCCESS;
  1104. }
  1105. MBSTRG(http_input_list) = NULL;
  1106. MBSTRG(http_input_list_size) = 0;
  1107. return SUCCESS;
  1108. }
  1109. if (FAILURE == php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
  1110. return FAILURE;
  1111. }
  1112. if (MBSTRG(http_input_list)) {
  1113. pefree(MBSTRG(http_input_list), 1);
  1114. }
  1115. MBSTRG(http_input_list) = list;
  1116. MBSTRG(http_input_list_size) = size;
  1117. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  1118. php_error_docref("ref.mbstring" TSRMLS_CC, E_DEPRECATED, "Use of mbstring.http_input is deprecated");
  1119. }
  1120. return SUCCESS;
  1121. }
  1122. /* }}} */
  1123. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_output) */
  1124. static PHP_INI_MH(OnUpdate_mbstring_http_output)
  1125. {
  1126. const mbfl_encoding *encoding;
  1127. if (new_value == NULL || new_value_length == 0) {
  1128. encoding = mbfl_name2encoding(get_output_encoding(TSRMLS_C));
  1129. if (!encoding) {
  1130. MBSTRG(http_output_encoding) = &mbfl_encoding_pass;
  1131. MBSTRG(current_http_output_encoding) = &mbfl_encoding_pass;
  1132. return SUCCESS;
  1133. }
  1134. } else {
  1135. encoding = mbfl_name2encoding(new_value);
  1136. if (!encoding) {
  1137. MBSTRG(http_output_encoding) = &mbfl_encoding_pass;
  1138. MBSTRG(current_http_output_encoding) = &mbfl_encoding_pass;
  1139. return FAILURE;
  1140. }
  1141. }
  1142. MBSTRG(http_output_encoding) = encoding;
  1143. MBSTRG(current_http_output_encoding) = encoding;
  1144. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  1145. php_error_docref("ref.mbstring" TSRMLS_CC, E_DEPRECATED, "Use of mbstring.http_output is deprecated");
  1146. }
  1147. return SUCCESS;
  1148. }
  1149. /* }}} */
  1150. /* {{{ static _php_mb_ini_mbstring_internal_encoding_set */
  1151. int _php_mb_ini_mbstring_internal_encoding_set(const char *new_value, uint new_value_length TSRMLS_DC)
  1152. {
  1153. const mbfl_encoding *encoding;
  1154. if (!new_value || !new_value_length || !(encoding = mbfl_name2encoding(new_value))) {
  1155. /* falls back to UTF-8 if an unknown encoding name is given */
  1156. encoding = mbfl_no2encoding(mbfl_no_encoding_utf8);
  1157. }
  1158. MBSTRG(internal_encoding) = encoding;
  1159. MBSTRG(current_internal_encoding) = encoding;
  1160. #if HAVE_MBREGEX
  1161. {
  1162. const char *enc_name = new_value;
  1163. if (FAILURE == php_mb_regex_set_default_mbctype(enc_name TSRMLS_CC)) {
  1164. /* falls back to UTF-8 if an unknown encoding name is given */
  1165. enc_name = "UTF-8";
  1166. php_mb_regex_set_default_mbctype(enc_name TSRMLS_CC);
  1167. }
  1168. php_mb_regex_set_mbctype(new_value TSRMLS_CC);
  1169. }
  1170. #endif
  1171. return SUCCESS;
  1172. }
  1173. /* }}} */
  1174. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_internal_encoding) */
  1175. static PHP_INI_MH(OnUpdate_mbstring_internal_encoding)
  1176. {
  1177. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  1178. php_error_docref("ref.mbstring" TSRMLS_CC, E_DEPRECATED, "Use of mbstring.internal_encoding is deprecated");
  1179. }
  1180. if (OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC) == FAILURE) {
  1181. return FAILURE;
  1182. }
  1183. if (stage & (PHP_INI_STAGE_STARTUP | PHP_INI_STAGE_SHUTDOWN | PHP_INI_STAGE_RUNTIME)) {
  1184. if (new_value && new_value_length) {
  1185. return _php_mb_ini_mbstring_internal_encoding_set(new_value, new_value_length TSRMLS_CC);
  1186. } else {
  1187. return _php_mb_ini_mbstring_internal_encoding_set(get_internal_encoding(TSRMLS_C), strlen(get_internal_encoding(TSRMLS_C))+1 TSRMLS_CC);
  1188. }
  1189. } else {
  1190. /* the corresponding mbstring globals needs to be set according to the
  1191. * ini value in the later stage because it never falls back to the
  1192. * default value if 1. no value for mbstring.internal_encoding is given,
  1193. * 2. mbstring.language directive is processed in per-dir or runtime
  1194. * context and 3. call to the handler for mbstring.language is done
  1195. * after mbstring.internal_encoding is handled. */
  1196. return SUCCESS;
  1197. }
  1198. }
  1199. /* }}} */
  1200. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_substitute_character) */
  1201. static PHP_INI_MH(OnUpdate_mbstring_substitute_character)
  1202. {
  1203. int c;
  1204. char *endptr = NULL;
  1205. if (new_value != NULL) {
  1206. if (strcasecmp("none", new_value) == 0) {
  1207. MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
  1208. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
  1209. } else if (strcasecmp("long", new_value) == 0) {
  1210. MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
  1211. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
  1212. } else if (strcasecmp("entity", new_value) == 0) {
  1213. MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY;
  1214. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY;
  1215. } else {
  1216. MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1217. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1218. if (new_value_length >0) {
  1219. c = strtol(new_value, &endptr, 0);
  1220. if (*endptr == '\0') {
  1221. MBSTRG(filter_illegal_substchar) = c;
  1222. MBSTRG(current_filter_illegal_substchar) = c;
  1223. }
  1224. }
  1225. }
  1226. } else {
  1227. MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1228. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1229. MBSTRG(filter_illegal_substchar) = 0x3f; /* '?' */
  1230. MBSTRG(current_filter_illegal_substchar) = 0x3f; /* '?' */
  1231. }
  1232. return SUCCESS;
  1233. }
  1234. /* }}} */
  1235. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_encoding_translation) */
  1236. static PHP_INI_MH(OnUpdate_mbstring_encoding_translation)
  1237. {
  1238. if (new_value == NULL) {
  1239. return FAILURE;
  1240. }
  1241. OnUpdateBool(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  1242. if (MBSTRG(encoding_translation)) {
  1243. sapi_unregister_post_entry(php_post_entries TSRMLS_CC);
  1244. sapi_register_post_entries(mbstr_post_entries TSRMLS_CC);
  1245. } else {
  1246. sapi_unregister_post_entry(mbstr_post_entries TSRMLS_CC);
  1247. sapi_register_post_entries(php_post_entries TSRMLS_CC);
  1248. }
  1249. return SUCCESS;
  1250. }
  1251. /* }}} */
  1252. /* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_output_conv_mimetypes */
  1253. static PHP_INI_MH(OnUpdate_mbstring_http_output_conv_mimetypes)
  1254. {
  1255. zval tmp;
  1256. void *re = NULL;
  1257. if (!new_value) {
  1258. new_value = entry->orig_value;
  1259. new_value_length = entry->orig_value_length;
  1260. }
  1261. php_trim(new_value, new_value_length, NULL, 0, &tmp, 3 TSRMLS_CC);
  1262. if (Z_STRLEN(tmp) > 0) {
  1263. if (!(re = _php_mb_compile_regex(Z_STRVAL(tmp) TSRMLS_CC))) {
  1264. zval_dtor(&tmp);
  1265. return FAILURE;
  1266. }
  1267. }
  1268. if (MBSTRG(http_output_conv_mimetypes)) {
  1269. _php_mb_free_regex(MBSTRG(http_output_conv_mimetypes));
  1270. }
  1271. MBSTRG(http_output_conv_mimetypes) = re;
  1272. zval_dtor(&tmp);
  1273. return SUCCESS;
  1274. }
  1275. /* }}} */
  1276. /* }}} */
  1277. /* {{{ php.ini directive registration */
  1278. PHP_INI_BEGIN()
  1279. PHP_INI_ENTRY("mbstring.language", "neutral", PHP_INI_ALL, OnUpdate_mbstring_language)
  1280. PHP_INI_ENTRY("mbstring.detect_order", NULL, PHP_INI_ALL, OnUpdate_mbstring_detect_order)
  1281. PHP_INI_ENTRY("mbstring.http_input", NULL, PHP_INI_ALL, OnUpdate_mbstring_http_input)
  1282. PHP_INI_ENTRY("mbstring.http_output", NULL, PHP_INI_ALL, OnUpdate_mbstring_http_output)
  1283. STD_PHP_INI_ENTRY("mbstring.internal_encoding", NULL, PHP_INI_ALL, OnUpdate_mbstring_internal_encoding, internal_encoding_name, zend_mbstring_globals, mbstring_globals)
  1284. PHP_INI_ENTRY("mbstring.substitute_character", NULL, PHP_INI_ALL, OnUpdate_mbstring_substitute_character)
  1285. STD_PHP_INI_ENTRY("mbstring.func_overload", "0",
  1286. PHP_INI_SYSTEM, OnUpdateLong, func_overload, zend_mbstring_globals, mbstring_globals)
  1287. STD_PHP_INI_BOOLEAN("mbstring.encoding_translation", "0",
  1288. PHP_INI_SYSTEM | PHP_INI_PERDIR,
  1289. OnUpdate_mbstring_encoding_translation,
  1290. encoding_translation, zend_mbstring_globals, mbstring_globals)
  1291. PHP_INI_ENTRY("mbstring.http_output_conv_mimetypes",
  1292. "^(text/|application/xhtml\\+xml)",
  1293. PHP_INI_ALL,
  1294. OnUpdate_mbstring_http_output_conv_mimetypes)
  1295. STD_PHP_INI_BOOLEAN("mbstring.strict_detection", "0",
  1296. PHP_INI_ALL,
  1297. OnUpdateLong,
  1298. strict_detection, zend_mbstring_globals, mbstring_globals)
  1299. PHP_INI_END()
  1300. /* }}} */
  1301. /* {{{ module global initialize handler */
  1302. static PHP_GINIT_FUNCTION(mbstring)
  1303. {
  1304. mbstring_globals->language = mbfl_no_language_uni;
  1305. mbstring_globals->internal_encoding = NULL;
  1306. mbstring_globals->current_internal_encoding = mbstring_globals->internal_encoding;
  1307. mbstring_globals->http_output_encoding = &mbfl_encoding_pass;
  1308. mbstring_globals->current_http_output_encoding = &mbfl_encoding_pass;
  1309. mbstring_globals->http_input_identify = NULL;
  1310. mbstring_globals->http_input_identify_get = NULL;
  1311. mbstring_globals->http_input_identify_post = NULL;
  1312. mbstring_globals->http_input_identify_cookie = NULL;
  1313. mbstring_globals->http_input_identify_string = NULL;
  1314. mbstring_globals->http_input_list = NULL;
  1315. mbstring_globals->http_input_list_size = 0;
  1316. mbstring_globals->detect_order_list = NULL;
  1317. mbstring_globals->detect_order_list_size = 0;
  1318. mbstring_globals->current_detect_order_list = NULL;
  1319. mbstring_globals->current_detect_order_list_size = 0;
  1320. mbstring_globals->default_detect_order_list = (enum mbfl_no_encoding *) php_mb_default_identify_list_neut;
  1321. mbstring_globals->default_detect_order_list_size = sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]);
  1322. mbstring_globals->filter_illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1323. mbstring_globals->filter_illegal_substchar = 0x3f; /* '?' */
  1324. mbstring_globals->current_filter_illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1325. mbstring_globals->current_filter_illegal_substchar = 0x3f; /* '?' */
  1326. mbstring_globals->illegalchars = 0;
  1327. mbstring_globals->func_overload = 0;
  1328. mbstring_globals->encoding_translation = 0;
  1329. mbstring_globals->strict_detection = 0;
  1330. mbstring_globals->outconv = NULL;
  1331. mbstring_globals->http_output_conv_mimetypes = NULL;
  1332. #if HAVE_MBREGEX
  1333. mbstring_globals->mb_regex_globals = php_mb_regex_globals_alloc(TSRMLS_C);
  1334. #endif
  1335. }
  1336. /* }}} */
  1337. /* {{{ PHP_GSHUTDOWN_FUNCTION */
  1338. static PHP_GSHUTDOWN_FUNCTION(mbstring)
  1339. {
  1340. if (mbstring_globals->http_input_list) {
  1341. free(mbstring_globals->http_input_list);
  1342. }
  1343. if (mbstring_globals->detect_order_list) {
  1344. free(mbstring_globals->detect_order_list);
  1345. }
  1346. if (mbstring_globals->http_output_conv_mimetypes) {
  1347. _php_mb_free_regex(mbstring_globals->http_output_conv_mimetypes);
  1348. }
  1349. #if HAVE_MBREGEX
  1350. php_mb_regex_globals_free(mbstring_globals->mb_regex_globals TSRMLS_CC);
  1351. #endif
  1352. }
  1353. /* }}} */
  1354. /* {{{ PHP_MINIT_FUNCTION(mbstring) */
  1355. PHP_MINIT_FUNCTION(mbstring)
  1356. {
  1357. __mbfl_allocators = &_php_mb_allocators;
  1358. REGISTER_INI_ENTRIES();
  1359. /* This is a global handler. Should not be set in a per-request handler. */
  1360. sapi_register_treat_data(mbstr_treat_data TSRMLS_CC);
  1361. /* Post handlers are stored in the thread-local context. */
  1362. if (MBSTRG(encoding_translation)) {
  1363. sapi_register_post_entries(mbstr_post_entries TSRMLS_CC);
  1364. }
  1365. REGISTER_LONG_CONSTANT("MB_OVERLOAD_MAIL", MB_OVERLOAD_MAIL, CONST_CS | CONST_PERSISTENT);
  1366. REGISTER_LONG_CONSTANT("MB_OVERLOAD_STRING", MB_OVERLOAD_STRING, CONST_CS | CONST_PERSISTENT);
  1367. REGISTER_LONG_CONSTANT("MB_OVERLOAD_REGEX", MB_OVERLOAD_REGEX, CONST_CS | CONST_PERSISTENT);
  1368. REGISTER_LONG_CONSTANT("MB_CASE_UPPER", PHP_UNICODE_CASE_UPPER, CONST_CS | CONST_PERSISTENT);
  1369. REGISTER_LONG_CONSTANT("MB_CASE_LOWER", PHP_UNICODE_CASE_LOWER, CONST_CS | CONST_PERSISTENT);
  1370. REGISTER_LONG_CONSTANT("MB_CASE_TITLE", PHP_UNICODE_CASE_TITLE, CONST_CS | CONST_PERSISTENT);
  1371. #if HAVE_MBREGEX
  1372. PHP_MINIT(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
  1373. #endif
  1374. if (FAILURE == zend_multibyte_set_functions(&php_mb_zend_multibyte_functions TSRMLS_CC)) {
  1375. return FAILURE;
  1376. }
  1377. php_rfc1867_set_multibyte_callbacks(
  1378. php_mb_encoding_translation,
  1379. php_mb_gpc_get_detect_order,
  1380. php_mb_gpc_set_input_encoding,
  1381. php_mb_rfc1867_getword,
  1382. php_mb_rfc1867_getword_conf,
  1383. php_mb_rfc1867_basename);
  1384. return SUCCESS;
  1385. }
  1386. /* }}} */
  1387. /* {{{ PHP_MSHUTDOWN_FUNCTION(mbstring) */
  1388. PHP_MSHUTDOWN_FUNCTION(mbstring)
  1389. {
  1390. UNREGISTER_INI_ENTRIES();
  1391. #if HAVE_MBREGEX
  1392. PHP_MSHUTDOWN(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
  1393. #endif
  1394. return SUCCESS;
  1395. }
  1396. /* }}} */
  1397. /* {{{ PHP_RINIT_FUNCTION(mbstring) */
  1398. PHP_RINIT_FUNCTION(mbstring)
  1399. {
  1400. zend_function *func, *orig;
  1401. const struct mb_overload_def *p;
  1402. MBSTRG(current_internal_encoding) = MBSTRG(internal_encoding);
  1403. MBSTRG(current_http_output_encoding) = MBSTRG(http_output_encoding);
  1404. MBSTRG(current_filter_illegal_mode) = MBSTRG(filter_illegal_mode);
  1405. MBSTRG(current_filter_illegal_substchar) = MBSTRG(filter_illegal_substchar);
  1406. MBSTRG(illegalchars) = 0;
  1407. php_mb_populate_current_detect_order_list(TSRMLS_C);
  1408. /* override original function. */
  1409. if (MBSTRG(func_overload)){
  1410. p = &(mb_ovld[0]);
  1411. while (p->type > 0) {
  1412. if ((MBSTRG(func_overload) & p->type) == p->type &&
  1413. zend_hash_find(EG(function_table), p->save_func,
  1414. strlen(p->save_func)+1, (void **)&orig) != SUCCESS) {
  1415. zend_hash_find(EG(function_table), p->ovld_func, strlen(p->ovld_func)+1 , (void **)&func);
  1416. if (zend_hash_find(EG(function_table), p->orig_func, strlen(p->orig_func)+1, (void **)&orig) != SUCCESS) {
  1417. php_error_docref("ref.mbstring" TSRMLS_CC, E_WARNING, "mbstring couldn't find function %s.", p->orig_func);
  1418. return FAILURE;
  1419. } else {
  1420. zend_hash_add(EG(function_table), p->save_func, strlen(p->save_func)+1, orig, sizeof(zend_function), NULL);
  1421. if (zend_hash_update(EG(function_table), p->orig_func, strlen(p->orig_func)+1, func, sizeof(zend_function),
  1422. NULL) == FAILURE) {
  1423. php_error_docref("ref.mbstring" TSRMLS_CC, E_WARNING, "mbstring couldn't replace function %s.", p->orig_func);
  1424. return FAILURE;
  1425. }
  1426. }
  1427. }
  1428. p++;
  1429. }
  1430. }
  1431. #if HAVE_MBREGEX
  1432. PHP_RINIT(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
  1433. #endif
  1434. zend_multibyte_set_internal_encoding((const zend_encoding *)MBSTRG(internal_encoding) TSRMLS_CC);
  1435. return SUCCESS;
  1436. }
  1437. /* }}} */
  1438. /* {{{ PHP_RSHUTDOWN_FUNCTION(mbstring) */
  1439. PHP_RSHUTDOWN_FUNCTION(mbstring)
  1440. {
  1441. const struct mb_overload_def *p;
  1442. zend_function *orig;
  1443. if (MBSTRG(current_detect_order_list) != NULL) {
  1444. efree(MBSTRG(current_detect_order_list));
  1445. MBSTRG(current_detect_order_list) = NULL;
  1446. MBSTRG(current_detect_order_list_size) = 0;
  1447. }
  1448. if (MBSTRG(outconv) != NULL) {
  1449. MBSTRG(illegalchars) += mbfl_buffer_illegalchars(MBSTRG(outconv));
  1450. mbfl_buffer_converter_delete(MBSTRG(outconv));
  1451. MBSTRG(outconv) = NULL;
  1452. }
  1453. /* clear http input identification. */
  1454. MBSTRG(http_input_identify) = NULL;
  1455. MBSTRG(http_input_identify_post) = NULL;
  1456. MBSTRG(http_input_identify_get) = NULL;
  1457. MBSTRG(http_input_identify_cookie) = NULL;
  1458. MBSTRG(http_input_identify_string) = NULL;
  1459. /* clear overloaded function. */
  1460. if (MBSTRG(func_overload)){
  1461. p = &(mb_ovld[0]);
  1462. while (p->type > 0) {
  1463. if ((MBSTRG(func_overload) & p->type) == p->type &&
  1464. zend_hash_find(EG(function_table), p->save_func,
  1465. strlen(p->save_func)+1, (void **)&orig) == SUCCESS) {
  1466. zend_hash_update(EG(function_table), p->orig_func, strlen(p->orig_func)+1, orig, sizeof(zend_function), NULL);
  1467. zend_hash_del(EG(function_table), p->save_func, strlen(p->save_func)+1);
  1468. }
  1469. p++;
  1470. }
  1471. }
  1472. #if HAVE_MBREGEX
  1473. PHP_RSHUTDOWN(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
  1474. #endif
  1475. return SUCCESS;
  1476. }
  1477. /* }}} */
  1478. /* {{{ PHP_MINFO_FUNCTION(mbstring) */
  1479. PHP_MINFO_FUNCTION(mbstring)
  1480. {
  1481. php_info_print_table_start();
  1482. php_info_print_table_row(2, "Multibyte Support", "enabled");
  1483. php_info_print_table_row(2, "Multibyte string engine", "libmbfl");
  1484. php_info_print_table_row(2, "HTTP input encoding translation", MBSTRG(encoding_translation) ? "enabled": "disabled");
  1485. {
  1486. char tmp[256];
  1487. snprintf(tmp, sizeof(tmp), "%d.%d.%d", MBFL_VERSION_MAJOR, MBFL_VERSION_MINOR, MBFL_VERSION_TEENY);
  1488. php_info_print_table_row(2, "libmbfl version", tmp);
  1489. }
  1490. php_info_print_table_end();
  1491. php_info_print_table_start();
  1492. php_info_print_table_header(1, "mbstring extension makes use of \"streamable kanji code filter and converter\", which is distributed under the GNU Lesser General Public License version 2.1.");
  1493. php_info_print_table_end();
  1494. #if HAVE_MBREGEX
  1495. PHP_MINFO(mb_regex)(ZEND_MODULE_INFO_FUNC_ARGS_PASSTHRU);
  1496. #endif
  1497. DISPLAY_INI_ENTRIES();
  1498. }
  1499. /* }}} */
  1500. /* {{{ proto string mb_language([string language])
  1501. Sets the current language or Returns the current language as a string */
  1502. PHP_FUNCTION(mb_language)
  1503. {
  1504. char *name = NULL;
  1505. int name_len = 0;
  1506. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
  1507. return;
  1508. }
  1509. if (name == NULL) {
  1510. RETVAL_STRING((char *)mbfl_no_language2name(MBSTRG(language)), 1);
  1511. } else {
  1512. if (FAILURE == zend_alter_ini_entry(
  1513. "mbstring.language", sizeof("mbstring.language"),
  1514. name, name_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME)) {
  1515. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown language \"%s\"", name);
  1516. RETVAL_FALSE;
  1517. } else {
  1518. RETVAL_TRUE;
  1519. }
  1520. }
  1521. }
  1522. /* }}} */
  1523. /* {{{ proto string mb_internal_encoding([string encoding])
  1524. Sets the current internal encoding or Returns the current internal encoding as a string */
  1525. PHP_FUNCTION(mb_internal_encoding)
  1526. {
  1527. const char *name = NULL;
  1528. int name_len;
  1529. const mbfl_encoding *encoding;
  1530. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
  1531. RETURN_FALSE;
  1532. }
  1533. if (name == NULL) {
  1534. name = MBSTRG(current_internal_encoding) ? MBSTRG(current_internal_encoding)->name: NULL;
  1535. if (name != NULL) {
  1536. RETURN_STRING(name, 1);
  1537. } else {
  1538. RETURN_FALSE;
  1539. }
  1540. } else {
  1541. encoding = mbfl_name2encoding(name);
  1542. if (!encoding) {
  1543. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", name);
  1544. RETURN_FALSE;
  1545. } else {
  1546. MBSTRG(current_internal_encoding) = encoding;
  1547. RETURN_TRUE;
  1548. }
  1549. }
  1550. }
  1551. /* }}} */
  1552. /* {{{ proto mixed mb_http_input([string type])
  1553. Returns the input encoding */
  1554. PHP_FUNCTION(mb_http_input)
  1555. {
  1556. char *typ = NULL;
  1557. int typ_len;
  1558. int retname;
  1559. char *list, *temp;
  1560. const mbfl_encoding *result = NULL;
  1561. retname = 1;
  1562. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &typ, &typ_len) == FAILURE) {
  1563. RETURN_FALSE;
  1564. }
  1565. if (typ == NULL) {
  1566. result = MBSTRG(http_input_identify);
  1567. } else {
  1568. switch (*typ) {
  1569. case 'G':
  1570. case 'g':
  1571. result = MBSTRG(http_input_identify_get);
  1572. break;
  1573. case 'P':
  1574. case 'p':
  1575. result = MBSTRG(http_input_identify_post);
  1576. break;
  1577. case 'C':
  1578. case 'c':
  1579. result = MBSTRG(http_input_identify_cookie);
  1580. break;
  1581. case 'S':
  1582. case 's':
  1583. result = MBSTRG(http_input_identify_string);
  1584. break;
  1585. case 'I':
  1586. case 'i':
  1587. {
  1588. const mbfl_encoding **entry = MBSTRG(http_input_list);
  1589. const size_t n = MBSTRG(http_input_list_size);
  1590. size_t i;
  1591. array_init(return_value);
  1592. for (i = 0; i < n; i++) {
  1593. add_next_index_string(return_value, (*entry)->name, 1);
  1594. entry++;
  1595. }
  1596. retname = 0;
  1597. }
  1598. break;
  1599. case 'L':
  1600. case 'l':
  1601. {
  1602. const mbfl_encoding **entry = MBSTRG(http_input_list);
  1603. const size_t n = MBSTRG(http_input_list_size);
  1604. size_t i;
  1605. list = NULL;
  1606. for (i = 0; i < n; i++) {
  1607. if (list) {
  1608. temp = list;
  1609. spprintf(&list, 0, "%s,%s", temp, (*entry)->name);
  1610. efree(temp);
  1611. if (!list) {
  1612. break;
  1613. }
  1614. } else {
  1615. list = estrdup((*entry)->name);
  1616. }
  1617. entry++;
  1618. }
  1619. }
  1620. if (!list) {
  1621. RETURN_FALSE;
  1622. }
  1623. RETVAL_STRING(list, 0);
  1624. retname = 0;
  1625. break;
  1626. default:
  1627. result = MBSTRG(http_input_identify);
  1628. break;
  1629. }
  1630. }
  1631. if (retname) {
  1632. if (result) {
  1633. RETVAL_STRING(result->name, 1);
  1634. } else {
  1635. RETVAL_FALSE;
  1636. }
  1637. }
  1638. }
  1639. /* }}} */
  1640. /* {{{ proto string mb_http_output([string encoding])
  1641. Sets the current output_encoding or returns the current output_encoding as a string */
  1642. PHP_FUNCTION(mb_http_output)
  1643. {
  1644. const char *name = NULL;
  1645. int name_len;
  1646. const mbfl_encoding *encoding;
  1647. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", (char **)&name, &name_len) == FAILURE) {
  1648. RETURN_FALSE;
  1649. }
  1650. if (name == NULL) {
  1651. name = MBSTRG(current_http_output_encoding) ? MBSTRG(current_http_output_encoding)->name: NULL;
  1652. if (name != NULL) {
  1653. RETURN_STRING(name, 1);
  1654. } else {
  1655. RETURN_FALSE;
  1656. }
  1657. } else {
  1658. encoding = mbfl_name2encoding(name);
  1659. if (!encoding) {
  1660. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", name);
  1661. RETURN_FALSE;
  1662. } else {
  1663. MBSTRG(current_http_output_encoding) = encoding;
  1664. RETURN_TRUE;
  1665. }
  1666. }
  1667. }
  1668. /* }}} */
  1669. /* {{{ proto bool|array mb_detect_order([mixed encoding-list])
  1670. Sets the current detect_order or Return the current detect_order as a array */
  1671. PHP_FUNCTION(mb_detect_order)
  1672. {
  1673. zval **arg1 = NULL;
  1674. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|Z", &arg1) == FAILURE) {
  1675. return;
  1676. }
  1677. if (!arg1) {
  1678. size_t i;
  1679. size_t n = MBSTRG(current_detect_order_list_size);
  1680. const mbfl_encoding **entry = MBSTRG(current_detect_order_list);
  1681. array_init(return_value);
  1682. for (i = 0; i < n; i++) {
  1683. add_next_index_string(return_value, (*entry)->name, 1);
  1684. entry++;
  1685. }
  1686. } else {
  1687. const mbfl_encoding **list = NULL;
  1688. size_t size = 0;
  1689. switch (Z_TYPE_PP(arg1)) {
  1690. case IS_ARRAY:
  1691. if (FAILURE == php_mb_parse_encoding_array(*arg1, &list, &size, 0 TSRMLS_CC)) {
  1692. if (list) {
  1693. efree(list);
  1694. }
  1695. RETURN_FALSE;
  1696. }
  1697. break;
  1698. default:
  1699. convert_to_string_ex(arg1);
  1700. if (FAILURE == php_mb_parse_encoding_list(Z_STRVAL_PP(arg1), Z_STRLEN_PP(arg1), &list, &size, 0 TSRMLS_CC)) {
  1701. if (list) {
  1702. efree(list);
  1703. }
  1704. RETURN_FALSE;
  1705. }
  1706. break;
  1707. }
  1708. if (list == NULL) {
  1709. RETURN_FALSE;
  1710. }
  1711. if (MBSTRG(current_detect_order_list)) {
  1712. efree(MBSTRG(current_detect_order_list));
  1713. }
  1714. MBSTRG(current_detect_order_list) = list;
  1715. MBSTRG(current_detect_order_list_size) = size;
  1716. RETURN_TRUE;
  1717. }
  1718. }
  1719. /* }}} */
  1720. /* {{{ proto mixed mb_substitute_character([mixed substchar])
  1721. Sets the current substitute_character or returns the current substitute_character */
  1722. PHP_FUNCTION(mb_substitute_character)
  1723. {
  1724. zval **arg1 = NULL;
  1725. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|Z", &arg1) == FAILURE) {
  1726. return;
  1727. }
  1728. if (!arg1) {
  1729. if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
  1730. RETURN_STRING("none", 1);
  1731. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG) {
  1732. RETURN_STRING("long", 1);
  1733. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY) {
  1734. RETURN_STRING("entity", 1);
  1735. } else {
  1736. RETURN_LONG(MBSTRG(current_filter_illegal_substchar));
  1737. }
  1738. } else {
  1739. RETVAL_TRUE;
  1740. switch (Z_TYPE_PP(arg1)) {
  1741. case IS_STRING:
  1742. if (strncasecmp("none", Z_STRVAL_PP(arg1), Z_STRLEN_PP(arg1)) == 0) {
  1743. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
  1744. } else if (strncasecmp("long", Z_STRVAL_PP(arg1), Z_STRLEN_PP(arg1)) == 0) {
  1745. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
  1746. } else if (strncasecmp("entity", Z_STRVAL_PP(arg1), Z_STRLEN_PP(arg1)) == 0) {
  1747. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY;
  1748. } else {
  1749. convert_to_long_ex(arg1);
  1750. if (Z_LVAL_PP(arg1) < 0xffff && Z_LVAL_PP(arg1) > 0x0) {
  1751. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1752. MBSTRG(current_filter_illegal_substchar) = Z_LVAL_PP(arg1);
  1753. } else {
  1754. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown character.");
  1755. RETURN_FALSE;
  1756. }
  1757. }
  1758. break;
  1759. default:
  1760. convert_to_long_ex(arg1);
  1761. if (Z_LVAL_PP(arg1) < 0xffff && Z_LVAL_PP(arg1) > 0x0) {
  1762. MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
  1763. MBSTRG(current_filter_illegal_substchar) = Z_LVAL_PP(arg1);
  1764. } else {
  1765. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown character.");
  1766. RETURN_FALSE;
  1767. }
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. /* }}} */
  1773. /* {{{ proto string mb_preferred_mime_name(string encoding)
  1774. Return the preferred MIME name (charset) as a string */
  1775. PHP_FUNCTION(mb_preferred_mime_name)
  1776. {
  1777. enum mbfl_no_encoding no_encoding;
  1778. char *name = NULL;
  1779. int name_len;
  1780. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
  1781. return;
  1782. } else {
  1783. no_encoding = mbfl_name2no_encoding(name);
  1784. if (no_encoding == mbfl_no_encoding_invalid) {
  1785. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", name);
  1786. RETVAL_FALSE;
  1787. } else {
  1788. const char *preferred_name = mbfl_no2preferred_mime_name(no_encoding);
  1789. if (preferred_name == NULL || *preferred_name == '\0') {
  1790. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No MIME preferred name corresponding to \"%s\"", name);
  1791. RETVAL_FALSE;
  1792. } else {
  1793. RETVAL_STRING((char *)preferred_name, 1);
  1794. }
  1795. }
  1796. }
  1797. }
  1798. /* }}} */
  1799. #define IS_SJIS1(c) ((((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xf5)) ? 1 : 0)
  1800. #define IS_SJIS2(c) ((((c)>=0x40 && (c)<=0x7e) || ((c)>=0x80 && (c)<=0xfc)) ? 1 : 0)
  1801. /* {{{ proto bool mb_parse_str(string encoded_string [, array result])
  1802. Parses GET/POST/COOKIE data and sets global variables */
  1803. PHP_FUNCTION(mb_parse_str)
  1804. {
  1805. zval *track_vars_array = NULL;
  1806. char *encstr = NULL;
  1807. int encstr_len;
  1808. php_mb_encoding_handler_info_t info;
  1809. const mbfl_encoding *detected;
  1810. track_vars_array = NULL;
  1811. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &encstr, &encstr_len, &track_vars_array) == FAILURE) {
  1812. return;
  1813. }
  1814. if (track_vars_array != NULL) {
  1815. /* Clear out the array */
  1816. zval_dtor(track_vars_array);
  1817. array_init(track_vars_array);
  1818. }
  1819. encstr = estrndup(encstr, encstr_len);
  1820. info.data_type = PARSE_STRING;
  1821. info.separator = PG(arg_separator).input;
  1822. info.report_errors = 1;
  1823. info.to_encoding = MBSTRG(current_internal_encoding);
  1824. info.to_language = MBSTRG(language);
  1825. info.from_encodings = MBSTRG(http_input_list);
  1826. info.num_from_encodings = MBSTRG(http_input_list_size);
  1827. info.from_language = MBSTRG(language);
  1828. if (track_vars_array != NULL) {
  1829. detected = _php_mb_encoding_handler_ex(&info, track_vars_array, encstr TSRMLS_CC);
  1830. } else {
  1831. zval tmp;
  1832. if (!EG(active_symbol_table)) {
  1833. zend_rebuild_symbol_table(TSRMLS_C);
  1834. }
  1835. Z_ARRVAL(tmp) = EG(active_symbol_table);
  1836. detected = _php_mb_encoding_handler_ex(&info, &tmp, encstr TSRMLS_CC);
  1837. }
  1838. MBSTRG(http_input_identify) = detected;
  1839. RETVAL_BOOL(detected);
  1840. if (encstr != NULL) efree(encstr);
  1841. }
  1842. /* }}} */
  1843. /* {{{ proto string mb_output_handler(string contents, int status)
  1844. Returns string in output buffer converted to the http_output encoding */
  1845. PHP_FUNCTION(mb_output_handler)
  1846. {
  1847. char *arg_string;
  1848. int arg_string_len;
  1849. long arg_status;
  1850. mbfl_string string, result;
  1851. const char *charset;
  1852. char *p;
  1853. const mbfl_encoding *encoding;
  1854. int last_feed, len;
  1855. unsigned char send_text_mimetype = 0;
  1856. char *s, *mimetype = NULL;
  1857. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &arg_string, &arg_string_len, &arg_status) == FAILURE) {
  1858. return;
  1859. }
  1860. encoding = MBSTRG(current_http_output_encoding);
  1861. /* start phase only */
  1862. if ((arg_status & PHP_OUTPUT_HANDLER_START) != 0) {
  1863. /* delete the converter just in case. */
  1864. if (MBSTRG(outconv)) {
  1865. MBSTRG(illegalchars) += mbfl_buffer_illegalchars(MBSTRG(outconv));
  1866. mbfl_buffer_converter_delete(MBSTRG(outconv));
  1867. MBSTRG(outconv) = NULL;
  1868. }
  1869. if (encoding == &mbfl_encoding_pass) {
  1870. RETURN_STRINGL(arg_string, arg_string_len, 1);
  1871. }
  1872. /* analyze mime type */
  1873. if (SG(sapi_headers).mimetype &&
  1874. _php_mb_match_regex(
  1875. MBSTRG(http_output_conv_mimetypes),
  1876. SG(sapi_headers).mimetype,
  1877. strlen(SG(sapi_headers).mimetype))) {
  1878. if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){
  1879. mimetype = estrdup(SG(sapi_headers).mimetype);
  1880. } else {
  1881. mimetype = estrndup(SG(sapi_headers).mimetype,s-SG(sapi_headers).mimetype);
  1882. }
  1883. send_text_mimetype = 1;
  1884. } else if (SG(sapi_headers).send_default_content_type) {
  1885. mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
  1886. }
  1887. /* if content-type is not yet set, set it and activate the converter */
  1888. if (SG(sapi_headers).send_default_content_type || send_text_mimetype) {
  1889. charset = encoding->mime_name;
  1890. if (charset) {
  1891. len = spprintf( &p, 0, "Content-Type: %s; charset=%s", mimetype, charset );
  1892. if (sapi_add_header(p, len, 0) != FAILURE) {
  1893. SG(sapi_headers).send_default_content_type = 0;
  1894. }
  1895. }
  1896. /* activate the converter */
  1897. MBSTRG(outconv) = mbfl_buffer_converter_new2(MBSTRG(current_internal_encoding), encoding, 0);
  1898. if (send_text_mimetype){
  1899. efree(mimetype);
  1900. }
  1901. }
  1902. }
  1903. /* just return if the converter is not activated. */
  1904. if (MBSTRG(outconv) == NULL) {
  1905. RETURN_STRINGL(arg_string, arg_string_len, 1);
  1906. }
  1907. /* flag */
  1908. last_feed = ((arg_status & PHP_OUTPUT_HANDLER_END) != 0);
  1909. /* mode */
  1910. mbfl_buffer_converter_illegal_mode(MBSTRG(outconv), MBSTRG(current_filter_illegal_mode));
  1911. mbfl_buffer_converter_illegal_substchar(MBSTRG(outconv), MBSTRG(current_filter_illegal_substchar));
  1912. /* feed the string */
  1913. mbfl_string_init(&string);
  1914. /* these are not needed. convd has encoding info.
  1915. string.no_language = MBSTRG(language);
  1916. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  1917. */
  1918. string.val = (unsigned char *)arg_string;
  1919. string.len = arg_string_len;
  1920. mbfl_buffer_converter_feed(MBSTRG(outconv), &string);
  1921. if (last_feed) {
  1922. mbfl_buffer_converter_flush(MBSTRG(outconv));
  1923. }
  1924. /* get the converter output, and return it */
  1925. mbfl_buffer_converter_result(MBSTRG(outconv), &result);
  1926. RETVAL_STRINGL((char *)result.val, result.len, 0); /* the string is already strdup()'ed */
  1927. /* delete the converter if it is the last feed. */
  1928. if (last_feed) {
  1929. MBSTRG(illegalchars) += mbfl_buffer_illegalchars(MBSTRG(outconv));
  1930. mbfl_buffer_converter_delete(MBSTRG(outconv));
  1931. MBSTRG(outconv) = NULL;
  1932. }
  1933. }
  1934. /* }}} */
  1935. /* {{{ proto int mb_strlen(string str [, string encoding])
  1936. Get character numbers of a string */
  1937. PHP_FUNCTION(mb_strlen)
  1938. {
  1939. int n;
  1940. mbfl_string string;
  1941. char *enc_name = NULL;
  1942. int enc_name_len;
  1943. mbfl_string_init(&string);
  1944. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", (char **)&string.val, &string.len, &enc_name, &enc_name_len) == FAILURE) {
  1945. RETURN_FALSE;
  1946. }
  1947. string.no_language = MBSTRG(language);
  1948. if (enc_name == NULL) {
  1949. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  1950. } else {
  1951. string.no_encoding = mbfl_name2no_encoding(enc_name);
  1952. if (string.no_encoding == mbfl_no_encoding_invalid) {
  1953. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  1954. RETURN_FALSE;
  1955. }
  1956. }
  1957. n = mbfl_strlen(&string);
  1958. if (n >= 0) {
  1959. RETVAL_LONG(n);
  1960. } else {
  1961. RETVAL_FALSE;
  1962. }
  1963. }
  1964. /* }}} */
  1965. /* {{{ proto int mb_strpos(string haystack, string needle [, int offset [, string encoding]])
  1966. Find position of first occurrence of a string within another */
  1967. PHP_FUNCTION(mb_strpos)
  1968. {
  1969. int n, reverse = 0;
  1970. long offset;
  1971. mbfl_string haystack, needle;
  1972. char *enc_name = NULL;
  1973. int enc_name_len;
  1974. mbfl_string_init(&haystack);
  1975. mbfl_string_init(&needle);
  1976. haystack.no_language = MBSTRG(language);
  1977. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  1978. needle.no_language = MBSTRG(language);
  1979. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  1980. offset = 0;
  1981. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ls", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &offset, &enc_name, &enc_name_len) == FAILURE) {
  1982. RETURN_FALSE;
  1983. }
  1984. if (enc_name != NULL) {
  1985. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name);
  1986. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  1987. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  1988. RETURN_FALSE;
  1989. }
  1990. }
  1991. if (offset < 0 || offset > mbfl_strlen(&haystack)) {
  1992. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string");
  1993. RETURN_FALSE;
  1994. }
  1995. if (needle.len == 0) {
  1996. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
  1997. RETURN_FALSE;
  1998. }
  1999. n = mbfl_strpos(&haystack, &needle, offset, reverse);
  2000. if (n >= 0) {
  2001. RETVAL_LONG(n);
  2002. } else {
  2003. switch (-n) {
  2004. case 1:
  2005. break;
  2006. case 2:
  2007. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Needle has not positive length");
  2008. break;
  2009. case 4:
  2010. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding or conversion error");
  2011. break;
  2012. case 8:
  2013. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Argument is empty");
  2014. break;
  2015. default:
  2016. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error in mb_strpos");
  2017. break;
  2018. }
  2019. RETVAL_FALSE;
  2020. }
  2021. }
  2022. /* }}} */
  2023. /* {{{ proto int mb_strrpos(string haystack, string needle [, int offset [, string encoding]])
  2024. Find position of last occurrence of a string within another */
  2025. PHP_FUNCTION(mb_strrpos)
  2026. {
  2027. int n;
  2028. mbfl_string haystack, needle;
  2029. char *enc_name = NULL;
  2030. int enc_name_len;
  2031. zval **zoffset = NULL;
  2032. long offset = 0, str_flg;
  2033. char *enc_name2 = NULL;
  2034. int enc_name_len2;
  2035. mbfl_string_init(&haystack);
  2036. mbfl_string_init(&needle);
  2037. haystack.no_language = MBSTRG(language);
  2038. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2039. needle.no_language = MBSTRG(language);
  2040. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2041. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|Zs", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &zoffset, &enc_name, &enc_name_len) == FAILURE) {
  2042. RETURN_FALSE;
  2043. }
  2044. if (zoffset) {
  2045. if (Z_TYPE_PP(zoffset) == IS_STRING) {
  2046. enc_name2 = Z_STRVAL_PP(zoffset);
  2047. enc_name_len2 = Z_STRLEN_PP(zoffset);
  2048. str_flg = 1;
  2049. if (enc_name2 != NULL) {
  2050. switch (*enc_name2) {
  2051. case '0':
  2052. case '1':
  2053. case '2':
  2054. case '3':
  2055. case '4':
  2056. case '5':
  2057. case '6':
  2058. case '7':
  2059. case '8':
  2060. case '9':
  2061. case ' ':
  2062. case '-':
  2063. case '.':
  2064. break;
  2065. default :
  2066. str_flg = 0;
  2067. break;
  2068. }
  2069. }
  2070. if (str_flg) {
  2071. convert_to_long_ex(zoffset);
  2072. offset = Z_LVAL_PP(zoffset);
  2073. } else {
  2074. enc_name = enc_name2;
  2075. enc_name_len = enc_name_len2;
  2076. }
  2077. } else {
  2078. convert_to_long_ex(zoffset);
  2079. offset = Z_LVAL_PP(zoffset);
  2080. }
  2081. }
  2082. if (enc_name != NULL) {
  2083. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name);
  2084. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2085. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  2086. RETURN_FALSE;
  2087. }
  2088. }
  2089. if (haystack.len <= 0) {
  2090. RETURN_FALSE;
  2091. }
  2092. if (needle.len <= 0) {
  2093. RETURN_FALSE;
  2094. }
  2095. {
  2096. int haystack_char_len = mbfl_strlen(&haystack);
  2097. if ((offset > 0 && offset > haystack_char_len) ||
  2098. (offset < 0 && -offset > haystack_char_len)) {
  2099. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  2100. RETURN_FALSE;
  2101. }
  2102. }
  2103. n = mbfl_strpos(&haystack, &needle, offset, 1);
  2104. if (n >= 0) {
  2105. RETVAL_LONG(n);
  2106. } else {
  2107. RETVAL_FALSE;
  2108. }
  2109. }
  2110. /* }}} */
  2111. /* {{{ proto int mb_stripos(string haystack, string needle [, int offset [, string encoding]])
  2112. Finds position of first occurrence of a string within another, case insensitive */
  2113. PHP_FUNCTION(mb_stripos)
  2114. {
  2115. int n;
  2116. long offset;
  2117. mbfl_string haystack, needle;
  2118. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2119. int from_encoding_len;
  2120. n = -1;
  2121. offset = 0;
  2122. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ls", (char **)&haystack.val, (int *)&haystack.len, (char **)&needle.val, (int *)&needle.len, &offset, &from_encoding, &from_encoding_len) == FAILURE) {
  2123. RETURN_FALSE;
  2124. }
  2125. if (needle.len == 0) {
  2126. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
  2127. RETURN_FALSE;
  2128. }
  2129. n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, offset, from_encoding TSRMLS_CC);
  2130. if (n >= 0) {
  2131. RETVAL_LONG(n);
  2132. } else {
  2133. RETVAL_FALSE;
  2134. }
  2135. }
  2136. /* }}} */
  2137. /* {{{ proto int mb_strripos(string haystack, string needle [, int offset [, string encoding]])
  2138. Finds position of last occurrence of a string within another, case insensitive */
  2139. PHP_FUNCTION(mb_strripos)
  2140. {
  2141. int n;
  2142. long offset;
  2143. mbfl_string haystack, needle;
  2144. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2145. int from_encoding_len;
  2146. n = -1;
  2147. offset = 0;
  2148. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ls", (char **)&haystack.val, (int *)&haystack.len, (char **)&needle.val, (int *)&needle.len, &offset, &from_encoding, &from_encoding_len) == FAILURE) {
  2149. RETURN_FALSE;
  2150. }
  2151. n = php_mb_stripos(1, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, offset, from_encoding TSRMLS_CC);
  2152. if (n >= 0) {
  2153. RETVAL_LONG(n);
  2154. } else {
  2155. RETVAL_FALSE;
  2156. }
  2157. }
  2158. /* }}} */
  2159. /* {{{ proto string mb_strstr(string haystack, string needle[, bool part[, string encoding]])
  2160. Finds first occurrence of a string within another */
  2161. PHP_FUNCTION(mb_strstr)
  2162. {
  2163. int n, len, mblen;
  2164. mbfl_string haystack, needle, result, *ret = NULL;
  2165. char *enc_name = NULL;
  2166. int enc_name_len;
  2167. zend_bool part = 0;
  2168. mbfl_string_init(&haystack);
  2169. mbfl_string_init(&needle);
  2170. haystack.no_language = MBSTRG(language);
  2171. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2172. needle.no_language = MBSTRG(language);
  2173. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2174. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bs", (char **)&haystack.val, (int *)&haystack.len, (char **)&needle.val, (int *)&needle.len, &part, &enc_name, &enc_name_len) == FAILURE) {
  2175. RETURN_FALSE;
  2176. }
  2177. if (enc_name != NULL) {
  2178. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name);
  2179. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2180. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  2181. RETURN_FALSE;
  2182. }
  2183. }
  2184. if (needle.len <= 0) {
  2185. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
  2186. RETURN_FALSE;
  2187. }
  2188. n = mbfl_strpos(&haystack, &needle, 0, 0);
  2189. if (n >= 0) {
  2190. mblen = mbfl_strlen(&haystack);
  2191. if (part) {
  2192. ret = mbfl_substr(&haystack, &result, 0, n);
  2193. if (ret != NULL) {
  2194. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2195. } else {
  2196. RETVAL_FALSE;
  2197. }
  2198. } else {
  2199. len = (mblen - n);
  2200. ret = mbfl_substr(&haystack, &result, n, len);
  2201. if (ret != NULL) {
  2202. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2203. } else {
  2204. RETVAL_FALSE;
  2205. }
  2206. }
  2207. } else {
  2208. RETVAL_FALSE;
  2209. }
  2210. }
  2211. /* }}} */
  2212. /* {{{ proto string mb_strrchr(string haystack, string needle[, bool part[, string encoding]])
  2213. Finds the last occurrence of a character in a string within another */
  2214. PHP_FUNCTION(mb_strrchr)
  2215. {
  2216. int n, len, mblen;
  2217. mbfl_string haystack, needle, result, *ret = NULL;
  2218. char *enc_name = NULL;
  2219. int enc_name_len;
  2220. zend_bool part = 0;
  2221. mbfl_string_init(&haystack);
  2222. mbfl_string_init(&needle);
  2223. haystack.no_language = MBSTRG(language);
  2224. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2225. needle.no_language = MBSTRG(language);
  2226. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2227. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bs", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &part, &enc_name, &enc_name_len) == FAILURE) {
  2228. RETURN_FALSE;
  2229. }
  2230. if (enc_name != NULL) {
  2231. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name);
  2232. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2233. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  2234. RETURN_FALSE;
  2235. }
  2236. }
  2237. if (haystack.len <= 0) {
  2238. RETURN_FALSE;
  2239. }
  2240. if (needle.len <= 0) {
  2241. RETURN_FALSE;
  2242. }
  2243. n = mbfl_strpos(&haystack, &needle, 0, 1);
  2244. if (n >= 0) {
  2245. mblen = mbfl_strlen(&haystack);
  2246. if (part) {
  2247. ret = mbfl_substr(&haystack, &result, 0, n);
  2248. if (ret != NULL) {
  2249. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2250. } else {
  2251. RETVAL_FALSE;
  2252. }
  2253. } else {
  2254. len = (mblen - n);
  2255. ret = mbfl_substr(&haystack, &result, n, len);
  2256. if (ret != NULL) {
  2257. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2258. } else {
  2259. RETVAL_FALSE;
  2260. }
  2261. }
  2262. } else {
  2263. RETVAL_FALSE;
  2264. }
  2265. }
  2266. /* }}} */
  2267. /* {{{ proto string mb_stristr(string haystack, string needle[, bool part[, string encoding]])
  2268. Finds first occurrence of a string within another, case insensitive */
  2269. PHP_FUNCTION(mb_stristr)
  2270. {
  2271. zend_bool part = 0;
  2272. unsigned int from_encoding_len, len, mblen;
  2273. int n;
  2274. mbfl_string haystack, needle, result, *ret = NULL;
  2275. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2276. mbfl_string_init(&haystack);
  2277. mbfl_string_init(&needle);
  2278. haystack.no_language = MBSTRG(language);
  2279. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2280. needle.no_language = MBSTRG(language);
  2281. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2282. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bs", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &part, &from_encoding, &from_encoding_len) == FAILURE) {
  2283. RETURN_FALSE;
  2284. }
  2285. if (!needle.len) {
  2286. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
  2287. RETURN_FALSE;
  2288. }
  2289. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(from_encoding);
  2290. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2291. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", from_encoding);
  2292. RETURN_FALSE;
  2293. }
  2294. n = php_mb_stripos(0, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, 0, from_encoding TSRMLS_CC);
  2295. if (n <0) {
  2296. RETURN_FALSE;
  2297. }
  2298. mblen = mbfl_strlen(&haystack);
  2299. if (part) {
  2300. ret = mbfl_substr(&haystack, &result, 0, n);
  2301. if (ret != NULL) {
  2302. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2303. } else {
  2304. RETVAL_FALSE;
  2305. }
  2306. } else {
  2307. len = (mblen - n);
  2308. ret = mbfl_substr(&haystack, &result, n, len);
  2309. if (ret != NULL) {
  2310. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2311. } else {
  2312. RETVAL_FALSE;
  2313. }
  2314. }
  2315. }
  2316. /* }}} */
  2317. /* {{{ proto string mb_strrichr(string haystack, string needle[, bool part[, string encoding]])
  2318. Finds the last occurrence of a character in a string within another, case insensitive */
  2319. PHP_FUNCTION(mb_strrichr)
  2320. {
  2321. zend_bool part = 0;
  2322. int n, from_encoding_len, len, mblen;
  2323. mbfl_string haystack, needle, result, *ret = NULL;
  2324. const char *from_encoding = MBSTRG(current_internal_encoding)->name;
  2325. mbfl_string_init(&haystack);
  2326. mbfl_string_init(&needle);
  2327. haystack.no_language = MBSTRG(language);
  2328. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2329. needle.no_language = MBSTRG(language);
  2330. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2331. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bs", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &part, &from_encoding, &from_encoding_len) == FAILURE) {
  2332. RETURN_FALSE;
  2333. }
  2334. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(from_encoding);
  2335. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2336. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", from_encoding);
  2337. RETURN_FALSE;
  2338. }
  2339. n = php_mb_stripos(1, (char *)haystack.val, haystack.len, (char *)needle.val, needle.len, 0, from_encoding TSRMLS_CC);
  2340. if (n <0) {
  2341. RETURN_FALSE;
  2342. }
  2343. mblen = mbfl_strlen(&haystack);
  2344. if (part) {
  2345. ret = mbfl_substr(&haystack, &result, 0, n);
  2346. if (ret != NULL) {
  2347. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2348. } else {
  2349. RETVAL_FALSE;
  2350. }
  2351. } else {
  2352. len = (mblen - n);
  2353. ret = mbfl_substr(&haystack, &result, n, len);
  2354. if (ret != NULL) {
  2355. RETVAL_STRINGL((char *)ret->val, ret->len, 0);
  2356. } else {
  2357. RETVAL_FALSE;
  2358. }
  2359. }
  2360. }
  2361. /* }}} */
  2362. /* {{{ proto int mb_substr_count(string haystack, string needle [, string encoding])
  2363. Count the number of substring occurrences */
  2364. PHP_FUNCTION(mb_substr_count)
  2365. {
  2366. int n;
  2367. mbfl_string haystack, needle;
  2368. char *enc_name = NULL;
  2369. int enc_name_len;
  2370. mbfl_string_init(&haystack);
  2371. mbfl_string_init(&needle);
  2372. haystack.no_language = MBSTRG(language);
  2373. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2374. needle.no_language = MBSTRG(language);
  2375. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2376. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &enc_name, &enc_name_len) == FAILURE) {
  2377. return;
  2378. }
  2379. if (enc_name != NULL) {
  2380. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name);
  2381. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  2382. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  2383. RETURN_FALSE;
  2384. }
  2385. }
  2386. if (needle.len <= 0) {
  2387. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty substring");
  2388. RETURN_FALSE;
  2389. }
  2390. n = mbfl_substr_count(&haystack, &needle);
  2391. if (n >= 0) {
  2392. RETVAL_LONG(n);
  2393. } else {
  2394. RETVAL_FALSE;
  2395. }
  2396. }
  2397. /* }}} */
  2398. /* {{{ proto string mb_substr(string str, int start [, int length [, string encoding]])
  2399. Returns part of a string */
  2400. PHP_FUNCTION(mb_substr)
  2401. {
  2402. size_t argc = ZEND_NUM_ARGS();
  2403. char *str, *encoding;
  2404. long from, len;
  2405. int mblen, str_len, encoding_len;
  2406. zval **z_len = NULL;
  2407. mbfl_string string, result, *ret;
  2408. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|Zs", &str, &str_len, &from, &z_len, &encoding, &encoding_len) == FAILURE) {
  2409. return;
  2410. }
  2411. mbfl_string_init(&string);
  2412. string.no_language = MBSTRG(language);
  2413. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2414. if (argc == 4) {
  2415. string.no_encoding = mbfl_name2no_encoding(encoding);
  2416. if (string.no_encoding == mbfl_no_encoding_invalid) {
  2417. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", encoding);
  2418. RETURN_FALSE;
  2419. }
  2420. }
  2421. string.val = (unsigned char *)str;
  2422. string.len = str_len;
  2423. if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
  2424. len = str_len;
  2425. } else {
  2426. convert_to_long_ex(z_len);
  2427. len = Z_LVAL_PP(z_len);
  2428. }
  2429. /* measures length */
  2430. mblen = 0;
  2431. if (from < 0 || len < 0) {
  2432. mblen = mbfl_strlen(&string);
  2433. }
  2434. /* if "from" position is negative, count start position from the end
  2435. * of the string
  2436. */
  2437. if (from < 0) {
  2438. from = mblen + from;
  2439. if (from < 0) {
  2440. from = 0;
  2441. }
  2442. }
  2443. /* if "length" position is negative, set it to the length
  2444. * needed to stop that many chars from the end of the string
  2445. */
  2446. if (len < 0) {
  2447. len = (mblen - from) + len;
  2448. if (len < 0) {
  2449. len = 0;
  2450. }
  2451. }
  2452. if (((MBSTRG(func_overload) & MB_OVERLOAD_STRING) == MB_OVERLOAD_STRING)
  2453. && (from >= mbfl_strlen(&string))) {
  2454. RETURN_FALSE;
  2455. }
  2456. if (from > INT_MAX) {
  2457. from = INT_MAX;
  2458. }
  2459. if (len > INT_MAX) {
  2460. len = INT_MAX;
  2461. }
  2462. ret = mbfl_substr(&string, &result, from, len);
  2463. if (NULL == ret) {
  2464. RETURN_FALSE;
  2465. }
  2466. RETURN_STRINGL((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  2467. }
  2468. /* }}} */
  2469. /* {{{ proto string mb_strcut(string str, int start [, int length [, string encoding]])
  2470. Returns part of a string */
  2471. PHP_FUNCTION(mb_strcut)
  2472. {
  2473. size_t argc = ZEND_NUM_ARGS();
  2474. char *encoding;
  2475. long from, len;
  2476. int encoding_len;
  2477. zval **z_len = NULL;
  2478. mbfl_string string, result, *ret;
  2479. mbfl_string_init(&string);
  2480. string.no_language = MBSTRG(language);
  2481. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2482. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|Zs", (char **)&string.val, (int **)&string.len, &from, &z_len, &encoding, &encoding_len) == FAILURE) {
  2483. return;
  2484. }
  2485. if (argc == 4) {
  2486. string.no_encoding = mbfl_name2no_encoding(encoding);
  2487. if (string.no_encoding == mbfl_no_encoding_invalid) {
  2488. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", encoding);
  2489. RETURN_FALSE;
  2490. }
  2491. }
  2492. if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
  2493. len = string.len;
  2494. } else {
  2495. convert_to_long_ex(z_len);
  2496. len = Z_LVAL_PP(z_len);
  2497. }
  2498. /* if "from" position is negative, count start position from the end
  2499. * of the string
  2500. */
  2501. if (from < 0) {
  2502. from = string.len + from;
  2503. if (from < 0) {
  2504. from = 0;
  2505. }
  2506. }
  2507. /* if "length" position is negative, set it to the length
  2508. * needed to stop that many chars from the end of the string
  2509. */
  2510. if (len < 0) {
  2511. len = (string.len - from) + len;
  2512. if (len < 0) {
  2513. len = 0;
  2514. }
  2515. }
  2516. if ((unsigned int)from > string.len) {
  2517. RETURN_FALSE;
  2518. }
  2519. ret = mbfl_strcut(&string, &result, from, len);
  2520. if (ret == NULL) {
  2521. RETURN_FALSE;
  2522. }
  2523. RETURN_STRINGL((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  2524. }
  2525. /* }}} */
  2526. /* {{{ proto int mb_strwidth(string str [, string encoding])
  2527. Gets terminal width of a string */
  2528. PHP_FUNCTION(mb_strwidth)
  2529. {
  2530. int n;
  2531. mbfl_string string;
  2532. char *enc_name = NULL;
  2533. int enc_name_len;
  2534. mbfl_string_init(&string);
  2535. string.no_language = MBSTRG(language);
  2536. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2537. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", (char **)&string.val, &string.len, &enc_name, &enc_name_len) == FAILURE) {
  2538. return;
  2539. }
  2540. if (enc_name != NULL) {
  2541. string.no_encoding = mbfl_name2no_encoding(enc_name);
  2542. if (string.no_encoding == mbfl_no_encoding_invalid) {
  2543. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name);
  2544. RETURN_FALSE;
  2545. }
  2546. }
  2547. n = mbfl_strwidth(&string);
  2548. if (n >= 0) {
  2549. RETVAL_LONG(n);
  2550. } else {
  2551. RETVAL_FALSE;
  2552. }
  2553. }
  2554. /* }}} */
  2555. /* {{{ proto string mb_strimwidth(string str, int start, int width [, string trimmarker [, string encoding]])
  2556. Trim the string in terminal width */
  2557. PHP_FUNCTION(mb_strimwidth)
  2558. {
  2559. char *str, *trimmarker, *encoding;
  2560. long from, width;
  2561. int str_len, trimmarker_len, encoding_len;
  2562. mbfl_string string, result, marker, *ret;
  2563. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|ss", &str, &str_len, &from, &width, &trimmarker, &trimmarker_len, &encoding, &encoding_len) == FAILURE) {
  2564. return;
  2565. }
  2566. mbfl_string_init(&string);
  2567. mbfl_string_init(&marker);
  2568. string.no_language = MBSTRG(language);
  2569. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2570. marker.no_language = MBSTRG(language);
  2571. marker.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2572. marker.val = NULL;
  2573. marker.len = 0;
  2574. if (ZEND_NUM_ARGS() == 5) {
  2575. string.no_encoding = marker.no_encoding = mbfl_name2no_encoding(encoding);
  2576. if (string.no_encoding == mbfl_no_encoding_invalid) {
  2577. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", encoding);
  2578. RETURN_FALSE;
  2579. }
  2580. }
  2581. string.val = (unsigned char *)str;
  2582. string.len = str_len;
  2583. if (from < 0 || from > str_len) {
  2584. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Start position is out of range");
  2585. RETURN_FALSE;
  2586. }
  2587. if (width < 0) {
  2588. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Width is negative value");
  2589. RETURN_FALSE;
  2590. }
  2591. if (ZEND_NUM_ARGS() >= 4) {
  2592. marker.val = (unsigned char *)trimmarker;
  2593. marker.len = trimmarker_len;
  2594. }
  2595. ret = mbfl_strimwidth(&string, &marker, &result, from, width);
  2596. if (ret == NULL) {
  2597. RETURN_FALSE;
  2598. }
  2599. RETVAL_STRINGL((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  2600. }
  2601. /* }}} */
  2602. /* {{{ MBSTRING_API char *php_mb_convert_encoding() */
  2603. MBSTRING_API char * php_mb_convert_encoding(const char *input, size_t length, const char *_to_encoding, const char *_from_encodings, size_t *output_len TSRMLS_DC)
  2604. {
  2605. mbfl_string string, result, *ret;
  2606. const mbfl_encoding *from_encoding, *to_encoding;
  2607. mbfl_buffer_converter *convd;
  2608. size_t size;
  2609. const mbfl_encoding **list;
  2610. char *output=NULL;
  2611. if (output_len) {
  2612. *output_len = 0;
  2613. }
  2614. if (!input) {
  2615. return NULL;
  2616. }
  2617. /* new encoding */
  2618. if (_to_encoding && strlen(_to_encoding)) {
  2619. to_encoding = mbfl_name2encoding(_to_encoding);
  2620. if (!to_encoding) {
  2621. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", _to_encoding);
  2622. return NULL;
  2623. }
  2624. } else {
  2625. to_encoding = MBSTRG(current_internal_encoding);
  2626. }
  2627. /* initialize string */
  2628. mbfl_string_init(&string);
  2629. mbfl_string_init(&result);
  2630. from_encoding = MBSTRG(current_internal_encoding);
  2631. string.no_encoding = from_encoding->no_encoding;
  2632. string.no_language = MBSTRG(language);
  2633. string.val = (unsigned char *)input;
  2634. string.len = length;
  2635. /* pre-conversion encoding */
  2636. if (_from_encodings) {
  2637. list = NULL;
  2638. size = 0;
  2639. php_mb_parse_encoding_list(_from_encodings, strlen(_from_encodings), &list, &size, 0 TSRMLS_CC);
  2640. if (size == 1) {
  2641. from_encoding = *list;
  2642. string.no_encoding = from_encoding->no_encoding;
  2643. } else if (size > 1) {
  2644. /* auto detect */
  2645. from_encoding = mbfl_identify_encoding2(&string, list, size, MBSTRG(strict_detection));
  2646. if (from_encoding) {
  2647. string.no_encoding = from_encoding->no_encoding;
  2648. } else {
  2649. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to detect character encoding");
  2650. from_encoding = &mbfl_encoding_pass;
  2651. to_encoding = from_encoding;
  2652. string.no_encoding = from_encoding->no_encoding;
  2653. }
  2654. } else {
  2655. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal character encoding specified");
  2656. }
  2657. if (list != NULL) {
  2658. efree((void *)list);
  2659. }
  2660. }
  2661. /* initialize converter */
  2662. convd = mbfl_buffer_converter_new2(from_encoding, to_encoding, string.len);
  2663. if (convd == NULL) {
  2664. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create character encoding converter");
  2665. return NULL;
  2666. }
  2667. mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
  2668. mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
  2669. /* do it */
  2670. ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
  2671. if (ret) {
  2672. if (output_len) {
  2673. *output_len = ret->len;
  2674. }
  2675. output = (char *)ret->val;
  2676. }
  2677. MBSTRG(illegalchars) += mbfl_buffer_illegalchars(convd);
  2678. mbfl_buffer_converter_delete(convd);
  2679. return output;
  2680. }
  2681. /* }}} */
  2682. /* {{{ proto string mb_convert_encoding(string str, string to-encoding [, mixed from-encoding])
  2683. Returns converted string in desired encoding */
  2684. PHP_FUNCTION(mb_convert_encoding)
  2685. {
  2686. char *arg_str, *arg_new;
  2687. int str_len, new_len;
  2688. zval *arg_old;
  2689. int i;
  2690. size_t size, l, n;
  2691. char *_from_encodings = NULL, *ret, *s_free = NULL;
  2692. zval **hash_entry;
  2693. HashTable *target_hash;
  2694. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &arg_str, &str_len, &arg_new, &new_len, &arg_old) == FAILURE) {
  2695. return;
  2696. }
  2697. if (ZEND_NUM_ARGS() == 3) {
  2698. switch (Z_TYPE_P(arg_old)) {
  2699. case IS_ARRAY:
  2700. target_hash = Z_ARRVAL_P(arg_old);
  2701. zend_hash_internal_pointer_reset(target_hash);
  2702. i = zend_hash_num_elements(target_hash);
  2703. _from_encodings = NULL;
  2704. while (i > 0) {
  2705. if (zend_hash_get_current_data(target_hash, (void **) &hash_entry) == FAILURE) {
  2706. break;
  2707. }
  2708. convert_to_string_ex(hash_entry);
  2709. if ( _from_encodings) {
  2710. l = strlen(_from_encodings);
  2711. n = strlen(Z_STRVAL_PP(hash_entry));
  2712. _from_encodings = erealloc(_from_encodings, l+n+2);
  2713. strcpy(_from_encodings+l, ",");
  2714. strcpy(_from_encodings+l+1, Z_STRVAL_PP(hash_entry));
  2715. } else {
  2716. _from_encodings = estrdup(Z_STRVAL_PP(hash_entry));
  2717. }
  2718. zend_hash_move_forward(target_hash);
  2719. i--;
  2720. }
  2721. if (_from_encodings != NULL && !strlen(_from_encodings)) {
  2722. efree(_from_encodings);
  2723. _from_encodings = NULL;
  2724. }
  2725. s_free = _from_encodings;
  2726. break;
  2727. default:
  2728. convert_to_string(arg_old);
  2729. _from_encodings = Z_STRVAL_P(arg_old);
  2730. break;
  2731. }
  2732. }
  2733. /* new encoding */
  2734. ret = php_mb_convert_encoding(arg_str, str_len, arg_new, _from_encodings, &size TSRMLS_CC);
  2735. if (ret != NULL) {
  2736. RETVAL_STRINGL(ret, size, 0); /* the string is already strdup()'ed */
  2737. } else {
  2738. RETVAL_FALSE;
  2739. }
  2740. if ( s_free) {
  2741. efree(s_free);
  2742. }
  2743. }
  2744. /* }}} */
  2745. /* {{{ proto string mb_convert_case(string sourcestring, int mode [, string encoding])
  2746. Returns a case-folded version of sourcestring */
  2747. PHP_FUNCTION(mb_convert_case)
  2748. {
  2749. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2750. char *str;
  2751. int str_len, from_encoding_len;
  2752. long case_mode = 0;
  2753. char *newstr;
  2754. size_t ret_len;
  2755. RETVAL_FALSE;
  2756. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|s!", &str, &str_len,
  2757. &case_mode, &from_encoding, &from_encoding_len) == FAILURE)
  2758. RETURN_FALSE;
  2759. newstr = php_unicode_convert_case(case_mode, str, (size_t) str_len, &ret_len, from_encoding TSRMLS_CC);
  2760. if (newstr) {
  2761. RETVAL_STRINGL(newstr, ret_len, 0);
  2762. }
  2763. }
  2764. /* }}} */
  2765. /* {{{ proto string mb_strtoupper(string sourcestring [, string encoding])
  2766. * Returns a uppercased version of sourcestring
  2767. */
  2768. PHP_FUNCTION(mb_strtoupper)
  2769. {
  2770. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2771. char *str;
  2772. int str_len, from_encoding_len;
  2773. char *newstr;
  2774. size_t ret_len;
  2775. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &str, &str_len,
  2776. &from_encoding, &from_encoding_len) == FAILURE) {
  2777. return;
  2778. }
  2779. newstr = php_unicode_convert_case(PHP_UNICODE_CASE_UPPER, str, (size_t) str_len, &ret_len, from_encoding TSRMLS_CC);
  2780. if (newstr) {
  2781. RETURN_STRINGL(newstr, ret_len, 0);
  2782. }
  2783. RETURN_FALSE;
  2784. }
  2785. /* }}} */
  2786. /* {{{ proto string mb_strtolower(string sourcestring [, string encoding])
  2787. * Returns a lowercased version of sourcestring
  2788. */
  2789. PHP_FUNCTION(mb_strtolower)
  2790. {
  2791. const char *from_encoding = MBSTRG(current_internal_encoding)->mime_name;
  2792. char *str;
  2793. int str_len, from_encoding_len;
  2794. char *newstr;
  2795. size_t ret_len;
  2796. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &str, &str_len,
  2797. &from_encoding, &from_encoding_len) == FAILURE) {
  2798. return;
  2799. }
  2800. newstr = php_unicode_convert_case(PHP_UNICODE_CASE_LOWER, str, (size_t) str_len, &ret_len, from_encoding TSRMLS_CC);
  2801. if (newstr) {
  2802. RETURN_STRINGL(newstr, ret_len, 0);
  2803. }
  2804. RETURN_FALSE;
  2805. }
  2806. /* }}} */
  2807. /* {{{ proto string mb_detect_encoding(string str [, mixed encoding_list [, bool strict]])
  2808. Encodings of the given string is returned (as a string) */
  2809. PHP_FUNCTION(mb_detect_encoding)
  2810. {
  2811. char *str;
  2812. int str_len;
  2813. zend_bool strict=0;
  2814. zval *encoding_list;
  2815. mbfl_string string;
  2816. const mbfl_encoding *ret;
  2817. const mbfl_encoding **elist, **list;
  2818. size_t size;
  2819. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &str, &str_len, &encoding_list, &strict) == FAILURE) {
  2820. return;
  2821. }
  2822. /* make encoding list */
  2823. list = NULL;
  2824. size = 0;
  2825. if (ZEND_NUM_ARGS() >= 2 && !ZVAL_IS_NULL(encoding_list)) {
  2826. switch (Z_TYPE_P(encoding_list)) {
  2827. case IS_ARRAY:
  2828. if (FAILURE == php_mb_parse_encoding_array(encoding_list, &list, &size, 0 TSRMLS_CC)) {
  2829. if (list) {
  2830. efree(list);
  2831. list = NULL;
  2832. size = 0;
  2833. }
  2834. }
  2835. break;
  2836. default:
  2837. convert_to_string(encoding_list);
  2838. if (FAILURE == php_mb_parse_encoding_list(Z_STRVAL_P(encoding_list), Z_STRLEN_P(encoding_list), &list, &size, 0 TSRMLS_CC)) {
  2839. if (list) {
  2840. efree(list);
  2841. list = NULL;
  2842. size = 0;
  2843. }
  2844. }
  2845. break;
  2846. }
  2847. if (size <= 0) {
  2848. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal argument");
  2849. }
  2850. }
  2851. if (ZEND_NUM_ARGS() < 3) {
  2852. strict = (zend_bool)MBSTRG(strict_detection);
  2853. }
  2854. if (size > 0 && list != NULL) {
  2855. elist = list;
  2856. } else {
  2857. elist = MBSTRG(current_detect_order_list);
  2858. size = MBSTRG(current_detect_order_list_size);
  2859. }
  2860. mbfl_string_init(&string);
  2861. string.no_language = MBSTRG(language);
  2862. string.val = (unsigned char *)str;
  2863. string.len = str_len;
  2864. ret = mbfl_identify_encoding2(&string, elist, size, strict);
  2865. if (list != NULL) {
  2866. efree((void *)list);
  2867. }
  2868. if (ret == NULL) {
  2869. RETURN_FALSE;
  2870. }
  2871. RETVAL_STRING((char *)ret->name, 1);
  2872. }
  2873. /* }}} */
  2874. /* {{{ proto mixed mb_list_encodings()
  2875. Returns an array of all supported entity encodings */
  2876. PHP_FUNCTION(mb_list_encodings)
  2877. {
  2878. const mbfl_encoding **encodings;
  2879. const mbfl_encoding *encoding;
  2880. int i;
  2881. array_init(return_value);
  2882. i = 0;
  2883. encodings = mbfl_get_supported_encodings();
  2884. while ((encoding = encodings[i++]) != NULL) {
  2885. add_next_index_string(return_value, (char *) encoding->name, 1);
  2886. }
  2887. }
  2888. /* }}} */
  2889. /* {{{ proto array mb_encoding_aliases(string encoding)
  2890. Returns an array of the aliases of a given encoding name */
  2891. PHP_FUNCTION(mb_encoding_aliases)
  2892. {
  2893. const mbfl_encoding *encoding;
  2894. char *name = NULL;
  2895. int name_len;
  2896. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
  2897. RETURN_FALSE;
  2898. }
  2899. encoding = mbfl_name2encoding(name);
  2900. if (!encoding) {
  2901. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", name);
  2902. RETURN_FALSE;
  2903. }
  2904. array_init(return_value);
  2905. if (encoding->aliases != NULL) {
  2906. const char **alias;
  2907. for (alias = *encoding->aliases; *alias; ++alias) {
  2908. add_next_index_string(return_value, (char *)*alias, 1);
  2909. }
  2910. }
  2911. }
  2912. /* }}} */
  2913. /* {{{ proto string mb_encode_mimeheader(string str [, string charset [, string transfer-encoding [, string linefeed [, int indent]]]])
  2914. Converts the string to MIME "encoded-word" in the format of =?charset?(B|Q)?encoded_string?= */
  2915. PHP_FUNCTION(mb_encode_mimeheader)
  2916. {
  2917. enum mbfl_no_encoding charset, transenc;
  2918. mbfl_string string, result, *ret;
  2919. char *charset_name = NULL;
  2920. int charset_name_len;
  2921. char *trans_enc_name = NULL;
  2922. int trans_enc_name_len;
  2923. char *linefeed = "\r\n";
  2924. int linefeed_len;
  2925. long indent = 0;
  2926. mbfl_string_init(&string);
  2927. string.no_language = MBSTRG(language);
  2928. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2929. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sssl", (char **)&string.val, &string.len, &charset_name, &charset_name_len, &trans_enc_name, &trans_enc_name_len, &linefeed, &linefeed_len, &indent) == FAILURE) {
  2930. return;
  2931. }
  2932. charset = mbfl_no_encoding_pass;
  2933. transenc = mbfl_no_encoding_base64;
  2934. if (charset_name != NULL) {
  2935. charset = mbfl_name2no_encoding(charset_name);
  2936. if (charset == mbfl_no_encoding_invalid) {
  2937. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", charset_name);
  2938. RETURN_FALSE;
  2939. }
  2940. } else {
  2941. const mbfl_language *lang = mbfl_no2language(MBSTRG(language));
  2942. if (lang != NULL) {
  2943. charset = lang->mail_charset;
  2944. transenc = lang->mail_header_encoding;
  2945. }
  2946. }
  2947. if (trans_enc_name != NULL) {
  2948. if (*trans_enc_name == 'B' || *trans_enc_name == 'b') {
  2949. transenc = mbfl_no_encoding_base64;
  2950. } else if (*trans_enc_name == 'Q' || *trans_enc_name == 'q') {
  2951. transenc = mbfl_no_encoding_qprint;
  2952. }
  2953. }
  2954. mbfl_string_init(&result);
  2955. ret = mbfl_mime_header_encode(&string, &result, charset, transenc, linefeed, indent);
  2956. if (ret != NULL) {
  2957. RETVAL_STRINGL_CHECK((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  2958. } else {
  2959. RETVAL_FALSE;
  2960. }
  2961. }
  2962. /* }}} */
  2963. /* {{{ proto string mb_decode_mimeheader(string string)
  2964. Decodes the MIME "encoded-word" in the string */
  2965. PHP_FUNCTION(mb_decode_mimeheader)
  2966. {
  2967. mbfl_string string, result, *ret;
  2968. mbfl_string_init(&string);
  2969. string.no_language = MBSTRG(language);
  2970. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2971. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", (char **)&string.val, &string.len) == FAILURE) {
  2972. return;
  2973. }
  2974. mbfl_string_init(&result);
  2975. ret = mbfl_mime_header_decode(&string, &result, MBSTRG(current_internal_encoding)->no_encoding);
  2976. if (ret != NULL) {
  2977. RETVAL_STRINGL((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  2978. } else {
  2979. RETVAL_FALSE;
  2980. }
  2981. }
  2982. /* }}} */
  2983. /* {{{ proto string mb_convert_kana(string str [, string option] [, string encoding])
  2984. Conversion between full-width character and half-width character (Japanese) */
  2985. PHP_FUNCTION(mb_convert_kana)
  2986. {
  2987. int opt, i;
  2988. mbfl_string string, result, *ret;
  2989. char *optstr = NULL;
  2990. int optstr_len;
  2991. char *encname = NULL;
  2992. int encname_len;
  2993. mbfl_string_init(&string);
  2994. string.no_language = MBSTRG(language);
  2995. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  2996. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ss", (char **)&string.val, &string.len, &optstr, &optstr_len, &encname, &encname_len) == FAILURE) {
  2997. return;
  2998. }
  2999. /* option */
  3000. if (optstr != NULL) {
  3001. char *p = optstr;
  3002. int n = optstr_len;
  3003. i = 0;
  3004. opt = 0;
  3005. while (i < n) {
  3006. i++;
  3007. switch (*p++) {
  3008. case 'A':
  3009. opt |= 0x1;
  3010. break;
  3011. case 'a':
  3012. opt |= 0x10;
  3013. break;
  3014. case 'R':
  3015. opt |= 0x2;
  3016. break;
  3017. case 'r':
  3018. opt |= 0x20;
  3019. break;
  3020. case 'N':
  3021. opt |= 0x4;
  3022. break;
  3023. case 'n':
  3024. opt |= 0x40;
  3025. break;
  3026. case 'S':
  3027. opt |= 0x8;
  3028. break;
  3029. case 's':
  3030. opt |= 0x80;
  3031. break;
  3032. case 'K':
  3033. opt |= 0x100;
  3034. break;
  3035. case 'k':
  3036. opt |= 0x1000;
  3037. break;
  3038. case 'H':
  3039. opt |= 0x200;
  3040. break;
  3041. case 'h':
  3042. opt |= 0x2000;
  3043. break;
  3044. case 'V':
  3045. opt |= 0x800;
  3046. break;
  3047. case 'C':
  3048. opt |= 0x10000;
  3049. break;
  3050. case 'c':
  3051. opt |= 0x20000;
  3052. break;
  3053. case 'M':
  3054. opt |= 0x100000;
  3055. break;
  3056. case 'm':
  3057. opt |= 0x200000;
  3058. break;
  3059. }
  3060. }
  3061. } else {
  3062. opt = 0x900;
  3063. }
  3064. /* encoding */
  3065. if (encname != NULL) {
  3066. string.no_encoding = mbfl_name2no_encoding(encname);
  3067. if (string.no_encoding == mbfl_no_encoding_invalid) {
  3068. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", encname);
  3069. RETURN_FALSE;
  3070. }
  3071. }
  3072. ret = mbfl_ja_jp_hantozen(&string, &result, opt);
  3073. if (ret != NULL) {
  3074. RETVAL_STRINGL_CHECK((char *)ret->val, ret->len, 0); /* the string is already strdup()'ed */
  3075. } else {
  3076. RETVAL_FALSE;
  3077. }
  3078. }
  3079. /* }}} */
  3080. #define PHP_MBSTR_STACK_BLOCK_SIZE 32
  3081. /* {{{ proto string mb_convert_variables(string to-encoding, mixed from-encoding, mixed vars [, ...])
  3082. Converts the string resource in variables to desired encoding */
  3083. PHP_FUNCTION(mb_convert_variables)
  3084. {
  3085. zval ***args, ***stack, **var, **hash_entry, **zfrom_enc;
  3086. HashTable *target_hash;
  3087. mbfl_string string, result, *ret;
  3088. const mbfl_encoding *from_encoding, *to_encoding;
  3089. mbfl_encoding_detector *identd;
  3090. mbfl_buffer_converter *convd;
  3091. int n, to_enc_len, argc, stack_level, stack_max;
  3092. size_t elistsz;
  3093. const mbfl_encoding **elist;
  3094. char *to_enc;
  3095. void *ptmp;
  3096. int recursion_error = 0;
  3097. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sZ+", &to_enc, &to_enc_len, &zfrom_enc, &args, &argc) == FAILURE) {
  3098. return;
  3099. }
  3100. /* new encoding */
  3101. to_encoding = mbfl_name2encoding(to_enc);
  3102. if (!to_encoding) {
  3103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", to_enc);
  3104. efree(args);
  3105. RETURN_FALSE;
  3106. }
  3107. /* initialize string */
  3108. mbfl_string_init(&string);
  3109. mbfl_string_init(&result);
  3110. from_encoding = MBSTRG(current_internal_encoding);
  3111. string.no_encoding = from_encoding->no_encoding;
  3112. string.no_language = MBSTRG(language);
  3113. /* pre-conversion encoding */
  3114. elist = NULL;
  3115. elistsz = 0;
  3116. switch (Z_TYPE_PP(zfrom_enc)) {
  3117. case IS_ARRAY:
  3118. php_mb_parse_encoding_array(*zfrom_enc, &elist, &elistsz, 0 TSRMLS_CC);
  3119. break;
  3120. default:
  3121. convert_to_string_ex(zfrom_enc);
  3122. php_mb_parse_encoding_list(Z_STRVAL_PP(zfrom_enc), Z_STRLEN_PP(zfrom_enc), &elist, &elistsz, 0 TSRMLS_CC);
  3123. break;
  3124. }
  3125. if (elistsz <= 0) {
  3126. from_encoding = &mbfl_encoding_pass;
  3127. } else if (elistsz == 1) {
  3128. from_encoding = *elist;
  3129. } else {
  3130. /* auto detect */
  3131. from_encoding = NULL;
  3132. stack_max = PHP_MBSTR_STACK_BLOCK_SIZE;
  3133. stack = (zval ***)safe_emalloc(stack_max, sizeof(zval **), 0);
  3134. stack_level = 0;
  3135. identd = mbfl_encoding_detector_new2(elist, elistsz, MBSTRG(strict_detection));
  3136. if (identd != NULL) {
  3137. n = 0;
  3138. while (n < argc || stack_level > 0) {
  3139. if (stack_level <= 0) {
  3140. var = args[n++];
  3141. if (Z_TYPE_PP(var) == IS_ARRAY || Z_TYPE_PP(var) == IS_OBJECT) {
  3142. target_hash = HASH_OF(*var);
  3143. if (target_hash != NULL) {
  3144. zend_hash_internal_pointer_reset(target_hash);
  3145. }
  3146. }
  3147. } else {
  3148. stack_level--;
  3149. var = stack[stack_level];
  3150. }
  3151. if (Z_TYPE_PP(var) == IS_ARRAY || Z_TYPE_PP(var) == IS_OBJECT) {
  3152. target_hash = HASH_OF(*var);
  3153. if (target_hash != NULL) {
  3154. while (zend_hash_get_current_data(target_hash, (void **) &hash_entry) != FAILURE) {
  3155. if (++target_hash->nApplyCount > 1) {
  3156. --target_hash->nApplyCount;
  3157. recursion_error = 1;
  3158. goto detect_end;
  3159. }
  3160. zend_hash_move_forward(target_hash);
  3161. if (Z_TYPE_PP(hash_entry) == IS_ARRAY || Z_TYPE_PP(hash_entry) == IS_OBJECT) {
  3162. if (stack_level >= stack_max) {
  3163. stack_max += PHP_MBSTR_STACK_BLOCK_SIZE;
  3164. ptmp = erealloc(stack, sizeof(zval **)*stack_max);
  3165. stack = (zval ***)ptmp;
  3166. }
  3167. stack[stack_level] = var;
  3168. stack_level++;
  3169. var = hash_entry;
  3170. target_hash = HASH_OF(*var);
  3171. if (target_hash != NULL) {
  3172. zend_hash_internal_pointer_reset(target_hash);
  3173. continue;
  3174. }
  3175. } else if (Z_TYPE_PP(hash_entry) == IS_STRING) {
  3176. string.val = (unsigned char *)Z_STRVAL_PP(hash_entry);
  3177. string.len = Z_STRLEN_PP(hash_entry);
  3178. if (mbfl_encoding_detector_feed(identd, &string)) {
  3179. goto detect_end; /* complete detecting */
  3180. }
  3181. }
  3182. }
  3183. }
  3184. } else if (Z_TYPE_PP(var) == IS_STRING) {
  3185. string.val = (unsigned char *)Z_STRVAL_PP(var);
  3186. string.len = Z_STRLEN_PP(var);
  3187. if (mbfl_encoding_detector_feed(identd, &string)) {
  3188. goto detect_end; /* complete detecting */
  3189. }
  3190. }
  3191. }
  3192. detect_end:
  3193. from_encoding = mbfl_encoding_detector_judge2(identd);
  3194. mbfl_encoding_detector_delete(identd);
  3195. }
  3196. if (recursion_error) {
  3197. while(stack_level-- && (var = stack[stack_level])) {
  3198. if (HASH_OF(*var)->nApplyCount > 1) {
  3199. HASH_OF(*var)->nApplyCount--;
  3200. }
  3201. }
  3202. efree(stack);
  3203. efree(args);
  3204. if (elist != NULL) {
  3205. efree((void *)elist);
  3206. }
  3207. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot handle recursive references");
  3208. RETURN_FALSE;
  3209. }
  3210. efree(stack);
  3211. if (!from_encoding) {
  3212. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to detect encoding");
  3213. from_encoding = &mbfl_encoding_pass;
  3214. }
  3215. }
  3216. if (elist != NULL) {
  3217. efree((void *)elist);
  3218. }
  3219. /* create converter */
  3220. convd = NULL;
  3221. if (from_encoding != &mbfl_encoding_pass) {
  3222. convd = mbfl_buffer_converter_new2(from_encoding, to_encoding, 0);
  3223. if (convd == NULL) {
  3224. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create converter");
  3225. RETURN_FALSE;
  3226. }
  3227. mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
  3228. mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
  3229. }
  3230. /* convert */
  3231. if (convd != NULL) {
  3232. stack_max = PHP_MBSTR_STACK_BLOCK_SIZE;
  3233. stack = (zval ***)safe_emalloc(stack_max, sizeof(zval **), 0);
  3234. stack_level = 0;
  3235. n = 0;
  3236. while (n < argc || stack_level > 0) {
  3237. if (stack_level <= 0) {
  3238. var = args[n++];
  3239. if (Z_TYPE_PP(var) == IS_ARRAY || Z_TYPE_PP(var) == IS_OBJECT) {
  3240. target_hash = HASH_OF(*var);
  3241. if (target_hash != NULL) {
  3242. zend_hash_internal_pointer_reset(target_hash);
  3243. }
  3244. }
  3245. } else {
  3246. stack_level--;
  3247. var = stack[stack_level];
  3248. }
  3249. if (Z_TYPE_PP(var) == IS_ARRAY || Z_TYPE_PP(var) == IS_OBJECT) {
  3250. target_hash = HASH_OF(*var);
  3251. if (target_hash != NULL) {
  3252. while (zend_hash_get_current_data(target_hash, (void **) &hash_entry) != FAILURE) {
  3253. zend_hash_move_forward(target_hash);
  3254. if (Z_TYPE_PP(hash_entry) == IS_ARRAY || Z_TYPE_PP(hash_entry) == IS_OBJECT) {
  3255. if (++(HASH_OF(*hash_entry)->nApplyCount) > 1) {
  3256. --(HASH_OF(*hash_entry)->nApplyCount);
  3257. recursion_error = 1;
  3258. goto conv_end;
  3259. }
  3260. if (stack_level >= stack_max) {
  3261. stack_max += PHP_MBSTR_STACK_BLOCK_SIZE;
  3262. ptmp = erealloc(stack, sizeof(zval **)*stack_max);
  3263. stack = (zval ***)ptmp;
  3264. }
  3265. stack[stack_level] = var;
  3266. stack_level++;
  3267. var = hash_entry;
  3268. SEPARATE_ZVAL_IF_NOT_REF(hash_entry);
  3269. target_hash = HASH_OF(*var);
  3270. if (target_hash != NULL) {
  3271. zend_hash_internal_pointer_reset(target_hash);
  3272. continue;
  3273. }
  3274. } else if (Z_TYPE_PP(hash_entry) == IS_STRING) {
  3275. string.val = (unsigned char *)Z_STRVAL_PP(hash_entry);
  3276. string.len = Z_STRLEN_PP(hash_entry);
  3277. ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
  3278. if (ret != NULL) {
  3279. if (Z_REFCOUNT_PP(hash_entry) > 1) {
  3280. Z_DELREF_PP(hash_entry);
  3281. MAKE_STD_ZVAL(*hash_entry);
  3282. } else {
  3283. zval_dtor(*hash_entry);
  3284. }
  3285. ZVAL_STRINGL(*hash_entry, (char *)ret->val, ret->len, 0);
  3286. }
  3287. }
  3288. }
  3289. }
  3290. } else if (Z_TYPE_PP(var) == IS_STRING) {
  3291. string.val = (unsigned char *)Z_STRVAL_PP(var);
  3292. string.len = Z_STRLEN_PP(var);
  3293. ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
  3294. if (ret != NULL) {
  3295. zval_dtor(*var);
  3296. ZVAL_STRINGL(*var, (char *)ret->val, ret->len, 0);
  3297. }
  3298. }
  3299. }
  3300. conv_end:
  3301. MBSTRG(illegalchars) += mbfl_buffer_illegalchars(convd);
  3302. mbfl_buffer_converter_delete(convd);
  3303. if (recursion_error) {
  3304. while(stack_level-- && (var = stack[stack_level])) {
  3305. if (HASH_OF(*var)->nApplyCount > 1) {
  3306. HASH_OF(*var)->nApplyCount--;
  3307. }
  3308. }
  3309. efree(stack);
  3310. efree(args);
  3311. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot handle recursive references");
  3312. RETURN_FALSE;
  3313. }
  3314. efree(stack);
  3315. }
  3316. efree(args);
  3317. if (from_encoding) {
  3318. RETURN_STRING(from_encoding->name, 1);
  3319. } else {
  3320. RETURN_FALSE;
  3321. }
  3322. }
  3323. /* }}} */
  3324. /* {{{ HTML numeric entity */
  3325. /* {{{ static void php_mb_numericentity_exec() */
  3326. static void
  3327. php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAMETERS, int type)
  3328. {
  3329. char *str, *encoding;
  3330. int str_len, encoding_len;
  3331. zval *zconvmap, **hash_entry;
  3332. HashTable *target_hash;
  3333. size_t argc = ZEND_NUM_ARGS();
  3334. int i, *convmap, *mapelm, mapsize=0;
  3335. zend_bool is_hex = 0;
  3336. mbfl_string string, result, *ret;
  3337. enum mbfl_no_encoding no_encoding;
  3338. if (zend_parse_parameters(argc TSRMLS_CC, "sz|sb", &str, &str_len, &zconvmap, &encoding, &encoding_len, &is_hex) == FAILURE) {
  3339. return;
  3340. }
  3341. mbfl_string_init(&string);
  3342. string.no_language = MBSTRG(language);
  3343. string.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  3344. string.val = (unsigned char *)str;
  3345. string.len = str_len;
  3346. /* encoding */
  3347. if ((argc == 3 || argc == 4) && encoding_len > 0) {
  3348. no_encoding = mbfl_name2no_encoding(encoding);
  3349. if (no_encoding == mbfl_no_encoding_invalid) {
  3350. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", encoding);
  3351. RETURN_FALSE;
  3352. } else {
  3353. string.no_encoding = no_encoding;
  3354. }
  3355. }
  3356. if (argc == 4) {
  3357. if (type == 0 && is_hex) {
  3358. type = 2; /* output in hex format */
  3359. }
  3360. }
  3361. /* conversion map */
  3362. convmap = NULL;
  3363. if (Z_TYPE_P(zconvmap) == IS_ARRAY) {
  3364. target_hash = Z_ARRVAL_P(zconvmap);
  3365. zend_hash_internal_pointer_reset(target_hash);
  3366. i = zend_hash_num_elements(target_hash);
  3367. if (i > 0) {
  3368. convmap = (int *)safe_emalloc(i, sizeof(int), 0);
  3369. mapelm = convmap;
  3370. mapsize = 0;
  3371. while (i > 0) {
  3372. if (zend_hash_get_current_data(target_hash, (void **) &hash_entry) == FAILURE) {
  3373. break;
  3374. }
  3375. convert_to_long_ex(hash_entry);
  3376. *mapelm++ = Z_LVAL_PP(hash_entry);
  3377. mapsize++;
  3378. i--;
  3379. zend_hash_move_forward(target_hash);
  3380. }
  3381. }
  3382. }
  3383. if (convmap == NULL) {
  3384. RETURN_FALSE;
  3385. }
  3386. mapsize /= 4;
  3387. ret = mbfl_html_numeric_entity(&string, &result, convmap, mapsize, type);
  3388. if (ret != NULL) {
  3389. RETVAL_STRINGL_CHECK((char *)ret->val, ret->len, 0);
  3390. } else {
  3391. RETVAL_FALSE;
  3392. }
  3393. efree((void *)convmap);
  3394. }
  3395. /* }}} */
  3396. /* {{{ proto string mb_encode_numericentity(string string, array convmap [, string encoding [, bool is_hex]])
  3397. Converts specified characters to HTML numeric entities */
  3398. PHP_FUNCTION(mb_encode_numericentity)
  3399. {
  3400. php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3401. }
  3402. /* }}} */
  3403. /* {{{ proto string mb_decode_numericentity(string string, array convmap [, string encoding])
  3404. Converts HTML numeric entities to character code */
  3405. PHP_FUNCTION(mb_decode_numericentity)
  3406. {
  3407. php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3408. }
  3409. /* }}} */
  3410. /* }}} */
  3411. /* {{{ proto int mb_send_mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
  3412. * Sends an email message with MIME scheme
  3413. */
  3414. #define SKIP_LONG_HEADER_SEP_MBSTRING(str, pos) \
  3415. if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) { \
  3416. pos += 2; \
  3417. while (str[pos + 1] == ' ' || str[pos + 1] == '\t') { \
  3418. pos++; \
  3419. } \
  3420. continue; \
  3421. }
  3422. #define MAIL_ASCIIZ_CHECK_MBSTRING(str, len) \
  3423. pp = str; \
  3424. ee = pp + len; \
  3425. while ((pp = memchr(pp, '\0', (ee - pp)))) { \
  3426. *pp = ' '; \
  3427. } \
  3428. #define APPEND_ONE_CHAR(ch) do { \
  3429. if (token.a > 0) { \
  3430. smart_str_appendc(&token, ch); \
  3431. } else {\
  3432. token.len++; \
  3433. } \
  3434. } while (0)
  3435. #define SEPARATE_SMART_STR(str) do {\
  3436. if ((str)->a == 0) { \
  3437. char *tmp_ptr; \
  3438. (str)->a = 1; \
  3439. while ((str)->a < (str)->len) { \
  3440. (str)->a <<= 1; \
  3441. } \
  3442. tmp_ptr = emalloc((str)->a + 1); \
  3443. memcpy(tmp_ptr, (str)->c, (str)->len); \
  3444. (str)->c = tmp_ptr; \
  3445. } \
  3446. } while (0)
  3447. static void my_smart_str_dtor(smart_str *s)
  3448. {
  3449. if (s->a > 0) {
  3450. smart_str_free(s);
  3451. }
  3452. }
  3453. static int _php_mbstr_parse_mail_headers(HashTable *ht, const char *str, size_t str_len)
  3454. {
  3455. const char *ps;
  3456. size_t icnt;
  3457. int state = 0;
  3458. int crlf_state = -1;
  3459. smart_str token = { 0, 0, 0 };
  3460. smart_str fld_name = { 0, 0, 0 }, fld_val = { 0, 0, 0 };
  3461. ps = str;
  3462. icnt = str_len;
  3463. /*
  3464. * C o n t e n t - T y p e : t e x t / h t m l \r\n
  3465. * ^ ^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^ ^^^^
  3466. * state 0 1 2 3
  3467. *
  3468. * C o n t e n t - T y p e : t e x t / h t m l \r\n
  3469. * ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
  3470. * crlf_state -1 0 1 -1
  3471. *
  3472. */
  3473. while (icnt > 0) {
  3474. switch (*ps) {
  3475. case ':':
  3476. if (crlf_state == 1) {
  3477. APPEND_ONE_CHAR('\r');
  3478. }
  3479. if (state == 0 || state == 1) {
  3480. fld_name = token;
  3481. state = 2;
  3482. } else {
  3483. APPEND_ONE_CHAR(*ps);
  3484. }
  3485. crlf_state = 0;
  3486. break;
  3487. case '\n':
  3488. if (crlf_state == -1) {
  3489. goto out;
  3490. }
  3491. crlf_state = -1;
  3492. break;
  3493. case '\r':
  3494. if (crlf_state == 1) {
  3495. APPEND_ONE_CHAR('\r');
  3496. } else {
  3497. crlf_state = 1;
  3498. }
  3499. break;
  3500. case ' ': case '\t':
  3501. if (crlf_state == -1) {
  3502. if (state == 3) {
  3503. /* continuing from the previous line */
  3504. SEPARATE_SMART_STR(&token);
  3505. state = 4;
  3506. } else {
  3507. /* simply skipping this new line */
  3508. state = 5;
  3509. }
  3510. } else {
  3511. if (crlf_state == 1) {
  3512. APPEND_ONE_CHAR('\r');
  3513. }
  3514. if (state == 1 || state == 3) {
  3515. APPEND_ONE_CHAR(*ps);
  3516. }
  3517. }
  3518. crlf_state = 0;
  3519. break;
  3520. default:
  3521. switch (state) {
  3522. case 0:
  3523. token.c = (char *)ps;
  3524. token.len = 0;
  3525. token.a = 0;
  3526. state = 1;
  3527. break;
  3528. case 2:
  3529. if (crlf_state != -1) {
  3530. token.c = (char *)ps;
  3531. token.len = 0;
  3532. token.a = 0;
  3533. state = 3;
  3534. break;
  3535. }
  3536. /* break is missing intentionally */
  3537. case 3:
  3538. if (crlf_state == -1) {
  3539. fld_val = token;
  3540. if (fld_name.c != NULL && fld_val.c != NULL) {
  3541. char *dummy;
  3542. /* FIXME: some locale free implementation is
  3543. * really required here,,, */
  3544. SEPARATE_SMART_STR(&fld_name);
  3545. php_strtoupper(fld_name.c, fld_name.len);
  3546. zend_hash_update(ht, (char *)fld_name.c, fld_name.len, &fld_val, sizeof(smart_str), (void **)&dummy);
  3547. my_smart_str_dtor(&fld_name);
  3548. }
  3549. memset(&fld_name, 0, sizeof(smart_str));
  3550. memset(&fld_val, 0, sizeof(smart_str));
  3551. token.c = (char *)ps;
  3552. token.len = 0;
  3553. token.a = 0;
  3554. state = 1;
  3555. }
  3556. break;
  3557. case 4:
  3558. APPEND_ONE_CHAR(' ');
  3559. state = 3;
  3560. break;
  3561. }
  3562. if (crlf_state == 1) {
  3563. APPEND_ONE_CHAR('\r');
  3564. }
  3565. APPEND_ONE_CHAR(*ps);
  3566. crlf_state = 0;
  3567. break;
  3568. }
  3569. ps++, icnt--;
  3570. }
  3571. out:
  3572. if (state == 2) {
  3573. token.c = "";
  3574. token.len = 0;
  3575. token.a = 0;
  3576. state = 3;
  3577. }
  3578. if (state == 3) {
  3579. fld_val = token;
  3580. if (fld_name.c != NULL && fld_val.c != NULL) {
  3581. void *dummy;
  3582. /* FIXME: some locale free implementation is
  3583. * really required here,,, */
  3584. SEPARATE_SMART_STR(&fld_name);
  3585. php_strtoupper(fld_name.c, fld_name.len);
  3586. zend_hash_update(ht, (char *)fld_name.c, fld_name.len, &fld_val, sizeof(smart_str), (void **)&dummy);
  3587. my_smart_str_dtor(&fld_name);
  3588. }
  3589. }
  3590. return state;
  3591. }
  3592. PHP_FUNCTION(mb_send_mail)
  3593. {
  3594. int n;
  3595. char *to = NULL;
  3596. int to_len;
  3597. char *message = NULL;
  3598. int message_len;
  3599. char *headers = NULL;
  3600. int headers_len;
  3601. char *subject = NULL;
  3602. int subject_len;
  3603. char *extra_cmd = NULL;
  3604. int extra_cmd_len;
  3605. int i;
  3606. char *to_r = NULL;
  3607. char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
  3608. struct {
  3609. int cnt_type:1;
  3610. int cnt_trans_enc:1;
  3611. } suppressed_hdrs = { 0, 0 };
  3612. char *message_buf = NULL, *subject_buf = NULL, *p;
  3613. mbfl_string orig_str, conv_str;
  3614. mbfl_string *pstr; /* pointer to mbfl string for return value */
  3615. enum mbfl_no_encoding
  3616. tran_cs, /* transfar text charset */
  3617. head_enc, /* header transfar encoding */
  3618. body_enc; /* body transfar encoding */
  3619. mbfl_memory_device device; /* automatic allocateable buffer for additional header */
  3620. const mbfl_language *lang;
  3621. int err = 0;
  3622. HashTable ht_headers;
  3623. smart_str *s;
  3624. extern void mbfl_memory_device_unput(mbfl_memory_device *device);
  3625. char *pp, *ee;
  3626. /* initialize */
  3627. mbfl_memory_device_init(&device, 0, 0);
  3628. mbfl_string_init(&orig_str);
  3629. mbfl_string_init(&conv_str);
  3630. /* character-set, transfer-encoding */
  3631. tran_cs = mbfl_no_encoding_utf8;
  3632. head_enc = mbfl_no_encoding_base64;
  3633. body_enc = mbfl_no_encoding_base64;
  3634. lang = mbfl_no2language(MBSTRG(language));
  3635. if (lang != NULL) {
  3636. tran_cs = lang->mail_charset;
  3637. head_enc = lang->mail_header_encoding;
  3638. body_enc = lang->mail_body_encoding;
  3639. }
  3640. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss", &to, &to_len, &subject, &subject_len, &message, &message_len, &headers, &headers_len, &extra_cmd, &extra_cmd_len) == FAILURE) {
  3641. return;
  3642. }
  3643. /* ASCIIZ check */
  3644. MAIL_ASCIIZ_CHECK_MBSTRING(to, to_len);
  3645. MAIL_ASCIIZ_CHECK_MBSTRING(subject, subject_len);
  3646. MAIL_ASCIIZ_CHECK_MBSTRING(message, message_len);
  3647. if (headers) {
  3648. MAIL_ASCIIZ_CHECK_MBSTRING(headers, headers_len);
  3649. }
  3650. if (extra_cmd) {
  3651. MAIL_ASCIIZ_CHECK_MBSTRING(extra_cmd, extra_cmd_len);
  3652. }
  3653. zend_hash_init(&ht_headers, 0, NULL, (dtor_func_t) my_smart_str_dtor, 0);
  3654. if (headers != NULL) {
  3655. _php_mbstr_parse_mail_headers(&ht_headers, headers, headers_len);
  3656. }
  3657. if (zend_hash_find(&ht_headers, "CONTENT-TYPE", sizeof("CONTENT-TYPE") - 1, (void **)&s) == SUCCESS) {
  3658. char *tmp;
  3659. char *param_name;
  3660. char *charset = NULL;
  3661. SEPARATE_SMART_STR(s);
  3662. smart_str_0(s);
  3663. p = strchr(s->c, ';');
  3664. if (p != NULL) {
  3665. /* skipping the padded spaces */
  3666. do {
  3667. ++p;
  3668. } while (*p == ' ' || *p == '\t');
  3669. if (*p != '\0') {
  3670. if ((param_name = php_strtok_r(p, "= ", &tmp)) != NULL) {
  3671. if (strcasecmp(param_name, "charset") == 0) {
  3672. enum mbfl_no_encoding _tran_cs = tran_cs;
  3673. charset = php_strtok_r(NULL, "= \"", &tmp);
  3674. if (charset != NULL) {
  3675. _tran_cs = mbfl_name2no_encoding(charset);
  3676. }
  3677. if (_tran_cs == mbfl_no_encoding_invalid) {
  3678. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported charset \"%s\" - will be regarded as ascii", charset);
  3679. _tran_cs = mbfl_no_encoding_ascii;
  3680. }
  3681. tran_cs = _tran_cs;
  3682. }
  3683. }
  3684. }
  3685. }
  3686. suppressed_hdrs.cnt_type = 1;
  3687. }
  3688. if (zend_hash_find(&ht_headers, "CONTENT-TRANSFER-ENCODING", sizeof("CONTENT-TRANSFER-ENCODING") - 1, (void **)&s) == SUCCESS) {
  3689. enum mbfl_no_encoding _body_enc;
  3690. SEPARATE_SMART_STR(s);
  3691. smart_str_0(s);
  3692. _body_enc = mbfl_name2no_encoding(s->c);
  3693. switch (_body_enc) {
  3694. case mbfl_no_encoding_base64:
  3695. case mbfl_no_encoding_7bit:
  3696. case mbfl_no_encoding_8bit:
  3697. body_enc = _body_enc;
  3698. break;
  3699. default:
  3700. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported transfer encoding \"%s\" - will be regarded as 8bit", s->c);
  3701. body_enc = mbfl_no_encoding_8bit;
  3702. break;
  3703. }
  3704. suppressed_hdrs.cnt_trans_enc = 1;
  3705. }
  3706. /* To: */
  3707. if (to != NULL) {
  3708. if (to_len > 0) {
  3709. to_r = estrndup(to, to_len);
  3710. for (; to_len; to_len--) {
  3711. if (!isspace((unsigned char) to_r[to_len - 1])) {
  3712. break;
  3713. }
  3714. to_r[to_len - 1] = '\0';
  3715. }
  3716. for (i = 0; to_r[i]; i++) {
  3717. if (iscntrl((unsigned char) to_r[i])) {
  3718. /* According to RFC 822, section 3.1.1 long headers may be separated into
  3719. * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
  3720. * To prevent these separators from being replaced with a space, we use the
  3721. * SKIP_LONG_HEADER_SEP_MBSTRING to skip over them.
  3722. */
  3723. SKIP_LONG_HEADER_SEP_MBSTRING(to_r, i);
  3724. to_r[i] = ' ';
  3725. }
  3726. }
  3727. } else {
  3728. to_r = to;
  3729. }
  3730. } else {
  3731. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing To: field");
  3732. err = 1;
  3733. }
  3734. /* Subject: */
  3735. if (subject != NULL && subject_len >= 0) {
  3736. orig_str.no_language = MBSTRG(language);
  3737. orig_str.val = (unsigned char *)subject;
  3738. orig_str.len = subject_len;
  3739. orig_str.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  3740. if (orig_str.no_encoding == mbfl_no_encoding_invalid || orig_str.no_encoding == mbfl_no_encoding_pass) {
  3741. const mbfl_encoding *encoding = mbfl_identify_encoding2(&orig_str, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
  3742. orig_str.no_encoding = encoding ? encoding->no_encoding: mbfl_no_encoding_invalid;
  3743. }
  3744. pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, "\n", sizeof("Subject: [PHP-jp nnnnnnnn]"));
  3745. if (pstr != NULL) {
  3746. subject_buf = subject = (char *)pstr->val;
  3747. }
  3748. } else {
  3749. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing Subject: field");
  3750. err = 1;
  3751. }
  3752. /* message body */
  3753. if (message != NULL) {
  3754. orig_str.no_language = MBSTRG(language);
  3755. orig_str.val = (unsigned char *)message;
  3756. orig_str.len = (unsigned int)message_len;
  3757. orig_str.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  3758. if (orig_str.no_encoding == mbfl_no_encoding_invalid || orig_str.no_encoding == mbfl_no_encoding_pass) {
  3759. const mbfl_encoding *encoding = mbfl_identify_encoding2(&orig_str, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
  3760. orig_str.no_encoding = encoding ? encoding->no_encoding: mbfl_no_encoding_invalid;
  3761. }
  3762. pstr = NULL;
  3763. {
  3764. mbfl_string tmpstr;
  3765. if (mbfl_convert_encoding(&orig_str, &tmpstr, tran_cs) != NULL) {
  3766. tmpstr.no_encoding=mbfl_no_encoding_8bit;
  3767. pstr = mbfl_convert_encoding(&tmpstr, &conv_str, body_enc);
  3768. efree(tmpstr.val);
  3769. }
  3770. }
  3771. if (pstr != NULL) {
  3772. message_buf = message = (char *)pstr->val;
  3773. }
  3774. } else {
  3775. /* this is not really an error, so it is allowed. */
  3776. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty message body");
  3777. message = NULL;
  3778. }
  3779. /* other headers */
  3780. #define PHP_MBSTR_MAIL_MIME_HEADER1 "MIME-Version: 1.0"
  3781. #define PHP_MBSTR_MAIL_MIME_HEADER2 "Content-Type: text/plain"
  3782. #define PHP_MBSTR_MAIL_MIME_HEADER3 "; charset="
  3783. #define PHP_MBSTR_MAIL_MIME_HEADER4 "Content-Transfer-Encoding: "
  3784. if (headers != NULL) {
  3785. p = headers;
  3786. n = headers_len;
  3787. mbfl_memory_device_strncat(&device, p, n);
  3788. if (n > 0 && p[n - 1] != '\n') {
  3789. mbfl_memory_device_strncat(&device, "\n", 1);
  3790. }
  3791. }
  3792. if (!zend_hash_exists(&ht_headers, "MIME-VERSION", sizeof("MIME-VERSION") - 1)) {
  3793. mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER1, sizeof(PHP_MBSTR_MAIL_MIME_HEADER1) - 1);
  3794. mbfl_memory_device_strncat(&device, "\n", 1);
  3795. }
  3796. if (!suppressed_hdrs.cnt_type) {
  3797. mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER2, sizeof(PHP_MBSTR_MAIL_MIME_HEADER2) - 1);
  3798. p = (char *)mbfl_no2preferred_mime_name(tran_cs);
  3799. if (p != NULL) {
  3800. mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER3, sizeof(PHP_MBSTR_MAIL_MIME_HEADER3) - 1);
  3801. mbfl_memory_device_strcat(&device, p);
  3802. }
  3803. mbfl_memory_device_strncat(&device, "\n", 1);
  3804. }
  3805. if (!suppressed_hdrs.cnt_trans_enc) {
  3806. mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER4, sizeof(PHP_MBSTR_MAIL_MIME_HEADER4) - 1);
  3807. p = (char *)mbfl_no2preferred_mime_name(body_enc);
  3808. if (p == NULL) {
  3809. p = "7bit";
  3810. }
  3811. mbfl_memory_device_strcat(&device, p);
  3812. mbfl_memory_device_strncat(&device, "\n", 1);
  3813. }
  3814. mbfl_memory_device_unput(&device);
  3815. mbfl_memory_device_output('\0', &device);
  3816. headers = (char *)device.buffer;
  3817. if (force_extra_parameters) {
  3818. extra_cmd = php_escape_shell_cmd(force_extra_parameters);
  3819. } else if (extra_cmd) {
  3820. extra_cmd = php_escape_shell_cmd(extra_cmd);
  3821. }
  3822. if (!err && php_mail(to_r, subject, message, headers, extra_cmd TSRMLS_CC)) {
  3823. RETVAL_TRUE;
  3824. } else {
  3825. RETVAL_FALSE;
  3826. }
  3827. if (extra_cmd) {
  3828. efree(extra_cmd);
  3829. }
  3830. if (to_r != to) {
  3831. efree(to_r);
  3832. }
  3833. if (subject_buf) {
  3834. efree((void *)subject_buf);
  3835. }
  3836. if (message_buf) {
  3837. efree((void *)message_buf);
  3838. }
  3839. mbfl_memory_device_clear(&device);
  3840. zend_hash_destroy(&ht_headers);
  3841. }
  3842. #undef SKIP_LONG_HEADER_SEP_MBSTRING
  3843. #undef MAIL_ASCIIZ_CHECK_MBSTRING
  3844. #undef APPEND_ONE_CHAR
  3845. #undef SEPARATE_SMART_STR
  3846. #undef PHP_MBSTR_MAIL_MIME_HEADER1
  3847. #undef PHP_MBSTR_MAIL_MIME_HEADER2
  3848. #undef PHP_MBSTR_MAIL_MIME_HEADER3
  3849. #undef PHP_MBSTR_MAIL_MIME_HEADER4
  3850. /* }}} */
  3851. /* {{{ proto mixed mb_get_info([string type])
  3852. Returns the current settings of mbstring */
  3853. PHP_FUNCTION(mb_get_info)
  3854. {
  3855. char *typ = NULL;
  3856. int typ_len;
  3857. size_t n;
  3858. char *name;
  3859. const struct mb_overload_def *over_func;
  3860. zval *row1, *row2;
  3861. const mbfl_language *lang = mbfl_no2language(MBSTRG(language));
  3862. const mbfl_encoding **entry;
  3863. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &typ, &typ_len) == FAILURE) {
  3864. RETURN_FALSE;
  3865. }
  3866. if (!typ || !strcasecmp("all", typ)) {
  3867. array_init(return_value);
  3868. if (MBSTRG(current_internal_encoding)) {
  3869. add_assoc_string(return_value, "internal_encoding", (char *)MBSTRG(current_internal_encoding)->name, 1);
  3870. }
  3871. if (MBSTRG(http_input_identify)) {
  3872. add_assoc_string(return_value, "http_input", (char *)MBSTRG(http_input_identify)->name, 1);
  3873. }
  3874. if (MBSTRG(current_http_output_encoding)) {
  3875. add_assoc_string(return_value, "http_output", (char *)MBSTRG(current_http_output_encoding)->name, 1);
  3876. }
  3877. if ((name = (char *)zend_ini_string("mbstring.http_output_conv_mimetypes", sizeof("mbstring.http_output_conv_mimetypes"), 0)) != NULL) {
  3878. add_assoc_string(return_value, "http_output_conv_mimetypes", name, 1);
  3879. }
  3880. add_assoc_long(return_value, "func_overload", MBSTRG(func_overload));
  3881. if (MBSTRG(func_overload)){
  3882. over_func = &(mb_ovld[0]);
  3883. MAKE_STD_ZVAL(row1);
  3884. array_init(row1);
  3885. while (over_func->type > 0) {
  3886. if ((MBSTRG(func_overload) & over_func->type) == over_func->type ) {
  3887. add_assoc_string(row1, over_func->orig_func, over_func->ovld_func, 1);
  3888. }
  3889. over_func++;
  3890. }
  3891. add_assoc_zval(return_value, "func_overload_list", row1);
  3892. } else {
  3893. add_assoc_string(return_value, "func_overload_list", "no overload", 1);
  3894. }
  3895. if (lang != NULL) {
  3896. if ((name = (char *)mbfl_no_encoding2name(lang->mail_charset)) != NULL) {
  3897. add_assoc_string(return_value, "mail_charset", name, 1);
  3898. }
  3899. if ((name = (char *)mbfl_no_encoding2name(lang->mail_header_encoding)) != NULL) {
  3900. add_assoc_string(return_value, "mail_header_encoding", name, 1);
  3901. }
  3902. if ((name = (char *)mbfl_no_encoding2name(lang->mail_body_encoding)) != NULL) {
  3903. add_assoc_string(return_value, "mail_body_encoding", name, 1);
  3904. }
  3905. }
  3906. add_assoc_long(return_value, "illegal_chars", MBSTRG(illegalchars));
  3907. if (MBSTRG(encoding_translation)) {
  3908. add_assoc_string(return_value, "encoding_translation", "On", 1);
  3909. } else {
  3910. add_assoc_string(return_value, "encoding_translation", "Off", 1);
  3911. }
  3912. if ((name = (char *)mbfl_no_language2name(MBSTRG(language))) != NULL) {
  3913. add_assoc_string(return_value, "language", name, 1);
  3914. }
  3915. n = MBSTRG(current_detect_order_list_size);
  3916. entry = MBSTRG(current_detect_order_list);
  3917. if (n > 0) {
  3918. size_t i;
  3919. MAKE_STD_ZVAL(row2);
  3920. array_init(row2);
  3921. for (i = 0; i < n; i++) {
  3922. add_next_index_string(row2, (*entry)->name, 1);
  3923. entry++;
  3924. }
  3925. add_assoc_zval(return_value, "detect_order", row2);
  3926. }
  3927. if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
  3928. add_assoc_string(return_value, "substitute_character", "none", 1);
  3929. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG) {
  3930. add_assoc_string(return_value, "substitute_character", "long", 1);
  3931. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY) {
  3932. add_assoc_string(return_value, "substitute_character", "entity", 1);
  3933. } else {
  3934. add_assoc_long(return_value, "substitute_character", MBSTRG(current_filter_illegal_substchar));
  3935. }
  3936. if (MBSTRG(strict_detection)) {
  3937. add_assoc_string(return_value, "strict_detection", "On", 1);
  3938. } else {
  3939. add_assoc_string(return_value, "strict_detection", "Off", 1);
  3940. }
  3941. } else if (!strcasecmp("internal_encoding", typ)) {
  3942. if (MBSTRG(current_internal_encoding)) {
  3943. RETVAL_STRING((char *)MBSTRG(current_internal_encoding)->name, 1);
  3944. }
  3945. } else if (!strcasecmp("http_input", typ)) {
  3946. if (MBSTRG(http_input_identify)) {
  3947. RETVAL_STRING((char *)MBSTRG(http_input_identify)->name, 1);
  3948. }
  3949. } else if (!strcasecmp("http_output", typ)) {
  3950. if (MBSTRG(current_http_output_encoding)) {
  3951. RETVAL_STRING((char *)MBSTRG(current_http_output_encoding)->name, 1);
  3952. }
  3953. } else if (!strcasecmp("http_output_conv_mimetypes", typ)) {
  3954. if ((name = (char *)zend_ini_string("mbstring.http_output_conv_mimetypes", sizeof("mbstring.http_output_conv_mimetypes"), 0)) != NULL) {
  3955. RETVAL_STRING(name, 1);
  3956. }
  3957. } else if (!strcasecmp("func_overload", typ)) {
  3958. RETVAL_LONG(MBSTRG(func_overload));
  3959. } else if (!strcasecmp("func_overload_list", typ)) {
  3960. if (MBSTRG(func_overload)){
  3961. over_func = &(mb_ovld[0]);
  3962. array_init(return_value);
  3963. while (over_func->type > 0) {
  3964. if ((MBSTRG(func_overload) & over_func->type) == over_func->type ) {
  3965. add_assoc_string(return_value, over_func->orig_func, over_func->ovld_func, 1);
  3966. }
  3967. over_func++;
  3968. }
  3969. } else {
  3970. RETVAL_STRING("no overload", 1);
  3971. }
  3972. } else if (!strcasecmp("mail_charset", typ)) {
  3973. if (lang != NULL && (name = (char *)mbfl_no_encoding2name(lang->mail_charset)) != NULL) {
  3974. RETVAL_STRING(name, 1);
  3975. }
  3976. } else if (!strcasecmp("mail_header_encoding", typ)) {
  3977. if (lang != NULL && (name = (char *)mbfl_no_encoding2name(lang->mail_header_encoding)) != NULL) {
  3978. RETVAL_STRING(name, 1);
  3979. }
  3980. } else if (!strcasecmp("mail_body_encoding", typ)) {
  3981. if (lang != NULL && (name = (char *)mbfl_no_encoding2name(lang->mail_body_encoding)) != NULL) {
  3982. RETVAL_STRING(name, 1);
  3983. }
  3984. } else if (!strcasecmp("illegal_chars", typ)) {
  3985. RETVAL_LONG(MBSTRG(illegalchars));
  3986. } else if (!strcasecmp("encoding_translation", typ)) {
  3987. if (MBSTRG(encoding_translation)) {
  3988. RETVAL_STRING("On", 1);
  3989. } else {
  3990. RETVAL_STRING("Off", 1);
  3991. }
  3992. } else if (!strcasecmp("language", typ)) {
  3993. if ((name = (char *)mbfl_no_language2name(MBSTRG(language))) != NULL) {
  3994. RETVAL_STRING(name, 1);
  3995. }
  3996. } else if (!strcasecmp("detect_order", typ)) {
  3997. n = MBSTRG(current_detect_order_list_size);
  3998. entry = MBSTRG(current_detect_order_list);
  3999. if (n > 0) {
  4000. size_t i;
  4001. array_init(return_value);
  4002. for (i = 0; i < n; i++) {
  4003. add_next_index_string(return_value, (*entry)->name, 1);
  4004. entry++;
  4005. }
  4006. }
  4007. } else if (!strcasecmp("substitute_character", typ)) {
  4008. if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
  4009. RETVAL_STRING("none", 1);
  4010. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG) {
  4011. RETVAL_STRING("long", 1);
  4012. } else if (MBSTRG(current_filter_illegal_mode) == MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY) {
  4013. RETVAL_STRING("entity", 1);
  4014. } else {
  4015. RETVAL_LONG(MBSTRG(current_filter_illegal_substchar));
  4016. }
  4017. } else if (!strcasecmp("strict_detection", typ)) {
  4018. if (MBSTRG(strict_detection)) {
  4019. RETVAL_STRING("On", 1);
  4020. } else {
  4021. RETVAL_STRING("Off", 1);
  4022. }
  4023. } else {
  4024. RETURN_FALSE;
  4025. }
  4026. }
  4027. /* }}} */
  4028. /* {{{ proto bool mb_check_encoding([string var[, string encoding]])
  4029. Check if the string is valid for the specified encoding */
  4030. PHP_FUNCTION(mb_check_encoding)
  4031. {
  4032. char *var = NULL;
  4033. int var_len;
  4034. char *enc = NULL;
  4035. int enc_len;
  4036. mbfl_buffer_converter *convd;
  4037. const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
  4038. mbfl_string string, result, *ret = NULL;
  4039. long illegalchars = 0;
  4040. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &var, &var_len, &enc, &enc_len) == FAILURE) {
  4041. RETURN_FALSE;
  4042. }
  4043. if (var == NULL) {
  4044. RETURN_BOOL(MBSTRG(illegalchars) == 0);
  4045. }
  4046. if (enc != NULL) {
  4047. encoding = mbfl_name2encoding(enc);
  4048. if (!encoding || encoding == &mbfl_encoding_pass) {
  4049. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid encoding \"%s\"", enc);
  4050. RETURN_FALSE;
  4051. }
  4052. }
  4053. convd = mbfl_buffer_converter_new2(encoding, encoding, 0);
  4054. if (convd == NULL) {
  4055. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create converter");
  4056. RETURN_FALSE;
  4057. }
  4058. mbfl_buffer_converter_illegal_mode(convd, MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE);
  4059. mbfl_buffer_converter_illegal_substchar(convd, 0);
  4060. /* initialize string */
  4061. mbfl_string_init_set(&string, mbfl_no_language_neutral, encoding->no_encoding);
  4062. mbfl_string_init(&result);
  4063. string.val = (unsigned char *)var;
  4064. string.len = var_len;
  4065. ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
  4066. illegalchars = mbfl_buffer_illegalchars(convd);
  4067. mbfl_buffer_converter_delete(convd);
  4068. RETVAL_FALSE;
  4069. if (ret != NULL) {
  4070. if (illegalchars == 0 && string.len == result.len && memcmp(string.val, result.val, string.len) == 0) {
  4071. RETVAL_TRUE;
  4072. }
  4073. mbfl_string_clear(&result);
  4074. }
  4075. }
  4076. /* }}} */
  4077. /* {{{ php_mb_populate_current_detect_order_list */
  4078. static void php_mb_populate_current_detect_order_list(TSRMLS_D)
  4079. {
  4080. const mbfl_encoding **entry = 0;
  4081. size_t nentries;
  4082. if (MBSTRG(current_detect_order_list)) {
  4083. return;
  4084. }
  4085. if (MBSTRG(detect_order_list) && MBSTRG(detect_order_list_size)) {
  4086. nentries = MBSTRG(detect_order_list_size);
  4087. entry = (const mbfl_encoding **)safe_emalloc(nentries, sizeof(mbfl_encoding*), 0);
  4088. memcpy(entry, MBSTRG(detect_order_list), sizeof(mbfl_encoding*) * nentries);
  4089. } else {
  4090. const enum mbfl_no_encoding *src = MBSTRG(default_detect_order_list);
  4091. size_t i;
  4092. nentries = MBSTRG(default_detect_order_list_size);
  4093. entry = (const mbfl_encoding **)safe_emalloc(nentries, sizeof(mbfl_encoding*), 0);
  4094. for (i = 0; i < nentries; i++) {
  4095. entry[i] = mbfl_no2encoding(src[i]);
  4096. }
  4097. }
  4098. MBSTRG(current_detect_order_list) = entry;
  4099. MBSTRG(current_detect_order_list_size) = nentries;
  4100. }
  4101. /* {{{ static int php_mb_encoding_translation() */
  4102. static int php_mb_encoding_translation(TSRMLS_D)
  4103. {
  4104. return MBSTRG(encoding_translation);
  4105. }
  4106. /* }}} */
  4107. /* {{{ MBSTRING_API size_t php_mb_mbchar_bytes_ex() */
  4108. MBSTRING_API size_t php_mb_mbchar_bytes_ex(const char *s, const mbfl_encoding *enc)
  4109. {
  4110. if (enc != NULL) {
  4111. if (enc->flag & MBFL_ENCTYPE_MBCS) {
  4112. if (enc->mblen_table != NULL) {
  4113. if (s != NULL) return enc->mblen_table[*(unsigned char *)s];
  4114. }
  4115. } else if (enc->flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE)) {
  4116. return 2;
  4117. } else if (enc->flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) {
  4118. return 4;
  4119. }
  4120. }
  4121. return 1;
  4122. }
  4123. /* }}} */
  4124. /* {{{ MBSTRING_API size_t php_mb_mbchar_bytes() */
  4125. MBSTRING_API size_t php_mb_mbchar_bytes(const char *s TSRMLS_DC)
  4126. {
  4127. return php_mb_mbchar_bytes_ex(s, MBSTRG(internal_encoding));
  4128. }
  4129. /* }}} */
  4130. /* {{{ MBSTRING_API char *php_mb_safe_strrchr_ex() */
  4131. MBSTRING_API char *php_mb_safe_strrchr_ex(const char *s, unsigned int c, size_t nbytes, const mbfl_encoding *enc)
  4132. {
  4133. register const char *p = s;
  4134. char *last=NULL;
  4135. if (nbytes == (size_t)-1) {
  4136. size_t nb = 0;
  4137. while (*p != '\0') {
  4138. if (nb == 0) {
  4139. if ((unsigned char)*p == (unsigned char)c) {
  4140. last = (char *)p;
  4141. }
  4142. nb = php_mb_mbchar_bytes_ex(p, enc);
  4143. if (nb == 0) {
  4144. return NULL; /* something is going wrong! */
  4145. }
  4146. }
  4147. --nb;
  4148. ++p;
  4149. }
  4150. } else {
  4151. register size_t bcnt = nbytes;
  4152. register size_t nbytes_char;
  4153. while (bcnt > 0) {
  4154. if ((unsigned char)*p == (unsigned char)c) {
  4155. last = (char *)p;
  4156. }
  4157. nbytes_char = php_mb_mbchar_bytes_ex(p, enc);
  4158. if (bcnt < nbytes_char) {
  4159. return NULL;
  4160. }
  4161. p += nbytes_char;
  4162. bcnt -= nbytes_char;
  4163. }
  4164. }
  4165. return last;
  4166. }
  4167. /* }}} */
  4168. /* {{{ MBSTRING_API char *php_mb_safe_strrchr() */
  4169. MBSTRING_API char *php_mb_safe_strrchr(const char *s, unsigned int c, size_t nbytes TSRMLS_DC)
  4170. {
  4171. return php_mb_safe_strrchr_ex(s, c, nbytes, MBSTRG(internal_encoding));
  4172. }
  4173. /* }}} */
  4174. /* {{{ MBSTRING_API int php_mb_stripos()
  4175. */
  4176. MBSTRING_API int php_mb_stripos(int mode, const char *old_haystack, unsigned int old_haystack_len, const char *old_needle, unsigned int old_needle_len, long offset, const char *from_encoding TSRMLS_DC)
  4177. {
  4178. int n;
  4179. mbfl_string haystack, needle;
  4180. n = -1;
  4181. mbfl_string_init(&haystack);
  4182. mbfl_string_init(&needle);
  4183. haystack.no_language = MBSTRG(language);
  4184. haystack.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  4185. needle.no_language = MBSTRG(language);
  4186. needle.no_encoding = MBSTRG(current_internal_encoding)->no_encoding;
  4187. do {
  4188. size_t len = 0;
  4189. haystack.val = (unsigned char *)php_unicode_convert_case(PHP_UNICODE_CASE_UPPER, (char *)old_haystack, old_haystack_len, &len, from_encoding TSRMLS_CC);
  4190. haystack.len = len;
  4191. if (!haystack.val) {
  4192. break;
  4193. }
  4194. if (haystack.len <= 0) {
  4195. break;
  4196. }
  4197. needle.val = (unsigned char *)php_unicode_convert_case(PHP_UNICODE_CASE_UPPER, (char *)old_needle, old_needle_len, &len, from_encoding TSRMLS_CC);
  4198. needle.len = len;
  4199. if (!needle.val) {
  4200. break;
  4201. }
  4202. if (needle.len <= 0) {
  4203. break;
  4204. }
  4205. haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(from_encoding);
  4206. if (haystack.no_encoding == mbfl_no_encoding_invalid) {
  4207. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", from_encoding);
  4208. break;
  4209. }
  4210. {
  4211. int haystack_char_len = mbfl_strlen(&haystack);
  4212. if (mode) {
  4213. if ((offset > 0 && offset > haystack_char_len) ||
  4214. (offset < 0 && -offset > haystack_char_len)) {
  4215. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is greater than the length of haystack string");
  4216. break;
  4217. }
  4218. } else {
  4219. if (offset < 0 || offset > haystack_char_len) {
  4220. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string");
  4221. break;
  4222. }
  4223. }
  4224. }
  4225. n = mbfl_strpos(&haystack, &needle, offset, mode);
  4226. } while(0);
  4227. if (haystack.val) {
  4228. efree(haystack.val);
  4229. }
  4230. if (needle.val) {
  4231. efree(needle.val);
  4232. }
  4233. return n;
  4234. }
  4235. /* }}} */
  4236. static void php_mb_gpc_get_detect_order(const zend_encoding ***list, size_t *list_size TSRMLS_DC) /* {{{ */
  4237. {
  4238. *list = (const zend_encoding **)MBSTRG(http_input_list);
  4239. *list_size = MBSTRG(http_input_list_size);
  4240. }
  4241. /* }}} */
  4242. static void php_mb_gpc_set_input_encoding(const zend_encoding *encoding TSRMLS_DC) /* {{{ */
  4243. {
  4244. MBSTRG(http_input_identify) = (const mbfl_encoding*)encoding;
  4245. }
  4246. /* }}} */
  4247. #endif /* HAVE_MBSTRING */
  4248. /*
  4249. * Local variables:
  4250. * tab-width: 4
  4251. * c-basic-offset: 4
  4252. * End:
  4253. * vim600: fdm=marker
  4254. * vim: noet sw=4 ts=4
  4255. */