archive_read_support_format_cpio.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_cpio.c 201163 2009-12-29 05:50:34Z kientzle $");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. /* #include <stdint.h> */ /* See archive_platform.h */
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_read_private.h"
  43. #define bin_magic_offset 0
  44. #define bin_magic_size 2
  45. #define bin_dev_offset 2
  46. #define bin_dev_size 2
  47. #define bin_ino_offset 4
  48. #define bin_ino_size 2
  49. #define bin_mode_offset 6
  50. #define bin_mode_size 2
  51. #define bin_uid_offset 8
  52. #define bin_uid_size 2
  53. #define bin_gid_offset 10
  54. #define bin_gid_size 2
  55. #define bin_nlink_offset 12
  56. #define bin_nlink_size 2
  57. #define bin_rdev_offset 14
  58. #define bin_rdev_size 2
  59. #define bin_mtime_offset 16
  60. #define bin_mtime_size 4
  61. #define bin_namesize_offset 20
  62. #define bin_namesize_size 2
  63. #define bin_filesize_offset 22
  64. #define bin_filesize_size 4
  65. #define bin_header_size 26
  66. #define odc_magic_offset 0
  67. #define odc_magic_size 6
  68. #define odc_dev_offset 6
  69. #define odc_dev_size 6
  70. #define odc_ino_offset 12
  71. #define odc_ino_size 6
  72. #define odc_mode_offset 18
  73. #define odc_mode_size 6
  74. #define odc_uid_offset 24
  75. #define odc_uid_size 6
  76. #define odc_gid_offset 30
  77. #define odc_gid_size 6
  78. #define odc_nlink_offset 36
  79. #define odc_nlink_size 6
  80. #define odc_rdev_offset 42
  81. #define odc_rdev_size 6
  82. #define odc_mtime_offset 48
  83. #define odc_mtime_size 11
  84. #define odc_namesize_offset 59
  85. #define odc_namesize_size 6
  86. #define odc_filesize_offset 65
  87. #define odc_filesize_size 11
  88. #define odc_header_size 76
  89. #define newc_magic_offset 0
  90. #define newc_magic_size 6
  91. #define newc_ino_offset 6
  92. #define newc_ino_size 8
  93. #define newc_mode_offset 14
  94. #define newc_mode_size 8
  95. #define newc_uid_offset 22
  96. #define newc_uid_size 8
  97. #define newc_gid_offset 30
  98. #define newc_gid_size 8
  99. #define newc_nlink_offset 38
  100. #define newc_nlink_size 8
  101. #define newc_mtime_offset 46
  102. #define newc_mtime_size 8
  103. #define newc_filesize_offset 54
  104. #define newc_filesize_size 8
  105. #define newc_devmajor_offset 62
  106. #define newc_devmajor_size 8
  107. #define newc_devminor_offset 70
  108. #define newc_devminor_size 8
  109. #define newc_rdevmajor_offset 78
  110. #define newc_rdevmajor_size 8
  111. #define newc_rdevminor_offset 86
  112. #define newc_rdevminor_size 8
  113. #define newc_namesize_offset 94
  114. #define newc_namesize_size 8
  115. #define newc_checksum_offset 102
  116. #define newc_checksum_size 8
  117. #define newc_header_size 110
  118. /*
  119. * An afio large ASCII header, which they named itself.
  120. * afio utility uses this header, if a file size is larger than 2G bytes
  121. * or inode/uid/gid is bigger than 65535(0xFFFF) or mtime is bigger than
  122. * 0x7fffffff, which we cannot record to odc header because of its limit.
  123. * If not, uses odc header.
  124. */
  125. #define afiol_magic_offset 0
  126. #define afiol_magic_size 6
  127. #define afiol_dev_offset 6
  128. #define afiol_dev_size 8 /* hex */
  129. #define afiol_ino_offset 14
  130. #define afiol_ino_size 16 /* hex */
  131. #define afiol_ino_m_offset 30 /* 'm' */
  132. #define afiol_mode_offset 31
  133. #define afiol_mode_size 6 /* oct */
  134. #define afiol_uid_offset 37
  135. #define afiol_uid_size 8 /* hex */
  136. #define afiol_gid_offset 45
  137. #define afiol_gid_size 8 /* hex */
  138. #define afiol_nlink_offset 53
  139. #define afiol_nlink_size 8 /* hex */
  140. #define afiol_rdev_offset 61
  141. #define afiol_rdev_size 8 /* hex */
  142. #define afiol_mtime_offset 69
  143. #define afiol_mtime_size 16 /* hex */
  144. #define afiol_mtime_n_offset 85 /* 'n' */
  145. #define afiol_namesize_offset 86
  146. #define afiol_namesize_size 4 /* hex */
  147. #define afiol_flag_offset 90
  148. #define afiol_flag_size 4 /* hex */
  149. #define afiol_xsize_offset 94
  150. #define afiol_xsize_size 4 /* hex */
  151. #define afiol_xsize_s_offset 98 /* 's' */
  152. #define afiol_filesize_offset 99
  153. #define afiol_filesize_size 16 /* hex */
  154. #define afiol_filesize_c_offset 115 /* ':' */
  155. #define afiol_header_size 116
  156. struct links_entry {
  157. struct links_entry *next;
  158. struct links_entry *previous;
  159. int links;
  160. dev_t dev;
  161. int64_t ino;
  162. char *name;
  163. };
  164. #define CPIO_MAGIC 0x13141516
  165. struct cpio {
  166. int magic;
  167. int (*read_header)(struct archive_read *, struct cpio *,
  168. struct archive_entry *, size_t *, size_t *);
  169. struct links_entry *links_head;
  170. int64_t entry_bytes_remaining;
  171. int64_t entry_bytes_unconsumed;
  172. int64_t entry_offset;
  173. int64_t entry_padding;
  174. struct archive_string_conv *opt_sconv;
  175. struct archive_string_conv *sconv_default;
  176. int init_default_conversion;
  177. };
  178. static int64_t atol16(const char *, unsigned);
  179. static int64_t atol8(const char *, unsigned);
  180. static int archive_read_format_cpio_bid(struct archive_read *, int);
  181. static int archive_read_format_cpio_options(struct archive_read *,
  182. const char *, const char *);
  183. static int archive_read_format_cpio_cleanup(struct archive_read *);
  184. static int archive_read_format_cpio_read_data(struct archive_read *,
  185. const void **, size_t *, int64_t *);
  186. static int archive_read_format_cpio_read_header(struct archive_read *,
  187. struct archive_entry *);
  188. static int archive_read_format_cpio_skip(struct archive_read *);
  189. static int64_t be4(const unsigned char *);
  190. static int find_odc_header(struct archive_read *);
  191. static int find_newc_header(struct archive_read *);
  192. static int header_bin_be(struct archive_read *, struct cpio *,
  193. struct archive_entry *, size_t *, size_t *);
  194. static int header_bin_le(struct archive_read *, struct cpio *,
  195. struct archive_entry *, size_t *, size_t *);
  196. static int header_newc(struct archive_read *, struct cpio *,
  197. struct archive_entry *, size_t *, size_t *);
  198. static int header_odc(struct archive_read *, struct cpio *,
  199. struct archive_entry *, size_t *, size_t *);
  200. static int header_afiol(struct archive_read *, struct cpio *,
  201. struct archive_entry *, size_t *, size_t *);
  202. static int is_octal(const char *, size_t);
  203. static int is_hex(const char *, size_t);
  204. static int64_t le4(const unsigned char *);
  205. static int record_hardlink(struct archive_read *a,
  206. struct cpio *cpio, struct archive_entry *entry);
  207. int
  208. archive_read_support_format_cpio(struct archive *_a)
  209. {
  210. struct archive_read *a = (struct archive_read *)_a;
  211. struct cpio *cpio;
  212. int r;
  213. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  214. ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
  215. cpio = (struct cpio *)calloc(1, sizeof(*cpio));
  216. if (cpio == NULL) {
  217. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  218. return (ARCHIVE_FATAL);
  219. }
  220. cpio->magic = CPIO_MAGIC;
  221. r = __archive_read_register_format(a,
  222. cpio,
  223. "cpio",
  224. archive_read_format_cpio_bid,
  225. archive_read_format_cpio_options,
  226. archive_read_format_cpio_read_header,
  227. archive_read_format_cpio_read_data,
  228. archive_read_format_cpio_skip,
  229. NULL,
  230. archive_read_format_cpio_cleanup,
  231. NULL,
  232. NULL);
  233. if (r != ARCHIVE_OK)
  234. free(cpio);
  235. return (ARCHIVE_OK);
  236. }
  237. static int
  238. archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
  239. {
  240. const unsigned char *p;
  241. struct cpio *cpio;
  242. int bid;
  243. (void)best_bid; /* UNUSED */
  244. cpio = (struct cpio *)(a->format->data);
  245. if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
  246. return (-1);
  247. bid = 0;
  248. if (memcmp(p, "070707", 6) == 0) {
  249. /* ASCII cpio archive (odc, POSIX.1) */
  250. cpio->read_header = header_odc;
  251. bid += 48;
  252. /*
  253. * XXX TODO: More verification; Could check that only octal
  254. * digits appear in appropriate header locations. XXX
  255. */
  256. } else if (memcmp(p, "070727", 6) == 0) {
  257. /* afio large ASCII cpio archive */
  258. cpio->read_header = header_odc;
  259. bid += 48;
  260. /*
  261. * XXX TODO: More verification; Could check that almost hex
  262. * digits appear in appropriate header locations. XXX
  263. */
  264. } else if (memcmp(p, "070701", 6) == 0) {
  265. /* ASCII cpio archive (SVR4 without CRC) */
  266. cpio->read_header = header_newc;
  267. bid += 48;
  268. /*
  269. * XXX TODO: More verification; Could check that only hex
  270. * digits appear in appropriate header locations. XXX
  271. */
  272. } else if (memcmp(p, "070702", 6) == 0) {
  273. /* ASCII cpio archive (SVR4 with CRC) */
  274. /* XXX TODO: Flag that we should check the CRC. XXX */
  275. cpio->read_header = header_newc;
  276. bid += 48;
  277. /*
  278. * XXX TODO: More verification; Could check that only hex
  279. * digits appear in appropriate header locations. XXX
  280. */
  281. } else if (p[0] * 256 + p[1] == 070707) {
  282. /* big-endian binary cpio archives */
  283. cpio->read_header = header_bin_be;
  284. bid += 16;
  285. /* Is more verification possible here? */
  286. } else if (p[0] + p[1] * 256 == 070707) {
  287. /* little-endian binary cpio archives */
  288. cpio->read_header = header_bin_le;
  289. bid += 16;
  290. /* Is more verification possible here? */
  291. } else
  292. return (ARCHIVE_WARN);
  293. return (bid);
  294. }
  295. static int
  296. archive_read_format_cpio_options(struct archive_read *a,
  297. const char *key, const char *val)
  298. {
  299. struct cpio *cpio;
  300. int ret = ARCHIVE_FAILED;
  301. cpio = (struct cpio *)(a->format->data);
  302. if (strcmp(key, "compat-2x") == 0) {
  303. /* Handle filnames as libarchive 2.x */
  304. cpio->init_default_conversion = (val != NULL)?1:0;
  305. return (ARCHIVE_OK);
  306. } else if (strcmp(key, "hdrcharset") == 0) {
  307. if (val == NULL || val[0] == 0)
  308. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  309. "cpio: hdrcharset option needs a character-set name");
  310. else {
  311. cpio->opt_sconv =
  312. archive_string_conversion_from_charset(
  313. &a->archive, val, 0);
  314. if (cpio->opt_sconv != NULL)
  315. ret = ARCHIVE_OK;
  316. else
  317. ret = ARCHIVE_FATAL;
  318. }
  319. return (ret);
  320. }
  321. /* Note: The "warn" return is just to inform the options
  322. * supervisor that we didn't handle it. It will generate
  323. * a suitable error if no one used this option. */
  324. return (ARCHIVE_WARN);
  325. }
  326. static int
  327. archive_read_format_cpio_read_header(struct archive_read *a,
  328. struct archive_entry *entry)
  329. {
  330. struct cpio *cpio;
  331. const void *h;
  332. struct archive_string_conv *sconv;
  333. size_t namelength;
  334. size_t name_pad;
  335. int r;
  336. cpio = (struct cpio *)(a->format->data);
  337. sconv = cpio->opt_sconv;
  338. if (sconv == NULL) {
  339. if (!cpio->init_default_conversion) {
  340. cpio->sconv_default =
  341. archive_string_default_conversion_for_read(
  342. &(a->archive));
  343. cpio->init_default_conversion = 1;
  344. }
  345. sconv = cpio->sconv_default;
  346. }
  347. r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
  348. if (r < ARCHIVE_WARN)
  349. return (r);
  350. /* Read name from buffer. */
  351. h = __archive_read_ahead(a, namelength + name_pad, NULL);
  352. if (h == NULL)
  353. return (ARCHIVE_FATAL);
  354. if (archive_entry_copy_pathname_l(entry,
  355. (const char *)h, namelength, sconv) != 0) {
  356. if (errno == ENOMEM) {
  357. archive_set_error(&a->archive, ENOMEM,
  358. "Can't allocate memory for Pathname");
  359. return (ARCHIVE_FATAL);
  360. }
  361. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  362. "Pathname can't be converted from %s to current locale.",
  363. archive_string_conversion_charset_name(sconv));
  364. r = ARCHIVE_WARN;
  365. }
  366. cpio->entry_offset = 0;
  367. __archive_read_consume(a, namelength + name_pad);
  368. /* If this is a symlink, read the link contents. */
  369. if (archive_entry_filetype(entry) == AE_IFLNK) {
  370. if (cpio->entry_bytes_remaining > 1024 * 1024) {
  371. archive_set_error(&a->archive, ENOMEM,
  372. "Rejecting malformed cpio archive: symlink contents exceed 1 megabyte");
  373. return (ARCHIVE_FATAL);
  374. }
  375. h = __archive_read_ahead(a,
  376. (size_t)cpio->entry_bytes_remaining, NULL);
  377. if (h == NULL)
  378. return (ARCHIVE_FATAL);
  379. if (archive_entry_copy_symlink_l(entry, (const char *)h,
  380. (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
  381. if (errno == ENOMEM) {
  382. archive_set_error(&a->archive, ENOMEM,
  383. "Can't allocate memory for Linkname");
  384. return (ARCHIVE_FATAL);
  385. }
  386. archive_set_error(&a->archive,
  387. ARCHIVE_ERRNO_FILE_FORMAT,
  388. "Linkname can't be converted from %s to "
  389. "current locale.",
  390. archive_string_conversion_charset_name(sconv));
  391. r = ARCHIVE_WARN;
  392. }
  393. __archive_read_consume(a, cpio->entry_bytes_remaining);
  394. cpio->entry_bytes_remaining = 0;
  395. }
  396. /* XXX TODO: If the full mode is 0160200, then this is a Solaris
  397. * ACL description for the following entry. Read this body
  398. * and parse it as a Solaris-style ACL, then read the next
  399. * header. XXX */
  400. /* Compare name to "TRAILER!!!" to test for end-of-archive. */
  401. if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
  402. /* TODO: Store file location of start of block. */
  403. archive_clear_error(&a->archive);
  404. return (ARCHIVE_EOF);
  405. }
  406. /* Detect and record hardlinks to previously-extracted entries. */
  407. if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
  408. return (ARCHIVE_FATAL);
  409. }
  410. return (r);
  411. }
  412. static int
  413. archive_read_format_cpio_read_data(struct archive_read *a,
  414. const void **buff, size_t *size, int64_t *offset)
  415. {
  416. ssize_t bytes_read;
  417. struct cpio *cpio;
  418. cpio = (struct cpio *)(a->format->data);
  419. if (cpio->entry_bytes_unconsumed) {
  420. __archive_read_consume(a, cpio->entry_bytes_unconsumed);
  421. cpio->entry_bytes_unconsumed = 0;
  422. }
  423. if (cpio->entry_bytes_remaining > 0) {
  424. *buff = __archive_read_ahead(a, 1, &bytes_read);
  425. if (bytes_read <= 0)
  426. return (ARCHIVE_FATAL);
  427. if (bytes_read > cpio->entry_bytes_remaining)
  428. bytes_read = (ssize_t)cpio->entry_bytes_remaining;
  429. *size = bytes_read;
  430. cpio->entry_bytes_unconsumed = bytes_read;
  431. *offset = cpio->entry_offset;
  432. cpio->entry_offset += bytes_read;
  433. cpio->entry_bytes_remaining -= bytes_read;
  434. return (ARCHIVE_OK);
  435. } else {
  436. if (cpio->entry_padding !=
  437. __archive_read_consume(a, cpio->entry_padding)) {
  438. return (ARCHIVE_FATAL);
  439. }
  440. cpio->entry_padding = 0;
  441. *buff = NULL;
  442. *size = 0;
  443. *offset = cpio->entry_offset;
  444. return (ARCHIVE_EOF);
  445. }
  446. }
  447. static int
  448. archive_read_format_cpio_skip(struct archive_read *a)
  449. {
  450. struct cpio *cpio = (struct cpio *)(a->format->data);
  451. int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
  452. cpio->entry_bytes_unconsumed;
  453. if (to_skip != __archive_read_consume(a, to_skip)) {
  454. return (ARCHIVE_FATAL);
  455. }
  456. cpio->entry_bytes_remaining = 0;
  457. cpio->entry_padding = 0;
  458. cpio->entry_bytes_unconsumed = 0;
  459. return (ARCHIVE_OK);
  460. }
  461. /*
  462. * Skip forward to the next cpio newc header by searching for the
  463. * 07070[12] string. This should be generalized and merged with
  464. * find_odc_header below.
  465. */
  466. static int
  467. is_hex(const char *p, size_t len)
  468. {
  469. while (len-- > 0) {
  470. if ((*p >= '0' && *p <= '9')
  471. || (*p >= 'a' && *p <= 'f')
  472. || (*p >= 'A' && *p <= 'F'))
  473. ++p;
  474. else
  475. return (0);
  476. }
  477. return (1);
  478. }
  479. static int
  480. find_newc_header(struct archive_read *a)
  481. {
  482. const void *h;
  483. const char *p, *q;
  484. size_t skip, skipped = 0;
  485. ssize_t bytes;
  486. for (;;) {
  487. h = __archive_read_ahead(a, newc_header_size, &bytes);
  488. if (h == NULL)
  489. return (ARCHIVE_FATAL);
  490. p = h;
  491. q = p + bytes;
  492. /* Try the typical case first, then go into the slow search.*/
  493. if (memcmp("07070", p, 5) == 0
  494. && (p[5] == '1' || p[5] == '2')
  495. && is_hex(p, newc_header_size))
  496. return (ARCHIVE_OK);
  497. /*
  498. * Scan ahead until we find something that looks
  499. * like a newc header.
  500. */
  501. while (p + newc_header_size <= q) {
  502. switch (p[5]) {
  503. case '1':
  504. case '2':
  505. if (memcmp("07070", p, 5) == 0
  506. && is_hex(p, newc_header_size)) {
  507. skip = p - (const char *)h;
  508. __archive_read_consume(a, skip);
  509. skipped += skip;
  510. if (skipped > 0) {
  511. archive_set_error(&a->archive,
  512. 0,
  513. "Skipped %d bytes before "
  514. "finding valid header",
  515. (int)skipped);
  516. return (ARCHIVE_WARN);
  517. }
  518. return (ARCHIVE_OK);
  519. }
  520. p += 2;
  521. break;
  522. case '0':
  523. p++;
  524. break;
  525. default:
  526. p += 6;
  527. break;
  528. }
  529. }
  530. skip = p - (const char *)h;
  531. __archive_read_consume(a, skip);
  532. skipped += skip;
  533. }
  534. }
  535. static int
  536. header_newc(struct archive_read *a, struct cpio *cpio,
  537. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  538. {
  539. const void *h;
  540. const char *header;
  541. int r;
  542. r = find_newc_header(a);
  543. if (r < ARCHIVE_WARN)
  544. return (r);
  545. /* Read fixed-size portion of header. */
  546. h = __archive_read_ahead(a, newc_header_size, NULL);
  547. if (h == NULL)
  548. return (ARCHIVE_FATAL);
  549. /* Parse out hex fields. */
  550. header = (const char *)h;
  551. if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
  552. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  553. a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
  554. } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
  555. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
  556. a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
  557. } else {
  558. /* TODO: Abort here? */
  559. }
  560. archive_entry_set_devmajor(entry,
  561. (dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
  562. archive_entry_set_devminor(entry,
  563. (dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
  564. archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
  565. archive_entry_set_mode(entry,
  566. (mode_t)atol16(header + newc_mode_offset, newc_mode_size));
  567. archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
  568. archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
  569. archive_entry_set_nlink(entry,
  570. (unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
  571. archive_entry_set_rdevmajor(entry,
  572. (dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
  573. archive_entry_set_rdevminor(entry,
  574. (dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
  575. archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
  576. *namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
  577. /* Pad name to 2 more than a multiple of 4. */
  578. *name_pad = (2 - *namelength) & 3;
  579. /*
  580. * Note: entry_bytes_remaining is at least 64 bits and
  581. * therefore guaranteed to be big enough for a 33-bit file
  582. * size.
  583. */
  584. cpio->entry_bytes_remaining =
  585. atol16(header + newc_filesize_offset, newc_filesize_size);
  586. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  587. /* Pad file contents to a multiple of 4. */
  588. cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
  589. __archive_read_consume(a, newc_header_size);
  590. return (r);
  591. }
  592. /*
  593. * Skip forward to the next cpio odc header by searching for the
  594. * 070707 string. This is a hand-optimized search that could
  595. * probably be easily generalized to handle all character-based
  596. * cpio variants.
  597. */
  598. static int
  599. is_octal(const char *p, size_t len)
  600. {
  601. while (len-- > 0) {
  602. if (*p < '0' || *p > '7')
  603. return (0);
  604. ++p;
  605. }
  606. return (1);
  607. }
  608. static int
  609. is_afio_large(const char *h, size_t len)
  610. {
  611. if (len < afiol_header_size)
  612. return (0);
  613. if (h[afiol_ino_m_offset] != 'm'
  614. || h[afiol_mtime_n_offset] != 'n'
  615. || h[afiol_xsize_s_offset] != 's'
  616. || h[afiol_filesize_c_offset] != ':')
  617. return (0);
  618. if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
  619. return (0);
  620. if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
  621. return (0);
  622. if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
  623. return (0);
  624. if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
  625. return (0);
  626. return (1);
  627. }
  628. static int
  629. find_odc_header(struct archive_read *a)
  630. {
  631. const void *h;
  632. const char *p, *q;
  633. size_t skip, skipped = 0;
  634. ssize_t bytes;
  635. for (;;) {
  636. h = __archive_read_ahead(a, odc_header_size, &bytes);
  637. if (h == NULL)
  638. return (ARCHIVE_FATAL);
  639. p = h;
  640. q = p + bytes;
  641. /* Try the typical case first, then go into the slow search.*/
  642. if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
  643. return (ARCHIVE_OK);
  644. if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
  645. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  646. return (ARCHIVE_OK);
  647. }
  648. /*
  649. * Scan ahead until we find something that looks
  650. * like an odc header.
  651. */
  652. while (p + odc_header_size <= q) {
  653. switch (p[5]) {
  654. case '7':
  655. if ((memcmp("070707", p, 6) == 0
  656. && is_octal(p, odc_header_size))
  657. || (memcmp("070727", p, 6) == 0
  658. && is_afio_large(p, q - p))) {
  659. skip = p - (const char *)h;
  660. __archive_read_consume(a, skip);
  661. skipped += skip;
  662. if (p[4] == '2')
  663. a->archive.archive_format =
  664. ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  665. if (skipped > 0) {
  666. archive_set_error(&a->archive,
  667. 0,
  668. "Skipped %d bytes before "
  669. "finding valid header",
  670. (int)skipped);
  671. return (ARCHIVE_WARN);
  672. }
  673. return (ARCHIVE_OK);
  674. }
  675. p += 2;
  676. break;
  677. case '0':
  678. p++;
  679. break;
  680. default:
  681. p += 6;
  682. break;
  683. }
  684. }
  685. skip = p - (const char *)h;
  686. __archive_read_consume(a, skip);
  687. skipped += skip;
  688. }
  689. }
  690. static int
  691. header_odc(struct archive_read *a, struct cpio *cpio,
  692. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  693. {
  694. const void *h;
  695. int r;
  696. const char *header;
  697. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  698. a->archive.archive_format_name = "POSIX octet-oriented cpio";
  699. /* Find the start of the next header. */
  700. r = find_odc_header(a);
  701. if (r < ARCHIVE_WARN)
  702. return (r);
  703. if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
  704. int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
  705. if (r2 == ARCHIVE_OK)
  706. return (r);
  707. else
  708. return (r2);
  709. }
  710. /* Read fixed-size portion of header. */
  711. h = __archive_read_ahead(a, odc_header_size, NULL);
  712. if (h == NULL)
  713. return (ARCHIVE_FATAL);
  714. /* Parse out octal fields. */
  715. header = (const char *)h;
  716. archive_entry_set_dev(entry,
  717. (dev_t)atol8(header + odc_dev_offset, odc_dev_size));
  718. archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
  719. archive_entry_set_mode(entry,
  720. (mode_t)atol8(header + odc_mode_offset, odc_mode_size));
  721. archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
  722. archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
  723. archive_entry_set_nlink(entry,
  724. (unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
  725. archive_entry_set_rdev(entry,
  726. (dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
  727. archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
  728. *namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
  729. *name_pad = 0; /* No padding of filename. */
  730. /*
  731. * Note: entry_bytes_remaining is at least 64 bits and
  732. * therefore guaranteed to be big enough for a 33-bit file
  733. * size.
  734. */
  735. cpio->entry_bytes_remaining =
  736. atol8(header + odc_filesize_offset, odc_filesize_size);
  737. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  738. cpio->entry_padding = 0;
  739. __archive_read_consume(a, odc_header_size);
  740. return (r);
  741. }
  742. /*
  743. * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
  744. * it would be nice that we can show uncompressed file size and we can
  745. * uncompressed file contents automatically, unfortunately we have nothing
  746. * to get a uncompressed file size while reading each header. it means
  747. * we also cannot uncompressed file contens under the our framework.
  748. */
  749. static int
  750. header_afiol(struct archive_read *a, struct cpio *cpio,
  751. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  752. {
  753. const void *h;
  754. const char *header;
  755. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  756. a->archive.archive_format_name = "afio large ASCII";
  757. /* Read fixed-size portion of header. */
  758. h = __archive_read_ahead(a, afiol_header_size, NULL);
  759. if (h == NULL)
  760. return (ARCHIVE_FATAL);
  761. /* Parse out octal fields. */
  762. header = (const char *)h;
  763. archive_entry_set_dev(entry,
  764. (dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
  765. archive_entry_set_ino(entry, atol16(header + afiol_ino_offset, afiol_ino_size));
  766. archive_entry_set_mode(entry,
  767. (mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
  768. archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
  769. archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
  770. archive_entry_set_nlink(entry,
  771. (unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
  772. archive_entry_set_rdev(entry,
  773. (dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
  774. archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
  775. *namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
  776. *name_pad = 0; /* No padding of filename. */
  777. cpio->entry_bytes_remaining =
  778. atol16(header + afiol_filesize_offset, afiol_filesize_size);
  779. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  780. cpio->entry_padding = 0;
  781. __archive_read_consume(a, afiol_header_size);
  782. return (ARCHIVE_OK);
  783. }
  784. static int
  785. header_bin_le(struct archive_read *a, struct cpio *cpio,
  786. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  787. {
  788. const void *h;
  789. const unsigned char *header;
  790. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
  791. a->archive.archive_format_name = "cpio (little-endian binary)";
  792. /* Read fixed-size portion of header. */
  793. h = __archive_read_ahead(a, bin_header_size, NULL);
  794. if (h == NULL) {
  795. archive_set_error(&a->archive, 0,
  796. "End of file trying to read next cpio header");
  797. return (ARCHIVE_FATAL);
  798. }
  799. /* Parse out binary fields. */
  800. header = (const unsigned char *)h;
  801. archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
  802. archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
  803. archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
  804. archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
  805. archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
  806. archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
  807. archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
  808. archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
  809. *namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
  810. *name_pad = *namelength & 1; /* Pad to even. */
  811. cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
  812. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  813. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  814. __archive_read_consume(a, bin_header_size);
  815. return (ARCHIVE_OK);
  816. }
  817. static int
  818. header_bin_be(struct archive_read *a, struct cpio *cpio,
  819. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  820. {
  821. const void *h;
  822. const unsigned char *header;
  823. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
  824. a->archive.archive_format_name = "cpio (big-endian binary)";
  825. /* Read fixed-size portion of header. */
  826. h = __archive_read_ahead(a, bin_header_size, NULL);
  827. if (h == NULL) {
  828. archive_set_error(&a->archive, 0,
  829. "End of file trying to read next cpio header");
  830. return (ARCHIVE_FATAL);
  831. }
  832. /* Parse out binary fields. */
  833. header = (const unsigned char *)h;
  834. archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
  835. archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
  836. archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
  837. archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
  838. archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
  839. archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
  840. archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
  841. archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
  842. *namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
  843. *name_pad = *namelength & 1; /* Pad to even. */
  844. cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
  845. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  846. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  847. __archive_read_consume(a, bin_header_size);
  848. return (ARCHIVE_OK);
  849. }
  850. static int
  851. archive_read_format_cpio_cleanup(struct archive_read *a)
  852. {
  853. struct cpio *cpio;
  854. cpio = (struct cpio *)(a->format->data);
  855. /* Free inode->name map */
  856. while (cpio->links_head != NULL) {
  857. struct links_entry *lp = cpio->links_head->next;
  858. if (cpio->links_head->name)
  859. free(cpio->links_head->name);
  860. free(cpio->links_head);
  861. cpio->links_head = lp;
  862. }
  863. free(cpio);
  864. (a->format->data) = NULL;
  865. return (ARCHIVE_OK);
  866. }
  867. static int64_t
  868. le4(const unsigned char *p)
  869. {
  870. return ((p[0] << 16) + (((int64_t)p[1]) << 24) + (p[2] << 0) + (p[3] << 8));
  871. }
  872. static int64_t
  873. be4(const unsigned char *p)
  874. {
  875. return ((((int64_t)p[0]) << 24) + (p[1] << 16) + (p[2] << 8) + (p[3]));
  876. }
  877. /*
  878. * Note that this implementation does not (and should not!) obey
  879. * locale settings; you cannot simply substitute strtol here, since
  880. * it does obey locale.
  881. */
  882. static int64_t
  883. atol8(const char *p, unsigned char_cnt)
  884. {
  885. int64_t l;
  886. int digit;
  887. l = 0;
  888. while (char_cnt-- > 0) {
  889. if (*p >= '0' && *p <= '7')
  890. digit = *p - '0';
  891. else
  892. return (l);
  893. p++;
  894. l <<= 3;
  895. l |= digit;
  896. }
  897. return (l);
  898. }
  899. static int64_t
  900. atol16(const char *p, unsigned char_cnt)
  901. {
  902. int64_t l;
  903. int digit;
  904. l = 0;
  905. while (char_cnt-- > 0) {
  906. if (*p >= 'a' && *p <= 'f')
  907. digit = *p - 'a' + 10;
  908. else if (*p >= 'A' && *p <= 'F')
  909. digit = *p - 'A' + 10;
  910. else if (*p >= '0' && *p <= '9')
  911. digit = *p - '0';
  912. else
  913. return (l);
  914. p++;
  915. l <<= 4;
  916. l |= digit;
  917. }
  918. return (l);
  919. }
  920. static int
  921. record_hardlink(struct archive_read *a,
  922. struct cpio *cpio, struct archive_entry *entry)
  923. {
  924. struct links_entry *le;
  925. dev_t dev;
  926. int64_t ino;
  927. if (archive_entry_nlink(entry) <= 1)
  928. return (ARCHIVE_OK);
  929. dev = archive_entry_dev(entry);
  930. ino = archive_entry_ino64(entry);
  931. /*
  932. * First look in the list of multiply-linked files. If we've
  933. * already dumped it, convert this entry to a hard link entry.
  934. */
  935. for (le = cpio->links_head; le; le = le->next) {
  936. if (le->dev == dev && le->ino == ino) {
  937. archive_entry_copy_hardlink(entry, le->name);
  938. if (--le->links <= 0) {
  939. if (le->previous != NULL)
  940. le->previous->next = le->next;
  941. if (le->next != NULL)
  942. le->next->previous = le->previous;
  943. if (cpio->links_head == le)
  944. cpio->links_head = le->next;
  945. free(le->name);
  946. free(le);
  947. }
  948. return (ARCHIVE_OK);
  949. }
  950. }
  951. le = (struct links_entry *)malloc(sizeof(struct links_entry));
  952. if (le == NULL) {
  953. archive_set_error(&a->archive,
  954. ENOMEM, "Out of memory adding file to list");
  955. return (ARCHIVE_FATAL);
  956. }
  957. if (cpio->links_head != NULL)
  958. cpio->links_head->previous = le;
  959. le->next = cpio->links_head;
  960. le->previous = NULL;
  961. cpio->links_head = le;
  962. le->dev = dev;
  963. le->ino = ino;
  964. le->links = archive_entry_nlink(entry) - 1;
  965. le->name = strdup(archive_entry_pathname(entry));
  966. if (le->name == NULL) {
  967. archive_set_error(&a->archive,
  968. ENOMEM, "Out of memory adding file to list");
  969. return (ARCHIVE_FATAL);
  970. }
  971. return (ARCHIVE_OK);
  972. }