1
0

archive_string.c 103 KB

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