xref: /freebsd/contrib/libarchive/libarchive/archive_read_open_memory.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
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 "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28 
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "archive.h"
34 
35 /*
36  * Glue to read an archive from a block of memory.
37  *
38  * This is mostly a huge help in building test harnesses;
39  * test programs can build archives in memory and read them
40  * back again without having to mess with files on disk.
41  */
42 
43 struct read_memory_data {
44 	const unsigned char	*start;
45 	const unsigned char	*p;
46 	const unsigned char	*end;
47 	ssize_t	 read_size;
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 offset, 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 
56 int
57 archive_read_open_memory(struct archive *a, const void *buff, size_t size)
58 {
59 	return archive_read_open_memory2(a, buff, size, size);
60 }
61 
62 /*
63  * Don't use _open_memory2() in production code; the archive_read_open_memory()
64  * version is the one you really want.  This is just here so that
65  * test harnesses can exercise block operations inside the library.
66  */
67 int
68 archive_read_open_memory2(struct archive *a, const void *buff,
69     size_t size, size_t read_size)
70 {
71 	struct read_memory_data *mine;
72 
73 	mine = (struct read_memory_data *)malloc(sizeof(*mine));
74 	if (mine == NULL) {
75 		archive_set_error(a, ENOMEM, "No memory");
76 		return (ARCHIVE_FATAL);
77 	}
78 	memset(mine, 0, sizeof(*mine));
79 	mine->start = mine->p = (const unsigned char *)buff;
80 	mine->end = mine->start + size;
81 	mine->read_size = read_size;
82 	archive_read_set_open_callback(a, memory_read_open);
83 	archive_read_set_read_callback(a, memory_read);
84 	archive_read_set_seek_callback(a, memory_read_seek);
85 	archive_read_set_skip_callback(a, memory_read_skip);
86 	archive_read_set_close_callback(a, memory_read_close);
87 	archive_read_set_callback_data(a, mine);
88 	return (archive_read_open1(a));
89 }
90 
91 /*
92  * There's nothing to open.
93  */
94 static int
95 memory_read_open(struct archive *a, void *client_data)
96 {
97 	(void)a; /* UNUSED */
98 	(void)client_data; /* UNUSED */
99 	return (ARCHIVE_OK);
100 }
101 
102 /*
103  * This is scary simple:  Just advance a pointer.  Limiting
104  * to read_size is not technically necessary, but it exercises
105  * more of the internal logic when used with a small block size
106  * in a test harness.  Production use should not specify a block
107  * size; then this is much faster.
108  */
109 static ssize_t
110 memory_read(struct archive *a, void *client_data, const void **buff)
111 {
112 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
113 	ssize_t size;
114 
115 	(void)a; /* UNUSED */
116 	*buff = mine->p;
117 	size = mine->end - mine->p;
118 	if (size > mine->read_size)
119 		size = mine->read_size;
120         mine->p += size;
121 	return (size);
122 }
123 
124 /*
125  * Advancing is just as simple.  Again, this is doing more than
126  * necessary in order to better exercise internal code when used
127  * as a test harness.
128  */
129 static int64_t
130 memory_read_skip(struct archive *a, void *client_data, int64_t skip)
131 {
132 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
133 
134 	(void)a; /* UNUSED */
135 	if ((int64_t)skip > (int64_t)(mine->end - mine->p))
136 		skip = mine->end - mine->p;
137 	/* Round down to block size. */
138 	skip /= mine->read_size;
139 	skip *= mine->read_size;
140 	mine->p += skip;
141 	return (skip);
142 }
143 
144 /*
145  * Seeking.
146  */
147 static int64_t
148 memory_read_seek(struct archive *a, void *client_data, int64_t offset, int whence)
149 {
150 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
151 
152 	(void)a; /* UNUSED */
153 	switch (whence) {
154 	case SEEK_SET:
155 		mine->p = mine->start + offset;
156 		break;
157 	case SEEK_CUR:
158 		mine->p += offset;
159 		break;
160 	case SEEK_END:
161 		mine->p = mine->end + offset;
162 		break;
163 	default:
164 		return ARCHIVE_FATAL;
165 	}
166 	if (mine->p < mine->start) {
167 		mine->p = mine->start;
168 		return ARCHIVE_FAILED;
169 	}
170 	if (mine->p > mine->end) {
171 		mine->p = mine->end;
172 		return ARCHIVE_FAILED;
173 	}
174 	return (mine->p - mine->start);
175 }
176 
177 /*
178  * Close is just cleaning up our one small bit of data.
179  */
180 static int
181 memory_read_close(struct archive *a, void *client_data)
182 {
183 	struct read_memory_data *mine = (struct read_memory_data *)client_data;
184 	(void)a; /* UNUSED */
185 	free(mine);
186 	return (ARCHIVE_OK);
187 }
188