archive_string.c 103 KB

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