archive_read_support_format_xar.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287
  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. l = 0;
  954. digit = *p - '0';
  955. while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
  956. l = (l * 10) + digit;
  957. digit = *++p - '0';
  958. }
  959. return (l);
  960. }
  961. static int64_t
  962. atol8(const char *p, size_t char_cnt)
  963. {
  964. int64_t l;
  965. int digit;
  966. l = 0;
  967. while (char_cnt-- > 0) {
  968. if (*p >= '0' && *p <= '7')
  969. digit = *p - '0';
  970. else
  971. break;
  972. p++;
  973. l <<= 3;
  974. l |= digit;
  975. }
  976. return (l);
  977. }
  978. static size_t
  979. atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
  980. {
  981. size_t fbsize = bsize;
  982. while (bsize && psize > 1) {
  983. unsigned char x;
  984. if (p[0] >= 'a' && p[0] <= 'z')
  985. x = (p[0] - 'a' + 0x0a) << 4;
  986. else if (p[0] >= 'A' && p[0] <= 'Z')
  987. x = (p[0] - 'A' + 0x0a) << 4;
  988. else if (p[0] >= '0' && p[0] <= '9')
  989. x = (p[0] - '0') << 4;
  990. else
  991. return (-1);
  992. if (p[1] >= 'a' && p[1] <= 'z')
  993. x |= p[1] - 'a' + 0x0a;
  994. else if (p[1] >= 'A' && p[1] <= 'Z')
  995. x |= p[1] - 'A' + 0x0a;
  996. else if (p[1] >= '0' && p[1] <= '9')
  997. x |= p[1] - '0';
  998. else
  999. return (-1);
  1000. *b++ = x;
  1001. bsize--;
  1002. p += 2;
  1003. psize -= 2;
  1004. }
  1005. return (fbsize - bsize);
  1006. }
  1007. static time_t
  1008. time_from_tm(struct tm *t)
  1009. {
  1010. #if HAVE_TIMEGM
  1011. /* Use platform timegm() if available. */
  1012. return (timegm(t));
  1013. #elif HAVE__MKGMTIME64
  1014. return (_mkgmtime64(t));
  1015. #else
  1016. /* Else use direct calculation using POSIX assumptions. */
  1017. /* First, fix up tm_yday based on the year/month/day. */
  1018. mktime(t);
  1019. /* Then we can compute timegm() from first principles. */
  1020. return (t->tm_sec
  1021. + t->tm_min * 60
  1022. + t->tm_hour * 3600
  1023. + t->tm_yday * 86400
  1024. + (t->tm_year - 70) * 31536000
  1025. + ((t->tm_year - 69) / 4) * 86400
  1026. - ((t->tm_year - 1) / 100) * 86400
  1027. + ((t->tm_year + 299) / 400) * 86400);
  1028. #endif
  1029. }
  1030. static time_t
  1031. parse_time(const char *p, size_t n)
  1032. {
  1033. struct tm tm;
  1034. time_t t = 0;
  1035. int64_t data;
  1036. memset(&tm, 0, sizeof(tm));
  1037. if (n != 20)
  1038. return (t);
  1039. data = atol10(p, 4);
  1040. if (data < 1900)
  1041. return (t);
  1042. tm.tm_year = (int)data - 1900;
  1043. p += 4;
  1044. if (*p++ != '-')
  1045. return (t);
  1046. data = atol10(p, 2);
  1047. if (data < 1 || data > 12)
  1048. return (t);
  1049. tm.tm_mon = (int)data -1;
  1050. p += 2;
  1051. if (*p++ != '-')
  1052. return (t);
  1053. data = atol10(p, 2);
  1054. if (data < 1 || data > 31)
  1055. return (t);
  1056. tm.tm_mday = (int)data;
  1057. p += 2;
  1058. if (*p++ != 'T')
  1059. return (t);
  1060. data = atol10(p, 2);
  1061. if (data < 0 || data > 23)
  1062. return (t);
  1063. tm.tm_hour = (int)data;
  1064. p += 2;
  1065. if (*p++ != ':')
  1066. return (t);
  1067. data = atol10(p, 2);
  1068. if (data < 0 || data > 59)
  1069. return (t);
  1070. tm.tm_min = (int)data;
  1071. p += 2;
  1072. if (*p++ != ':')
  1073. return (t);
  1074. data = atol10(p, 2);
  1075. if (data < 0 || data > 60)
  1076. return (t);
  1077. tm.tm_sec = (int)data;
  1078. #if 0
  1079. p += 2;
  1080. if (*p != 'Z')
  1081. return (t);
  1082. #endif
  1083. t = time_from_tm(&tm);
  1084. return (t);
  1085. }
  1086. static int
  1087. heap_add_entry(struct archive_read *a,
  1088. struct heap_queue *heap, struct xar_file *file)
  1089. {
  1090. uint64_t file_id, parent_id;
  1091. int hole, parent;
  1092. /* Expand our pending files list as necessary. */
  1093. if (heap->used >= heap->allocated) {
  1094. struct xar_file **new_pending_files;
  1095. int new_size = heap->allocated * 2;
  1096. if (heap->allocated < 1024)
  1097. new_size = 1024;
  1098. /* Overflow might keep us from growing the list. */
  1099. if (new_size <= heap->allocated) {
  1100. archive_set_error(&a->archive,
  1101. ENOMEM, "Out of memory");
  1102. return (ARCHIVE_FATAL);
  1103. }
  1104. new_pending_files = (struct xar_file **)
  1105. malloc(new_size * sizeof(new_pending_files[0]));
  1106. if (new_pending_files == NULL) {
  1107. archive_set_error(&a->archive,
  1108. ENOMEM, "Out of memory");
  1109. return (ARCHIVE_FATAL);
  1110. }
  1111. memcpy(new_pending_files, heap->files,
  1112. heap->allocated * sizeof(new_pending_files[0]));
  1113. if (heap->files != NULL)
  1114. free(heap->files);
  1115. heap->files = new_pending_files;
  1116. heap->allocated = new_size;
  1117. }
  1118. file_id = file->id;
  1119. /*
  1120. * Start with hole at end, walk it up tree to find insertion point.
  1121. */
  1122. hole = heap->used++;
  1123. while (hole > 0) {
  1124. parent = (hole - 1)/2;
  1125. parent_id = heap->files[parent]->id;
  1126. if (file_id >= parent_id) {
  1127. heap->files[hole] = file;
  1128. return (ARCHIVE_OK);
  1129. }
  1130. /* Move parent into hole <==> move hole up tree. */
  1131. heap->files[hole] = heap->files[parent];
  1132. hole = parent;
  1133. }
  1134. heap->files[0] = file;
  1135. return (ARCHIVE_OK);
  1136. }
  1137. static struct xar_file *
  1138. heap_get_entry(struct heap_queue *heap)
  1139. {
  1140. uint64_t a_id, b_id, c_id;
  1141. int a, b, c;
  1142. struct xar_file *r, *tmp;
  1143. if (heap->used < 1)
  1144. return (NULL);
  1145. /*
  1146. * The first file in the list is the earliest; we'll return this.
  1147. */
  1148. r = heap->files[0];
  1149. /*
  1150. * Move the last item in the heap to the root of the tree
  1151. */
  1152. heap->files[0] = heap->files[--(heap->used)];
  1153. /*
  1154. * Rebalance the heap.
  1155. */
  1156. a = 0; /* Starting element and its heap key */
  1157. a_id = heap->files[a]->id;
  1158. for (;;) {
  1159. b = a + a + 1; /* First child */
  1160. if (b >= heap->used)
  1161. return (r);
  1162. b_id = heap->files[b]->id;
  1163. c = b + 1; /* Use second child if it is smaller. */
  1164. if (c < heap->used) {
  1165. c_id = heap->files[c]->id;
  1166. if (c_id < b_id) {
  1167. b = c;
  1168. b_id = c_id;
  1169. }
  1170. }
  1171. if (a_id <= b_id)
  1172. return (r);
  1173. tmp = heap->files[a];
  1174. heap->files[a] = heap->files[b];
  1175. heap->files[b] = tmp;
  1176. a = b;
  1177. }
  1178. }
  1179. static int
  1180. add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
  1181. {
  1182. struct hdlink *hdlink;
  1183. for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
  1184. if (hdlink->id == file->link) {
  1185. file->hdnext = hdlink->files;
  1186. hdlink->cnt++;
  1187. hdlink->files = file;
  1188. return (ARCHIVE_OK);
  1189. }
  1190. }
  1191. hdlink = malloc(sizeof(*hdlink));
  1192. if (hdlink == NULL) {
  1193. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1194. return (ARCHIVE_FATAL);
  1195. }
  1196. file->hdnext = NULL;
  1197. hdlink->id = file->link;
  1198. hdlink->cnt = 1;
  1199. hdlink->files = file;
  1200. hdlink->next = xar->hdlink_list;
  1201. xar->hdlink_list = hdlink;
  1202. return (ARCHIVE_OK);
  1203. }
  1204. static void
  1205. _checksum_init(struct chksumwork *sumwrk, int sum_alg)
  1206. {
  1207. sumwrk->alg = sum_alg;
  1208. switch (sum_alg) {
  1209. case CKSUM_NONE:
  1210. break;
  1211. case CKSUM_SHA1:
  1212. archive_sha1_init(&(sumwrk->sha1ctx));
  1213. break;
  1214. case CKSUM_MD5:
  1215. archive_md5_init(&(sumwrk->md5ctx));
  1216. break;
  1217. }
  1218. }
  1219. static void
  1220. _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
  1221. {
  1222. switch (sumwrk->alg) {
  1223. case CKSUM_NONE:
  1224. break;
  1225. case CKSUM_SHA1:
  1226. archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
  1227. break;
  1228. case CKSUM_MD5:
  1229. archive_md5_update(&(sumwrk->md5ctx), buff, size);
  1230. break;
  1231. }
  1232. }
  1233. static int
  1234. _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
  1235. {
  1236. unsigned char sum[MAX_SUM_SIZE];
  1237. int r = ARCHIVE_OK;
  1238. switch (sumwrk->alg) {
  1239. case CKSUM_NONE:
  1240. break;
  1241. case CKSUM_SHA1:
  1242. archive_sha1_final(&(sumwrk->sha1ctx), sum);
  1243. if (len != SHA1_SIZE ||
  1244. memcmp(val, sum, SHA1_SIZE) != 0)
  1245. r = ARCHIVE_FAILED;
  1246. break;
  1247. case CKSUM_MD5:
  1248. archive_md5_final(&(sumwrk->md5ctx), sum);
  1249. if (len != MD5_SIZE ||
  1250. memcmp(val, sum, MD5_SIZE) != 0)
  1251. r = ARCHIVE_FAILED;
  1252. break;
  1253. }
  1254. return (r);
  1255. }
  1256. static void
  1257. checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
  1258. {
  1259. struct xar *xar;
  1260. xar = (struct xar *)(a->format->data);
  1261. _checksum_init(&(xar->a_sumwrk), a_sum_alg);
  1262. _checksum_init(&(xar->e_sumwrk), e_sum_alg);
  1263. }
  1264. static void
  1265. checksum_update(struct archive_read *a, const void *abuff, size_t asize,
  1266. const void *ebuff, size_t esize)
  1267. {
  1268. struct xar *xar;
  1269. xar = (struct xar *)(a->format->data);
  1270. _checksum_update(&(xar->a_sumwrk), abuff, asize);
  1271. _checksum_update(&(xar->e_sumwrk), ebuff, esize);
  1272. }
  1273. static int
  1274. checksum_final(struct archive_read *a, const void *a_sum_val,
  1275. size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
  1276. {
  1277. struct xar *xar;
  1278. int r;
  1279. xar = (struct xar *)(a->format->data);
  1280. r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
  1281. if (r == ARCHIVE_OK)
  1282. r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
  1283. if (r != ARCHIVE_OK)
  1284. archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
  1285. "Sumcheck error");
  1286. return (r);
  1287. }
  1288. static int
  1289. decompression_init(struct archive_read *a, enum enctype encoding)
  1290. {
  1291. struct xar *xar;
  1292. const char *detail;
  1293. int r;
  1294. xar = (struct xar *)(a->format->data);
  1295. xar->rd_encoding = encoding;
  1296. switch (encoding) {
  1297. case NONE:
  1298. break;
  1299. case GZIP:
  1300. if (xar->stream_valid)
  1301. r = inflateReset(&(xar->stream));
  1302. else
  1303. r = inflateInit(&(xar->stream));
  1304. if (r != Z_OK) {
  1305. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1306. "Couldn't initialize zlib stream.");
  1307. return (ARCHIVE_FATAL);
  1308. }
  1309. xar->stream_valid = 1;
  1310. xar->stream.total_in = 0;
  1311. xar->stream.total_out = 0;
  1312. break;
  1313. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1314. case BZIP2:
  1315. if (xar->bzstream_valid) {
  1316. BZ2_bzDecompressEnd(&(xar->bzstream));
  1317. xar->bzstream_valid = 0;
  1318. }
  1319. r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
  1320. if (r == BZ_MEM_ERROR)
  1321. r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
  1322. if (r != BZ_OK) {
  1323. int err = ARCHIVE_ERRNO_MISC;
  1324. detail = NULL;
  1325. switch (r) {
  1326. case BZ_PARAM_ERROR:
  1327. detail = "invalid setup parameter";
  1328. break;
  1329. case BZ_MEM_ERROR:
  1330. err = ENOMEM;
  1331. detail = "out of memory";
  1332. break;
  1333. case BZ_CONFIG_ERROR:
  1334. detail = "mis-compiled library";
  1335. break;
  1336. }
  1337. archive_set_error(&a->archive, err,
  1338. "Internal error initializing decompressor: %s",
  1339. detail == NULL ? "??" : detail);
  1340. xar->bzstream_valid = 0;
  1341. return (ARCHIVE_FATAL);
  1342. }
  1343. xar->bzstream_valid = 1;
  1344. xar->bzstream.total_in_lo32 = 0;
  1345. xar->bzstream.total_in_hi32 = 0;
  1346. xar->bzstream.total_out_lo32 = 0;
  1347. xar->bzstream.total_out_hi32 = 0;
  1348. break;
  1349. #endif
  1350. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1351. #if LZMA_VERSION_MAJOR >= 5
  1352. /* Effectively disable the limiter. */
  1353. #define LZMA_MEMLIMIT UINT64_MAX
  1354. #else
  1355. /* NOTE: This needs to check memory size which running system has. */
  1356. #define LZMA_MEMLIMIT (1U << 30)
  1357. #endif
  1358. case XZ:
  1359. case LZMA:
  1360. if (xar->lzstream_valid) {
  1361. lzma_end(&(xar->lzstream));
  1362. xar->lzstream_valid = 0;
  1363. }
  1364. if (xar->entry_encoding == XZ)
  1365. r = lzma_stream_decoder(&(xar->lzstream),
  1366. LZMA_MEMLIMIT,/* memlimit */
  1367. LZMA_CONCATENATED);
  1368. else
  1369. r = lzma_alone_decoder(&(xar->lzstream),
  1370. LZMA_MEMLIMIT);/* memlimit */
  1371. if (r != LZMA_OK) {
  1372. switch (r) {
  1373. case LZMA_MEM_ERROR:
  1374. archive_set_error(&a->archive,
  1375. ENOMEM,
  1376. "Internal error initializing "
  1377. "compression library: "
  1378. "Cannot allocate memory");
  1379. break;
  1380. case LZMA_OPTIONS_ERROR:
  1381. archive_set_error(&a->archive,
  1382. ARCHIVE_ERRNO_MISC,
  1383. "Internal error initializing "
  1384. "compression library: "
  1385. "Invalid or unsupported options");
  1386. break;
  1387. default:
  1388. archive_set_error(&a->archive,
  1389. ARCHIVE_ERRNO_MISC,
  1390. "Internal error initializing "
  1391. "lzma library");
  1392. break;
  1393. }
  1394. return (ARCHIVE_FATAL);
  1395. }
  1396. xar->lzstream_valid = 1;
  1397. xar->lzstream.total_in = 0;
  1398. xar->lzstream.total_out = 0;
  1399. break;
  1400. #endif
  1401. /*
  1402. * Unsupported compression.
  1403. */
  1404. default:
  1405. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
  1406. case BZIP2:
  1407. #endif
  1408. #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
  1409. case LZMA:
  1410. case XZ:
  1411. #endif
  1412. switch (xar->entry_encoding) {
  1413. case BZIP2: detail = "bzip2"; break;
  1414. case LZMA: detail = "lzma"; break;
  1415. case XZ: detail = "xz"; break;
  1416. default: detail = "??"; break;
  1417. }
  1418. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1419. "%s compression not supported on this platform",
  1420. detail);
  1421. return (ARCHIVE_FAILED);
  1422. }
  1423. return (ARCHIVE_OK);
  1424. }
  1425. static int
  1426. decompress(struct archive_read *a, const void **buff, size_t *outbytes,
  1427. const void *b, size_t *used)
  1428. {
  1429. struct xar *xar;
  1430. void *outbuff;
  1431. size_t avail_in, avail_out;
  1432. int r;
  1433. xar = (struct xar *)(a->format->data);
  1434. avail_in = *used;
  1435. outbuff = (void *)(uintptr_t)*buff;
  1436. if (outbuff == NULL) {
  1437. if (xar->outbuff == NULL) {
  1438. xar->outbuff = malloc(OUTBUFF_SIZE);
  1439. if (xar->outbuff == NULL) {
  1440. archive_set_error(&a->archive, ENOMEM,
  1441. "Couldn't allocate memory for out buffer");
  1442. return (ARCHIVE_FATAL);
  1443. }
  1444. }
  1445. outbuff = xar->outbuff;
  1446. *buff = outbuff;
  1447. avail_out = OUTBUFF_SIZE;
  1448. } else
  1449. avail_out = *outbytes;
  1450. switch (xar->rd_encoding) {
  1451. case GZIP:
  1452. xar->stream.next_in = (Bytef *)(uintptr_t)b;
  1453. xar->stream.avail_in = avail_in;
  1454. xar->stream.next_out = (unsigned char *)outbuff;
  1455. xar->stream.avail_out = avail_out;
  1456. r = inflate(&(xar->stream), 0);
  1457. switch (r) {
  1458. case Z_OK: /* Decompressor made some progress.*/
  1459. case Z_STREAM_END: /* Found end of stream. */
  1460. break;
  1461. default:
  1462. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1463. "File decompression failed (%d)", r);
  1464. return (ARCHIVE_FATAL);
  1465. }
  1466. *used = avail_in - xar->stream.avail_in;
  1467. *outbytes = avail_out - xar->stream.avail_out;
  1468. break;
  1469. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1470. case BZIP2:
  1471. xar->bzstream.next_in = (char *)(uintptr_t)b;
  1472. xar->bzstream.avail_in = avail_in;
  1473. xar->bzstream.next_out = (char *)outbuff;
  1474. xar->bzstream.avail_out = avail_out;
  1475. r = BZ2_bzDecompress(&(xar->bzstream));
  1476. switch (r) {
  1477. case BZ_STREAM_END: /* Found end of stream. */
  1478. switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
  1479. case BZ_OK:
  1480. break;
  1481. default:
  1482. archive_set_error(&(a->archive),
  1483. ARCHIVE_ERRNO_MISC,
  1484. "Failed to clean up decompressor");
  1485. return (ARCHIVE_FATAL);
  1486. }
  1487. xar->bzstream_valid = 0;
  1488. /* FALLTHROUGH */
  1489. case BZ_OK: /* Decompressor made some progress. */
  1490. break;
  1491. default:
  1492. archive_set_error(&(a->archive),
  1493. ARCHIVE_ERRNO_MISC,
  1494. "bzip decompression failed");
  1495. return (ARCHIVE_FATAL);
  1496. }
  1497. *used = avail_in - xar->bzstream.avail_in;
  1498. *outbytes = avail_out - xar->bzstream.avail_out;
  1499. break;
  1500. #endif
  1501. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1502. case LZMA:
  1503. case XZ:
  1504. xar->lzstream.next_in = b;
  1505. xar->lzstream.avail_in = avail_in;
  1506. xar->lzstream.next_out = (unsigned char *)outbuff;
  1507. xar->lzstream.avail_out = avail_out;
  1508. r = lzma_code(&(xar->lzstream), LZMA_RUN);
  1509. switch (r) {
  1510. case LZMA_STREAM_END: /* Found end of stream. */
  1511. lzma_end(&(xar->lzstream));
  1512. xar->lzstream_valid = 0;
  1513. /* FALLTHROUGH */
  1514. case LZMA_OK: /* Decompressor made some progress. */
  1515. break;
  1516. default:
  1517. archive_set_error(&(a->archive),
  1518. ARCHIVE_ERRNO_MISC,
  1519. "%s decompression failed(%d)",
  1520. (xar->entry_encoding == XZ)?"xz":"lzma",
  1521. r);
  1522. return (ARCHIVE_FATAL);
  1523. }
  1524. *used = avail_in - xar->lzstream.avail_in;
  1525. *outbytes = avail_out - xar->lzstream.avail_out;
  1526. break;
  1527. #endif
  1528. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
  1529. case BZIP2:
  1530. #endif
  1531. #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
  1532. case LZMA:
  1533. case XZ:
  1534. #endif
  1535. case NONE:
  1536. default:
  1537. if (outbuff == xar->outbuff) {
  1538. *buff = b;
  1539. *used = avail_in;
  1540. *outbytes = avail_in;
  1541. } else {
  1542. if (avail_out > avail_in)
  1543. avail_out = avail_in;
  1544. memcpy(outbuff, b, avail_out);
  1545. *used = avail_out;
  1546. *outbytes = avail_out;
  1547. }
  1548. break;
  1549. }
  1550. return (ARCHIVE_OK);
  1551. }
  1552. static int
  1553. decompression_cleanup(struct archive_read *a)
  1554. {
  1555. struct xar *xar;
  1556. int r;
  1557. xar = (struct xar *)(a->format->data);
  1558. r = ARCHIVE_OK;
  1559. if (xar->stream_valid) {
  1560. if (inflateEnd(&(xar->stream)) != Z_OK) {
  1561. archive_set_error(&a->archive,
  1562. ARCHIVE_ERRNO_MISC,
  1563. "Failed to clean up zlib decompressor");
  1564. r = ARCHIVE_FATAL;
  1565. }
  1566. }
  1567. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1568. if (xar->bzstream_valid) {
  1569. if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
  1570. archive_set_error(&a->archive,
  1571. ARCHIVE_ERRNO_MISC,
  1572. "Failed to clean up bzip2 decompressor");
  1573. r = ARCHIVE_FATAL;
  1574. }
  1575. }
  1576. #endif
  1577. #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1578. if (xar->lzstream_valid)
  1579. lzma_end(&(xar->lzstream));
  1580. #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
  1581. if (xar->lzstream_valid) {
  1582. if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
  1583. archive_set_error(&a->archive,
  1584. ARCHIVE_ERRNO_MISC,
  1585. "Failed to clean up lzmadec decompressor");
  1586. r = ARCHIVE_FATAL;
  1587. }
  1588. }
  1589. #endif
  1590. return (r);
  1591. }
  1592. static void
  1593. checksum_cleanup(struct archive_read *a) {
  1594. struct xar *xar;
  1595. xar = (struct xar *)(a->format->data);
  1596. _checksum_final(&(xar->a_sumwrk), NULL, 0);
  1597. _checksum_final(&(xar->e_sumwrk), NULL, 0);
  1598. }
  1599. static void
  1600. xmlattr_cleanup(struct xmlattr_list *list)
  1601. {
  1602. struct xmlattr *attr, *next;
  1603. attr = list->first;
  1604. while (attr != NULL) {
  1605. next = attr->next;
  1606. free(attr->name);
  1607. free(attr->value);
  1608. free(attr);
  1609. attr = next;
  1610. }
  1611. list->first = NULL;
  1612. list->last = &(list->first);
  1613. }
  1614. static int
  1615. file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
  1616. {
  1617. struct xar_file *file;
  1618. struct xmlattr *attr;
  1619. file = calloc(1, sizeof(*file));
  1620. if (file == NULL) {
  1621. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1622. return (ARCHIVE_FATAL);
  1623. }
  1624. file->parent = xar->file;
  1625. file->mode = 0777 | AE_IFREG;
  1626. file->atime = time(NULL);
  1627. file->mtime = time(NULL);
  1628. xar->file = file;
  1629. xar->xattr = NULL;
  1630. for (attr = list->first; attr != NULL; attr = attr->next) {
  1631. if (strcmp(attr->name, "id") == 0)
  1632. file->id = atol10(attr->value, strlen(attr->value));
  1633. }
  1634. file->nlink = 1;
  1635. if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
  1636. return (ARCHIVE_FATAL);
  1637. return (ARCHIVE_OK);
  1638. }
  1639. static void
  1640. file_free(struct xar_file *file)
  1641. {
  1642. struct xattr *xattr;
  1643. archive_string_free(&(file->pathname));
  1644. archive_string_free(&(file->symlink));
  1645. archive_string_free(&(file->uname));
  1646. archive_string_free(&(file->gname));
  1647. archive_string_free(&(file->hardlink));
  1648. xattr = file->xattr_list;
  1649. while (xattr != NULL) {
  1650. struct xattr *next;
  1651. next = xattr->next;
  1652. xattr_free(xattr);
  1653. xattr = next;
  1654. }
  1655. free(file);
  1656. }
  1657. static int
  1658. xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
  1659. {
  1660. struct xattr *xattr, **nx;
  1661. struct xmlattr *attr;
  1662. xattr = calloc(1, sizeof(*xattr));
  1663. if (xattr == NULL) {
  1664. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1665. return (ARCHIVE_FATAL);
  1666. }
  1667. xar->xattr = xattr;
  1668. for (attr = list->first; attr != NULL; attr = attr->next) {
  1669. if (strcmp(attr->name, "id") == 0)
  1670. xattr->id = atol10(attr->value, strlen(attr->value));
  1671. }
  1672. /* Chain to xattr list. */
  1673. for (nx = &(xar->file->xattr_list);
  1674. *nx != NULL; nx = &((*nx)->next)) {
  1675. if (xattr->id < (*nx)->id)
  1676. break;
  1677. }
  1678. xattr->next = *nx;
  1679. *nx = xattr;
  1680. return (ARCHIVE_OK);
  1681. }
  1682. static void
  1683. xattr_free(struct xattr *xattr)
  1684. {
  1685. archive_string_free(&(xattr->name));
  1686. free(xattr);
  1687. }
  1688. static int
  1689. getencoding(struct xmlattr_list *list)
  1690. {
  1691. struct xmlattr *attr;
  1692. enum enctype encoding = NONE;
  1693. for (attr = list->first; attr != NULL; attr = attr->next) {
  1694. if (strcmp(attr->name, "style") == 0) {
  1695. if (strcmp(attr->value, "application/octet-stream") == 0)
  1696. encoding = NONE;
  1697. else if (strcmp(attr->value, "application/x-gzip") == 0)
  1698. encoding = GZIP;
  1699. else if (strcmp(attr->value, "application/x-bzip2") == 0)
  1700. encoding = BZIP2;
  1701. else if (strcmp(attr->value, "application/x-lzma") == 0)
  1702. encoding = LZMA;
  1703. else if (strcmp(attr->value, "application/x-xz") == 0)
  1704. encoding = XZ;
  1705. }
  1706. }
  1707. return (encoding);
  1708. }
  1709. static int
  1710. getsumalgorithm(struct xmlattr_list *list)
  1711. {
  1712. struct xmlattr *attr;
  1713. int alg = CKSUM_NONE;
  1714. for (attr = list->first; attr != NULL; attr = attr->next) {
  1715. if (strcmp(attr->name, "style") == 0) {
  1716. const char *v = attr->value;
  1717. if ((v[0] == 'S' || v[0] == 's') &&
  1718. (v[1] == 'H' || v[1] == 'h') &&
  1719. (v[2] == 'A' || v[2] == 'a') &&
  1720. v[3] == '1' && v[4] == '\0')
  1721. alg = CKSUM_SHA1;
  1722. if ((v[0] == 'M' || v[0] == 'm') &&
  1723. (v[1] == 'D' || v[1] == 'd') &&
  1724. v[2] == '5' && v[3] == '\0')
  1725. alg = CKSUM_MD5;
  1726. }
  1727. }
  1728. return (alg);
  1729. }
  1730. static int
  1731. unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
  1732. {
  1733. struct unknown_tag *tag;
  1734. tag = malloc(sizeof(*tag));
  1735. if (tag == NULL) {
  1736. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  1737. return (ARCHIVE_FATAL);
  1738. }
  1739. tag->next = xar->unknowntags;
  1740. archive_string_init(&(tag->name));
  1741. archive_strcpy(&(tag->name), name);
  1742. if (xar->unknowntags == NULL) {
  1743. #if DEBUG
  1744. fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
  1745. #endif
  1746. xar->xmlsts_unknown = xar->xmlsts;
  1747. xar->xmlsts = UNKNOWN;
  1748. }
  1749. xar->unknowntags = tag;
  1750. return (ARCHIVE_OK);
  1751. }
  1752. static void
  1753. unknowntag_end(struct xar *xar, const char *name)
  1754. {
  1755. struct unknown_tag *tag;
  1756. tag = xar->unknowntags;
  1757. if (tag == NULL || name == NULL)
  1758. return;
  1759. if (strcmp(tag->name.s, name) == 0) {
  1760. xar->unknowntags = tag->next;
  1761. archive_string_free(&(tag->name));
  1762. free(tag);
  1763. if (xar->unknowntags == NULL) {
  1764. #if DEBUG
  1765. fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
  1766. #endif
  1767. xar->xmlsts = xar->xmlsts_unknown;
  1768. }
  1769. }
  1770. }
  1771. static int
  1772. xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
  1773. {
  1774. struct xar *xar;
  1775. struct xmlattr *attr;
  1776. xar = (struct xar *)(a->format->data);
  1777. #if DEBUG
  1778. fprintf(stderr, "xml_sta:[%s]\n", name);
  1779. for (attr = list->first; attr != NULL; attr = attr->next)
  1780. fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
  1781. attr->name, attr->value);
  1782. #endif
  1783. xar->base64text = 0;
  1784. switch (xar->xmlsts) {
  1785. case INIT:
  1786. if (strcmp(name, "xar") == 0)
  1787. xar->xmlsts = XAR;
  1788. else
  1789. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1790. return (ARCHIVE_FATAL);
  1791. break;
  1792. case XAR:
  1793. if (strcmp(name, "toc") == 0)
  1794. xar->xmlsts = TOC;
  1795. else
  1796. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1797. return (ARCHIVE_FATAL);
  1798. break;
  1799. case TOC:
  1800. if (strcmp(name, "creation-time") == 0)
  1801. xar->xmlsts = TOC_CREATION_TIME;
  1802. else if (strcmp(name, "checksum") == 0)
  1803. xar->xmlsts = TOC_CHECKSUM;
  1804. else if (strcmp(name, "file") == 0) {
  1805. if (file_new(a, xar, list) != ARCHIVE_OK)
  1806. return (ARCHIVE_FATAL);
  1807. xar->xmlsts = TOC_FILE;
  1808. }
  1809. else
  1810. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1811. return (ARCHIVE_FATAL);
  1812. break;
  1813. case TOC_CHECKSUM:
  1814. if (strcmp(name, "offset") == 0)
  1815. xar->xmlsts = TOC_CHECKSUM_OFFSET;
  1816. else if (strcmp(name, "size") == 0)
  1817. xar->xmlsts = TOC_CHECKSUM_SIZE;
  1818. else
  1819. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1820. return (ARCHIVE_FATAL);
  1821. break;
  1822. case TOC_FILE:
  1823. if (strcmp(name, "file") == 0) {
  1824. if (file_new(a, xar, list) != ARCHIVE_OK)
  1825. return (ARCHIVE_FATAL);
  1826. }
  1827. else if (strcmp(name, "data") == 0)
  1828. xar->xmlsts = FILE_DATA;
  1829. else if (strcmp(name, "ea") == 0) {
  1830. if (xattr_new(a, xar, list) != ARCHIVE_OK)
  1831. return (ARCHIVE_FATAL);
  1832. xar->xmlsts = FILE_EA;
  1833. }
  1834. else if (strcmp(name, "ctime") == 0)
  1835. xar->xmlsts = FILE_CTIME;
  1836. else if (strcmp(name, "mtime") == 0)
  1837. xar->xmlsts = FILE_MTIME;
  1838. else if (strcmp(name, "atime") == 0)
  1839. xar->xmlsts = FILE_ATIME;
  1840. else if (strcmp(name, "group") == 0)
  1841. xar->xmlsts = FILE_GROUP;
  1842. else if (strcmp(name, "gid") == 0)
  1843. xar->xmlsts = FILE_GID;
  1844. else if (strcmp(name, "user") == 0)
  1845. xar->xmlsts = FILE_USER;
  1846. else if (strcmp(name, "uid") == 0)
  1847. xar->xmlsts = FILE_UID;
  1848. else if (strcmp(name, "mode") == 0)
  1849. xar->xmlsts = FILE_MODE;
  1850. else if (strcmp(name, "device") == 0)
  1851. xar->xmlsts = FILE_DEVICE;
  1852. else if (strcmp(name, "deviceno") == 0)
  1853. xar->xmlsts = FILE_DEVICENO;
  1854. else if (strcmp(name, "inode") == 0)
  1855. xar->xmlsts = FILE_INODE;
  1856. else if (strcmp(name, "link") == 0)
  1857. xar->xmlsts = FILE_LINK;
  1858. else if (strcmp(name, "type") == 0) {
  1859. xar->xmlsts = FILE_TYPE;
  1860. for (attr = list->first; attr != NULL;
  1861. attr = attr->next) {
  1862. if (strcmp(attr->name, "link") != 0)
  1863. continue;
  1864. if (strcmp(attr->value, "original") == 0) {
  1865. xar->file->hdnext = xar->hdlink_orgs;
  1866. xar->hdlink_orgs = xar->file;
  1867. } else {
  1868. xar->file->link = (unsigned)atol10(attr->value,
  1869. strlen(attr->value));
  1870. if (xar->file->link > 0)
  1871. if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
  1872. return (ARCHIVE_FATAL);
  1873. };
  1874. }
  1875. }
  1876. }
  1877. else if (strcmp(name, "name") == 0) {
  1878. xar->xmlsts = FILE_NAME;
  1879. for (attr = list->first; attr != NULL;
  1880. attr = attr->next) {
  1881. if (strcmp(attr->name, "enctype") == 0 &&
  1882. strcmp(attr->value, "base64") == 0)
  1883. xar->base64text = 1;
  1884. }
  1885. }
  1886. else if (strcmp(name, "acl") == 0)
  1887. xar->xmlsts = FILE_ACL;
  1888. else if (strcmp(name, "flags") == 0)
  1889. xar->xmlsts = FILE_FLAGS;
  1890. else if (strcmp(name, "ext2") == 0)
  1891. xar->xmlsts = FILE_EXT2;
  1892. else
  1893. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1894. return (ARCHIVE_FATAL);
  1895. break;
  1896. case FILE_DATA:
  1897. if (strcmp(name, "length") == 0)
  1898. xar->xmlsts = FILE_DATA_LENGTH;
  1899. else if (strcmp(name, "offset") == 0)
  1900. xar->xmlsts = FILE_DATA_OFFSET;
  1901. else if (strcmp(name, "size") == 0)
  1902. xar->xmlsts = FILE_DATA_SIZE;
  1903. else if (strcmp(name, "encoding") == 0) {
  1904. xar->xmlsts = FILE_DATA_ENCODING;
  1905. xar->file->encoding = getencoding(list);
  1906. }
  1907. else if (strcmp(name, "archived-checksum") == 0) {
  1908. xar->xmlsts = FILE_DATA_A_CHECKSUM;
  1909. xar->file->a_sum.alg = getsumalgorithm(list);
  1910. }
  1911. else if (strcmp(name, "extracted-checksum") == 0) {
  1912. xar->xmlsts = FILE_DATA_E_CHECKSUM;
  1913. xar->file->e_sum.alg = getsumalgorithm(list);
  1914. }
  1915. else if (strcmp(name, "content") == 0)
  1916. xar->xmlsts = FILE_DATA_CONTENT;
  1917. else
  1918. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1919. return (ARCHIVE_FATAL);
  1920. break;
  1921. case FILE_DEVICE:
  1922. if (strcmp(name, "major") == 0)
  1923. xar->xmlsts = FILE_DEVICE_MAJOR;
  1924. else if (strcmp(name, "minor") == 0)
  1925. xar->xmlsts = FILE_DEVICE_MINOR;
  1926. else
  1927. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1928. return (ARCHIVE_FATAL);
  1929. break;
  1930. case FILE_DATA_CONTENT:
  1931. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1932. return (ARCHIVE_FATAL);
  1933. break;
  1934. case FILE_EA:
  1935. if (strcmp(name, "length") == 0)
  1936. xar->xmlsts = FILE_EA_LENGTH;
  1937. else if (strcmp(name, "offset") == 0)
  1938. xar->xmlsts = FILE_EA_OFFSET;
  1939. else if (strcmp(name, "size") == 0)
  1940. xar->xmlsts = FILE_EA_SIZE;
  1941. else if (strcmp(name, "encoding") == 0) {
  1942. xar->xmlsts = FILE_EA_ENCODING;
  1943. xar->xattr->encoding = getencoding(list);
  1944. } else if (strcmp(name, "archived-checksum") == 0)
  1945. xar->xmlsts = FILE_EA_A_CHECKSUM;
  1946. else if (strcmp(name, "extracted-checksum") == 0)
  1947. xar->xmlsts = FILE_EA_E_CHECKSUM;
  1948. else if (strcmp(name, "name") == 0)
  1949. xar->xmlsts = FILE_EA_NAME;
  1950. else if (strcmp(name, "fstype") == 0)
  1951. xar->xmlsts = FILE_EA_FSTYPE;
  1952. else
  1953. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1954. return (ARCHIVE_FATAL);
  1955. break;
  1956. case FILE_ACL:
  1957. if (strcmp(name, "appleextended") == 0)
  1958. xar->xmlsts = FILE_ACL_APPLEEXTENDED;
  1959. else if (strcmp(name, "default") == 0)
  1960. xar->xmlsts = FILE_ACL_DEFAULT;
  1961. else if (strcmp(name, "access") == 0)
  1962. xar->xmlsts = FILE_ACL_ACCESS;
  1963. else
  1964. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1965. return (ARCHIVE_FATAL);
  1966. break;
  1967. case FILE_FLAGS:
  1968. if (!xml_parse_file_flags(xar, name))
  1969. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1970. return (ARCHIVE_FATAL);
  1971. break;
  1972. case FILE_EXT2:
  1973. if (!xml_parse_file_ext2(xar, name))
  1974. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  1975. return (ARCHIVE_FATAL);
  1976. break;
  1977. case TOC_CREATION_TIME:
  1978. case TOC_CHECKSUM_OFFSET:
  1979. case TOC_CHECKSUM_SIZE:
  1980. case FILE_DATA_LENGTH:
  1981. case FILE_DATA_OFFSET:
  1982. case FILE_DATA_SIZE:
  1983. case FILE_DATA_ENCODING:
  1984. case FILE_DATA_A_CHECKSUM:
  1985. case FILE_DATA_E_CHECKSUM:
  1986. case FILE_EA_LENGTH:
  1987. case FILE_EA_OFFSET:
  1988. case FILE_EA_SIZE:
  1989. case FILE_EA_ENCODING:
  1990. case FILE_EA_A_CHECKSUM:
  1991. case FILE_EA_E_CHECKSUM:
  1992. case FILE_EA_NAME:
  1993. case FILE_EA_FSTYPE:
  1994. case FILE_CTIME:
  1995. case FILE_MTIME:
  1996. case FILE_ATIME:
  1997. case FILE_GROUP:
  1998. case FILE_GID:
  1999. case FILE_USER:
  2000. case FILE_UID:
  2001. case FILE_INODE:
  2002. case FILE_DEVICE_MAJOR:
  2003. case FILE_DEVICE_MINOR:
  2004. case FILE_DEVICENO:
  2005. case FILE_MODE:
  2006. case FILE_TYPE:
  2007. case FILE_LINK:
  2008. case FILE_NAME:
  2009. case FILE_ACL_DEFAULT:
  2010. case FILE_ACL_ACCESS:
  2011. case FILE_ACL_APPLEEXTENDED:
  2012. case FILE_FLAGS_USER_NODUMP:
  2013. case FILE_FLAGS_USER_IMMUTABLE:
  2014. case FILE_FLAGS_USER_APPEND:
  2015. case FILE_FLAGS_USER_OPAQUE:
  2016. case FILE_FLAGS_USER_NOUNLINK:
  2017. case FILE_FLAGS_SYS_ARCHIVED:
  2018. case FILE_FLAGS_SYS_IMMUTABLE:
  2019. case FILE_FLAGS_SYS_APPEND:
  2020. case FILE_FLAGS_SYS_NOUNLINK:
  2021. case FILE_FLAGS_SYS_SNAPSHOT:
  2022. case FILE_EXT2_SecureDeletion:
  2023. case FILE_EXT2_Undelete:
  2024. case FILE_EXT2_Compress:
  2025. case FILE_EXT2_Synchronous:
  2026. case FILE_EXT2_Immutable:
  2027. case FILE_EXT2_AppendOnly:
  2028. case FILE_EXT2_NoDump:
  2029. case FILE_EXT2_NoAtime:
  2030. case FILE_EXT2_CompDirty:
  2031. case FILE_EXT2_CompBlock:
  2032. case FILE_EXT2_NoCompBlock:
  2033. case FILE_EXT2_CompError:
  2034. case FILE_EXT2_BTree:
  2035. case FILE_EXT2_HashIndexed:
  2036. case FILE_EXT2_iMagic:
  2037. case FILE_EXT2_Journaled:
  2038. case FILE_EXT2_NoTail:
  2039. case FILE_EXT2_DirSync:
  2040. case FILE_EXT2_TopDir:
  2041. case FILE_EXT2_Reserved:
  2042. case UNKNOWN:
  2043. if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
  2044. return (ARCHIVE_FATAL);
  2045. break;
  2046. }
  2047. return (ARCHIVE_OK);
  2048. }
  2049. static void
  2050. xml_end(void *userData, const char *name)
  2051. {
  2052. struct archive_read *a;
  2053. struct xar *xar;
  2054. a = (struct archive_read *)userData;
  2055. xar = (struct xar *)(a->format->data);
  2056. #if DEBUG
  2057. fprintf(stderr, "xml_end:[%s]\n", name);
  2058. #endif
  2059. switch (xar->xmlsts) {
  2060. case INIT:
  2061. break;
  2062. case XAR:
  2063. if (strcmp(name, "xar") == 0)
  2064. xar->xmlsts = INIT;
  2065. break;
  2066. case TOC:
  2067. if (strcmp(name, "toc") == 0)
  2068. xar->xmlsts = XAR;
  2069. break;
  2070. case TOC_CREATION_TIME:
  2071. if (strcmp(name, "creation-time") == 0)
  2072. xar->xmlsts = TOC;
  2073. break;
  2074. case TOC_CHECKSUM:
  2075. if (strcmp(name, "checksum") == 0)
  2076. xar->xmlsts = TOC;
  2077. break;
  2078. case TOC_CHECKSUM_OFFSET:
  2079. if (strcmp(name, "offset") == 0)
  2080. xar->xmlsts = TOC_CHECKSUM;
  2081. break;
  2082. case TOC_CHECKSUM_SIZE:
  2083. if (strcmp(name, "size") == 0)
  2084. xar->xmlsts = TOC_CHECKSUM;
  2085. break;
  2086. case TOC_FILE:
  2087. if (strcmp(name, "file") == 0) {
  2088. if (xar->file->parent != NULL &&
  2089. ((xar->file->mode & AE_IFMT) == AE_IFDIR))
  2090. xar->file->parent->subdirs++;
  2091. xar->file = xar->file->parent;
  2092. if (xar->file == NULL)
  2093. xar->xmlsts = TOC;
  2094. }
  2095. break;
  2096. case FILE_DATA:
  2097. if (strcmp(name, "data") == 0)
  2098. xar->xmlsts = TOC_FILE;
  2099. break;
  2100. case FILE_DATA_LENGTH:
  2101. if (strcmp(name, "length") == 0)
  2102. xar->xmlsts = FILE_DATA;
  2103. break;
  2104. case FILE_DATA_OFFSET:
  2105. if (strcmp(name, "offset") == 0)
  2106. xar->xmlsts = FILE_DATA;
  2107. break;
  2108. case FILE_DATA_SIZE:
  2109. if (strcmp(name, "size") == 0)
  2110. xar->xmlsts = FILE_DATA;
  2111. break;
  2112. case FILE_DATA_ENCODING:
  2113. if (strcmp(name, "encoding") == 0)
  2114. xar->xmlsts = FILE_DATA;
  2115. break;
  2116. case FILE_DATA_A_CHECKSUM:
  2117. if (strcmp(name, "archived-checksum") == 0)
  2118. xar->xmlsts = FILE_DATA;
  2119. break;
  2120. case FILE_DATA_E_CHECKSUM:
  2121. if (strcmp(name, "extracted-checksum") == 0)
  2122. xar->xmlsts = FILE_DATA;
  2123. break;
  2124. case FILE_DATA_CONTENT:
  2125. if (strcmp(name, "content") == 0)
  2126. xar->xmlsts = FILE_DATA;
  2127. break;
  2128. case FILE_EA:
  2129. if (strcmp(name, "ea") == 0) {
  2130. xar->xmlsts = TOC_FILE;
  2131. xar->xattr = NULL;
  2132. }
  2133. break;
  2134. case FILE_EA_LENGTH:
  2135. if (strcmp(name, "length") == 0)
  2136. xar->xmlsts = FILE_EA;
  2137. break;
  2138. case FILE_EA_OFFSET:
  2139. if (strcmp(name, "offset") == 0)
  2140. xar->xmlsts = FILE_EA;
  2141. break;
  2142. case FILE_EA_SIZE:
  2143. if (strcmp(name, "size") == 0)
  2144. xar->xmlsts = FILE_EA;
  2145. break;
  2146. case FILE_EA_ENCODING:
  2147. if (strcmp(name, "encoding") == 0)
  2148. xar->xmlsts = FILE_EA;
  2149. break;
  2150. case FILE_EA_A_CHECKSUM:
  2151. if (strcmp(name, "archived-checksum") == 0)
  2152. xar->xmlsts = FILE_EA;
  2153. break;
  2154. case FILE_EA_E_CHECKSUM:
  2155. if (strcmp(name, "extracted-checksum") == 0)
  2156. xar->xmlsts = FILE_EA;
  2157. break;
  2158. case FILE_EA_NAME:
  2159. if (strcmp(name, "name") == 0)
  2160. xar->xmlsts = FILE_EA;
  2161. break;
  2162. case FILE_EA_FSTYPE:
  2163. if (strcmp(name, "fstype") == 0)
  2164. xar->xmlsts = FILE_EA;
  2165. break;
  2166. case FILE_CTIME:
  2167. if (strcmp(name, "ctime") == 0)
  2168. xar->xmlsts = TOC_FILE;
  2169. break;
  2170. case FILE_MTIME:
  2171. if (strcmp(name, "mtime") == 0)
  2172. xar->xmlsts = TOC_FILE;
  2173. break;
  2174. case FILE_ATIME:
  2175. if (strcmp(name, "atime") == 0)
  2176. xar->xmlsts = TOC_FILE;
  2177. break;
  2178. case FILE_GROUP:
  2179. if (strcmp(name, "group") == 0)
  2180. xar->xmlsts = TOC_FILE;
  2181. break;
  2182. case FILE_GID:
  2183. if (strcmp(name, "gid") == 0)
  2184. xar->xmlsts = TOC_FILE;
  2185. break;
  2186. case FILE_USER:
  2187. if (strcmp(name, "user") == 0)
  2188. xar->xmlsts = TOC_FILE;
  2189. break;
  2190. case FILE_UID:
  2191. if (strcmp(name, "uid") == 0)
  2192. xar->xmlsts = TOC_FILE;
  2193. break;
  2194. case FILE_MODE:
  2195. if (strcmp(name, "mode") == 0)
  2196. xar->xmlsts = TOC_FILE;
  2197. break;
  2198. case FILE_DEVICE:
  2199. if (strcmp(name, "device") == 0)
  2200. xar->xmlsts = TOC_FILE;
  2201. break;
  2202. case FILE_DEVICE_MAJOR:
  2203. if (strcmp(name, "major") == 0)
  2204. xar->xmlsts = FILE_DEVICE;
  2205. break;
  2206. case FILE_DEVICE_MINOR:
  2207. if (strcmp(name, "minor") == 0)
  2208. xar->xmlsts = FILE_DEVICE;
  2209. break;
  2210. case FILE_DEVICENO:
  2211. if (strcmp(name, "deviceno") == 0)
  2212. xar->xmlsts = TOC_FILE;
  2213. break;
  2214. case FILE_INODE:
  2215. if (strcmp(name, "inode") == 0)
  2216. xar->xmlsts = TOC_FILE;
  2217. break;
  2218. case FILE_LINK:
  2219. if (strcmp(name, "link") == 0)
  2220. xar->xmlsts = TOC_FILE;
  2221. break;
  2222. case FILE_TYPE:
  2223. if (strcmp(name, "type") == 0)
  2224. xar->xmlsts = TOC_FILE;
  2225. break;
  2226. case FILE_NAME:
  2227. if (strcmp(name, "name") == 0)
  2228. xar->xmlsts = TOC_FILE;
  2229. break;
  2230. case FILE_ACL:
  2231. if (strcmp(name, "acl") == 0)
  2232. xar->xmlsts = TOC_FILE;
  2233. break;
  2234. case FILE_ACL_DEFAULT:
  2235. if (strcmp(name, "default") == 0)
  2236. xar->xmlsts = FILE_ACL;
  2237. break;
  2238. case FILE_ACL_ACCESS:
  2239. if (strcmp(name, "access") == 0)
  2240. xar->xmlsts = FILE_ACL;
  2241. break;
  2242. case FILE_ACL_APPLEEXTENDED:
  2243. if (strcmp(name, "appleextended") == 0)
  2244. xar->xmlsts = FILE_ACL;
  2245. break;
  2246. case FILE_FLAGS:
  2247. if (strcmp(name, "flags") == 0)
  2248. xar->xmlsts = TOC_FILE;
  2249. break;
  2250. case FILE_FLAGS_USER_NODUMP:
  2251. if (strcmp(name, "UserNoDump") == 0)
  2252. xar->xmlsts = FILE_FLAGS;
  2253. break;
  2254. case FILE_FLAGS_USER_IMMUTABLE:
  2255. if (strcmp(name, "UserImmutable") == 0)
  2256. xar->xmlsts = FILE_FLAGS;
  2257. break;
  2258. case FILE_FLAGS_USER_APPEND:
  2259. if (strcmp(name, "UserAppend") == 0)
  2260. xar->xmlsts = FILE_FLAGS;
  2261. break;
  2262. case FILE_FLAGS_USER_OPAQUE:
  2263. if (strcmp(name, "UserOpaque") == 0)
  2264. xar->xmlsts = FILE_FLAGS;
  2265. break;
  2266. case FILE_FLAGS_USER_NOUNLINK:
  2267. if (strcmp(name, "UserNoUnlink") == 0)
  2268. xar->xmlsts = FILE_FLAGS;
  2269. break;
  2270. case FILE_FLAGS_SYS_ARCHIVED:
  2271. if (strcmp(name, "SystemArchived") == 0)
  2272. xar->xmlsts = FILE_FLAGS;
  2273. break;
  2274. case FILE_FLAGS_SYS_IMMUTABLE:
  2275. if (strcmp(name, "SystemImmutable") == 0)
  2276. xar->xmlsts = FILE_FLAGS;
  2277. break;
  2278. case FILE_FLAGS_SYS_APPEND:
  2279. if (strcmp(name, "SystemAppend") == 0)
  2280. xar->xmlsts = FILE_FLAGS;
  2281. break;
  2282. case FILE_FLAGS_SYS_NOUNLINK:
  2283. if (strcmp(name, "SystemNoUnlink") == 0)
  2284. xar->xmlsts = FILE_FLAGS;
  2285. break;
  2286. case FILE_FLAGS_SYS_SNAPSHOT:
  2287. if (strcmp(name, "SystemSnapshot") == 0)
  2288. xar->xmlsts = FILE_FLAGS;
  2289. break;
  2290. case FILE_EXT2:
  2291. if (strcmp(name, "ext2") == 0)
  2292. xar->xmlsts = TOC_FILE;
  2293. break;
  2294. case FILE_EXT2_SecureDeletion:
  2295. if (strcmp(name, "SecureDeletion") == 0)
  2296. xar->xmlsts = FILE_EXT2;
  2297. break;
  2298. case FILE_EXT2_Undelete:
  2299. if (strcmp(name, "Undelete") == 0)
  2300. xar->xmlsts = FILE_EXT2;
  2301. break;
  2302. case FILE_EXT2_Compress:
  2303. if (strcmp(name, "Compress") == 0)
  2304. xar->xmlsts = FILE_EXT2;
  2305. break;
  2306. case FILE_EXT2_Synchronous:
  2307. if (strcmp(name, "Synchronous") == 0)
  2308. xar->xmlsts = FILE_EXT2;
  2309. break;
  2310. case FILE_EXT2_Immutable:
  2311. if (strcmp(name, "Immutable") == 0)
  2312. xar->xmlsts = FILE_EXT2;
  2313. break;
  2314. case FILE_EXT2_AppendOnly:
  2315. if (strcmp(name, "AppendOnly") == 0)
  2316. xar->xmlsts = FILE_EXT2;
  2317. break;
  2318. case FILE_EXT2_NoDump:
  2319. if (strcmp(name, "NoDump") == 0)
  2320. xar->xmlsts = FILE_EXT2;
  2321. break;
  2322. case FILE_EXT2_NoAtime:
  2323. if (strcmp(name, "NoAtime") == 0)
  2324. xar->xmlsts = FILE_EXT2;
  2325. break;
  2326. case FILE_EXT2_CompDirty:
  2327. if (strcmp(name, "CompDirty") == 0)
  2328. xar->xmlsts = FILE_EXT2;
  2329. break;
  2330. case FILE_EXT2_CompBlock:
  2331. if (strcmp(name, "CompBlock") == 0)
  2332. xar->xmlsts = FILE_EXT2;
  2333. break;
  2334. case FILE_EXT2_NoCompBlock:
  2335. if (strcmp(name, "NoCompBlock") == 0)
  2336. xar->xmlsts = FILE_EXT2;
  2337. break;
  2338. case FILE_EXT2_CompError:
  2339. if (strcmp(name, "CompError") == 0)
  2340. xar->xmlsts = FILE_EXT2;
  2341. break;
  2342. case FILE_EXT2_BTree:
  2343. if (strcmp(name, "BTree") == 0)
  2344. xar->xmlsts = FILE_EXT2;
  2345. break;
  2346. case FILE_EXT2_HashIndexed:
  2347. if (strcmp(name, "HashIndexed") == 0)
  2348. xar->xmlsts = FILE_EXT2;
  2349. break;
  2350. case FILE_EXT2_iMagic:
  2351. if (strcmp(name, "iMagic") == 0)
  2352. xar->xmlsts = FILE_EXT2;
  2353. break;
  2354. case FILE_EXT2_Journaled:
  2355. if (strcmp(name, "Journaled") == 0)
  2356. xar->xmlsts = FILE_EXT2;
  2357. break;
  2358. case FILE_EXT2_NoTail:
  2359. if (strcmp(name, "NoTail") == 0)
  2360. xar->xmlsts = FILE_EXT2;
  2361. break;
  2362. case FILE_EXT2_DirSync:
  2363. if (strcmp(name, "DirSync") == 0)
  2364. xar->xmlsts = FILE_EXT2;
  2365. break;
  2366. case FILE_EXT2_TopDir:
  2367. if (strcmp(name, "TopDir") == 0)
  2368. xar->xmlsts = FILE_EXT2;
  2369. break;
  2370. case FILE_EXT2_Reserved:
  2371. if (strcmp(name, "Reserved") == 0)
  2372. xar->xmlsts = FILE_EXT2;
  2373. break;
  2374. case UNKNOWN:
  2375. unknowntag_end(xar, name);
  2376. break;
  2377. }
  2378. }
  2379. static const int base64[256] = {
  2380. -1, -1, -1, -1, -1, -1, -1, -1,
  2381. -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
  2382. -1, -1, -1, -1, -1, -1, -1, -1,
  2383. -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
  2384. -1, -1, -1, -1, -1, -1, -1, -1,
  2385. -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
  2386. 52, 53, 54, 55, 56, 57, 58, 59,
  2387. 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
  2388. -1, 0, 1, 2, 3, 4, 5, 6,
  2389. 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
  2390. 15, 16, 17, 18, 19, 20, 21, 22,
  2391. 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
  2392. -1, 26, 27, 28, 29, 30, 31, 32,
  2393. 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
  2394. 41, 42, 43, 44, 45, 46, 47, 48,
  2395. 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
  2396. -1, -1, -1, -1, -1, -1, -1, -1,
  2397. -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
  2398. -1, -1, -1, -1, -1, -1, -1, -1,
  2399. -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
  2400. -1, -1, -1, -1, -1, -1, -1, -1,
  2401. -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
  2402. -1, -1, -1, -1, -1, -1, -1, -1,
  2403. -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
  2404. -1, -1, -1, -1, -1, -1, -1, -1,
  2405. -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
  2406. -1, -1, -1, -1, -1, -1, -1, -1,
  2407. -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
  2408. -1, -1, -1, -1, -1, -1, -1, -1,
  2409. -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
  2410. -1, -1, -1, -1, -1, -1, -1, -1,
  2411. -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
  2412. };
  2413. static void
  2414. strappend_base64(struct xar *xar,
  2415. struct archive_string *as, const char *s, size_t l)
  2416. {
  2417. unsigned char buff[256];
  2418. unsigned char *out;
  2419. const unsigned char *b;
  2420. size_t len;
  2421. (void)xar; /* UNUSED */
  2422. len = 0;
  2423. out = buff;
  2424. b = (const unsigned char *)s;
  2425. while (l > 0) {
  2426. int n = 0;
  2427. if (l > 0) {
  2428. if (base64[b[0]] < 0 || base64[b[1]] < 0)
  2429. break;
  2430. n = base64[*b++] << 18;
  2431. n |= base64[*b++] << 12;
  2432. *out++ = n >> 16;
  2433. len++;
  2434. l -= 2;
  2435. }
  2436. if (l > 0) {
  2437. if (base64[*b] < 0)
  2438. break;
  2439. n |= base64[*b++] << 6;
  2440. *out++ = (n >> 8) & 0xFF;
  2441. len++;
  2442. --l;
  2443. }
  2444. if (l > 0) {
  2445. if (base64[*b] < 0)
  2446. break;
  2447. n |= base64[*b++];
  2448. *out++ = n & 0xFF;
  2449. len++;
  2450. --l;
  2451. }
  2452. if (len+3 >= sizeof(buff)) {
  2453. archive_strncat(as, (const char *)buff, len);
  2454. len = 0;
  2455. out = buff;
  2456. }
  2457. }
  2458. if (len > 0)
  2459. archive_strncat(as, (const char *)buff, len);
  2460. }
  2461. static void
  2462. xml_data(void *userData, const char *s, int len)
  2463. {
  2464. struct archive_read *a;
  2465. struct xar *xar;
  2466. a = (struct archive_read *)userData;
  2467. xar = (struct xar *)(a->format->data);
  2468. #if DEBUG
  2469. {
  2470. char buff[1024];
  2471. if (len > (int)(sizeof(buff)-1))
  2472. len = (int)(sizeof(buff)-1);
  2473. strncpy(buff, s, len);
  2474. buff[len] = 0;
  2475. fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
  2476. }
  2477. #endif
  2478. switch (xar->xmlsts) {
  2479. case TOC_CHECKSUM_OFFSET:
  2480. xar->toc_chksum_offset = atol10(s, len);
  2481. break;
  2482. case TOC_CHECKSUM_SIZE:
  2483. xar->toc_chksum_size = atol10(s, len);
  2484. break;
  2485. default:
  2486. break;
  2487. }
  2488. if (xar->file == NULL)
  2489. return;
  2490. switch (xar->xmlsts) {
  2491. case FILE_NAME:
  2492. if (xar->file->parent != NULL) {
  2493. archive_string_concat(&(xar->file->pathname),
  2494. &(xar->file->parent->pathname));
  2495. archive_strappend_char(&(xar->file->pathname), '/');
  2496. }
  2497. xar->file->has |= HAS_PATHNAME;
  2498. if (xar->base64text) {
  2499. strappend_base64(xar,
  2500. &(xar->file->pathname), s, len);
  2501. } else
  2502. archive_strncat(&(xar->file->pathname), s, len);
  2503. break;
  2504. case FILE_LINK:
  2505. xar->file->has |= HAS_SYMLINK;
  2506. archive_strncpy(&(xar->file->symlink), s, len);
  2507. break;
  2508. case FILE_TYPE:
  2509. if (strncmp("file", s, len) == 0 ||
  2510. strncmp("hardlink", s, len) == 0)
  2511. xar->file->mode =
  2512. (xar->file->mode & ~AE_IFMT) | AE_IFREG;
  2513. if (strncmp("directory", s, len) == 0)
  2514. xar->file->mode =
  2515. (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
  2516. if (strncmp("symlink", s, len) == 0)
  2517. xar->file->mode =
  2518. (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
  2519. if (strncmp("character special", s, len) == 0)
  2520. xar->file->mode =
  2521. (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
  2522. if (strncmp("block special", s, len) == 0)
  2523. xar->file->mode =
  2524. (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
  2525. if (strncmp("socket", s, len) == 0)
  2526. xar->file->mode =
  2527. (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
  2528. if (strncmp("fifo", s, len) == 0)
  2529. xar->file->mode =
  2530. (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
  2531. xar->file->has |= HAS_TYPE;
  2532. break;
  2533. case FILE_INODE:
  2534. xar->file->has |= HAS_INO;
  2535. xar->file->ino64 = atol10(s, len);
  2536. break;
  2537. case FILE_DEVICE_MAJOR:
  2538. xar->file->has |= HAS_DEVMAJOR;
  2539. xar->file->devmajor = (dev_t)atol10(s, len);
  2540. break;
  2541. case FILE_DEVICE_MINOR:
  2542. xar->file->has |= HAS_DEVMINOR;
  2543. xar->file->devminor = (dev_t)atol10(s, len);
  2544. break;
  2545. case FILE_DEVICENO:
  2546. xar->file->has |= HAS_DEV;
  2547. xar->file->dev = (dev_t)atol10(s, len);
  2548. break;
  2549. case FILE_MODE:
  2550. xar->file->has |= HAS_MODE;
  2551. xar->file->mode =
  2552. (xar->file->mode & AE_IFMT) |
  2553. ((mode_t)(atol8(s, len)) & ~AE_IFMT);
  2554. break;
  2555. case FILE_GROUP:
  2556. xar->file->has |= HAS_GID;
  2557. archive_strncpy(&(xar->file->gname), s, len);
  2558. break;
  2559. case FILE_GID:
  2560. xar->file->has |= HAS_GID;
  2561. xar->file->gid = atol10(s, len);
  2562. break;
  2563. case FILE_USER:
  2564. xar->file->has |= HAS_UID;
  2565. archive_strncpy(&(xar->file->uname), s, len);
  2566. break;
  2567. case FILE_UID:
  2568. xar->file->has |= HAS_UID;
  2569. xar->file->uid = atol10(s, len);
  2570. break;
  2571. case FILE_CTIME:
  2572. xar->file->has |= HAS_TIME;
  2573. xar->file->ctime = parse_time(s, len);
  2574. break;
  2575. case FILE_MTIME:
  2576. xar->file->has |= HAS_TIME;
  2577. xar->file->mtime = parse_time(s, len);
  2578. break;
  2579. case FILE_ATIME:
  2580. xar->file->has |= HAS_TIME;
  2581. xar->file->atime = parse_time(s, len);
  2582. break;
  2583. case FILE_DATA_LENGTH:
  2584. xar->file->has |= HAS_DATA;
  2585. xar->file->length = atol10(s, len);
  2586. break;
  2587. case FILE_DATA_OFFSET:
  2588. xar->file->has |= HAS_DATA;
  2589. xar->file->offset = atol10(s, len);
  2590. break;
  2591. case FILE_DATA_SIZE:
  2592. xar->file->has |= HAS_DATA;
  2593. xar->file->size = atol10(s, len);
  2594. break;
  2595. case FILE_DATA_A_CHECKSUM:
  2596. xar->file->a_sum.len = atohex(xar->file->a_sum.val,
  2597. sizeof(xar->file->a_sum.val), s, len);
  2598. break;
  2599. case FILE_DATA_E_CHECKSUM:
  2600. xar->file->e_sum.len = atohex(xar->file->e_sum.val,
  2601. sizeof(xar->file->e_sum.val), s, len);
  2602. break;
  2603. case FILE_EA_LENGTH:
  2604. xar->file->has |= HAS_XATTR;
  2605. xar->xattr->length = atol10(s, len);
  2606. break;
  2607. case FILE_EA_OFFSET:
  2608. xar->file->has |= HAS_XATTR;
  2609. xar->xattr->offset = atol10(s, len);
  2610. break;
  2611. case FILE_EA_SIZE:
  2612. xar->file->has |= HAS_XATTR;
  2613. xar->xattr->size = atol10(s, len);
  2614. break;
  2615. case FILE_EA_A_CHECKSUM:
  2616. xar->file->has |= HAS_XATTR;
  2617. xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
  2618. sizeof(xar->xattr->a_sum.val), s, len);
  2619. break;
  2620. case FILE_EA_E_CHECKSUM:
  2621. xar->file->has |= HAS_XATTR;
  2622. xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
  2623. sizeof(xar->xattr->e_sum.val), s, len);
  2624. break;
  2625. case FILE_EA_NAME:
  2626. xar->file->has |= HAS_XATTR;
  2627. archive_strncpy(&(xar->xattr->name), s, len);
  2628. break;
  2629. case FILE_EA_FSTYPE:
  2630. xar->file->has |= HAS_XATTR;
  2631. archive_strncpy(&(xar->xattr->fstype), s, len);
  2632. break;
  2633. break;
  2634. case FILE_ACL_DEFAULT:
  2635. case FILE_ACL_ACCESS:
  2636. case FILE_ACL_APPLEEXTENDED:
  2637. xar->file->has |= HAS_ACL;
  2638. /* TODO */
  2639. break;
  2640. case INIT:
  2641. case XAR:
  2642. case TOC:
  2643. case TOC_CREATION_TIME:
  2644. case TOC_CHECKSUM:
  2645. case TOC_CHECKSUM_OFFSET:
  2646. case TOC_CHECKSUM_SIZE:
  2647. case TOC_FILE:
  2648. case FILE_DATA:
  2649. case FILE_DATA_ENCODING:
  2650. case FILE_DATA_CONTENT:
  2651. case FILE_DEVICE:
  2652. case FILE_EA:
  2653. case FILE_EA_ENCODING:
  2654. case FILE_ACL:
  2655. case FILE_FLAGS:
  2656. case FILE_FLAGS_USER_NODUMP:
  2657. case FILE_FLAGS_USER_IMMUTABLE:
  2658. case FILE_FLAGS_USER_APPEND:
  2659. case FILE_FLAGS_USER_OPAQUE:
  2660. case FILE_FLAGS_USER_NOUNLINK:
  2661. case FILE_FLAGS_SYS_ARCHIVED:
  2662. case FILE_FLAGS_SYS_IMMUTABLE:
  2663. case FILE_FLAGS_SYS_APPEND:
  2664. case FILE_FLAGS_SYS_NOUNLINK:
  2665. case FILE_FLAGS_SYS_SNAPSHOT:
  2666. case FILE_EXT2:
  2667. case FILE_EXT2_SecureDeletion:
  2668. case FILE_EXT2_Undelete:
  2669. case FILE_EXT2_Compress:
  2670. case FILE_EXT2_Synchronous:
  2671. case FILE_EXT2_Immutable:
  2672. case FILE_EXT2_AppendOnly:
  2673. case FILE_EXT2_NoDump:
  2674. case FILE_EXT2_NoAtime:
  2675. case FILE_EXT2_CompDirty:
  2676. case FILE_EXT2_CompBlock:
  2677. case FILE_EXT2_NoCompBlock:
  2678. case FILE_EXT2_CompError:
  2679. case FILE_EXT2_BTree:
  2680. case FILE_EXT2_HashIndexed:
  2681. case FILE_EXT2_iMagic:
  2682. case FILE_EXT2_Journaled:
  2683. case FILE_EXT2_NoTail:
  2684. case FILE_EXT2_DirSync:
  2685. case FILE_EXT2_TopDir:
  2686. case FILE_EXT2_Reserved:
  2687. case UNKNOWN:
  2688. break;
  2689. }
  2690. }
  2691. /*
  2692. * BSD file flags.
  2693. */
  2694. static int
  2695. xml_parse_file_flags(struct xar *xar, const char *name)
  2696. {
  2697. const char *flag = NULL;
  2698. if (strcmp(name, "UserNoDump") == 0) {
  2699. xar->xmlsts = FILE_FLAGS_USER_NODUMP;
  2700. flag = "nodump";
  2701. }
  2702. else if (strcmp(name, "UserImmutable") == 0) {
  2703. xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
  2704. flag = "uimmutable";
  2705. }
  2706. else if (strcmp(name, "UserAppend") == 0) {
  2707. xar->xmlsts = FILE_FLAGS_USER_APPEND;
  2708. flag = "uappend";
  2709. }
  2710. else if (strcmp(name, "UserOpaque") == 0) {
  2711. xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
  2712. flag = "opaque";
  2713. }
  2714. else if (strcmp(name, "UserNoUnlink") == 0) {
  2715. xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
  2716. flag = "nouunlink";
  2717. }
  2718. else if (strcmp(name, "SystemArchived") == 0) {
  2719. xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
  2720. flag = "archived";
  2721. }
  2722. else if (strcmp(name, "SystemImmutable") == 0) {
  2723. xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
  2724. flag = "simmutable";
  2725. }
  2726. else if (strcmp(name, "SystemAppend") == 0) {
  2727. xar->xmlsts = FILE_FLAGS_SYS_APPEND;
  2728. flag = "sappend";
  2729. }
  2730. else if (strcmp(name, "SystemNoUnlink") == 0) {
  2731. xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
  2732. flag = "nosunlink";
  2733. }
  2734. else if (strcmp(name, "SystemSnapshot") == 0) {
  2735. xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
  2736. flag = "snapshot";
  2737. }
  2738. if (flag == NULL)
  2739. return (0);
  2740. xar->file->has |= HAS_FFLAGS;
  2741. if (archive_strlen(&(xar->file->fflags_text)) > 0)
  2742. archive_strappend_char(&(xar->file->fflags_text), ',');
  2743. archive_strcat(&(xar->file->fflags_text), flag);
  2744. return (1);
  2745. }
  2746. /*
  2747. * Linux file flags.
  2748. */
  2749. static int
  2750. xml_parse_file_ext2(struct xar *xar, const char *name)
  2751. {
  2752. const char *flag = NULL;
  2753. if (strcmp(name, "SecureDeletion") == 0) {
  2754. xar->xmlsts = FILE_EXT2_SecureDeletion;
  2755. flag = "securedeletion";
  2756. }
  2757. else if (strcmp(name, "Undelete") == 0) {
  2758. xar->xmlsts = FILE_EXT2_Undelete;
  2759. flag = "nouunlink";
  2760. }
  2761. else if (strcmp(name, "Compress") == 0) {
  2762. xar->xmlsts = FILE_EXT2_Compress;
  2763. flag = "compress";
  2764. }
  2765. else if (strcmp(name, "Synchronous") == 0) {
  2766. xar->xmlsts = FILE_EXT2_Synchronous;
  2767. flag = "sync";
  2768. }
  2769. else if (strcmp(name, "Immutable") == 0) {
  2770. xar->xmlsts = FILE_EXT2_Immutable;
  2771. flag = "simmutable";
  2772. }
  2773. else if (strcmp(name, "AppendOnly") == 0) {
  2774. xar->xmlsts = FILE_EXT2_AppendOnly;
  2775. flag = "sappend";
  2776. }
  2777. else if (strcmp(name, "NoDump") == 0) {
  2778. xar->xmlsts = FILE_EXT2_NoDump;
  2779. flag = "nodump";
  2780. }
  2781. else if (strcmp(name, "NoAtime") == 0) {
  2782. xar->xmlsts = FILE_EXT2_NoAtime;
  2783. flag = "noatime";
  2784. }
  2785. else if (strcmp(name, "CompDirty") == 0) {
  2786. xar->xmlsts = FILE_EXT2_CompDirty;
  2787. flag = "compdirty";
  2788. }
  2789. else if (strcmp(name, "CompBlock") == 0) {
  2790. xar->xmlsts = FILE_EXT2_CompBlock;
  2791. flag = "comprblk";
  2792. }
  2793. else if (strcmp(name, "NoCompBlock") == 0) {
  2794. xar->xmlsts = FILE_EXT2_NoCompBlock;
  2795. flag = "nocomprblk";
  2796. }
  2797. else if (strcmp(name, "CompError") == 0) {
  2798. xar->xmlsts = FILE_EXT2_CompError;
  2799. flag = "comperr";
  2800. }
  2801. else if (strcmp(name, "BTree") == 0) {
  2802. xar->xmlsts = FILE_EXT2_BTree;
  2803. flag = "btree";
  2804. }
  2805. else if (strcmp(name, "HashIndexed") == 0) {
  2806. xar->xmlsts = FILE_EXT2_HashIndexed;
  2807. flag = "hashidx";
  2808. }
  2809. else if (strcmp(name, "iMagic") == 0) {
  2810. xar->xmlsts = FILE_EXT2_iMagic;
  2811. flag = "imagic";
  2812. }
  2813. else if (strcmp(name, "Journaled") == 0) {
  2814. xar->xmlsts = FILE_EXT2_Journaled;
  2815. flag = "journal";
  2816. }
  2817. else if (strcmp(name, "NoTail") == 0) {
  2818. xar->xmlsts = FILE_EXT2_NoTail;
  2819. flag = "notail";
  2820. }
  2821. else if (strcmp(name, "DirSync") == 0) {
  2822. xar->xmlsts = FILE_EXT2_DirSync;
  2823. flag = "dirsync";
  2824. }
  2825. else if (strcmp(name, "TopDir") == 0) {
  2826. xar->xmlsts = FILE_EXT2_TopDir;
  2827. flag = "topdir";
  2828. }
  2829. else if (strcmp(name, "Reserved") == 0) {
  2830. xar->xmlsts = FILE_EXT2_Reserved;
  2831. flag = "reserved";
  2832. }
  2833. if (flag == NULL)
  2834. return (0);
  2835. if (archive_strlen(&(xar->file->fflags_text)) > 0)
  2836. archive_strappend_char(&(xar->file->fflags_text), ',');
  2837. archive_strcat(&(xar->file->fflags_text), flag);
  2838. return (1);
  2839. }
  2840. #ifdef HAVE_LIBXML_XMLREADER_H
  2841. static int
  2842. xml2_xmlattr_setup(struct archive_read *a,
  2843. struct xmlattr_list *list, xmlTextReaderPtr reader)
  2844. {
  2845. struct xmlattr *attr;
  2846. int r;
  2847. list->first = NULL;
  2848. list->last = &(list->first);
  2849. r = xmlTextReaderMoveToFirstAttribute(reader);
  2850. while (r == 1) {
  2851. attr = malloc(sizeof*(attr));
  2852. if (attr == NULL) {
  2853. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2854. return (ARCHIVE_FATAL);
  2855. }
  2856. attr->name = strdup(
  2857. (const char *)xmlTextReaderConstLocalName(reader));
  2858. if (attr->name == NULL) {
  2859. free(attr);
  2860. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2861. return (ARCHIVE_FATAL);
  2862. }
  2863. attr->value = strdup(
  2864. (const char *)xmlTextReaderConstValue(reader));
  2865. if (attr->value == NULL) {
  2866. free(attr->name);
  2867. free(attr);
  2868. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2869. return (ARCHIVE_FATAL);
  2870. }
  2871. attr->next = NULL;
  2872. *list->last = attr;
  2873. list->last = &(attr->next);
  2874. r = xmlTextReaderMoveToNextAttribute(reader);
  2875. }
  2876. return (r);
  2877. }
  2878. static int
  2879. xml2_read_cb(void *context, char *buffer, int len)
  2880. {
  2881. struct archive_read *a;
  2882. struct xar *xar;
  2883. const void *d;
  2884. size_t outbytes;
  2885. size_t used = 0;
  2886. int r;
  2887. a = (struct archive_read *)context;
  2888. xar = (struct xar *)(a->format->data);
  2889. if (xar->toc_remaining <= 0)
  2890. return (0);
  2891. d = buffer;
  2892. outbytes = len;
  2893. r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
  2894. if (r != ARCHIVE_OK)
  2895. return (r);
  2896. __archive_read_consume(a, used);
  2897. xar->toc_remaining -= used;
  2898. xar->offset += used;
  2899. xar->toc_total += outbytes;
  2900. PRINT_TOC(buffer, len);
  2901. return ((int)outbytes);
  2902. }
  2903. static int
  2904. xml2_close_cb(void *context)
  2905. {
  2906. (void)context; /* UNUSED */
  2907. return (0);
  2908. }
  2909. static void
  2910. xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
  2911. xmlTextReaderLocatorPtr locator)
  2912. {
  2913. struct archive_read *a;
  2914. (void)locator; /* UNUSED */
  2915. a = (struct archive_read *)arg;
  2916. switch (severity) {
  2917. case XML_PARSER_SEVERITY_VALIDITY_WARNING:
  2918. case XML_PARSER_SEVERITY_WARNING:
  2919. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2920. "XML Parsing error: %s", msg);
  2921. break;
  2922. case XML_PARSER_SEVERITY_VALIDITY_ERROR:
  2923. case XML_PARSER_SEVERITY_ERROR:
  2924. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2925. "XML Parsing error: %s", msg);
  2926. break;
  2927. }
  2928. }
  2929. static int
  2930. xml2_read_toc(struct archive_read *a)
  2931. {
  2932. xmlTextReaderPtr reader;
  2933. struct xmlattr_list list;
  2934. int r;
  2935. reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
  2936. if (reader == NULL) {
  2937. archive_set_error(&a->archive, ENOMEM,
  2938. "Couldn't allocate memory for xml parser");
  2939. return (ARCHIVE_FATAL);
  2940. }
  2941. xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
  2942. while ((r = xmlTextReaderRead(reader)) == 1) {
  2943. const char *name, *value;
  2944. int type, empty;
  2945. type = xmlTextReaderNodeType(reader);
  2946. name = (const char *)xmlTextReaderConstLocalName(reader);
  2947. switch (type) {
  2948. case XML_READER_TYPE_ELEMENT:
  2949. empty = xmlTextReaderIsEmptyElement(reader);
  2950. r = xml2_xmlattr_setup(a, &list, reader);
  2951. if (r == ARCHIVE_OK)
  2952. r = xml_start(a, name, &list);
  2953. xmlattr_cleanup(&list);
  2954. if (r != ARCHIVE_OK)
  2955. return (r);
  2956. if (empty)
  2957. xml_end(a, name);
  2958. break;
  2959. case XML_READER_TYPE_END_ELEMENT:
  2960. xml_end(a, name);
  2961. break;
  2962. case XML_READER_TYPE_TEXT:
  2963. value = (const char *)xmlTextReaderConstValue(reader);
  2964. xml_data(a, value, strlen(value));
  2965. break;
  2966. case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
  2967. default:
  2968. break;
  2969. }
  2970. if (r < 0)
  2971. break;
  2972. }
  2973. xmlFreeTextReader(reader);
  2974. xmlCleanupParser();
  2975. return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
  2976. }
  2977. #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
  2978. static int
  2979. expat_xmlattr_setup(struct archive_read *a,
  2980. struct xmlattr_list *list, const XML_Char **atts)
  2981. {
  2982. struct xmlattr *attr;
  2983. char *name, *value;
  2984. list->first = NULL;
  2985. list->last = &(list->first);
  2986. if (atts == NULL)
  2987. return (ARCHIVE_OK);
  2988. while (atts[0] != NULL && atts[1] != NULL) {
  2989. attr = malloc(sizeof*(attr));
  2990. name = strdup(atts[0]);
  2991. value = strdup(atts[1]);
  2992. if (attr == NULL || name == NULL || value == NULL) {
  2993. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2994. free(attr);
  2995. free(name);
  2996. free(value);
  2997. return (ARCHIVE_FATAL);
  2998. }
  2999. attr->name = name;
  3000. attr->value = value;
  3001. attr->next = NULL;
  3002. *list->last = attr;
  3003. list->last = &(attr->next);
  3004. atts += 2;
  3005. }
  3006. return (ARCHIVE_OK);
  3007. }
  3008. static void
  3009. expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
  3010. {
  3011. struct expat_userData *ud = (struct expat_userData *)userData;
  3012. struct archive_read *a = ud->archive;
  3013. struct xmlattr_list list;
  3014. int r;
  3015. r = expat_xmlattr_setup(a, &list, atts);
  3016. if (r == ARCHIVE_OK)
  3017. r = xml_start(a, (const char *)name, &list);
  3018. xmlattr_cleanup(&list);
  3019. ud->state = r;
  3020. }
  3021. static void
  3022. expat_end_cb(void *userData, const XML_Char *name)
  3023. {
  3024. struct expat_userData *ud = (struct expat_userData *)userData;
  3025. xml_end(ud->archive, (const char *)name);
  3026. }
  3027. static void
  3028. expat_data_cb(void *userData, const XML_Char *s, int len)
  3029. {
  3030. struct expat_userData *ud = (struct expat_userData *)userData;
  3031. xml_data(ud->archive, s, len);
  3032. }
  3033. static int
  3034. expat_read_toc(struct archive_read *a)
  3035. {
  3036. struct xar *xar;
  3037. XML_Parser parser;
  3038. struct expat_userData ud;
  3039. ud.state = ARCHIVE_OK;
  3040. ud.archive = a;
  3041. xar = (struct xar *)(a->format->data);
  3042. /* Initialize XML Parser library. */
  3043. parser = XML_ParserCreate(NULL);
  3044. if (parser == NULL) {
  3045. archive_set_error(&a->archive, ENOMEM,
  3046. "Couldn't allocate memory for xml parser");
  3047. return (ARCHIVE_FATAL);
  3048. }
  3049. XML_SetUserData(parser, &ud);
  3050. XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
  3051. XML_SetCharacterDataHandler(parser, expat_data_cb);
  3052. xar->xmlsts = INIT;
  3053. while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
  3054. enum XML_Status xr;
  3055. const void *d;
  3056. size_t outbytes;
  3057. size_t used;
  3058. int r;
  3059. d = NULL;
  3060. r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
  3061. if (r != ARCHIVE_OK)
  3062. return (r);
  3063. xar->toc_remaining -= used;
  3064. xar->offset += used;
  3065. xar->toc_total += outbytes;
  3066. PRINT_TOC(d, outbytes);
  3067. xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
  3068. __archive_read_consume(a, used);
  3069. if (xr == XML_STATUS_ERROR) {
  3070. XML_ParserFree(parser);
  3071. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3072. "XML Parsing failed");
  3073. return (ARCHIVE_FATAL);
  3074. }
  3075. }
  3076. XML_ParserFree(parser);
  3077. return (ud.state);
  3078. }
  3079. #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
  3080. #endif /* Support xar format */