archive_write_set_format_ar.c 15 KB

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