archive_string.c 107 KB

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