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 unsigned char *buffer;
45 size_t copy_buff_size;
49 static int memory_read_close(struct archive *, void *);
50 static int memory_read_open(struct archive *, void *);
51 #if ARCHIVE_VERSION_NUMBER < 2000000
52 static ssize_t memory_read_skip(struct archive *, void *, size_t request);
54 static off_t memory_read_skip(struct archive *, void *, off_t request);
56 static ssize_t memory_read(struct archive *, void *, const void **buff);
57 static int read_open_memory_internal(struct archive *a, void *buff,
58 size_t size, size_t read_size, int fullapi);
62 read_open_memory(struct archive *a, void *buff, size_t size, size_t read_size)
64 return read_open_memory_internal(a, buff, size, read_size, 1);
68 * As above, but don't register any optional part of the API, to verify
69 * that internals work correctly with just the minimal entry points.
72 read_open_memory2(struct archive *a, void *buff, size_t size, size_t read_size)
74 return read_open_memory_internal(a, buff, size, read_size, 0);
78 read_open_memory_internal(struct archive *a, void *buff,
79 size_t size, size_t read_size, int fullapi)
81 struct read_memory_data *mine;
83 mine = (struct read_memory_data *)malloc(sizeof(*mine));
85 archive_set_error(a, ENOMEM, "No memory");
86 return (ARCHIVE_FATAL);
88 memset(mine, 0, sizeof(*mine));
89 mine->buffer = (unsigned char *)buff;
90 mine->end = mine->buffer + size;
91 mine->read_size = read_size;
92 mine->copy_buff_size = read_size + 64;
93 mine->copy_buff = malloc(mine->copy_buff_size);
95 return (archive_read_open2(a, mine, memory_read_open,
96 memory_read, memory_read_skip, memory_read_close));
98 return (archive_read_open2(a, mine, NULL,
99 memory_read, NULL, memory_read_close));
103 * There's nothing to open.
106 memory_read_open(struct archive *a, void *client_data)
108 (void)a; /* UNUSED */
109 (void)client_data; /* UNUSED */
114 * In order to exercise libarchive's internal read-combining logic,
115 * we deliberately copy data for each read to a separate buffer.
116 * That way, code that runs off the end of the provided data
120 memory_read(struct archive *a, void *client_data, const void **buff)
122 struct read_memory_data *mine = (struct read_memory_data *)client_data;
125 (void)a; /* UNUSED */
126 size = mine->end - mine->buffer;
127 if (size > mine->read_size)
128 size = mine->read_size;
129 memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
130 memcpy(mine->copy_buff, mine->buffer, size);
131 *buff = mine->copy_buff;
133 mine->buffer += size;
134 return ((ssize_t)size);
138 * How mean can a skip() routine be? Let's try to find out.
140 #if ARCHIVE_VERSION_NUMBER < 2000000
142 memory_read_skip(struct archive *a, void *client_data, size_t skip)
145 memory_read_skip(struct archive *a, void *client_data, off_t skip)
148 struct read_memory_data *mine = (struct read_memory_data *)client_data;
150 (void)a; /* UNUSED */
151 /* We can't skip by more than is available. */
152 if ((off_t)skip > (off_t)(mine->end - mine->buffer))
153 skip = mine->end - mine->buffer;
154 /* Always do small skips by prime amounts. */
157 mine->buffer += skip;
162 * Close is just cleaning up our one small bit of data.
165 memory_read_close(struct archive *a, void *client_data)
167 struct read_memory_data *mine = (struct read_memory_data *)client_data;
168 (void)a; /* UNUSED */
169 free(mine->copy_buff);