Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_write_open_memory.c
blob1998f128bbc1e557dff6c60c110caead8fe69371
1 /*-
2 * Copyright (c) 2003-2006 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "archive.h"
35 #include "archive_private.h"
38 * This is a little tricky. I used to allow the
39 * compression handling layer to fork the compressor,
40 * which means this write function gets invoked in
41 * a separate process. That would, of course, make it impossible
42 * to actually use the data stored into memory here.
43 * Fortunately, none of the compressors fork today and
44 * I'm reluctant to use that route in the future but, if
45 * forking compressors ever do reappear, this will have
46 * to get a lot more complicated.
49 struct write_memory_data {
50 size_t used;
51 size_t size;
52 size_t * client_size;
53 unsigned char * buff;
56 static int memory_write_close(struct archive *, void *);
57 static int memory_write_open(struct archive *, void *);
58 static ssize_t memory_write(struct archive *, void *, void *buff, size_t);
61 * Client provides a pointer to a block of memory to receive
62 * the data. The 'size' param both tells us the size of the
63 * client buffer and lets us tell the client the final size.
65 int
66 archive_write_open_memory(struct archive *a, void *buff, size_t buffSize, size_t *used)
68 struct write_memory_data *mine;
70 mine = malloc(sizeof(*mine));
71 if (mine == NULL) {
72 archive_set_error(a, ENOMEM, "No memory");
73 return (ARCHIVE_FATAL);
75 memset(mine, 0, sizeof(*mine));
76 mine->buff = buff;
77 mine->size = buffSize;
78 mine->client_size = used;
79 return (archive_write_open(a, mine,
80 memory_write_open, memory_write, memory_write_close));
83 static int
84 memory_write_open(struct archive *a, void *client_data)
86 struct write_memory_data *mine;
87 mine = client_data;
88 mine->used = 0;
89 if (mine->client_size != NULL)
90 *mine->client_size = mine->used;
91 /* Disable padding if it hasn't been set explicitly. */
92 if (-1 == archive_write_get_bytes_in_last_block(a))
93 archive_write_set_bytes_in_last_block(a, 1);
94 return (ARCHIVE_OK);
98 * Copy the data into the client buffer.
99 * Note that we update mine->client_size on every write.
100 * In particular, this means the client can follow exactly
101 * how much has been written into their buffer at any time.
103 static ssize_t
104 memory_write(struct archive *a, void *client_data, void *buff, size_t length)
106 struct write_memory_data *mine;
107 mine = client_data;
109 if (mine->used + length > mine->size) {
110 archive_set_error(a, ENOMEM, "Buffer exhausted");
111 return (ARCHIVE_FATAL);
113 memcpy(mine->buff + mine->used, buff, length);
114 mine->used += length;
115 if (mine->client_size != NULL)
116 *mine->client_size = mine->used;
117 return (length);
120 static int
121 memory_write_close(struct archive *a, void *client_data)
123 struct write_memory_data *mine;
124 (void)a; /* UNUSED */
125 mine = client_data;
126 free(mine);
127 return (ARCHIVE_OK);