archive_string.c 103 KB

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