archive_read_support_format_xar.c 79 KB

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