iconv.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rui Hirokawa <rui_hirokawa@ybb.ne.jp> |
  16. | Stig Bakken <ssb@php.net> |
  17. | Moriyoshi Koizumi <moriyoshi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "php.h"
  25. #include "php_globals.h"
  26. #include "ext/standard/info.h"
  27. #include "main/php_output.h"
  28. #include "SAPI.h"
  29. #include "php_ini.h"
  30. #ifdef HAVE_STDLIB_H
  31. # include <stdlib.h>
  32. #endif
  33. #include <errno.h>
  34. #include "php_iconv.h"
  35. #ifdef HAVE_ICONV
  36. #ifdef PHP_ICONV_H_PATH
  37. #include PHP_ICONV_H_PATH
  38. #else
  39. #include <iconv.h>
  40. #endif
  41. #ifdef HAVE_GLIBC_ICONV
  42. #include <gnu/libc-version.h>
  43. #endif
  44. #ifdef HAVE_LIBICONV
  45. #undef iconv
  46. #endif
  47. #include "ext/standard/php_smart_str.h"
  48. #include "ext/standard/base64.h"
  49. #include "ext/standard/quot_print.h"
  50. #define _php_iconv_memequal(a, b, c) \
  51. ((c) == sizeof(unsigned long) ? *((unsigned long *)(a)) == *((unsigned long *)(b)) : ((c) == sizeof(unsigned int) ? *((unsigned int *)(a)) == *((unsigned int *)(b)) : memcmp(a, b, c) == 0))
  52. /* {{{ arginfo */
  53. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_strlen, 0, 0, 1)
  54. ZEND_ARG_INFO(0, str)
  55. ZEND_ARG_INFO(0, charset)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_substr, 0, 0, 2)
  58. ZEND_ARG_INFO(0, str)
  59. ZEND_ARG_INFO(0, offset)
  60. ZEND_ARG_INFO(0, length)
  61. ZEND_ARG_INFO(0, charset)
  62. ZEND_END_ARG_INFO()
  63. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_strpos, 0, 0, 2)
  64. ZEND_ARG_INFO(0, haystack)
  65. ZEND_ARG_INFO(0, needle)
  66. ZEND_ARG_INFO(0, offset)
  67. ZEND_ARG_INFO(0, charset)
  68. ZEND_END_ARG_INFO()
  69. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_strrpos, 0, 0, 2)
  70. ZEND_ARG_INFO(0, haystack)
  71. ZEND_ARG_INFO(0, needle)
  72. ZEND_ARG_INFO(0, charset)
  73. ZEND_END_ARG_INFO()
  74. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_mime_encode, 0, 0, 2)
  75. ZEND_ARG_INFO(0, field_name)
  76. ZEND_ARG_INFO(0, field_value)
  77. ZEND_ARG_INFO(0, preference) /* ZEND_ARG_ARRAY_INFO(0, preference, 1) */
  78. ZEND_END_ARG_INFO()
  79. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_mime_decode, 0, 0, 1)
  80. ZEND_ARG_INFO(0, encoded_string)
  81. ZEND_ARG_INFO(0, mode)
  82. ZEND_ARG_INFO(0, charset)
  83. ZEND_END_ARG_INFO()
  84. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_mime_decode_headers, 0, 0, 1)
  85. ZEND_ARG_INFO(0, headers)
  86. ZEND_ARG_INFO(0, mode)
  87. ZEND_ARG_INFO(0, charset)
  88. ZEND_END_ARG_INFO()
  89. ZEND_BEGIN_ARG_INFO(arginfo_iconv, 0)
  90. ZEND_ARG_INFO(0, in_charset)
  91. ZEND_ARG_INFO(0, out_charset)
  92. ZEND_ARG_INFO(0, str)
  93. ZEND_END_ARG_INFO()
  94. ZEND_BEGIN_ARG_INFO(arginfo_iconv_set_encoding, 0)
  95. ZEND_ARG_INFO(0, type)
  96. ZEND_ARG_INFO(0, charset)
  97. ZEND_END_ARG_INFO()
  98. ZEND_BEGIN_ARG_INFO_EX(arginfo_iconv_get_encoding, 0, 0, 0)
  99. ZEND_ARG_INFO(0, type)
  100. ZEND_END_ARG_INFO()
  101. /* }}} */
  102. /* {{{ iconv_functions[]
  103. */
  104. const zend_function_entry iconv_functions[] = {
  105. PHP_RAW_NAMED_FE(iconv,php_if_iconv, arginfo_iconv)
  106. PHP_FE(iconv_get_encoding, arginfo_iconv_get_encoding)
  107. PHP_FE(iconv_set_encoding, arginfo_iconv_set_encoding)
  108. PHP_FE(iconv_strlen, arginfo_iconv_strlen)
  109. PHP_FE(iconv_substr, arginfo_iconv_substr)
  110. PHP_FE(iconv_strpos, arginfo_iconv_strpos)
  111. PHP_FE(iconv_strrpos, arginfo_iconv_strrpos)
  112. PHP_FE(iconv_mime_encode, arginfo_iconv_mime_encode)
  113. PHP_FE(iconv_mime_decode, arginfo_iconv_mime_decode)
  114. PHP_FE(iconv_mime_decode_headers, arginfo_iconv_mime_decode_headers)
  115. PHP_FE_END
  116. };
  117. /* }}} */
  118. ZEND_DECLARE_MODULE_GLOBALS(iconv)
  119. static PHP_GINIT_FUNCTION(iconv);
  120. /* {{{ iconv_module_entry
  121. */
  122. zend_module_entry iconv_module_entry = {
  123. STANDARD_MODULE_HEADER,
  124. "iconv",
  125. iconv_functions,
  126. PHP_MINIT(miconv),
  127. PHP_MSHUTDOWN(miconv),
  128. NULL,
  129. NULL,
  130. PHP_MINFO(miconv),
  131. NO_VERSION_YET,
  132. PHP_MODULE_GLOBALS(iconv),
  133. PHP_GINIT(iconv),
  134. NULL,
  135. NULL,
  136. STANDARD_MODULE_PROPERTIES_EX
  137. };
  138. /* }}} */
  139. #ifdef COMPILE_DL_ICONV
  140. ZEND_GET_MODULE(iconv)
  141. #endif
  142. /* {{{ PHP_GINIT_FUNCTION */
  143. static PHP_GINIT_FUNCTION(iconv)
  144. {
  145. iconv_globals->input_encoding = NULL;
  146. iconv_globals->output_encoding = NULL;
  147. iconv_globals->internal_encoding = NULL;
  148. }
  149. /* }}} */
  150. #if defined(HAVE_LIBICONV) && defined(ICONV_ALIASED_LIBICONV)
  151. #define iconv libiconv
  152. #endif
  153. /* {{{ typedef enum php_iconv_enc_scheme_t */
  154. typedef enum _php_iconv_enc_scheme_t {
  155. PHP_ICONV_ENC_SCHEME_BASE64,
  156. PHP_ICONV_ENC_SCHEME_QPRINT
  157. } php_iconv_enc_scheme_t;
  158. /* }}} */
  159. #define PHP_ICONV_MIME_DECODE_STRICT (1<<0)
  160. #define PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR (1<<1)
  161. /* {{{ prototypes */
  162. static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l, iconv_t cd);
  163. static php_iconv_err_t _php_iconv_appendc(smart_str *d, const char c, iconv_t cd);
  164. static void _php_iconv_show_error(php_iconv_err_t err, const char *out_charset, const char *in_charset TSRMLS_DC);
  165. static php_iconv_err_t _php_iconv_strlen(unsigned int *pretval, const char *str, size_t nbytes, const char *enc);
  166. static php_iconv_err_t _php_iconv_substr(smart_str *pretval, const char *str, size_t nbytes, int offset, int len, const char *enc);
  167. static php_iconv_err_t _php_iconv_strpos(unsigned int *pretval, const char *haystk, size_t haystk_nbytes, const char *ndl, size_t ndl_nbytes, int offset, const char *enc);
  168. static php_iconv_err_t _php_iconv_mime_encode(smart_str *pretval, const char *fname, size_t fname_nbytes, const char *fval, size_t fval_nbytes, unsigned int max_line_len, const char *lfchars, php_iconv_enc_scheme_t enc_scheme, const char *out_charset, const char *enc);
  169. static php_iconv_err_t _php_iconv_mime_decode(smart_str *pretval, const char *str, size_t str_nbytes, const char *enc, const char **next_pos, int mode);
  170. static php_iconv_err_t php_iconv_stream_filter_register_factory(TSRMLS_D);
  171. static php_iconv_err_t php_iconv_stream_filter_unregister_factory(TSRMLS_D);
  172. static int php_iconv_output_conflict(const char *handler_name, size_t handler_name_len TSRMLS_DC);
  173. static php_output_handler *php_iconv_output_handler_init(const char *name, size_t name_len, size_t chunk_size, int flags TSRMLS_DC);
  174. static int php_iconv_output_handler(void **nothing, php_output_context *output_context);
  175. /* }}} */
  176. /* {{{ static globals */
  177. static char _generic_superset_name[] = ICONV_UCS4_ENCODING;
  178. #define GENERIC_SUPERSET_NAME _generic_superset_name
  179. #define GENERIC_SUPERSET_NBYTES 4
  180. /* }}} */
  181. static PHP_INI_MH(OnUpdateInputEncoding)
  182. {
  183. if (new_value_length >= ICONV_CSNMAXLEN) {
  184. return FAILURE;
  185. }
  186. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  187. php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.input_encoding is deprecated");
  188. }
  189. OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  190. return SUCCESS;
  191. }
  192. static PHP_INI_MH(OnUpdateOutputEncoding)
  193. {
  194. if(new_value_length >= ICONV_CSNMAXLEN) {
  195. return FAILURE;
  196. }
  197. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  198. php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.output_encoding is deprecated");
  199. }
  200. OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  201. return SUCCESS;
  202. }
  203. static PHP_INI_MH(OnUpdateInternalEncoding)
  204. {
  205. if(new_value_length >= ICONV_CSNMAXLEN) {
  206. return FAILURE;
  207. }
  208. if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
  209. php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.internal_encoding is deprecated");
  210. }
  211. OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  212. return SUCCESS;
  213. }
  214. /* {{{ PHP_INI
  215. */
  216. PHP_INI_BEGIN()
  217. STD_PHP_INI_ENTRY("iconv.input_encoding", "", PHP_INI_ALL, OnUpdateInputEncoding, input_encoding, zend_iconv_globals, iconv_globals)
  218. STD_PHP_INI_ENTRY("iconv.output_encoding", "", PHP_INI_ALL, OnUpdateOutputEncoding, output_encoding, zend_iconv_globals, iconv_globals)
  219. STD_PHP_INI_ENTRY("iconv.internal_encoding", "", PHP_INI_ALL, OnUpdateInternalEncoding, internal_encoding, zend_iconv_globals, iconv_globals)
  220. PHP_INI_END()
  221. /* }}} */
  222. /* {{{ PHP_MINIT_FUNCTION */
  223. PHP_MINIT_FUNCTION(miconv)
  224. {
  225. char *version = "unknown";
  226. REGISTER_INI_ENTRIES();
  227. #if HAVE_LIBICONV
  228. {
  229. static char buf[16];
  230. snprintf(buf, sizeof(buf), "%d.%d",
  231. ((_libiconv_version >> 8) & 0x0f), (_libiconv_version & 0x0f));
  232. version = buf;
  233. }
  234. #elif HAVE_GLIBC_ICONV
  235. version = (char *)gnu_get_libc_version();
  236. #elif defined(NETWARE)
  237. version = "OS built-in";
  238. #endif
  239. #ifdef PHP_ICONV_IMPL
  240. REGISTER_STRING_CONSTANT("ICONV_IMPL", PHP_ICONV_IMPL, CONST_CS | CONST_PERSISTENT);
  241. #elif HAVE_LIBICONV
  242. REGISTER_STRING_CONSTANT("ICONV_IMPL", "libiconv", CONST_CS | CONST_PERSISTENT);
  243. #elif defined(NETWARE)
  244. REGISTER_STRING_CONSTANT("ICONV_IMPL", "Novell", CONST_CS | CONST_PERSISTENT);
  245. #else
  246. REGISTER_STRING_CONSTANT("ICONV_IMPL", "unknown", CONST_CS | CONST_PERSISTENT);
  247. #endif
  248. REGISTER_STRING_CONSTANT("ICONV_VERSION", version, CONST_CS | CONST_PERSISTENT);
  249. REGISTER_LONG_CONSTANT("ICONV_MIME_DECODE_STRICT", PHP_ICONV_MIME_DECODE_STRICT, CONST_CS | CONST_PERSISTENT);
  250. REGISTER_LONG_CONSTANT("ICONV_MIME_DECODE_CONTINUE_ON_ERROR", PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR, CONST_CS | CONST_PERSISTENT);
  251. if (php_iconv_stream_filter_register_factory(TSRMLS_C) != PHP_ICONV_ERR_SUCCESS) {
  252. return FAILURE;
  253. }
  254. php_output_handler_alias_register(ZEND_STRL("ob_iconv_handler"), php_iconv_output_handler_init TSRMLS_CC);
  255. php_output_handler_conflict_register(ZEND_STRL("ob_iconv_handler"), php_iconv_output_conflict TSRMLS_CC);
  256. return SUCCESS;
  257. }
  258. /* }}} */
  259. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  260. PHP_MSHUTDOWN_FUNCTION(miconv)
  261. {
  262. php_iconv_stream_filter_unregister_factory(TSRMLS_C);
  263. UNREGISTER_INI_ENTRIES();
  264. return SUCCESS;
  265. }
  266. /* }}} */
  267. /* {{{ PHP_MINFO_FUNCTION */
  268. PHP_MINFO_FUNCTION(miconv)
  269. {
  270. zval iconv_impl, iconv_ver;
  271. zend_get_constant("ICONV_IMPL", sizeof("ICONV_IMPL")-1, &iconv_impl TSRMLS_CC);
  272. zend_get_constant("ICONV_VERSION", sizeof("ICONV_VERSION")-1, &iconv_ver TSRMLS_CC);
  273. php_info_print_table_start();
  274. php_info_print_table_row(2, "iconv support", "enabled");
  275. php_info_print_table_row(2, "iconv implementation", Z_STRVAL(iconv_impl));
  276. php_info_print_table_row(2, "iconv library version", Z_STRVAL(iconv_ver));
  277. php_info_print_table_end();
  278. DISPLAY_INI_ENTRIES();
  279. zval_dtor(&iconv_impl);
  280. zval_dtor(&iconv_ver);
  281. }
  282. /* }}} */
  283. static char *get_internal_encoding(TSRMLS_D) {
  284. if (ICONVG(internal_encoding) && ICONVG(internal_encoding)[0]) {
  285. return ICONVG(internal_encoding);
  286. } else if (PG(internal_encoding) && PG(internal_encoding)[0]) {
  287. return PG(internal_encoding);
  288. } else if (SG(default_charset)) {
  289. return SG(default_charset);
  290. }
  291. return "";
  292. }
  293. static char *get_input_encoding(TSRMLS_D) {
  294. if (ICONVG(input_encoding) && ICONVG(input_encoding)[0]) {
  295. return ICONVG(input_encoding);
  296. } else if (PG(input_encoding) && PG(input_encoding)[0]) {
  297. return PG(input_encoding);
  298. } else if (SG(default_charset)) {
  299. return SG(default_charset);
  300. }
  301. return "";
  302. }
  303. static char *get_output_encoding(TSRMLS_D) {
  304. if (ICONVG(output_encoding) && ICONVG(output_encoding)[0]) {
  305. return ICONVG(output_encoding);
  306. } else if (PG(output_encoding) && PG(output_encoding)[0]) {
  307. return PG(output_encoding);
  308. } else if (SG(default_charset)) {
  309. return SG(default_charset);
  310. }
  311. return "";
  312. }
  313. static int php_iconv_output_conflict(const char *handler_name, size_t handler_name_len TSRMLS_DC)
  314. {
  315. if (php_output_get_level(TSRMLS_C)) {
  316. if (php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("ob_iconv_handler") TSRMLS_CC)
  317. || php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("mb_output_handler") TSRMLS_CC)) {
  318. return FAILURE;
  319. }
  320. }
  321. return SUCCESS;
  322. }
  323. static php_output_handler *php_iconv_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags TSRMLS_DC)
  324. {
  325. return php_output_handler_create_internal(handler_name, handler_name_len, php_iconv_output_handler, chunk_size, flags TSRMLS_CC);
  326. }
  327. static int php_iconv_output_handler(void **nothing, php_output_context *output_context)
  328. {
  329. char *s, *content_type, *mimetype = NULL;
  330. int output_status, mimetype_len = 0;
  331. PHP_OUTPUT_TSRMLS(output_context);
  332. if (output_context->op & PHP_OUTPUT_HANDLER_START) {
  333. output_status = php_output_get_status(TSRMLS_C);
  334. if (output_status & PHP_OUTPUT_SENT) {
  335. return FAILURE;
  336. }
  337. if (SG(sapi_headers).mimetype && !strncasecmp(SG(sapi_headers).mimetype, "text/", 5)) {
  338. if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){
  339. mimetype = SG(sapi_headers).mimetype;
  340. } else {
  341. mimetype = SG(sapi_headers).mimetype;
  342. mimetype_len = s - SG(sapi_headers).mimetype;
  343. }
  344. } else if (SG(sapi_headers).send_default_content_type) {
  345. mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
  346. }
  347. if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) {
  348. int len;
  349. char *p = strstr(get_output_encoding(TSRMLS_C), "//");
  350. if (p) {
  351. len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%.*s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, (int)(p - get_output_encoding(TSRMLS_C)), get_output_encoding(TSRMLS_C));
  352. } else {
  353. len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, get_output_encoding(TSRMLS_C));
  354. }
  355. if (content_type && SUCCESS == sapi_add_header(content_type, len, 0)) {
  356. SG(sapi_headers).send_default_content_type = 0;
  357. php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC);
  358. }
  359. }
  360. }
  361. if (output_context->in.used) {
  362. output_context->out.free = 1;
  363. _php_iconv_show_error(php_iconv_string(output_context->in.data, output_context->in.used, &output_context->out.data, &output_context->out.used, get_output_encoding(TSRMLS_C), get_internal_encoding(TSRMLS_C)), get_output_encoding(TSRMLS_C), get_internal_encoding(TSRMLS_C) TSRMLS_CC);
  364. }
  365. return SUCCESS;
  366. }
  367. /* {{{ _php_iconv_appendl() */
  368. static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l, iconv_t cd)
  369. {
  370. const char *in_p = s;
  371. size_t in_left = l;
  372. char *out_p;
  373. size_t out_left = 0;
  374. size_t buf_growth = 128;
  375. #if !ICONV_SUPPORTS_ERRNO
  376. size_t prev_in_left = in_left;
  377. #endif
  378. if (in_p != NULL) {
  379. while (in_left > 0) {
  380. out_left = buf_growth - out_left;
  381. {
  382. size_t newlen;
  383. smart_str_alloc((d), out_left, 0);
  384. }
  385. out_p = (d)->c + (d)->len;
  386. if (iconv(cd, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  387. #if ICONV_SUPPORTS_ERRNO
  388. switch (errno) {
  389. case EINVAL:
  390. return PHP_ICONV_ERR_ILLEGAL_CHAR;
  391. case EILSEQ:
  392. return PHP_ICONV_ERR_ILLEGAL_SEQ;
  393. case E2BIG:
  394. break;
  395. default:
  396. return PHP_ICONV_ERR_UNKNOWN;
  397. }
  398. #else
  399. if (prev_in_left == in_left) {
  400. return PHP_ICONV_ERR_UNKNOWN;
  401. }
  402. #endif
  403. }
  404. #if !ICONV_SUPPORTS_ERRNO
  405. prev_in_left = in_left;
  406. #endif
  407. (d)->len += (buf_growth - out_left);
  408. buf_growth <<= 1;
  409. }
  410. } else {
  411. for (;;) {
  412. out_left = buf_growth - out_left;
  413. {
  414. size_t newlen;
  415. smart_str_alloc((d), out_left, 0);
  416. }
  417. out_p = (d)->c + (d)->len;
  418. if (iconv(cd, NULL, NULL, (char **) &out_p, &out_left) == (size_t)0) {
  419. (d)->len += (buf_growth - out_left);
  420. break;
  421. } else {
  422. #if ICONV_SUPPORTS_ERRNO
  423. if (errno != E2BIG) {
  424. return PHP_ICONV_ERR_UNKNOWN;
  425. }
  426. #else
  427. if (out_left != 0) {
  428. return PHP_ICONV_ERR_UNKNOWN;
  429. }
  430. #endif
  431. }
  432. (d)->len += (buf_growth - out_left);
  433. buf_growth <<= 1;
  434. }
  435. }
  436. return PHP_ICONV_ERR_SUCCESS;
  437. }
  438. /* }}} */
  439. /* {{{ _php_iconv_appendc() */
  440. static php_iconv_err_t _php_iconv_appendc(smart_str *d, const char c, iconv_t cd)
  441. {
  442. return _php_iconv_appendl(d, &c, 1, cd);
  443. }
  444. /* }}} */
  445. /* {{{ */
  446. #if ICONV_BROKEN_IGNORE
  447. static int _php_check_ignore(const char *charset)
  448. {
  449. size_t clen = strlen(charset);
  450. if (clen >= 9 && strcmp("//IGNORE", charset+clen-8) == 0) {
  451. return 1;
  452. }
  453. if (clen >= 19 && strcmp("//IGNORE//TRANSLIT", charset+clen-18) == 0) {
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. #else
  459. #define _php_check_ignore(x) (0)
  460. #endif
  461. /* }}} */
  462. /* {{{ php_iconv_string()
  463. */
  464. PHP_ICONV_API php_iconv_err_t php_iconv_string(const char *in_p, size_t in_len,
  465. char **out, size_t *out_len,
  466. const char *out_charset, const char *in_charset)
  467. {
  468. #if !ICONV_SUPPORTS_ERRNO
  469. size_t in_size, out_size, out_left;
  470. char *out_buffer, *out_p;
  471. iconv_t cd;
  472. size_t result;
  473. *out = NULL;
  474. *out_len = 0;
  475. /*
  476. This is not the right way to get output size...
  477. This is not space efficient for large text.
  478. This is also problem for encoding like UTF-7/UTF-8/ISO-2022 which
  479. a single char can be more than 4 bytes.
  480. I added 15 extra bytes for safety. <yohgaki@php.net>
  481. */
  482. out_size = in_len * sizeof(int) + 15;
  483. out_left = out_size;
  484. in_size = in_len;
  485. cd = iconv_open(out_charset, in_charset);
  486. if (cd == (iconv_t)(-1)) {
  487. return PHP_ICONV_ERR_UNKNOWN;
  488. }
  489. out_buffer = (char *) emalloc(out_size + 1);
  490. out_p = out_buffer;
  491. #ifdef NETWARE
  492. result = iconv(cd, (char **) &in_p, &in_size, (char **)
  493. #else
  494. result = iconv(cd, (const char **) &in_p, &in_size, (char **)
  495. #endif
  496. &out_p, &out_left);
  497. if (result == (size_t)(-1)) {
  498. efree(out_buffer);
  499. return PHP_ICONV_ERR_UNKNOWN;
  500. }
  501. if (out_left < 8) {
  502. size_t pos = out_p - out_buffer;
  503. out_buffer = (char *) safe_erealloc(out_buffer, out_size, 1, 8);
  504. out_p = out_buffer+pos;
  505. out_size += 7;
  506. out_left += 7;
  507. }
  508. /* flush the shift-out sequences */
  509. result = iconv(cd, NULL, NULL, &out_p, &out_left);
  510. if (result == (size_t)(-1)) {
  511. efree(out_buffer);
  512. return PHP_ICONV_ERR_UNKNOWN;
  513. }
  514. *out_len = out_size - out_left;
  515. out_buffer[*out_len] = '\0';
  516. *out = out_buffer;
  517. iconv_close(cd);
  518. return PHP_ICONV_ERR_SUCCESS;
  519. #else
  520. /*
  521. iconv supports errno. Handle it better way.
  522. */
  523. iconv_t cd;
  524. size_t in_left, out_size, out_left;
  525. char *out_p, *out_buf, *tmp_buf;
  526. size_t bsz, result = 0;
  527. php_iconv_err_t retval = PHP_ICONV_ERR_SUCCESS;
  528. int ignore_ilseq = _php_check_ignore(out_charset);
  529. *out = NULL;
  530. *out_len = 0;
  531. cd = iconv_open(out_charset, in_charset);
  532. if (cd == (iconv_t)(-1)) {
  533. if (errno == EINVAL) {
  534. return PHP_ICONV_ERR_WRONG_CHARSET;
  535. } else {
  536. return PHP_ICONV_ERR_CONVERTER;
  537. }
  538. }
  539. in_left= in_len;
  540. out_left = in_len + 32; /* Avoid realloc() most cases */
  541. out_size = 0;
  542. bsz = out_left;
  543. out_buf = (char *) emalloc(bsz+1);
  544. out_p = out_buf;
  545. while (in_left > 0) {
  546. result = iconv(cd, (char **) &in_p, &in_left, (char **) &out_p, &out_left);
  547. out_size = bsz - out_left;
  548. if (result == (size_t)(-1)) {
  549. if (ignore_ilseq && errno == EILSEQ) {
  550. if (in_left <= 1) {
  551. result = 0;
  552. } else {
  553. errno = 0;
  554. in_p++;
  555. in_left--;
  556. continue;
  557. }
  558. }
  559. if (errno == E2BIG && in_left > 0) {
  560. /* converted string is longer than out buffer */
  561. bsz += in_len;
  562. tmp_buf = (char*) erealloc(out_buf, bsz+1);
  563. out_p = out_buf = tmp_buf;
  564. out_p += out_size;
  565. out_left = bsz - out_size;
  566. continue;
  567. }
  568. }
  569. break;
  570. }
  571. if (result != (size_t)(-1)) {
  572. /* flush the shift-out sequences */
  573. for (;;) {
  574. result = iconv(cd, NULL, NULL, (char **) &out_p, &out_left);
  575. out_size = bsz - out_left;
  576. if (result != (size_t)(-1)) {
  577. break;
  578. }
  579. if (errno == E2BIG) {
  580. bsz += 16;
  581. tmp_buf = (char *) erealloc(out_buf, bsz);
  582. out_p = out_buf = tmp_buf;
  583. out_p += out_size;
  584. out_left = bsz - out_size;
  585. } else {
  586. break;
  587. }
  588. }
  589. }
  590. iconv_close(cd);
  591. if (result == (size_t)(-1)) {
  592. switch (errno) {
  593. case EINVAL:
  594. retval = PHP_ICONV_ERR_ILLEGAL_CHAR;
  595. break;
  596. case EILSEQ:
  597. retval = PHP_ICONV_ERR_ILLEGAL_SEQ;
  598. break;
  599. case E2BIG:
  600. /* should not happen */
  601. retval = PHP_ICONV_ERR_TOO_BIG;
  602. break;
  603. default:
  604. /* other error */
  605. retval = PHP_ICONV_ERR_UNKNOWN;
  606. efree(out_buf);
  607. return PHP_ICONV_ERR_UNKNOWN;
  608. }
  609. }
  610. *out_p = '\0';
  611. *out = out_buf;
  612. *out_len = out_size;
  613. return retval;
  614. #endif
  615. }
  616. /* }}} */
  617. /* {{{ _php_iconv_strlen() */
  618. static php_iconv_err_t _php_iconv_strlen(unsigned int *pretval, const char *str, size_t nbytes, const char *enc)
  619. {
  620. char buf[GENERIC_SUPERSET_NBYTES*2];
  621. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  622. iconv_t cd;
  623. const char *in_p;
  624. size_t in_left;
  625. char *out_p;
  626. size_t out_left;
  627. unsigned int cnt;
  628. *pretval = (unsigned int)-1;
  629. cd = iconv_open(GENERIC_SUPERSET_NAME, enc);
  630. if (cd == (iconv_t)(-1)) {
  631. #if ICONV_SUPPORTS_ERRNO
  632. if (errno == EINVAL) {
  633. return PHP_ICONV_ERR_WRONG_CHARSET;
  634. } else {
  635. return PHP_ICONV_ERR_CONVERTER;
  636. }
  637. #else
  638. return PHP_ICONV_ERR_UNKNOWN;
  639. #endif
  640. }
  641. errno = out_left = 0;
  642. for (in_p = str, in_left = nbytes, cnt = 0; in_left > 0; cnt+=2) {
  643. size_t prev_in_left;
  644. out_p = buf;
  645. out_left = sizeof(buf);
  646. prev_in_left = in_left;
  647. if (iconv(cd, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  648. if (prev_in_left == in_left) {
  649. break;
  650. }
  651. }
  652. }
  653. if (out_left > 0) {
  654. cnt -= out_left / GENERIC_SUPERSET_NBYTES;
  655. }
  656. #if ICONV_SUPPORTS_ERRNO
  657. switch (errno) {
  658. case EINVAL:
  659. err = PHP_ICONV_ERR_ILLEGAL_CHAR;
  660. break;
  661. case EILSEQ:
  662. err = PHP_ICONV_ERR_ILLEGAL_SEQ;
  663. break;
  664. case E2BIG:
  665. case 0:
  666. *pretval = cnt;
  667. break;
  668. default:
  669. err = PHP_ICONV_ERR_UNKNOWN;
  670. break;
  671. }
  672. #else
  673. *pretval = cnt;
  674. #endif
  675. iconv_close(cd);
  676. return err;
  677. }
  678. /* }}} */
  679. /* {{{ _php_iconv_substr() */
  680. static php_iconv_err_t _php_iconv_substr(smart_str *pretval,
  681. const char *str, size_t nbytes, int offset, int len, const char *enc)
  682. {
  683. char buf[GENERIC_SUPERSET_NBYTES];
  684. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  685. iconv_t cd1, cd2;
  686. const char *in_p;
  687. size_t in_left;
  688. char *out_p;
  689. size_t out_left;
  690. unsigned int cnt;
  691. int total_len;
  692. err = _php_iconv_strlen(&total_len, str, nbytes, enc);
  693. if (err != PHP_ICONV_ERR_SUCCESS) {
  694. return err;
  695. }
  696. if (len < 0) {
  697. if ((len += (total_len - offset)) < 0) {
  698. return PHP_ICONV_ERR_SUCCESS;
  699. }
  700. }
  701. if (offset < 0) {
  702. if ((offset += total_len) < 0) {
  703. return PHP_ICONV_ERR_SUCCESS;
  704. }
  705. }
  706. if(len > total_len) {
  707. len = total_len;
  708. }
  709. if (offset >= total_len) {
  710. return PHP_ICONV_ERR_SUCCESS;
  711. }
  712. if ((offset + len) > total_len ) {
  713. /* trying to compute the length */
  714. len = total_len - offset;
  715. }
  716. if (len == 0) {
  717. smart_str_appendl(pretval, "", 0);
  718. smart_str_0(pretval);
  719. return PHP_ICONV_ERR_SUCCESS;
  720. }
  721. cd1 = iconv_open(GENERIC_SUPERSET_NAME, enc);
  722. if (cd1 == (iconv_t)(-1)) {
  723. #if ICONV_SUPPORTS_ERRNO
  724. if (errno == EINVAL) {
  725. return PHP_ICONV_ERR_WRONG_CHARSET;
  726. } else {
  727. return PHP_ICONV_ERR_CONVERTER;
  728. }
  729. #else
  730. return PHP_ICONV_ERR_UNKNOWN;
  731. #endif
  732. }
  733. cd2 = (iconv_t)NULL;
  734. errno = 0;
  735. for (in_p = str, in_left = nbytes, cnt = 0; in_left > 0 && len > 0; ++cnt) {
  736. size_t prev_in_left;
  737. out_p = buf;
  738. out_left = sizeof(buf);
  739. prev_in_left = in_left;
  740. if (iconv(cd1, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  741. if (prev_in_left == in_left) {
  742. break;
  743. }
  744. }
  745. if (cnt >= (unsigned int)offset) {
  746. if (cd2 == (iconv_t)NULL) {
  747. cd2 = iconv_open(enc, GENERIC_SUPERSET_NAME);
  748. if (cd2 == (iconv_t)(-1)) {
  749. cd2 = (iconv_t)NULL;
  750. #if ICONV_SUPPORTS_ERRNO
  751. if (errno == EINVAL) {
  752. err = PHP_ICONV_ERR_WRONG_CHARSET;
  753. } else {
  754. err = PHP_ICONV_ERR_CONVERTER;
  755. }
  756. #else
  757. err = PHP_ICONV_ERR_UNKNOWN;
  758. #endif
  759. break;
  760. }
  761. }
  762. if (_php_iconv_appendl(pretval, buf, sizeof(buf), cd2) != PHP_ICONV_ERR_SUCCESS) {
  763. break;
  764. }
  765. --len;
  766. }
  767. }
  768. #if ICONV_SUPPORTS_ERRNO
  769. switch (errno) {
  770. case EINVAL:
  771. err = PHP_ICONV_ERR_ILLEGAL_CHAR;
  772. break;
  773. case EILSEQ:
  774. err = PHP_ICONV_ERR_ILLEGAL_SEQ;
  775. break;
  776. case E2BIG:
  777. break;
  778. }
  779. #endif
  780. if (err == PHP_ICONV_ERR_SUCCESS) {
  781. if (cd2 != (iconv_t)NULL) {
  782. _php_iconv_appendl(pretval, NULL, 0, cd2);
  783. }
  784. smart_str_0(pretval);
  785. }
  786. if (cd1 != (iconv_t)NULL) {
  787. iconv_close(cd1);
  788. }
  789. if (cd2 != (iconv_t)NULL) {
  790. iconv_close(cd2);
  791. }
  792. return err;
  793. }
  794. /* }}} */
  795. /* {{{ _php_iconv_strpos() */
  796. static php_iconv_err_t _php_iconv_strpos(unsigned int *pretval,
  797. const char *haystk, size_t haystk_nbytes,
  798. const char *ndl, size_t ndl_nbytes,
  799. int offset, const char *enc)
  800. {
  801. char buf[GENERIC_SUPERSET_NBYTES];
  802. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  803. iconv_t cd;
  804. const char *in_p;
  805. size_t in_left;
  806. char *out_p;
  807. size_t out_left;
  808. unsigned int cnt;
  809. char *ndl_buf;
  810. const char *ndl_buf_p;
  811. size_t ndl_buf_len, ndl_buf_left;
  812. unsigned int match_ofs;
  813. *pretval = (unsigned int)-1;
  814. err = php_iconv_string(ndl, ndl_nbytes,
  815. &ndl_buf, &ndl_buf_len, GENERIC_SUPERSET_NAME, enc);
  816. if (err != PHP_ICONV_ERR_SUCCESS) {
  817. if (ndl_buf != NULL) {
  818. efree(ndl_buf);
  819. }
  820. return err;
  821. }
  822. cd = iconv_open(GENERIC_SUPERSET_NAME, enc);
  823. if (cd == (iconv_t)(-1)) {
  824. if (ndl_buf != NULL) {
  825. efree(ndl_buf);
  826. }
  827. #if ICONV_SUPPORTS_ERRNO
  828. if (errno == EINVAL) {
  829. return PHP_ICONV_ERR_WRONG_CHARSET;
  830. } else {
  831. return PHP_ICONV_ERR_CONVERTER;
  832. }
  833. #else
  834. return PHP_ICONV_ERR_UNKNOWN;
  835. #endif
  836. }
  837. ndl_buf_p = ndl_buf;
  838. ndl_buf_left = ndl_buf_len;
  839. match_ofs = (unsigned int)-1;
  840. for (in_p = haystk, in_left = haystk_nbytes, cnt = 0; in_left > 0; ++cnt) {
  841. size_t prev_in_left;
  842. out_p = buf;
  843. out_left = sizeof(buf);
  844. prev_in_left = in_left;
  845. if (iconv(cd, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  846. if (prev_in_left == in_left) {
  847. #if ICONV_SUPPORTS_ERRNO
  848. switch (errno) {
  849. case EINVAL:
  850. err = PHP_ICONV_ERR_ILLEGAL_CHAR;
  851. break;
  852. case EILSEQ:
  853. err = PHP_ICONV_ERR_ILLEGAL_SEQ;
  854. break;
  855. case E2BIG:
  856. break;
  857. default:
  858. err = PHP_ICONV_ERR_UNKNOWN;
  859. break;
  860. }
  861. #endif
  862. break;
  863. }
  864. }
  865. if (offset >= 0) {
  866. if (cnt >= (unsigned int)offset) {
  867. if (_php_iconv_memequal(buf, ndl_buf_p, sizeof(buf))) {
  868. if (match_ofs == (unsigned int)-1) {
  869. match_ofs = cnt;
  870. }
  871. ndl_buf_p += GENERIC_SUPERSET_NBYTES;
  872. ndl_buf_left -= GENERIC_SUPERSET_NBYTES;
  873. if (ndl_buf_left == 0) {
  874. *pretval = match_ofs;
  875. break;
  876. }
  877. } else {
  878. unsigned int i, j, lim;
  879. i = 0;
  880. j = GENERIC_SUPERSET_NBYTES;
  881. lim = (unsigned int)(ndl_buf_p - ndl_buf);
  882. while (j < lim) {
  883. if (_php_iconv_memequal(&ndl_buf[j], &ndl_buf[i],
  884. GENERIC_SUPERSET_NBYTES)) {
  885. i += GENERIC_SUPERSET_NBYTES;
  886. } else {
  887. j -= i;
  888. i = 0;
  889. }
  890. j += GENERIC_SUPERSET_NBYTES;
  891. }
  892. if (_php_iconv_memequal(buf, &ndl_buf[i], sizeof(buf))) {
  893. match_ofs += (lim - i) / GENERIC_SUPERSET_NBYTES;
  894. i += GENERIC_SUPERSET_NBYTES;
  895. ndl_buf_p = &ndl_buf[i];
  896. ndl_buf_left = ndl_buf_len - i;
  897. } else {
  898. match_ofs = (unsigned int)-1;
  899. ndl_buf_p = ndl_buf;
  900. ndl_buf_left = ndl_buf_len;
  901. }
  902. }
  903. }
  904. } else {
  905. if (_php_iconv_memequal(buf, ndl_buf_p, sizeof(buf))) {
  906. if (match_ofs == (unsigned int)-1) {
  907. match_ofs = cnt;
  908. }
  909. ndl_buf_p += GENERIC_SUPERSET_NBYTES;
  910. ndl_buf_left -= GENERIC_SUPERSET_NBYTES;
  911. if (ndl_buf_left == 0) {
  912. *pretval = match_ofs;
  913. ndl_buf_p = ndl_buf;
  914. ndl_buf_left = ndl_buf_len;
  915. match_ofs = -1;
  916. }
  917. } else {
  918. unsigned int i, j, lim;
  919. i = 0;
  920. j = GENERIC_SUPERSET_NBYTES;
  921. lim = (unsigned int)(ndl_buf_p - ndl_buf);
  922. while (j < lim) {
  923. if (_php_iconv_memequal(&ndl_buf[j], &ndl_buf[i],
  924. GENERIC_SUPERSET_NBYTES)) {
  925. i += GENERIC_SUPERSET_NBYTES;
  926. } else {
  927. j -= i;
  928. i = 0;
  929. }
  930. j += GENERIC_SUPERSET_NBYTES;
  931. }
  932. if (_php_iconv_memequal(buf, &ndl_buf[i], sizeof(buf))) {
  933. match_ofs += (lim - i) / GENERIC_SUPERSET_NBYTES;
  934. i += GENERIC_SUPERSET_NBYTES;
  935. ndl_buf_p = &ndl_buf[i];
  936. ndl_buf_left = ndl_buf_len - i;
  937. } else {
  938. match_ofs = (unsigned int)-1;
  939. ndl_buf_p = ndl_buf;
  940. ndl_buf_left = ndl_buf_len;
  941. }
  942. }
  943. }
  944. }
  945. if (ndl_buf) {
  946. efree(ndl_buf);
  947. }
  948. iconv_close(cd);
  949. return err;
  950. }
  951. /* }}} */
  952. /* {{{ _php_iconv_mime_encode() */
  953. static php_iconv_err_t _php_iconv_mime_encode(smart_str *pretval, const char *fname, size_t fname_nbytes, const char *fval, size_t fval_nbytes, unsigned int max_line_len, const char *lfchars, php_iconv_enc_scheme_t enc_scheme, const char *out_charset, const char *enc)
  954. {
  955. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  956. iconv_t cd = (iconv_t)(-1), cd_pl = (iconv_t)(-1);
  957. unsigned int char_cnt = 0;
  958. size_t out_charset_len;
  959. size_t lfchars_len;
  960. char *buf = NULL;
  961. char *encoded = NULL;
  962. size_t encoded_len;
  963. const char *in_p;
  964. size_t in_left;
  965. char *out_p;
  966. size_t out_left;
  967. static int qp_table[256] = {
  968. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x00 */
  969. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 */
  970. 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x20 */
  971. 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 3, 1, 3, /* 0x30 */
  972. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 */
  973. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, /* 0x50 */
  974. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 */
  975. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, /* 0x70 */
  976. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x80 */
  977. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x90 */
  978. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xA0 */
  979. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xB0 */
  980. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xC0 */
  981. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xD0 */
  982. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xE0 */
  983. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 /* 0xF0 */
  984. };
  985. out_charset_len = strlen(out_charset);
  986. lfchars_len = strlen(lfchars);
  987. if ((fname_nbytes + 2) >= max_line_len
  988. || (out_charset_len + 12) >= max_line_len) {
  989. /* field name is too long */
  990. err = PHP_ICONV_ERR_TOO_BIG;
  991. goto out;
  992. }
  993. cd_pl = iconv_open(ICONV_ASCII_ENCODING, enc);
  994. if (cd_pl == (iconv_t)(-1)) {
  995. #if ICONV_SUPPORTS_ERRNO
  996. if (errno == EINVAL) {
  997. err = PHP_ICONV_ERR_WRONG_CHARSET;
  998. } else {
  999. err = PHP_ICONV_ERR_CONVERTER;
  1000. }
  1001. #else
  1002. err = PHP_ICONV_ERR_UNKNOWN;
  1003. #endif
  1004. goto out;
  1005. }
  1006. cd = iconv_open(out_charset, enc);
  1007. if (cd == (iconv_t)(-1)) {
  1008. #if ICONV_SUPPORTS_ERRNO
  1009. if (errno == EINVAL) {
  1010. err = PHP_ICONV_ERR_WRONG_CHARSET;
  1011. } else {
  1012. err = PHP_ICONV_ERR_CONVERTER;
  1013. }
  1014. #else
  1015. err = PHP_ICONV_ERR_UNKNOWN;
  1016. #endif
  1017. goto out;
  1018. }
  1019. buf = safe_emalloc(1, max_line_len, 5);
  1020. char_cnt = max_line_len;
  1021. _php_iconv_appendl(pretval, fname, fname_nbytes, cd_pl);
  1022. char_cnt -= fname_nbytes;
  1023. smart_str_appendl(pretval, ": ", sizeof(": ") - 1);
  1024. char_cnt -= 2;
  1025. in_p = fval;
  1026. in_left = fval_nbytes;
  1027. do {
  1028. size_t prev_in_left;
  1029. size_t out_size;
  1030. if (char_cnt < (out_charset_len + 12)) {
  1031. /* lfchars must be encoded in ASCII here*/
  1032. smart_str_appendl(pretval, lfchars, lfchars_len);
  1033. smart_str_appendc(pretval, ' ');
  1034. char_cnt = max_line_len - 1;
  1035. }
  1036. smart_str_appendl(pretval, "=?", sizeof("=?") - 1);
  1037. char_cnt -= 2;
  1038. smart_str_appendl(pretval, out_charset, out_charset_len);
  1039. char_cnt -= out_charset_len;
  1040. smart_str_appendc(pretval, '?');
  1041. char_cnt --;
  1042. switch (enc_scheme) {
  1043. case PHP_ICONV_ENC_SCHEME_BASE64: {
  1044. size_t ini_in_left;
  1045. const char *ini_in_p;
  1046. size_t out_reserved = 4;
  1047. int dummy;
  1048. smart_str_appendc(pretval, 'B');
  1049. char_cnt--;
  1050. smart_str_appendc(pretval, '?');
  1051. char_cnt--;
  1052. prev_in_left = ini_in_left = in_left;
  1053. ini_in_p = in_p;
  1054. out_size = (char_cnt - 2) / 4 * 3;
  1055. for (;;) {
  1056. out_p = buf;
  1057. if (out_size <= out_reserved) {
  1058. err = PHP_ICONV_ERR_TOO_BIG;
  1059. goto out;
  1060. }
  1061. out_left = out_size - out_reserved;
  1062. if (iconv(cd, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  1063. #if ICONV_SUPPORTS_ERRNO
  1064. switch (errno) {
  1065. case EINVAL:
  1066. err = PHP_ICONV_ERR_ILLEGAL_CHAR;
  1067. goto out;
  1068. case EILSEQ:
  1069. err = PHP_ICONV_ERR_ILLEGAL_SEQ;
  1070. goto out;
  1071. case E2BIG:
  1072. if (prev_in_left == in_left) {
  1073. err = PHP_ICONV_ERR_TOO_BIG;
  1074. goto out;
  1075. }
  1076. break;
  1077. default:
  1078. err = PHP_ICONV_ERR_UNKNOWN;
  1079. goto out;
  1080. }
  1081. #else
  1082. if (prev_in_left == in_left) {
  1083. err = PHP_ICONV_ERR_UNKNOWN;
  1084. goto out;
  1085. }
  1086. #endif
  1087. }
  1088. out_left += out_reserved;
  1089. if (iconv(cd, NULL, NULL, (char **) &out_p, &out_left) == (size_t)-1) {
  1090. #if ICONV_SUPPORTS_ERRNO
  1091. if (errno != E2BIG) {
  1092. err = PHP_ICONV_ERR_UNKNOWN;
  1093. goto out;
  1094. }
  1095. #else
  1096. if (out_left != 0) {
  1097. err = PHP_ICONV_ERR_UNKNOWN;
  1098. goto out;
  1099. }
  1100. #endif
  1101. } else {
  1102. break;
  1103. }
  1104. if (iconv(cd, NULL, NULL, NULL, NULL) == (size_t)-1) {
  1105. err = PHP_ICONV_ERR_UNKNOWN;
  1106. goto out;
  1107. }
  1108. out_reserved += 4;
  1109. in_left = ini_in_left;
  1110. in_p = ini_in_p;
  1111. }
  1112. prev_in_left = in_left;
  1113. encoded = (char *) php_base64_encode((unsigned char *) buf, (int)(out_size - out_left), &dummy);
  1114. encoded_len = (size_t)dummy;
  1115. if (char_cnt < encoded_len) {
  1116. /* something went wrong! */
  1117. err = PHP_ICONV_ERR_UNKNOWN;
  1118. goto out;
  1119. }
  1120. smart_str_appendl(pretval, encoded, encoded_len);
  1121. char_cnt -= encoded_len;
  1122. smart_str_appendl(pretval, "?=", sizeof("?=") - 1);
  1123. char_cnt -= 2;
  1124. efree(encoded);
  1125. encoded = NULL;
  1126. } break; /* case PHP_ICONV_ENC_SCHEME_BASE64: */
  1127. case PHP_ICONV_ENC_SCHEME_QPRINT: {
  1128. size_t ini_in_left;
  1129. const char *ini_in_p;
  1130. const unsigned char *p;
  1131. size_t nbytes_required;
  1132. smart_str_appendc(pretval, 'Q');
  1133. char_cnt--;
  1134. smart_str_appendc(pretval, '?');
  1135. char_cnt--;
  1136. prev_in_left = ini_in_left = in_left;
  1137. ini_in_p = in_p;
  1138. for (out_size = (char_cnt - 2) / 3; out_size > 0;) {
  1139. size_t prev_out_left;
  1140. nbytes_required = 0;
  1141. out_p = buf;
  1142. out_left = out_size;
  1143. if (iconv(cd, (char **)&in_p, &in_left, (char **) &out_p, &out_left) == (size_t)-1) {
  1144. #if ICONV_SUPPORTS_ERRNO
  1145. switch (errno) {
  1146. case EINVAL:
  1147. err = PHP_ICONV_ERR_ILLEGAL_CHAR;
  1148. goto out;
  1149. case EILSEQ:
  1150. err = PHP_ICONV_ERR_ILLEGAL_SEQ;
  1151. goto out;
  1152. case E2BIG:
  1153. if (prev_in_left == in_left) {
  1154. err = PHP_ICONV_ERR_UNKNOWN;
  1155. goto out;
  1156. }
  1157. break;
  1158. default:
  1159. err = PHP_ICONV_ERR_UNKNOWN;
  1160. goto out;
  1161. }
  1162. #else
  1163. if (prev_in_left == in_left) {
  1164. err = PHP_ICONV_ERR_UNKNOWN;
  1165. goto out;
  1166. }
  1167. #endif
  1168. }
  1169. prev_out_left = out_left;
  1170. if (iconv(cd, NULL, NULL, (char **) &out_p, &out_left) == (size_t)-1) {
  1171. #if ICONV_SUPPORTS_ERRNO
  1172. if (errno != E2BIG) {
  1173. err = PHP_ICONV_ERR_UNKNOWN;
  1174. goto out;
  1175. }
  1176. #else
  1177. if (out_left == prev_out_left) {
  1178. err = PHP_ICONV_ERR_UNKNOWN;
  1179. goto out;
  1180. }
  1181. #endif
  1182. }
  1183. for (p = (unsigned char *)buf; p < (unsigned char *)out_p; p++) {
  1184. nbytes_required += qp_table[*p];
  1185. }
  1186. if (nbytes_required <= char_cnt - 2) {
  1187. break;
  1188. }
  1189. out_size -= ((nbytes_required - (char_cnt - 2)) + 1) / 3;
  1190. in_left = ini_in_left;
  1191. in_p = ini_in_p;
  1192. }
  1193. for (p = (unsigned char *)buf; p < (unsigned char *)out_p; p++) {
  1194. if (qp_table[*p] == 1) {
  1195. smart_str_appendc(pretval, *(char *)p);
  1196. char_cnt--;
  1197. } else {
  1198. static char qp_digits[] = "0123456789ABCDEF";
  1199. smart_str_appendc(pretval, '=');
  1200. smart_str_appendc(pretval, qp_digits[(*p >> 4) & 0x0f]);
  1201. smart_str_appendc(pretval, qp_digits[(*p & 0x0f)]);
  1202. char_cnt -= 3;
  1203. }
  1204. }
  1205. smart_str_appendl(pretval, "?=", sizeof("?=") - 1);
  1206. char_cnt -= 2;
  1207. if (iconv(cd, NULL, NULL, NULL, NULL) == (size_t)-1) {
  1208. err = PHP_ICONV_ERR_UNKNOWN;
  1209. goto out;
  1210. }
  1211. } break; /* case PHP_ICONV_ENC_SCHEME_QPRINT: */
  1212. }
  1213. } while (in_left > 0);
  1214. smart_str_0(pretval);
  1215. out:
  1216. if (cd != (iconv_t)(-1)) {
  1217. iconv_close(cd);
  1218. }
  1219. if (cd_pl != (iconv_t)(-1)) {
  1220. iconv_close(cd_pl);
  1221. }
  1222. if (encoded != NULL) {
  1223. efree(encoded);
  1224. }
  1225. if (buf != NULL) {
  1226. efree(buf);
  1227. }
  1228. return err;
  1229. }
  1230. /* }}} */
  1231. /* {{{ _php_iconv_mime_decode() */
  1232. static php_iconv_err_t _php_iconv_mime_decode(smart_str *pretval, const char *str, size_t str_nbytes, const char *enc, const char **next_pos, int mode)
  1233. {
  1234. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  1235. iconv_t cd = (iconv_t)(-1), cd_pl = (iconv_t)(-1);
  1236. const char *p1;
  1237. size_t str_left;
  1238. unsigned int scan_stat = 0;
  1239. const char *csname = NULL;
  1240. size_t csname_len;
  1241. const char *encoded_text = NULL;
  1242. size_t encoded_text_len = 0;
  1243. const char *encoded_word = NULL;
  1244. const char *spaces = NULL;
  1245. php_iconv_enc_scheme_t enc_scheme = PHP_ICONV_ENC_SCHEME_BASE64;
  1246. if (next_pos != NULL) {
  1247. *next_pos = NULL;
  1248. }
  1249. cd_pl = iconv_open(enc, ICONV_ASCII_ENCODING);
  1250. if (cd_pl == (iconv_t)(-1)) {
  1251. #if ICONV_SUPPORTS_ERRNO
  1252. if (errno == EINVAL) {
  1253. err = PHP_ICONV_ERR_WRONG_CHARSET;
  1254. } else {
  1255. err = PHP_ICONV_ERR_CONVERTER;
  1256. }
  1257. #else
  1258. err = PHP_ICONV_ERR_UNKNOWN;
  1259. #endif
  1260. goto out;
  1261. }
  1262. p1 = str;
  1263. for (str_left = str_nbytes; str_left > 0; str_left--, p1++) {
  1264. int eos = 0;
  1265. switch (scan_stat) {
  1266. case 0: /* expecting any character */
  1267. switch (*p1) {
  1268. case '\r': /* part of an EOL sequence? */
  1269. scan_stat = 7;
  1270. break;
  1271. case '\n':
  1272. scan_stat = 8;
  1273. break;
  1274. case '=': /* first letter of an encoded chunk */
  1275. encoded_word = p1;
  1276. scan_stat = 1;
  1277. break;
  1278. case ' ': case '\t': /* a chunk of whitespaces */
  1279. spaces = p1;
  1280. scan_stat = 11;
  1281. break;
  1282. default: /* first letter of a non-encoded word */
  1283. _php_iconv_appendc(pretval, *p1, cd_pl);
  1284. encoded_word = NULL;
  1285. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1286. scan_stat = 12;
  1287. }
  1288. break;
  1289. }
  1290. break;
  1291. case 1: /* expecting a delimiter */
  1292. if (*p1 != '?') {
  1293. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1294. if (err != PHP_ICONV_ERR_SUCCESS) {
  1295. goto out;
  1296. }
  1297. encoded_word = NULL;
  1298. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1299. scan_stat = 12;
  1300. } else {
  1301. scan_stat = 0;
  1302. }
  1303. break;
  1304. }
  1305. csname = p1 + 1;
  1306. scan_stat = 2;
  1307. break;
  1308. case 2: /* expecting a charset name */
  1309. switch (*p1) {
  1310. case '?': /* normal delimiter: encoding scheme follows */
  1311. scan_stat = 3;
  1312. break;
  1313. case '*': /* new style delimiter: locale id follows */
  1314. scan_stat = 10;
  1315. break;
  1316. }
  1317. if (scan_stat != 2) {
  1318. char tmpbuf[80];
  1319. if (csname == NULL) {
  1320. err = PHP_ICONV_ERR_MALFORMED;
  1321. goto out;
  1322. }
  1323. csname_len = (size_t)(p1 - csname);
  1324. if (csname_len > sizeof(tmpbuf) - 1) {
  1325. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1326. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1327. if (err != PHP_ICONV_ERR_SUCCESS) {
  1328. goto out;
  1329. }
  1330. encoded_word = NULL;
  1331. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1332. scan_stat = 12;
  1333. } else {
  1334. scan_stat = 0;
  1335. }
  1336. break;
  1337. } else {
  1338. err = PHP_ICONV_ERR_MALFORMED;
  1339. goto out;
  1340. }
  1341. }
  1342. memcpy(tmpbuf, csname, csname_len);
  1343. tmpbuf[csname_len] = '\0';
  1344. if (cd != (iconv_t)(-1)) {
  1345. iconv_close(cd);
  1346. }
  1347. cd = iconv_open(enc, tmpbuf);
  1348. if (cd == (iconv_t)(-1)) {
  1349. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1350. /* Bad character set, but the user wants us to
  1351. * press on. In this case, we'll just insert the
  1352. * undecoded encoded word, since there isn't really
  1353. * a more sensible behaviour available; the only
  1354. * other options are to swallow the encoded word
  1355. * entirely or decode it with an arbitrarily chosen
  1356. * single byte encoding, both of which seem to have
  1357. * a higher WTF factor than leaving it undecoded.
  1358. *
  1359. * Given this approach, we need to skip ahead to
  1360. * the end of the encoded word. */
  1361. int qmarks = 2;
  1362. while (qmarks > 0 && str_left > 1) {
  1363. if (*(++p1) == '?') {
  1364. --qmarks;
  1365. }
  1366. --str_left;
  1367. }
  1368. /* Look ahead to check for the terminating = that
  1369. * should be there as well; if it's there, we'll
  1370. * also include that. If it's not, there isn't much
  1371. * we can do at this point. */
  1372. if (*(p1 + 1) == '=') {
  1373. ++p1;
  1374. --str_left;
  1375. }
  1376. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1377. if (err != PHP_ICONV_ERR_SUCCESS) {
  1378. goto out;
  1379. }
  1380. /* Let's go back and see if there are further
  1381. * encoded words or bare content, and hope they
  1382. * might actually have a valid character set. */
  1383. scan_stat = 12;
  1384. break;
  1385. } else {
  1386. #if ICONV_SUPPORTS_ERRNO
  1387. if (errno == EINVAL) {
  1388. err = PHP_ICONV_ERR_WRONG_CHARSET;
  1389. } else {
  1390. err = PHP_ICONV_ERR_CONVERTER;
  1391. }
  1392. #else
  1393. err = PHP_ICONV_ERR_UNKNOWN;
  1394. #endif
  1395. goto out;
  1396. }
  1397. }
  1398. }
  1399. break;
  1400. case 3: /* expecting a encoding scheme specifier */
  1401. switch (*p1) {
  1402. case 'b':
  1403. case 'B':
  1404. enc_scheme = PHP_ICONV_ENC_SCHEME_BASE64;
  1405. scan_stat = 4;
  1406. break;
  1407. case 'q':
  1408. case 'Q':
  1409. enc_scheme = PHP_ICONV_ENC_SCHEME_QPRINT;
  1410. scan_stat = 4;
  1411. break;
  1412. default:
  1413. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1414. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1415. if (err != PHP_ICONV_ERR_SUCCESS) {
  1416. goto out;
  1417. }
  1418. encoded_word = NULL;
  1419. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1420. scan_stat = 12;
  1421. } else {
  1422. scan_stat = 0;
  1423. }
  1424. break;
  1425. } else {
  1426. err = PHP_ICONV_ERR_MALFORMED;
  1427. goto out;
  1428. }
  1429. }
  1430. break;
  1431. case 4: /* expecting a delimiter */
  1432. if (*p1 != '?') {
  1433. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1434. /* pass the entire chunk through the converter */
  1435. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1436. if (err != PHP_ICONV_ERR_SUCCESS) {
  1437. goto out;
  1438. }
  1439. encoded_word = NULL;
  1440. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1441. scan_stat = 12;
  1442. } else {
  1443. scan_stat = 0;
  1444. }
  1445. break;
  1446. } else {
  1447. err = PHP_ICONV_ERR_MALFORMED;
  1448. goto out;
  1449. }
  1450. }
  1451. encoded_text = p1 + 1;
  1452. scan_stat = 5;
  1453. break;
  1454. case 5: /* expecting an encoded portion */
  1455. if (*p1 == '?') {
  1456. encoded_text_len = (size_t)(p1 - encoded_text);
  1457. scan_stat = 6;
  1458. }
  1459. break;
  1460. case 7: /* expecting a "\n" character */
  1461. if (*p1 == '\n') {
  1462. scan_stat = 8;
  1463. } else {
  1464. /* bare CR */
  1465. _php_iconv_appendc(pretval, '\r', cd_pl);
  1466. _php_iconv_appendc(pretval, *p1, cd_pl);
  1467. scan_stat = 0;
  1468. }
  1469. break;
  1470. case 8: /* checking whether the following line is part of a
  1471. folded header */
  1472. if (*p1 != ' ' && *p1 != '\t') {
  1473. --p1;
  1474. str_left = 1; /* quit_loop */
  1475. break;
  1476. }
  1477. if (encoded_word == NULL) {
  1478. _php_iconv_appendc(pretval, ' ', cd_pl);
  1479. }
  1480. spaces = NULL;
  1481. scan_stat = 11;
  1482. break;
  1483. case 6: /* expecting a End-Of-Chunk character "=" */
  1484. if (*p1 != '=') {
  1485. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1486. /* pass the entire chunk through the converter */
  1487. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1488. if (err != PHP_ICONV_ERR_SUCCESS) {
  1489. goto out;
  1490. }
  1491. encoded_word = NULL;
  1492. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1493. scan_stat = 12;
  1494. } else {
  1495. scan_stat = 0;
  1496. }
  1497. break;
  1498. } else {
  1499. err = PHP_ICONV_ERR_MALFORMED;
  1500. goto out;
  1501. }
  1502. }
  1503. scan_stat = 9;
  1504. if (str_left == 1) {
  1505. eos = 1;
  1506. } else {
  1507. break;
  1508. }
  1509. case 9: /* choice point, seeing what to do next.*/
  1510. switch (*p1) {
  1511. default:
  1512. /* Handle non-RFC-compliant formats
  1513. *
  1514. * RFC2047 requires the character that comes right
  1515. * after an encoded word (chunk) to be a whitespace,
  1516. * while there are lots of broken implementations that
  1517. * generate such malformed headers that don't fulfill
  1518. * that requirement.
  1519. */
  1520. if (!eos) {
  1521. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1522. /* pass the entire chunk through the converter */
  1523. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1524. if (err != PHP_ICONV_ERR_SUCCESS) {
  1525. goto out;
  1526. }
  1527. scan_stat = 12;
  1528. break;
  1529. }
  1530. }
  1531. /* break is omitted intentionally */
  1532. case '\r': case '\n': case ' ': case '\t': {
  1533. char *decoded_text;
  1534. size_t decoded_text_len;
  1535. int dummy;
  1536. switch (enc_scheme) {
  1537. case PHP_ICONV_ENC_SCHEME_BASE64:
  1538. decoded_text = (char *)php_base64_decode((unsigned char*)encoded_text, (int)encoded_text_len, &dummy);
  1539. decoded_text_len = (size_t)dummy;
  1540. break;
  1541. case PHP_ICONV_ENC_SCHEME_QPRINT:
  1542. decoded_text = (char *)php_quot_print_decode((unsigned char*)encoded_text, (int)encoded_text_len, &decoded_text_len, 1);
  1543. break;
  1544. default:
  1545. decoded_text = NULL;
  1546. break;
  1547. }
  1548. if (decoded_text == NULL) {
  1549. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1550. /* pass the entire chunk through the converter */
  1551. err = _php_iconv_appendl(pretval, encoded_word, (size_t)((p1 + 1) - encoded_word), cd_pl);
  1552. if (err != PHP_ICONV_ERR_SUCCESS) {
  1553. goto out;
  1554. }
  1555. encoded_word = NULL;
  1556. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1557. scan_stat = 12;
  1558. } else {
  1559. scan_stat = 0;
  1560. }
  1561. break;
  1562. } else {
  1563. err = PHP_ICONV_ERR_UNKNOWN;
  1564. goto out;
  1565. }
  1566. }
  1567. err = _php_iconv_appendl(pretval, decoded_text, decoded_text_len, cd);
  1568. efree(decoded_text);
  1569. if (err != PHP_ICONV_ERR_SUCCESS) {
  1570. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1571. /* pass the entire chunk through the converter */
  1572. err = _php_iconv_appendl(pretval, encoded_word, (size_t)(p1 - encoded_word), cd_pl);
  1573. encoded_word = NULL;
  1574. if (err != PHP_ICONV_ERR_SUCCESS) {
  1575. break;
  1576. }
  1577. } else {
  1578. goto out;
  1579. }
  1580. }
  1581. if (eos) { /* reached end-of-string. done. */
  1582. scan_stat = 0;
  1583. break;
  1584. }
  1585. switch (*p1) {
  1586. case '\r': /* part of an EOL sequence? */
  1587. scan_stat = 7;
  1588. break;
  1589. case '\n':
  1590. scan_stat = 8;
  1591. break;
  1592. case '=': /* first letter of an encoded chunk */
  1593. scan_stat = 1;
  1594. break;
  1595. case ' ': case '\t': /* medial whitespaces */
  1596. spaces = p1;
  1597. scan_stat = 11;
  1598. break;
  1599. default: /* first letter of a non-encoded word */
  1600. _php_iconv_appendc(pretval, *p1, cd_pl);
  1601. scan_stat = 12;
  1602. break;
  1603. }
  1604. } break;
  1605. }
  1606. break;
  1607. case 10: /* expects a language specifier. dismiss it for now */
  1608. if (*p1 == '?') {
  1609. scan_stat = 3;
  1610. }
  1611. break;
  1612. case 11: /* expecting a chunk of whitespaces */
  1613. switch (*p1) {
  1614. case '\r': /* part of an EOL sequence? */
  1615. scan_stat = 7;
  1616. break;
  1617. case '\n':
  1618. scan_stat = 8;
  1619. break;
  1620. case '=': /* first letter of an encoded chunk */
  1621. if (spaces != NULL && encoded_word == NULL) {
  1622. _php_iconv_appendl(pretval, spaces, (size_t)(p1 - spaces), cd_pl);
  1623. spaces = NULL;
  1624. }
  1625. encoded_word = p1;
  1626. scan_stat = 1;
  1627. break;
  1628. case ' ': case '\t':
  1629. break;
  1630. default: /* first letter of a non-encoded word */
  1631. if (spaces != NULL) {
  1632. _php_iconv_appendl(pretval, spaces, (size_t)(p1 - spaces), cd_pl);
  1633. spaces = NULL;
  1634. }
  1635. _php_iconv_appendc(pretval, *p1, cd_pl);
  1636. encoded_word = NULL;
  1637. if ((mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1638. scan_stat = 12;
  1639. } else {
  1640. scan_stat = 0;
  1641. }
  1642. break;
  1643. }
  1644. break;
  1645. case 12: /* expecting a non-encoded word */
  1646. switch (*p1) {
  1647. case '\r': /* part of an EOL sequence? */
  1648. scan_stat = 7;
  1649. break;
  1650. case '\n':
  1651. scan_stat = 8;
  1652. break;
  1653. case ' ': case '\t':
  1654. spaces = p1;
  1655. scan_stat = 11;
  1656. break;
  1657. case '=': /* first letter of an encoded chunk */
  1658. if (!(mode & PHP_ICONV_MIME_DECODE_STRICT)) {
  1659. encoded_word = p1;
  1660. scan_stat = 1;
  1661. break;
  1662. }
  1663. /* break is omitted intentionally */
  1664. default:
  1665. _php_iconv_appendc(pretval, *p1, cd_pl);
  1666. break;
  1667. }
  1668. break;
  1669. }
  1670. }
  1671. switch (scan_stat) {
  1672. case 0: case 8: case 11: case 12:
  1673. break;
  1674. default:
  1675. if ((mode & PHP_ICONV_MIME_DECODE_CONTINUE_ON_ERROR)) {
  1676. if (scan_stat == 1) {
  1677. _php_iconv_appendc(pretval, '=', cd_pl);
  1678. }
  1679. err = PHP_ICONV_ERR_SUCCESS;
  1680. } else {
  1681. err = PHP_ICONV_ERR_MALFORMED;
  1682. goto out;
  1683. }
  1684. }
  1685. if (next_pos != NULL) {
  1686. *next_pos = p1;
  1687. }
  1688. smart_str_0(pretval);
  1689. out:
  1690. if (cd != (iconv_t)(-1)) {
  1691. iconv_close(cd);
  1692. }
  1693. if (cd_pl != (iconv_t)(-1)) {
  1694. iconv_close(cd_pl);
  1695. }
  1696. return err;
  1697. }
  1698. /* }}} */
  1699. /* {{{ php_iconv_show_error() */
  1700. static void _php_iconv_show_error(php_iconv_err_t err, const char *out_charset, const char *in_charset TSRMLS_DC)
  1701. {
  1702. switch (err) {
  1703. case PHP_ICONV_ERR_SUCCESS:
  1704. break;
  1705. case PHP_ICONV_ERR_CONVERTER:
  1706. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot open converter");
  1707. break;
  1708. case PHP_ICONV_ERR_WRONG_CHARSET:
  1709. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong charset, conversion from `%s' to `%s' is not allowed",
  1710. in_charset, out_charset);
  1711. break;
  1712. case PHP_ICONV_ERR_ILLEGAL_CHAR:
  1713. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected an incomplete multibyte character in input string");
  1714. break;
  1715. case PHP_ICONV_ERR_ILLEGAL_SEQ:
  1716. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected an illegal character in input string");
  1717. break;
  1718. case PHP_ICONV_ERR_TOO_BIG:
  1719. /* should not happen */
  1720. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer length exceeded");
  1721. break;
  1722. case PHP_ICONV_ERR_MALFORMED:
  1723. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Malformed string");
  1724. break;
  1725. default:
  1726. /* other error */
  1727. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown error (%d)", errno);
  1728. break;
  1729. }
  1730. }
  1731. /* }}} */
  1732. /* {{{ proto int iconv_strlen(string str [, string charset])
  1733. Returns the character count of str */
  1734. PHP_FUNCTION(iconv_strlen)
  1735. {
  1736. char *charset = get_internal_encoding(TSRMLS_C);
  1737. int charset_len = 0;
  1738. char *str;
  1739. int str_len;
  1740. php_iconv_err_t err;
  1741. unsigned int retval;
  1742. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
  1743. &str, &str_len, &charset, &charset_len) == FAILURE) {
  1744. RETURN_FALSE;
  1745. }
  1746. if (charset_len >= ICONV_CSNMAXLEN) {
  1747. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1748. RETURN_FALSE;
  1749. }
  1750. err = _php_iconv_strlen(&retval, str, str_len, charset);
  1751. _php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset TSRMLS_CC);
  1752. if (err == PHP_ICONV_ERR_SUCCESS) {
  1753. RETVAL_LONG(retval);
  1754. } else {
  1755. RETVAL_FALSE;
  1756. }
  1757. }
  1758. /* }}} */
  1759. /* {{{ proto string iconv_substr(string str, int offset, [int length, string charset])
  1760. Returns specified part of a string */
  1761. PHP_FUNCTION(iconv_substr)
  1762. {
  1763. char *charset = get_internal_encoding(TSRMLS_C);
  1764. int charset_len = 0;
  1765. char *str;
  1766. int str_len;
  1767. long offset, length = 0;
  1768. php_iconv_err_t err;
  1769. smart_str retval = {0};
  1770. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ls",
  1771. &str, &str_len, &offset, &length,
  1772. &charset, &charset_len) == FAILURE) {
  1773. RETURN_FALSE;
  1774. }
  1775. if (charset_len >= ICONV_CSNMAXLEN) {
  1776. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1777. RETURN_FALSE;
  1778. }
  1779. if (ZEND_NUM_ARGS() < 3) {
  1780. length = str_len;
  1781. }
  1782. err = _php_iconv_substr(&retval, str, str_len, offset, length, charset);
  1783. _php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset TSRMLS_CC);
  1784. if (err == PHP_ICONV_ERR_SUCCESS && str != NULL && retval.c != NULL) {
  1785. RETURN_STRINGL(retval.c, retval.len, 0);
  1786. }
  1787. smart_str_free(&retval);
  1788. RETURN_FALSE;
  1789. }
  1790. /* }}} */
  1791. /* {{{ proto int iconv_strpos(string haystack, string needle [, int offset [, string charset]])
  1792. Finds position of first occurrence of needle within part of haystack beginning with offset */
  1793. PHP_FUNCTION(iconv_strpos)
  1794. {
  1795. char *charset = get_internal_encoding(TSRMLS_C);
  1796. int charset_len = 0;
  1797. char *haystk;
  1798. int haystk_len;
  1799. char *ndl;
  1800. int ndl_len;
  1801. long offset = 0;
  1802. php_iconv_err_t err;
  1803. unsigned int retval;
  1804. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ls",
  1805. &haystk, &haystk_len, &ndl, &ndl_len,
  1806. &offset, &charset, &charset_len) == FAILURE) {
  1807. RETURN_FALSE;
  1808. }
  1809. if (charset_len >= ICONV_CSNMAXLEN) {
  1810. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1811. RETURN_FALSE;
  1812. }
  1813. if (offset < 0) {
  1814. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string.");
  1815. RETURN_FALSE;
  1816. }
  1817. if (ndl_len < 1) {
  1818. RETURN_FALSE;
  1819. }
  1820. err = _php_iconv_strpos(&retval, haystk, haystk_len, ndl, ndl_len,
  1821. offset, charset);
  1822. _php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset TSRMLS_CC);
  1823. if (err == PHP_ICONV_ERR_SUCCESS && retval != (unsigned int)-1) {
  1824. RETVAL_LONG((long)retval);
  1825. } else {
  1826. RETVAL_FALSE;
  1827. }
  1828. }
  1829. /* }}} */
  1830. /* {{{ proto int iconv_strrpos(string haystack, string needle [, string charset])
  1831. Finds position of last occurrence of needle within part of haystack beginning with offset */
  1832. PHP_FUNCTION(iconv_strrpos)
  1833. {
  1834. char *charset = get_internal_encoding(TSRMLS_C);
  1835. int charset_len = 0;
  1836. char *haystk;
  1837. int haystk_len;
  1838. char *ndl;
  1839. int ndl_len;
  1840. php_iconv_err_t err;
  1841. unsigned int retval;
  1842. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s",
  1843. &haystk, &haystk_len, &ndl, &ndl_len,
  1844. &charset, &charset_len) == FAILURE) {
  1845. RETURN_FALSE;
  1846. }
  1847. if (ndl_len < 1) {
  1848. RETURN_FALSE;
  1849. }
  1850. if (charset_len >= ICONV_CSNMAXLEN) {
  1851. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1852. RETURN_FALSE;
  1853. }
  1854. err = _php_iconv_strpos(&retval, haystk, haystk_len, ndl, ndl_len,
  1855. -1, charset);
  1856. _php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset TSRMLS_CC);
  1857. if (err == PHP_ICONV_ERR_SUCCESS && retval != (unsigned int)-1) {
  1858. RETVAL_LONG((long)retval);
  1859. } else {
  1860. RETVAL_FALSE;
  1861. }
  1862. }
  1863. /* }}} */
  1864. /* {{{ proto string iconv_mime_encode(string field_name, string field_value [, array preference])
  1865. Composes a mime header field with field_name and field_value in a specified scheme */
  1866. PHP_FUNCTION(iconv_mime_encode)
  1867. {
  1868. const char *field_name = NULL;
  1869. int field_name_len;
  1870. const char *field_value = NULL;
  1871. int field_value_len;
  1872. zval *pref = NULL;
  1873. zval tmp_zv, *tmp_zv_p = NULL;
  1874. smart_str retval = {0};
  1875. php_iconv_err_t err;
  1876. const char *in_charset = get_internal_encoding(TSRMLS_C);
  1877. const char *out_charset = in_charset;
  1878. long line_len = 76;
  1879. const char *lfchars = "\r\n";
  1880. php_iconv_enc_scheme_t scheme_id = PHP_ICONV_ENC_SCHEME_BASE64;
  1881. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a",
  1882. &field_name, &field_name_len, &field_value, &field_value_len,
  1883. &pref) == FAILURE) {
  1884. RETURN_FALSE;
  1885. }
  1886. if (pref != NULL) {
  1887. zval **ppval;
  1888. if (zend_hash_find(Z_ARRVAL_P(pref), "scheme", sizeof("scheme"), (void **)&ppval) == SUCCESS) {
  1889. if (Z_TYPE_PP(ppval) == IS_STRING && Z_STRLEN_PP(ppval) > 0) {
  1890. switch (Z_STRVAL_PP(ppval)[0]) {
  1891. case 'B': case 'b':
  1892. scheme_id = PHP_ICONV_ENC_SCHEME_BASE64;
  1893. break;
  1894. case 'Q': case 'q':
  1895. scheme_id = PHP_ICONV_ENC_SCHEME_QPRINT;
  1896. break;
  1897. }
  1898. }
  1899. }
  1900. if (zend_hash_find(Z_ARRVAL_P(pref), "input-charset", sizeof("input-charset"), (void **)&ppval) == SUCCESS) {
  1901. if (Z_STRLEN_PP(ppval) >= ICONV_CSNMAXLEN) {
  1902. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1903. RETURN_FALSE;
  1904. }
  1905. if (Z_TYPE_PP(ppval) == IS_STRING && Z_STRLEN_PP(ppval) > 0) {
  1906. in_charset = Z_STRVAL_PP(ppval);
  1907. }
  1908. }
  1909. if (zend_hash_find(Z_ARRVAL_P(pref), "output-charset", sizeof("output-charset"), (void **)&ppval) == SUCCESS) {
  1910. if (Z_STRLEN_PP(ppval) >= ICONV_CSNMAXLEN) {
  1911. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1912. RETURN_FALSE;
  1913. }
  1914. if (Z_TYPE_PP(ppval) == IS_STRING && Z_STRLEN_PP(ppval) > 0) {
  1915. out_charset = Z_STRVAL_PP(ppval);
  1916. }
  1917. }
  1918. if (zend_hash_find(Z_ARRVAL_P(pref), "line-length", sizeof("line-length"), (void **)&ppval) == SUCCESS) {
  1919. zval val, *pval = *ppval;
  1920. if (Z_TYPE_P(pval) != IS_LONG) {
  1921. val = *pval;
  1922. zval_copy_ctor(&val);
  1923. convert_to_long(&val);
  1924. pval = &val;
  1925. }
  1926. line_len = Z_LVAL_P(pval);
  1927. if (pval == &val) {
  1928. zval_dtor(&val);
  1929. }
  1930. }
  1931. if (zend_hash_find(Z_ARRVAL_P(pref), "line-break-chars", sizeof("line-break-chars"), (void **)&ppval) == SUCCESS) {
  1932. if (Z_TYPE_PP(ppval) != IS_STRING) {
  1933. tmp_zv = **ppval;
  1934. zval_copy_ctor(&tmp_zv);
  1935. convert_to_string(&tmp_zv);
  1936. lfchars = Z_STRVAL(tmp_zv);
  1937. tmp_zv_p = &tmp_zv;
  1938. } else {
  1939. lfchars = Z_STRVAL_PP(ppval);
  1940. }
  1941. }
  1942. }
  1943. err = _php_iconv_mime_encode(&retval, field_name, field_name_len,
  1944. field_value, field_value_len, line_len, lfchars, scheme_id,
  1945. out_charset, in_charset);
  1946. _php_iconv_show_error(err, out_charset, in_charset TSRMLS_CC);
  1947. if (err == PHP_ICONV_ERR_SUCCESS) {
  1948. if (retval.c != NULL) {
  1949. RETVAL_STRINGL(retval.c, retval.len, 0);
  1950. } else {
  1951. RETVAL_EMPTY_STRING();
  1952. }
  1953. } else {
  1954. smart_str_free(&retval);
  1955. RETVAL_FALSE;
  1956. }
  1957. if (tmp_zv_p != NULL) {
  1958. zval_dtor(tmp_zv_p);
  1959. }
  1960. }
  1961. /* }}} */
  1962. /* {{{ proto string iconv_mime_decode(string encoded_string [, int mode, string charset])
  1963. Decodes a mime header field */
  1964. PHP_FUNCTION(iconv_mime_decode)
  1965. {
  1966. char *encoded_str;
  1967. int encoded_str_len;
  1968. char *charset = get_internal_encoding(TSRMLS_C);
  1969. int charset_len = 0;
  1970. long mode = 0;
  1971. smart_str retval = {0};
  1972. php_iconv_err_t err;
  1973. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls",
  1974. &encoded_str, &encoded_str_len, &mode, &charset, &charset_len) == FAILURE) {
  1975. RETURN_FALSE;
  1976. }
  1977. if (charset_len >= ICONV_CSNMAXLEN) {
  1978. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  1979. RETURN_FALSE;
  1980. }
  1981. err = _php_iconv_mime_decode(&retval, encoded_str, encoded_str_len, charset, NULL, mode);
  1982. _php_iconv_show_error(err, charset, "???" TSRMLS_CC);
  1983. if (err == PHP_ICONV_ERR_SUCCESS) {
  1984. if (retval.c != NULL) {
  1985. RETVAL_STRINGL(retval.c, retval.len, 0);
  1986. } else {
  1987. RETVAL_EMPTY_STRING();
  1988. }
  1989. } else {
  1990. smart_str_free(&retval);
  1991. RETVAL_FALSE;
  1992. }
  1993. }
  1994. /* }}} */
  1995. /* {{{ proto array iconv_mime_decode_headers(string headers [, int mode, string charset])
  1996. Decodes multiple mime header fields */
  1997. PHP_FUNCTION(iconv_mime_decode_headers)
  1998. {
  1999. const char *encoded_str;
  2000. int encoded_str_len;
  2001. char *charset = get_internal_encoding(TSRMLS_C);
  2002. int charset_len = 0;
  2003. long mode = 0;
  2004. php_iconv_err_t err = PHP_ICONV_ERR_SUCCESS;
  2005. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls",
  2006. &encoded_str, &encoded_str_len, &mode, &charset, &charset_len) == FAILURE) {
  2007. RETURN_FALSE;
  2008. }
  2009. if (charset_len >= ICONV_CSNMAXLEN) {
  2010. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  2011. RETURN_FALSE;
  2012. }
  2013. array_init(return_value);
  2014. while (encoded_str_len > 0) {
  2015. smart_str decoded_header = {0};
  2016. char *header_name = NULL;
  2017. size_t header_name_len = 0;
  2018. char *header_value = NULL;
  2019. size_t header_value_len = 0;
  2020. char *p, *limit;
  2021. const char *next_pos;
  2022. if (PHP_ICONV_ERR_SUCCESS != (err = _php_iconv_mime_decode(&decoded_header, encoded_str, encoded_str_len, charset, &next_pos, mode))) {
  2023. smart_str_free(&decoded_header);
  2024. break;
  2025. }
  2026. if (decoded_header.c == NULL) {
  2027. break;
  2028. }
  2029. limit = decoded_header.c + decoded_header.len;
  2030. for (p = decoded_header.c; p < limit; p++) {
  2031. if (*p == ':') {
  2032. *p = '\0';
  2033. header_name = decoded_header.c;
  2034. header_name_len = (p - decoded_header.c) + 1;
  2035. while (++p < limit) {
  2036. if (*p != ' ' && *p != '\t') {
  2037. break;
  2038. }
  2039. }
  2040. header_value = p;
  2041. header_value_len = limit - p;
  2042. break;
  2043. }
  2044. }
  2045. if (header_name != NULL) {
  2046. zval **elem, *new_elem;
  2047. if (zend_hash_find(Z_ARRVAL_P(return_value), header_name, header_name_len, (void **)&elem) == SUCCESS) {
  2048. if (Z_TYPE_PP(elem) != IS_ARRAY) {
  2049. MAKE_STD_ZVAL(new_elem);
  2050. array_init(new_elem);
  2051. Z_ADDREF_PP(elem);
  2052. add_next_index_zval(new_elem, *elem);
  2053. zend_hash_update(Z_ARRVAL_P(return_value), header_name, header_name_len, (void *)&new_elem, sizeof(new_elem), NULL);
  2054. elem = &new_elem;
  2055. }
  2056. add_next_index_stringl(*elem, header_value, header_value_len, 1);
  2057. } else {
  2058. add_assoc_stringl_ex(return_value, header_name, header_name_len, header_value, header_value_len, 1);
  2059. }
  2060. }
  2061. encoded_str_len -= next_pos - encoded_str;
  2062. encoded_str = next_pos;
  2063. smart_str_free(&decoded_header);
  2064. }
  2065. if (err != PHP_ICONV_ERR_SUCCESS) {
  2066. _php_iconv_show_error(err, charset, "???" TSRMLS_CC);
  2067. zval_dtor(return_value);
  2068. RETVAL_FALSE;
  2069. }
  2070. }
  2071. /* }}} */
  2072. /* {{{ proto string iconv(string in_charset, string out_charset, string str)
  2073. Returns str converted to the out_charset character set */
  2074. PHP_NAMED_FUNCTION(php_if_iconv)
  2075. {
  2076. char *in_charset, *out_charset, *in_buffer, *out_buffer;
  2077. size_t out_len;
  2078. int in_charset_len = 0, out_charset_len = 0, in_buffer_len;
  2079. php_iconv_err_t err;
  2080. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss",
  2081. &in_charset, &in_charset_len, &out_charset, &out_charset_len, &in_buffer, &in_buffer_len) == FAILURE)
  2082. return;
  2083. if (in_charset_len >= ICONV_CSNMAXLEN || out_charset_len >= ICONV_CSNMAXLEN) {
  2084. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  2085. RETURN_FALSE;
  2086. }
  2087. err = php_iconv_string(in_buffer, (size_t)in_buffer_len,
  2088. &out_buffer, &out_len, out_charset, in_charset);
  2089. _php_iconv_show_error(err, out_charset, in_charset TSRMLS_CC);
  2090. if (err == PHP_ICONV_ERR_SUCCESS && out_buffer != NULL) {
  2091. RETVAL_STRINGL_CHECK(out_buffer, out_len, 0);
  2092. } else {
  2093. if (out_buffer != NULL) {
  2094. efree(out_buffer);
  2095. }
  2096. RETURN_FALSE;
  2097. }
  2098. }
  2099. /* }}} */
  2100. /* {{{ proto bool iconv_set_encoding(string type, string charset)
  2101. Sets internal encoding and output encoding for ob_iconv_handler() */
  2102. PHP_FUNCTION(iconv_set_encoding)
  2103. {
  2104. char *type, *charset;
  2105. int type_len, charset_len =0, retval;
  2106. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &type, &type_len, &charset, &charset_len) == FAILURE)
  2107. return;
  2108. if (charset_len >= ICONV_CSNMAXLEN) {
  2109. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
  2110. RETURN_FALSE;
  2111. }
  2112. if(!strcasecmp("input_encoding", type)) {
  2113. retval = zend_alter_ini_entry("iconv.input_encoding", sizeof("iconv.input_encoding"), charset, charset_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  2114. } else if(!strcasecmp("output_encoding", type)) {
  2115. retval = zend_alter_ini_entry("iconv.output_encoding", sizeof("iconv.output_encoding"), charset, charset_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  2116. } else if(!strcasecmp("internal_encoding", type)) {
  2117. retval = zend_alter_ini_entry("iconv.internal_encoding", sizeof("iconv.internal_encoding"), charset, charset_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  2118. } else {
  2119. RETURN_FALSE;
  2120. }
  2121. if (retval == SUCCESS) {
  2122. RETURN_TRUE;
  2123. } else {
  2124. RETURN_FALSE;
  2125. }
  2126. }
  2127. /* }}} */
  2128. /* {{{ proto mixed iconv_get_encoding([string type])
  2129. Get internal encoding and output encoding for ob_iconv_handler() */
  2130. PHP_FUNCTION(iconv_get_encoding)
  2131. {
  2132. char *type = "all";
  2133. int type_len = sizeof("all")-1;
  2134. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &type, &type_len) == FAILURE)
  2135. return;
  2136. if (!strcasecmp("all", type)) {
  2137. array_init(return_value);
  2138. add_assoc_string(return_value, "input_encoding", get_input_encoding(TSRMLS_C), 1);
  2139. add_assoc_string(return_value, "output_encoding", get_output_encoding(TSRMLS_C), 1);
  2140. add_assoc_string(return_value, "internal_encoding", get_internal_encoding(TSRMLS_C), 1);
  2141. } else if (!strcasecmp("input_encoding", type)) {
  2142. RETVAL_STRING(get_input_encoding(TSRMLS_C), 1);
  2143. } else if (!strcasecmp("output_encoding", type)) {
  2144. RETVAL_STRING(get_output_encoding(TSRMLS_C), 1);
  2145. } else if (!strcasecmp("internal_encoding", type)) {
  2146. RETVAL_STRING(get_internal_encoding(TSRMLS_C), 1);
  2147. } else {
  2148. RETURN_FALSE;
  2149. }
  2150. }
  2151. /* }}} */
  2152. /* {{{ iconv stream filter */
  2153. typedef struct _php_iconv_stream_filter {
  2154. iconv_t cd;
  2155. int persistent;
  2156. char *to_charset;
  2157. size_t to_charset_len;
  2158. char *from_charset;
  2159. size_t from_charset_len;
  2160. char stub[128];
  2161. size_t stub_len;
  2162. } php_iconv_stream_filter;
  2163. /* }}} iconv stream filter */
  2164. /* {{{ php_iconv_stream_filter_dtor */
  2165. static void php_iconv_stream_filter_dtor(php_iconv_stream_filter *self)
  2166. {
  2167. iconv_close(self->cd);
  2168. pefree(self->to_charset, self->persistent);
  2169. pefree(self->from_charset, self->persistent);
  2170. }
  2171. /* }}} */
  2172. /* {{{ php_iconv_stream_filter_ctor() */
  2173. static php_iconv_err_t php_iconv_stream_filter_ctor(php_iconv_stream_filter *self,
  2174. const char *to_charset, size_t to_charset_len,
  2175. const char *from_charset, size_t from_charset_len, int persistent)
  2176. {
  2177. if (NULL == (self->to_charset = pemalloc(to_charset_len + 1, persistent))) {
  2178. return PHP_ICONV_ERR_ALLOC;
  2179. }
  2180. self->to_charset_len = to_charset_len;
  2181. if (NULL == (self->from_charset = pemalloc(from_charset_len + 1, persistent))) {
  2182. pefree(self->to_charset, persistent);
  2183. return PHP_ICONV_ERR_ALLOC;
  2184. }
  2185. self->from_charset_len = from_charset_len;
  2186. memcpy(self->to_charset, to_charset, to_charset_len);
  2187. self->to_charset[to_charset_len] = '\0';
  2188. memcpy(self->from_charset, from_charset, from_charset_len);
  2189. self->from_charset[from_charset_len] = '\0';
  2190. if ((iconv_t)-1 == (self->cd = iconv_open(self->to_charset, self->from_charset))) {
  2191. pefree(self->from_charset, persistent);
  2192. pefree(self->to_charset, persistent);
  2193. return PHP_ICONV_ERR_UNKNOWN;
  2194. }
  2195. self->persistent = persistent;
  2196. self->stub_len = 0;
  2197. return PHP_ICONV_ERR_SUCCESS;
  2198. }
  2199. /* }}} */
  2200. /* {{{ php_iconv_stream_filter_append_bucket */
  2201. static int php_iconv_stream_filter_append_bucket(
  2202. php_iconv_stream_filter *self,
  2203. php_stream *stream, php_stream_filter *filter,
  2204. php_stream_bucket_brigade *buckets_out,
  2205. const char *ps, size_t buf_len, size_t *consumed,
  2206. int persistent TSRMLS_DC)
  2207. {
  2208. php_stream_bucket *new_bucket;
  2209. char *out_buf = NULL;
  2210. size_t out_buf_size;
  2211. char *pd, *pt;
  2212. size_t ocnt, prev_ocnt, icnt, tcnt;
  2213. size_t initial_out_buf_size;
  2214. if (ps == NULL) {
  2215. initial_out_buf_size = 64;
  2216. icnt = 1;
  2217. } else {
  2218. initial_out_buf_size = buf_len;
  2219. icnt = buf_len;
  2220. }
  2221. out_buf_size = ocnt = prev_ocnt = initial_out_buf_size;
  2222. if (NULL == (out_buf = pemalloc(out_buf_size, persistent))) {
  2223. return FAILURE;
  2224. }
  2225. pd = out_buf;
  2226. if (self->stub_len > 0) {
  2227. pt = self->stub;
  2228. tcnt = self->stub_len;
  2229. while (tcnt > 0) {
  2230. if (iconv(self->cd, &pt, &tcnt, &pd, &ocnt) == (size_t)-1) {
  2231. #if ICONV_SUPPORTS_ERRNO
  2232. switch (errno) {
  2233. case EILSEQ:
  2234. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): invalid multibyte sequence", self->from_charset, self->to_charset);
  2235. goto out_failure;
  2236. case EINVAL:
  2237. if (ps != NULL) {
  2238. if (icnt > 0) {
  2239. if (self->stub_len >= sizeof(self->stub)) {
  2240. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): insufficient buffer", self->from_charset, self->to_charset);
  2241. goto out_failure;
  2242. }
  2243. self->stub[self->stub_len++] = *(ps++);
  2244. icnt--;
  2245. pt = self->stub;
  2246. tcnt = self->stub_len;
  2247. } else {
  2248. tcnt = 0;
  2249. break;
  2250. }
  2251. } else {
  2252. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): invalid multibyte sequence", self->from_charset, self->to_charset);
  2253. goto out_failure;
  2254. }
  2255. break;
  2256. case E2BIG: {
  2257. char *new_out_buf;
  2258. size_t new_out_buf_size;
  2259. new_out_buf_size = out_buf_size << 1;
  2260. if (new_out_buf_size < out_buf_size) {
  2261. /* whoa! no bigger buckets are sold anywhere... */
  2262. if (NULL == (new_bucket = php_stream_bucket_new(stream, out_buf, (out_buf_size - ocnt), 1, persistent TSRMLS_CC))) {
  2263. goto out_failure;
  2264. }
  2265. php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);
  2266. out_buf_size = ocnt = initial_out_buf_size;
  2267. if (NULL == (out_buf = pemalloc(out_buf_size, persistent))) {
  2268. return FAILURE;
  2269. }
  2270. pd = out_buf;
  2271. } else {
  2272. if (NULL == (new_out_buf = perealloc(out_buf, new_out_buf_size, persistent))) {
  2273. if (NULL == (new_bucket = php_stream_bucket_new(stream, out_buf, (out_buf_size - ocnt), 1, persistent TSRMLS_CC))) {
  2274. goto out_failure;
  2275. }
  2276. php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);
  2277. return FAILURE;
  2278. }
  2279. pd = new_out_buf + (pd - out_buf);
  2280. ocnt += (new_out_buf_size - out_buf_size);
  2281. out_buf = new_out_buf;
  2282. out_buf_size = new_out_buf_size;
  2283. }
  2284. } break;
  2285. default:
  2286. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): unknown error", self->from_charset, self->to_charset);
  2287. goto out_failure;
  2288. }
  2289. #else
  2290. if (ocnt == prev_ocnt) {
  2291. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): unknown error", self->from_charset, self->to_charset);
  2292. goto out_failure;
  2293. }
  2294. #endif
  2295. }
  2296. prev_ocnt = ocnt;
  2297. }
  2298. memmove(self->stub, pt, tcnt);
  2299. self->stub_len = tcnt;
  2300. }
  2301. while (icnt > 0) {
  2302. if ((ps == NULL ? iconv(self->cd, NULL, NULL, &pd, &ocnt):
  2303. iconv(self->cd, (char **)&ps, &icnt, &pd, &ocnt)) == (size_t)-1) {
  2304. #if ICONV_SUPPORTS_ERRNO
  2305. switch (errno) {
  2306. case EILSEQ:
  2307. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): invalid multibyte sequence", self->from_charset, self->to_charset);
  2308. goto out_failure;
  2309. case EINVAL:
  2310. if (ps != NULL) {
  2311. if (icnt > sizeof(self->stub)) {
  2312. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): insufficient buffer", self->from_charset, self->to_charset);
  2313. goto out_failure;
  2314. }
  2315. memcpy(self->stub, ps, icnt);
  2316. self->stub_len = icnt;
  2317. ps += icnt;
  2318. icnt = 0;
  2319. } else {
  2320. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): unexpected octet values", self->from_charset, self->to_charset);
  2321. goto out_failure;
  2322. }
  2323. break;
  2324. case E2BIG: {
  2325. char *new_out_buf;
  2326. size_t new_out_buf_size;
  2327. new_out_buf_size = out_buf_size << 1;
  2328. if (new_out_buf_size < out_buf_size) {
  2329. /* whoa! no bigger buckets are sold anywhere... */
  2330. if (NULL == (new_bucket = php_stream_bucket_new(stream, out_buf, (out_buf_size - ocnt), 1, persistent TSRMLS_CC))) {
  2331. goto out_failure;
  2332. }
  2333. php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);
  2334. out_buf_size = ocnt = initial_out_buf_size;
  2335. if (NULL == (out_buf = pemalloc(out_buf_size, persistent))) {
  2336. return FAILURE;
  2337. }
  2338. pd = out_buf;
  2339. } else {
  2340. if (NULL == (new_out_buf = perealloc(out_buf, new_out_buf_size, persistent))) {
  2341. if (NULL == (new_bucket = php_stream_bucket_new(stream, out_buf, (out_buf_size - ocnt), 1, persistent TSRMLS_CC))) {
  2342. goto out_failure;
  2343. }
  2344. php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);
  2345. return FAILURE;
  2346. }
  2347. pd = new_out_buf + (pd - out_buf);
  2348. ocnt += (new_out_buf_size - out_buf_size);
  2349. out_buf = new_out_buf;
  2350. out_buf_size = new_out_buf_size;
  2351. }
  2352. } break;
  2353. default:
  2354. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): unknown error", self->from_charset, self->to_charset);
  2355. goto out_failure;
  2356. }
  2357. #else
  2358. if (ocnt == prev_ocnt) {
  2359. php_error_docref(NULL TSRMLS_CC, E_WARNING, "iconv stream filter (\"%s\"=>\"%s\"): unknown error", self->from_charset, self->to_charset);
  2360. goto out_failure;
  2361. }
  2362. #endif
  2363. } else {
  2364. if (ps == NULL) {
  2365. break;
  2366. }
  2367. }
  2368. prev_ocnt = ocnt;
  2369. }
  2370. if (out_buf_size - ocnt > 0) {
  2371. if (NULL == (new_bucket = php_stream_bucket_new(stream, out_buf, (out_buf_size - ocnt), 1, persistent TSRMLS_CC))) {
  2372. goto out_failure;
  2373. }
  2374. php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);
  2375. } else {
  2376. pefree(out_buf, persistent);
  2377. }
  2378. *consumed += buf_len - icnt;
  2379. return SUCCESS;
  2380. out_failure:
  2381. pefree(out_buf, persistent);
  2382. return FAILURE;
  2383. }
  2384. /* }}} php_iconv_stream_filter_append_bucket */
  2385. /* {{{ php_iconv_stream_filter_do_filter */
  2386. static php_stream_filter_status_t php_iconv_stream_filter_do_filter(
  2387. php_stream *stream, php_stream_filter *filter,
  2388. php_stream_bucket_brigade *buckets_in,
  2389. php_stream_bucket_brigade *buckets_out,
  2390. size_t *bytes_consumed, int flags TSRMLS_DC)
  2391. {
  2392. php_stream_bucket *bucket = NULL;
  2393. size_t consumed = 0;
  2394. php_iconv_stream_filter *self = (php_iconv_stream_filter *)filter->abstract;
  2395. while (buckets_in->head != NULL) {
  2396. bucket = buckets_in->head;
  2397. php_stream_bucket_unlink(bucket TSRMLS_CC);
  2398. if (php_iconv_stream_filter_append_bucket(self, stream, filter,
  2399. buckets_out, bucket->buf, bucket->buflen, &consumed,
  2400. php_stream_is_persistent(stream) TSRMLS_CC) != SUCCESS) {
  2401. goto out_failure;
  2402. }
  2403. php_stream_bucket_delref(bucket TSRMLS_CC);
  2404. }
  2405. if (flags != PSFS_FLAG_NORMAL) {
  2406. if (php_iconv_stream_filter_append_bucket(self, stream, filter,
  2407. buckets_out, NULL, 0, &consumed,
  2408. php_stream_is_persistent(stream) TSRMLS_CC) != SUCCESS) {
  2409. goto out_failure;
  2410. }
  2411. }
  2412. if (bytes_consumed != NULL) {
  2413. *bytes_consumed = consumed;
  2414. }
  2415. return PSFS_PASS_ON;
  2416. out_failure:
  2417. if (bucket != NULL) {
  2418. php_stream_bucket_delref(bucket TSRMLS_CC);
  2419. }
  2420. return PSFS_ERR_FATAL;
  2421. }
  2422. /* }}} */
  2423. /* {{{ php_iconv_stream_filter_cleanup */
  2424. static void php_iconv_stream_filter_cleanup(php_stream_filter *filter TSRMLS_DC)
  2425. {
  2426. php_iconv_stream_filter_dtor((php_iconv_stream_filter *)filter->abstract);
  2427. pefree(filter->abstract, ((php_iconv_stream_filter *)filter->abstract)->persistent);
  2428. }
  2429. /* }}} */
  2430. static php_stream_filter_ops php_iconv_stream_filter_ops = {
  2431. php_iconv_stream_filter_do_filter,
  2432. php_iconv_stream_filter_cleanup,
  2433. "convert.iconv.*"
  2434. };
  2435. /* {{{ php_iconv_stream_filter_create */
  2436. static php_stream_filter *php_iconv_stream_filter_factory_create(const char *name, zval *params, int persistent TSRMLS_DC)
  2437. {
  2438. php_stream_filter *retval = NULL;
  2439. php_iconv_stream_filter *inst;
  2440. char *from_charset = NULL, *to_charset = NULL;
  2441. size_t from_charset_len, to_charset_len;
  2442. if ((from_charset = strchr(name, '.')) == NULL) {
  2443. return NULL;
  2444. }
  2445. ++from_charset;
  2446. if ((from_charset = strchr(from_charset, '.')) == NULL) {
  2447. return NULL;
  2448. }
  2449. ++from_charset;
  2450. if ((to_charset = strpbrk(from_charset, "/.")) == NULL) {
  2451. return NULL;
  2452. }
  2453. from_charset_len = to_charset - from_charset;
  2454. ++to_charset;
  2455. to_charset_len = strlen(to_charset);
  2456. if (from_charset_len >= ICONV_CSNMAXLEN || to_charset_len >= ICONV_CSNMAXLEN) {
  2457. return NULL;
  2458. }
  2459. if (NULL == (inst = pemalloc(sizeof(php_iconv_stream_filter), persistent))) {
  2460. return NULL;
  2461. }
  2462. if (php_iconv_stream_filter_ctor(inst, to_charset, to_charset_len, from_charset, from_charset_len, persistent) != PHP_ICONV_ERR_SUCCESS) {
  2463. pefree(inst, persistent);
  2464. return NULL;
  2465. }
  2466. if (NULL == (retval = php_stream_filter_alloc(&php_iconv_stream_filter_ops, inst, persistent))) {
  2467. php_iconv_stream_filter_dtor(inst);
  2468. pefree(inst, persistent);
  2469. }
  2470. return retval;
  2471. }
  2472. /* }}} */
  2473. /* {{{ php_iconv_stream_register_factory */
  2474. static php_iconv_err_t php_iconv_stream_filter_register_factory(TSRMLS_D)
  2475. {
  2476. static php_stream_filter_factory filter_factory = {
  2477. php_iconv_stream_filter_factory_create
  2478. };
  2479. if (FAILURE == php_stream_filter_register_factory(
  2480. php_iconv_stream_filter_ops.label,
  2481. &filter_factory TSRMLS_CC)) {
  2482. return PHP_ICONV_ERR_UNKNOWN;
  2483. }
  2484. return PHP_ICONV_ERR_SUCCESS;
  2485. }
  2486. /* }}} */
  2487. /* {{{ php_iconv_stream_unregister_factory */
  2488. static php_iconv_err_t php_iconv_stream_filter_unregister_factory(TSRMLS_D)
  2489. {
  2490. if (FAILURE == php_stream_filter_unregister_factory(
  2491. php_iconv_stream_filter_ops.label TSRMLS_CC)) {
  2492. return PHP_ICONV_ERR_UNKNOWN;
  2493. }
  2494. return PHP_ICONV_ERR_SUCCESS;
  2495. }
  2496. /* }}} */
  2497. /* }}} */
  2498. #endif
  2499. /*
  2500. * Local variables:
  2501. * tab-width: 4
  2502. * c-basic-offset: 4
  2503. * End:
  2504. * vim600: sw=4 ts=4 fdm=marker
  2505. * vim<600: sw=4 ts=4
  2506. */