archive_string.c 104 KB

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