archive_string.c 107 KB

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