2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 __FBSDID("$FreeBSD$");
34 * Read an archive from a block of memory.
36 * This is identical to archive_read_open_memory(), except
37 * that it goes out of its way to be a little bit unpleasant,
38 * in order to better test the libarchive internals.
41 struct read_memory_data {
42 const unsigned char *start;
43 const unsigned char *p;
44 const unsigned char *end;
46 size_t copy_buff_size;
47 size_t copy_buff_offset;
51 static int memory_read_close(struct archive *, void *);
52 static int memory_read_open(struct archive *, void *);
53 static int64_t memory_read_seek(struct archive *, void *, int64_t request, int whence);
54 static int64_t memory_read_skip(struct archive *, void *, int64_t request);
55 static ssize_t memory_read(struct archive *, void *, const void **buff);
56 static int read_open_memory_internal(struct archive *a, const void *buff,
57 size_t size, size_t read_size, int fullapi);
61 read_open_memory(struct archive *a, const void *buff, size_t size, size_t read_size)
63 return read_open_memory_internal(a, buff, size, read_size, 2);
67 * As above, but don't register any optional part of the API, to verify
68 * that internals work correctly with just the minimal entry points.
71 read_open_memory_minimal(struct archive *a, const void *buff, size_t size, size_t read_size)
73 return read_open_memory_internal(a, buff, size, read_size, 1);
77 * Include a seek callback as well.
80 read_open_memory_seek(struct archive *a, const void *buff, size_t size, size_t read_size)
82 return read_open_memory_internal(a, buff, size, read_size, 3);
86 read_open_memory_internal(struct archive *a, const void *buff,
87 size_t size, size_t read_size, int level)
89 struct read_memory_data *mine = NULL;
93 archive_read_set_seek_callback(a, memory_read_seek);
95 archive_read_set_open_callback(a, memory_read_open);
96 archive_read_set_skip_callback(a, memory_read_skip);
98 mine = malloc(sizeof(*mine));
100 archive_set_error(a, ENOMEM, "No memory");
101 return (ARCHIVE_FATAL);
103 memset(mine, 0, sizeof(*mine));
104 mine->start = mine->p = (const unsigned char *)buff;
105 mine->end = mine->start + size;
106 mine->read_size = read_size;
107 mine->copy_buff_offset = 32;
108 mine->copy_buff_size = read_size + mine->copy_buff_offset * 2;
109 mine->copy_buff = malloc(mine->copy_buff_size);
110 memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
112 archive_read_set_read_callback(a, memory_read);
113 archive_read_set_close_callback(a, memory_read_close);
114 archive_read_set_callback_data(a, mine);
116 return archive_read_open1(a);
120 * There's nothing to open.
123 memory_read_open(struct archive *a, void *client_data)
125 (void)a; /* UNUSED */
126 (void)client_data; /* UNUSED */
131 * In order to exercise libarchive's internal read-combining logic,
132 * we deliberately copy data for each read to a separate buffer.
133 * That way, code that runs off the end of the provided data
137 memory_read(struct archive *a, void *client_data, const void **buff)
139 struct read_memory_data *mine = (struct read_memory_data *)client_data;
142 (void)a; /* UNUSED */
143 size = mine->end - mine->p;
148 if ((size_t)size > mine->read_size)
149 size = mine->read_size;
151 memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
152 memcpy(mine->copy_buff + mine->copy_buff_offset, mine->p, size);
153 *buff = mine->copy_buff + mine->copy_buff_offset;
156 return ((ssize_t)size);
160 * How mean can a skip() routine be? Let's try to find out.
163 memory_read_skip(struct archive *a, void *client_data, int64_t skip)
165 struct read_memory_data *mine = (struct read_memory_data *)client_data;
167 (void)a; /* UNUSED */
168 /* We can't skip by more than is available. */
169 if ((off_t)skip > (off_t)(mine->end - mine->p))
170 skip = mine->end - mine->p;
171 /* Always do small skips by prime amounts. */
181 memory_read_seek(struct archive *a, void *client_data, int64_t offset, int whence)
183 struct read_memory_data *mine = (struct read_memory_data *)client_data;
185 (void)a; /* UNUSED */
188 mine->p = mine->start + offset;
191 mine->p = mine->end + offset;
197 if (mine->p < mine->start) {
198 mine->p = mine->start;
199 return ARCHIVE_FAILED;
201 if (mine->p > mine->end) {
203 return ARCHIVE_FAILED;
205 return (mine->p - mine->start);
209 * Close is just cleaning up our one small bit of data.
212 memory_read_close(struct archive *a, void *client_data)
214 struct read_memory_data *mine = (struct read_memory_data *)client_data;
215 (void)a; /* UNUSED */
217 free(mine->copy_buff);