Import of libarchive and bsdtar 1.3.1
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_read_open_memory.c
1 /*-
2  * Copyright (c) 2003-2006 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: src/lib/libarchive/archive_read_open_fd.c,v 1.5 2006/01/17 04:49:04 kientzle Exp $");
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         unsigned char   *buffer;
45         unsigned char   *end;
46         ssize_t  read_size;
47 };
48
49 static int      memory_read_close(struct archive *, void *);
50 static int      memory_read_open(struct archive *, void *);
51 static ssize_t  memory_read_skip(struct archive *, void *, size_t request);
52 static ssize_t  memory_read(struct archive *, void *, const void **buff);
53
54 int
55 archive_read_open_memory(struct archive *a, void *buff, size_t size)
56 {
57         return archive_read_open_memory2(a, buff, size, size);
58 }
59
60 /*
61  * Don't use this in production code; the archive_read_open_memory()
62  * version is the one you really want.  This is just here so that
63  * test harnesses can exercise block operations inside the library.
64  */
65 int
66 archive_read_open_memory2(struct archive *a, void *buff,
67     size_t size, size_t read_size)
68 {
69         struct read_memory_data *mine;
70
71         mine = malloc(sizeof(*mine));
72         if (mine == NULL) {
73                 archive_set_error(a, ENOMEM, "No memory");
74                 return (ARCHIVE_FATAL);
75         }
76         memset(mine, 0, sizeof(*mine));
77         mine->buffer = buff;
78         mine->end = mine->buffer + size;
79         mine->read_size = read_size;
80         return (archive_read_open2(a, mine, memory_read_open,
81                     memory_read, memory_read_skip, memory_read_close));
82 }
83
84 /*
85  * There's nothing to open.
86  */
87 static int
88 memory_read_open(struct archive *a, void *client_data)
89 {
90         (void)a; /* UNUSED */
91         (void)client_data; /* UNUSED */
92         return (ARCHIVE_OK);
93 }
94
95 /*
96  * This is scary simple:  Just advance a pointer.  Limiting
97  * to read_size is not technically necessary, but it exercises
98  * more of the internal logic when used with a small block size
99  * in a test harness.  Production use should not specify a block
100  * size; then this is much faster.
101  */
102 static ssize_t
103 memory_read(struct archive *a, void *client_data, const void **buff)
104 {
105         struct read_memory_data *mine = client_data;
106         ssize_t size;
107
108         (void)a; /* UNUSED */
109         *buff = mine->buffer;
110         size = mine->end - mine->buffer;
111         if (size > mine->read_size)
112                 size = mine->read_size;
113         mine->buffer += size;
114         return (size);
115 }
116
117 /*
118  * Advancing is just as simple.  Again, this is doing more than
119  * necessary in order to better exercise internal code when used
120  * as a test harness.
121  */
122 static ssize_t
123 memory_read_skip(struct archive *a, void *client_data, size_t skip)
124 {
125         struct read_memory_data *mine = client_data;
126
127         (void)a; /* UNUSED */
128         if (mine->buffer + skip > mine->end)
129                 skip = mine->end - mine->buffer;
130         /* Round down to block size. */
131         skip /= mine->read_size;
132         skip *= mine->read_size;
133         mine->buffer += skip;
134         return (skip);
135 }
136
137 /*
138  * Close is just cleaning up our one small bit of data.
139  */
140 static int
141 memory_read_close(struct archive *a, void *client_data)
142 {
143         struct read_memory_data *mine = client_data;
144         (void)a; /* UNUSED */
145         free(mine);
146         return (ARCHIVE_OK);
147 }