archive_string.c 103 KB

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