archive_write_set_format_ar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*-
  2. * Copyright (c) 2007 Kai Wang
  3. * Copyright (c) 2007 Tim Kientzle
  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. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_ar.c 201108 2009-12-28 03:28:21Z kientzle $");
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  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_private.h"
  41. #include "archive_write_private.h"
  42. #include "archive_write_set_format_private.h"
  43. struct ar_w {
  44. uint64_t entry_bytes_remaining;
  45. uint64_t entry_padding;
  46. int is_strtab;
  47. int has_strtab;
  48. char wrote_global_header;
  49. char *strtab;
  50. };
  51. /*
  52. * Define structure of the "ar" header.
  53. */
  54. #define AR_name_offset 0
  55. #define AR_name_size 16
  56. #define AR_date_offset 16
  57. #define AR_date_size 12
  58. #define AR_uid_offset 28
  59. #define AR_uid_size 6
  60. #define AR_gid_offset 34
  61. #define AR_gid_size 6
  62. #define AR_mode_offset 40
  63. #define AR_mode_size 8
  64. #define AR_size_offset 48
  65. #define AR_size_size 10
  66. #define AR_fmag_offset 58
  67. #define AR_fmag_size 2
  68. static int archive_write_set_format_ar(struct archive_write *);
  69. static int archive_write_ar_header(struct archive_write *,
  70. struct archive_entry *);
  71. static ssize_t archive_write_ar_data(struct archive_write *,
  72. const void *buff, size_t s);
  73. static int archive_write_ar_free(struct archive_write *);
  74. static int archive_write_ar_close(struct archive_write *);
  75. static int archive_write_ar_finish_entry(struct archive_write *);
  76. static const char *ar_basename(const char *path);
  77. static int format_octal(int64_t v, char *p, int s);
  78. static int format_decimal(int64_t v, char *p, int s);
  79. int
  80. archive_write_set_format_ar_bsd(struct archive *_a)
  81. {
  82. struct archive_write *a = (struct archive_write *)_a;
  83. int r;
  84. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  85. ARCHIVE_STATE_NEW, "archive_write_set_format_ar_bsd");
  86. r = archive_write_set_format_ar(a);
  87. if (r == ARCHIVE_OK) {
  88. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  89. a->archive.archive_format_name = "ar (BSD)";
  90. }
  91. return (r);
  92. }
  93. int
  94. archive_write_set_format_ar_svr4(struct archive *_a)
  95. {
  96. struct archive_write *a = (struct archive_write *)_a;
  97. int r;
  98. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  99. ARCHIVE_STATE_NEW, "archive_write_set_format_ar_svr4");
  100. r = archive_write_set_format_ar(a);
  101. if (r == ARCHIVE_OK) {
  102. a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
  103. a->archive.archive_format_name = "ar (GNU/SVR4)";
  104. }
  105. return (r);
  106. }
  107. /*
  108. * Generic initialization.
  109. */
  110. static int
  111. archive_write_set_format_ar(struct archive_write *a)
  112. {
  113. struct ar_w *ar;
  114. /* If someone else was already registered, unregister them. */
  115. if (a->format_free != NULL)
  116. (a->format_free)(a);
  117. ar = (struct ar_w *)calloc(1, sizeof(*ar));
  118. if (ar == NULL) {
  119. archive_set_error(&a->archive, ENOMEM, "Can't allocate ar data");
  120. return (ARCHIVE_FATAL);
  121. }
  122. a->format_data = ar;
  123. a->format_name = "ar";
  124. a->format_write_header = archive_write_ar_header;
  125. a->format_write_data = archive_write_ar_data;
  126. a->format_close = archive_write_ar_close;
  127. a->format_free = archive_write_ar_free;
  128. a->format_finish_entry = archive_write_ar_finish_entry;
  129. return (ARCHIVE_OK);
  130. }
  131. static int
  132. archive_write_ar_header(struct archive_write *a, struct archive_entry *entry)
  133. {
  134. int ret, append_fn;
  135. char buff[60];
  136. char *ss, *se;
  137. struct ar_w *ar;
  138. const char *pathname;
  139. const char *filename;
  140. int64_t size;
  141. append_fn = 0;
  142. ar = (struct ar_w *)a->format_data;
  143. ar->is_strtab = 0;
  144. filename = NULL;
  145. size = archive_entry_size(entry);
  146. /*
  147. * Reject files with empty name.
  148. */
  149. pathname = archive_entry_pathname(entry);
  150. if (pathname == NULL || *pathname == '\0') {
  151. archive_set_error(&a->archive, EINVAL,
  152. "Invalid filename");
  153. return (ARCHIVE_WARN);
  154. }
  155. /*
  156. * If we are now at the beginning of the archive,
  157. * we need first write the ar global header.
  158. */
  159. if (!ar->wrote_global_header) {
  160. __archive_write_output(a, "!<arch>\n", 8);
  161. ar->wrote_global_header = 1;
  162. }
  163. memset(buff, ' ', 60);
  164. memcpy(&buff[AR_fmag_offset], "`\n", 2);
  165. if (strcmp(pathname, "/") == 0 ) {
  166. /* Entry is archive symbol table in GNU format */
  167. buff[AR_name_offset] = '/';
  168. goto stat;
  169. }
  170. if (strcmp(pathname, "/SYM64/") == 0) {
  171. /* Entry is archive symbol table in GNU 64-bit format */
  172. memcpy(buff + AR_name_offset, "/SYM64/", 7);
  173. goto stat;
  174. }
  175. if (strcmp(pathname, "__.SYMDEF") == 0) {
  176. /* Entry is archive symbol table in BSD format */
  177. memcpy(buff + AR_name_offset, "__.SYMDEF", 9);
  178. goto stat;
  179. }
  180. if (strcmp(pathname, "//") == 0) {
  181. /*
  182. * Entry is archive filename table, inform that we should
  183. * collect strtab in next _data call.
  184. */
  185. ar->is_strtab = 1;
  186. buff[AR_name_offset] = buff[AR_name_offset + 1] = '/';
  187. /*
  188. * For archive string table, only ar_size field should
  189. * be set.
  190. */
  191. goto size;
  192. }
  193. /*
  194. * Otherwise, entry is a normal archive member.
  195. * Strip leading paths from filenames, if any.
  196. */
  197. if ((filename = ar_basename(pathname)) == NULL) {
  198. /* Reject filenames with trailing "/" */
  199. archive_set_error(&a->archive, EINVAL,
  200. "Invalid filename");
  201. return (ARCHIVE_WARN);
  202. }
  203. if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU) {
  204. /*
  205. * SVR4/GNU variant use a "/" to mark then end of the filename,
  206. * make it possible to have embedded spaces in the filename.
  207. * So, the longest filename here (without extension) is
  208. * actually 15 bytes.
  209. */
  210. if (strlen(filename) <= 15) {
  211. memcpy(&buff[AR_name_offset],
  212. filename, strlen(filename));
  213. buff[AR_name_offset + strlen(filename)] = '/';
  214. } else {
  215. /*
  216. * For filename longer than 15 bytes, GNU variant
  217. * makes use of a string table and instead stores the
  218. * offset of the real filename to in the ar_name field.
  219. * The string table should have been written before.
  220. */
  221. if (ar->has_strtab <= 0) {
  222. archive_set_error(&a->archive, EINVAL,
  223. "Can't find string table");
  224. return (ARCHIVE_WARN);
  225. }
  226. se = (char *)malloc(strlen(filename) + 3);
  227. if (se == NULL) {
  228. archive_set_error(&a->archive, ENOMEM,
  229. "Can't allocate filename buffer");
  230. return (ARCHIVE_FATAL);
  231. }
  232. memcpy(se, filename, strlen(filename));
  233. strcpy(se + strlen(filename), "/\n");
  234. ss = strstr(ar->strtab, se);
  235. free(se);
  236. if (ss == NULL) {
  237. archive_set_error(&a->archive, EINVAL,
  238. "Invalid string table");
  239. return (ARCHIVE_WARN);
  240. }
  241. /*
  242. * GNU variant puts "/" followed by digits into
  243. * ar_name field. These digits indicates the real
  244. * filename string's offset to the string table.
  245. */
  246. buff[AR_name_offset] = '/';
  247. if (format_decimal(ss - ar->strtab,
  248. buff + AR_name_offset + 1,
  249. AR_name_size - 1)) {
  250. archive_set_error(&a->archive, ERANGE,
  251. "string table offset too large");
  252. return (ARCHIVE_WARN);
  253. }
  254. }
  255. } else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD) {
  256. /*
  257. * BSD variant: for any file name which is more than
  258. * 16 chars or contains one or more embedded space(s), the
  259. * string "#1/" followed by the ASCII length of the name is
  260. * put into the ar_name field. The file size (stored in the
  261. * ar_size field) is incremented by the length of the name.
  262. * The name is then written immediately following the
  263. * archive header.
  264. */
  265. if (strlen(filename) <= 16 && strchr(filename, ' ') == NULL) {
  266. memcpy(&buff[AR_name_offset], filename, strlen(filename));
  267. buff[AR_name_offset + strlen(filename)] = ' ';
  268. }
  269. else {
  270. memcpy(buff + AR_name_offset, "#1/", 3);
  271. if (format_decimal(strlen(filename),
  272. buff + AR_name_offset + 3,
  273. AR_name_size - 3)) {
  274. archive_set_error(&a->archive, ERANGE,
  275. "File name too long");
  276. return (ARCHIVE_WARN);
  277. }
  278. append_fn = 1;
  279. size += strlen(filename);
  280. }
  281. }
  282. stat:
  283. if (format_decimal(archive_entry_mtime(entry), buff + AR_date_offset, AR_date_size)) {
  284. archive_set_error(&a->archive, ERANGE,
  285. "File modification time too large");
  286. return (ARCHIVE_WARN);
  287. }
  288. if (format_decimal(archive_entry_uid(entry), buff + AR_uid_offset, AR_uid_size)) {
  289. archive_set_error(&a->archive, ERANGE,
  290. "Numeric user ID too large");
  291. return (ARCHIVE_WARN);
  292. }
  293. if (format_decimal(archive_entry_gid(entry), buff + AR_gid_offset, AR_gid_size)) {
  294. archive_set_error(&a->archive, ERANGE,
  295. "Numeric group ID too large");
  296. return (ARCHIVE_WARN);
  297. }
  298. if (format_octal(archive_entry_mode(entry), buff + AR_mode_offset, AR_mode_size)) {
  299. archive_set_error(&a->archive, ERANGE,
  300. "Numeric mode too large");
  301. return (ARCHIVE_WARN);
  302. }
  303. /*
  304. * Sanity Check: A non-pseudo archive member should always be
  305. * a regular file.
  306. */
  307. if (filename != NULL && archive_entry_filetype(entry) != AE_IFREG) {
  308. archive_set_error(&a->archive, EINVAL,
  309. "Regular file required for non-pseudo member");
  310. return (ARCHIVE_WARN);
  311. }
  312. size:
  313. if (format_decimal(size, buff + AR_size_offset, AR_size_size)) {
  314. archive_set_error(&a->archive, ERANGE,
  315. "File size out of range");
  316. return (ARCHIVE_WARN);
  317. }
  318. ret = __archive_write_output(a, buff, 60);
  319. if (ret != ARCHIVE_OK)
  320. return (ret);
  321. ar->entry_bytes_remaining = size;
  322. ar->entry_padding = ar->entry_bytes_remaining % 2;
  323. if (append_fn > 0) {
  324. ret = __archive_write_output(a, filename, strlen(filename));
  325. if (ret != ARCHIVE_OK)
  326. return (ret);
  327. ar->entry_bytes_remaining -= strlen(filename);
  328. }
  329. return (ARCHIVE_OK);
  330. }
  331. static ssize_t
  332. archive_write_ar_data(struct archive_write *a, const void *buff, size_t s)
  333. {
  334. struct ar_w *ar;
  335. int ret;
  336. ar = (struct ar_w *)a->format_data;
  337. if (s > ar->entry_bytes_remaining)
  338. s = (size_t)ar->entry_bytes_remaining;
  339. if (ar->is_strtab > 0) {
  340. if (ar->has_strtab > 0) {
  341. archive_set_error(&a->archive, EINVAL,
  342. "More than one string tables exist");
  343. return (ARCHIVE_WARN);
  344. }
  345. ar->strtab = (char *)malloc(s + 1);
  346. if (ar->strtab == NULL) {
  347. archive_set_error(&a->archive, ENOMEM,
  348. "Can't allocate strtab buffer");
  349. return (ARCHIVE_FATAL);
  350. }
  351. memcpy(ar->strtab, buff, s);
  352. ar->strtab[s] = '\0';
  353. ar->has_strtab = 1;
  354. }
  355. ret = __archive_write_output(a, buff, s);
  356. if (ret != ARCHIVE_OK)
  357. return (ret);
  358. ar->entry_bytes_remaining -= s;
  359. return (s);
  360. }
  361. static int
  362. archive_write_ar_free(struct archive_write *a)
  363. {
  364. struct ar_w *ar;
  365. ar = (struct ar_w *)a->format_data;
  366. if (ar == NULL)
  367. return (ARCHIVE_OK);
  368. if (ar->has_strtab > 0) {
  369. free(ar->strtab);
  370. ar->strtab = NULL;
  371. }
  372. free(ar);
  373. a->format_data = NULL;
  374. return (ARCHIVE_OK);
  375. }
  376. static int
  377. archive_write_ar_close(struct archive_write *a)
  378. {
  379. struct ar_w *ar;
  380. int ret;
  381. /*
  382. * If we haven't written anything yet, we need to write
  383. * the ar global header now to make it a valid ar archive.
  384. */
  385. ar = (struct ar_w *)a->format_data;
  386. if (!ar->wrote_global_header) {
  387. ar->wrote_global_header = 1;
  388. ret = __archive_write_output(a, "!<arch>\n", 8);
  389. return (ret);
  390. }
  391. return (ARCHIVE_OK);
  392. }
  393. static int
  394. archive_write_ar_finish_entry(struct archive_write *a)
  395. {
  396. struct ar_w *ar;
  397. int ret;
  398. ar = (struct ar_w *)a->format_data;
  399. if (ar->entry_bytes_remaining != 0) {
  400. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  401. "Entry remaining bytes larger than 0");
  402. return (ARCHIVE_WARN);
  403. }
  404. if (ar->entry_padding == 0) {
  405. return (ARCHIVE_OK);
  406. }
  407. if (ar->entry_padding != 1) {
  408. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  409. "Padding wrong size: %ju should be 1 or 0",
  410. (uintmax_t)ar->entry_padding);
  411. return (ARCHIVE_WARN);
  412. }
  413. ret = __archive_write_output(a, "\n", 1);
  414. return (ret);
  415. }
  416. /*
  417. * Format a number into the specified field using base-8.
  418. * NB: This version is slightly different from the one in
  419. * _ustar.c
  420. */
  421. static int
  422. format_octal(int64_t v, char *p, int s)
  423. {
  424. int len;
  425. char *h;
  426. len = s;
  427. h = p;
  428. /* Octal values can't be negative, so use 0. */
  429. if (v < 0) {
  430. while (len-- > 0)
  431. *p++ = '0';
  432. return (-1);
  433. }
  434. p += s; /* Start at the end and work backwards. */
  435. do {
  436. *--p = (char)('0' + (v & 7));
  437. v >>= 3;
  438. } while (--s > 0 && v > 0);
  439. if (v == 0) {
  440. memmove(h, p, len - s);
  441. p = h + len - s;
  442. while (s-- > 0)
  443. *p++ = ' ';
  444. return (0);
  445. }
  446. /* If it overflowed, fill field with max value. */
  447. while (len-- > 0)
  448. *p++ = '7';
  449. return (-1);
  450. }
  451. /*
  452. * Format a number into the specified field using base-10.
  453. */
  454. static int
  455. format_decimal(int64_t v, char *p, int s)
  456. {
  457. int len;
  458. char *h;
  459. len = s;
  460. h = p;
  461. /* Negative values in ar header are meaningless, so use 0. */
  462. if (v < 0) {
  463. while (len-- > 0)
  464. *p++ = '0';
  465. return (-1);
  466. }
  467. p += s;
  468. do {
  469. *--p = (char)('0' + (v % 10));
  470. v /= 10;
  471. } while (--s > 0 && v > 0);
  472. if (v == 0) {
  473. memmove(h, p, len - s);
  474. p = h + len - s;
  475. while (s-- > 0)
  476. *p++ = ' ';
  477. return (0);
  478. }
  479. /* If it overflowed, fill field with max value. */
  480. while (len-- > 0)
  481. *p++ = '9';
  482. return (-1);
  483. }
  484. static const char *
  485. ar_basename(const char *path)
  486. {
  487. const char *endp, *startp;
  488. endp = path + strlen(path) - 1;
  489. /*
  490. * For filename with trailing slash(es), we return
  491. * NULL indicating an error.
  492. */
  493. if (*endp == '/')
  494. return (NULL);
  495. /* Find the start of the base */
  496. startp = endp;
  497. while (startp > path && *(startp - 1) != '/')
  498. startp--;
  499. return (startp);
  500. }