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