2 * Copyright (c) 2003-2006 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
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
{
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.
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
));
72 archive_set_error(a
, ENOMEM
, "No memory");
73 return (ARCHIVE_FATAL
);
75 memset(mine
, 0, sizeof(*mine
));
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
));
84 memory_write_open(struct archive
*a
, void *client_data
)
86 struct write_memory_data
*mine
;
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);
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.
104 memory_write(struct archive
*a
, void *client_data
, void *buff
, size_t length
)
106 struct write_memory_data
*mine
;
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
;
121 memory_write_close(struct archive
*a
, void *client_data
)
123 struct write_memory_data
*mine
;
124 (void)a
; /* UNUSED */