archive_string.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237
  1. /*-
  2. * Copyright (c) 2003-2011 Tim Kientzle
  3. * Copyright (c) 2011 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_string.c 201095 2009-12-28 02:33:22Z kientzle $");
  28. /*
  29. * Basic resizable string support, to simplify manipulating arbitrary-sized
  30. * strings while minimizing heap activity.
  31. *
  32. * In particular, the buffer used by a string object is only grown, it
  33. * never shrinks, so you can clear and reuse the same string object
  34. * without incurring additional memory allocations.
  35. */
  36. #ifdef HAVE_ERRNO_H
  37. #include <errno.h>
  38. #endif
  39. #ifdef HAVE_ICONV_H
  40. #include <iconv.h>
  41. #endif
  42. #ifdef HAVE_LANGINFO_H
  43. #include <langinfo.h>
  44. #endif
  45. #ifdef HAVE_LOCALCHARSET_H
  46. #include <localcharset.h>
  47. #endif
  48. #ifdef HAVE_STDLIB_H
  49. #include <stdlib.h>
  50. #endif
  51. #ifdef HAVE_STRING_H
  52. #include <string.h>
  53. #endif
  54. #ifdef HAVE_WCHAR_H
  55. #include <wchar.h>
  56. #endif
  57. #if defined(_WIN32) && !defined(__CYGWIN__)
  58. #include <windows.h>
  59. #include <locale.h>
  60. #endif
  61. #if defined(__APPLE__)
  62. #include <CoreServices/CoreServices.h>
  63. #endif
  64. #include "archive_endian.h"
  65. #include "archive_private.h"
  66. #include "archive_string.h"
  67. #include "archive_string_composition.h"
  68. #if !defined(HAVE_WMEMCPY) && !defined(wmemcpy)
  69. #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
  70. #endif
  71. struct archive_string_conv {
  72. struct archive_string_conv *next;
  73. char *from_charset;
  74. char *to_charset;
  75. unsigned from_cp;
  76. unsigned to_cp;
  77. /* Set 1 if from_charset and to_charset are the same. */
  78. int same;
  79. int flag;
  80. #define SCONV_TO_CHARSET 1 /* MBS is being converted to specified
  81. * charset. */
  82. #define SCONV_FROM_CHARSET (1<<1) /* MBS is being converted from
  83. * specified charset. */
  84. #define SCONV_BEST_EFFORT (1<<2) /* Copy at least ASCII code. */
  85. #define SCONV_WIN_CP (1<<3) /* Use Windows API for converting
  86. * MBS. */
  87. #define SCONV_UTF8_LIBARCHIVE_2 (1<<4) /* Incorrect UTF-8 made by libarchive
  88. * 2.x in the wrong assumption. */
  89. #define SCONV_NORMALIZATION_C (1<<6) /* Need normalization to be Form C.
  90. * Before UTF-8 characters are actually
  91. * processed. */
  92. #define SCONV_NORMALIZATION_D (1<<7) /* Need normalization to be Form D.
  93. * Before UTF-8 characters are actually
  94. * processed.
  95. * Currently this only for MAC OS X. */
  96. #define SCONV_TO_UTF8 (1<<8) /* "to charset" side is UTF-8. */
  97. #define SCONV_FROM_UTF8 (1<<9) /* "from charset" side is UTF-8. */
  98. #define SCONV_TO_UTF16BE (1<<10) /* "to charset" side is UTF-16BE. */
  99. #define SCONV_FROM_UTF16BE (1<<11) /* "from charset" side is UTF-16BE. */
  100. #define SCONV_TO_UTF16LE (1<<12) /* "to charset" side is UTF-16LE. */
  101. #define SCONV_FROM_UTF16LE (1<<13) /* "from charset" side is UTF-16LE. */
  102. #define SCONV_TO_UTF16 (SCONV_TO_UTF16BE | SCONV_TO_UTF16LE)
  103. #define SCONV_FROM_UTF16 (SCONV_FROM_UTF16BE | SCONV_FROM_UTF16LE)
  104. #if HAVE_ICONV
  105. iconv_t cd;
  106. iconv_t cd_w;/* Use at archive_mstring on
  107. * Windows. */
  108. #endif
  109. /* A temporary buffer for normalization. */
  110. struct archive_string utftmp;
  111. #if defined(__APPLE__)
  112. UnicodeToTextInfo uniInfo;
  113. struct archive_string utf16nfc;
  114. struct archive_string utf16nfd;
  115. #endif
  116. int (*converter[2])(struct archive_string *, const void *, size_t,
  117. struct archive_string_conv *);
  118. int nconverter;
  119. };
  120. #define CP_C_LOCALE 0 /* "C" locale only for this file. */
  121. #define CP_UTF16LE 1200
  122. #define CP_UTF16BE 1201
  123. #define IS_HIGH_SURROGATE_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDBFF)
  124. #define IS_LOW_SURROGATE_LA(uc) ((uc) >= 0xDC00 && (uc) <= 0xDFFF)
  125. #define IS_SURROGATE_PAIR_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDFFF)
  126. #define UNICODE_MAX 0x10FFFF
  127. #define UNICODE_R_CHAR 0xFFFD /* Replacement character. */
  128. /* Set U+FFFD(Replacement character) in UTF-8. */
  129. #define UTF8_SET_R_CHAR(outp) do { \
  130. (outp)[0] = 0xef; \
  131. (outp)[1] = 0xbf; \
  132. (outp)[2] = 0xbd; \
  133. } while (0)
  134. #define UTF8_R_CHAR_SIZE 3
  135. static struct archive_string_conv *find_sconv_object(struct archive *,
  136. const char *, const char *);
  137. static void add_sconv_object(struct archive *, struct archive_string_conv *);
  138. static struct archive_string_conv *create_sconv_object(const char *,
  139. const char *, unsigned, int);
  140. static void free_sconv_object(struct archive_string_conv *);
  141. static struct archive_string_conv *get_sconv_object(struct archive *,
  142. const char *, const char *, int);
  143. static unsigned make_codepage_from_charset(const char *);
  144. static unsigned get_current_codepage(void);
  145. static unsigned get_current_oemcp(void);
  146. static size_t mbsnbytes(const void *, size_t);
  147. static size_t utf16nbytes(const void *, size_t);
  148. #if defined(_WIN32) && !defined(__CYGWIN__)
  149. static int archive_wstring_append_from_mbs_in_codepage(
  150. struct archive_wstring *, const char *, size_t,
  151. struct archive_string_conv *);
  152. static int archive_string_append_from_wcs_in_codepage(struct archive_string *,
  153. const wchar_t *, size_t, struct archive_string_conv *);
  154. static int is_big_endian(void);
  155. static int strncat_in_codepage(struct archive_string *, const void *,
  156. size_t, struct archive_string_conv *);
  157. static int win_strncat_from_utf16be(struct archive_string *, const void *, size_t,
  158. struct archive_string_conv *);
  159. static int win_strncat_from_utf16le(struct archive_string *, const void *, size_t,
  160. struct archive_string_conv *);
  161. static int win_strncat_to_utf16be(struct archive_string *, const void *, size_t,
  162. struct archive_string_conv *);
  163. static int win_strncat_to_utf16le(struct archive_string *, const void *, size_t,
  164. struct archive_string_conv *);
  165. #endif
  166. static int best_effort_strncat_from_utf16be(struct archive_string *, const void *,
  167. size_t, struct archive_string_conv *);
  168. static int best_effort_strncat_from_utf16le(struct archive_string *, const void *,
  169. size_t, struct archive_string_conv *);
  170. static int best_effort_strncat_to_utf16be(struct archive_string *, const void *,
  171. size_t, struct archive_string_conv *);
  172. static int best_effort_strncat_to_utf16le(struct archive_string *, const void *,
  173. size_t, struct archive_string_conv *);
  174. #if defined(HAVE_ICONV)
  175. static int iconv_strncat_in_locale(struct archive_string *, const void *,
  176. size_t, struct archive_string_conv *);
  177. #endif
  178. static int best_effort_strncat_in_locale(struct archive_string *, const void *,
  179. size_t, struct archive_string_conv *);
  180. static int _utf8_to_unicode(uint32_t *, const char *, size_t);
  181. static int utf8_to_unicode(uint32_t *, const char *, size_t);
  182. static inline uint32_t combine_surrogate_pair(uint32_t, uint32_t);
  183. static int cesu8_to_unicode(uint32_t *, const char *, size_t);
  184. static size_t unicode_to_utf8(char *, size_t, uint32_t);
  185. static int utf16_to_unicode(uint32_t *, const char *, size_t, int);
  186. static size_t unicode_to_utf16be(char *, size_t, uint32_t);
  187. static size_t unicode_to_utf16le(char *, size_t, uint32_t);
  188. static int strncat_from_utf8_libarchive2(struct archive_string *,
  189. const void *, size_t, struct archive_string_conv *);
  190. static int strncat_from_utf8_to_utf8(struct archive_string *, const void *,
  191. size_t, struct archive_string_conv *);
  192. static int archive_string_normalize_C(struct archive_string *, const void *,
  193. size_t, struct archive_string_conv *);
  194. #if defined(__APPLE__)
  195. static int archive_string_normalize_D(struct archive_string *, const void *,
  196. size_t, struct archive_string_conv *);
  197. #endif
  198. static int archive_string_append_unicode(struct archive_string *,
  199. const void *, size_t, struct archive_string_conv *);
  200. static struct archive_string *
  201. archive_string_append(struct archive_string *as, const char *p, size_t s)
  202. {
  203. if (archive_string_ensure(as, as->length + s + 1) == NULL)
  204. return (NULL);
  205. memcpy(as->s + as->length, p, s);
  206. as->length += s;
  207. as->s[as->length] = 0;
  208. return (as);
  209. }
  210. static struct archive_wstring *
  211. archive_wstring_append(struct archive_wstring *as, const wchar_t *p, size_t s)
  212. {
  213. if (archive_wstring_ensure(as, as->length + s + 1) == NULL)
  214. return (NULL);
  215. wmemcpy(as->s + as->length, p, s);
  216. as->length += s;
  217. as->s[as->length] = 0;
  218. return (as);
  219. }
  220. void
  221. archive_string_concat(struct archive_string *dest, struct archive_string *src)
  222. {
  223. if (archive_string_append(dest, src->s, src->length) == NULL)
  224. __archive_errx(1, "Out of memory");
  225. }
  226. void
  227. archive_wstring_concat(struct archive_wstring *dest, struct archive_wstring *src)
  228. {
  229. if (archive_wstring_append(dest, src->s, src->length) == NULL)
  230. __archive_errx(1, "Out of memory");
  231. }
  232. void
  233. archive_string_free(struct archive_string *as)
  234. {
  235. as->length = 0;
  236. as->buffer_length = 0;
  237. free(as->s);
  238. as->s = NULL;
  239. }
  240. void
  241. archive_wstring_free(struct archive_wstring *as)
  242. {
  243. as->length = 0;
  244. as->buffer_length = 0;
  245. free(as->s);
  246. as->s = NULL;
  247. }
  248. struct archive_wstring *
  249. archive_wstring_ensure(struct archive_wstring *as, size_t s)
  250. {
  251. return (struct archive_wstring *)
  252. archive_string_ensure((struct archive_string *)as,
  253. s * sizeof(wchar_t));
  254. }
  255. /* Returns NULL on any allocation failure. */
  256. struct archive_string *
  257. archive_string_ensure(struct archive_string *as, size_t s)
  258. {
  259. char *p;
  260. size_t new_length;
  261. /* If buffer is already big enough, don't reallocate. */
  262. if (as->s && (s <= as->buffer_length))
  263. return (as);
  264. /*
  265. * Growing the buffer at least exponentially ensures that
  266. * append operations are always linear in the number of
  267. * characters appended. Using a smaller growth rate for
  268. * larger buffers reduces memory waste somewhat at the cost of
  269. * a larger constant factor.
  270. */
  271. if (as->buffer_length < 32)
  272. /* Start with a minimum 32-character buffer. */
  273. new_length = 32;
  274. else if (as->buffer_length < 8192)
  275. /* Buffers under 8k are doubled for speed. */
  276. new_length = as->buffer_length + as->buffer_length;
  277. else {
  278. /* Buffers 8k and over grow by at least 25% each time. */
  279. new_length = as->buffer_length + as->buffer_length / 4;
  280. /* Be safe: If size wraps, fail. */
  281. if (new_length < as->buffer_length) {
  282. /* On failure, wipe the string and return NULL. */
  283. archive_string_free(as);
  284. errno = ENOMEM;/* Make sure errno has ENOMEM. */
  285. return (NULL);
  286. }
  287. }
  288. /*
  289. * The computation above is a lower limit to how much we'll
  290. * grow the buffer. In any case, we have to grow it enough to
  291. * hold the request.
  292. */
  293. if (new_length < s)
  294. new_length = s;
  295. /* Now we can reallocate the buffer. */
  296. p = (char *)realloc(as->s, new_length);
  297. if (p == NULL) {
  298. /* On failure, wipe the string and return NULL. */
  299. archive_string_free(as);
  300. errno = ENOMEM;/* Make sure errno has ENOMEM. */
  301. return (NULL);
  302. }
  303. as->s = p;
  304. as->buffer_length = new_length;
  305. return (as);
  306. }
  307. /*
  308. * TODO: See if there's a way to avoid scanning
  309. * the source string twice. Then test to see
  310. * if it actually helps (remember that we're almost
  311. * always called with pretty short arguments, so
  312. * such an optimization might not help).
  313. */
  314. struct archive_string *
  315. archive_strncat(struct archive_string *as, const void *_p, size_t n)
  316. {
  317. size_t s;
  318. const char *p, *pp;
  319. p = (const char *)_p;
  320. /* Like strlen(p), except won't examine positions beyond p[n]. */
  321. s = 0;
  322. pp = p;
  323. while (s < n && *pp) {
  324. pp++;
  325. s++;
  326. }
  327. if ((as = archive_string_append(as, p, s)) == NULL)
  328. __archive_errx(1, "Out of memory");
  329. return (as);
  330. }
  331. struct archive_wstring *
  332. archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
  333. {
  334. size_t s;
  335. const wchar_t *pp;
  336. /* Like strlen(p), except won't examine positions beyond p[n]. */
  337. s = 0;
  338. pp = p;
  339. while (s < n && *pp) {
  340. pp++;
  341. s++;
  342. }
  343. if ((as = archive_wstring_append(as, p, s)) == NULL)
  344. __archive_errx(1, "Out of memory");
  345. return (as);
  346. }
  347. struct archive_string *
  348. archive_strcat(struct archive_string *as, const void *p)
  349. {
  350. /* strcat is just strncat without an effective limit.
  351. * Assert that we'll never get called with a source
  352. * string over 16MB.
  353. * TODO: Review all uses of strcat in the source
  354. * and try to replace them with strncat().
  355. */
  356. return archive_strncat(as, p, 0x1000000);
  357. }
  358. struct archive_wstring *
  359. archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
  360. {
  361. /* Ditto. */
  362. return archive_wstrncat(as, p, 0x1000000);
  363. }
  364. struct archive_string *
  365. archive_strappend_char(struct archive_string *as, char c)
  366. {
  367. if ((as = archive_string_append(as, &c, 1)) == NULL)
  368. __archive_errx(1, "Out of memory");
  369. return (as);
  370. }
  371. struct archive_wstring *
  372. archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
  373. {
  374. if ((as = archive_wstring_append(as, &c, 1)) == NULL)
  375. __archive_errx(1, "Out of memory");
  376. return (as);
  377. }
  378. /*
  379. * Get the "current character set" name to use with iconv.
  380. * On FreeBSD, the empty character set name "" chooses
  381. * the correct character encoding for the current locale,
  382. * so this isn't necessary.
  383. * But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
  384. * on that system, we have to explicitly call nl_langinfo()
  385. * to get the right name. Not sure about other platforms.
  386. *
  387. * NOTE: GNU libiconv does not recognize the character-set name
  388. * which some platform nl_langinfo(CODESET) returns, so we should
  389. * use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
  390. */
  391. static const char *
  392. default_iconv_charset(const char *charset) {
  393. if (charset != NULL && charset[0] != '\0')
  394. return charset;
  395. #if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
  396. /* locale_charset() is broken on Mac OS */
  397. return locale_charset();
  398. #elif HAVE_NL_LANGINFO
  399. return nl_langinfo(CODESET);
  400. #else
  401. return "";
  402. #endif
  403. }
  404. #if defined(_WIN32) && !defined(__CYGWIN__)
  405. /*
  406. * Convert MBS to WCS.
  407. * Note: returns -1 if conversion fails.
  408. */
  409. int
  410. archive_wstring_append_from_mbs(struct archive_wstring *dest,
  411. const char *p, size_t len)
  412. {
  413. int r = archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
  414. if (r != 0 && errno == ENOMEM)
  415. __archive_errx(1, "No memory");
  416. return (r);
  417. }
  418. static int
  419. archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
  420. const char *s, size_t length, struct archive_string_conv *sc)
  421. {
  422. int count, ret = 0;
  423. UINT from_cp;
  424. if (sc != NULL)
  425. from_cp = sc->from_cp;
  426. else
  427. from_cp = get_current_codepage();
  428. if (from_cp == CP_C_LOCALE) {
  429. /*
  430. * "C" locale special process.
  431. */
  432. wchar_t *ws;
  433. const unsigned char *mp;
  434. if (NULL == archive_wstring_ensure(dest,
  435. dest->length + length + 1))
  436. return (-1);
  437. ws = dest->s + dest->length;
  438. mp = (const unsigned char *)s;
  439. count = 0;
  440. while (count < (int)length && *mp) {
  441. *ws++ = (wchar_t)*mp++;
  442. count++;
  443. }
  444. } else if (sc != NULL && (sc->flag & SCONV_NORMALIZATION_C)) {
  445. /*
  446. * Normalize UTF-8 and UTF-16BE and convert it directly
  447. * to UTF-16 as wchar_t.
  448. */
  449. struct archive_string u16;
  450. int saved_flag = sc->flag;/* save current flag. */
  451. if (is_big_endian())
  452. sc->flag |= SCONV_TO_UTF16BE;
  453. else
  454. sc->flag |= SCONV_TO_UTF16LE;
  455. if (sc->flag & SCONV_FROM_UTF16) {
  456. /*
  457. * UTF-16BE/LE NFD ===> UTF-16 NFC
  458. */
  459. count = utf16nbytes(s, length);
  460. } else {
  461. /*
  462. * UTF-8 NFD ===> UTF-16 NFC
  463. */
  464. count = mbsnbytes(s, length);
  465. }
  466. u16.s = (char *)dest->s;
  467. u16.length = dest->length << 1;;
  468. u16.buffer_length = dest->buffer_length;
  469. ret = archive_string_normalize_C(&u16, s, count, sc);
  470. dest->s = (wchar_t *)u16.s;
  471. dest->length = u16.length >> 1;
  472. dest->buffer_length = u16.buffer_length;
  473. sc->flag = saved_flag;/* restore the saved flag. */
  474. return (ret);
  475. } else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
  476. count = utf16nbytes(s, length);
  477. count >>= 1; /* to be WCS length */
  478. /* Allocate memory for WCS. */
  479. if (NULL == archive_wstring_ensure(dest,
  480. dest->length + count + 1))
  481. return (-1);
  482. wmemcpy(dest->s + dest->length, (wchar_t *)s, count);
  483. if ((sc->flag & SCONV_FROM_UTF16BE) && !is_big_endian()) {
  484. uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
  485. int b;
  486. for (b = 0; b < count; b++) {
  487. uint16_t val = archive_le16dec(u16+b);
  488. archive_be16enc(u16+b, val);
  489. }
  490. } else if ((sc->flag & SCONV_FROM_UTF16LE) && is_big_endian()) {
  491. uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
  492. int b;
  493. for (b = 0; b < count; b++) {
  494. uint16_t val = archive_be16dec(u16+b);
  495. archive_le16enc(u16+b, val);
  496. }
  497. }
  498. } else {
  499. DWORD mbflag;
  500. if (sc == NULL)
  501. mbflag = 0;
  502. else if (sc->flag & SCONV_FROM_CHARSET) {
  503. /* Do not trust the length which comes from
  504. * an archive file. */
  505. length = mbsnbytes(s, length);
  506. mbflag = 0;
  507. } else
  508. mbflag = MB_PRECOMPOSED;
  509. if (length == 0) {
  510. /*
  511. * We do not need to convert any characters but make
  512. * sure `dest' has a valid buffer(no NULL pointer).
  513. */
  514. if (NULL == archive_wstring_ensure(dest,
  515. dest->length + 1))
  516. return (-1);
  517. dest->s[dest->length] = L'\0';
  518. return (0);
  519. }
  520. /*
  521. * Count how many bytes are needed for WCS.
  522. */
  523. count = MultiByteToWideChar(from_cp,
  524. mbflag, s, length, NULL, 0);
  525. if (count == 0) {
  526. if (dest->s == NULL) {
  527. if (NULL == archive_wstring_ensure(dest,
  528. dest->length + 1))
  529. return (-1);
  530. }
  531. dest->s[dest->length] = L'\0';
  532. return (-1);
  533. }
  534. /* Allocate memory for WCS. */
  535. if (NULL == archive_wstring_ensure(dest,
  536. dest->length + count + 1))
  537. return (-1);
  538. /* Convert MBS to WCS. */
  539. count = MultiByteToWideChar(from_cp,
  540. mbflag, s, length, dest->s + dest->length, count);
  541. if (count == 0)
  542. ret = -1;
  543. }
  544. dest->length += count;
  545. dest->s[dest->length] = L'\0';
  546. return (ret);
  547. }
  548. #elif defined(HAVE_MBSNRTOWCS)
  549. /*
  550. * Convert MBS to WCS.
  551. * Note: returns -1 if conversion fails.
  552. */
  553. int
  554. archive_wstring_append_from_mbs(struct archive_wstring *dest,
  555. const char *p, size_t len)
  556. {
  557. size_t r;
  558. /*
  559. * No single byte will be more than one wide character,
  560. * so this length estimate will always be big enough.
  561. */
  562. size_t wcs_length = len;
  563. size_t mbs_length = len;
  564. const char *mbs = p;
  565. wchar_t *wcs;
  566. mbstate_t shift_state;
  567. memset(&shift_state, 0, sizeof(shift_state));
  568. if (NULL == archive_wstring_ensure(dest, dest->length + wcs_length + 1))
  569. __archive_errx(1,
  570. "No memory for archive_wstring_append_from_mbs()");
  571. wcs = dest->s + dest->length;
  572. r = mbsnrtowcs(wcs, &mbs, mbs_length, wcs_length, &shift_state);
  573. if (r != (size_t)-1) {
  574. dest->length += r;
  575. dest->s[dest->length] = L'\0';
  576. return (0);
  577. }
  578. dest->s[dest->length] = L'\0';
  579. return (-1);
  580. }
  581. #else
  582. /*
  583. * Convert MBS to WCS.
  584. * Note: returns -1 if conversion fails.
  585. */
  586. int
  587. archive_wstring_append_from_mbs(struct archive_wstring *dest,
  588. const char *p, size_t len)
  589. {
  590. size_t r;
  591. /*
  592. * No single byte will be more than one wide character,
  593. * so this length estimate will always be big enough.
  594. */
  595. size_t wcs_length = len;
  596. size_t mbs_length = len;
  597. const char *mbs = p;
  598. wchar_t *wcs;
  599. #if HAVE_MBRTOWC
  600. mbstate_t shift_state;
  601. memset(&shift_state, 0, sizeof(shift_state));
  602. #endif
  603. if (NULL == archive_wstring_ensure(dest, dest->length + wcs_length + 1))
  604. __archive_errx(1,
  605. "No memory for archive_wstring_append_from_mbs()");
  606. wcs = dest->s + dest->length;
  607. /*
  608. * We cannot use mbsrtowcs/mbstowcs here because those may convert
  609. * extra MBS when strlen(p) > len and one wide character consis of
  610. * multi bytes.
  611. */
  612. while (wcs_length > 0 && *mbs && mbs_length > 0) {
  613. #if HAVE_MBRTOWC
  614. r = mbrtowc(wcs, mbs, wcs_length, &shift_state);
  615. #else
  616. r = mbtowc(wcs, mbs, wcs_length);
  617. #endif
  618. if (r == (size_t)-1 || r == (size_t)-2) {
  619. dest->s[dest->length] = L'\0';
  620. return (-1);
  621. }
  622. if (r == 0 || r > mbs_length)
  623. break;
  624. wcs++;
  625. wcs_length--;
  626. mbs += r;
  627. mbs_length -= r;
  628. }
  629. dest->length = wcs - dest->s;
  630. dest->s[dest->length] = L'\0';
  631. return (0);
  632. }
  633. #endif
  634. #if defined(_WIN32) && !defined(__CYGWIN__)
  635. /*
  636. * WCS ==> MBS.
  637. * Note: returns -1 if conversion fails.
  638. *
  639. * Win32 builds use WideCharToMultiByte from the Windows API.
  640. * (Maybe Cygwin should too? WideCharToMultiByte will know a
  641. * lot more about local character encodings than the wcrtomb()
  642. * wrapper is going to know.)
  643. */
  644. int
  645. archive_string_append_from_wcs(struct archive_string *as,
  646. const wchar_t *w, size_t len)
  647. {
  648. int r = archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
  649. if (r != 0 && errno == ENOMEM)
  650. __archive_errx(1, "No memory");
  651. return (r);
  652. }
  653. static int
  654. archive_string_append_from_wcs_in_codepage(struct archive_string *as,
  655. const wchar_t *ws, size_t len, struct archive_string_conv *sc)
  656. {
  657. BOOL defchar_used, *dp;
  658. int count, ret = 0;
  659. UINT to_cp;
  660. int wslen = (int)len;
  661. if (sc != NULL)
  662. to_cp = sc->to_cp;
  663. else
  664. to_cp = get_current_codepage();
  665. if (to_cp == CP_C_LOCALE) {
  666. /*
  667. * "C" locale special process.
  668. */
  669. const wchar_t *wp = ws;
  670. char *p;
  671. if (NULL == archive_string_ensure(as,
  672. as->length + wslen +1))
  673. return (-1);
  674. p = as->s + as->length;
  675. count = 0;
  676. defchar_used = 0;
  677. while (count < wslen && *wp) {
  678. if (*wp > 255) {
  679. *p++ = '?';
  680. wp++;
  681. defchar_used = 1;
  682. } else
  683. *p++ = (char)*wp++;
  684. count++;
  685. }
  686. } else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
  687. uint16_t *u16;
  688. if (NULL ==
  689. archive_string_ensure(as, as->length + len * 2 + 2))
  690. return (-1);
  691. u16 = (uint16_t *)(as->s + as->length);
  692. count = 0;
  693. defchar_used = 0;
  694. if (sc->flag & SCONV_TO_UTF16BE) {
  695. while (count < (int)len && *ws) {
  696. archive_be16enc(u16+count, *ws);
  697. ws++;
  698. count++;
  699. }
  700. } else {
  701. while (count < (int)len && *ws) {
  702. archive_le16enc(u16+count, *ws);
  703. ws++;
  704. count++;
  705. }
  706. }
  707. count <<= 1; /* to be byte size */
  708. } else {
  709. /* Make sure the MBS buffer has plenty to set. */
  710. if (NULL ==
  711. archive_string_ensure(as, as->length + len * 2 + 1))
  712. return (-1);
  713. do {
  714. defchar_used = 0;
  715. if (to_cp == CP_UTF8 || sc == NULL)
  716. dp = NULL;
  717. else
  718. dp = &defchar_used;
  719. count = WideCharToMultiByte(to_cp, 0, ws, wslen,
  720. as->s + as->length, as->buffer_length-1, NULL, dp);
  721. if (count == 0 &&
  722. GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  723. /* Expand the MBS buffer and retry. */
  724. if (NULL == archive_string_ensure(as,
  725. as->buffer_length + len))
  726. return (-1);
  727. continue;
  728. }
  729. if (count == 0)
  730. ret = -1;
  731. } while (0);
  732. }
  733. as->length += count;
  734. as->s[as->length] = '\0';
  735. return (defchar_used?-1:ret);
  736. }
  737. #elif defined(HAVE_WCSNRTOMBS)
  738. /*
  739. * Translates a wide character string into current locale character set
  740. * and appends to the archive_string. Note: returns -1 if conversion
  741. * fails.
  742. */
  743. int
  744. archive_string_append_from_wcs(struct archive_string *as,
  745. const wchar_t *w, size_t len)
  746. {
  747. mbstate_t shift_state;
  748. size_t r, ndest, nwc;
  749. char *dest;
  750. const wchar_t *wp, *wpp;
  751. int ret_val = 0;
  752. wp = w;
  753. nwc = len;
  754. ndest = len * 2;
  755. /* Initialize the shift state. */
  756. memset(&shift_state, 0, sizeof(shift_state));
  757. while (nwc > 0) {
  758. /* Allocate buffer for MBS. */
  759. if (archive_string_ensure(as, as->length + ndest + 1) == NULL)
  760. __archive_errx(1, "Out of memory");
  761. dest = as->s + as->length;
  762. wpp = wp;
  763. r = wcsnrtombs(dest, &wp, nwc,
  764. as->buffer_length - as->length -1,
  765. &shift_state);
  766. if (r == (size_t)-1) {
  767. if (errno == EILSEQ) {
  768. /* Retry conversion just for safe WCS. */
  769. size_t xwc = wp - wpp;
  770. wp = wpp;
  771. r = wcsnrtombs(dest, &wp, xwc,
  772. as->buffer_length - as->length -1,
  773. &shift_state);
  774. if (r == (size_t)-1)
  775. /* This would not happen. */
  776. return (-1);
  777. as->length += r;
  778. nwc -= wp - wpp;
  779. /* Skip an illegal wide char. */
  780. as->s[as->length++] = '?';
  781. wp++;
  782. nwc--;
  783. ret_val = -1;
  784. continue;
  785. } else {
  786. ret_val = -1;
  787. break;
  788. }
  789. }
  790. as->length += r;
  791. if (wp == NULL || (wp - wpp) >= nwc)
  792. break;
  793. /* Get a remaining WCS lenth. */
  794. nwc -= wp - wpp;
  795. }
  796. /* All wide characters are translated to MBS. */
  797. as->s[as->length] = '\0';
  798. return (ret_val);
  799. }
  800. #elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
  801. /*
  802. * Translates a wide character string into current locale character set
  803. * and appends to the archive_string. Note: returns -1 if conversion
  804. * fails.
  805. */
  806. int
  807. archive_string_append_from_wcs(struct archive_string *as,
  808. const wchar_t *w, size_t len)
  809. {
  810. /* We cannot use the standard wcstombs() here because it
  811. * cannot tell us how big the output buffer should be. So
  812. * I've built a loop around wcrtomb() or wctomb() that
  813. * converts a character at a time and resizes the string as
  814. * needed. We prefer wcrtomb() when it's available because
  815. * it's thread-safe. */
  816. int n, ret_val = 0;
  817. char *p;
  818. char *end;
  819. #if HAVE_WCRTOMB
  820. mbstate_t shift_state;
  821. memset(&shift_state, 0, sizeof(shift_state));
  822. #else
  823. /* Clear the shift state before starting. */
  824. wctomb(NULL, L'\0');
  825. #endif
  826. /*
  827. * Allocate buffer for MBS.
  828. * We need this allocation here since it is possible that
  829. * as->s is still NULL.
  830. */
  831. if (archive_string_ensure(as, as->length + len + 1) == NULL)
  832. __archive_errx(1, "Out of memory");
  833. p = as->s + as->length;
  834. end = as->s + as->buffer_length - MB_CUR_MAX -1;
  835. while (*w != L'\0' && len > 0) {
  836. if (p >= end) {
  837. as->length = p - as->s;
  838. as->s[as->length] = '\0';
  839. /* Re-allocate buffer for MBS. */
  840. if (archive_string_ensure(as,
  841. as->length + len * 2 + 1) == NULL)
  842. __archive_errx(1, "Out of memory");
  843. p = as->s + as->length;
  844. end = as->s + as->buffer_length - MB_CUR_MAX -1;
  845. }
  846. #if HAVE_WCRTOMB
  847. n = wcrtomb(p, *w++, &shift_state);
  848. #else
  849. n = wctomb(p, *w++);
  850. #endif
  851. if (n == -1) {
  852. if (errno == EILSEQ) {
  853. /* Skip an illegal wide char. */
  854. *p++ = '?';
  855. ret_val = -1;
  856. } else {
  857. ret_val = -1;
  858. break;
  859. }
  860. } else
  861. p += n;
  862. len--;
  863. }
  864. as->length = p - as->s;
  865. as->s[as->length] = '\0';
  866. return (ret_val);
  867. }
  868. #else /* HAVE_WCTOMB || HAVE_WCRTOMB */
  869. /*
  870. * TODO: Test if __STDC_ISO_10646__ is defined.
  871. * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
  872. * one character at a time. If a non-Windows platform doesn't have
  873. * either of these, fall back to the built-in UTF8 conversion.
  874. */
  875. int
  876. archive_string_append_from_wcs(struct archive_string *as,
  877. const wchar_t *w, size_t len)
  878. {
  879. (void)as;/* UNUSED */
  880. (void)w;/* UNUSED */
  881. (void)len;/* UNUSED */
  882. return (-1);
  883. }
  884. #endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
  885. /*
  886. * Find a string conversion object by a pair of 'from' charset name
  887. * and 'to' charset name from an archive object.
  888. * Return NULL if not found.
  889. */
  890. static struct archive_string_conv *
  891. find_sconv_object(struct archive *a, const char *fc, const char *tc)
  892. {
  893. struct archive_string_conv *sc;
  894. if (a == NULL)
  895. return (NULL);
  896. for (sc = a->sconv; sc != NULL; sc = sc->next) {
  897. if (strcmp(sc->from_charset, fc) == 0 &&
  898. strcmp(sc->to_charset, tc) == 0)
  899. break;
  900. }
  901. return (sc);
  902. }
  903. /*
  904. * Register a string object to an archive object.
  905. */
  906. static void
  907. add_sconv_object(struct archive *a, struct archive_string_conv *sc)
  908. {
  909. struct archive_string_conv **psc;
  910. /* Add a new sconv to sconv list. */
  911. psc = &(a->sconv);
  912. while (*psc != NULL)
  913. psc = &((*psc)->next);
  914. *psc = sc;
  915. }
  916. #if defined(__APPLE__)
  917. static int
  918. createUniInfo(struct archive_string_conv *sconv)
  919. {
  920. UnicodeMapping map;
  921. OSStatus err;
  922. map.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
  923. kUnicodeNoSubset, kUnicode16BitFormat);
  924. map.otherEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
  925. kUnicodeHFSPlusDecompVariant, kUnicode16BitFormat);
  926. map.mappingVersion = kUnicodeUseLatestMapping;
  927. sconv->uniInfo = NULL;
  928. err = CreateUnicodeToTextInfo(&map, &(sconv->uniInfo));
  929. return ((err == noErr)? 0: -1);
  930. }
  931. #endif /* __APPLE__ */
  932. static void
  933. add_converter(struct archive_string_conv *sc, int (*converter)
  934. (struct archive_string *, const void *, size_t,
  935. struct archive_string_conv *))
  936. {
  937. if (sc == NULL || sc->nconverter >= 2)
  938. __archive_errx(1, "Programing error");
  939. sc->converter[sc->nconverter++] = converter;
  940. }
  941. static void
  942. setup_converter(struct archive_string_conv *sc)
  943. {
  944. /* Reset. */
  945. sc->nconverter = 0;
  946. /*
  947. * Perform special sequence for the incorrect UTF-8 filenames
  948. * made by libarchive2.x.
  949. */
  950. if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
  951. add_converter(sc, strncat_from_utf8_libarchive2);
  952. return;
  953. }
  954. /*
  955. * Convert a string to UTF-16BE/LE.
  956. */
  957. if (sc->flag & SCONV_TO_UTF16) {
  958. /*
  959. * If the current locale is UTF-8, we can translate
  960. * a UTF-8 string into a UTF-16BE string.
  961. */
  962. if (sc->flag & SCONV_FROM_UTF8) {
  963. add_converter(sc, archive_string_append_unicode);
  964. return;
  965. }
  966. #if defined(_WIN32) && !defined(__CYGWIN__)
  967. if (sc->flag & SCONV_WIN_CP) {
  968. if (sc->flag & SCONV_TO_UTF16BE)
  969. add_converter(sc, win_strncat_to_utf16be);
  970. else
  971. add_converter(sc, win_strncat_to_utf16le);
  972. return;
  973. }
  974. #endif
  975. #if defined(HAVE_ICONV)
  976. if (sc->cd != (iconv_t)-1) {
  977. add_converter(sc, iconv_strncat_in_locale);
  978. return;
  979. }
  980. #endif
  981. if (sc->flag & SCONV_BEST_EFFORT) {
  982. if (sc->flag & SCONV_TO_UTF16BE)
  983. add_converter(sc, best_effort_strncat_to_utf16be);
  984. else
  985. add_converter(sc, best_effort_strncat_to_utf16le);
  986. } else
  987. /* Make sure we have no converter. */
  988. sc->nconverter = 0;
  989. return;
  990. }
  991. /*
  992. * Convert a string from UTF-16BE/LE.
  993. */
  994. if (sc->flag & SCONV_FROM_UTF16) {
  995. /*
  996. * At least we should normalize a UTF-16BE string.
  997. */
  998. #if defined(__APPLE__)
  999. if (sc->flag & SCONV_NORMALIZATION_D)
  1000. add_converter(sc,archive_string_normalize_D);
  1001. else
  1002. #endif
  1003. if (sc->flag & SCONV_NORMALIZATION_C)
  1004. add_converter(sc, archive_string_normalize_C);
  1005. if (sc->flag & SCONV_TO_UTF8) {
  1006. /*
  1007. * If the current locale is UTF-8, we can translate
  1008. * a UTF-16BE/LE string into a UTF-8 string directly.
  1009. */
  1010. if (!(sc->flag &
  1011. (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
  1012. add_converter(sc,
  1013. archive_string_append_unicode);
  1014. return;
  1015. }
  1016. #if defined(_WIN32) && !defined(__CYGWIN__)
  1017. if (sc->flag & SCONV_WIN_CP) {
  1018. if (sc->flag & SCONV_FROM_UTF16BE)
  1019. add_converter(sc, win_strncat_from_utf16be);
  1020. else
  1021. add_converter(sc, win_strncat_from_utf16le);
  1022. return;
  1023. }
  1024. #endif
  1025. #if defined(HAVE_ICONV)
  1026. if (sc->cd != (iconv_t)-1) {
  1027. add_converter(sc, iconv_strncat_in_locale);
  1028. return;
  1029. }
  1030. #endif
  1031. if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
  1032. == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
  1033. add_converter(sc, best_effort_strncat_from_utf16be);
  1034. else if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
  1035. == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
  1036. add_converter(sc, best_effort_strncat_from_utf16le);
  1037. else
  1038. /* Make sure we have no converter. */
  1039. sc->nconverter = 0;
  1040. return;
  1041. }
  1042. if (sc->flag & SCONV_FROM_UTF8) {
  1043. /*
  1044. * At least we should normalize a UTF-8 string.
  1045. */
  1046. #if defined(__APPLE__)
  1047. if (sc->flag & SCONV_NORMALIZATION_D)
  1048. add_converter(sc,archive_string_normalize_D);
  1049. else
  1050. #endif
  1051. if (sc->flag & SCONV_NORMALIZATION_C)
  1052. add_converter(sc, archive_string_normalize_C);
  1053. /*
  1054. * Copy UTF-8 string with a check of CESU-8.
  1055. * Apparently, iconv does not check surrogate pairs in UTF-8
  1056. * when both from-charset and to-charset are UTF-8, and then
  1057. * we use our UTF-8 copy code.
  1058. */
  1059. if (sc->flag & SCONV_TO_UTF8) {
  1060. /*
  1061. * If the current locale is UTF-8, we can translate
  1062. * a UTF-16BE string into a UTF-8 string directly.
  1063. */
  1064. if (!(sc->flag &
  1065. (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
  1066. add_converter(sc, strncat_from_utf8_to_utf8);
  1067. return;
  1068. }
  1069. }
  1070. #if defined(_WIN32) && !defined(__CYGWIN__)
  1071. /*
  1072. * On Windows we can use Windows API for a string conversion.
  1073. */
  1074. if (sc->flag & SCONV_WIN_CP) {
  1075. add_converter(sc, strncat_in_codepage);
  1076. return;
  1077. }
  1078. #endif
  1079. #if HAVE_ICONV
  1080. if (sc->cd != (iconv_t)-1) {
  1081. add_converter(sc, iconv_strncat_in_locale);
  1082. return;
  1083. }
  1084. #endif
  1085. /*
  1086. * Try conversion in the best effort or no conversion.
  1087. */
  1088. if ((sc->flag & SCONV_BEST_EFFORT) || sc->same)
  1089. add_converter(sc, best_effort_strncat_in_locale);
  1090. else
  1091. /* Make sure we have no converter. */
  1092. sc->nconverter = 0;
  1093. }
  1094. /*
  1095. * Return canonicalized charset-name but this supports just UTF-8, UTF-16BE
  1096. * and CP932 which are referenced in create_sconv_object().
  1097. */
  1098. static const char *
  1099. canonical_charset_name(const char *charset)
  1100. {
  1101. char cs[16];
  1102. char *p;
  1103. const char *s;
  1104. if (charset == NULL || charset[0] == '\0'
  1105. || strlen(charset) > 15)
  1106. return (charset);
  1107. /* Copy name to uppercase. */
  1108. p = cs;
  1109. s = charset;
  1110. while (*s) {
  1111. char c = *s++;
  1112. if (c >= 'a' && c <= 'z')
  1113. c -= 'a' - 'A';
  1114. *p++ = c;
  1115. }
  1116. *p++ = '\0';
  1117. if (strcmp(cs, "UTF-8") == 0 ||
  1118. strcmp(cs, "UTF8") == 0)
  1119. return ("UTF-8");
  1120. if (strcmp(cs, "UTF-16BE") == 0 ||
  1121. strcmp(cs, "UTF16BE") == 0)
  1122. return ("UTF-16BE");
  1123. if (strcmp(cs, "UTF-16LE") == 0 ||
  1124. strcmp(cs, "UTF16LE") == 0)
  1125. return ("UTF-16LE");
  1126. if (strcmp(cs, "CP932") == 0)
  1127. return ("CP932");
  1128. return (charset);
  1129. }
  1130. /*
  1131. * Create a string conversion object.
  1132. */
  1133. static struct archive_string_conv *
  1134. create_sconv_object(const char *fc, const char *tc,
  1135. unsigned current_codepage, int flag)
  1136. {
  1137. struct archive_string_conv *sc;
  1138. sc = calloc(1, sizeof(*sc));
  1139. if (sc == NULL)
  1140. return (NULL);
  1141. sc->next = NULL;
  1142. sc->from_charset = strdup(fc);
  1143. if (sc->from_charset == NULL) {
  1144. free(sc);
  1145. return (NULL);
  1146. }
  1147. sc->to_charset = strdup(tc);
  1148. if (sc->to_charset == NULL) {
  1149. free(sc->from_charset);
  1150. free(sc);
  1151. return (NULL);
  1152. }
  1153. archive_string_init(&sc->utftmp);
  1154. #if defined(__APPLE__)
  1155. archive_string_init(&sc->utf16nfc);
  1156. archive_string_init(&sc->utf16nfd);
  1157. #endif
  1158. if (flag & SCONV_TO_CHARSET) {
  1159. /*
  1160. * Convert characters from the current locale charset to
  1161. * a specified charset.
  1162. */
  1163. sc->from_cp = current_codepage;
  1164. sc->to_cp = make_codepage_from_charset(tc);
  1165. #if defined(_WIN32) && !defined(__CYGWIN__)
  1166. if (IsValidCodePage(sc->to_cp))
  1167. flag |= SCONV_WIN_CP;
  1168. #endif
  1169. } else if (flag & SCONV_FROM_CHARSET) {
  1170. /*
  1171. * Convert characters from a specified charset to
  1172. * the current locale charset.
  1173. */
  1174. sc->to_cp = current_codepage;
  1175. sc->from_cp = make_codepage_from_charset(fc);
  1176. #if defined(_WIN32) && !defined(__CYGWIN__)
  1177. if (IsValidCodePage(sc->from_cp))
  1178. flag |= SCONV_WIN_CP;
  1179. #endif
  1180. }
  1181. /*
  1182. * Check if "from charset" and "to charset" are the same.
  1183. */
  1184. if (strcmp(fc, tc) == 0 ||
  1185. (sc->from_cp != -1 && sc->from_cp == sc->to_cp))
  1186. sc->same = 1;
  1187. else
  1188. sc->same = 0;
  1189. /*
  1190. * Mark if "from charset" or "to charset" are UTF-8 or UTF-16BE/LE.
  1191. */
  1192. if (strcmp(tc, "UTF-8") == 0)
  1193. flag |= SCONV_TO_UTF8;
  1194. else if (strcmp(tc, "UTF-16BE") == 0)
  1195. flag |= SCONV_TO_UTF16BE;
  1196. else if (strcmp(tc, "UTF-16LE") == 0)
  1197. flag |= SCONV_TO_UTF16LE;
  1198. if (strcmp(fc, "UTF-8") == 0)
  1199. flag |= SCONV_FROM_UTF8;
  1200. else if (strcmp(fc, "UTF-16BE") == 0)
  1201. flag |= SCONV_FROM_UTF16BE;
  1202. else if (strcmp(fc, "UTF-16LE") == 0)
  1203. flag |= SCONV_FROM_UTF16LE;
  1204. #if defined(_WIN32) && !defined(__CYGWIN__)
  1205. if (sc->to_cp == CP_UTF8)
  1206. flag |= SCONV_TO_UTF8;
  1207. else if (sc->to_cp == CP_UTF16BE)
  1208. flag |= SCONV_TO_UTF16BE | SCONV_WIN_CP;
  1209. else if (sc->to_cp == CP_UTF16LE)
  1210. flag |= SCONV_TO_UTF16LE | SCONV_WIN_CP;
  1211. if (sc->from_cp == CP_UTF8)
  1212. flag |= SCONV_FROM_UTF8;
  1213. else if (sc->from_cp == CP_UTF16BE)
  1214. flag |= SCONV_FROM_UTF16BE | SCONV_WIN_CP;
  1215. else if (sc->from_cp == CP_UTF16LE)
  1216. flag |= SCONV_FROM_UTF16LE | SCONV_WIN_CP;
  1217. #endif
  1218. /*
  1219. * Set a flag for Unicode NFD. Usually iconv cannot correctly
  1220. * handle it. So we have to translate NFD characters to NFC ones
  1221. * ourselves before iconv handles. Another reason is to prevent
  1222. * that the same sight of two filenames, one is NFC and other
  1223. * is NFD, would be in its directory.
  1224. * On Mac OS X, although its filesystem layer automatically
  1225. * convert filenames to NFD, it would be useful for filename
  1226. * comparing to find out the same filenames that we normalize
  1227. * that to be NFD ourselves.
  1228. */
  1229. if ((flag & SCONV_FROM_CHARSET) &&
  1230. (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8))) {
  1231. #if defined(__APPLE__)
  1232. if (flag & SCONV_TO_UTF8) {
  1233. if (createUniInfo(sc) == 0)
  1234. flag |= SCONV_NORMALIZATION_D;
  1235. } else
  1236. #endif
  1237. flag |= SCONV_NORMALIZATION_C;
  1238. }
  1239. #if defined(HAVE_ICONV)
  1240. sc->cd_w = (iconv_t)-1;
  1241. /*
  1242. * Create an iconv object.
  1243. */
  1244. if (((flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) &&
  1245. (flag & (SCONV_FROM_UTF8 | SCONV_FROM_UTF16))) ||
  1246. (flag & SCONV_WIN_CP)) {
  1247. /* This case we won't use iconv. */
  1248. sc->cd = (iconv_t)-1;
  1249. #if defined(__APPLE__)
  1250. } else if ((flag & SCONV_FROM_CHARSET) && (flag & SCONV_TO_UTF8)) {
  1251. /*
  1252. * In case reading an archive file.
  1253. * Translate non-Unicode filenames in an archive file to
  1254. * UTF-8-MAC filenames.
  1255. */
  1256. sc->cd = iconv_open("UTF-8-MAC", fc);
  1257. if (sc->cd == (iconv_t)-1) {
  1258. if ((sc->flag & SCONV_BEST_EFFORT) &&
  1259. strcmp(fc, "CP932") == 0) {
  1260. sc->cd = iconv_open("UTF-8-MAC", "SJIS");
  1261. if (sc->cd == (iconv_t)-1) {
  1262. sc->cd = iconv_open(tc, fc);
  1263. if (sc->cd == (iconv_t)-1)
  1264. sc->cd = iconv_open(tc, "SJIS");
  1265. }
  1266. } else
  1267. sc->cd = iconv_open(tc, fc);
  1268. }
  1269. } else if ((flag & SCONV_TO_CHARSET) && (flag & SCONV_FROM_UTF8)) {
  1270. /*
  1271. * In case writing an archive file.
  1272. * Translate UTF-8-MAC filenames in HFS Plus to non-Unicode
  1273. * filenames.
  1274. */
  1275. sc->cd = iconv_open(tc, "UTF-8-MAC");
  1276. if (sc->cd == (iconv_t)-1) {
  1277. if ((sc->flag & SCONV_BEST_EFFORT) &&
  1278. strcmp(tc, "CP932") == 0) {
  1279. sc->cd = iconv_open("SJIS", "UTF-8-MAC");
  1280. if (sc->cd == (iconv_t)-1) {
  1281. sc->cd = iconv_open(tc, fc);
  1282. if (sc->cd == (iconv_t)-1)
  1283. sc->cd = iconv_open("SJIS", fc);
  1284. }
  1285. } else
  1286. sc->cd = iconv_open(tc, fc);
  1287. }
  1288. #endif
  1289. } else {
  1290. sc->cd = iconv_open(tc, fc);
  1291. if (sc->cd == (iconv_t)-1 && (sc->flag & SCONV_BEST_EFFORT)) {
  1292. /*
  1293. * Unfortunaly, all of iconv implements do support
  1294. * "CP932" character-set, so we should use "SJIS"
  1295. * instead if iconv_open failed.
  1296. */
  1297. if (strcmp(tc, "CP932") == 0)
  1298. sc->cd = iconv_open("SJIS", fc);
  1299. else if (strcmp(fc, "CP932") == 0)
  1300. sc->cd = iconv_open(tc, "SJIS");
  1301. }
  1302. #if defined(_WIN32) && !defined(__CYGWIN__)
  1303. /*
  1304. * archive_mstring on Windows directly convert multi-bytes
  1305. * into archive_wstring in order not to depend on locale
  1306. * so that you can do a I18N programing. This will be
  1307. * used only in archive_mstring_copy_mbs_len_l so far.
  1308. */
  1309. if (flag & SCONV_FROM_CHARSET) {
  1310. sc->cd_w = iconv_open("UTF-8", fc);
  1311. if (sc->cd_w == (iconv_t)-1 &&
  1312. (sc->flag & SCONV_BEST_EFFORT)) {
  1313. if (strcmp(fc, "CP932") == 0)
  1314. sc->cd_w = iconv_open("UTF-8", "SJIS");
  1315. }
  1316. }
  1317. #endif /* _WIN32 && !__CYGWIN__ */
  1318. }
  1319. #endif /* HAVE_ICONV */
  1320. sc->flag = flag;
  1321. /*
  1322. * Setup converters.
  1323. */
  1324. setup_converter(sc);
  1325. return (sc);
  1326. }
  1327. /*
  1328. * Free a string conversion object.
  1329. */
  1330. static void
  1331. free_sconv_object(struct archive_string_conv *sc)
  1332. {
  1333. free(sc->from_charset);
  1334. free(sc->to_charset);
  1335. archive_string_free(&sc->utftmp);
  1336. #if HAVE_ICONV
  1337. if (sc->cd != (iconv_t)-1)
  1338. iconv_close(sc->cd);
  1339. if (sc->cd_w != (iconv_t)-1)
  1340. iconv_close(sc->cd_w);
  1341. #endif
  1342. #if defined(__APPLE__)
  1343. archive_string_free(&sc->utf16nfc);
  1344. archive_string_free(&sc->utf16nfd);
  1345. if (sc->uniInfo != NULL)
  1346. DisposeUnicodeToTextInfo(&(sc->uniInfo));
  1347. #endif
  1348. free(sc);
  1349. }
  1350. #if defined(_WIN32) && !defined(__CYGWIN__)
  1351. static unsigned
  1352. my_atoi(const char *p)
  1353. {
  1354. unsigned cp;
  1355. cp = 0;
  1356. while (*p) {
  1357. if (*p >= '0' && *p <= '9')
  1358. cp = cp * 10 + (*p - '0');
  1359. else
  1360. return (-1);
  1361. p++;
  1362. }
  1363. return (cp);
  1364. }
  1365. /*
  1366. * Translate Charset name (as used by iconv) into CodePage (as used by Windows)
  1367. * Return -1 if failed.
  1368. *
  1369. * Note: This translation code may be insufficient.
  1370. */
  1371. static struct charset {
  1372. const char *name;
  1373. unsigned cp;
  1374. } charsets[] = {
  1375. /* MUST BE SORTED! */
  1376. {"ASCII", 1252},
  1377. {"ASMO-708", 708},
  1378. {"BIG5", 950},
  1379. {"CHINESE", 936},
  1380. {"CP367", 1252},
  1381. {"CP819", 1252},
  1382. {"CP1025", 21025},
  1383. {"DOS-720", 720},
  1384. {"DOS-862", 862},
  1385. {"EUC-CN", 51936},
  1386. {"EUC-JP", 51932},
  1387. {"EUC-KR", 949},
  1388. {"EUCCN", 51936},
  1389. {"EUCJP", 51932},
  1390. {"EUCKR", 949},
  1391. {"GB18030", 54936},
  1392. {"GB2312", 936},
  1393. {"HEBREW", 1255},
  1394. {"HZ-GB-2312", 52936},
  1395. {"IBM273", 20273},
  1396. {"IBM277", 20277},
  1397. {"IBM278", 20278},
  1398. {"IBM280", 20280},
  1399. {"IBM284", 20284},
  1400. {"IBM285", 20285},
  1401. {"IBM290", 20290},
  1402. {"IBM297", 20297},
  1403. {"IBM367", 1252},
  1404. {"IBM420", 20420},
  1405. {"IBM423", 20423},
  1406. {"IBM424", 20424},
  1407. {"IBM819", 1252},
  1408. {"IBM871", 20871},
  1409. {"IBM880", 20880},
  1410. {"IBM905", 20905},
  1411. {"IBM924", 20924},
  1412. {"ISO-8859-1", 28591},
  1413. {"ISO-8859-13", 28603},
  1414. {"ISO-8859-15", 28605},
  1415. {"ISO-8859-2", 28592},
  1416. {"ISO-8859-3", 28593},
  1417. {"ISO-8859-4", 28594},
  1418. {"ISO-8859-5", 28595},
  1419. {"ISO-8859-6", 28596},
  1420. {"ISO-8859-7", 28597},
  1421. {"ISO-8859-8", 28598},
  1422. {"ISO-8859-9", 28599},
  1423. {"ISO8859-1", 28591},
  1424. {"ISO8859-13", 28603},
  1425. {"ISO8859-15", 28605},
  1426. {"ISO8859-2", 28592},
  1427. {"ISO8859-3", 28593},
  1428. {"ISO8859-4", 28594},
  1429. {"ISO8859-5", 28595},
  1430. {"ISO8859-6", 28596},
  1431. {"ISO8859-7", 28597},
  1432. {"ISO8859-8", 28598},
  1433. {"ISO8859-9", 28599},
  1434. {"JOHAB", 1361},
  1435. {"KOI8-R", 20866},
  1436. {"KOI8-U", 21866},
  1437. {"KS_C_5601-1987", 949},
  1438. {"LATIN1", 1252},
  1439. {"LATIN2", 28592},
  1440. {"MACINTOSH", 10000},
  1441. {"SHIFT-JIS", 932},
  1442. {"SHIFT_JIS", 932},
  1443. {"SJIS", 932},
  1444. {"US", 1252},
  1445. {"US-ASCII", 1252},
  1446. {"UTF-16", 1200},
  1447. {"UTF-16BE", 1201},
  1448. {"UTF-16LE", 1200},
  1449. {"UTF-8", CP_UTF8},
  1450. {"X-EUROPA", 29001},
  1451. {"X-MAC-ARABIC", 10004},
  1452. {"X-MAC-CE", 10029},
  1453. {"X-MAC-CHINESEIMP", 10008},
  1454. {"X-MAC-CHINESETRAD", 10002},
  1455. {"X-MAC-CROATIAN", 10082},
  1456. {"X-MAC-CYRILLIC", 10007},
  1457. {"X-MAC-GREEK", 10006},
  1458. {"X-MAC-HEBREW", 10005},
  1459. {"X-MAC-ICELANDIC", 10079},
  1460. {"X-MAC-JAPANESE", 10001},
  1461. {"X-MAC-KOREAN", 10003},
  1462. {"X-MAC-ROMANIAN", 10010},
  1463. {"X-MAC-THAI", 10021},
  1464. {"X-MAC-TURKISH", 10081},
  1465. {"X-MAC-UKRAINIAN", 10017},
  1466. };
  1467. static unsigned
  1468. make_codepage_from_charset(const char *charset)
  1469. {
  1470. char cs[16];
  1471. char *p;
  1472. unsigned cp;
  1473. int a, b;
  1474. if (charset == NULL || strlen(charset) > 15)
  1475. return -1;
  1476. /* Copy name to uppercase. */
  1477. p = cs;
  1478. while (*charset) {
  1479. char c = *charset++;
  1480. if (c >= 'a' && c <= 'z')
  1481. c -= 'a' - 'A';
  1482. *p++ = c;
  1483. }
  1484. *p++ = '\0';
  1485. cp = -1;
  1486. /* Look it up in the table first, so that we can easily
  1487. * override CP367, which we map to 1252 instead of 367. */
  1488. a = 0;
  1489. b = sizeof(charsets)/sizeof(charsets[0]);
  1490. while (b > a) {
  1491. int c = (b + a) / 2;
  1492. int r = strcmp(charsets[c].name, cs);
  1493. if (r < 0)
  1494. a = c + 1;
  1495. else if (r > 0)
  1496. b = c;
  1497. else
  1498. return charsets[c].cp;
  1499. }
  1500. /* If it's not in the table, try to parse it. */
  1501. switch (*cs) {
  1502. case 'C':
  1503. if (cs[1] == 'P' && cs[2] >= '0' && cs[2] <= '9') {
  1504. cp = my_atoi(cs + 2);
  1505. } else if (strcmp(cs, "CP_ACP") == 0)
  1506. cp = get_current_codepage();
  1507. else if (strcmp(cs, "CP_OEMCP") == 0)
  1508. cp = get_current_oemcp();
  1509. break;
  1510. case 'I':
  1511. if (cs[1] == 'B' && cs[2] == 'M' &&
  1512. cs[3] >= '0' && cs[3] <= '9') {
  1513. cp = my_atoi(cs + 3);
  1514. }
  1515. break;
  1516. case 'W':
  1517. if (strncmp(cs, "WINDOWS-", 8) == 0) {
  1518. cp = my_atoi(cs + 8);
  1519. if (cp != 874 && (cp < 1250 || cp > 1258))
  1520. cp = -1;/* This may invalid code. */
  1521. }
  1522. break;
  1523. }
  1524. return (cp);
  1525. }
  1526. /*
  1527. * Return ANSI Code Page of current locale set by setlocale().
  1528. */
  1529. static unsigned
  1530. get_current_codepage()
  1531. {
  1532. char *locale, *p;
  1533. unsigned cp;
  1534. locale = setlocale(LC_CTYPE, NULL);
  1535. if (locale == NULL)
  1536. return (GetACP());
  1537. if (locale[0] == 'C' && locale[1] == '\0')
  1538. return (CP_C_LOCALE);
  1539. p = strrchr(locale, '.');
  1540. if (p == NULL)
  1541. return (GetACP());
  1542. cp = my_atoi(p+1);
  1543. if (cp <= 0)
  1544. return (GetACP());
  1545. return (cp);
  1546. }
  1547. /*
  1548. * Translation table between Locale Name and ACP/OEMCP.
  1549. */
  1550. static struct {
  1551. unsigned acp;
  1552. unsigned ocp;
  1553. const char *locale;
  1554. } acp_ocp_map[] = {
  1555. { 950, 950, "Chinese_Taiwan" },
  1556. { 936, 936, "Chinese_People's Republic of China" },
  1557. { 950, 950, "Chinese_Taiwan" },
  1558. { 1250, 852, "Czech_Czech Republic" },
  1559. { 1252, 850, "Danish_Denmark" },
  1560. { 1252, 850, "Dutch_Netherlands" },
  1561. { 1252, 850, "Dutch_Belgium" },
  1562. { 1252, 437, "English_United States" },
  1563. { 1252, 850, "English_Australia" },
  1564. { 1252, 850, "English_Canada" },
  1565. { 1252, 850, "English_New Zealand" },
  1566. { 1252, 850, "English_United Kingdom" },
  1567. { 1252, 437, "English_United States" },
  1568. { 1252, 850, "Finnish_Finland" },
  1569. { 1252, 850, "French_France" },
  1570. { 1252, 850, "French_Belgium" },
  1571. { 1252, 850, "French_Canada" },
  1572. { 1252, 850, "French_Switzerland" },
  1573. { 1252, 850, "German_Germany" },
  1574. { 1252, 850, "German_Austria" },
  1575. { 1252, 850, "German_Switzerland" },
  1576. { 1253, 737, "Greek_Greece" },
  1577. { 1250, 852, "Hungarian_Hungary" },
  1578. { 1252, 850, "Icelandic_Iceland" },
  1579. { 1252, 850, "Italian_Italy" },
  1580. { 1252, 850, "Italian_Switzerland" },
  1581. { 932, 932, "Japanese_Japan" },
  1582. { 949, 949, "Korean_Korea" },
  1583. { 1252, 850, "Norwegian (BokmOl)_Norway" },
  1584. { 1252, 850, "Norwegian (BokmOl)_Norway" },
  1585. { 1252, 850, "Norwegian-Nynorsk_Norway" },
  1586. { 1250, 852, "Polish_Poland" },
  1587. { 1252, 850, "Portuguese_Portugal" },
  1588. { 1252, 850, "Portuguese_Brazil" },
  1589. { 1251, 866, "Russian_Russia" },
  1590. { 1250, 852, "Slovak_Slovakia" },
  1591. { 1252, 850, "Spanish_Spain" },
  1592. { 1252, 850, "Spanish_Mexico" },
  1593. { 1252, 850, "Spanish_Spain" },
  1594. { 1252, 850, "Swedish_Sweden" },
  1595. { 1254, 857, "Turkish_Turkey" },
  1596. { 0, 0, NULL}
  1597. };
  1598. /*
  1599. * Return OEM Code Page of current locale set by setlocale().
  1600. */
  1601. static unsigned
  1602. get_current_oemcp()
  1603. {
  1604. int i;
  1605. char *locale, *p;
  1606. size_t len;
  1607. locale = setlocale(LC_CTYPE, NULL);
  1608. if (locale == NULL)
  1609. return (GetOEMCP());
  1610. if (locale[0] == 'C' && locale[1] == '\0')
  1611. return (CP_C_LOCALE);
  1612. p = strrchr(locale, '.');
  1613. if (p == NULL)
  1614. return (GetOEMCP());
  1615. len = p - locale;
  1616. for (i = 0; acp_ocp_map[i].acp; i++) {
  1617. if (strncmp(acp_ocp_map[i].locale, locale, len) == 0)
  1618. return (acp_ocp_map[i].ocp);
  1619. }
  1620. return (GetOEMCP());
  1621. }
  1622. #else
  1623. /*
  1624. * POSIX platform does not use CodePage.
  1625. */
  1626. static unsigned
  1627. get_current_codepage()
  1628. {
  1629. return (-1);/* Unknown */
  1630. }
  1631. static unsigned
  1632. make_codepage_from_charset(const char *charset)
  1633. {
  1634. (void)charset; /* UNUSED */
  1635. return (-1);/* Unknown */
  1636. }
  1637. static unsigned
  1638. get_current_oemcp()
  1639. {
  1640. return (-1);/* Unknown */
  1641. }
  1642. #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
  1643. /*
  1644. * Return a string conversion object.
  1645. */
  1646. static struct archive_string_conv *
  1647. get_sconv_object(struct archive *a, const char *fc, const char *tc, int flag)
  1648. {
  1649. struct archive_string_conv *sc;
  1650. unsigned current_codepage;
  1651. /* Check if we have made the sconv object. */
  1652. sc = find_sconv_object(a, fc, tc);
  1653. if (sc != NULL)
  1654. return (sc);
  1655. if (a == NULL)
  1656. current_codepage = get_current_codepage();
  1657. else
  1658. current_codepage = a->current_codepage;
  1659. sc = create_sconv_object(canonical_charset_name(fc),
  1660. canonical_charset_name(tc), current_codepage, flag);
  1661. if (sc == NULL) {
  1662. if (a != NULL)
  1663. archive_set_error(a, ENOMEM,
  1664. "Could not allocate memory for "
  1665. "a string conversion object");
  1666. return (NULL);
  1667. }
  1668. /*
  1669. * If there is no converter for current string conversion object,
  1670. * we cannot handle this conversion.
  1671. */
  1672. if (sc->nconverter == 0) {
  1673. if (a != NULL) {
  1674. #if HAVE_ICONV
  1675. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1676. "iconv_open failed : Cannot handle ``%s''",
  1677. (flag & SCONV_TO_CHARSET)?tc:fc);
  1678. #else
  1679. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1680. "A character-set conversion not fully supported "
  1681. "on this platform");
  1682. #endif
  1683. }
  1684. /* Failed; free a sconv object. */
  1685. free_sconv_object(sc);
  1686. return (NULL);
  1687. }
  1688. /*
  1689. * Success!
  1690. */
  1691. if (a != NULL)
  1692. add_sconv_object(a, sc);
  1693. return (sc);
  1694. }
  1695. static const char *
  1696. get_current_charset(struct archive *a)
  1697. {
  1698. const char *cur_charset;
  1699. if (a == NULL)
  1700. cur_charset = default_iconv_charset("");
  1701. else {
  1702. cur_charset = default_iconv_charset(a->current_code);
  1703. if (a->current_code == NULL) {
  1704. a->current_code = strdup(cur_charset);
  1705. a->current_codepage = get_current_codepage();
  1706. a->current_oemcp = get_current_oemcp();
  1707. }
  1708. }
  1709. return (cur_charset);
  1710. }
  1711. /*
  1712. * Make and Return a string conversion object.
  1713. * Return NULL if the platform does not support the specified conversion
  1714. * and best_effort is 0.
  1715. * If best_effort is set, A string conversion object must be returned
  1716. * unless memory allocation for the object fails, but the conversion
  1717. * might fail when non-ASCII code is found.
  1718. */
  1719. struct archive_string_conv *
  1720. archive_string_conversion_to_charset(struct archive *a, const char *charset,
  1721. int best_effort)
  1722. {
  1723. int flag = SCONV_TO_CHARSET;
  1724. if (best_effort)
  1725. flag |= SCONV_BEST_EFFORT;
  1726. return (get_sconv_object(a, get_current_charset(a), charset, flag));
  1727. }
  1728. struct archive_string_conv *
  1729. archive_string_conversion_from_charset(struct archive *a, const char *charset,
  1730. int best_effort)
  1731. {
  1732. int flag = SCONV_FROM_CHARSET;
  1733. if (best_effort)
  1734. flag |= SCONV_BEST_EFFORT;
  1735. return (get_sconv_object(a, charset, get_current_charset(a), flag));
  1736. }
  1737. /*
  1738. * archive_string_default_conversion_*_archive() are provided for Windows
  1739. * platform because other archiver application use CP_OEMCP for
  1740. * MultiByteToWideChar() and WideCharToMultiByte() for the filenames
  1741. * in tar or zip files. But mbstowcs/wcstombs(CRT) usually use CP_ACP
  1742. * unless you use setlocale(LC_ALL, ".OCP")(specify CP_OEMCP).
  1743. * So we should make a string conversion between CP_ACP and CP_OEMCP
  1744. * for compatibillty.
  1745. */
  1746. #if defined(_WIN32) && !defined(__CYGWIN__)
  1747. struct archive_string_conv *
  1748. archive_string_default_conversion_for_read(struct archive *a)
  1749. {
  1750. const char *cur_charset = get_current_charset(a);
  1751. char oemcp[16];
  1752. /* NOTE: a check of cur_charset is unneeded but we need
  1753. * that get_current_charset() has been surely called at
  1754. * this time whatever C compiler optimized. */
  1755. if (cur_charset != NULL &&
  1756. (a->current_codepage == CP_C_LOCALE ||
  1757. a->current_codepage == a->current_oemcp))
  1758. return (NULL);/* no conversion. */
  1759. _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
  1760. /* Make sure a null termination must be set. */
  1761. oemcp[sizeof(oemcp)-1] = '\0';
  1762. return (get_sconv_object(a, oemcp, cur_charset,
  1763. SCONV_FROM_CHARSET));
  1764. }
  1765. struct archive_string_conv *
  1766. archive_string_default_conversion_for_write(struct archive *a)
  1767. {
  1768. const char *cur_charset = get_current_charset(a);
  1769. char oemcp[16];
  1770. /* NOTE: a check of cur_charset is unneeded but we need
  1771. * that get_current_charset() has been surely called at
  1772. * this time whatever C compiler optimized. */
  1773. if (cur_charset != NULL &&
  1774. (a->current_codepage == CP_C_LOCALE ||
  1775. a->current_codepage == a->current_oemcp))
  1776. return (NULL);/* no conversion. */
  1777. _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
  1778. /* Make sure a null termination must be set. */
  1779. oemcp[sizeof(oemcp)-1] = '\0';
  1780. return (get_sconv_object(a, cur_charset, oemcp,
  1781. SCONV_TO_CHARSET));
  1782. }
  1783. #else
  1784. struct archive_string_conv *
  1785. archive_string_default_conversion_for_read(struct archive *a)
  1786. {
  1787. (void)a; /* UNUSED */
  1788. return (NULL);
  1789. }
  1790. struct archive_string_conv *
  1791. archive_string_default_conversion_for_write(struct archive *a)
  1792. {
  1793. (void)a; /* UNUSED */
  1794. return (NULL);
  1795. }
  1796. #endif
  1797. /*
  1798. * Dispose of all character conversion objects in the archive object.
  1799. */
  1800. void
  1801. archive_string_conversion_free(struct archive *a)
  1802. {
  1803. struct archive_string_conv *sc;
  1804. struct archive_string_conv *sc_next;
  1805. for (sc = a->sconv; sc != NULL; sc = sc_next) {
  1806. sc_next = sc->next;
  1807. free_sconv_object(sc);
  1808. }
  1809. a->sconv = NULL;
  1810. free(a->current_code);
  1811. a->current_code = NULL;
  1812. }
  1813. /*
  1814. * Return a conversion charset name.
  1815. */
  1816. const char *
  1817. archive_string_conversion_charset_name(struct archive_string_conv *sc)
  1818. {
  1819. if (sc->flag & SCONV_TO_CHARSET)
  1820. return (sc->to_charset);
  1821. else
  1822. return (sc->from_charset);
  1823. }
  1824. /*
  1825. * Change the behavior of a string conversion.
  1826. */
  1827. void
  1828. archive_string_conversion_set_opt(struct archive_string_conv *sc, int opt)
  1829. {
  1830. switch (opt) {
  1831. /*
  1832. * A filename in UTF-8 was made with libarchive 2.x in a wrong
  1833. * assumption that wchar_t was Unicode.
  1834. * This option enables simulating the assumption in order to read
  1835. * that filname correctly.
  1836. */
  1837. case SCONV_SET_OPT_UTF8_LIBARCHIVE2X:
  1838. #if (defined(_WIN32) && !defined(__CYGWIN__)) \
  1839. || defined(__STDC_ISO_10646__) || defined(__APPLE__)
  1840. /*
  1841. * Nothing to do for it since wchar_t on these platforms
  1842. * is really Unicode.
  1843. */
  1844. (void)sc; /* UNUSED */
  1845. #else
  1846. if ((sc->flag & SCONV_UTF8_LIBARCHIVE_2) == 0) {
  1847. sc->flag |= SCONV_UTF8_LIBARCHIVE_2;
  1848. /* Re-setup string converters. */
  1849. setup_converter(sc);
  1850. }
  1851. #endif
  1852. break;
  1853. default:
  1854. break;
  1855. }
  1856. }
  1857. /*
  1858. *
  1859. * Copy one archive_string to another in locale conversion.
  1860. *
  1861. * archive_strncpy_in_locale();
  1862. * archive_strcpy_in_locale();
  1863. *
  1864. */
  1865. static size_t
  1866. mbsnbytes(const void *_p, size_t n)
  1867. {
  1868. size_t s;
  1869. const char *p, *pp;
  1870. if (_p == NULL)
  1871. return (0);
  1872. p = (const char *)_p;
  1873. /* Like strlen(p), except won't examine positions beyond p[n]. */
  1874. s = 0;
  1875. pp = p;
  1876. while (s < n && *pp) {
  1877. pp++;
  1878. s++;
  1879. }
  1880. return (s);
  1881. }
  1882. static size_t
  1883. utf16nbytes(const void *_p, size_t n)
  1884. {
  1885. size_t s;
  1886. const char *p, *pp;
  1887. if (_p == NULL)
  1888. return (0);
  1889. p = (const char *)_p;
  1890. /* Like strlen(p), except won't examine positions beyond p[n]. */
  1891. s = 0;
  1892. pp = p;
  1893. n >>= 1;
  1894. while (s < n && (pp[0] || pp[1])) {
  1895. pp += 2;
  1896. s++;
  1897. }
  1898. return (s<<1);
  1899. }
  1900. int
  1901. archive_strncpy_in_locale(struct archive_string *as, const void *_p, size_t n,
  1902. struct archive_string_conv *sc)
  1903. {
  1904. as->length = 0;
  1905. return (archive_strncat_in_locale(as, _p, n, sc));
  1906. }
  1907. int
  1908. archive_strncat_in_locale(struct archive_string *as, const void *_p, size_t n,
  1909. struct archive_string_conv *sc)
  1910. {
  1911. const void *s;
  1912. size_t length;
  1913. int i, r = 0, r2;
  1914. /* We must allocate memory even if there is no data for conversion
  1915. * or copy. This simulates archive_string_append behavior. */
  1916. if (_p == NULL || n == 0) {
  1917. int tn = 1;
  1918. if (sc != NULL && (sc->flag & SCONV_TO_UTF16))
  1919. tn = 2;
  1920. if (archive_string_ensure(as, as->length + tn) == NULL)
  1921. return (-1);
  1922. as->s[as->length] = 0;
  1923. if (tn == 2)
  1924. as->s[as->length+1] = 0;
  1925. return (0);
  1926. }
  1927. /*
  1928. * If sc is NULL, we just make a copy.
  1929. */
  1930. if (sc == NULL) {
  1931. length = mbsnbytes(_p, n);
  1932. if (archive_string_append(as, _p, length) == NULL)
  1933. return (-1);/* No memory */
  1934. return (0);
  1935. }
  1936. if (sc->flag & SCONV_FROM_UTF16)
  1937. length = utf16nbytes(_p, n);
  1938. else
  1939. length = mbsnbytes(_p, n);
  1940. s = _p;
  1941. i = 0;
  1942. if (sc->nconverter > 1) {
  1943. sc->utftmp.length = 0;
  1944. r2 = sc->converter[0](&(sc->utftmp), s, length, sc);
  1945. if (r2 != 0 && errno == ENOMEM)
  1946. return (r2);
  1947. if (r > r2)
  1948. r = r2;
  1949. s = sc->utftmp.s;
  1950. length = sc->utftmp.length;
  1951. ++i;
  1952. }
  1953. r2 = sc->converter[i](as, s, length, sc);
  1954. if (r > r2)
  1955. r = r2;
  1956. return (r);
  1957. }
  1958. #if HAVE_ICONV
  1959. /*
  1960. * Return -1 if conversion failes.
  1961. */
  1962. static int
  1963. iconv_strncat_in_locale(struct archive_string *as, const void *_p,
  1964. size_t length, struct archive_string_conv *sc)
  1965. {
  1966. ICONV_CONST char *inp;
  1967. size_t remaining;
  1968. iconv_t cd;
  1969. char *outp;
  1970. size_t avail, bs;
  1971. int return_value = 0; /* success */
  1972. int to_size, from_size;
  1973. if (sc->flag & SCONV_TO_UTF16)
  1974. to_size = 2;
  1975. else
  1976. to_size = 1;
  1977. if (sc->flag & SCONV_FROM_UTF16)
  1978. from_size = 2;
  1979. else
  1980. from_size = 1;
  1981. if (archive_string_ensure(as, as->length + length*2+to_size) == NULL)
  1982. return (-1);
  1983. cd = sc->cd;
  1984. inp = (char *)(uintptr_t)_p;
  1985. remaining = length;
  1986. outp = as->s + as->length;
  1987. avail = as->buffer_length - as->length - to_size;
  1988. while (remaining >= (size_t)from_size) {
  1989. size_t result = iconv(cd, &inp, &remaining, &outp, &avail);
  1990. if (result != (size_t)-1)
  1991. break; /* Conversion completed. */
  1992. if (errno == EILSEQ || errno == EINVAL) {
  1993. /*
  1994. * If an output charset is UTF-8 or UTF-16BE/LE,
  1995. * unknown character should be U+FFFD
  1996. * (replacement character).
  1997. */
  1998. if (sc->flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) {
  1999. size_t rbytes;
  2000. if (sc->flag & SCONV_TO_UTF8)
  2001. rbytes = UTF8_R_CHAR_SIZE;
  2002. else
  2003. rbytes = 2;
  2004. if (avail < rbytes) {
  2005. as->length = outp - as->s;
  2006. bs = as->buffer_length +
  2007. (remaining * to_size) + rbytes;
  2008. if (NULL ==
  2009. archive_string_ensure(as, bs))
  2010. return (-1);
  2011. outp = as->s + as->length;
  2012. avail = as->buffer_length
  2013. - as->length - to_size;
  2014. }
  2015. if (sc->flag & SCONV_TO_UTF8)
  2016. UTF8_SET_R_CHAR(outp);
  2017. else if (sc->flag & SCONV_TO_UTF16BE)
  2018. archive_be16enc(outp, UNICODE_R_CHAR);
  2019. else
  2020. archive_le16enc(outp, UNICODE_R_CHAR);
  2021. outp += rbytes;
  2022. avail -= rbytes;
  2023. } else {
  2024. /* Skip the illegal input bytes. */
  2025. *outp++ = '?';
  2026. avail--;
  2027. }
  2028. inp += from_size;
  2029. remaining -= from_size;
  2030. return_value = -1; /* failure */
  2031. } else {
  2032. /* E2BIG no output buffer,
  2033. * Increase an output buffer. */
  2034. as->length = outp - as->s;
  2035. bs = as->buffer_length + remaining * 2;
  2036. if (NULL == archive_string_ensure(as, bs))
  2037. return (-1);
  2038. outp = as->s + as->length;
  2039. avail = as->buffer_length - as->length - to_size;
  2040. }
  2041. }
  2042. as->length = outp - as->s;
  2043. as->s[as->length] = 0;
  2044. if (to_size == 2)
  2045. as->s[as->length+1] = 0;
  2046. return (return_value);
  2047. }
  2048. #endif /* HAVE_ICONV */
  2049. #if defined(_WIN32) && !defined(__CYGWIN__)
  2050. /*
  2051. * Translate a string from a some CodePage to an another CodePage by
  2052. * Windows APIs, and copy the result. Return -1 if conversion failes.
  2053. */
  2054. static int
  2055. strncat_in_codepage(struct archive_string *as,
  2056. const void *_p, size_t length, struct archive_string_conv *sc)
  2057. {
  2058. const char *s = (const char *)_p;
  2059. struct archive_wstring aws;
  2060. size_t l;
  2061. int r, saved_flag;
  2062. archive_string_init(&aws);
  2063. saved_flag = sc->flag;
  2064. sc->flag &= ~(SCONV_NORMALIZATION_D | SCONV_NORMALIZATION_C);
  2065. r = archive_wstring_append_from_mbs_in_codepage(&aws, s, length, sc);
  2066. sc->flag = saved_flag;
  2067. if (r != 0) {
  2068. archive_wstring_free(&aws);
  2069. if (errno != ENOMEM)
  2070. archive_string_append(as, s, length);
  2071. return (-1);
  2072. }
  2073. l = as->length;
  2074. r = archive_string_append_from_wcs_in_codepage(
  2075. as, aws.s, aws.length, sc);
  2076. if (r != 0 && errno != ENOMEM && l == as->length)
  2077. archive_string_append(as, s, length);
  2078. archive_wstring_free(&aws);
  2079. return (r);
  2080. }
  2081. /*
  2082. * Test whether MBS ==> WCS is okay.
  2083. */
  2084. static int
  2085. invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
  2086. {
  2087. const char *p = (const char *)_p;
  2088. unsigned codepage;
  2089. DWORD mbflag = MB_ERR_INVALID_CHARS;
  2090. if (sc->flag & SCONV_FROM_CHARSET)
  2091. codepage = sc->to_cp;
  2092. else
  2093. codepage = sc->from_cp;
  2094. if (codepage == CP_C_LOCALE)
  2095. return (0);
  2096. if (codepage != CP_UTF8)
  2097. mbflag |= MB_PRECOMPOSED;
  2098. if (MultiByteToWideChar(codepage, mbflag, p, n, NULL, 0) == 0)
  2099. return (-1); /* Invalid */
  2100. return (0); /* Okay */
  2101. }
  2102. #else
  2103. /*
  2104. * Test whether MBS ==> WCS is okay.
  2105. */
  2106. static int
  2107. invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
  2108. {
  2109. const char *p = (const char *)_p;
  2110. size_t r;
  2111. #if HAVE_MBRTOWC
  2112. mbstate_t shift_state;
  2113. memset(&shift_state, 0, sizeof(shift_state));
  2114. #else
  2115. /* Clear the shift state before starting. */
  2116. mbtowc(NULL, NULL, 0);
  2117. #endif
  2118. while (n) {
  2119. wchar_t wc;
  2120. #if HAVE_MBRTOWC
  2121. r = mbrtowc(&wc, p, n, &shift_state);
  2122. #else
  2123. r = mbtowc(&wc, p, n);
  2124. #endif
  2125. if (r == (size_t)-1 || r == (size_t)-2)
  2126. return (-1);/* Invalid. */
  2127. if (r == 0)
  2128. break;
  2129. p += r;
  2130. n -= r;
  2131. }
  2132. (void)sc; /* UNUSED */
  2133. return (0); /* All Okey. */
  2134. }
  2135. #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
  2136. /*
  2137. * Basically returns -1 because we cannot make a conversion of charset
  2138. * without iconv but in some cases this would return 0.
  2139. * Returns 0 if all copied characters are ASCII.
  2140. * Returns 0 if both from-locale and to-locale are the same and those
  2141. * can be WCS with no error.
  2142. */
  2143. static int
  2144. best_effort_strncat_in_locale(struct archive_string *as, const void *_p,
  2145. size_t length, struct archive_string_conv *sc)
  2146. {
  2147. size_t remaining;
  2148. char *outp;
  2149. const char *inp;
  2150. size_t avail;
  2151. int return_value = 0; /* success */
  2152. /*
  2153. * If both from-locale and to-locale is the same, this makes a copy.
  2154. * And then this checks all copied MBS can be WCS if so returns 0.
  2155. */
  2156. if (sc->same) {
  2157. if (archive_string_append(as, _p, length) == NULL)
  2158. return (-1);/* No memory */
  2159. return (invalid_mbs(_p, length, sc));
  2160. }
  2161. /*
  2162. * If a character is ASCII, this just copies it. If not, this
  2163. * assigns '?' charater instead but in UTF-8 locale this assigns
  2164. * byte sequence 0xEF 0xBD 0xBD, which are code point U+FFFD,
  2165. * a Replacement Character in Unicode.
  2166. */
  2167. if (archive_string_ensure(as, as->length + length + 1) == NULL)
  2168. return (-1);
  2169. remaining = length;
  2170. inp = (const char *)_p;
  2171. outp = as->s + as->length;
  2172. avail = as->buffer_length - as->length -1;
  2173. while (*inp && remaining > 0) {
  2174. if (*inp < 0 && (sc->flag & SCONV_TO_UTF8)) {
  2175. if (avail < UTF8_R_CHAR_SIZE) {
  2176. as->length = outp - as->s;
  2177. if (NULL == archive_string_ensure(as,
  2178. as->buffer_length + remaining +
  2179. UTF8_R_CHAR_SIZE))
  2180. return (-1);
  2181. outp = as->s + as->length;
  2182. avail = as->buffer_length - as->length -1;
  2183. }
  2184. /*
  2185. * When coping a string in UTF-8, unknown character
  2186. * should be U+FFFD (replacement character).
  2187. */
  2188. UTF8_SET_R_CHAR(outp);
  2189. outp += UTF8_R_CHAR_SIZE;
  2190. avail -= UTF8_R_CHAR_SIZE;
  2191. inp++;
  2192. remaining--;
  2193. return_value = -1;
  2194. } else if (*inp < 0) {
  2195. *outp++ = '?';
  2196. inp++;
  2197. remaining--;
  2198. return_value = -1;
  2199. } else {
  2200. *outp++ = *inp++;
  2201. remaining--;
  2202. }
  2203. }
  2204. as->length = outp - as->s;
  2205. as->s[as->length] = '\0';
  2206. return (return_value);
  2207. }
  2208. /*
  2209. * Unicode conversion functions.
  2210. * - UTF-8 <===> UTF-8 in removing surrogate pairs.
  2211. * - UTF-8 NFD ===> UTF-8 NFC in removing surrogate pairs.
  2212. * - UTF-8 made by libarchive 2.x ===> UTF-8.
  2213. * - UTF-16BE <===> UTF-8.
  2214. *
  2215. */
  2216. /*
  2217. * Utility to convert a single UTF-8 sequence.
  2218. *
  2219. * Usually return used bytes, return used byte in negative value when
  2220. * a unicode character is replaced with U+FFFD.
  2221. * See also http://unicode.org/review/pr-121.html Public Review Issue #121
  2222. * Recommended Practice for Replacement Characters.
  2223. */
  2224. static int
  2225. _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
  2226. {
  2227. static const char utf8_count[256] = {
  2228. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
  2229. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
  2230. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
  2231. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
  2232. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
  2233. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
  2234. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
  2235. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
  2236. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
  2237. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
  2238. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
  2239. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
  2240. 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
  2241. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
  2242. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
  2243. 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
  2244. };
  2245. int ch, i;
  2246. int cnt;
  2247. uint32_t wc;
  2248. /* Sanity check. */
  2249. if (n == 0)
  2250. return (0);
  2251. /*
  2252. * Decode 1-4 bytes depending on the value of the first byte.
  2253. */
  2254. ch = (unsigned char)*s;
  2255. if (ch == 0)
  2256. return (0); /* Standard: return 0 for end-of-string. */
  2257. cnt = utf8_count[ch];
  2258. /* Invalide sequence or there are not plenty bytes. */
  2259. if ((int)n < cnt) {
  2260. cnt = n;
  2261. for (i = 1; i < cnt; i++) {
  2262. if ((s[i] & 0xc0) != 0x80) {
  2263. cnt = i;
  2264. break;
  2265. }
  2266. }
  2267. goto invalid_sequence;
  2268. }
  2269. /* Make a Unicode code point from a single UTF-8 sequence. */
  2270. switch (cnt) {
  2271. case 1: /* 1 byte sequence. */
  2272. *pwc = ch & 0x7f;
  2273. return (cnt);
  2274. case 2: /* 2 bytes sequence. */
  2275. if ((s[1] & 0xc0) != 0x80) {
  2276. cnt = 1;
  2277. goto invalid_sequence;
  2278. }
  2279. *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
  2280. return (cnt);
  2281. case 3: /* 3 bytes sequence. */
  2282. if ((s[1] & 0xc0) != 0x80) {
  2283. cnt = 1;
  2284. goto invalid_sequence;
  2285. }
  2286. if ((s[2] & 0xc0) != 0x80) {
  2287. cnt = 2;
  2288. goto invalid_sequence;
  2289. }
  2290. wc = ((ch & 0x0f) << 12)
  2291. | ((s[1] & 0x3f) << 6)
  2292. | (s[2] & 0x3f);
  2293. if (wc < 0x800)
  2294. goto invalid_sequence;/* Overlong sequence. */
  2295. break;
  2296. case 4: /* 4 bytes sequence. */
  2297. if ((s[1] & 0xc0) != 0x80) {
  2298. cnt = 1;
  2299. goto invalid_sequence;
  2300. }
  2301. if ((s[2] & 0xc0) != 0x80) {
  2302. cnt = 2;
  2303. goto invalid_sequence;
  2304. }
  2305. if ((s[3] & 0xc0) != 0x80) {
  2306. cnt = 3;
  2307. goto invalid_sequence;
  2308. }
  2309. wc = ((ch & 0x07) << 18)
  2310. | ((s[1] & 0x3f) << 12)
  2311. | ((s[2] & 0x3f) << 6)
  2312. | (s[3] & 0x3f);
  2313. if (wc < 0x10000)
  2314. goto invalid_sequence;/* Overlong sequence. */
  2315. break;
  2316. default: /* Others are all invalid sequence. */
  2317. if (ch == 0xc0 || ch == 0xc1)
  2318. cnt = 2;
  2319. else if (ch >= 0xf5 && ch <= 0xf7)
  2320. cnt = 4;
  2321. else if (ch >= 0xf8 && ch <= 0xfb)
  2322. cnt = 5;
  2323. else if (ch == 0xfc || ch == 0xfd)
  2324. cnt = 6;
  2325. else
  2326. cnt = 1;
  2327. if ((int)n < cnt)
  2328. cnt = n;
  2329. for (i = 1; i < cnt; i++) {
  2330. if ((s[i] & 0xc0) != 0x80) {
  2331. cnt = i;
  2332. break;
  2333. }
  2334. }
  2335. goto invalid_sequence;
  2336. }
  2337. /* The code point larger than 0x10FFFF is not leagal
  2338. * Unicode values. */
  2339. if (wc > UNICODE_MAX)
  2340. goto invalid_sequence;
  2341. /* Correctly gets a Unicode, returns used bytes. */
  2342. *pwc = wc;
  2343. return (cnt);
  2344. invalid_sequence:
  2345. *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
  2346. return (cnt * -1);
  2347. }
  2348. static int
  2349. utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
  2350. {
  2351. int cnt;
  2352. cnt = _utf8_to_unicode(pwc, s, n);
  2353. /* Any of Surrogate pair is not leagal Unicode values. */
  2354. if (cnt == 3 && IS_SURROGATE_PAIR_LA(*pwc))
  2355. return (-3);
  2356. return (cnt);
  2357. }
  2358. static inline uint32_t
  2359. combine_surrogate_pair(uint32_t uc, uint32_t uc2)
  2360. {
  2361. uc -= 0xD800;
  2362. uc *= 0x400;
  2363. uc += uc2 - 0xDC00;
  2364. uc += 0x10000;
  2365. return (uc);
  2366. }
  2367. /*
  2368. * Convert a single UTF-8/CESU-8 sequence to a Unicode code point in
  2369. * removing surrogate pairs.
  2370. *
  2371. * CESU-8: The Compatibility Encoding Scheme for UTF-16.
  2372. *
  2373. * Usually return used bytes, return used byte in negative value when
  2374. * a unicode character is replaced with U+FFFD.
  2375. */
  2376. static int
  2377. cesu8_to_unicode(uint32_t *pwc, const char *s, size_t n)
  2378. {
  2379. uint32_t wc, wc2;
  2380. int cnt;
  2381. cnt = _utf8_to_unicode(&wc, s, n);
  2382. if (cnt == 3 && IS_HIGH_SURROGATE_LA(wc)) {
  2383. if (n - 3 < 3) {
  2384. /* Invalid byte sequence. */
  2385. goto invalid_sequence;
  2386. }
  2387. cnt = _utf8_to_unicode(&wc2, s+3, n-3);
  2388. if (cnt != 3 || !IS_LOW_SURROGATE_LA(wc2)) {
  2389. /* Invalid byte sequence. */
  2390. goto invalid_sequence;
  2391. }
  2392. wc = combine_surrogate_pair(wc, wc2);
  2393. cnt = 6;
  2394. } else if (cnt == 3 && IS_LOW_SURROGATE_LA(wc)) {
  2395. /* Invalid byte sequence. */
  2396. goto invalid_sequence;
  2397. }
  2398. *pwc = wc;
  2399. return (cnt);
  2400. invalid_sequence:
  2401. *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
  2402. if (cnt > 0)
  2403. cnt *= -1;
  2404. return (cnt);
  2405. }
  2406. /*
  2407. * Convert a Unicode code point to a single UTF-8 sequence.
  2408. *
  2409. * NOTE:This function does not check if the Unicode is leagal or not.
  2410. * Please you definitely check it before calling this.
  2411. */
  2412. static size_t
  2413. unicode_to_utf8(char *p, size_t remaining, uint32_t uc)
  2414. {
  2415. char *_p = p;
  2416. /* Translate code point to UTF8 */
  2417. if (uc <= 0x7f) {
  2418. if (remaining == 0)
  2419. return (0);
  2420. *p++ = (char)uc;
  2421. } else if (uc <= 0x7ff) {
  2422. if (remaining < 2)
  2423. return (0);
  2424. *p++ = 0xc0 | ((uc >> 6) & 0x1f);
  2425. *p++ = 0x80 | (uc & 0x3f);
  2426. } else if (uc <= 0xffff) {
  2427. if (remaining < 3)
  2428. return (0);
  2429. *p++ = 0xe0 | ((uc >> 12) & 0x0f);
  2430. *p++ = 0x80 | ((uc >> 6) & 0x3f);
  2431. *p++ = 0x80 | (uc & 0x3f);
  2432. } else if (uc <= UNICODE_MAX) {
  2433. if (remaining < 4)
  2434. return (0);
  2435. *p++ = 0xf0 | ((uc >> 18) & 0x07);
  2436. *p++ = 0x80 | ((uc >> 12) & 0x3f);
  2437. *p++ = 0x80 | ((uc >> 6) & 0x3f);
  2438. *p++ = 0x80 | (uc & 0x3f);
  2439. } else {
  2440. /*
  2441. * Undescribed code point should be U+FFFD
  2442. * (replacement character).
  2443. */
  2444. if (remaining < UTF8_R_CHAR_SIZE)
  2445. return (0);
  2446. UTF8_SET_R_CHAR(p);
  2447. p += UTF8_R_CHAR_SIZE;
  2448. }
  2449. return (p - _p);
  2450. }
  2451. static int
  2452. utf16be_to_unicode(uint32_t *pwc, const char *s, size_t n)
  2453. {
  2454. return (utf16_to_unicode(pwc, s, n, 1));
  2455. }
  2456. static int
  2457. utf16le_to_unicode(uint32_t *pwc, const char *s, size_t n)
  2458. {
  2459. return (utf16_to_unicode(pwc, s, n, 0));
  2460. }
  2461. static int
  2462. utf16_to_unicode(uint32_t *pwc, const char *s, size_t n, int be)
  2463. {
  2464. const char *utf16 = s;
  2465. unsigned uc;
  2466. if (n == 0)
  2467. return (0);
  2468. if (n == 1) {
  2469. /* set the Replacement Character instead. */
  2470. *pwc = UNICODE_R_CHAR;
  2471. return (-1);
  2472. }
  2473. if (be)
  2474. uc = archive_be16dec(utf16);
  2475. else
  2476. uc = archive_le16dec(utf16);
  2477. utf16 += 2;
  2478. /* If this is a surrogate pair, assemble the full code point.*/
  2479. if (IS_HIGH_SURROGATE_LA(uc)) {
  2480. unsigned uc2;
  2481. if (n >= 4) {
  2482. if (be)
  2483. uc2 = archive_be16dec(utf16);
  2484. else
  2485. uc2 = archive_le16dec(utf16);
  2486. } else
  2487. uc2 = 0;
  2488. if (IS_LOW_SURROGATE_LA(uc2)) {
  2489. uc = combine_surrogate_pair(uc, uc2);
  2490. utf16 += 2;
  2491. } else {
  2492. /* Undescribed code point should be U+FFFD
  2493. * (replacement character). */
  2494. *pwc = UNICODE_R_CHAR;
  2495. return (-2);
  2496. }
  2497. }
  2498. /*
  2499. * Surrogate pair values(0xd800 through 0xdfff) are only
  2500. * used by UTF-16, so, after above culculation, the code
  2501. * must not be surrogate values, and Unicode has no codes
  2502. * larger than 0x10ffff. Thus, those are not leagal Unicode
  2503. * values.
  2504. */
  2505. if (IS_SURROGATE_PAIR_LA(uc) || uc > UNICODE_MAX) {
  2506. /* Undescribed code point should be U+FFFD
  2507. * (replacement character). */
  2508. *pwc = UNICODE_R_CHAR;
  2509. return (((int)(utf16 - s)) * -1);
  2510. }
  2511. *pwc = uc;
  2512. return ((int)(utf16 - s));
  2513. }
  2514. static size_t
  2515. unicode_to_utf16be(char *p, size_t remaining, uint32_t uc)
  2516. {
  2517. char *utf16 = p;
  2518. if (uc > 0xffff) {
  2519. /* We have a code point that won't fit into a
  2520. * wchar_t; convert it to a surrogate pair. */
  2521. if (remaining < 4)
  2522. return (0);
  2523. uc -= 0x10000;
  2524. archive_be16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
  2525. archive_be16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
  2526. return (4);
  2527. } else {
  2528. if (remaining < 2)
  2529. return (0);
  2530. archive_be16enc(utf16, uc);
  2531. return (2);
  2532. }
  2533. }
  2534. static size_t
  2535. unicode_to_utf16le(char *p, size_t remaining, uint32_t uc)
  2536. {
  2537. char *utf16 = p;
  2538. if (uc > 0xffff) {
  2539. /* We have a code point that won't fit into a
  2540. * wchar_t; convert it to a surrogate pair. */
  2541. if (remaining < 4)
  2542. return (0);
  2543. uc -= 0x10000;
  2544. archive_le16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
  2545. archive_le16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
  2546. return (4);
  2547. } else {
  2548. if (remaining < 2)
  2549. return (0);
  2550. archive_le16enc(utf16, uc);
  2551. return (2);
  2552. }
  2553. }
  2554. /*
  2555. * Copy UTF-8 string in checking surrogate pair.
  2556. * If any surrogate pair are found, it would be canonicalized.
  2557. */
  2558. static int
  2559. strncat_from_utf8_to_utf8(struct archive_string *as, const void *_p, size_t len,
  2560. struct archive_string_conv *sc)
  2561. {
  2562. const char *s;
  2563. char *p, *endp;
  2564. int n, ret = 0;
  2565. (void)sc; /* UNUSED */
  2566. if (archive_string_ensure(as, as->length + len + 1) == NULL)
  2567. return (-1);
  2568. s = (const char *)_p;
  2569. p = as->s + as->length;
  2570. endp = as->s + as->buffer_length -1;
  2571. do {
  2572. uint32_t uc;
  2573. const char *ss = s;
  2574. size_t w;
  2575. /*
  2576. * Forward byte sequence until a conversion of that is needed.
  2577. */
  2578. while ((n = utf8_to_unicode(&uc, s, len)) > 0) {
  2579. s += n;
  2580. len -= n;
  2581. }
  2582. if (ss < s) {
  2583. if (p + (s - ss) > endp) {
  2584. as->length = p - as->s;
  2585. if (archive_string_ensure(as,
  2586. as->buffer_length + len + 1) == NULL)
  2587. return (-1);
  2588. p = as->s + as->length;
  2589. endp = as->s + as->buffer_length -1;
  2590. }
  2591. memcpy(p, ss, s - ss);
  2592. p += s - ss;
  2593. }
  2594. /*
  2595. * If n is negative, current byte sequence needs a replacement.
  2596. */
  2597. if (n < 0) {
  2598. if (n == -3 && IS_SURROGATE_PAIR_LA(uc)) {
  2599. /* Current byte sequence may be CESU-8. */
  2600. n = cesu8_to_unicode(&uc, s, len);
  2601. }
  2602. if (n < 0) {
  2603. ret = -1;
  2604. n *= -1;/* Use a replaced unicode character. */
  2605. }
  2606. /* Rebuild UTF-8 byte sequence. */
  2607. while ((w = unicode_to_utf8(p, endp - p, uc)) == 0) {
  2608. as->length = p - as->s;
  2609. if (archive_string_ensure(as,
  2610. as->buffer_length + len + 1) == NULL)
  2611. return (-1);
  2612. p = as->s + as->length;
  2613. endp = as->s + as->buffer_length -1;
  2614. }
  2615. p += w;
  2616. s += n;
  2617. len -= n;
  2618. }
  2619. } while (n > 0);
  2620. as->length = p - as->s;
  2621. as->s[as->length] = '\0';
  2622. return (ret);
  2623. }
  2624. static int
  2625. archive_string_append_unicode(struct archive_string *as, const void *_p,
  2626. size_t len, struct archive_string_conv *sc)
  2627. {
  2628. const char *s;
  2629. char *p, *endp;
  2630. uint32_t uc;
  2631. size_t w;
  2632. int n, ret = 0, ts, tm;
  2633. int (*parse)(uint32_t *, const char *, size_t);
  2634. size_t (*unparse)(char *, size_t, uint32_t);
  2635. if (sc->flag & SCONV_TO_UTF16BE) {
  2636. unparse = unicode_to_utf16be;
  2637. ts = 2;
  2638. } else if (sc->flag & SCONV_TO_UTF16LE) {
  2639. unparse = unicode_to_utf16le;
  2640. ts = 2;
  2641. } else if (sc->flag & SCONV_TO_UTF8) {
  2642. unparse = unicode_to_utf8;
  2643. ts = 1;
  2644. } else {
  2645. /*
  2646. * This case is going to be converted to another
  2647. * character-set through iconv.
  2648. */
  2649. if (sc->flag & SCONV_FROM_UTF16BE) {
  2650. unparse = unicode_to_utf16be;
  2651. ts = 2;
  2652. } else if (sc->flag & SCONV_FROM_UTF16LE) {
  2653. unparse = unicode_to_utf16le;
  2654. ts = 2;
  2655. } else {
  2656. unparse = unicode_to_utf8;
  2657. ts = 1;
  2658. }
  2659. }
  2660. if (sc->flag & SCONV_FROM_UTF16BE) {
  2661. parse = utf16be_to_unicode;
  2662. tm = 1;
  2663. } else if (sc->flag & SCONV_FROM_UTF16LE) {
  2664. parse = utf16le_to_unicode;
  2665. tm = 1;
  2666. } else {
  2667. parse = cesu8_to_unicode;
  2668. tm = ts;
  2669. }
  2670. if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
  2671. return (-1);
  2672. s = (const char *)_p;
  2673. p = as->s + as->length;
  2674. endp = as->s + as->buffer_length - ts;
  2675. while ((n = parse(&uc, s, len)) != 0) {
  2676. if (n < 0) {
  2677. /* Use a replaced unicode character. */
  2678. n *= -1;
  2679. ret = -1;
  2680. }
  2681. s += n;
  2682. len -= n;
  2683. while ((w = unparse(p, endp - p, uc)) == 0) {
  2684. /* There is not enough output buffer so
  2685. * we have to expand it. */
  2686. as->length = p - as->s;
  2687. if (archive_string_ensure(as,
  2688. as->buffer_length + len * tm + ts) == NULL)
  2689. return (-1);
  2690. p = as->s + as->length;
  2691. endp = as->s + as->buffer_length - ts;
  2692. }
  2693. p += w;
  2694. }
  2695. as->length = p - as->s;
  2696. as->s[as->length] = '\0';
  2697. if (ts == 2)
  2698. as->s[as->length+1] = '\0';
  2699. return (ret);
  2700. }
  2701. /*
  2702. * Following Constants for Hangul compositions this information comes from
  2703. * Unicode Standard Annex #15 http://unicode.org/reports/tr15/
  2704. */
  2705. #define HC_SBASE 0xAC00
  2706. #define HC_LBASE 0x1100
  2707. #define HC_VBASE 0x1161
  2708. #define HC_TBASE 0x11A7
  2709. #define HC_LCOUNT 19
  2710. #define HC_VCOUNT 21
  2711. #define HC_TCOUNT 28
  2712. #define HC_NCOUNT (HC_VCOUNT * HC_TCOUNT)
  2713. #define HC_SCOUNT (HC_LCOUNT * HC_NCOUNT)
  2714. static uint32_t
  2715. get_nfc(uint32_t uc, uint32_t uc2)
  2716. {
  2717. int t, b;
  2718. t = 0;
  2719. b = sizeof(u_composition_table)/sizeof(u_composition_table[0]) -1;
  2720. while (b >= t) {
  2721. int m = (t + b) / 2;
  2722. if (u_composition_table[m].cp1 < uc)
  2723. t = m + 1;
  2724. else if (u_composition_table[m].cp1 > uc)
  2725. b = m - 1;
  2726. else if (u_composition_table[m].cp2 < uc2)
  2727. t = m + 1;
  2728. else if (u_composition_table[m].cp2 > uc2)
  2729. b = m - 1;
  2730. else
  2731. return (u_composition_table[m].nfc);
  2732. }
  2733. return (0);
  2734. }
  2735. #define FDC_MAX 10 /* The maximum number of Following Decomposable
  2736. * Characters. */
  2737. /*
  2738. * Update first code point.
  2739. */
  2740. #define UPDATE_UC(new_uc) do { \
  2741. uc = new_uc; \
  2742. ucptr = NULL; \
  2743. } while (0)
  2744. /*
  2745. * Replace first code point with second code point.
  2746. */
  2747. #define REPLACE_UC_WITH_UC2() do { \
  2748. uc = uc2; \
  2749. ucptr = uc2ptr; \
  2750. n = n2; \
  2751. } while (0)
  2752. #define EXPAND_BUFFER() do { \
  2753. as->length = p - as->s; \
  2754. if (archive_string_ensure(as, \
  2755. as->buffer_length + len * tm + ts) == NULL)\
  2756. return (-1); \
  2757. p = as->s + as->length; \
  2758. endp = as->s + as->buffer_length - ts; \
  2759. } while (0)
  2760. #define UNPARSE(p, endp, uc) do { \
  2761. while ((w = unparse(p, (endp) - (p), uc)) == 0) {\
  2762. EXPAND_BUFFER(); \
  2763. } \
  2764. p += w; \
  2765. } while (0)
  2766. /*
  2767. * Write first code point.
  2768. * If the code point has not be changed from its original code,
  2769. * this just copies it from its original buffer pointer.
  2770. * If not, this converts it to UTF-8 byte sequence and copies it.
  2771. */
  2772. #define WRITE_UC() do { \
  2773. if (ucptr) { \
  2774. if (p + n > endp) \
  2775. EXPAND_BUFFER(); \
  2776. switch (n) { \
  2777. case 4: \
  2778. *p++ = *ucptr++; \
  2779. /* FALL THROUGH */ \
  2780. case 3: \
  2781. *p++ = *ucptr++; \
  2782. /* FALL THROUGH */ \
  2783. case 2: \
  2784. *p++ = *ucptr++; \
  2785. /* FALL THROUGH */ \
  2786. case 1: \
  2787. *p++ = *ucptr; \
  2788. break; \
  2789. } \
  2790. ucptr = NULL; \
  2791. } else { \
  2792. UNPARSE(p, endp, uc); \
  2793. } \
  2794. } while (0)
  2795. /*
  2796. * Collect following decomposable code points.
  2797. */
  2798. #define COLLECT_CPS(start) do { \
  2799. int _i; \
  2800. for (_i = start; _i < FDC_MAX ; _i++) { \
  2801. nx = parse(&ucx[_i], s, len); \
  2802. if (nx <= 0) \
  2803. break; \
  2804. cx = CCC(ucx[_i]); \
  2805. if (cl >= cx && cl != 228 && cx != 228)\
  2806. break; \
  2807. s += nx; \
  2808. len -= nx; \
  2809. cl = cx; \
  2810. ccx[_i] = cx; \
  2811. } \
  2812. if (_i >= FDC_MAX) { \
  2813. ret = -1; \
  2814. ucx_size = FDC_MAX; \
  2815. } else \
  2816. ucx_size = _i; \
  2817. } while (0)
  2818. /*
  2819. * Normalize UTF-8/UTF-16BE characters to Form C and copy the result.
  2820. *
  2821. * TODO: Convert composition exclusions,which are never converted
  2822. * from NFC,NFD,NFKC and NFKD, to Form C.
  2823. */
  2824. static int
  2825. archive_string_normalize_C(struct archive_string *as, const void *_p,
  2826. size_t len, struct archive_string_conv *sc)
  2827. {
  2828. const char *s = (const char *)_p;
  2829. char *p, *endp;
  2830. uint32_t uc, uc2;
  2831. size_t w;
  2832. int always_replace, n, n2, ret = 0, spair, ts, tm;
  2833. int (*parse)(uint32_t *, const char *, size_t);
  2834. size_t (*unparse)(char *, size_t, uint32_t);
  2835. always_replace = 1;
  2836. ts = 1;/* text size. */
  2837. if (sc->flag & SCONV_TO_UTF16BE) {
  2838. unparse = unicode_to_utf16be;
  2839. ts = 2;
  2840. if (sc->flag & SCONV_FROM_UTF16BE)
  2841. always_replace = 0;
  2842. } else if (sc->flag & SCONV_TO_UTF16LE) {
  2843. unparse = unicode_to_utf16le;
  2844. ts = 2;
  2845. if (sc->flag & SCONV_FROM_UTF16LE)
  2846. always_replace = 0;
  2847. } else if (sc->flag & SCONV_TO_UTF8) {
  2848. unparse = unicode_to_utf8;
  2849. if (sc->flag & SCONV_FROM_UTF8)
  2850. always_replace = 0;
  2851. } else {
  2852. /*
  2853. * This case is going to be converted to another
  2854. * character-set through iconv.
  2855. */
  2856. always_replace = 0;
  2857. if (sc->flag & SCONV_FROM_UTF16BE) {
  2858. unparse = unicode_to_utf16be;
  2859. ts = 2;
  2860. } else if (sc->flag & SCONV_FROM_UTF16LE) {
  2861. unparse = unicode_to_utf16le;
  2862. ts = 2;
  2863. } else {
  2864. unparse = unicode_to_utf8;
  2865. }
  2866. }
  2867. if (sc->flag & SCONV_FROM_UTF16BE) {
  2868. parse = utf16be_to_unicode;
  2869. tm = 1;
  2870. spair = 4;/* surrogate pair size in UTF-16. */
  2871. } else if (sc->flag & SCONV_FROM_UTF16LE) {
  2872. parse = utf16le_to_unicode;
  2873. tm = 1;
  2874. spair = 4;/* surrogate pair size in UTF-16. */
  2875. } else {
  2876. parse = cesu8_to_unicode;
  2877. tm = ts;
  2878. spair = 6;/* surrogate pair size in UTF-8. */
  2879. }
  2880. if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
  2881. return (-1);
  2882. p = as->s + as->length;
  2883. endp = as->s + as->buffer_length - ts;
  2884. while ((n = parse(&uc, s, len)) != 0) {
  2885. const char *ucptr, *uc2ptr;
  2886. if (n < 0) {
  2887. /* Use a replaced unicode character. */
  2888. UNPARSE(p, endp, uc);
  2889. s += n*-1;
  2890. len -= n*-1;
  2891. ret = -1;
  2892. continue;
  2893. } else if (n == spair || always_replace)
  2894. /* uc is converted from a surrogate pair.
  2895. * this should be treated as a changed code. */
  2896. ucptr = NULL;
  2897. else
  2898. ucptr = s;
  2899. s += n;
  2900. len -= n;
  2901. /* Read second code point. */
  2902. while ((n2 = parse(&uc2, s, len)) > 0) {
  2903. uint32_t ucx[FDC_MAX];
  2904. int ccx[FDC_MAX];
  2905. int cl, cx, i, nx, ucx_size;
  2906. int LIndex,SIndex;
  2907. uint32_t nfc;
  2908. if (n2 == spair || always_replace)
  2909. /* uc2 is converted from a surrogate pair.
  2910. * this should be treated as a changed code. */
  2911. uc2ptr = NULL;
  2912. else
  2913. uc2ptr = s;
  2914. s += n2;
  2915. len -= n2;
  2916. /*
  2917. * If current second code point is out of decomposable
  2918. * code points, finding compositions is unneeded.
  2919. */
  2920. if (!IS_DECOMPOSABLE_BLOCK(uc2)) {
  2921. WRITE_UC();
  2922. REPLACE_UC_WITH_UC2();
  2923. continue;
  2924. }
  2925. /*
  2926. * Try to combine current code points.
  2927. */
  2928. /*
  2929. * We have to combine Hangul characters according to
  2930. * http://uniicode.org/reports/tr15/#Hangul
  2931. */
  2932. if (0 <= (LIndex = uc - HC_LBASE) &&
  2933. LIndex < HC_LCOUNT) {
  2934. /*
  2935. * Hangul Composition.
  2936. * 1. Two current code points are L and V.
  2937. */
  2938. int VIndex = uc2 - HC_VBASE;
  2939. if (0 <= VIndex && VIndex < HC_VCOUNT) {
  2940. /* Make syllable of form LV. */
  2941. UPDATE_UC(HC_SBASE +
  2942. (LIndex * HC_VCOUNT + VIndex) *
  2943. HC_TCOUNT);
  2944. } else {
  2945. WRITE_UC();
  2946. REPLACE_UC_WITH_UC2();
  2947. }
  2948. continue;
  2949. } else if (0 <= (SIndex = uc - HC_SBASE) &&
  2950. SIndex < HC_SCOUNT && (SIndex % HC_TCOUNT) == 0) {
  2951. /*
  2952. * Hangul Composition.
  2953. * 2. Two current code points are LV and T.
  2954. */
  2955. int TIndex = uc2 - HC_TBASE;
  2956. if (0 < TIndex && TIndex < HC_TCOUNT) {
  2957. /* Make syllable of form LVT. */
  2958. UPDATE_UC(uc + TIndex);
  2959. } else {
  2960. WRITE_UC();
  2961. REPLACE_UC_WITH_UC2();
  2962. }
  2963. continue;
  2964. } else if ((nfc = get_nfc(uc, uc2)) != 0) {
  2965. /* A composition to current code points
  2966. * is found. */
  2967. UPDATE_UC(nfc);
  2968. continue;
  2969. } else if ((cl = CCC(uc2)) == 0) {
  2970. /* Clearly 'uc2' the second code point is not
  2971. * a decomposable code. */
  2972. WRITE_UC();
  2973. REPLACE_UC_WITH_UC2();
  2974. continue;
  2975. }
  2976. /*
  2977. * Collect following decomposable code points.
  2978. */
  2979. cx = 0;
  2980. ucx[0] = uc2;
  2981. ccx[0] = cl;
  2982. COLLECT_CPS(1);
  2983. /*
  2984. * Find a composed code in the collected code points.
  2985. */
  2986. i = 1;
  2987. while (i < ucx_size) {
  2988. int j;
  2989. if ((nfc = get_nfc(uc, ucx[i])) == 0) {
  2990. i++;
  2991. continue;
  2992. }
  2993. /*
  2994. * nfc is composed of uc and ucx[i].
  2995. */
  2996. UPDATE_UC(nfc);
  2997. /*
  2998. * Remove ucx[i] by shifting
  2999. * follwoing code points.
  3000. */
  3001. for (j = i; j+1 < ucx_size; j++) {
  3002. ucx[j] = ucx[j+1];
  3003. ccx[j] = ccx[j+1];
  3004. }
  3005. ucx_size --;
  3006. /*
  3007. * Collect following code points blocked
  3008. * by ucx[i] the removed code point.
  3009. */
  3010. if (ucx_size > 0 && i == ucx_size &&
  3011. nx > 0 && cx == cl) {
  3012. cl = ccx[ucx_size-1];
  3013. COLLECT_CPS(ucx_size);
  3014. }
  3015. /*
  3016. * Restart finding a composed code with
  3017. * the updated uc from the top of the
  3018. * collected code points.
  3019. */
  3020. i = 0;
  3021. }
  3022. /*
  3023. * Apparently the current code points are not
  3024. * decomposed characters or already composed.
  3025. */
  3026. WRITE_UC();
  3027. for (i = 0; i < ucx_size; i++)
  3028. UNPARSE(p, endp, ucx[i]);
  3029. /*
  3030. * Flush out remaining canonical combining characters.
  3031. */
  3032. if (nx > 0 && cx == cl && len > 0) {
  3033. while ((nx = parse(&ucx[0], s, len))
  3034. > 0) {
  3035. cx = CCC(ucx[0]);
  3036. if (cl > cx)
  3037. break;
  3038. s += nx;
  3039. len -= nx;
  3040. cl = cx;
  3041. UNPARSE(p, endp, ucx[0]);
  3042. }
  3043. }
  3044. break;
  3045. }
  3046. if (n2 < 0) {
  3047. WRITE_UC();
  3048. /* Use a replaced unicode character. */
  3049. UNPARSE(p, endp, uc2);
  3050. s += n2*-1;
  3051. len -= n2*-1;
  3052. ret = -1;
  3053. continue;
  3054. } else if (n2 == 0) {
  3055. WRITE_UC();
  3056. break;
  3057. }
  3058. }
  3059. as->length = p - as->s;
  3060. as->s[as->length] = '\0';
  3061. if (ts == 2)
  3062. as->s[as->length+1] = '\0';
  3063. return (ret);
  3064. }
  3065. #if defined(__APPLE__)
  3066. /*
  3067. * Normalize UTF-8 characters to Form D and copy the result.
  3068. */
  3069. static int
  3070. archive_string_normalize_D(struct archive_string *as, const void *_p,
  3071. size_t len, struct archive_string_conv *sc)
  3072. {
  3073. const UniChar *inp;
  3074. char *outp;
  3075. size_t newsize;
  3076. ByteCount inCount, outCount;
  3077. ByteCount inAvail, outAvail;
  3078. OSStatus err;
  3079. int ret, saved_flag;
  3080. /*
  3081. * Convert the current string to UTF-16LE for normalization.
  3082. * The character-set of the current string must be UTF-16BE or
  3083. * UTF-8.
  3084. */
  3085. archive_string_empty(&(sc->utf16nfc));
  3086. saved_flag = sc->flag;/* save a flag. */
  3087. sc->flag &= ~(SCONV_TO_UTF16BE | SCONV_TO_UTF8);
  3088. sc->flag |= SCONV_TO_UTF16LE;
  3089. ret = archive_string_append_unicode(&(sc->utf16nfc), _p, len, sc);
  3090. sc->flag = saved_flag;/* restore the saved flag */
  3091. if (archive_strlen(&(sc->utf16nfc)) == 0) {
  3092. if (archive_string_ensure(as, as->length + 1) == NULL)
  3093. return (-1);
  3094. return (ret);
  3095. }
  3096. /*
  3097. * Normalize an NFC string to be an NFD(HFS Plus version).
  3098. */
  3099. newsize = sc->utf16nfc.length + 2;
  3100. if (archive_string_ensure(&(sc->utf16nfd), newsize) == NULL)
  3101. return (-1);
  3102. inp = (UniChar *)sc->utf16nfc.s;
  3103. inAvail = archive_strlen(&(sc->utf16nfc));
  3104. sc->utf16nfd.length = 0;
  3105. outp = sc->utf16nfd.s;
  3106. outAvail = sc->utf16nfd.buffer_length -2;
  3107. do {
  3108. /* Reinitialize all state information. */
  3109. if (ResetUnicodeToTextInfo(sc->uniInfo) != noErr)
  3110. goto return_no_changed_data;
  3111. inCount = outCount = 0;
  3112. err = ConvertFromUnicodeToText(sc->uniInfo,
  3113. inAvail, inp,
  3114. kUnicodeDefaultDirectionMask, 0, NULL, NULL, NULL,
  3115. outAvail, &inCount, &outCount, outp);
  3116. if (err == noErr) {
  3117. sc->utf16nfd.length = outCount;
  3118. sc->utf16nfd.s[sc->utf16nfd.length] = 0;
  3119. sc->utf16nfd.s[sc->utf16nfd.length+1] = 0;
  3120. } else if (err == kTECOutputBufferFullStatus) {
  3121. newsize = inAvail - inCount;
  3122. if (newsize > inAvail)
  3123. newsize = inAvail;
  3124. newsize += sc->utf16nfd.buffer_length + 2;
  3125. if (archive_string_ensure(&(sc->utf16nfd), newsize)
  3126. == NULL)
  3127. return (-1);
  3128. outp = sc->utf16nfd.s;
  3129. outAvail = sc->utf16nfd.buffer_length -2;
  3130. } else
  3131. goto return_no_changed_data;
  3132. } while (err == kTECOutputBufferFullStatus);
  3133. /*
  3134. * If there is a next-step conversion, we should convert
  3135. * a UTF-16LE(NFD) string back to the original Unicode type.
  3136. */
  3137. saved_flag = sc->flag;/* save a flag. */
  3138. if (!(sc->flag &
  3139. (SCONV_TO_UTF16BE | SCONV_TO_UTF16LE | SCONV_TO_UTF8))) {
  3140. /*
  3141. * This case is going to be converted to another
  3142. * character-set through iconv.
  3143. */
  3144. if (sc->flag & SCONV_FROM_UTF16BE)
  3145. sc->flag |= SCONV_TO_UTF16BE;
  3146. else if (sc->flag & SCONV_FROM_UTF16LE)
  3147. sc->flag |= SCONV_TO_UTF16LE;
  3148. else
  3149. sc->flag |= SCONV_TO_UTF8;
  3150. }
  3151. sc->flag &= ~(SCONV_FROM_UTF16BE | SCONV_FROM_UTF8);
  3152. sc->flag |= SCONV_FROM_UTF16LE;
  3153. if (archive_string_append_unicode(as, sc->utf16nfd.s,
  3154. sc->utf16nfd.length, sc) != 0)
  3155. ret = -1;
  3156. sc->flag = saved_flag;/* restore the saved flag */
  3157. return (ret);
  3158. return_no_changed_data:
  3159. /*
  3160. * Something conversion error happend, so we return a no normalized
  3161. * string with an error.
  3162. */
  3163. (void)archive_string_append_unicode(as, _p, len, sc);
  3164. return (-1);
  3165. }
  3166. #endif /* __APPLE__ */
  3167. /*
  3168. * libarchive 2.x made incorrect UTF-8 strings in the wrong assumuption
  3169. * that WCS is Unicode. it is true for servel platforms but some are false.
  3170. * And then people who did not use UTF-8 locale on the non Unicode WCS
  3171. * platform and made a tar file with libarchive(mostly bsdtar) 2.x. Those
  3172. * now cannot get right filename from libarchive 3.x and later since we
  3173. * fixed the wrong assumption and it is incompatible to older its versions.
  3174. * So we provide special option, "compat-2x.x", for resolving it.
  3175. * That option enable the string conversion of libarchive 2.x.
  3176. *
  3177. * Translates the wrong UTF-8 string made by libarchive 2.x into current
  3178. * locale character set and appends to the archive_string.
  3179. * Note: returns -1 if conversion fails.
  3180. */
  3181. static int
  3182. strncat_from_utf8_libarchive2(struct archive_string *as,
  3183. const void *_p, size_t len, struct archive_string_conv *sc)
  3184. {
  3185. const char *s;
  3186. int n;
  3187. char *p;
  3188. char *end;
  3189. uint32_t unicode;
  3190. #if HAVE_WCRTOMB
  3191. mbstate_t shift_state;
  3192. memset(&shift_state, 0, sizeof(shift_state));
  3193. #else
  3194. /* Clear the shift state before starting. */
  3195. wctomb(NULL, L'\0');
  3196. #endif
  3197. (void)sc; /* UNUSED */
  3198. /*
  3199. * Allocate buffer for MBS.
  3200. * We need this allocation here since it is possible that
  3201. * as->s is still NULL.
  3202. */
  3203. if (archive_string_ensure(as, as->length + len + 1) == NULL)
  3204. return (-1);
  3205. s = (const char *)_p;
  3206. p = as->s + as->length;
  3207. end = as->s + as->buffer_length - MB_CUR_MAX -1;
  3208. while ((n = _utf8_to_unicode(&unicode, s, len)) != 0) {
  3209. wchar_t wc;
  3210. if (p >= end) {
  3211. as->length = p - as->s;
  3212. /* Re-allocate buffer for MBS. */
  3213. if (archive_string_ensure(as,
  3214. as->length + len * 2 + 1) == NULL)
  3215. return (-1);
  3216. p = as->s + as->length;
  3217. end = as->s + as->buffer_length - MB_CUR_MAX -1;
  3218. }
  3219. /*
  3220. * As libarchie 2.x, translates the UTF-8 characters into
  3221. * wide-characters in the assumption that WCS is Unicode.
  3222. */
  3223. if (n < 0) {
  3224. n *= -1;
  3225. wc = L'?';
  3226. } else
  3227. wc = (wchar_t)unicode;
  3228. s += n;
  3229. len -= n;
  3230. /*
  3231. * Translates the wide-character into the current locale MBS.
  3232. */
  3233. #if HAVE_WCRTOMB
  3234. n = wcrtomb(p, wc, &shift_state);
  3235. #else
  3236. n = wctomb(p, wc);
  3237. #endif
  3238. if (n == -1)
  3239. return (-1);
  3240. p += n;
  3241. }
  3242. as->length = p - as->s;
  3243. as->s[as->length] = '\0';
  3244. return (0);
  3245. }
  3246. /*
  3247. * Conversion functions between current locale dependent MBS and UTF-16BE.
  3248. * strncat_from_utf16be() : UTF-16BE --> MBS
  3249. * strncat_to_utf16be() : MBS --> UTF16BE
  3250. */
  3251. #if defined(_WIN32) && !defined(__CYGWIN__)
  3252. /*
  3253. * Convert a UTF-16BE/LE string to current locale and copy the result.
  3254. * Return -1 if conversion failes.
  3255. */
  3256. static int
  3257. win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
  3258. struct archive_string_conv *sc, int be)
  3259. {
  3260. struct archive_string tmp;
  3261. const char *u16;
  3262. int ll;
  3263. BOOL defchar;
  3264. char *mbs;
  3265. size_t mbs_size, b;
  3266. int ret = 0;
  3267. bytes &= ~1;
  3268. if (archive_string_ensure(as, as->length + bytes +1) == NULL)
  3269. return (-1);
  3270. mbs = as->s + as->length;
  3271. mbs_size = as->buffer_length - as->length -1;
  3272. if (sc->to_cp == CP_C_LOCALE) {
  3273. /*
  3274. * "C" locale special process.
  3275. */
  3276. u16 = _p;
  3277. ll = 0;
  3278. for (b = 0; b < bytes; b += 2) {
  3279. uint16_t val;
  3280. if (be)
  3281. val = archive_be16dec(u16+b);
  3282. else
  3283. val = archive_le16dec(u16+b);
  3284. if (val > 255) {
  3285. *mbs++ = '?';
  3286. ret = -1;
  3287. } else
  3288. *mbs++ = (char)(val&0xff);
  3289. ll++;
  3290. }
  3291. as->length += ll;
  3292. as->s[as->length] = '\0';
  3293. return (ret);
  3294. }
  3295. archive_string_init(&tmp);
  3296. if (be) {
  3297. if (is_big_endian()) {
  3298. u16 = _p;
  3299. } else {
  3300. if (archive_string_ensure(&tmp, bytes+2) == NULL)
  3301. return (-1);
  3302. memcpy(tmp.s, _p, bytes);
  3303. for (b = 0; b < bytes; b += 2) {
  3304. uint16_t val = archive_be16dec(tmp.s+b);
  3305. archive_le16enc(tmp.s+b, val);
  3306. }
  3307. u16 = tmp.s;
  3308. }
  3309. } else {
  3310. if (!is_big_endian()) {
  3311. u16 = _p;
  3312. } else {
  3313. if (archive_string_ensure(&tmp, bytes+2) == NULL)
  3314. return (-1);
  3315. memcpy(tmp.s, _p, bytes);
  3316. for (b = 0; b < bytes; b += 2) {
  3317. uint16_t val = archive_le16dec(tmp.s+b);
  3318. archive_be16enc(tmp.s+b, val);
  3319. }
  3320. u16 = tmp.s;
  3321. }
  3322. }
  3323. do {
  3324. defchar = 0;
  3325. ll = WideCharToMultiByte(sc->to_cp, 0,
  3326. (LPCWSTR)u16, bytes>>1, mbs, mbs_size,
  3327. NULL, &defchar);
  3328. if (ll == 0 &&
  3329. GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  3330. /* Need more buffer for MBS. */
  3331. ll = WideCharToMultiByte(sc->to_cp, 0,
  3332. (LPCWSTR)u16, bytes, NULL, 0, NULL, NULL);
  3333. if (archive_string_ensure(as, ll +1) == NULL)
  3334. return (-1);
  3335. mbs = as->s + as->length;
  3336. mbs_size = as->buffer_length - as->length -1;
  3337. continue;
  3338. }
  3339. } while (0);
  3340. archive_string_free(&tmp);
  3341. as->length += ll;
  3342. as->s[as->length] = '\0';
  3343. if (ll == 0 || defchar)
  3344. ret = -1;
  3345. return (ret);
  3346. }
  3347. static int
  3348. win_strncat_from_utf16be(struct archive_string *as, const void *_p, size_t bytes,
  3349. struct archive_string_conv *sc)
  3350. {
  3351. return (win_strncat_from_utf16(as, _p, bytes, sc, 1));
  3352. }
  3353. static int
  3354. win_strncat_from_utf16le(struct archive_string *as, const void *_p, size_t bytes,
  3355. struct archive_string_conv *sc)
  3356. {
  3357. return (win_strncat_from_utf16(as, _p, bytes, sc, 0));
  3358. }
  3359. static int
  3360. is_big_endian(void)
  3361. {
  3362. uint16_t d = 1;
  3363. return (archive_be16dec(&d) == 1);
  3364. }
  3365. /*
  3366. * Convert a current locale string to UTF-16BE/LE and copy the result.
  3367. * Return -1 if conversion failes.
  3368. */
  3369. static int
  3370. win_strncat_to_utf16(struct archive_string *as16, const void *_p, size_t length,
  3371. struct archive_string_conv *sc, int bigendian)
  3372. {
  3373. const char *s = (const char *)_p;
  3374. char *u16;
  3375. size_t count, avail;
  3376. if (archive_string_ensure(as16,
  3377. as16->length + (length + 1) * 2) == NULL)
  3378. return (-1);
  3379. u16 = as16->s + as16->length;
  3380. avail = as16->buffer_length - 2;
  3381. if (sc->from_cp == CP_C_LOCALE) {
  3382. /*
  3383. * "C" locale special process.
  3384. */
  3385. count = 0;
  3386. while (count < length && *s) {
  3387. if (bigendian)
  3388. archive_be16enc(u16, *s);
  3389. else
  3390. archive_le16enc(u16, *s);
  3391. u16 += 2;
  3392. s++;
  3393. count++;
  3394. }
  3395. as16->length += count << 1;
  3396. as16->s[as16->length] = 0;
  3397. as16->s[as16->length+1] = 0;
  3398. return (0);
  3399. }
  3400. do {
  3401. count = MultiByteToWideChar(sc->from_cp,
  3402. MB_PRECOMPOSED, s, length, (LPWSTR)u16, (int)avail>>1);
  3403. if (count == 0 &&
  3404. GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  3405. /* Need more buffer for UTF-16 string */
  3406. count = MultiByteToWideChar(sc->from_cp,
  3407. MB_PRECOMPOSED, s, length, NULL, 0);
  3408. if (archive_string_ensure(as16, (count +1) * 2)
  3409. == NULL)
  3410. return (-1);
  3411. u16 = as16->s + as16->length;
  3412. avail = as16->buffer_length - 2;
  3413. continue;
  3414. }
  3415. } while (0);
  3416. as16->length += count * 2;
  3417. as16->s[as16->length] = 0;
  3418. as16->s[as16->length+1] = 0;
  3419. if (count == 0)
  3420. return (-1);
  3421. if (is_big_endian()) {
  3422. if (!bigendian) {
  3423. while (count > 0) {
  3424. uint16_t v = archive_be16dec(u16);
  3425. archive_le16enc(u16, v);
  3426. u16 += 2;
  3427. count--;
  3428. }
  3429. }
  3430. } else {
  3431. if (bigendian) {
  3432. while (count > 0) {
  3433. uint16_t v = archive_le16dec(u16);
  3434. archive_be16enc(u16, v);
  3435. u16 += 2;
  3436. count--;
  3437. }
  3438. }
  3439. }
  3440. return (0);
  3441. }
  3442. static int
  3443. win_strncat_to_utf16be(struct archive_string *as16, const void *_p, size_t length,
  3444. struct archive_string_conv *sc)
  3445. {
  3446. return (win_strncat_to_utf16(as16, _p, length, sc, 1));
  3447. }
  3448. static int
  3449. win_strncat_to_utf16le(struct archive_string *as16, const void *_p, size_t length,
  3450. struct archive_string_conv *sc)
  3451. {
  3452. return (win_strncat_to_utf16(as16, _p, length, sc, 0));
  3453. }
  3454. #endif /* _WIN32 && !__CYGWIN__ */
  3455. /*
  3456. * Do the best effort for conversions.
  3457. * We cannot handle UTF-16BE character-set without such iconv,
  3458. * but there is a chance if a string consists just ASCII code or
  3459. * a current locale is UTF-8.
  3460. */
  3461. /*
  3462. * Convert a UTF-16BE string to current locale and copy the result.
  3463. * Return -1 if conversion failes.
  3464. */
  3465. static int
  3466. best_effort_strncat_from_utf16(struct archive_string *as, const void *_p,
  3467. size_t bytes, struct archive_string_conv *sc, int be)
  3468. {
  3469. const char *utf16 = (const char *)_p;
  3470. char *mbs;
  3471. uint32_t uc;
  3472. int n, ret;
  3473. (void)sc; /* UNUSED */
  3474. /*
  3475. * Other case, we should do the best effort.
  3476. * If all character are ASCII(<0x7f), we can convert it.
  3477. * if not , we set a alternative character and return -1.
  3478. */
  3479. ret = 0;
  3480. if (archive_string_ensure(as, as->length + bytes +1) == NULL)
  3481. return (-1);
  3482. mbs = as->s + as->length;
  3483. while ((n = utf16_to_unicode(&uc, utf16, bytes, be)) != 0) {
  3484. if (n < 0) {
  3485. n *= -1;
  3486. ret = -1;
  3487. }
  3488. bytes -= n;
  3489. utf16 += n;
  3490. if (uc > 127) {
  3491. /* We cannot handle it. */
  3492. *mbs++ = '?';
  3493. ret = -1;
  3494. } else
  3495. *mbs++ = (char)uc;
  3496. }
  3497. as->length = mbs - as->s;
  3498. as->s[as->length] = '\0';
  3499. return (ret);
  3500. }
  3501. static int
  3502. best_effort_strncat_from_utf16be(struct archive_string *as, const void *_p,
  3503. size_t bytes, struct archive_string_conv *sc)
  3504. {
  3505. return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 1));
  3506. }
  3507. static int
  3508. best_effort_strncat_from_utf16le(struct archive_string *as, const void *_p,
  3509. size_t bytes, struct archive_string_conv *sc)
  3510. {
  3511. return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 0));
  3512. }
  3513. /*
  3514. * Convert a current locale string to UTF-16BE/LE and copy the result.
  3515. * Return -1 if conversion failes.
  3516. */
  3517. static int
  3518. best_effort_strncat_to_utf16(struct archive_string *as16, const void *_p,
  3519. size_t length, struct archive_string_conv *sc, int bigendian)
  3520. {
  3521. const char *s = (const char *)_p;
  3522. char *utf16;
  3523. size_t remaining;
  3524. int ret;
  3525. (void)sc; /* UNUSED */
  3526. /*
  3527. * Other case, we should do the best effort.
  3528. * If all character are ASCII(<0x7f), we can convert it.
  3529. * if not , we set a alternative character and return -1.
  3530. */
  3531. ret = 0;
  3532. remaining = length;
  3533. if (archive_string_ensure(as16,
  3534. as16->length + (length + 1) * 2) == NULL)
  3535. return (-1);
  3536. utf16 = as16->s + as16->length;
  3537. while (remaining--) {
  3538. unsigned c = *s++;
  3539. if (c > 127) {
  3540. /* We cannot handle it. */
  3541. c = UNICODE_R_CHAR;
  3542. ret = -1;
  3543. }
  3544. if (bigendian)
  3545. archive_be16enc(utf16, c);
  3546. else
  3547. archive_le16enc(utf16, c);
  3548. utf16 += 2;
  3549. }
  3550. as16->length = utf16 - as16->s;
  3551. as16->s[as16->length] = 0;
  3552. as16->s[as16->length+1] = 0;
  3553. return (ret);
  3554. }
  3555. static int
  3556. best_effort_strncat_to_utf16be(struct archive_string *as16, const void *_p,
  3557. size_t length, struct archive_string_conv *sc)
  3558. {
  3559. return (best_effort_strncat_to_utf16(as16, _p, length, sc, 1));
  3560. }
  3561. static int
  3562. best_effort_strncat_to_utf16le(struct archive_string *as16, const void *_p,
  3563. size_t length, struct archive_string_conv *sc)
  3564. {
  3565. return (best_effort_strncat_to_utf16(as16, _p, length, sc, 0));
  3566. }
  3567. /*
  3568. * Multistring operations.
  3569. */
  3570. void
  3571. archive_mstring_clean(struct archive_mstring *aes)
  3572. {
  3573. archive_wstring_free(&(aes->aes_wcs));
  3574. archive_string_free(&(aes->aes_mbs));
  3575. archive_string_free(&(aes->aes_utf8));
  3576. archive_string_free(&(aes->aes_mbs_in_locale));
  3577. aes->aes_set = 0;
  3578. }
  3579. void
  3580. archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src)
  3581. {
  3582. dest->aes_set = src->aes_set;
  3583. archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
  3584. archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
  3585. archive_wstring_copy(&(dest->aes_wcs), &(src->aes_wcs));
  3586. }
  3587. int
  3588. archive_mstring_get_utf8(struct archive *a, struct archive_mstring *aes,
  3589. const char **p)
  3590. {
  3591. struct archive_string_conv *sc;
  3592. int r;
  3593. /* If we already have a UTF8 form, return that immediately. */
  3594. if (aes->aes_set & AES_SET_UTF8) {
  3595. *p = aes->aes_utf8.s;
  3596. return (0);
  3597. }
  3598. *p = NULL;
  3599. if (aes->aes_set & AES_SET_MBS) {
  3600. sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
  3601. if (sc == NULL)
  3602. return (-1);/* Couldn't allocate memory for sc. */
  3603. r = archive_strncpy_in_locale(&(aes->aes_mbs), aes->aes_mbs.s,
  3604. aes->aes_mbs.length, sc);
  3605. if (a == NULL)
  3606. free_sconv_object(sc);
  3607. if (r == 0) {
  3608. aes->aes_set |= AES_SET_UTF8;
  3609. *p = aes->aes_utf8.s;
  3610. return (0);/* success. */
  3611. } else
  3612. return (-1);/* failure. */
  3613. }
  3614. return (0);/* success. */
  3615. }
  3616. int
  3617. archive_mstring_get_mbs(struct archive *a, struct archive_mstring *aes,
  3618. const char **p)
  3619. {
  3620. int r, ret = 0;
  3621. (void)a; /* UNUSED */
  3622. /* If we already have an MBS form, return that immediately. */
  3623. if (aes->aes_set & AES_SET_MBS) {
  3624. *p = aes->aes_mbs.s;
  3625. return (ret);
  3626. }
  3627. *p = NULL;
  3628. /* If there's a WCS form, try converting with the native locale. */
  3629. if (aes->aes_set & AES_SET_WCS) {
  3630. archive_string_empty(&(aes->aes_mbs));
  3631. r = archive_string_append_from_wcs(&(aes->aes_mbs),
  3632. aes->aes_wcs.s, aes->aes_wcs.length);
  3633. *p = aes->aes_mbs.s;
  3634. if (r == 0) {
  3635. aes->aes_set |= AES_SET_MBS;
  3636. return (ret);
  3637. } else
  3638. ret = -1;
  3639. }
  3640. /*
  3641. * Only a UTF-8 form cannot avail because its conversion already
  3642. * failed at archive_mstring_update_utf8().
  3643. */
  3644. return (ret);
  3645. }
  3646. int
  3647. archive_mstring_get_wcs(struct archive *a, struct archive_mstring *aes,
  3648. const wchar_t **wp)
  3649. {
  3650. int r, ret = 0;
  3651. (void)a;/* UNUSED */
  3652. /* Return WCS form if we already have it. */
  3653. if (aes->aes_set & AES_SET_WCS) {
  3654. *wp = aes->aes_wcs.s;
  3655. return (ret);
  3656. }
  3657. *wp = NULL;
  3658. /* Try converting MBS to WCS using native locale. */
  3659. if (aes->aes_set & AES_SET_MBS) {
  3660. archive_wstring_empty(&(aes->aes_wcs));
  3661. r = archive_wstring_append_from_mbs(&(aes->aes_wcs),
  3662. aes->aes_mbs.s, aes->aes_mbs.length);
  3663. if (r == 0) {
  3664. aes->aes_set |= AES_SET_WCS;
  3665. *wp = aes->aes_wcs.s;
  3666. } else
  3667. ret = -1;/* failure. */
  3668. }
  3669. return (ret);
  3670. }
  3671. int
  3672. archive_mstring_get_mbs_l(struct archive_mstring *aes,
  3673. const char **p, size_t *length, struct archive_string_conv *sc)
  3674. {
  3675. int r, ret = 0;
  3676. #if defined(_WIN32) && !defined(__CYGWIN__)
  3677. /*
  3678. * Internationalization programing on Windows must use Wide
  3679. * characters because Windows platform cannot make locale UTF-8.
  3680. */
  3681. if (sc != NULL && (aes->aes_set & AES_SET_WCS) != 0) {
  3682. archive_string_empty(&(aes->aes_mbs_in_locale));
  3683. r = archive_string_append_from_wcs_in_codepage(
  3684. &(aes->aes_mbs_in_locale), aes->aes_wcs.s,
  3685. aes->aes_wcs.length, sc);
  3686. if (r == 0) {
  3687. *p = aes->aes_mbs_in_locale.s;
  3688. if (length != NULL)
  3689. *length = aes->aes_mbs_in_locale.length;
  3690. return (0);
  3691. } else if (errno == ENOMEM)
  3692. return (-1);
  3693. else
  3694. ret = -1;
  3695. }
  3696. #endif
  3697. /* If there is not an MBS form but is a WCS form, try converting
  3698. * with the native locale to be used for translating it to specified
  3699. * character-set. */
  3700. if ((aes->aes_set & AES_SET_MBS) == 0 &&
  3701. (aes->aes_set & AES_SET_WCS) != 0) {
  3702. archive_string_empty(&(aes->aes_mbs));
  3703. r = archive_string_append_from_wcs(&(aes->aes_mbs),
  3704. aes->aes_wcs.s, aes->aes_wcs.length);
  3705. if (r == 0)
  3706. aes->aes_set |= AES_SET_MBS;
  3707. else if (errno == ENOMEM)
  3708. return (-1);
  3709. else
  3710. ret = -1;
  3711. }
  3712. /* If we already have an MBS form, use it to be translated to
  3713. * specified character-set. */
  3714. if (aes->aes_set & AES_SET_MBS) {
  3715. if (sc == NULL) {
  3716. /* Conversion is unneeded. */
  3717. *p = aes->aes_mbs.s;
  3718. if (length != NULL)
  3719. *length = aes->aes_mbs.length;
  3720. return (0);
  3721. }
  3722. ret = archive_strncpy_in_locale(&(aes->aes_mbs_in_locale),
  3723. aes->aes_mbs.s, aes->aes_mbs.length, sc);
  3724. *p = aes->aes_mbs_in_locale.s;
  3725. if (length != NULL)
  3726. *length = aes->aes_mbs_in_locale.length;
  3727. } else {
  3728. *p = NULL;
  3729. if (length != NULL)
  3730. *length = 0;
  3731. }
  3732. return (ret);
  3733. }
  3734. int
  3735. archive_mstring_copy_mbs(struct archive_mstring *aes, const char *mbs)
  3736. {
  3737. if (mbs == NULL) {
  3738. aes->aes_set = 0;
  3739. return (0);
  3740. }
  3741. return (archive_mstring_copy_mbs_len(aes, mbs, strlen(mbs)));
  3742. }
  3743. int
  3744. archive_mstring_copy_mbs_len(struct archive_mstring *aes, const char *mbs,
  3745. size_t len)
  3746. {
  3747. if (mbs == NULL) {
  3748. aes->aes_set = 0;
  3749. return (0);
  3750. }
  3751. aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
  3752. archive_strncpy(&(aes->aes_mbs), mbs, len);
  3753. archive_string_empty(&(aes->aes_utf8));
  3754. archive_wstring_empty(&(aes->aes_wcs));
  3755. return (0);
  3756. }
  3757. int
  3758. archive_mstring_copy_wcs(struct archive_mstring *aes, const wchar_t *wcs)
  3759. {
  3760. return archive_mstring_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
  3761. }
  3762. int
  3763. archive_mstring_copy_wcs_len(struct archive_mstring *aes, const wchar_t *wcs,
  3764. size_t len)
  3765. {
  3766. if (wcs == NULL) {
  3767. aes->aes_set = 0;
  3768. }
  3769. aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
  3770. archive_string_empty(&(aes->aes_mbs));
  3771. archive_string_empty(&(aes->aes_utf8));
  3772. archive_wstrncpy(&(aes->aes_wcs), wcs, len);
  3773. return (0);
  3774. }
  3775. int
  3776. archive_mstring_copy_mbs_len_l(struct archive_mstring *aes,
  3777. const char *mbs, size_t len, struct archive_string_conv *sc)
  3778. {
  3779. int r;
  3780. if (mbs == NULL) {
  3781. aes->aes_set = 0;
  3782. return (0);
  3783. }
  3784. archive_string_empty(&(aes->aes_mbs));
  3785. archive_wstring_empty(&(aes->aes_wcs));
  3786. archive_string_empty(&(aes->aes_utf8));
  3787. #if defined(_WIN32) && !defined(__CYGWIN__)
  3788. /*
  3789. * Internationalization programing on Windows must use Wide
  3790. * characters because Windows platform cannot make locale UTF-8.
  3791. */
  3792. if (sc == NULL) {
  3793. if (archive_string_append(&(aes->aes_mbs),
  3794. mbs, mbsnbytes(mbs, len)) == NULL) {
  3795. aes->aes_set = 0;
  3796. r = -1;
  3797. } else {
  3798. aes->aes_set = AES_SET_MBS;
  3799. r = 0;
  3800. }
  3801. #if defined(HAVE_ICONV)
  3802. } else if (sc != NULL && sc->cd_w != (iconv_t)-1) {
  3803. /*
  3804. * This case happens only when MultiByteToWideChar() cannot
  3805. * handle sc->from_cp, and we have to iconv in order to
  3806. * translate character-set to wchar_t,UTF-16.
  3807. */
  3808. iconv_t cd = sc->cd;
  3809. unsigned from_cp;
  3810. int flag;
  3811. /*
  3812. * Translate multi-bytes from some character-set to UTF-8.
  3813. */
  3814. sc->cd = sc->cd_w;
  3815. r = archive_strncpy_in_locale(&(aes->aes_utf8), mbs, len, sc);
  3816. sc->cd = cd;
  3817. if (r != 0) {
  3818. aes->aes_set = 0;
  3819. return (r);
  3820. }
  3821. aes->aes_set = AES_SET_UTF8;
  3822. /*
  3823. * Append the UTF-8 string into wstring.
  3824. */
  3825. flag = sc->flag;
  3826. sc->flag &= ~(SCONV_NORMALIZATION_C
  3827. | SCONV_TO_UTF16| SCONV_FROM_UTF16);
  3828. from_cp = sc->from_cp;
  3829. sc->from_cp = CP_UTF8;
  3830. r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
  3831. aes->aes_utf8.s, aes->aes_utf8.length, sc);
  3832. sc->flag = flag;
  3833. sc->from_cp = from_cp;
  3834. if (r == 0)
  3835. aes->aes_set |= AES_SET_WCS;
  3836. #endif
  3837. } else {
  3838. r = archive_wstring_append_from_mbs_in_codepage(
  3839. &(aes->aes_wcs), mbs, len, sc);
  3840. if (r == 0)
  3841. aes->aes_set = AES_SET_WCS;
  3842. else
  3843. aes->aes_set = 0;
  3844. }
  3845. #else
  3846. r = archive_strncpy_in_locale(&(aes->aes_mbs), mbs, len, sc);
  3847. if (r == 0)
  3848. aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
  3849. else
  3850. aes->aes_set = 0;
  3851. #endif
  3852. return (r);
  3853. }
  3854. /*
  3855. * The 'update' form tries to proactively update all forms of
  3856. * this string (WCS and MBS) and returns an error if any of
  3857. * them fail. This is used by the 'pax' handler, for instance,
  3858. * to detect and report character-conversion failures early while
  3859. * still allowing clients to get potentially useful values from
  3860. * the more tolerant lazy conversions. (get_mbs and get_wcs will
  3861. * strive to give the user something useful, so you can get hopefully
  3862. * usable values even if some of the character conversions are failing.)
  3863. */
  3864. int
  3865. archive_mstring_update_utf8(struct archive *a, struct archive_mstring *aes,
  3866. const char *utf8)
  3867. {
  3868. struct archive_string_conv *sc;
  3869. int r;
  3870. if (utf8 == NULL) {
  3871. aes->aes_set = 0;
  3872. return (0); /* Succeeded in clearing everything. */
  3873. }
  3874. /* Save the UTF8 string. */
  3875. archive_strcpy(&(aes->aes_utf8), utf8);
  3876. /* Empty the mbs and wcs strings. */
  3877. archive_string_empty(&(aes->aes_mbs));
  3878. archive_wstring_empty(&(aes->aes_wcs));
  3879. aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
  3880. /* Try converting UTF-8 to MBS, return false on failure. */
  3881. sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
  3882. if (sc == NULL)
  3883. return (-1);/* Couldn't allocate memory for sc. */
  3884. r = archive_strcpy_in_locale(&(aes->aes_mbs), utf8, sc);
  3885. if (a == NULL)
  3886. free_sconv_object(sc);
  3887. if (r != 0)
  3888. return (-1);
  3889. aes->aes_set = AES_SET_UTF8 | AES_SET_MBS; /* Both UTF8 and MBS set. */
  3890. /* Try converting MBS to WCS, return false on failure. */
  3891. if (archive_wstring_append_from_mbs(&(aes->aes_wcs), aes->aes_mbs.s,
  3892. aes->aes_mbs.length))
  3893. return (-1);
  3894. aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
  3895. /* All conversions succeeded. */
  3896. return (0);
  3897. }