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