archive_string.c 103 KB

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