1 /* @(#)movearch.c	1.36 20/07/08 Copyright 1993, 1995, 2001-2020 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)movearch.c	1.36 20/07/08 Copyright 1993, 1995, 2001-2020 J. Schilling";
6 #endif
7 /*
8  *	Handle non-file type data that needs to be moved from/to the archive.
9  *
10  *	Copyright (c) 1993, 1995, 2001-2020 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include <schily/standard.h>
27 #include <schily/types.h>
28 #include <schily/string.h>
29 #include <schily/schily.h>
30 #include "star.h"
31 #include "props.h"
32 #include "table.h"
33 #include "starsubs.h"
34 #include "movearch.h"
35 
36 EXPORT	ssize_t	move_from_arch	__PR((move_t *move, char *p, size_t amount));
37 EXPORT	ssize_t	move_to_arch	__PR((move_t *move, char *p, size_t amount));
38 
39 
40 /*
41  * Move data from archive.
42  */
43 EXPORT ssize_t
move_from_arch(move,p,amount)44 move_from_arch(move, p, amount)
45 	move_t	*move;
46 	char	*p;
47 	size_t	amount;
48 {
49 	movebytes(p, move->m_data, amount);
50 	move->m_data += amount;
51 	/*
52 	 * If we make sure that the buffer holds at least one character more
53 	 * than needed, then we may safely add another null byte at the end of
54 	 * the extracted buffer.
55 	 * This makes sure, that a buggy tar implementation which wight archive
56 	 * non null-terminated long filenames with 'L' or 'K' filetype may
57 	 * not cause us to core dump.
58 	 * It is needed when extracting extended attribute buffers from
59 	 * POSIX.1-2001 archives as POSIX.1-2001 makes the buffer '\n' but not
60 	 * null-terminated.
61 	 */
62 	move->m_data[0] = '\0';
63 	return (amount);
64 }
65 
66 /*
67  * Move data to archive.
68  */
69 EXPORT ssize_t
move_to_arch(move,p,amount)70 move_to_arch(move, p, amount)
71 	move_t	*move;
72 	char	*p;
73 	size_t	amount;
74 {
75 	if (amount > move->m_size)
76 		amount = move->m_size;
77 	movebytes(move->m_data, p, amount);
78 	move->m_data += amount;
79 	move->m_size -= amount;
80 	if (move->m_flags & MF_ADDSLASH) {
81 		if (move->m_size == 1) {
82 			p[amount-1] = '/';
83 		} else if (move->m_size == 0) {
84 			if (amount > 1)
85 				p[amount-2] = '/';
86 			p[amount-1] = '\0';
87 		}
88 	}
89 	return (amount);
90 }
91