-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-compressed-data.c
171 lines (141 loc) · 5.74 KB
/
add-compressed-data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
add-compressed-data.c -- add already compressed file to zip archive
Copyright (C) 2022-2023 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
This layered source can be used to add pre-compressed data to a zip archive.
The data is taken from the lower layer source.
Metadata (uncompressed size, crc, compression method) must be provided by the caller.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zip.h>
struct ctx {
zip_uint64_t uncompressed_size;
zip_uint32_t crc;
zip_uint32_t compression_method;
};
zip_int64_t callback(zip_source_t* src, void *ud, void* data, zip_uint64_t length, zip_source_cmd_t command) {
struct ctx* ctx = (struct ctx*)ud;
switch (command) {
case ZIP_SOURCE_FREE:
/* Free our context. */
free(ctx);
return 0;
case ZIP_SOURCE_STAT: {
zip_stat_t *st = (zip_stat_t *)data;
/* Fix metadata with provided values. */
if (st->valid & ZIP_STAT_SIZE) {
st->comp_size = st->size;
st->valid |= ZIP_STAT_COMP_SIZE;
}
st->size = ctx->uncompressed_size;
st->crc = ctx->crc;
st->comp_method = ctx->compression_method;
st->valid |= ZIP_STAT_COMP_METHOD | ZIP_STAT_SIZE | ZIP_STAT_CRC;
return 0;
}
default:
/* For all other commands, use default implementation */
return zip_source_pass_to_lower_layer(src, data, length, command);
}
}
zip_source_t* create_layered_compressed_source(zip_source_t* source, zip_uint64_t uncompressed_size, zip_uint32_t crc, zip_uint32_t compression_method, zip_error_t *error) {
struct ctx* ctx = (struct ctx*)malloc(sizeof(*ctx));
zip_source_t *compressed_source;
/* Allocate context. */
if (ctx == NULL) {
zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
/* Initialize context */
ctx->compression_method = compression_method;
ctx->uncompressed_size = uncompressed_size;
ctx->crc = crc;
/* Create layered source using our callback and context. */
compressed_source = zip_source_layered_create(source, callback, ctx, error);
/* In case of error, free context. */
if (compressed_source == NULL) {
free(ctx);
}
return compressed_source;
}
/* This is the information needed to add pre-compressed data to a zip archive. data must be compressed in a format compatible with Zip (e.g. no gzip header for deflate). */
zip_uint16_t compression_method = ZIP_CM_DEFLATE;
zip_uint64_t uncompressed_size = 60;
zip_uint32_t crc = 0xb0354048;
zip_uint8_t data[] = {
0x4B, 0x4C, 0x44, 0x06, 0x5C, 0x49, 0x28, 0x80,
0x2B, 0x11, 0x55 ,0x36, 0x19, 0x05, 0x70, 0x01,
0x00
};
int
main(int argc, char *argv[]) {
const char *archive;
zip_source_t *src, *src_comp;
zip_t *za;
int err;
if (argc != 2) {
fprintf(stderr, "usage: %s archive\n", argv[0]);
return 1;
}
archive = argv[1];
if ((za = zip_open(archive, ZIP_CREATE, &err)) == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, err);
fprintf(stderr, "%s: cannot open zip archive '%s': %s\n", argv[0], archive, zip_error_strerror(&error));
zip_error_fini(&error);
exit(1);
}
/* The data can come from any source. To keep the example simple, it is provided in a static buffer here. */
if ((src = zip_source_buffer(za, data, sizeof(data), 0)) == NULL) {
fprintf(stderr, "%s: cannot create buffer source: %s\n", argv[0], zip_strerror(za));
zip_discard(za);
exit(1);
}
zip_error_t error;
if ((src_comp = create_layered_compressed_source(src, uncompressed_size, crc, compression_method, &error)) == NULL) {
fprintf(stderr, "%s: cannot create layered source: %s\n", argv[0], zip_error_strerror(&error));
zip_source_free(src);
zip_discard(za);
exit(1);
}
if ((zip_file_add(za, "precompressed", src_comp, 0)) < 0) {
fprintf(stderr, "%s: cannot add precompressed file: %s\n", argv[0], zip_strerror(za));
zip_source_free(src_comp);
zip_discard(za);
exit(1);
}
if ((zip_close(za)) < 0) {
fprintf(stderr, "%s: cannot close archive '%s': %s\n", argv[0], archive, zip_strerror(za));
zip_discard(za);
exit(1);
}
exit(0);
}