-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate-content-size.cpp
93 lines (84 loc) · 2.6 KB
/
create-content-size.cpp
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
// Copyright 2021 Google LLC
// Copyright 2019-2021 Alexander Galanin <[email protected]>
// http://galanin.nnov.ru/~al
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <zip.h>
struct context {
zip_uint64_t offset;
long long size;
};
zip_int64_t callback(void* user,
void* data,
zip_uint64_t len,
enum zip_source_cmd cmd) {
struct context* ctx = (struct context*)user;
switch (cmd) {
case ZIP_SOURCE_OPEN:
ctx->offset = 0;
return 0;
case ZIP_SOURCE_READ: {
size_t nr = std::numeric_limits<size_t>::max();
if (len < nr)
nr = static_cast<size_t>(len);
if (ctx->size - ctx->offset < nr)
nr = static_cast<size_t>(ctx->size - ctx->offset);
memset(data, 0, nr);
ctx->offset += nr;
return nr;
}
case ZIP_SOURCE_STAT: {
struct zip_stat* st = (struct zip_stat*)data;
zip_stat_init(st);
st->valid = ZIP_STAT_SIZE | ZIP_STAT_MTIME;
st->size = ctx->size;
st->mtime = time(NULL);
return sizeof(struct zip_stat);
}
case ZIP_SOURCE_FREE:
return 0;
case ZIP_SOURCE_CLOSE:
return 0;
case ZIP_SOURCE_ERROR: {
int* errs = static_cast<int*>(data);
errs[0] = ZIP_ER_OPNOTSUPP;
errs[1] = EINVAL;
return 2 * sizeof(int);
}
case ZIP_SOURCE_SUPPORTS:
return ZIP_SOURCE_SUPPORTS_READABLE;
default:
// indicate unsupported operation
return -1;
}
}
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <output-zip-file> <size>\n", argv[0]);
return EXIT_FAILURE;
}
struct context ctx;
ctx.size = atoll(argv[2]);
printf("creating %s of size %llx bytes\n", argv[1], ctx.size);
int err;
struct zip* z = zip_open(argv[1], ZIP_CREATE | ZIP_TRUNCATE, &err);
struct zip_source* s = zip_source_function(z, callback, &ctx);
zip_add(z, "content", s);
zip_close(z);
return 0;
}