xref: /freebsd/contrib/libarchive/libarchive/test/read_open_memory.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "test.h"
27 
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 /*
33  * Read an archive from a block of memory.
34  *
35  * This is identical to archive_read_open_memory(), except
36  * that it goes out of its way to be a little bit unpleasant,
37  * in order to better test the libarchive internals.
38  */
39 
40 struct read_memory_data {
41 	const unsigned char	*start;
42 	const unsigned char	*p;
43 	const unsigned char	*end;
44 	size_t	 read_size;
45 	size_t copy_buff_size;
46 	size_t copy_buff_offset;
47 	char *copy_buff;
48 };
49 
50 static int	memory_read_close(struct archive *, void *);
51 static int	memory_read_open(struct archive *, void *);
52 static int64_t	memory_read_seek(struct archive *, void *, int64_t request, int whence);
53 static int64_t	memory_read_skip(struct archive *, void *, int64_t request);
54 static ssize_t	memory_read(struct archive *, void *, const void **buff);
55 static int	read_open_memory_internal(struct archive *a, const void *buff,
56     size_t size, size_t read_size, int fullapi);
57 
58 
59 int
read_open_memory(struct archive * a,const void * buff,size_t size,size_t read_size)60 read_open_memory(struct archive *a, const void *buff, size_t size, size_t read_size)
61 {
62 	return read_open_memory_internal(a, buff, size, read_size, 2);
63 }
64 
65 /*
66  * As above, but don't register any optional part of the API, to verify
67  * that internals work correctly with just the minimal entry points.
68  */
69 int
read_open_memory_minimal(struct archive * a,const void * buff,size_t size,size_t read_size)70 read_open_memory_minimal(struct archive *a, const void *buff, size_t size, size_t read_size)
71 {
72 	return read_open_memory_internal(a, buff, size, read_size, 1);
73 }
74 
75 /*
76  * Include a seek callback as well.
77  */
78 int
read_open_memory_seek(struct archive * a,const void * buff,size_t size,size_t read_size)79 read_open_memory_seek(struct archive *a, const void *buff, size_t size, size_t read_size)
80 {
81 	return read_open_memory_internal(a, buff, size, read_size, 3);
82 }
83 
84 static int
read_open_memory_internal(struct archive * a,const void * buff,size_t size,size_t read_size,int level)85 read_open_memory_internal(struct archive *a, const void *buff,
86     size_t size, size_t read_size, int level)
87 {
88 	struct read_memory_data *mine = NULL;
89 	int r;
90 
91 	switch (level) {
92 	case 3:
93 		archive_read_set_seek_callback(a, memory_read_seek);
94 		__LA_FALLTHROUGH;
95 	case 2:
96 		archive_read_set_open_callback(a, memory_read_open);
97 		archive_read_set_skip_callback(a, memory_read_skip);
98 		__LA_FALLTHROUGH;
99 	case 1:
100 		mine = malloc(sizeof(*mine));
101 		if (mine == NULL) {
102 			archive_set_error(a, ENOMEM, "No memory");
103 			return (ARCHIVE_FATAL);
104 		}
105 		memset(mine, 0, sizeof(*mine));
106 		mine->start = mine->p = (const unsigned char *)buff;
107 		mine->end = mine->start + size;
108 		mine->read_size = read_size;
109 		mine->copy_buff_offset = 32;
110 		mine->copy_buff_size = read_size + mine->copy_buff_offset * 2;
111 		mine->copy_buff = malloc(mine->copy_buff_size);
112 		memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
113 
114 		archive_read_set_read_callback(a, memory_read);
115 		archive_read_set_close_callback(a, memory_read_close);
116 		r = archive_read_set_callback_data(a, mine);
117 		if (r < 0)
118 			return (r);
119 		__LA_FALLTHROUGH;
120 	default:
121 		break;
122 	}
123 	return archive_read_open1(a);
124 }
125 
126 /*
127  * There's nothing to open.
128  */
129 static int
memory_read_open(struct archive * a,void * client_data)130 memory_read_open(struct archive *a, void *client_data)
131 {
132 	(void)a; /* UNUSED */
133 	(void)client_data; /* UNUSED */
134 	return (ARCHIVE_OK);
135 }
136 
137 /*
138  * In order to exercise libarchive's internal read-combining logic,
139  * we deliberately copy data for each read to a separate buffer.
140  * That way, code that runs off the end of the provided data
141  * will screw up.
142  */
143 static ssize_t
memory_read(struct archive * a,void * client_data,const void ** buff)144 memory_read(struct archive *a, void *client_data, const void **buff)
145 {
146 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
147 	ssize_t size;
148 
149 	(void)a; /* UNUSED */
150 	size = mine->end - mine->p;
151 	if (size < 0) {
152 		buff = NULL;
153 		return 0;
154 	}
155 	if ((size_t)size > mine->read_size)
156 		size = mine->read_size;
157 	else
158 		memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
159 	memcpy(mine->copy_buff + mine->copy_buff_offset, mine->p, size);
160 	*buff = mine->copy_buff + mine->copy_buff_offset;
161 
162         mine->p += size;
163 	return ((ssize_t)size);
164 }
165 
166 /*
167  * How mean can a skip() routine be?  Let's try to find out.
168  */
169 static int64_t
memory_read_skip(struct archive * a,void * client_data,int64_t skip)170 memory_read_skip(struct archive *a, void *client_data, int64_t skip)
171 {
172 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
173 
174 	(void)a; /* UNUSED */
175 	/* We can't skip by more than is available. */
176 	if (skip > mine->end - mine->p)
177 		skip = mine->end - mine->p;
178 	/* Always do small skips by prime amounts. */
179 	if (skip > 71)
180 		skip = 71;
181 	mine->p += skip;
182 	return (skip);
183 }
184 
185 /*
186  */
187 static int64_t
memory_read_seek(struct archive * a,void * client_data,int64_t offset,int whence)188 memory_read_seek(struct archive *a, void *client_data, int64_t offset, int whence)
189 {
190 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
191 
192 	(void)a; /* UNUSED */
193 	switch (whence) {
194 	case SEEK_SET:
195 		mine->p = mine->start + offset;
196 		break;
197 	case SEEK_END:
198 		mine->p = mine->end + offset;
199 		break;
200 	case SEEK_CUR:
201 		mine->p += offset;
202 		break;
203 	}
204 	if (mine->p < mine->start) {
205 		mine->p = mine->start;
206 		return ARCHIVE_FAILED;
207 	}
208 	if (mine->p > mine->end) {
209 		mine->p = mine->end;
210 		return ARCHIVE_FAILED;
211 	}
212 	return (mine->p - mine->start);
213 }
214 
215 /*
216  * Close is just cleaning up our one small bit of data.
217  */
218 static int
memory_read_close(struct archive * a,void * client_data)219 memory_read_close(struct archive *a, void *client_data)
220 {
221 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
222 	(void)a; /* UNUSED */
223 	if (mine != NULL)
224 		free(mine->copy_buff);
225 	free(mine);
226 	return (ARCHIVE_OK);
227 }
228