archive_string.c 107 KB

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