compile fixes
[freebsd-src/fkvm-freebsd.git] / lib / libarchive / test / read_open_memory.c
blobfac9b23490f8a8684f188430d76c0697acc28f5e
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "test.h"
27 __FBSDID("$FreeBSD$");
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
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;
43 unsigned char *end;
44 size_t read_size;
45 size_t copy_buff_size;
46 char *copy_buff;
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);
53 #else
54 static off_t memory_read_skip(struct archive *, void *, off_t request);
55 #endif
56 static ssize_t memory_read(struct archive *, void *, const void **buff);
58 int
59 read_open_memory(struct archive *a, void *buff, size_t size, size_t read_size)
61 struct read_memory_data *mine;
63 mine = (struct read_memory_data *)malloc(sizeof(*mine));
64 if (mine == NULL) {
65 archive_set_error(a, ENOMEM, "No memory");
66 return (ARCHIVE_FATAL);
68 memset(mine, 0, sizeof(*mine));
69 mine->buffer = (unsigned char *)buff;
70 mine->end = mine->buffer + size;
71 mine->read_size = read_size;
72 mine->copy_buff_size = read_size + 64;
73 mine->copy_buff = malloc(mine->copy_buff_size);
74 return (archive_read_open2(a, mine, memory_read_open,
75 memory_read, memory_read_skip, memory_read_close));
79 * There's nothing to open.
81 static int
82 memory_read_open(struct archive *a, void *client_data)
84 (void)a; /* UNUSED */
85 (void)client_data; /* UNUSED */
86 return (ARCHIVE_OK);
90 * In order to exercise libarchive's internal read-combining logic,
91 * we deliberately copy data for each read to a separate buffer.
92 * That way, code that runs off the end of the provided data
93 * will screw up.
95 static ssize_t
96 memory_read(struct archive *a, void *client_data, const void **buff)
98 struct read_memory_data *mine = (struct read_memory_data *)client_data;
99 size_t size;
101 (void)a; /* UNUSED */
102 size = mine->end - mine->buffer;
103 if (size > mine->read_size)
104 size = mine->read_size;
105 memset(mine->copy_buff, 0xA5, mine->copy_buff_size);
106 memcpy(mine->copy_buff, mine->buffer, size);
107 *buff = mine->copy_buff;
109 mine->buffer += size;
110 return (size);
114 * How mean can a skip() routine be? Let's try to find out.
116 #if ARCHIVE_VERSION_NUMBER < 2000000
117 static ssize_t
118 memory_read_skip(struct archive *a, void *client_data, size_t skip)
119 #else
120 static off_t
121 memory_read_skip(struct archive *a, void *client_data, off_t skip)
122 #endif
124 struct read_memory_data *mine = (struct read_memory_data *)client_data;
126 (void)a; /* UNUSED */
127 /* We can't skip by more than is available. */
128 if ((off_t)skip > (off_t)(mine->end - mine->buffer))
129 skip = mine->end - mine->buffer;
130 /* Always do small skips by prime amounts. */
131 if (skip > 71)
132 skip = 71;
133 mine->buffer += skip;
134 return (skip);
138 * Close is just cleaning up our one small bit of data.
140 static int
141 memory_read_close(struct archive *a, void *client_data)
143 struct read_memory_data *mine = (struct read_memory_data *)client_data;
144 (void)a; /* UNUSED */
145 free(mine->copy_buff);
146 free(mine);
147 return (ARCHIVE_OK);