]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/read_open_memory.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / read_open_memory.c
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 __FBSDID("$FreeBSD$");
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 /*
34  * Read an archive from a block of memory.
35  *
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.
39  */
40
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;
47 };
48
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);
57 static int      read_open_memory_internal(struct archive *a, void *buff,
58     size_t size, size_t read_size, int fullapi);
59
60
61 int
62 read_open_memory(struct archive *a, void *buff, size_t size, size_t read_size)
63 {
64         return read_open_memory_internal(a, buff, size, read_size, 1);
65 }
66
67 /*
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.
70  */
71 int
72 read_open_memory2(struct archive *a, void *buff, size_t size, size_t read_size)
73 {
74         return read_open_memory_internal(a, buff, size, read_size, 0);
75 }
76
77 static int
78 read_open_memory_internal(struct archive *a, void *buff,
79     size_t size, size_t read_size, int fullapi)
80 {
81         struct read_memory_data *mine;
82
83         mine = (struct read_memory_data *)malloc(sizeof(*mine));
84         if (mine == NULL) {
85                 archive_set_error(a, ENOMEM, "No memory");
86                 return (ARCHIVE_FATAL);
87         }
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);
94         if (fullapi)
95                 return (archive_read_open2(a, mine, memory_read_open,
96                             memory_read, memory_read_skip, memory_read_close));
97         else
98                 return (archive_read_open2(a, mine, NULL,
99                             memory_read, NULL, memory_read_close));
100 }
101
102 /*
103  * There's nothing to open.
104  */
105 static int
106 memory_read_open(struct archive *a, void *client_data)
107 {
108         (void)a; /* UNUSED */
109         (void)client_data; /* UNUSED */
110         return (ARCHIVE_OK);
111 }
112
113 /*
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
117  * will screw up.
118  */
119 static ssize_t
120 memory_read(struct archive *a, void *client_data, const void **buff)
121 {
122         struct read_memory_data *mine = (struct read_memory_data *)client_data;
123         size_t size;
124
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;
132
133         mine->buffer += size;
134         return ((ssize_t)size);
135 }
136
137 /*
138  * How mean can a skip() routine be?  Let's try to find out.
139  */
140 #if ARCHIVE_VERSION_NUMBER < 2000000
141 static ssize_t
142 memory_read_skip(struct archive *a, void *client_data, size_t skip)
143 #else
144 static off_t
145 memory_read_skip(struct archive *a, void *client_data, off_t skip)
146 #endif
147 {
148         struct read_memory_data *mine = (struct read_memory_data *)client_data;
149
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. */
155         if (skip > 71)
156                 skip = 71;
157         mine->buffer += skip;
158         return (skip);
159 }
160
161 /*
162  * Close is just cleaning up our one small bit of data.
163  */
164 static int
165 memory_read_close(struct archive *a, void *client_data)
166 {
167         struct read_memory_data *mine = (struct read_memory_data *)client_data;
168         (void)a; /* UNUSED */
169         free(mine->copy_buff);
170         free(mine);
171         return (ARCHIVE_OK);
172 }