Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_zip.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include <zlib.h>
36
37#include "nilorea/n_common.h"
38#include "nilorea/n_log.h"
39#include "nilorea/n_zip.h"
40
42static int log_level = LOG_ERR;
44static int failures = 0;
45
49static void expect_true(const char* what, int ok) {
50 if (!ok) {
51 n_log(LOG_ERR, "FAIL: %s", what);
52 failures++;
53 } else {
54 n_log(LOG_INFO, "ok: %s", what);
55 }
56}
57
62static int process_args(int argc, char** argv) {
63 int opt = 0;
64
65 while ((opt = getopt(argc, argv, "V:")) != -1) {
66 if (opt == 'V') {
67 log_level = atoi(optarg);
68 } else {
69 return -1;
70 }
71 }
72 return 0;
73}
74
78static void put16(unsigned char* p, unsigned v) {
79 p[0] = (unsigned char)(v & 0xff);
80 p[1] = (unsigned char)((v >> 8) & 0xff);
81}
82
86static void put32(unsigned char* p, unsigned long v) {
87 put16(p, (unsigned)(v & 0xffff));
88 put16(p + 2, (unsigned)((v >> 16) & 0xffff));
89}
90
92typedef struct {
94 const char* name;
96 unsigned method;
98 const unsigned char* data;
100 size_t len;
101} MEMBER;
102
109static int write_zip(const char* path, const MEMBER* m, int count) {
110 FILE* f = fopen(path, "wb");
111 unsigned long offsets[4], sizes[4], crcs[4];
112 unsigned char* packed[4];
113 unsigned long dir_at = 0, dir_size = 0, at = 0;
114 int i = 0;
115
116 if (!f || count > 4) return -1;
117 for (i = 0; i < count; i++) {
118 unsigned char header[30];
119 size_t n = strlen(m[i].name);
120
121 crcs[i] = crc32(crc32(0L, Z_NULL, 0), m[i].data, (uInt)m[i].len);
122 packed[i] = (unsigned char*)malloc(m[i].len + 1024);
123 if (m[i].method == 8) {
124 z_stream zs;
125
126 memset(&zs, 0, sizeof(zs));
127 deflateInit2(&zs, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
128 zs.next_in = (Bytef*)m[i].data;
129 zs.avail_in = (uInt)m[i].len;
130 zs.next_out = packed[i];
131 zs.avail_out = (uInt)(m[i].len + 1024);
132 deflate(&zs, Z_FINISH);
133 sizes[i] = zs.total_out;
134 deflateEnd(&zs);
135 } else {
136 memcpy(packed[i], m[i].data, m[i].len);
137 sizes[i] = (unsigned long)m[i].len;
138 }
139 memset(header, 0, sizeof(header));
140 put32(header, 0x04034b50ul);
141 put16(header + 4, 20);
142 put16(header + 8, m[i].method);
143 put32(header + 14, crcs[i]);
144 put32(header + 18, sizes[i]);
145 put32(header + 22, (unsigned long)m[i].len);
146 put16(header + 26, (unsigned)n);
147 offsets[i] = at;
148 fwrite(header, 1, 30, f);
149 fwrite(m[i].name, 1, n, f);
150 fwrite(packed[i], 1, sizes[i], f);
151 at += 30 + (unsigned long)n + sizes[i];
152 }
153 dir_at = at;
154 for (i = 0; i < count; i++) {
155 unsigned char record[46];
156 size_t n = strlen(m[i].name);
157
158 memset(record, 0, sizeof(record));
159 put32(record, 0x02014b50ul);
160 put16(record + 4, 20);
161 put16(record + 6, 20);
162 put16(record + 10, m[i].method);
163 put32(record + 16, crcs[i]);
164 put32(record + 20, sizes[i]);
165 put32(record + 24, (unsigned long)m[i].len);
166 put16(record + 28, (unsigned)n);
167 put32(record + 42, offsets[i]);
168 fwrite(record, 1, 46, f);
169 fwrite(m[i].name, 1, n, f);
170 dir_size += 46 + (unsigned long)n;
171 free(packed[i]);
172 }
173 {
174 unsigned char end[22];
175
176 memset(end, 0, sizeof(end));
177 put32(end, 0x06054b50ul);
178 put16(end + 8, (unsigned)count);
179 put16(end + 10, (unsigned)count);
180 put32(end + 12, dir_size);
181 put32(end + 16, dir_at);
182 fwrite(end, 1, 22, f);
183 }
184 fclose(f);
185 return 0;
186}
187
193static int file_is(const char* path, const unsigned char* data, size_t len) {
194 FILE* f = fopen(path, "rb");
195 unsigned char* got = NULL;
196 size_t n = 0;
197 int same = 0;
198
199 if (!f) return 0;
200 got = (unsigned char*)malloc(len + 1);
201 if (got) {
202 n = fread(got, 1, len + 1, f);
203 same = n == len && memcmp(got, data, len) == 0;
204 }
205 free(got);
206 fclose(f);
207 return same;
208}
209
214int main(int argc, char** argv) {
215 static const char* archive = "ex_zip.tmp.zip";
216 static const char* out = "ex_zip.tmp.out";
217 static const unsigned char small[] = "a stored member, as it is";
218 size_t big_len = 700000, i = 0;
219 unsigned char* big = NULL;
220 MEMBER members[2];
221 N_ZIP_MEMBER info;
222
224 if (process_args(argc, argv) != 0) {
225 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
226 return 2;
227 }
229 big = (unsigned char*)malloc(big_len);
230 if (!big) return 1;
231 /* More than one read chunk, and compressible, so the inflate loop turns
232 * more than once each way. */
233 for (i = 0; i < big_len; i++) big[i] = (unsigned char)((i * 7) % 251);
234 members[0].name = "notes/small.txt";
235 members[0].method = 0;
236 members[0].data = small;
237 members[0].len = sizeof(small) - 1;
238 members[1].name = "model/model.onnx";
239 members[1].method = 8;
240 members[1].data = big;
241 members[1].len = big_len;
242 expect_true("an archive is written", write_zip(archive, members, 2) == 0);
243
244 expect_true("a member is found", n_zip_find(archive, "model/model.onnx", &info) == 0 && info.method == 8 &&
245 info.size == big_len);
246 expect_true("a missing one is not", n_zip_find(archive, "model/other.onnx", NULL) == -1);
247 expect_true("a stored member comes out as it went in",
248 n_zip_extract(archive, "notes/small.txt", out) == 0 && file_is(out, small, sizeof(small) - 1));
249 expect_true("a deflated one too", n_zip_extract(archive, "model/model.onnx", out) == 0 && file_is(out, big, big_len));
250 expect_true("a missing one is refused", n_zip_extract(archive, "nothing", out) == -1);
251 {
252 unsigned char* data = NULL;
253 size_t len = 0;
254 char name[64];
255
256 expect_true("a member is read into memory as it went in",
257 n_zip_read(archive, "model/model.onnx", big_len, &data, &len) == 0 && len == big_len &&
258 memcmp(data, big, big_len) == 0 && data[len] == 0);
259 free(data);
260 data = NULL;
261 expect_true("one larger than allowed is refused",
262 n_zip_read(archive, "model/model.onnx", big_len - 1, &data, &len) == -1 && !data);
263 expect_true("a member is found by how its name ends",
264 n_zip_find_ending(archive, "/small.txt", name, sizeof(name)) == 0 &&
265 strcmp(name, "notes/small.txt") == 0);
266 expect_true("an ending nothing has is not", n_zip_find_ending(archive, "/none.txt", name, sizeof(name)) == -1);
267 }
268 {
269 /* One byte of the deflated data changed: the CRC-32 says so, and the
270 * output is not left behind. */
271 FILE* f = fopen(archive, "r+b");
272
273 if (f) {
274 unsigned char b = 0;
275
276 fseek(f, 30 + 15 + (long)(sizeof(small) - 1) + 30 + 16 + 100, SEEK_SET);
277 if (fread(&b, 1, 1, f) == 1) {
278 b ^= 0x5a;
279 fseek(f, -1, SEEK_CUR);
280 fwrite(&b, 1, 1, f);
281 }
282 fclose(f);
283 }
284 expect_true("a damaged member is refused and removed",
285 n_zip_extract(archive, "model/model.onnx", out) == -1 && access(out, F_OK) != 0);
286 }
287 expect_true("a file that is not an archive is refused", n_zip_find("ex_zip.c", "x", NULL) == -1);
288 remove(archive);
289 remove(out);
290 free(big);
291 if (failures) {
292 fprintf(stderr, "ex_zip: %d failure(s)\n", failures);
293 return 1;
294 }
295 printf("ex_zip: ok\n");
296 return 0;
297}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static int write_zip(const char *path, const MEMBER *m, int count)
write a ZIP of stored members, as torch.save() does
Definition ex_pth.c:144
static void put16(unsigned char *p, uint16_t v, int be)
Write a 16 bit value in a byte order.
Definition ex_tiff.c:58
static void put32(unsigned char *p, uint32_t v, int be)
Write a 32 bit value in a byte order.
Definition ex_tiff.c:67
unsigned method
0 stored, 8 deflated
Definition ex_zip.c:96
static int file_is(const char *path, const unsigned char *data, size_t len)
is a file these bytes
Definition ex_zip.c:193
const char * name
its name
Definition ex_pth.c:132
size_t len
how many
Definition ex_pth.c:136
const unsigned char * data
its bytes
Definition ex_pth.c:134
one stored member
Definition ex_pth.c:130
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
uint64_t size
its size once out
Definition n_zip.h:58
unsigned method
0 stored, 8 deflated
Definition n_zip.h:54
int n_zip_extract(const char *path, const char *name, const char *out)
write a member out to a file, checking its CRC-32
Definition n_zip.c:267
int n_zip_find(const char *path, const char *name, N_ZIP_MEMBER *member)
find a member by its full name in an archive's central directory
Definition n_zip.c:152
int n_zip_find_ending(const char *path, const char *ending, char *name, size_t name_len)
find the first member whose name ends with a given text, as a PyTorch checkpoint's "archive/data....
Definition n_zip.c:156
int n_zip_read(const char *path, const char *name, size_t max_size, unsigned char **data, size_t *len)
read a member into memory, checking its CRC-32
Definition n_zip.c:282
What n_zip_find() says about a member.
Definition n_zip.h:52
Common headers and low-level functions & define.
Generic log system.
Reading members out of a ZIP archive, to a file or into memory.