2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_ar.c 201108 2009-12-28 03:28:21Z kientzle $");
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
47 uint64_t entry_bytes_remaining;
48 uint64_t entry_padding;
51 char wrote_global_header;
56 * Define structure of the "ar" header.
58 #define AR_name_offset 0
59 #define AR_name_size 16
60 #define AR_date_offset 16
61 #define AR_date_size 12
62 #define AR_uid_offset 28
64 #define AR_gid_offset 34
66 #define AR_mode_offset 40
67 #define AR_mode_size 8
68 #define AR_size_offset 48
69 #define AR_size_size 10
70 #define AR_fmag_offset 58
71 #define AR_fmag_size 2
73 static int archive_write_set_format_ar(struct archive_write *);
74 static int archive_write_ar_header(struct archive_write *,
75 struct archive_entry *);
76 static ssize_t archive_write_ar_data(struct archive_write *,
77 const void *buff, size_t s);
78 static int archive_write_ar_free(struct archive_write *);
79 static int archive_write_ar_close(struct archive_write *);
80 static int archive_write_ar_finish_entry(struct archive_write *);
81 static const char *ar_basename(const char *path);
82 static int format_octal(int64_t v, char *p, int s);
83 static int format_decimal(int64_t v, char *p, int s);
86 archive_write_set_format_ar_bsd(struct archive *_a)
88 struct archive_write *a = (struct archive_write *)_a;
91 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
92 ARCHIVE_STATE_NEW, "archive_write_set_format_ar_bsd");
93 r = archive_write_set_format_ar(a);
94 if (r == ARCHIVE_OK) {
95 a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
96 a->archive.archive_format_name = "ar (BSD)";
102 archive_write_set_format_ar_svr4(struct archive *_a)
104 struct archive_write *a = (struct archive_write *)_a;
107 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
108 ARCHIVE_STATE_NEW, "archive_write_set_format_ar_svr4");
109 r = archive_write_set_format_ar(a);
110 if (r == ARCHIVE_OK) {
111 a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
112 a->archive.archive_format_name = "ar (GNU/SVR4)";
118 * Generic initialization.
121 archive_write_set_format_ar(struct archive_write *a)
125 /* If someone else was already registered, unregister them. */
126 if (a->format_free != NULL)
129 ar = (struct ar_w *)calloc(1, sizeof(*ar));
131 archive_set_error(&a->archive, ENOMEM, "Can't allocate ar data");
132 return (ARCHIVE_FATAL);
136 a->format_name = "ar";
137 a->format_write_header = archive_write_ar_header;
138 a->format_write_data = archive_write_ar_data;
139 a->format_close = archive_write_ar_close;
140 a->format_free = archive_write_ar_free;
141 a->format_finish_entry = archive_write_ar_finish_entry;
146 archive_write_ar_header(struct archive_write *a, struct archive_entry *entry)
152 const char *pathname;
153 const char *filename;
157 ar = (struct ar_w *)a->format_data;
160 size = archive_entry_size(entry);
164 * Reject files with empty name.
166 pathname = archive_entry_pathname(entry);
167 if (pathname == NULL || *pathname == '\0') {
168 archive_set_error(&a->archive, EINVAL,
170 return (ARCHIVE_WARN);
174 * If we are now at the beginning of the archive,
175 * we need first write the ar global header.
177 if (!ar->wrote_global_header) {
178 __archive_write_output(a, "!<arch>\n", 8);
179 ar->wrote_global_header = 1;
182 memset(buff, ' ', 60);
183 memcpy(&buff[AR_fmag_offset], "`\n", 2);
185 if (strcmp(pathname, "/") == 0 ) {
186 /* Entry is archive symbol table in GNU format */
187 buff[AR_name_offset] = '/';
190 if (strcmp(pathname, "__.SYMDEF") == 0) {
191 /* Entry is archive symbol table in BSD format */
192 memcpy(buff + AR_name_offset, "__.SYMDEF", 9);
195 if (strcmp(pathname, "//") == 0) {
197 * Entry is archive filename table, inform that we should
198 * collect strtab in next _data call.
201 buff[AR_name_offset] = buff[AR_name_offset + 1] = '/';
203 * For archive string table, only ar_size field should
210 * Otherwise, entry is a normal archive member.
211 * Strip leading paths from filenames, if any.
213 if ((filename = ar_basename(pathname)) == NULL) {
214 /* Reject filenames with trailing "/" */
215 archive_set_error(&a->archive, EINVAL,
217 return (ARCHIVE_WARN);
220 if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU) {
222 * SVR4/GNU variant use a "/" to mark then end of the filename,
223 * make it possible to have embedded spaces in the filename.
224 * So, the longest filename here (without extension) is
227 if (strlen(filename) <= 15) {
228 memcpy(&buff[AR_name_offset],
229 filename, strlen(filename));
230 buff[AR_name_offset + strlen(filename)] = '/';
233 * For filename longer than 15 bytes, GNU variant
234 * makes use of a string table and instead stores the
235 * offset of the real filename to in the ar_name field.
236 * The string table should have been written before.
238 if (ar->has_strtab <= 0) {
239 archive_set_error(&a->archive, EINVAL,
240 "Can't find string table");
241 return (ARCHIVE_WARN);
244 se = (char *)malloc(strlen(filename) + 3);
246 archive_set_error(&a->archive, ENOMEM,
247 "Can't allocate filename buffer");
248 return (ARCHIVE_FATAL);
251 memcpy(se, filename, strlen(filename));
252 strcpy(se + strlen(filename), "/\n");
254 ss = strstr(ar->strtab, se);
258 archive_set_error(&a->archive, EINVAL,
259 "Invalid string table");
260 return (ARCHIVE_WARN);
264 * GNU variant puts "/" followed by digits into
265 * ar_name field. These digits indicates the real
266 * filename string's offset to the string table.
268 buff[AR_name_offset] = '/';
269 if (format_decimal(ss - ar->strtab,
270 buff + AR_name_offset + 1,
272 archive_set_error(&a->archive, ERANGE,
273 "string table offset too large");
274 return (ARCHIVE_WARN);
277 } else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD) {
279 * BSD variant: for any file name which is more than
280 * 16 chars or contains one or more embedded space(s), the
281 * string "#1/" followed by the ASCII length of the name is
282 * put into the ar_name field. The file size (stored in the
283 * ar_size field) is incremented by the length of the name.
284 * The name is then written immediately following the
287 if (strlen(filename) <= 16 && strchr(filename, ' ') == NULL) {
288 memcpy(&buff[AR_name_offset], filename, strlen(filename));
289 buff[AR_name_offset + strlen(filename)] = ' ';
292 memcpy(buff + AR_name_offset, "#1/", 3);
293 if (format_decimal(strlen(filename),
294 buff + AR_name_offset + 3,
296 archive_set_error(&a->archive, ERANGE,
297 "File name too long");
298 return (ARCHIVE_WARN);
301 size += strlen(filename);
306 if (format_decimal(archive_entry_mtime(entry), buff + AR_date_offset, AR_date_size)) {
307 archive_set_error(&a->archive, ERANGE,
308 "File modification time too large");
309 return (ARCHIVE_WARN);
311 if (format_decimal(archive_entry_uid(entry), buff + AR_uid_offset, AR_uid_size)) {
312 archive_set_error(&a->archive, ERANGE,
313 "Numeric user ID too large");
314 return (ARCHIVE_WARN);
316 if (format_decimal(archive_entry_gid(entry), buff + AR_gid_offset, AR_gid_size)) {
317 archive_set_error(&a->archive, ERANGE,
318 "Numeric group ID too large");
319 return (ARCHIVE_WARN);
321 if (format_octal(archive_entry_mode(entry), buff + AR_mode_offset, AR_mode_size)) {
322 archive_set_error(&a->archive, ERANGE,
323 "Numeric mode too large");
324 return (ARCHIVE_WARN);
327 * Sanity Check: A non-pseudo archive member should always be
330 if (filename != NULL && archive_entry_filetype(entry) != AE_IFREG) {
331 archive_set_error(&a->archive, EINVAL,
332 "Regular file required for non-pseudo member");
333 return (ARCHIVE_WARN);
337 if (format_decimal(size, buff + AR_size_offset, AR_size_size)) {
338 archive_set_error(&a->archive, ERANGE,
339 "File size out of range");
340 return (ARCHIVE_WARN);
343 ret = __archive_write_output(a, buff, 60);
344 if (ret != ARCHIVE_OK)
347 ar->entry_bytes_remaining = size;
348 ar->entry_padding = ar->entry_bytes_remaining % 2;
351 ret = __archive_write_output(a, filename, strlen(filename));
352 if (ret != ARCHIVE_OK)
354 ar->entry_bytes_remaining -= strlen(filename);
361 archive_write_ar_data(struct archive_write *a, const void *buff, size_t s)
366 ar = (struct ar_w *)a->format_data;
367 if (s > ar->entry_bytes_remaining)
368 s = (size_t)ar->entry_bytes_remaining;
370 if (ar->is_strtab > 0) {
371 if (ar->has_strtab > 0) {
372 archive_set_error(&a->archive, EINVAL,
373 "More than one string tables exist");
374 return (ARCHIVE_WARN);
377 ar->strtab = (char *)malloc(s + 1);
378 if (ar->strtab == NULL) {
379 archive_set_error(&a->archive, ENOMEM,
380 "Can't allocate strtab buffer");
381 return (ARCHIVE_FATAL);
383 memcpy(ar->strtab, buff, s);
384 ar->strtab[s] = '\0';
388 ret = __archive_write_output(a, buff, s);
389 if (ret != ARCHIVE_OK)
392 ar->entry_bytes_remaining -= s;
397 archive_write_ar_free(struct archive_write *a)
401 ar = (struct ar_w *)a->format_data;
406 if (ar->has_strtab > 0) {
412 a->format_data = NULL;
417 archive_write_ar_close(struct archive_write *a)
423 * If we haven't written anything yet, we need to write
424 * the ar global header now to make it a valid ar archive.
426 ar = (struct ar_w *)a->format_data;
427 if (!ar->wrote_global_header) {
428 ar->wrote_global_header = 1;
429 ret = __archive_write_output(a, "!<arch>\n", 8);
437 archive_write_ar_finish_entry(struct archive_write *a)
442 ar = (struct ar_w *)a->format_data;
444 if (ar->entry_bytes_remaining != 0) {
445 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
446 "Entry remaining bytes larger than 0");
447 return (ARCHIVE_WARN);
450 if (ar->entry_padding == 0) {
454 if (ar->entry_padding != 1) {
455 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
456 "Padding wrong size: %ju should be 1 or 0",
457 (uintmax_t)ar->entry_padding);
458 return (ARCHIVE_WARN);
461 ret = __archive_write_output(a, "\n", 1);
466 * Format a number into the specified field using base-8.
467 * NB: This version is slightly different from the one in
471 format_octal(int64_t v, char *p, int s)
479 /* Octal values can't be negative, so use 0. */
486 p += s; /* Start at the end and work backwards. */
488 *--p = (char)('0' + (v & 7));
490 } while (--s > 0 && v > 0);
493 memmove(h, p, len - s);
499 /* If it overflowed, fill field with max value. */
507 * Format a number into the specified field using base-10.
510 format_decimal(int64_t v, char *p, int s)
518 /* Negative values in ar header are meaningless, so use 0. */
527 *--p = (char)('0' + (v % 10));
529 } while (--s > 0 && v > 0);
532 memmove(h, p, len - s);
538 /* If it overflowed, fill field with max value. */
546 ar_basename(const char *path)
548 const char *endp, *startp;
550 endp = path + strlen(path) - 1;
552 * For filename with trailing slash(es), we return
553 * NULL indicating an error.
558 /* Find the start of the base */
560 while (startp > path && *(startp - 1) != '/')