archive_read_support_format_xar.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319
  1. /*-
  2. * Copyright (c) 2009 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #if HAVE_LIBXML_XMLREADER_H
  34. #include <libxml/xmlreader.h>
  35. #elif HAVE_BSDXML_H
  36. #include <bsdxml.h>
  37. #elif HAVE_EXPAT_H
  38. #include <expat.h>
  39. #endif
  40. #ifdef HAVE_BZLIB_H
  41. #include <cm_bzlib.h>
  42. #endif
  43. #if HAVE_LZMA_H
  44. #include <cm_lzma.h>
  45. #endif
  46. #ifdef HAVE_ZLIB_H
  47. #include <cm_zlib.h>
  48. #endif
  49. #include "archive.h"
  50. #include "archive_digest_private.h"
  51. #include "archive_endian.h"
  52. #include "archive_entry.h"
  53. #include "archive_entry_locale.h"
  54. #include "archive_private.h"
  55. #include "archive_read_private.h"
  56. #if (!defined(HAVE_LIBXML_XMLREADER_H) && \
  57. !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H)) ||\
  58. !defined(HAVE_ZLIB_H) || \
  59. !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
  60. /*
  61. * xar needs several external libraries.
  62. * o libxml2 or expat --- XML parser
  63. * o openssl or MD5/SHA1 hash function
  64. * o zlib
  65. * o bzlib2 (option)
  66. * o liblzma (option)
  67. */
  68. int
  69. archive_read_support_format_xar(struct archive *_a)
  70. {
  71. struct archive_read *a = (struct archive_read *)_a;
  72. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  73. ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
  74. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  75. "Xar not supported on this platform");
  76. return (ARCHIVE_WARN);
  77. }
  78. #else /* Support xar format */
  79. /* #define DEBUG 1 */
  80. /* #define DEBUG_PRINT_TOC 1 */
  81. #if DEBUG_PRINT_TOC
  82. #define PRINT_TOC(d, outbytes) do { \
  83. unsigned char *x = (unsigned char *)(uintptr_t)d; \
  84. unsigned char c = x[outbytes-1]; \
  85. x[outbytes - 1] = 0; \
  86. fprintf(stderr, "%s", x); \
  87. fprintf(stderr, "%c", c); \
  88. x[outbytes - 1] = c; \
  89. } while (0)
  90. #else
  91. #define PRINT_TOC(d, outbytes)
  92. #endif
  93. #define HEADER_MAGIC 0x78617221
  94. #define HEADER_SIZE 28
  95. #define HEADER_VERSION 1
  96. #define CKSUM_NONE 0
  97. #define CKSUM_SHA1 1
  98. #define CKSUM_MD5 2
  99. #define MD5_SIZE 16
  100. #define SHA1_SIZE 20
  101. #define MAX_SUM_SIZE 20
  102. enum enctype {
  103. NONE,
  104. GZIP,
  105. BZIP2,
  106. LZMA,
  107. XZ,
  108. };
  109. struct chksumval {
  110. int alg;
  111. size_t len;
  112. unsigned char val[MAX_SUM_SIZE];
  113. };
  114. struct chksumwork {
  115. int alg;
  116. #ifdef ARCHIVE_HAS_MD5
  117. archive_md5_ctx md5ctx;
  118. #endif
  119. #ifdef ARCHIVE_HAS_SHA1
  120. archive_sha1_ctx sha1ctx;
  121. #endif
  122. };
  123. struct xattr {
  124. struct xattr *next;
  125. struct archive_string name;
  126. uint64_t id;
  127. uint64_t length;
  128. uint64_t offset;
  129. uint64_t size;
  130. enum enctype encoding;
  131. struct chksumval a_sum;
  132. struct chksumval e_sum;
  133. struct archive_string fstype;
  134. };
  135. struct xar_file {
  136. struct xar_file *next;
  137. struct xar_file *hdnext;
  138. struct xar_file *parent;
  139. int subdirs;
  140. unsigned int has;
  141. #define HAS_DATA 0x00001
  142. #define HAS_PATHNAME 0x00002
  143. #define HAS_SYMLINK 0x00004
  144. #define HAS_TIME 0x00008
  145. #define HAS_UID 0x00010
  146. #define HAS_GID 0x00020
  147. #define HAS_MODE 0x00040
  148. #define HAS_TYPE 0x00080
  149. #define HAS_DEV 0x00100
  150. #define HAS_DEVMAJOR 0x00200
  151. #define HAS_DEVMINOR 0x00400
  152. #define HAS_INO 0x00800
  153. #define HAS_FFLAGS 0x01000
  154. #define HAS_XATTR 0x02000
  155. #define HAS_ACL 0x04000
  156. #define HAS_CTIME 0x08000
  157. #define HAS_MTIME 0x10000
  158. #define HAS_ATIME 0x20000
  159. uint64_t id;
  160. uint64_t length;
  161. uint64_t offset;
  162. uint64_t size;
  163. enum enctype encoding;
  164. struct chksumval a_sum;
  165. struct chksumval e_sum;
  166. struct archive_string pathname;
  167. struct archive_string symlink;
  168. time_t ctime;
  169. time_t mtime;
  170. time_t atime;
  171. struct archive_string uname;
  172. int64_t uid;
  173. struct archive_string gname;
  174. int64_t gid;
  175. mode_t mode;
  176. dev_t dev;
  177. dev_t devmajor;
  178. dev_t devminor;
  179. int64_t ino64;
  180. struct archive_string fflags_text;
  181. unsigned int link;
  182. unsigned int nlink;
  183. struct archive_string hardlink;
  184. struct xattr *xattr_list;
  185. };
  186. struct hdlink {
  187. struct hdlink *next;
  188. unsigned int id;
  189. int cnt;
  190. struct xar_file *files;
  191. };
  192. struct heap_queue {
  193. struct xar_file **files;
  194. int allocated;
  195. int used;
  196. };
  197. enum xmlstatus {
  198. INIT,
  199. XAR,
  200. TOC,
  201. TOC_CREATION_TIME,
  202. TOC_CHECKSUM,
  203. TOC_CHECKSUM_OFFSET,
  204. TOC_CHECKSUM_SIZE,
  205. TOC_FILE,
  206. FILE_DATA,
  207. FILE_DATA_LENGTH,
  208. FILE_DATA_OFFSET,
  209. FILE_DATA_SIZE,
  210. FILE_DATA_ENCODING,
  211. FILE_DATA_A_CHECKSUM,
  212. FILE_DATA_E_CHECKSUM,
  213. FILE_DATA_CONTENT,
  214. FILE_EA,
  215. FILE_EA_LENGTH,
  216. FILE_EA_OFFSET,
  217. FILE_EA_SIZE,
  218. FILE_EA_ENCODING,
  219. FILE_EA_A_CHECKSUM,
  220. FILE_EA_E_CHECKSUM,
  221. FILE_EA_NAME,
  222. FILE_EA_FSTYPE,
  223. FILE_CTIME,
  224. FILE_MTIME,
  225. FILE_ATIME,
  226. FILE_GROUP,
  227. FILE_GID,
  228. FILE_USER,
  229. FILE_UID,
  230. FILE_MODE,
  231. FILE_DEVICE,
  232. FILE_DEVICE_MAJOR,
  233. FILE_DEVICE_MINOR,
  234. FILE_DEVICENO,
  235. FILE_INODE,
  236. FILE_LINK,
  237. FILE_TYPE,
  238. FILE_NAME,
  239. FILE_ACL,
  240. FILE_ACL_DEFAULT,
  241. FILE_ACL_ACCESS,
  242. FILE_ACL_APPLEEXTENDED,
  243. /* BSD file flags. */
  244. FILE_FLAGS,
  245. FILE_FLAGS_USER_NODUMP,
  246. FILE_FLAGS_USER_IMMUTABLE,
  247. FILE_FLAGS_USER_APPEND,
  248. FILE_FLAGS_USER_OPAQUE,
  249. FILE_FLAGS_USER_NOUNLINK,
  250. FILE_FLAGS_SYS_ARCHIVED,
  251. FILE_FLAGS_SYS_IMMUTABLE,
  252. FILE_FLAGS_SYS_APPEND,
  253. FILE_FLAGS_SYS_NOUNLINK,
  254. FILE_FLAGS_SYS_SNAPSHOT,
  255. /* Linux file flags. */
  256. FILE_EXT2,
  257. FILE_EXT2_SecureDeletion,
  258. FILE_EXT2_Undelete,
  259. FILE_EXT2_Compress,
  260. FILE_EXT2_Synchronous,
  261. FILE_EXT2_Immutable,
  262. FILE_EXT2_AppendOnly,
  263. FILE_EXT2_NoDump,
  264. FILE_EXT2_NoAtime,
  265. FILE_EXT2_CompDirty,
  266. FILE_EXT2_CompBlock,
  267. FILE_EXT2_NoCompBlock,
  268. FILE_EXT2_CompError,
  269. FILE_EXT2_BTree,
  270. FILE_EXT2_HashIndexed,
  271. FILE_EXT2_iMagic,
  272. FILE_EXT2_Journaled,
  273. FILE_EXT2_NoTail,
  274. FILE_EXT2_DirSync,
  275. FILE_EXT2_TopDir,
  276. FILE_EXT2_Reserved,
  277. UNKNOWN,
  278. };
  279. struct unknown_tag {
  280. struct unknown_tag *next;
  281. struct archive_string name;
  282. };
  283. struct xar {
  284. uint64_t offset; /* Current position in the file. */
  285. int64_t total;
  286. uint64_t h_base;
  287. int end_of_file;
  288. #define OUTBUFF_SIZE (1024 * 64)
  289. unsigned char *outbuff;
  290. enum xmlstatus xmlsts;
  291. enum xmlstatus xmlsts_unknown;
  292. struct unknown_tag *unknowntags;
  293. int base64text;
  294. /*
  295. * TOC
  296. */
  297. uint64_t toc_remaining;
  298. uint64_t toc_total;
  299. uint64_t toc_chksum_offset;
  300. uint64_t toc_chksum_size;
  301. /*
  302. * For Decoding data.
  303. */
  304. enum enctype rd_encoding;
  305. z_stream stream;
  306. int stream_valid;
  307. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  308. bz_stream bzstream;
  309. int bzstream_valid;
  310. #endif
  311. #if HAVE_LZMA_H && HAVE_LIBLZMA
  312. lzma_stream lzstream;
  313. int lzstream_valid;
  314. #endif
  315. /*
  316. * For Checksum data.
  317. */
  318. struct chksumwork a_sumwrk;
  319. struct chksumwork e_sumwrk;
  320. struct xar_file *file; /* current reading file. */
  321. struct xattr *xattr; /* current reading extended attribute. */
  322. struct heap_queue file_queue;
  323. struct xar_file *hdlink_orgs;
  324. struct hdlink *hdlink_list;
  325. int entry_init;
  326. uint64_t entry_total;
  327. uint64_t entry_remaining;
  328. size_t entry_unconsumed;
  329. uint64_t entry_size;
  330. enum enctype entry_encoding;
  331. struct chksumval entry_a_sum;
  332. struct chksumval entry_e_sum;
  333. struct archive_string_conv *sconv;
  334. };
  335. struct xmlattr {
  336. struct xmlattr *next;
  337. char *name;
  338. char *value;
  339. };
  340. struct xmlattr_list {
  341. struct xmlattr *first;
  342. struct xmlattr **last;
  343. };
  344. static int xar_bid(struct archive_read *, int);
  345. static int xar_read_header(struct archive_read *,
  346. struct archive_entry *);
  347. static int xar_read_data(struct archive_read *,
  348. const void **, size_t *, int64_t *);
  349. static int xar_read_data_skip(struct archive_read *);
  350. static int xar_cleanup(struct archive_read *);
  351. static int move_reading_point(struct archive_read *, uint64_t);
  352. static int rd_contents_init(struct archive_read *,
  353. enum enctype, int, int);
  354. static int rd_contents(struct archive_read *, const void **,
  355. size_t *, size_t *, uint64_t);
  356. static uint64_t atol10(const char *, size_t);
  357. static int64_t atol8(const char *, size_t);
  358. static size_t atohex(unsigned char *, size_t, const char *, size_t);
  359. static time_t parse_time(const char *p, size_t n);
  360. static int heap_add_entry(struct archive_read *a,
  361. struct heap_queue *, struct xar_file *);
  362. static struct xar_file *heap_get_entry(struct heap_queue *);
  363. static int add_link(struct archive_read *,
  364. struct xar *, struct xar_file *);
  365. static void checksum_init(struct archive_read *, int, int);
  366. static void checksum_update(struct archive_read *, const void *,
  367. size_t, const void *, size_t);
  368. static int checksum_final(struct archive_read *, const void *,
  369. size_t, const void *, size_t);
  370. static void checksum_cleanup(struct archive_read *);
  371. static int decompression_init(struct archive_read *, enum enctype);
  372. static int decompress(struct archive_read *, const void **,
  373. size_t *, const void *, size_t *);
  374. static int decompression_cleanup(struct archive_read *);
  375. static void xmlattr_cleanup(struct xmlattr_list *);
  376. static int file_new(struct archive_read *,
  377. struct xar *, struct xmlattr_list *);
  378. static void file_free(struct xar_file *);
  379. static int xattr_new(struct archive_read *,
  380. struct xar *, struct xmlattr_list *);
  381. static void xattr_free(struct xattr *);
  382. static int getencoding(struct xmlattr_list *);
  383. static int getsumalgorithm(struct xmlattr_list *);
  384. static int unknowntag_start(struct archive_read *,
  385. struct xar *, const char *);
  386. static void unknowntag_end(struct xar *, const char *);
  387. static int xml_start(struct archive_read *,
  388. const char *, struct xmlattr_list *);
  389. static void xml_end(void *, const char *);
  390. static void xml_data(void *, const char *, int);
  391. static int xml_parse_file_flags(struct xar *, const char *);
  392. static int xml_parse_file_ext2(struct xar *, const char *);
  393. #if defined(HAVE_LIBXML_XMLREADER_H)
  394. static int xml2_xmlattr_setup(struct archive_read *,
  395. struct xmlattr_list *, xmlTextReaderPtr);
  396. static int xml2_read_cb(void *, char *, int);
  397. static int xml2_close_cb(void *);
  398. static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
  399. xmlTextReaderLocatorPtr);
  400. static int xml2_read_toc(struct archive_read *);
  401. #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
  402. struct expat_userData {
  403. int state;
  404. struct archive_read *archive;
  405. };
  406. static int expat_xmlattr_setup(struct archive_read *,
  407. struct xmlattr_list *, const XML_Char **);
  408. static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
  409. static void expat_end_cb(void *, const XML_Char *);
  410. static void expat_data_cb(void *, const XML_Char *, int);
  411. static int expat_read_toc(struct archive_read *);
  412. #endif
  413. int
  414. archive_read_support_format_xar(struct archive *_a)
  415. {
  416. struct xar *xar;
  417. struct archive_read *a = (struct archive_read *)_a;
  418. int r;
  419. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  420. ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
  421. xar = (struct xar *)calloc(1, sizeof(*xar));
  422. if (xar == NULL) {
  423. archive_set_error(&a->archive, ENOMEM,
  424. "Can't allocate xar data");
  425. return (ARCHIVE_FATAL);
  426. }
  427. r = __archive_read_register_format(a,
  428. xar,
  429. "xar",
  430. xar_bid,
  431. NULL,
  432. xar_read_header,
  433. xar_read_data,
  434. xar_read_data_skip,
  435. NULL,
  436. xar_cleanup,
  437. NULL,
  438. NULL);
  439. if (r != ARCHIVE_OK)
  440. free(xar);
  441. return (r);
  442. }
  443. static int
  444. xar_bid(struct archive_read *a, int best_bid)
  445. {
  446. const unsigned char *b;
  447. int bid;
  448. (void)best_bid; /* UNUSED */
  449. b = __archive_read_ahead(a, HEADER_SIZE, NULL);
  450. if (b == NULL)
  451. return (-1);
  452. bid = 0;
  453. /*
  454. * Verify magic code
  455. */
  456. if (archive_be32dec(b) != HEADER_MAGIC)
  457. return (0);
  458. bid += 32;
  459. /*
  460. * Verify header size
  461. */
  462. if (archive_be16dec(b+4) != HEADER_SIZE)
  463. return (0);
  464. bid += 16;
  465. /*
  466. * Verify header version
  467. */
  468. if (archive_be16dec(b+6) != HEADER_VERSION)
  469. return (0);
  470. bid += 16;
  471. /*
  472. * Verify type of checksum
  473. */
  474. switch (archive_be32dec(b+24)) {
  475. case CKSUM_NONE:
  476. case CKSUM_SHA1:
  477. case CKSUM_MD5:
  478. bid += 32;
  479. break;
  480. default:
  481. return (0);
  482. }
  483. return (bid);
  484. }
  485. static int
  486. read_toc(struct archive_read *a)
  487. {
  488. struct xar *xar;
  489. struct xar_file *file;
  490. const unsigned char *b;
  491. uint64_t toc_compressed_size;
  492. uint64_t toc_uncompressed_size;
  493. uint32_t toc_chksum_alg;
  494. ssize_t bytes;
  495. int r;
  496. xar = (struct xar *)(a->format->data);
  497. /*
  498. * Read xar header.
  499. */
  500. b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
  501. if (bytes < 0)
  502. return ((int)bytes);
  503. if (bytes < HEADER_SIZE) {
  504. archive_set_error(&a->archive,
  505. ARCHIVE_ERRNO_FILE_FORMAT,
  506. "Truncated archive header");
  507. return (ARCHIVE_FATAL);
  508. }
  509. if (archive_be32dec(b) != HEADER_MAGIC) {
  510. archive_set_error(&a->archive,
  511. ARCHIVE_ERRNO_FILE_FORMAT,
  512. "Invalid header magic");
  513. return (ARCHIVE_FATAL);
  514. }
  515. if (archive_be16dec(b+6) != HEADER_VERSION) {
  516. archive_set_error(&a->archive,
  517. ARCHIVE_ERRNO_FILE_FORMAT,
  518. "Unsupported header version(%d)",
  519. archive_be16dec(b+6));
  520. return (ARCHIVE_FATAL);
  521. }
  522. toc_compressed_size = archive_be64dec(b+8);
  523. xar->toc_remaining = toc_compressed_size;
  524. toc_uncompressed_size = archive_be64dec(b+16);
  525. toc_chksum_alg = archive_be32dec(b+24);
  526. __archive_read_consume(a, HEADER_SIZE);
  527. xar->offset += HEADER_SIZE;
  528. xar->toc_total = 0;
  529. /*
  530. * Read TOC(Table of Contents).
  531. */
  532. /* Initialize reading contents. */
  533. r = move_reading_point(a, HEADER_SIZE);
  534. if (r != ARCHIVE_OK)
  535. return (r);
  536. r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
  537. if (r != ARCHIVE_OK)
  538. return (r);
  539. #ifdef HAVE_LIBXML_XMLREADER_H
  540. r = xml2_read_toc(a);
  541. #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
  542. r = expat_read_toc(a);
  543. #endif
  544. if (r != ARCHIVE_OK)
  545. return (r);
  546. /* Set 'The HEAP' base. */
  547. xar->h_base = xar->offset;
  548. if (xar->toc_total != toc_uncompressed_size) {
  549. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  550. "TOC uncompressed size error");
  551. return (ARCHIVE_FATAL);
  552. }
  553. /*
  554. * Checksum TOC
  555. */
  556. if (toc_chksum_alg != CKSUM_NONE) {
  557. r = move_reading_point(a, xar->toc_chksum_offset);
  558. if (r != ARCHIVE_OK)
  559. return (r);
  560. b = __archive_read_ahead(a,
  561. (size_t)xar->toc_chksum_size, &bytes);
  562. if (bytes < 0)
  563. return ((int)bytes);
  564. if ((uint64_t)bytes < xar->toc_chksum_size) {
  565. archive_set_error(&a->archive,
  566. ARCHIVE_ERRNO_FILE_FORMAT,
  567. "Truncated archive file");
  568. return (ARCHIVE_FATAL);
  569. }
  570. r = checksum_final(a, b,
  571. (size_t)xar->toc_chksum_size, NULL, 0);
  572. __archive_read_consume(a, xar->toc_chksum_size);
  573. xar->offset += xar->toc_chksum_size;
  574. if (r != ARCHIVE_OK)
  575. return (ARCHIVE_FATAL);
  576. }
  577. /*
  578. * Connect hardlinked files.
  579. */
  580. for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
  581. struct hdlink **hdlink;
  582. for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
  583. hdlink = &((*hdlink)->next)) {
  584. if ((*hdlink)->id == file->id) {
  585. struct hdlink *hltmp;
  586. struct xar_file *f2;
  587. int nlink = (*hdlink)->cnt + 1;
  588. file->nlink = nlink;
  589. for (f2 = (*hdlink)->files; f2 != NULL;
  590. f2 = f2->hdnext) {
  591. f2->nlink = nlink;
  592. archive_string_copy(
  593. &(f2->hardlink), &(file->pathname));
  594. }
  595. /* Remove resolved files from hdlist_list. */
  596. hltmp = *hdlink;
  597. *hdlink = hltmp->next;
  598. free(hltmp);
  599. break;
  600. }
  601. }
  602. }
  603. a->archive.archive_format = ARCHIVE_FORMAT_XAR;
  604. a->archive.archive_format_name = "xar";
  605. return (ARCHIVE_OK);
  606. }
  607. static int
  608. xar_read_header(struct archive_read *a, struct archive_entry *entry)
  609. {
  610. struct xar *xar;
  611. struct xar_file *file;
  612. struct xattr *xattr;
  613. int r;
  614. xar = (struct xar *)(a->format->data);
  615. r = ARCHIVE_OK;
  616. if (xar->offset == 0) {
  617. /* Create a character conversion object. */
  618. if (xar->sconv == NULL) {
  619. xar->sconv = archive_string_conversion_from_charset(
  620. &(a->archive), "UTF-8", 1);
  621. if (xar->sconv == NULL)
  622. return (ARCHIVE_FATAL);
  623. }
  624. /* Read TOC. */
  625. r = read_toc(a);
  626. if (r != ARCHIVE_OK)
  627. return (r);
  628. }
  629. for (;;) {
  630. file = xar->file = heap_get_entry(&(xar->file_queue));
  631. if (file == NULL) {
  632. xar->end_of_file = 1;
  633. return (ARCHIVE_EOF);
  634. }
  635. if ((file->mode & AE_IFMT) != AE_IFDIR)
  636. break;
  637. if (file->has != (HAS_PATHNAME | HAS_TYPE))
  638. break;
  639. /*
  640. * If a file type is a directory and it does not have
  641. * any metadata, do not export.
  642. */
  643. file_free(file);
  644. }
  645. if (file->has & HAS_ATIME) {
  646. archive_entry_set_atime(entry, file->atime, 0);
  647. }
  648. if (file->has & HAS_CTIME) {
  649. archive_entry_set_ctime(entry, file->ctime, 0);
  650. }
  651. if (file->has & HAS_MTIME) {
  652. archive_entry_set_mtime(entry, file->mtime, 0);
  653. }
  654. archive_entry_set_gid(entry, file->gid);
  655. if (file->gname.length > 0 &&
  656. archive_entry_copy_gname_l(entry, file->gname.s,
  657. archive_strlen(&(file->gname)), xar->sconv) != 0) {
  658. if (errno == ENOMEM) {
  659. archive_set_error(&a->archive, ENOMEM,
  660. "Can't allocate memory for Gname");
  661. return (ARCHIVE_FATAL);
  662. }
  663. archive_set_error(&a->archive,
  664. ARCHIVE_ERRNO_FILE_FORMAT,
  665. "Gname cannot be converted from %s to current locale.",
  666. archive_string_conversion_charset_name(xar->sconv));
  667. r = ARCHIVE_WARN;
  668. }
  669. archive_entry_set_uid(entry, file->uid);
  670. if (file->uname.length > 0 &&
  671. archive_entry_copy_uname_l(entry, file->uname.s,
  672. archive_strlen(&(file->uname)), xar->sconv) != 0) {
  673. if (errno == ENOMEM) {
  674. archive_set_error(&a->archive, ENOMEM,
  675. "Can't allocate memory for Uname");
  676. return (ARCHIVE_FATAL);
  677. }
  678. archive_set_error(&a->archive,
  679. ARCHIVE_ERRNO_FILE_FORMAT,
  680. "Uname cannot be converted from %s to current locale.",
  681. archive_string_conversion_charset_name(xar->sconv));
  682. r = ARCHIVE_WARN;
  683. }
  684. archive_entry_set_mode(entry, file->mode);
  685. if (archive_entry_copy_pathname_l(entry, file->pathname.s,
  686. archive_strlen(&(file->pathname)), xar->sconv) != 0) {
  687. if (errno == ENOMEM) {
  688. archive_set_error(&a->archive, ENOMEM,
  689. "Can't allocate memory for Pathname");
  690. return (ARCHIVE_FATAL);
  691. }
  692. archive_set_error(&a->archive,
  693. ARCHIVE_ERRNO_FILE_FORMAT,
  694. "Pathname cannot be converted from %s to current locale.",
  695. archive_string_conversion_charset_name(xar->sconv));
  696. r = ARCHIVE_WARN;
  697. }
  698. if (file->symlink.length > 0 &&
  699. archive_entry_copy_symlink_l(entry, file->symlink.s,
  700. archive_strlen(&(file->symlink)), xar->sconv) != 0) {
  701. if (errno == ENOMEM) {
  702. archive_set_error(&a->archive, ENOMEM,
  703. "Can't allocate memory for Linkname");
  704. return (ARCHIVE_FATAL);
  705. }
  706. archive_set_error(&a->archive,
  707. ARCHIVE_ERRNO_FILE_FORMAT,
  708. "Linkname cannot be converted from %s to current locale.",
  709. archive_string_conversion_charset_name(xar->sconv));
  710. r = ARCHIVE_WARN;
  711. }
  712. /* Set proper nlink. */
  713. if ((file->mode & AE_IFMT) == AE_IFDIR)
  714. archive_entry_set_nlink(entry, file->subdirs + 2);
  715. else
  716. archive_entry_set_nlink(entry, file->nlink);
  717. archive_entry_set_size(entry, file->size);
  718. if (archive_strlen(&(file->hardlink)) > 0)
  719. archive_entry_set_hardlink(entry, file->hardlink.s);
  720. archive_entry_set_ino64(entry, file->ino64);
  721. if (file->has & HAS_DEV)
  722. archive_entry_set_dev(entry, file->dev);
  723. if (file->has & HAS_DEVMAJOR)
  724. archive_entry_set_devmajor(entry, file->devmajor);
  725. if (file->has & HAS_DEVMINOR)
  726. archive_entry_set_devminor(entry, file->devminor);
  727. if (archive_strlen(&(file->fflags_text)) > 0)
  728. archive_entry_copy_fflags_text(entry, file->fflags_text.s);
  729. xar->entry_init = 1;
  730. xar->entry_total = 0;
  731. xar->entry_remaining = file->length;
  732. xar->entry_size = file->size;
  733. xar->entry_encoding = file->encoding;
  734. xar->entry_a_sum = file->a_sum;
  735. xar->entry_e_sum = file->e_sum;
  736. /*
  737. * Read extended attributes.
  738. */
  739. xattr = file->xattr_list;
  740. while (xattr != NULL) {
  741. const void *d;
  742. size_t outbytes = 0;
  743. size_t used = 0;
  744. r = move_reading_point(a, xattr->offset);
  745. if (r != ARCHIVE_OK)
  746. break;
  747. r = rd_contents_init(a, xattr->encoding,
  748. xattr->a_sum.alg, xattr->e_sum.alg);
  749. if (r != ARCHIVE_OK)
  750. break;
  751. d = NULL;
  752. r = rd_contents(a, &d, &outbytes, &used, xattr->length);
  753. if (r != ARCHIVE_OK)
  754. break;
  755. if (outbytes != xattr->size) {
  756. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  757. "Decompressed size error");
  758. r = ARCHIVE_FATAL;
  759. break;
  760. }
  761. r = checksum_final(a,
  762. xattr->a_sum.val, xattr->a_sum.len,
  763. xattr->e_sum.val, xattr->e_sum.len);
  764. if (r != ARCHIVE_OK) {
  765. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  766. "Xattr checksum error");
  767. r = ARCHIVE_WARN;
  768. break;
  769. }
  770. if (xattr->name.s == NULL) {
  771. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  772. "Xattr name error");
  773. r = ARCHIVE_WARN;
  774. break;
  775. }
  776. archive_entry_xattr_add_entry(entry,
  777. xattr->name.s, d, outbytes);
  778. xattr = xattr->next;
  779. }
  780. if (r != ARCHIVE_OK) {
  781. file_free(file);
  782. return (r);
  783. }
  784. if (xar->entry_remaining > 0)
  785. /* Move reading point to the beginning of current
  786. * file contents. */
  787. r = move_reading_point(a, file->offset);
  788. else
  789. r = ARCHIVE_OK;
  790. file_free(file);
  791. return (r);
  792. }
  793. static int
  794. xar_read_data(struct archive_read *a,
  795. const void **buff, size_t *size, int64_t *offset)
  796. {
  797. struct xar *xar;
  798. size_t used = 0;
  799. int r;
  800. xar = (struct xar *)(a->format->data);
  801. if (xar->entry_unconsumed) {
  802. __archive_read_consume(a, xar->entry_unconsumed);
  803. xar->entry_unconsumed = 0;
  804. }
  805. if (xar->end_of_file || xar->entry_remaining <= 0) {
  806. r = ARCHIVE_EOF;
  807. goto abort_read_data;
  808. }
  809. if (xar->entry_init) {
  810. r = rd_contents_init(a, xar->entry_encoding,
  811. xar->entry_a_sum.alg, xar->entry_e_sum.alg);
  812. if (r != ARCHIVE_OK) {
  813. xar->entry_remaining = 0;
  814. return (r);
  815. }
  816. xar->entry_init = 0;
  817. }
  818. *buff = NULL;
  819. r = rd_contents(a, buff, size, &used, xar->entry_remaining);
  820. if (r != ARCHIVE_OK)
  821. goto abort_read_data;
  822. *offset = xar->entry_total;
  823. xar->entry_total += *size;
  824. xar->total += *size;
  825. xar->offset += used;
  826. xar->entry_remaining -= used;
  827. xar->entry_unconsumed = used;
  828. if (xar->entry_remaining == 0) {
  829. if (xar->entry_total != xar->entry_size) {
  830. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  831. "Decompressed size error");
  832. r = ARCHIVE_FATAL;
  833. goto abort_read_data;
  834. }
  835. r = checksum_final(a,
  836. xar->entry_a_sum.val, xar->entry_a_sum.len,
  837. xar->entry_e_sum.val, xar->entry_e_sum.len);
  838. if (r != ARCHIVE_OK)
  839. goto abort_read_data;
  840. }
  841. return (ARCHIVE_OK);
  842. abort_read_data:
  843. *buff = NULL;
  844. *size = 0;
  845. *offset = xar->total;
  846. return (r);
  847. }
  848. static int
  849. xar_read_data_skip(struct archive_read *a)
  850. {
  851. struct xar *xar;
  852. int64_t bytes_skipped;
  853. xar = (struct xar *)(a->format->data);
  854. if (xar->end_of_file)
  855. return (ARCHIVE_EOF);
  856. bytes_skipped = __archive_read_consume(a, xar->entry_remaining +
  857. xar->entry_unconsumed);
  858. if (bytes_skipped < 0)
  859. return (ARCHIVE_FATAL);
  860. xar->offset += bytes_skipped;
  861. xar->entry_unconsumed = 0;
  862. return (ARCHIVE_OK);
  863. }
  864. static int
  865. xar_cleanup(struct archive_read *a)
  866. {
  867. struct xar *xar;
  868. struct hdlink *hdlink;
  869. int i;
  870. int r;
  871. xar = (struct xar *)(a->format->data);
  872. checksum_cleanup(a);
  873. r = decompression_cleanup(a);
  874. hdlink = xar->hdlink_list;
  875. while (hdlink != NULL) {
  876. struct hdlink *next = hdlink->next;
  877. free(hdlink);
  878. hdlink = next;
  879. }
  880. for (i = 0; i < xar->file_queue.used; i++)
  881. file_free(xar->file_queue.files[i]);
  882. free(xar->file_queue.files);
  883. while (xar->unknowntags != NULL) {
  884. struct unknown_tag *tag;
  885. tag = xar->unknowntags;
  886. xar->unknowntags = tag->next;
  887. archive_string_free(&(tag->name));
  888. free(tag);
  889. }
  890. free(xar->outbuff);
  891. free(xar);
  892. a->format->data = NULL;
  893. return (r);
  894. }
  895. static int
  896. move_reading_point(struct archive_read *a, uint64_t offset)
  897. {
  898. struct xar *xar;
  899. xar = (struct xar *)(a->format->data);
  900. if (xar->offset - xar->h_base != offset) {
  901. /* Seek forward to the start of file contents. */
  902. int64_t step;
  903. step = offset - (xar->offset - xar->h_base);
  904. if (step > 0) {
  905. step = __archive_read_consume(a, step);
  906. if (step < 0)
  907. return ((int)step);
  908. xar->offset += step;
  909. } else {
  910. int64_t pos = __archive_read_seek(a, xar->h_base + offset, SEEK_SET);
  911. if (pos == ARCHIVE_FAILED) {
  912. archive_set_error(&(a->archive),
  913. ARCHIVE_ERRNO_MISC,
  914. "Cannot seek.");
  915. return (ARCHIVE_FAILED);
  916. }
  917. xar->offset = pos;
  918. }
  919. }
  920. return (ARCHIVE_OK);
  921. }
  922. static int
  923. rd_contents_init(struct archive_read *a, enum enctype encoding,
  924. int a_sum_alg, int e_sum_alg)
  925. {
  926. int r;
  927. /* Init decompress library. */
  928. if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
  929. return (r);
  930. /* Init checksum library. */
  931. checksum_init(a, a_sum_alg, e_sum_alg);
  932. return (ARCHIVE_OK);
  933. }
  934. static int
  935. rd_contents(struct archive_read *a, const void **buff, size_t *size,
  936. size_t *used, uint64_t remaining)
  937. {
  938. const unsigned char *b;
  939. ssize_t bytes;
  940. /* Get whatever bytes are immediately available. */
  941. b = __archive_read_ahead(a, 1, &bytes);
  942. if (bytes < 0)
  943. return ((int)bytes);
  944. if (bytes == 0) {
  945. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  946. "Truncated archive file");
  947. return (ARCHIVE_FATAL);
  948. }
  949. if ((uint64_t)bytes > remaining)
  950. bytes = (ssize_t)remaining;
  951. /*
  952. * Decompress contents of file.
  953. */
  954. *used = bytes;
  955. if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
  956. return (ARCHIVE_FATAL);
  957. /*
  958. * Update checksum of a compressed data and a extracted data.
  959. */
  960. checksum_update(a, b, *used, *buff, *size);
  961. return (ARCHIVE_OK);
  962. }
  963. /*
  964. * Note that this implementation does not (and should not!) obey
  965. * locale settings; you cannot simply substitute strtol here, since
  966. * it does obey locale.
  967. */
  968. static uint64_t
  969. atol10(const char *p, size_t char_cnt)
  970. {
  971. uint64_t l;
  972. int digit;
  973. if (char_cnt == 0)
  974. return (0);
  975. l = 0;
  976. digit = *p - '0';
  977. while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
  978. l = (l * 10) + digit;
  979. digit = *++p - '0';
  980. }
  981. return (l);
  982. }
  983. static int64_t
  984. atol8(const char *p, size_t char_cnt)
  985. {
  986. int64_t l;
  987. int digit;
  988. if (char_cnt == 0)
  989. return (0);
  990. l = 0;
  991. while (char_cnt-- > 0) {
  992. if (*p >= '0' && *p <= '7')
  993. digit = *p - '0';
  994. else
  995. break;
  996. p++;
  997. l <<= 3;
  998. l |= digit;
  999. }
  1000. return (l);
  1001. }
  1002. static size_t
  1003. atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
  1004. {
  1005. size_t fbsize = bsize;
  1006. while (bsize && psize > 1) {
  1007. unsigned char x;
  1008. if (p[0] >= 'a' && p[0] <= 'z')
  1009. x = (p[0] - 'a' + 0x0a) << 4;
  1010. else if (p[0] >= 'A' && p[0] <= 'Z')
  1011. x = (p[0] - 'A' + 0x0a) << 4;
  1012. else if (p[0] >= '0' && p[0] <= '9')
  1013. x = (p[0] - '0') << 4;
  1014. else
  1015. return (-1);
  1016. if (p[1] >= 'a' && p[1] <= 'z')
  1017. x |= p[1] - 'a' + 0x0a;
  1018. else if (p[1] >= 'A' && p[1] <= 'Z')
  1019. x |= p[1] - 'A' + 0x0a;
  1020. else if (p[1] >= '0' && p[1] <= '9')
  1021. x |= p[1] - '0';
  1022. else
  1023. return (-1);
  1024. *b++ = x;
  1025. bsize--;
  1026. p += 2;
  1027. psize -= 2;
  1028. }
  1029. return (fbsize - bsize);
  1030. }
  1031. static time_t
  1032. time_from_tm(struct tm *t)
  1033. {
  1034. #if HAVE_TIMEGM
  1035. /* Use platform timegm() if available. */
  1036. return (timegm(t));
  1037. #elif HAVE__MKGMTIME64
  1038. return (_mkgmtime64(t));
  1039. #else
  1040. /* Else use direct calculation using POSIX assumptions. */
  1041. /* First, fix up tm_yday based on the year/month/day. */
  1042. mktime(t);
  1043. /* Then we can compute timegm() from first principles. */
  1044. return (t->tm_sec
  1045. + t->tm_min * 60
  1046. + t->tm_hour * 3600
  1047. + t->tm_yday * 86400
  1048. + (t->tm_year - 70) * 31536000
  1049. + ((t->tm_year - 69) / 4) * 86400
  1050. - ((t->tm_year - 1) / 100) * 86400
  1051. + ((t->tm_year + 299) / 400) * 86400);
  1052. #endif
  1053. }
  1054. static time_t
  1055. parse_time(const char *p, size_t n)
  1056. {
  1057. struct tm tm;
  1058. time_t t = 0;
  1059. int64_t data;
  1060. memset(&tm, 0, sizeof(tm));
  1061. if (n != 20)
  1062. return (t);
  1063. data = atol10(p, 4);
  1064. if (data < 1900)
  1065. return (t);
  1066. tm.tm_year = (int)data - 1900;
  1067. p += 4;
  1068. if (*p++ != '-')
  1069. return (t);
  1070. data = atol10(p, 2);
  1071. if (data < 1 || data > 12)
  1072. return (t);
  1073. tm.tm_mon = (int)data -1;
  1074. p += 2;
  1075. if (*p++ != '-')
  1076. return (t);
  1077. data = atol10(p, 2);
  1078. if (data < 1 || data > 31)
  1079. return (t);
  1080. tm.tm_mday = (int)data;
  1081. p += 2;
  1082. if (*p++ != 'T')
  1083. return (t);
  1084. data = atol10(p, 2);
  1085. if (data < 0 || data > 23)
  1086. return (t);
  1087. tm.tm_hour = (int)data;
  1088. p += 2;
  1089. if (*p++ != ':')
  1090. return (t);
  1091. data = atol10(p, 2);
  1092. if (data < 0 || data > 59)
  1093. return (t);
  1094. tm.tm_min = (int)data;
  1095. p += 2;
  1096. if (*p++ != ':')
  1097. return (t);
  1098. data = atol10(p, 2);
  1099. if (data < 0 || data > 60)
  1100. return (t);
  1101. tm.tm_sec = (int)data;
  1102. #if 0
  1103. p += 2;
  1104. if (*p != 'Z')
  1105. return (t);
  1106. #endif
  1107. t = time_from_tm(&tm);
  1108. return (t);
  1109. }
  1110. static int
  1111. heap_add_entry(struct archive_read *a,
  1112. struct heap_queue *heap, struct xar_file *file)
  1113. {
  1114. uint64_t file_id, parent_id;
  1115. int hole, parent;
  1116. /* Expand our pending files list as necessary. */
  1117. if (heap->used >= heap->allocated) {
  1118. struct xar_file **new_pending_files;
  1119. int new_size = heap->allocated * 2;
  1120. if (heap->allocated < 1024)
  1121. new_size = 1024;
  1122. /* Overflow might keep us from growing the list. */
  1123. if (new_size <= heap->allocated) {
  1124. archive_set_error(&a->archive,
  1125. ENOMEM, "Out of memory");
  1126. return (ARCHIVE_FATAL);
  1127. }
  1128. new_pending_files = (struct xar_file **)
  1129. malloc(new_size * sizeof(new_pending_files[0]));
  1130. if (new_pending_files == NULL) {
  1131. archive_set_error(&a->archive,
  1132. ENOMEM, "Out of memory");
  1133. return (ARCHIVE_FATAL);
  1134. }
  1135. memcpy(new_pending_files, heap->files,
  1136. heap->allocated * sizeof(new_pending_files[0]));
  1137. free(heap->files);
  1138. heap->files = new_pending_files;
  1139. heap->allocated = new_size;
  1140. }
  1141. file_id = file->id;
  1142. /*
  1143. * Start with hole at end, walk it up tree to find insertion point.
  1144. */
  1145. hole = heap->used++;
  1146. while (hole > 0) {
  1147. parent = (hole - 1)/2;
  1148. parent_id = heap->files[parent]->id;
  1149. if (file_id >= parent_id) {
  1150. heap->files[hole] = file;
  1151. return (ARCHIVE_OK);
  1152. }
  1153. /* Move parent into hole <==> move hole up tree. */
  1154. heap->files[hole] = heap->files[parent];
  1155. hole = parent;
  1156. }
  1157. heap->files[0] = file;
  1158. return (ARCHIVE_OK);
  1159. }
  1160. static struct xar_file *
  1161. heap_get_entry(struct heap_queue *heap)
  1162. {
  1163. uint64_t a_id, b_id, c_id;
  1164. int a, b, c;
  1165. struct xar_file *r, *tmp;
  1166. if (heap->used < 1)
  1167. return (NULL);
  1168. /*
  1169. * The first file in the list is the earliest; we'll return this.
  1170. */
  1171. r = heap->files[0];
  1172. /*
  1173. * Move the last item in the heap to the root of the tree
  1174. */
  1175. heap->files[0] = heap->files[--(heap->used)];
  1176. /*
  1177. * Rebalance the heap.
  1178. */
  1179. a = 0; /* Starting element and its heap key */
  1180. a_id = heap->files[a]->id;
  1181. for (;;) {
  1182. b = a + a + 1; /* First child */
  1183. if (b >= heap->used)
  1184. return (r);
  1185. b_id = heap->files[b]->id;
  1186. c = b + 1; /* Use second child if it is smaller. */
  1187. if (c < heap->used) {
  1188. c_id = heap->files[c]->id;
  1189. if (c_id < b_id) {
  1190. b = c;
  1191. b_id = c_id;
  1192. }
  1193. }
  1194. if (a_id <= b_id)
  1195. return (r);
  1196. tmp = heap->files[a];
  1197. heap->files[a] = heap->files[b];
  1198. heap->files[b] = tmp;
  1199. a = b;
  1200. }
  1201. }
  1202. static int
  1203. add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
  1204. {
  1205. struct hdlink *hdlink;
  1206. for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
  1207. if (hdlink->id == file->link) {
  1208. file->hdnext = hdlink->files;
  1209. hdlink->cnt++;
  1210. hdlink->files = file;
  1211. return (ARCHIVE_OK);
  1212. }
  1213. }
  1214. hdlink = malloc(sizeof(*hdlink));
  1215. if (hdlink == NULL) {
  1216. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1217. return (ARCHIVE_FATAL);
  1218. }
  1219. file->hdnext = NULL;
  1220. hdlink->id = file->link;
  1221. hdlink->cnt = 1;
  1222. hdlink->files = file;
  1223. hdlink->next = xar->hdlink_list;
  1224. xar->hdlink_list = hdlink;
  1225. return (ARCHIVE_OK);
  1226. }
  1227. static void
  1228. _checksum_init(struct chksumwork *sumwrk, int sum_alg)
  1229. {
  1230. sumwrk->alg = sum_alg;
  1231. switch (sum_alg) {
  1232. case CKSUM_NONE:
  1233. break;
  1234. case CKSUM_SHA1:
  1235. archive_sha1_init(&(sumwrk->sha1ctx));
  1236. break;
  1237. case CKSUM_MD5:
  1238. archive_md5_init(&(sumwrk->md5ctx));
  1239. break;
  1240. }
  1241. }
  1242. static void
  1243. _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
  1244. {
  1245. switch (sumwrk->alg) {
  1246. case CKSUM_NONE:
  1247. break;
  1248. case CKSUM_SHA1:
  1249. archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
  1250. break;
  1251. case CKSUM_MD5:
  1252. archive_md5_update(&(sumwrk->md5ctx), buff, size);
  1253. break;
  1254. }
  1255. }
  1256. static int
  1257. _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
  1258. {
  1259. unsigned char sum[MAX_SUM_SIZE];
  1260. int r = ARCHIVE_OK;
  1261. switch (sumwrk->alg) {
  1262. case CKSUM_NONE:
  1263. break;
  1264. case CKSUM_SHA1:
  1265. archive_sha1_final(&(sumwrk->sha1ctx), sum);
  1266. if (len != SHA1_SIZE ||
  1267. memcmp(val, sum, SHA1_SIZE) != 0)
  1268. r = ARCHIVE_FAILED;
  1269. break;
  1270. case CKSUM_MD5:
  1271. archive_md5_final(&(sumwrk->md5ctx), sum);
  1272. if (len != MD5_SIZE ||
  1273. memcmp(val, sum, MD5_SIZE) != 0)
  1274. r = ARCHIVE_FAILED;
  1275. break;
  1276. }
  1277. return (r);
  1278. }
  1279. static void
  1280. checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
  1281. {
  1282. struct xar *xar;
  1283. xar = (struct xar *)(a->format->data);
  1284. _checksum_init(&(xar->a_sumwrk), a_sum_alg);
  1285. _checksum_init(&(xar->e_sumwrk), e_sum_alg);
  1286. }
  1287. static void
  1288. checksum_update(struct archive_read *a, const void *abuff, size_t asize,
  1289. const void *ebuff, size_t esize)
  1290. {
  1291. struct xar *xar;
  1292. xar = (struct xar *)(a->format->data);
  1293. _checksum_update(&(xar->a_sumwrk), abuff, asize);
  1294. _checksum_update(&(xar->e_sumwrk), ebuff, esize);
  1295. }
  1296. static int
  1297. checksum_final(struct archive_read *a, const void *a_sum_val,
  1298. size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
  1299. {
  1300. struct xar *xar;
  1301. int r;
  1302. xar = (struct xar *)(a->format->data);
  1303. r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
  1304. if (r == ARCHIVE_OK)
  1305. r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
  1306. if (r != ARCHIVE_OK)
  1307. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1308. "Sumcheck error");
  1309. return (r);
  1310. }
  1311. static int
  1312. decompression_init(struct archive_read *a, enum enctype encoding)
  1313. {
  1314. struct xar *xar;
  1315. const char *detail;
  1316. int r;
  1317. xar = (struct xar *)(a->format->data);
  1318. xar->rd_encoding = encoding;
  1319. switch (encoding) {
  1320. case NONE:
  1321. break;
  1322. case GZIP:
  1323. if (xar->stream_valid)
  1324. r = inflateReset(&(xar->stream));
  1325. else
  1326. r = inflateInit(&(xar->stream));
  1327. if (r != Z_OK) {
  1328. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1329. "Couldn't initialize zlib stream.");
  1330. return (ARCHIVE_FATAL);
  1331. }
  1332. xar->stream_valid = 1;
  1333. xar->stream.total_in = 0;
  1334. xar->stream.total_out = 0;
  1335. break;
  1336. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1337. case BZIP2:
  1338. if (xar->bzstream_valid) {
  1339. BZ2_bzDecompressEnd(&(xar->bzstream));
  1340. xar->bzstream_valid = 0;
  1341. }
  1342. r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
  1343. if (r == BZ_MEM_ERROR)
  1344. r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
  1345. if (r != BZ_OK) {
  1346. int err = ARCHIVE_ERRNO_MISC;
  1347. detail = NULL;
  1348. switch (r) {
  1349. case BZ_PARAM_ERROR:
  1350. detail = "invalid setup parameter";
  1351. break;
  1352. case BZ_MEM_ERROR:
  1353. err = ENOMEM;
  1354. detail = "out of memory";
  1355. break;
  1356. case BZ_CONFIG_ERROR:
  1357. detail = "mis-compiled library";
  1358. break;
  1359. }
  1360. archive_set_error(&a->archive, err,
  1361. "Internal error initializing decompressor: %s",
  1362. detail == NULL ? "??" : detail);
  1363. xar->bzstream_valid = 0;
  1364. return (ARCHIVE_FATAL);
  1365. }
  1366. xar->bzstream_valid = 1;
  1367. xar->bzstream.total_in_lo32 = 0;
  1368. xar->bzstream.total_in_hi32 = 0;
  1369. xar->bzstream.total_out_lo32 = 0;
  1370. xar->bzstream.total_out_hi32 = 0;
  1371. break;
  1372. #endif
  1373. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1374. #if LZMA_VERSION_MAJOR >= 5
  1375. /* Effectively disable the limiter. */
  1376. #define LZMA_MEMLIMIT UINT64_MAX
  1377. #else
  1378. /* NOTE: This needs to check memory size which running system has. */
  1379. #define LZMA_MEMLIMIT (1U << 30)
  1380. #endif
  1381. case XZ:
  1382. case LZMA:
  1383. if (xar->lzstream_valid) {
  1384. lzma_end(&(xar->lzstream));
  1385. xar->lzstream_valid = 0;
  1386. }
  1387. if (xar->entry_encoding == XZ)
  1388. r = lzma_stream_decoder(&(xar->lzstream),
  1389. LZMA_MEMLIMIT,/* memlimit */
  1390. LZMA_CONCATENATED);
  1391. else
  1392. r = lzma_alone_decoder(&(xar->lzstream),
  1393. LZMA_MEMLIMIT);/* memlimit */
  1394. if (r != LZMA_OK) {
  1395. switch (r) {
  1396. case LZMA_MEM_ERROR:
  1397. archive_set_error(&a->archive,
  1398. ENOMEM,
  1399. "Internal error initializing "
  1400. "compression library: "
  1401. "Cannot allocate memory");
  1402. break;
  1403. case LZMA_OPTIONS_ERROR:
  1404. archive_set_error(&a->archive,
  1405. ARCHIVE_ERRNO_MISC,
  1406. "Internal error initializing "
  1407. "compression library: "
  1408. "Invalid or unsupported options");
  1409. break;
  1410. default:
  1411. archive_set_error(&a->archive,
  1412. ARCHIVE_ERRNO_MISC,
  1413. "Internal error initializing "
  1414. "lzma library");
  1415. break;
  1416. }
  1417. return (ARCHIVE_FATAL);
  1418. }
  1419. xar->lzstream_valid = 1;
  1420. xar->lzstream.total_in = 0;
  1421. xar->lzstream.total_out = 0;
  1422. break;
  1423. #endif
  1424. /*
  1425. * Unsupported compression.
  1426. */
  1427. default:
  1428. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
  1429. case BZIP2:
  1430. #endif
  1431. #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
  1432. case LZMA:
  1433. case XZ:
  1434. #endif
  1435. switch (xar->entry_encoding) {
  1436. case BZIP2: detail = "bzip2"; break;
  1437. case LZMA: detail = "lzma"; break;
  1438. case XZ: detail = "xz"; break;
  1439. default: detail = "??"; break;
  1440. }
  1441. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1442. "%s compression not supported on this platform",
  1443. detail);
  1444. return (ARCHIVE_FAILED);
  1445. }
  1446. return (ARCHIVE_OK);
  1447. }
  1448. static int
  1449. decompress(struct archive_read *a, const void **buff, size_t *outbytes,
  1450. const void *b, size_t *used)
  1451. {
  1452. struct xar *xar;
  1453. void *outbuff;
  1454. size_t avail_in, avail_out;
  1455. int r;
  1456. xar = (struct xar *)(a->format->data);
  1457. avail_in = *used;
  1458. outbuff = (void *)(uintptr_t)*buff;
  1459. if (outbuff == NULL) {
  1460. if (xar->outbuff == NULL) {
  1461. xar->outbuff = malloc(OUTBUFF_SIZE);
  1462. if (xar->outbuff == NULL) {
  1463. archive_set_error(&a->archive, ENOMEM,
  1464. "Couldn't allocate memory for out buffer");
  1465. return (ARCHIVE_FATAL);
  1466. }
  1467. }
  1468. outbuff = xar->outbuff;
  1469. *buff = outbuff;
  1470. avail_out = OUTBUFF_SIZE;
  1471. } else
  1472. avail_out = *outbytes;
  1473. switch (xar->rd_encoding) {
  1474. case GZIP:
  1475. xar->stream.next_in = (Bytef *)(uintptr_t)b;
  1476. xar->stream.avail_in = avail_in;
  1477. xar->stream.next_out = (unsigned char *)outbuff;
  1478. xar->stream.avail_out = avail_out;
  1479. r = inflate(&(xar->stream), 0);
  1480. switch (r) {
  1481. case Z_OK: /* Decompressor made some progress.*/
  1482. case Z_STREAM_END: /* Found end of stream. */
  1483. break;
  1484. default:
  1485. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1486. "File decompression failed (%d)", r);
  1487. return (ARCHIVE_FATAL);
  1488. }
  1489. *used = avail_in - xar->stream.avail_in;
  1490. *outbytes = avail_out - xar->stream.avail_out;
  1491. break;
  1492. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1493. case BZIP2:
  1494. xar->bzstream.next_in = (char *)(uintptr_t)b;
  1495. xar->bzstream.avail_in = avail_in;
  1496. xar->bzstream.next_out = (char *)outbuff;
  1497. xar->bzstream.avail_out = avail_out;
  1498. r = BZ2_bzDecompress(&(xar->bzstream));
  1499. switch (r) {
  1500. case BZ_STREAM_END: /* Found end of stream. */
  1501. switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
  1502. case BZ_OK:
  1503. break;
  1504. default:
  1505. archive_set_error(&(a->archive),
  1506. ARCHIVE_ERRNO_MISC,
  1507. "Failed to clean up decompressor");
  1508. return (ARCHIVE_FATAL);
  1509. }
  1510. xar->bzstream_valid = 0;
  1511. /* FALLTHROUGH */
  1512. case BZ_OK: /* Decompressor made some progress. */
  1513. break;
  1514. default:
  1515. archive_set_error(&(a->archive),
  1516. ARCHIVE_ERRNO_MISC,
  1517. "bzip decompression failed");
  1518. return (ARCHIVE_FATAL);
  1519. }
  1520. *used = avail_in - xar->bzstream.avail_in;
  1521. *outbytes = avail_out - xar->bzstream.avail_out;
  1522. break;
  1523. #endif
  1524. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1525. case LZMA:
  1526. case XZ:
  1527. xar->lzstream.next_in = b;
  1528. xar->lzstream.avail_in = avail_in;
  1529. xar->lzstream.next_out = (unsigned char *)outbuff;
  1530. xar->lzstream.avail_out = avail_out;
  1531. r = lzma_code(&(xar->lzstream), LZMA_RUN);
  1532. switch (r) {
  1533. case LZMA_STREAM_END: /* Found end of stream. */
  1534. lzma_end(&(xar->lzstream));
  1535. xar->lzstream_valid = 0;
  1536. /* FALLTHROUGH */
  1537. case LZMA_OK: /* Decompressor made some progress. */
  1538. break;
  1539. default:
  1540. archive_set_error(&(a->archive),
  1541. ARCHIVE_ERRNO_MISC,
  1542. "%s decompression failed(%d)",
  1543. (xar->entry_encoding == XZ)?"xz":"lzma",
  1544. r);
  1545. return (ARCHIVE_FATAL);
  1546. }
  1547. *used = avail_in - xar->lzstream.avail_in;
  1548. *outbytes = avail_out - xar->lzstream.avail_out;
  1549. break;
  1550. #endif
  1551. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
  1552. case BZIP2:
  1553. #endif
  1554. #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
  1555. case LZMA:
  1556. case XZ:
  1557. #endif
  1558. case NONE:
  1559. default:
  1560. if (outbuff == xar->outbuff) {
  1561. *buff = b;
  1562. *used = avail_in;
  1563. *outbytes = avail_in;
  1564. } else {
  1565. if (avail_out > avail_in)
  1566. avail_out = avail_in;
  1567. memcpy(outbuff, b, avail_out);
  1568. *used = avail_out;
  1569. *outbytes = avail_out;
  1570. }
  1571. break;
  1572. }
  1573. return (ARCHIVE_OK);
  1574. }
  1575. static int
  1576. decompression_cleanup(struct archive_read *a)
  1577. {
  1578. struct xar *xar;
  1579. int r;
  1580. xar = (struct xar *)(a->format->data);
  1581. r = ARCHIVE_OK;
  1582. if (xar->stream_valid) {
  1583. if (inflateEnd(&(xar->stream)) != Z_OK) {
  1584. archive_set_error(&a->archive,
  1585. ARCHIVE_ERRNO_MISC,
  1586. "Failed to clean up zlib decompressor");
  1587. r = ARCHIVE_FATAL;
  1588. }
  1589. }
  1590. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1591. if (xar->bzstream_valid) {
  1592. if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
  1593. archive_set_error(&a->archive,
  1594. ARCHIVE_ERRNO_MISC,
  1595. "Failed to clean up bzip2 decompressor");
  1596. r = ARCHIVE_FATAL;
  1597. }
  1598. }
  1599. #endif
  1600. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1601. if (xar->lzstream_valid)
  1602. lzma_end(&(xar->lzstream));
  1603. #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1604. if (xar->lzstream_valid) {
  1605. if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
  1606. archive_set_error(&a->archive,
  1607. ARCHIVE_ERRNO_MISC,
  1608. "Failed to clean up lzmadec decompressor");
  1609. r = ARCHIVE_FATAL;
  1610. }
  1611. }
  1612. #endif
  1613. return (r);
  1614. }
  1615. static void
  1616. checksum_cleanup(struct archive_read *a) {
  1617. struct xar *xar;
  1618. xar = (struct xar *)(a->format->data);
  1619. _checksum_final(&(xar->a_sumwrk), NULL, 0);
  1620. _checksum_final(&(xar->e_sumwrk), NULL, 0);
  1621. }
  1622. static void
  1623. xmlattr_cleanup(struct xmlattr_list *list)
  1624. {
  1625. struct xmlattr *attr, *next;
  1626. attr = list->first;
  1627. while (attr != NULL) {
  1628. next = attr->next;
  1629. free(attr->name);
  1630. free(attr->value);
  1631. free(attr);
  1632. attr = next;
  1633. }
  1634. list->first = NULL;
  1635. list->last = &(list->first);
  1636. }
  1637. static int
  1638. file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
  1639. {
  1640. struct xar_file *file;
  1641. struct xmlattr *attr;
  1642. file = calloc(1, sizeof(*file));
  1643. if (file == NULL) {
  1644. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1645. return (ARCHIVE_FATAL);
  1646. }
  1647. file->parent = xar->file;
  1648. file->mode = 0777 | AE_IFREG;
  1649. file->atime = 0;
  1650. file->mtime = 0;
  1651. xar->file = file;
  1652. xar->xattr = NULL;
  1653. for (attr = list->first; attr != NULL; attr = attr->next) {
  1654. if (strcmp(attr->name, "id") == 0)
  1655. file->id = atol10(attr->value, strlen(attr->value));
  1656. }
  1657. file->nlink = 1;
  1658. if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
  1659. return (ARCHIVE_FATAL);
  1660. return (ARCHIVE_OK);
  1661. }
  1662. static void
  1663. file_free(struct xar_file *file)
  1664. {
  1665. struct xattr *xattr;
  1666. archive_string_free(&(file->pathname));
  1667. archive_string_free(&(file->symlink));
  1668. archive_string_free(&(file->uname));
  1669. archive_string_free(&(file->gname));
  1670. archive_string_free(&(file->hardlink));
  1671. xattr = file->xattr_list;
  1672. while (xattr != NULL) {
  1673. struct xattr *next;
  1674. next = xattr->next;
  1675. xattr_free(xattr);
  1676. xattr = next;
  1677. }
  1678. free(file);
  1679. }
  1680. static int
  1681. xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
  1682. {
  1683. struct xattr *xattr, **nx;
  1684. struct xmlattr *attr;
  1685. xattr = calloc(1, sizeof(*xattr));
  1686. if (xattr == NULL) {
  1687. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1688. return (ARCHIVE_FATAL);
  1689. }
  1690. xar->xattr = xattr;
  1691. for (attr = list->first; attr != NULL; attr = attr->next) {
  1692. if (strcmp(attr->name, "id") == 0)
  1693. xattr->id = atol10(attr->value, strlen(attr->value));
  1694. }
  1695. /* Chain to xattr list. */
  1696. for (nx = &(xar->file->xattr_list);
  1697. *nx != NULL; nx = &((*nx)->next)) {
  1698. if (xattr->id < (*nx)->id)
  1699. break;
  1700. }
  1701. xattr->next = *nx;
  1702. *nx = xattr;
  1703. return (ARCHIVE_OK);
  1704. }
  1705. static void
  1706. xattr_free(struct xattr *xattr)
  1707. {
  1708. archive_string_free(&(xattr->name));
  1709. free(xattr);
  1710. }
  1711. static int
  1712. getencoding(struct xmlattr_list *list)
  1713. {
  1714. struct xmlattr *attr;
  1715. enum enctype encoding = NONE;
  1716. for (attr = list->first; attr != NULL; attr = attr->next) {
  1717. if (strcmp(attr->name, "style") == 0) {
  1718. if (strcmp(attr->value, "application/octet-stream") == 0)
  1719. encoding = NONE;
  1720. else if (strcmp(attr->value, "application/x-gzip") == 0)
  1721. encoding = GZIP;
  1722. else if (strcmp(attr->value, "application/x-bzip2") == 0)
  1723. encoding = BZIP2;
  1724. else if (strcmp(attr->value, "application/x-lzma") == 0)
  1725. encoding = LZMA;
  1726. else if (strcmp(attr->value, "application/x-xz") == 0)
  1727. encoding = XZ;
  1728. }
  1729. }
  1730. return (encoding);
  1731. }
  1732. static int
  1733. getsumalgorithm(struct xmlattr_list *list)
  1734. {
  1735. struct xmlattr *attr;
  1736. int alg = CKSUM_NONE;
  1737. for (attr = list->first; attr != NULL; attr = attr->next) {
  1738. if (strcmp(attr->name, "style") == 0) {
  1739. const char *v = attr->value;
  1740. if ((v[0] == 'S' || v[0] == 's') &&
  1741. (v[1] == 'H' || v[1] == 'h') &&
  1742. (v[2] == 'A' || v[2] == 'a') &&
  1743. v[3] == '1' && v[4] == '\0')
  1744. alg = CKSUM_SHA1;
  1745. if ((v[0] == 'M' || v[0] == 'm') &&
  1746. (v[1] == 'D' || v[1] == 'd') &&
  1747. v[2] == '5' && v[3] == '\0')
  1748. alg = CKSUM_MD5;
  1749. }
  1750. }
  1751. return (alg);
  1752. }
  1753. static int
  1754. unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
  1755. {
  1756. struct unknown_tag *tag;
  1757. tag = malloc(sizeof(*tag));
  1758. if (tag == NULL) {
  1759. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1760. return (ARCHIVE_FATAL);
  1761. }
  1762. tag->next = xar->unknowntags;
  1763. archive_string_init(&(tag->name));
  1764. archive_strcpy(&(tag->name), name);
  1765. if (xar->unknowntags == NULL) {
  1766. #if DEBUG
  1767. fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
  1768. #endif
  1769. xar->xmlsts_unknown = xar->xmlsts;
  1770. xar->xmlsts = UNKNOWN;
  1771. }
  1772. xar->unknowntags = tag;
  1773. return (ARCHIVE_OK);
  1774. }
  1775. static void
  1776. unknowntag_end(struct xar *xar, const char *name)
  1777. {
  1778. struct unknown_tag *tag;
  1779. tag = xar->unknowntags;
  1780. if (tag == NULL || name == NULL)
  1781. return;
  1782. if (strcmp(tag->name.s, name) == 0) {
  1783. xar->unknowntags = tag->next;
  1784. archive_string_free(&(tag->name));
  1785. free(tag);
  1786. if (xar->unknowntags == NULL) {
  1787. #if DEBUG
  1788. fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
  1789. #endif
  1790. xar->xmlsts = xar->xmlsts_unknown;
  1791. }
  1792. }
  1793. }
  1794. static int
  1795. xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
  1796. {
  1797. struct xar *xar;
  1798. struct xmlattr *attr;
  1799. xar = (struct xar *)(a->format->data);
  1800. #if DEBUG
  1801. fprintf(stderr, "xml_sta:[%s]\n", name);
  1802. for (attr = list->first; attr != NULL; attr = attr->next)
  1803. fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
  1804. attr->name, attr->value);
  1805. #endif
  1806. xar->base64text = 0;
  1807. switch (xar->xmlsts) {
  1808. case INIT:
  1809. if (strcmp(name, "xar") == 0)
  1810. xar->xmlsts = XAR;
  1811. else
  1812. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1813. return (ARCHIVE_FATAL);
  1814. break;
  1815. case XAR:
  1816. if (strcmp(name, "toc") == 0)
  1817. xar->xmlsts = TOC;
  1818. else
  1819. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1820. return (ARCHIVE_FATAL);
  1821. break;
  1822. case TOC:
  1823. if (strcmp(name, "creation-time") == 0)
  1824. xar->xmlsts = TOC_CREATION_TIME;
  1825. else if (strcmp(name, "checksum") == 0)
  1826. xar->xmlsts = TOC_CHECKSUM;
  1827. else if (strcmp(name, "file") == 0) {
  1828. if (file_new(a, xar, list) != ARCHIVE_OK)
  1829. return (ARCHIVE_FATAL);
  1830. xar->xmlsts = TOC_FILE;
  1831. }
  1832. else
  1833. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1834. return (ARCHIVE_FATAL);
  1835. break;
  1836. case TOC_CHECKSUM:
  1837. if (strcmp(name, "offset") == 0)
  1838. xar->xmlsts = TOC_CHECKSUM_OFFSET;
  1839. else if (strcmp(name, "size") == 0)
  1840. xar->xmlsts = TOC_CHECKSUM_SIZE;
  1841. else
  1842. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1843. return (ARCHIVE_FATAL);
  1844. break;
  1845. case TOC_FILE:
  1846. if (strcmp(name, "file") == 0) {
  1847. if (file_new(a, xar, list) != ARCHIVE_OK)
  1848. return (ARCHIVE_FATAL);
  1849. }
  1850. else if (strcmp(name, "data") == 0)
  1851. xar->xmlsts = FILE_DATA;
  1852. else if (strcmp(name, "ea") == 0) {
  1853. if (xattr_new(a, xar, list) != ARCHIVE_OK)
  1854. return (ARCHIVE_FATAL);
  1855. xar->xmlsts = FILE_EA;
  1856. }
  1857. else if (strcmp(name, "ctime") == 0)
  1858. xar->xmlsts = FILE_CTIME;
  1859. else if (strcmp(name, "mtime") == 0)
  1860. xar->xmlsts = FILE_MTIME;
  1861. else if (strcmp(name, "atime") == 0)
  1862. xar->xmlsts = FILE_ATIME;
  1863. else if (strcmp(name, "group") == 0)
  1864. xar->xmlsts = FILE_GROUP;
  1865. else if (strcmp(name, "gid") == 0)
  1866. xar->xmlsts = FILE_GID;
  1867. else if (strcmp(name, "user") == 0)
  1868. xar->xmlsts = FILE_USER;
  1869. else if (strcmp(name, "uid") == 0)
  1870. xar->xmlsts = FILE_UID;
  1871. else if (strcmp(name, "mode") == 0)
  1872. xar->xmlsts = FILE_MODE;
  1873. else if (strcmp(name, "device") == 0)
  1874. xar->xmlsts = FILE_DEVICE;
  1875. else if (strcmp(name, "deviceno") == 0)
  1876. xar->xmlsts = FILE_DEVICENO;
  1877. else if (strcmp(name, "inode") == 0)
  1878. xar->xmlsts = FILE_INODE;
  1879. else if (strcmp(name, "link") == 0)
  1880. xar->xmlsts = FILE_LINK;
  1881. else if (strcmp(name, "type") == 0) {
  1882. xar->xmlsts = FILE_TYPE;
  1883. for (attr = list->first; attr != NULL;
  1884. attr = attr->next) {
  1885. if (strcmp(attr->name, "link") != 0)
  1886. continue;
  1887. if (strcmp(attr->value, "original") == 0) {
  1888. xar->file->hdnext = xar->hdlink_orgs;
  1889. xar->hdlink_orgs = xar->file;
  1890. } else {
  1891. xar->file->link = (unsigned)atol10(attr->value,
  1892. strlen(attr->value));
  1893. if (xar->file->link > 0)
  1894. if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
  1895. return (ARCHIVE_FATAL);
  1896. };
  1897. }
  1898. }
  1899. }
  1900. else if (strcmp(name, "name") == 0) {
  1901. xar->xmlsts = FILE_NAME;
  1902. for (attr = list->first; attr != NULL;
  1903. attr = attr->next) {
  1904. if (strcmp(attr->name, "enctype") == 0 &&
  1905. strcmp(attr->value, "base64") == 0)
  1906. xar->base64text = 1;
  1907. }
  1908. }
  1909. else if (strcmp(name, "acl") == 0)
  1910. xar->xmlsts = FILE_ACL;
  1911. else if (strcmp(name, "flags") == 0)
  1912. xar->xmlsts = FILE_FLAGS;
  1913. else if (strcmp(name, "ext2") == 0)
  1914. xar->xmlsts = FILE_EXT2;
  1915. else
  1916. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1917. return (ARCHIVE_FATAL);
  1918. break;
  1919. case FILE_DATA:
  1920. if (strcmp(name, "length") == 0)
  1921. xar->xmlsts = FILE_DATA_LENGTH;
  1922. else if (strcmp(name, "offset") == 0)
  1923. xar->xmlsts = FILE_DATA_OFFSET;
  1924. else if (strcmp(name, "size") == 0)
  1925. xar->xmlsts = FILE_DATA_SIZE;
  1926. else if (strcmp(name, "encoding") == 0) {
  1927. xar->xmlsts = FILE_DATA_ENCODING;
  1928. xar->file->encoding = getencoding(list);
  1929. }
  1930. else if (strcmp(name, "archived-checksum") == 0) {
  1931. xar->xmlsts = FILE_DATA_A_CHECKSUM;
  1932. xar->file->a_sum.alg = getsumalgorithm(list);
  1933. }
  1934. else if (strcmp(name, "extracted-checksum") == 0) {
  1935. xar->xmlsts = FILE_DATA_E_CHECKSUM;
  1936. xar->file->e_sum.alg = getsumalgorithm(list);
  1937. }
  1938. else if (strcmp(name, "content") == 0)
  1939. xar->xmlsts = FILE_DATA_CONTENT;
  1940. else
  1941. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1942. return (ARCHIVE_FATAL);
  1943. break;
  1944. case FILE_DEVICE:
  1945. if (strcmp(name, "major") == 0)
  1946. xar->xmlsts = FILE_DEVICE_MAJOR;
  1947. else if (strcmp(name, "minor") == 0)
  1948. xar->xmlsts = FILE_DEVICE_MINOR;
  1949. else
  1950. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1951. return (ARCHIVE_FATAL);
  1952. break;
  1953. case FILE_DATA_CONTENT:
  1954. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1955. return (ARCHIVE_FATAL);
  1956. break;
  1957. case FILE_EA:
  1958. if (strcmp(name, "length") == 0)
  1959. xar->xmlsts = FILE_EA_LENGTH;
  1960. else if (strcmp(name, "offset") == 0)
  1961. xar->xmlsts = FILE_EA_OFFSET;
  1962. else if (strcmp(name, "size") == 0)
  1963. xar->xmlsts = FILE_EA_SIZE;
  1964. else if (strcmp(name, "encoding") == 0) {
  1965. xar->xmlsts = FILE_EA_ENCODING;
  1966. xar->xattr->encoding = getencoding(list);
  1967. } else if (strcmp(name, "archived-checksum") == 0)
  1968. xar->xmlsts = FILE_EA_A_CHECKSUM;
  1969. else if (strcmp(name, "extracted-checksum") == 0)
  1970. xar->xmlsts = FILE_EA_E_CHECKSUM;
  1971. else if (strcmp(name, "name") == 0)
  1972. xar->xmlsts = FILE_EA_NAME;
  1973. else if (strcmp(name, "fstype") == 0)
  1974. xar->xmlsts = FILE_EA_FSTYPE;
  1975. else
  1976. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1977. return (ARCHIVE_FATAL);
  1978. break;
  1979. case FILE_ACL:
  1980. if (strcmp(name, "appleextended") == 0)
  1981. xar->xmlsts = FILE_ACL_APPLEEXTENDED;
  1982. else if (strcmp(name, "default") == 0)
  1983. xar->xmlsts = FILE_ACL_DEFAULT;
  1984. else if (strcmp(name, "access") == 0)
  1985. xar->xmlsts = FILE_ACL_ACCESS;
  1986. else
  1987. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1988. return (ARCHIVE_FATAL);
  1989. break;
  1990. case FILE_FLAGS:
  1991. if (!xml_parse_file_flags(xar, name))
  1992. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1993. return (ARCHIVE_FATAL);
  1994. break;
  1995. case FILE_EXT2:
  1996. if (!xml_parse_file_ext2(xar, name))
  1997. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1998. return (ARCHIVE_FATAL);
  1999. break;
  2000. case TOC_CREATION_TIME:
  2001. case TOC_CHECKSUM_OFFSET:
  2002. case TOC_CHECKSUM_SIZE:
  2003. case FILE_DATA_LENGTH:
  2004. case FILE_DATA_OFFSET:
  2005. case FILE_DATA_SIZE:
  2006. case FILE_DATA_ENCODING:
  2007. case FILE_DATA_A_CHECKSUM:
  2008. case FILE_DATA_E_CHECKSUM:
  2009. case FILE_EA_LENGTH:
  2010. case FILE_EA_OFFSET:
  2011. case FILE_EA_SIZE:
  2012. case FILE_EA_ENCODING:
  2013. case FILE_EA_A_CHECKSUM:
  2014. case FILE_EA_E_CHECKSUM:
  2015. case FILE_EA_NAME:
  2016. case FILE_EA_FSTYPE:
  2017. case FILE_CTIME:
  2018. case FILE_MTIME:
  2019. case FILE_ATIME:
  2020. case FILE_GROUP:
  2021. case FILE_GID:
  2022. case FILE_USER:
  2023. case FILE_UID:
  2024. case FILE_INODE:
  2025. case FILE_DEVICE_MAJOR:
  2026. case FILE_DEVICE_MINOR:
  2027. case FILE_DEVICENO:
  2028. case FILE_MODE:
  2029. case FILE_TYPE:
  2030. case FILE_LINK:
  2031. case FILE_NAME:
  2032. case FILE_ACL_DEFAULT:
  2033. case FILE_ACL_ACCESS:
  2034. case FILE_ACL_APPLEEXTENDED:
  2035. case FILE_FLAGS_USER_NODUMP:
  2036. case FILE_FLAGS_USER_IMMUTABLE:
  2037. case FILE_FLAGS_USER_APPEND:
  2038. case FILE_FLAGS_USER_OPAQUE:
  2039. case FILE_FLAGS_USER_NOUNLINK:
  2040. case FILE_FLAGS_SYS_ARCHIVED:
  2041. case FILE_FLAGS_SYS_IMMUTABLE:
  2042. case FILE_FLAGS_SYS_APPEND:
  2043. case FILE_FLAGS_SYS_NOUNLINK:
  2044. case FILE_FLAGS_SYS_SNAPSHOT:
  2045. case FILE_EXT2_SecureDeletion:
  2046. case FILE_EXT2_Undelete:
  2047. case FILE_EXT2_Compress:
  2048. case FILE_EXT2_Synchronous:
  2049. case FILE_EXT2_Immutable:
  2050. case FILE_EXT2_AppendOnly:
  2051. case FILE_EXT2_NoDump:
  2052. case FILE_EXT2_NoAtime:
  2053. case FILE_EXT2_CompDirty:
  2054. case FILE_EXT2_CompBlock:
  2055. case FILE_EXT2_NoCompBlock:
  2056. case FILE_EXT2_CompError:
  2057. case FILE_EXT2_BTree:
  2058. case FILE_EXT2_HashIndexed:
  2059. case FILE_EXT2_iMagic:
  2060. case FILE_EXT2_Journaled:
  2061. case FILE_EXT2_NoTail:
  2062. case FILE_EXT2_DirSync:
  2063. case FILE_EXT2_TopDir:
  2064. case FILE_EXT2_Reserved:
  2065. case UNKNOWN:
  2066. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  2067. return (ARCHIVE_FATAL);
  2068. break;
  2069. }
  2070. return (ARCHIVE_OK);
  2071. }
  2072. static void
  2073. xml_end(void *userData, const char *name)
  2074. {
  2075. struct archive_read *a;
  2076. struct xar *xar;
  2077. a = (struct archive_read *)userData;
  2078. xar = (struct xar *)(a->format->data);
  2079. #if DEBUG
  2080. fprintf(stderr, "xml_end:[%s]\n", name);
  2081. #endif
  2082. switch (xar->xmlsts) {
  2083. case INIT:
  2084. break;
  2085. case XAR:
  2086. if (strcmp(name, "xar") == 0)
  2087. xar->xmlsts = INIT;
  2088. break;
  2089. case TOC:
  2090. if (strcmp(name, "toc") == 0)
  2091. xar->xmlsts = XAR;
  2092. break;
  2093. case TOC_CREATION_TIME:
  2094. if (strcmp(name, "creation-time") == 0)
  2095. xar->xmlsts = TOC;
  2096. break;
  2097. case TOC_CHECKSUM:
  2098. if (strcmp(name, "checksum") == 0)
  2099. xar->xmlsts = TOC;
  2100. break;
  2101. case TOC_CHECKSUM_OFFSET:
  2102. if (strcmp(name, "offset") == 0)
  2103. xar->xmlsts = TOC_CHECKSUM;
  2104. break;
  2105. case TOC_CHECKSUM_SIZE:
  2106. if (strcmp(name, "size") == 0)
  2107. xar->xmlsts = TOC_CHECKSUM;
  2108. break;
  2109. case TOC_FILE:
  2110. if (strcmp(name, "file") == 0) {
  2111. if (xar->file->parent != NULL &&
  2112. ((xar->file->mode & AE_IFMT) == AE_IFDIR))
  2113. xar->file->parent->subdirs++;
  2114. xar->file = xar->file->parent;
  2115. if (xar->file == NULL)
  2116. xar->xmlsts = TOC;
  2117. }
  2118. break;
  2119. case FILE_DATA:
  2120. if (strcmp(name, "data") == 0)
  2121. xar->xmlsts = TOC_FILE;
  2122. break;
  2123. case FILE_DATA_LENGTH:
  2124. if (strcmp(name, "length") == 0)
  2125. xar->xmlsts = FILE_DATA;
  2126. break;
  2127. case FILE_DATA_OFFSET:
  2128. if (strcmp(name, "offset") == 0)
  2129. xar->xmlsts = FILE_DATA;
  2130. break;
  2131. case FILE_DATA_SIZE:
  2132. if (strcmp(name, "size") == 0)
  2133. xar->xmlsts = FILE_DATA;
  2134. break;
  2135. case FILE_DATA_ENCODING:
  2136. if (strcmp(name, "encoding") == 0)
  2137. xar->xmlsts = FILE_DATA;
  2138. break;
  2139. case FILE_DATA_A_CHECKSUM:
  2140. if (strcmp(name, "archived-checksum") == 0)
  2141. xar->xmlsts = FILE_DATA;
  2142. break;
  2143. case FILE_DATA_E_CHECKSUM:
  2144. if (strcmp(name, "extracted-checksum") == 0)
  2145. xar->xmlsts = FILE_DATA;
  2146. break;
  2147. case FILE_DATA_CONTENT:
  2148. if (strcmp(name, "content") == 0)
  2149. xar->xmlsts = FILE_DATA;
  2150. break;
  2151. case FILE_EA:
  2152. if (strcmp(name, "ea") == 0) {
  2153. xar->xmlsts = TOC_FILE;
  2154. xar->xattr = NULL;
  2155. }
  2156. break;
  2157. case FILE_EA_LENGTH:
  2158. if (strcmp(name, "length") == 0)
  2159. xar->xmlsts = FILE_EA;
  2160. break;
  2161. case FILE_EA_OFFSET:
  2162. if (strcmp(name, "offset") == 0)
  2163. xar->xmlsts = FILE_EA;
  2164. break;
  2165. case FILE_EA_SIZE:
  2166. if (strcmp(name, "size") == 0)
  2167. xar->xmlsts = FILE_EA;
  2168. break;
  2169. case FILE_EA_ENCODING:
  2170. if (strcmp(name, "encoding") == 0)
  2171. xar->xmlsts = FILE_EA;
  2172. break;
  2173. case FILE_EA_A_CHECKSUM:
  2174. if (strcmp(name, "archived-checksum") == 0)
  2175. xar->xmlsts = FILE_EA;
  2176. break;
  2177. case FILE_EA_E_CHECKSUM:
  2178. if (strcmp(name, "extracted-checksum") == 0)
  2179. xar->xmlsts = FILE_EA;
  2180. break;
  2181. case FILE_EA_NAME:
  2182. if (strcmp(name, "name") == 0)
  2183. xar->xmlsts = FILE_EA;
  2184. break;
  2185. case FILE_EA_FSTYPE:
  2186. if (strcmp(name, "fstype") == 0)
  2187. xar->xmlsts = FILE_EA;
  2188. break;
  2189. case FILE_CTIME:
  2190. if (strcmp(name, "ctime") == 0)
  2191. xar->xmlsts = TOC_FILE;
  2192. break;
  2193. case FILE_MTIME:
  2194. if (strcmp(name, "mtime") == 0)
  2195. xar->xmlsts = TOC_FILE;
  2196. break;
  2197. case FILE_ATIME:
  2198. if (strcmp(name, "atime") == 0)
  2199. xar->xmlsts = TOC_FILE;
  2200. break;
  2201. case FILE_GROUP:
  2202. if (strcmp(name, "group") == 0)
  2203. xar->xmlsts = TOC_FILE;
  2204. break;
  2205. case FILE_GID:
  2206. if (strcmp(name, "gid") == 0)
  2207. xar->xmlsts = TOC_FILE;
  2208. break;
  2209. case FILE_USER:
  2210. if (strcmp(name, "user") == 0)
  2211. xar->xmlsts = TOC_FILE;
  2212. break;
  2213. case FILE_UID:
  2214. if (strcmp(name, "uid") == 0)
  2215. xar->xmlsts = TOC_FILE;
  2216. break;
  2217. case FILE_MODE:
  2218. if (strcmp(name, "mode") == 0)
  2219. xar->xmlsts = TOC_FILE;
  2220. break;
  2221. case FILE_DEVICE:
  2222. if (strcmp(name, "device") == 0)
  2223. xar->xmlsts = TOC_FILE;
  2224. break;
  2225. case FILE_DEVICE_MAJOR:
  2226. if (strcmp(name, "major") == 0)
  2227. xar->xmlsts = FILE_DEVICE;
  2228. break;
  2229. case FILE_DEVICE_MINOR:
  2230. if (strcmp(name, "minor") == 0)
  2231. xar->xmlsts = FILE_DEVICE;
  2232. break;
  2233. case FILE_DEVICENO:
  2234. if (strcmp(name, "deviceno") == 0)
  2235. xar->xmlsts = TOC_FILE;
  2236. break;
  2237. case FILE_INODE:
  2238. if (strcmp(name, "inode") == 0)
  2239. xar->xmlsts = TOC_FILE;
  2240. break;
  2241. case FILE_LINK:
  2242. if (strcmp(name, "link") == 0)
  2243. xar->xmlsts = TOC_FILE;
  2244. break;
  2245. case FILE_TYPE:
  2246. if (strcmp(name, "type") == 0)
  2247. xar->xmlsts = TOC_FILE;
  2248. break;
  2249. case FILE_NAME:
  2250. if (strcmp(name, "name") == 0)
  2251. xar->xmlsts = TOC_FILE;
  2252. break;
  2253. case FILE_ACL:
  2254. if (strcmp(name, "acl") == 0)
  2255. xar->xmlsts = TOC_FILE;
  2256. break;
  2257. case FILE_ACL_DEFAULT:
  2258. if (strcmp(name, "default") == 0)
  2259. xar->xmlsts = FILE_ACL;
  2260. break;
  2261. case FILE_ACL_ACCESS:
  2262. if (strcmp(name, "access") == 0)
  2263. xar->xmlsts = FILE_ACL;
  2264. break;
  2265. case FILE_ACL_APPLEEXTENDED:
  2266. if (strcmp(name, "appleextended") == 0)
  2267. xar->xmlsts = FILE_ACL;
  2268. break;
  2269. case FILE_FLAGS:
  2270. if (strcmp(name, "flags") == 0)
  2271. xar->xmlsts = TOC_FILE;
  2272. break;
  2273. case FILE_FLAGS_USER_NODUMP:
  2274. if (strcmp(name, "UserNoDump") == 0)
  2275. xar->xmlsts = FILE_FLAGS;
  2276. break;
  2277. case FILE_FLAGS_USER_IMMUTABLE:
  2278. if (strcmp(name, "UserImmutable") == 0)
  2279. xar->xmlsts = FILE_FLAGS;
  2280. break;
  2281. case FILE_FLAGS_USER_APPEND:
  2282. if (strcmp(name, "UserAppend") == 0)
  2283. xar->xmlsts = FILE_FLAGS;
  2284. break;
  2285. case FILE_FLAGS_USER_OPAQUE:
  2286. if (strcmp(name, "UserOpaque") == 0)
  2287. xar->xmlsts = FILE_FLAGS;
  2288. break;
  2289. case FILE_FLAGS_USER_NOUNLINK:
  2290. if (strcmp(name, "UserNoUnlink") == 0)
  2291. xar->xmlsts = FILE_FLAGS;
  2292. break;
  2293. case FILE_FLAGS_SYS_ARCHIVED:
  2294. if (strcmp(name, "SystemArchived") == 0)
  2295. xar->xmlsts = FILE_FLAGS;
  2296. break;
  2297. case FILE_FLAGS_SYS_IMMUTABLE:
  2298. if (strcmp(name, "SystemImmutable") == 0)
  2299. xar->xmlsts = FILE_FLAGS;
  2300. break;
  2301. case FILE_FLAGS_SYS_APPEND:
  2302. if (strcmp(name, "SystemAppend") == 0)
  2303. xar->xmlsts = FILE_FLAGS;
  2304. break;
  2305. case FILE_FLAGS_SYS_NOUNLINK:
  2306. if (strcmp(name, "SystemNoUnlink") == 0)
  2307. xar->xmlsts = FILE_FLAGS;
  2308. break;
  2309. case FILE_FLAGS_SYS_SNAPSHOT:
  2310. if (strcmp(name, "SystemSnapshot") == 0)
  2311. xar->xmlsts = FILE_FLAGS;
  2312. break;
  2313. case FILE_EXT2:
  2314. if (strcmp(name, "ext2") == 0)
  2315. xar->xmlsts = TOC_FILE;
  2316. break;
  2317. case FILE_EXT2_SecureDeletion:
  2318. if (strcmp(name, "SecureDeletion") == 0)
  2319. xar->xmlsts = FILE_EXT2;
  2320. break;
  2321. case FILE_EXT2_Undelete:
  2322. if (strcmp(name, "Undelete") == 0)
  2323. xar->xmlsts = FILE_EXT2;
  2324. break;
  2325. case FILE_EXT2_Compress:
  2326. if (strcmp(name, "Compress") == 0)
  2327. xar->xmlsts = FILE_EXT2;
  2328. break;
  2329. case FILE_EXT2_Synchronous:
  2330. if (strcmp(name, "Synchronous") == 0)
  2331. xar->xmlsts = FILE_EXT2;
  2332. break;
  2333. case FILE_EXT2_Immutable:
  2334. if (strcmp(name, "Immutable") == 0)
  2335. xar->xmlsts = FILE_EXT2;
  2336. break;
  2337. case FILE_EXT2_AppendOnly:
  2338. if (strcmp(name, "AppendOnly") == 0)
  2339. xar->xmlsts = FILE_EXT2;
  2340. break;
  2341. case FILE_EXT2_NoDump:
  2342. if (strcmp(name, "NoDump") == 0)
  2343. xar->xmlsts = FILE_EXT2;
  2344. break;
  2345. case FILE_EXT2_NoAtime:
  2346. if (strcmp(name, "NoAtime") == 0)
  2347. xar->xmlsts = FILE_EXT2;
  2348. break;
  2349. case FILE_EXT2_CompDirty:
  2350. if (strcmp(name, "CompDirty") == 0)
  2351. xar->xmlsts = FILE_EXT2;
  2352. break;
  2353. case FILE_EXT2_CompBlock:
  2354. if (strcmp(name, "CompBlock") == 0)
  2355. xar->xmlsts = FILE_EXT2;
  2356. break;
  2357. case FILE_EXT2_NoCompBlock:
  2358. if (strcmp(name, "NoCompBlock") == 0)
  2359. xar->xmlsts = FILE_EXT2;
  2360. break;
  2361. case FILE_EXT2_CompError:
  2362. if (strcmp(name, "CompError") == 0)
  2363. xar->xmlsts = FILE_EXT2;
  2364. break;
  2365. case FILE_EXT2_BTree:
  2366. if (strcmp(name, "BTree") == 0)
  2367. xar->xmlsts = FILE_EXT2;
  2368. break;
  2369. case FILE_EXT2_HashIndexed:
  2370. if (strcmp(name, "HashIndexed") == 0)
  2371. xar->xmlsts = FILE_EXT2;
  2372. break;
  2373. case FILE_EXT2_iMagic:
  2374. if (strcmp(name, "iMagic") == 0)
  2375. xar->xmlsts = FILE_EXT2;
  2376. break;
  2377. case FILE_EXT2_Journaled:
  2378. if (strcmp(name, "Journaled") == 0)
  2379. xar->xmlsts = FILE_EXT2;
  2380. break;
  2381. case FILE_EXT2_NoTail:
  2382. if (strcmp(name, "NoTail") == 0)
  2383. xar->xmlsts = FILE_EXT2;
  2384. break;
  2385. case FILE_EXT2_DirSync:
  2386. if (strcmp(name, "DirSync") == 0)
  2387. xar->xmlsts = FILE_EXT2;
  2388. break;
  2389. case FILE_EXT2_TopDir:
  2390. if (strcmp(name, "TopDir") == 0)
  2391. xar->xmlsts = FILE_EXT2;
  2392. break;
  2393. case FILE_EXT2_Reserved:
  2394. if (strcmp(name, "Reserved") == 0)
  2395. xar->xmlsts = FILE_EXT2;
  2396. break;
  2397. case UNKNOWN:
  2398. unknowntag_end(xar, name);
  2399. break;
  2400. }
  2401. }
  2402. static const int base64[256] = {
  2403. -1, -1, -1, -1, -1, -1, -1, -1,
  2404. -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
  2405. -1, -1, -1, -1, -1, -1, -1, -1,
  2406. -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
  2407. -1, -1, -1, -1, -1, -1, -1, -1,
  2408. -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
  2409. 52, 53, 54, 55, 56, 57, 58, 59,
  2410. 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
  2411. -1, 0, 1, 2, 3, 4, 5, 6,
  2412. 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
  2413. 15, 16, 17, 18, 19, 20, 21, 22,
  2414. 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
  2415. -1, 26, 27, 28, 29, 30, 31, 32,
  2416. 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
  2417. 41, 42, 43, 44, 45, 46, 47, 48,
  2418. 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
  2419. -1, -1, -1, -1, -1, -1, -1, -1,
  2420. -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
  2421. -1, -1, -1, -1, -1, -1, -1, -1,
  2422. -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
  2423. -1, -1, -1, -1, -1, -1, -1, -1,
  2424. -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
  2425. -1, -1, -1, -1, -1, -1, -1, -1,
  2426. -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
  2427. -1, -1, -1, -1, -1, -1, -1, -1,
  2428. -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
  2429. -1, -1, -1, -1, -1, -1, -1, -1,
  2430. -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
  2431. -1, -1, -1, -1, -1, -1, -1, -1,
  2432. -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
  2433. -1, -1, -1, -1, -1, -1, -1, -1,
  2434. -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
  2435. };
  2436. static void
  2437. strappend_base64(struct xar *xar,
  2438. struct archive_string *as, const char *s, size_t l)
  2439. {
  2440. unsigned char buff[256];
  2441. unsigned char *out;
  2442. const unsigned char *b;
  2443. size_t len;
  2444. (void)xar; /* UNUSED */
  2445. len = 0;
  2446. out = buff;
  2447. b = (const unsigned char *)s;
  2448. while (l > 0) {
  2449. int n = 0;
  2450. if (base64[b[0]] < 0 || base64[b[1]] < 0)
  2451. break;
  2452. n = base64[*b++] << 18;
  2453. n |= base64[*b++] << 12;
  2454. *out++ = n >> 16;
  2455. len++;
  2456. l -= 2;
  2457. if (l > 0) {
  2458. if (base64[*b] < 0)
  2459. break;
  2460. n |= base64[*b++] << 6;
  2461. *out++ = (n >> 8) & 0xFF;
  2462. len++;
  2463. --l;
  2464. }
  2465. if (l > 0) {
  2466. if (base64[*b] < 0)
  2467. break;
  2468. n |= base64[*b++];
  2469. *out++ = n & 0xFF;
  2470. len++;
  2471. --l;
  2472. }
  2473. if (len+3 >= sizeof(buff)) {
  2474. archive_strncat(as, (const char *)buff, len);
  2475. len = 0;
  2476. out = buff;
  2477. }
  2478. }
  2479. if (len > 0)
  2480. archive_strncat(as, (const char *)buff, len);
  2481. }
  2482. static int
  2483. is_string(const char *known, const char *data, size_t len)
  2484. {
  2485. if (strlen(known) != len)
  2486. return -1;
  2487. return memcmp(data, known, len);
  2488. }
  2489. static void
  2490. xml_data(void *userData, const char *s, int len)
  2491. {
  2492. struct archive_read *a;
  2493. struct xar *xar;
  2494. a = (struct archive_read *)userData;
  2495. xar = (struct xar *)(a->format->data);
  2496. #if DEBUG
  2497. {
  2498. char buff[1024];
  2499. if (len > (int)(sizeof(buff)-1))
  2500. len = (int)(sizeof(buff)-1);
  2501. strncpy(buff, s, len);
  2502. buff[len] = 0;
  2503. fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
  2504. }
  2505. #endif
  2506. switch (xar->xmlsts) {
  2507. case TOC_CHECKSUM_OFFSET:
  2508. xar->toc_chksum_offset = atol10(s, len);
  2509. break;
  2510. case TOC_CHECKSUM_SIZE:
  2511. xar->toc_chksum_size = atol10(s, len);
  2512. break;
  2513. default:
  2514. break;
  2515. }
  2516. if (xar->file == NULL)
  2517. return;
  2518. switch (xar->xmlsts) {
  2519. case FILE_NAME:
  2520. if (xar->file->parent != NULL) {
  2521. archive_string_concat(&(xar->file->pathname),
  2522. &(xar->file->parent->pathname));
  2523. archive_strappend_char(&(xar->file->pathname), '/');
  2524. }
  2525. xar->file->has |= HAS_PATHNAME;
  2526. if (xar->base64text) {
  2527. strappend_base64(xar,
  2528. &(xar->file->pathname), s, len);
  2529. } else
  2530. archive_strncat(&(xar->file->pathname), s, len);
  2531. break;
  2532. case FILE_LINK:
  2533. xar->file->has |= HAS_SYMLINK;
  2534. archive_strncpy(&(xar->file->symlink), s, len);
  2535. break;
  2536. case FILE_TYPE:
  2537. if (is_string("file", s, len) == 0 ||
  2538. is_string("hardlink", s, len) == 0)
  2539. xar->file->mode =
  2540. (xar->file->mode & ~AE_IFMT) | AE_IFREG;
  2541. if (is_string("directory", s, len) == 0)
  2542. xar->file->mode =
  2543. (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
  2544. if (is_string("symlink", s, len) == 0)
  2545. xar->file->mode =
  2546. (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
  2547. if (is_string("character special", s, len) == 0)
  2548. xar->file->mode =
  2549. (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
  2550. if (is_string("block special", s, len) == 0)
  2551. xar->file->mode =
  2552. (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
  2553. if (is_string("socket", s, len) == 0)
  2554. xar->file->mode =
  2555. (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
  2556. if (is_string("fifo", s, len) == 0)
  2557. xar->file->mode =
  2558. (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
  2559. xar->file->has |= HAS_TYPE;
  2560. break;
  2561. case FILE_INODE:
  2562. xar->file->has |= HAS_INO;
  2563. xar->file->ino64 = atol10(s, len);
  2564. break;
  2565. case FILE_DEVICE_MAJOR:
  2566. xar->file->has |= HAS_DEVMAJOR;
  2567. xar->file->devmajor = (dev_t)atol10(s, len);
  2568. break;
  2569. case FILE_DEVICE_MINOR:
  2570. xar->file->has |= HAS_DEVMINOR;
  2571. xar->file->devminor = (dev_t)atol10(s, len);
  2572. break;
  2573. case FILE_DEVICENO:
  2574. xar->file->has |= HAS_DEV;
  2575. xar->file->dev = (dev_t)atol10(s, len);
  2576. break;
  2577. case FILE_MODE:
  2578. xar->file->has |= HAS_MODE;
  2579. xar->file->mode =
  2580. (xar->file->mode & AE_IFMT) |
  2581. ((mode_t)(atol8(s, len)) & ~AE_IFMT);
  2582. break;
  2583. case FILE_GROUP:
  2584. xar->file->has |= HAS_GID;
  2585. archive_strncpy(&(xar->file->gname), s, len);
  2586. break;
  2587. case FILE_GID:
  2588. xar->file->has |= HAS_GID;
  2589. xar->file->gid = atol10(s, len);
  2590. break;
  2591. case FILE_USER:
  2592. xar->file->has |= HAS_UID;
  2593. archive_strncpy(&(xar->file->uname), s, len);
  2594. break;
  2595. case FILE_UID:
  2596. xar->file->has |= HAS_UID;
  2597. xar->file->uid = atol10(s, len);
  2598. break;
  2599. case FILE_CTIME:
  2600. xar->file->has |= HAS_TIME | HAS_CTIME;
  2601. xar->file->ctime = parse_time(s, len);
  2602. break;
  2603. case FILE_MTIME:
  2604. xar->file->has |= HAS_TIME | HAS_MTIME;
  2605. xar->file->mtime = parse_time(s, len);
  2606. break;
  2607. case FILE_ATIME:
  2608. xar->file->has |= HAS_TIME | HAS_ATIME;
  2609. xar->file->atime = parse_time(s, len);
  2610. break;
  2611. case FILE_DATA_LENGTH:
  2612. xar->file->has |= HAS_DATA;
  2613. xar->file->length = atol10(s, len);
  2614. break;
  2615. case FILE_DATA_OFFSET:
  2616. xar->file->has |= HAS_DATA;
  2617. xar->file->offset = atol10(s, len);
  2618. break;
  2619. case FILE_DATA_SIZE:
  2620. xar->file->has |= HAS_DATA;
  2621. xar->file->size = atol10(s, len);
  2622. break;
  2623. case FILE_DATA_A_CHECKSUM:
  2624. xar->file->a_sum.len = atohex(xar->file->a_sum.val,
  2625. sizeof(xar->file->a_sum.val), s, len);
  2626. break;
  2627. case FILE_DATA_E_CHECKSUM:
  2628. xar->file->e_sum.len = atohex(xar->file->e_sum.val,
  2629. sizeof(xar->file->e_sum.val), s, len);
  2630. break;
  2631. case FILE_EA_LENGTH:
  2632. xar->file->has |= HAS_XATTR;
  2633. xar->xattr->length = atol10(s, len);
  2634. break;
  2635. case FILE_EA_OFFSET:
  2636. xar->file->has |= HAS_XATTR;
  2637. xar->xattr->offset = atol10(s, len);
  2638. break;
  2639. case FILE_EA_SIZE:
  2640. xar->file->has |= HAS_XATTR;
  2641. xar->xattr->size = atol10(s, len);
  2642. break;
  2643. case FILE_EA_A_CHECKSUM:
  2644. xar->file->has |= HAS_XATTR;
  2645. xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
  2646. sizeof(xar->xattr->a_sum.val), s, len);
  2647. break;
  2648. case FILE_EA_E_CHECKSUM:
  2649. xar->file->has |= HAS_XATTR;
  2650. xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
  2651. sizeof(xar->xattr->e_sum.val), s, len);
  2652. break;
  2653. case FILE_EA_NAME:
  2654. xar->file->has |= HAS_XATTR;
  2655. archive_strncpy(&(xar->xattr->name), s, len);
  2656. break;
  2657. case FILE_EA_FSTYPE:
  2658. xar->file->has |= HAS_XATTR;
  2659. archive_strncpy(&(xar->xattr->fstype), s, len);
  2660. break;
  2661. break;
  2662. case FILE_ACL_DEFAULT:
  2663. case FILE_ACL_ACCESS:
  2664. case FILE_ACL_APPLEEXTENDED:
  2665. xar->file->has |= HAS_ACL;
  2666. /* TODO */
  2667. break;
  2668. case INIT:
  2669. case XAR:
  2670. case TOC:
  2671. case TOC_CREATION_TIME:
  2672. case TOC_CHECKSUM:
  2673. case TOC_CHECKSUM_OFFSET:
  2674. case TOC_CHECKSUM_SIZE:
  2675. case TOC_FILE:
  2676. case FILE_DATA:
  2677. case FILE_DATA_ENCODING:
  2678. case FILE_DATA_CONTENT:
  2679. case FILE_DEVICE:
  2680. case FILE_EA:
  2681. case FILE_EA_ENCODING:
  2682. case FILE_ACL:
  2683. case FILE_FLAGS:
  2684. case FILE_FLAGS_USER_NODUMP:
  2685. case FILE_FLAGS_USER_IMMUTABLE:
  2686. case FILE_FLAGS_USER_APPEND:
  2687. case FILE_FLAGS_USER_OPAQUE:
  2688. case FILE_FLAGS_USER_NOUNLINK:
  2689. case FILE_FLAGS_SYS_ARCHIVED:
  2690. case FILE_FLAGS_SYS_IMMUTABLE:
  2691. case FILE_FLAGS_SYS_APPEND:
  2692. case FILE_FLAGS_SYS_NOUNLINK:
  2693. case FILE_FLAGS_SYS_SNAPSHOT:
  2694. case FILE_EXT2:
  2695. case FILE_EXT2_SecureDeletion:
  2696. case FILE_EXT2_Undelete:
  2697. case FILE_EXT2_Compress:
  2698. case FILE_EXT2_Synchronous:
  2699. case FILE_EXT2_Immutable:
  2700. case FILE_EXT2_AppendOnly:
  2701. case FILE_EXT2_NoDump:
  2702. case FILE_EXT2_NoAtime:
  2703. case FILE_EXT2_CompDirty:
  2704. case FILE_EXT2_CompBlock:
  2705. case FILE_EXT2_NoCompBlock:
  2706. case FILE_EXT2_CompError:
  2707. case FILE_EXT2_BTree:
  2708. case FILE_EXT2_HashIndexed:
  2709. case FILE_EXT2_iMagic:
  2710. case FILE_EXT2_Journaled:
  2711. case FILE_EXT2_NoTail:
  2712. case FILE_EXT2_DirSync:
  2713. case FILE_EXT2_TopDir:
  2714. case FILE_EXT2_Reserved:
  2715. case UNKNOWN:
  2716. break;
  2717. }
  2718. }
  2719. /*
  2720. * BSD file flags.
  2721. */
  2722. static int
  2723. xml_parse_file_flags(struct xar *xar, const char *name)
  2724. {
  2725. const char *flag = NULL;
  2726. if (strcmp(name, "UserNoDump") == 0) {
  2727. xar->xmlsts = FILE_FLAGS_USER_NODUMP;
  2728. flag = "nodump";
  2729. }
  2730. else if (strcmp(name, "UserImmutable") == 0) {
  2731. xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
  2732. flag = "uimmutable";
  2733. }
  2734. else if (strcmp(name, "UserAppend") == 0) {
  2735. xar->xmlsts = FILE_FLAGS_USER_APPEND;
  2736. flag = "uappend";
  2737. }
  2738. else if (strcmp(name, "UserOpaque") == 0) {
  2739. xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
  2740. flag = "opaque";
  2741. }
  2742. else if (strcmp(name, "UserNoUnlink") == 0) {
  2743. xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
  2744. flag = "nouunlink";
  2745. }
  2746. else if (strcmp(name, "SystemArchived") == 0) {
  2747. xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
  2748. flag = "archived";
  2749. }
  2750. else if (strcmp(name, "SystemImmutable") == 0) {
  2751. xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
  2752. flag = "simmutable";
  2753. }
  2754. else if (strcmp(name, "SystemAppend") == 0) {
  2755. xar->xmlsts = FILE_FLAGS_SYS_APPEND;
  2756. flag = "sappend";
  2757. }
  2758. else if (strcmp(name, "SystemNoUnlink") == 0) {
  2759. xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
  2760. flag = "nosunlink";
  2761. }
  2762. else if (strcmp(name, "SystemSnapshot") == 0) {
  2763. xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
  2764. flag = "snapshot";
  2765. }
  2766. if (flag == NULL)
  2767. return (0);
  2768. xar->file->has |= HAS_FFLAGS;
  2769. if (archive_strlen(&(xar->file->fflags_text)) > 0)
  2770. archive_strappend_char(&(xar->file->fflags_text), ',');
  2771. archive_strcat(&(xar->file->fflags_text), flag);
  2772. return (1);
  2773. }
  2774. /*
  2775. * Linux file flags.
  2776. */
  2777. static int
  2778. xml_parse_file_ext2(struct xar *xar, const char *name)
  2779. {
  2780. const char *flag = NULL;
  2781. if (strcmp(name, "SecureDeletion") == 0) {
  2782. xar->xmlsts = FILE_EXT2_SecureDeletion;
  2783. flag = "securedeletion";
  2784. }
  2785. else if (strcmp(name, "Undelete") == 0) {
  2786. xar->xmlsts = FILE_EXT2_Undelete;
  2787. flag = "nouunlink";
  2788. }
  2789. else if (strcmp(name, "Compress") == 0) {
  2790. xar->xmlsts = FILE_EXT2_Compress;
  2791. flag = "compress";
  2792. }
  2793. else if (strcmp(name, "Synchronous") == 0) {
  2794. xar->xmlsts = FILE_EXT2_Synchronous;
  2795. flag = "sync";
  2796. }
  2797. else if (strcmp(name, "Immutable") == 0) {
  2798. xar->xmlsts = FILE_EXT2_Immutable;
  2799. flag = "simmutable";
  2800. }
  2801. else if (strcmp(name, "AppendOnly") == 0) {
  2802. xar->xmlsts = FILE_EXT2_AppendOnly;
  2803. flag = "sappend";
  2804. }
  2805. else if (strcmp(name, "NoDump") == 0) {
  2806. xar->xmlsts = FILE_EXT2_NoDump;
  2807. flag = "nodump";
  2808. }
  2809. else if (strcmp(name, "NoAtime") == 0) {
  2810. xar->xmlsts = FILE_EXT2_NoAtime;
  2811. flag = "noatime";
  2812. }
  2813. else if (strcmp(name, "CompDirty") == 0) {
  2814. xar->xmlsts = FILE_EXT2_CompDirty;
  2815. flag = "compdirty";
  2816. }
  2817. else if (strcmp(name, "CompBlock") == 0) {
  2818. xar->xmlsts = FILE_EXT2_CompBlock;
  2819. flag = "comprblk";
  2820. }
  2821. else if (strcmp(name, "NoCompBlock") == 0) {
  2822. xar->xmlsts = FILE_EXT2_NoCompBlock;
  2823. flag = "nocomprblk";
  2824. }
  2825. else if (strcmp(name, "CompError") == 0) {
  2826. xar->xmlsts = FILE_EXT2_CompError;
  2827. flag = "comperr";
  2828. }
  2829. else if (strcmp(name, "BTree") == 0) {
  2830. xar->xmlsts = FILE_EXT2_BTree;
  2831. flag = "btree";
  2832. }
  2833. else if (strcmp(name, "HashIndexed") == 0) {
  2834. xar->xmlsts = FILE_EXT2_HashIndexed;
  2835. flag = "hashidx";
  2836. }
  2837. else if (strcmp(name, "iMagic") == 0) {
  2838. xar->xmlsts = FILE_EXT2_iMagic;
  2839. flag = "imagic";
  2840. }
  2841. else if (strcmp(name, "Journaled") == 0) {
  2842. xar->xmlsts = FILE_EXT2_Journaled;
  2843. flag = "journal";
  2844. }
  2845. else if (strcmp(name, "NoTail") == 0) {
  2846. xar->xmlsts = FILE_EXT2_NoTail;
  2847. flag = "notail";
  2848. }
  2849. else if (strcmp(name, "DirSync") == 0) {
  2850. xar->xmlsts = FILE_EXT2_DirSync;
  2851. flag = "dirsync";
  2852. }
  2853. else if (strcmp(name, "TopDir") == 0) {
  2854. xar->xmlsts = FILE_EXT2_TopDir;
  2855. flag = "topdir";
  2856. }
  2857. else if (strcmp(name, "Reserved") == 0) {
  2858. xar->xmlsts = FILE_EXT2_Reserved;
  2859. flag = "reserved";
  2860. }
  2861. if (flag == NULL)
  2862. return (0);
  2863. if (archive_strlen(&(xar->file->fflags_text)) > 0)
  2864. archive_strappend_char(&(xar->file->fflags_text), ',');
  2865. archive_strcat(&(xar->file->fflags_text), flag);
  2866. return (1);
  2867. }
  2868. #ifdef HAVE_LIBXML_XMLREADER_H
  2869. static int
  2870. xml2_xmlattr_setup(struct archive_read *a,
  2871. struct xmlattr_list *list, xmlTextReaderPtr reader)
  2872. {
  2873. struct xmlattr *attr;
  2874. int r;
  2875. list->first = NULL;
  2876. list->last = &(list->first);
  2877. r = xmlTextReaderMoveToFirstAttribute(reader);
  2878. while (r == 1) {
  2879. attr = malloc(sizeof*(attr));
  2880. if (attr == NULL) {
  2881. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2882. return (ARCHIVE_FATAL);
  2883. }
  2884. attr->name = strdup(
  2885. (const char *)xmlTextReaderConstLocalName(reader));
  2886. if (attr->name == NULL) {
  2887. free(attr);
  2888. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2889. return (ARCHIVE_FATAL);
  2890. }
  2891. attr->value = strdup(
  2892. (const char *)xmlTextReaderConstValue(reader));
  2893. if (attr->value == NULL) {
  2894. free(attr->name);
  2895. free(attr);
  2896. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2897. return (ARCHIVE_FATAL);
  2898. }
  2899. attr->next = NULL;
  2900. *list->last = attr;
  2901. list->last = &(attr->next);
  2902. r = xmlTextReaderMoveToNextAttribute(reader);
  2903. }
  2904. return (r);
  2905. }
  2906. static int
  2907. xml2_read_cb(void *context, char *buffer, int len)
  2908. {
  2909. struct archive_read *a;
  2910. struct xar *xar;
  2911. const void *d;
  2912. size_t outbytes;
  2913. size_t used = 0;
  2914. int r;
  2915. a = (struct archive_read *)context;
  2916. xar = (struct xar *)(a->format->data);
  2917. if (xar->toc_remaining <= 0)
  2918. return (0);
  2919. d = buffer;
  2920. outbytes = len;
  2921. r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
  2922. if (r != ARCHIVE_OK)
  2923. return (r);
  2924. __archive_read_consume(a, used);
  2925. xar->toc_remaining -= used;
  2926. xar->offset += used;
  2927. xar->toc_total += outbytes;
  2928. PRINT_TOC(buffer, len);
  2929. return ((int)outbytes);
  2930. }
  2931. static int
  2932. xml2_close_cb(void *context)
  2933. {
  2934. (void)context; /* UNUSED */
  2935. return (0);
  2936. }
  2937. static void
  2938. xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
  2939. xmlTextReaderLocatorPtr locator)
  2940. {
  2941. struct archive_read *a;
  2942. (void)locator; /* UNUSED */
  2943. a = (struct archive_read *)arg;
  2944. switch (severity) {
  2945. case XML_PARSER_SEVERITY_VALIDITY_WARNING:
  2946. case XML_PARSER_SEVERITY_WARNING:
  2947. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2948. "XML Parsing error: %s", msg);
  2949. break;
  2950. case XML_PARSER_SEVERITY_VALIDITY_ERROR:
  2951. case XML_PARSER_SEVERITY_ERROR:
  2952. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2953. "XML Parsing error: %s", msg);
  2954. break;
  2955. }
  2956. }
  2957. static int
  2958. xml2_read_toc(struct archive_read *a)
  2959. {
  2960. xmlTextReaderPtr reader;
  2961. struct xmlattr_list list;
  2962. int r;
  2963. reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
  2964. if (reader == NULL) {
  2965. archive_set_error(&a->archive, ENOMEM,
  2966. "Couldn't allocate memory for xml parser");
  2967. return (ARCHIVE_FATAL);
  2968. }
  2969. xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
  2970. while ((r = xmlTextReaderRead(reader)) == 1) {
  2971. const char *name, *value;
  2972. int type, empty;
  2973. type = xmlTextReaderNodeType(reader);
  2974. name = (const char *)xmlTextReaderConstLocalName(reader);
  2975. switch (type) {
  2976. case XML_READER_TYPE_ELEMENT:
  2977. empty = xmlTextReaderIsEmptyElement(reader);
  2978. r = xml2_xmlattr_setup(a, &list, reader);
  2979. if (r == ARCHIVE_OK)
  2980. r = xml_start(a, name, &list);
  2981. xmlattr_cleanup(&list);
  2982. if (r != ARCHIVE_OK)
  2983. return (r);
  2984. if (empty)
  2985. xml_end(a, name);
  2986. break;
  2987. case XML_READER_TYPE_END_ELEMENT:
  2988. xml_end(a, name);
  2989. break;
  2990. case XML_READER_TYPE_TEXT:
  2991. value = (const char *)xmlTextReaderConstValue(reader);
  2992. xml_data(a, value, strlen(value));
  2993. break;
  2994. case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
  2995. default:
  2996. break;
  2997. }
  2998. if (r < 0)
  2999. break;
  3000. }
  3001. xmlFreeTextReader(reader);
  3002. xmlCleanupParser();
  3003. return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
  3004. }
  3005. #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
  3006. static int
  3007. expat_xmlattr_setup(struct archive_read *a,
  3008. struct xmlattr_list *list, const XML_Char **atts)
  3009. {
  3010. struct xmlattr *attr;
  3011. char *name, *value;
  3012. list->first = NULL;
  3013. list->last = &(list->first);
  3014. if (atts == NULL)
  3015. return (ARCHIVE_OK);
  3016. while (atts[0] != NULL && atts[1] != NULL) {
  3017. attr = malloc(sizeof*(attr));
  3018. name = strdup(atts[0]);
  3019. value = strdup(atts[1]);
  3020. if (attr == NULL || name == NULL || value == NULL) {
  3021. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  3022. free(attr);
  3023. free(name);
  3024. free(value);
  3025. return (ARCHIVE_FATAL);
  3026. }
  3027. attr->name = name;
  3028. attr->value = value;
  3029. attr->next = NULL;
  3030. *list->last = attr;
  3031. list->last = &(attr->next);
  3032. atts += 2;
  3033. }
  3034. return (ARCHIVE_OK);
  3035. }
  3036. static void
  3037. expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
  3038. {
  3039. struct expat_userData *ud = (struct expat_userData *)userData;
  3040. struct archive_read *a = ud->archive;
  3041. struct xmlattr_list list;
  3042. int r;
  3043. r = expat_xmlattr_setup(a, &list, atts);
  3044. if (r == ARCHIVE_OK)
  3045. r = xml_start(a, (const char *)name, &list);
  3046. xmlattr_cleanup(&list);
  3047. ud->state = r;
  3048. }
  3049. static void
  3050. expat_end_cb(void *userData, const XML_Char *name)
  3051. {
  3052. struct expat_userData *ud = (struct expat_userData *)userData;
  3053. xml_end(ud->archive, (const char *)name);
  3054. }
  3055. static void
  3056. expat_data_cb(void *userData, const XML_Char *s, int len)
  3057. {
  3058. struct expat_userData *ud = (struct expat_userData *)userData;
  3059. xml_data(ud->archive, s, len);
  3060. }
  3061. static int
  3062. expat_read_toc(struct archive_read *a)
  3063. {
  3064. struct xar *xar;
  3065. XML_Parser parser;
  3066. struct expat_userData ud;
  3067. ud.state = ARCHIVE_OK;
  3068. ud.archive = a;
  3069. xar = (struct xar *)(a->format->data);
  3070. /* Initialize XML Parser library. */
  3071. parser = XML_ParserCreate(NULL);
  3072. if (parser == NULL) {
  3073. archive_set_error(&a->archive, ENOMEM,
  3074. "Couldn't allocate memory for xml parser");
  3075. return (ARCHIVE_FATAL);
  3076. }
  3077. XML_SetUserData(parser, &ud);
  3078. XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
  3079. XML_SetCharacterDataHandler(parser, expat_data_cb);
  3080. xar->xmlsts = INIT;
  3081. while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
  3082. enum XML_Status xr;
  3083. const void *d;
  3084. size_t outbytes;
  3085. size_t used;
  3086. int r;
  3087. d = NULL;
  3088. r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
  3089. if (r != ARCHIVE_OK)
  3090. return (r);
  3091. xar->toc_remaining -= used;
  3092. xar->offset += used;
  3093. xar->toc_total += outbytes;
  3094. PRINT_TOC(d, outbytes);
  3095. xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
  3096. __archive_read_consume(a, used);
  3097. if (xr == XML_STATUS_ERROR) {
  3098. XML_ParserFree(parser);
  3099. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3100. "XML Parsing failed");
  3101. return (ARCHIVE_FATAL);
  3102. }
  3103. }
  3104. XML_ParserFree(parser);
  3105. return (ud.state);
  3106. }
  3107. #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
  3108. #endif /* Support xar format */