archive_string.c 104 KB

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