xref: /freebsd/contrib/libarchive/libarchive/archive_write_set_format_cpio_newc.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2006 Rudolf Marek SYSGO s.r.o.
4  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
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 #include <stdio.h>
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_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_write_private.h"
46 #include "archive_write_set_format_private.h"
47 
48 static ssize_t	archive_write_newc_data(struct archive_write *,
49 		    const void *buff, size_t s);
50 static int	archive_write_newc_close(struct archive_write *);
51 static int	archive_write_newc_free(struct archive_write *);
52 static int	archive_write_newc_finish_entry(struct archive_write *);
53 static int	archive_write_newc_header(struct archive_write *,
54 		    struct archive_entry *);
55 static int      archive_write_newc_options(struct archive_write *,
56 		    const char *, const char *);
57 static int	format_hex(int64_t, void *, int);
58 static int64_t	format_hex_recursive(int64_t, char *, int);
59 static int	write_header(struct archive_write *, struct archive_entry *);
60 
61 struct cpio {
62 	uint64_t	  entry_bytes_remaining;
63 	int		  padding;
64 
65 	struct archive_string_conv *opt_sconv;
66 	struct archive_string_conv *sconv_default;
67 	int		  init_default_conversion;
68 };
69 
70 #define	c_magic_offset 0
71 #define	c_magic_size 6
72 #define	c_ino_offset 6
73 #define	c_ino_size 8
74 #define	c_mode_offset 14
75 #define	c_mode_size 8
76 #define	c_uid_offset 22
77 #define	c_uid_size 8
78 #define	c_gid_offset 30
79 #define	c_gid_size 8
80 #define	c_nlink_offset 38
81 #define	c_nlink_size 8
82 #define	c_mtime_offset 46
83 #define	c_mtime_size 8
84 #define	c_filesize_offset 54
85 #define	c_filesize_size 8
86 #define	c_devmajor_offset 62
87 #define	c_devmajor_size 8
88 #define	c_devminor_offset 70
89 #define	c_devminor_size 8
90 #define	c_rdevmajor_offset 78
91 #define	c_rdevmajor_size 8
92 #define	c_rdevminor_offset 86
93 #define	c_rdevminor_size 8
94 #define	c_namesize_offset 94
95 #define	c_namesize_size 8
96 #define	c_checksum_offset 102
97 #define	c_checksum_size 8
98 #define	c_header_size 110
99 
100 /* Logic trick: difference between 'n' and next multiple of 4 */
101 #define PAD4(n)	(3 & (1 + ~(n)))
102 
103 /*
104  * Set output format to 'cpio' format.
105  */
106 int
archive_write_set_format_cpio_newc(struct archive * _a)107 archive_write_set_format_cpio_newc(struct archive *_a)
108 {
109 	struct archive_write *a = (struct archive_write *)_a;
110 	struct cpio *cpio;
111 
112 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
113 	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_newc");
114 
115 	/* If someone else was already registered, unregister them. */
116 	(void)__archive_write_unregister_format(a);
117 
118 	cpio = calloc(1, sizeof(*cpio));
119 	if (cpio == NULL) {
120 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
121 		return (ARCHIVE_FATAL);
122 	}
123 	a->format_data = cpio;
124 	a->format_name = "cpio";
125 	a->format_options = archive_write_newc_options;
126 	a->format_write_header = archive_write_newc_header;
127 	a->format_write_data = archive_write_newc_data;
128 	a->format_finish_entry = archive_write_newc_finish_entry;
129 	a->format_close = archive_write_newc_close;
130 	a->format_free = archive_write_newc_free;
131 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
132 	a->archive.archive_format_name = "SVR4 cpio nocrc";
133 	return (ARCHIVE_OK);
134 }
135 
136 static int
archive_write_newc_options(struct archive_write * a,const char * key,const char * val)137 archive_write_newc_options(struct archive_write *a, const char *key,
138     const char *val)
139 {
140 	struct cpio *cpio = a->format_data;
141 	int ret = ARCHIVE_FAILED;
142 
143 	if (strcmp(key, "hdrcharset")  == 0) {
144 		if (val == NULL || val[0] == 0)
145 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
146 			    "%s: hdrcharset option needs a character-set name",
147 			    a->format_name);
148 		else {
149 			cpio->opt_sconv = archive_string_conversion_to_charset(
150 			    &a->archive, val, 0);
151 			if (cpio->opt_sconv != NULL)
152 				ret = ARCHIVE_OK;
153 			else
154 				ret = ARCHIVE_FATAL;
155 		}
156 		return (ret);
157 	}
158 
159 	/* Note: The "warn" return is just to inform the options
160 	 * supervisor that we didn't handle it.  It will generate
161 	 * a suitable error if no one used this option. */
162 	return (ARCHIVE_WARN);
163 }
164 
165 static struct archive_string_conv *
get_sconv(struct archive_write * a)166 get_sconv(struct archive_write *a)
167 {
168 	struct cpio *cpio = a->format_data;
169 	struct archive_string_conv *sconv;
170 
171 	sconv = cpio->opt_sconv;
172 	if (sconv == NULL) {
173 		if (!cpio->init_default_conversion) {
174 			cpio->sconv_default =
175 			    archive_string_default_conversion_for_write(
176 			      &(a->archive));
177 			cpio->init_default_conversion = 1;
178 		}
179 		sconv = cpio->sconv_default;
180 	}
181 	return (sconv);
182 }
183 
184 static int
archive_write_newc_header(struct archive_write * a,struct archive_entry * entry)185 archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
186 {
187 	const char *path;
188 	size_t len;
189 
190 	if (archive_entry_filetype(entry) == 0 && archive_entry_hardlink(entry) == NULL) {
191 		archive_set_error(&a->archive, -1, "Filetype required");
192 		return (ARCHIVE_FAILED);
193 	}
194 
195 	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
196 	    && errno == ENOMEM) {
197 		archive_set_error(&a->archive, ENOMEM,
198 		    "Can't allocate memory for Pathname");
199 		return (ARCHIVE_FATAL);
200 	}
201 	if (len == 0 || path == NULL || path[0] == '\0') {
202 		archive_set_error(&a->archive, -1, "Pathname required");
203 		return (ARCHIVE_FAILED);
204 	}
205 
206 	if (archive_entry_hardlink(entry) == NULL
207 	    && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
208 		archive_set_error(&a->archive, -1, "Size required");
209 		return (ARCHIVE_FAILED);
210 	}
211 	return write_header(a, entry);
212 }
213 
214 static int
write_header(struct archive_write * a,struct archive_entry * entry)215 write_header(struct archive_write *a, struct archive_entry *entry)
216 {
217 	struct cpio *cpio = a->format_data;
218 	int64_t ino;
219 	const char *p, *path;
220 	int ret, ret_final;
221 	char h[c_header_size];
222 	struct archive_string_conv *sconv;
223 	struct archive_entry *entry_main;
224 	size_t len, pathlength;
225 	int pad;
226 
227 	ret_final = ARCHIVE_OK;
228 	sconv = get_sconv(a);
229 
230 #if defined(_WIN32) && !defined(__CYGWIN__)
231 	/* Make sure the path separators in pathname, hardlink and symlink
232 	 * are all slash '/', not the Windows path separator '\'. */
233 	entry_main = __la_win_entry_in_posix_pathseparator(entry);
234 	if (entry_main == NULL) {
235 		archive_set_error(&a->archive, ENOMEM,
236 		    "Can't allocate ustar data");
237 		return(ARCHIVE_FATAL);
238 	}
239 	if (entry != entry_main)
240 		entry = entry_main;
241 	else
242 		entry_main = NULL;
243 #else
244 	entry_main = NULL;
245 #endif
246 
247 	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
248 	if (ret != 0) {
249 		if (errno == ENOMEM) {
250 			archive_set_error(&a->archive, ENOMEM,
251 			    "Can't allocate memory for Pathname");
252 			ret_final = ARCHIVE_FATAL;
253 			goto exit_write_header;
254 		}
255 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
256 		    "Can't translate pathname '%s' to %s",
257 		    archive_entry_pathname(entry),
258 		    archive_string_conversion_charset_name(sconv));
259 		ret_final = ARCHIVE_WARN;
260 	}
261 	pathlength = len + 1; /* Include trailing null. */
262 
263 	memset(h, 0, c_header_size);
264 	format_hex(0x070701, h + c_magic_offset, c_magic_size);
265 	format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
266 	    c_devmajor_size);
267 	format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
268 	    c_devminor_size);
269 
270 	ino = archive_entry_ino64(entry);
271 	if (ino > 0xffffffff) {
272 		archive_set_error(&a->archive, ERANGE,
273 		    "large inode number truncated");
274 		ret_final = ARCHIVE_WARN;
275 	}
276 
277 	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
278 	format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
279 	format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
280 	format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
281 	format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
282 	format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
283 	if (archive_entry_filetype(entry) == AE_IFBLK
284 	    || archive_entry_filetype(entry) == AE_IFCHR) {
285 	    format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
286 	    format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
287 	} else {
288 	    format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
289 	    format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
290 	}
291 	format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
292 	if (format_hex(pathlength, h + c_namesize_offset, c_namesize_size)) {
293 		archive_set_error(&a->archive, ERANGE,
294 		    "Filename is too long for cpio format");
295 		ret_final = ARCHIVE_FAILED;
296 		goto exit_write_header;
297 	}
298 	format_hex(0, h + c_checksum_offset, c_checksum_size);
299 
300 	/* Non-regular files don't store bodies. */
301 	if (archive_entry_filetype(entry) != AE_IFREG)
302 		archive_entry_set_size(entry, 0);
303 
304 	/* Symlinks get the link written as the body of the entry. */
305 	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
306 	if (ret != 0) {
307 		if (errno == ENOMEM) {
308 			archive_set_error(&a->archive, ENOMEM,
309 			    "Can't allocate memory for Likname");
310 			ret_final = ARCHIVE_FATAL;
311 			goto exit_write_header;
312 		}
313 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
314 		    "Can't translate linkname '%s' to %s",
315 		    archive_entry_symlink(entry),
316 		    archive_string_conversion_charset_name(sconv));
317 		ret_final = ARCHIVE_WARN;
318 	}
319 	if (len > 0 && p != NULL  &&  *p != '\0')
320 		ret = format_hex(strlen(p), h + c_filesize_offset,
321 		    c_filesize_size);
322 	else
323 		ret = format_hex(archive_entry_size(entry),
324 		    h + c_filesize_offset, c_filesize_size);
325 	if (ret) {
326 		archive_set_error(&a->archive, ERANGE,
327 		    "File is too large for this format");
328 		ret_final = ARCHIVE_FAILED;
329 		goto exit_write_header;
330 	}
331 
332 	ret = __archive_write_output(a, h, c_header_size);
333 	if (ret != ARCHIVE_OK) {
334 		ret_final = ARCHIVE_FATAL;
335 		goto exit_write_header;
336 	}
337 
338 	/* Pad pathname to even length. */
339 	ret = __archive_write_output(a, path, pathlength);
340 	if (ret != ARCHIVE_OK) {
341 		ret_final = ARCHIVE_FATAL;
342 		goto exit_write_header;
343 	}
344 	pad = PAD4(pathlength + c_header_size);
345 	if (pad) {
346 		ret = __archive_write_output(a, "\0\0\0", pad);
347 		if (ret != ARCHIVE_OK) {
348 			ret_final = ARCHIVE_FATAL;
349 			goto exit_write_header;
350 		}
351 	}
352 
353 	cpio->entry_bytes_remaining = archive_entry_size(entry);
354 	cpio->padding = (int)PAD4(cpio->entry_bytes_remaining);
355 
356 	/* Write the symlink now. */
357 	if (p != NULL  &&  *p != '\0') {
358 		ret = __archive_write_output(a, p, strlen(p));
359 		if (ret != ARCHIVE_OK) {
360 			ret_final = ARCHIVE_FATAL;
361 			goto exit_write_header;
362 		}
363 		pad = PAD4(strlen(p));
364 		ret = __archive_write_output(a, "\0\0\0", pad);
365 		if (ret != ARCHIVE_OK) {
366 			ret_final = ARCHIVE_FATAL;
367 			goto exit_write_header;
368 		}
369 	}
370 exit_write_header:
371 	archive_entry_free(entry_main);
372 	return (ret_final);
373 }
374 
375 static ssize_t
archive_write_newc_data(struct archive_write * a,const void * buff,size_t s)376 archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
377 {
378 	struct cpio *cpio = a->format_data;
379 	int ret;
380 
381 	if (s > cpio->entry_bytes_remaining)
382 		s = (size_t)cpio->entry_bytes_remaining;
383 
384 	ret = __archive_write_output(a, buff, s);
385 	cpio->entry_bytes_remaining -= s;
386 	if (ret >= 0)
387 		return (s);
388 	else
389 		return (ret);
390 }
391 
392 /*
393  * Format a number into the specified field.
394  */
395 static int
format_hex(int64_t v,void * p,int digits)396 format_hex(int64_t v, void *p, int digits)
397 {
398 	int64_t	max;
399 	int	ret;
400 
401 	max = (((int64_t)1) << (digits * 4)) - 1;
402 	if (v >= 0  &&  v <= max) {
403 	    format_hex_recursive(v, (char *)p, digits);
404 	    ret = 0;
405 	} else {
406 	    format_hex_recursive(max, (char *)p, digits);
407 	    ret = -1;
408 	}
409 	return (ret);
410 }
411 
412 static int64_t
format_hex_recursive(int64_t v,char * p,int s)413 format_hex_recursive(int64_t v, char *p, int s)
414 {
415 	if (s == 0)
416 		return (v);
417 	v = format_hex_recursive(v, p+1, s-1);
418 	*p = "0123456789abcdef"[v & 0xf];
419 	return (v >> 4);
420 }
421 
422 static int
archive_write_newc_close(struct archive_write * a)423 archive_write_newc_close(struct archive_write *a)
424 {
425 	int er;
426 	struct archive_entry *trailer;
427 
428 	trailer = archive_entry_new();
429 	if (trailer == NULL)
430 		return ARCHIVE_FATAL;
431 	archive_entry_set_nlink(trailer, 1);
432 	archive_entry_set_size(trailer, 0);
433 	archive_entry_set_pathname(trailer, "TRAILER!!!");
434 	/* Bypass the required data checks. */
435 	er = write_header(a, trailer);
436 	archive_entry_free(trailer);
437 	return (er);
438 }
439 
440 static int
archive_write_newc_free(struct archive_write * a)441 archive_write_newc_free(struct archive_write *a)
442 {
443 	struct cpio *cpio = a->format_data;
444 
445 	free(cpio);
446 	a->format_data = NULL;
447 	return (ARCHIVE_OK);
448 }
449 
450 static int
archive_write_newc_finish_entry(struct archive_write * a)451 archive_write_newc_finish_entry(struct archive_write *a)
452 {
453 	struct cpio *cpio = a->format_data;
454 
455 	return (__archive_write_nulls(a,
456 	    cpio->entry_bytes_remaining + cpio->padding));
457 }
458