archive_write_set_format_ar.c 14 KB

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