forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
219 lines (164 loc) · 6.2 KB
/
Copy pathtest.c
File metadata and controls
219 lines (164 loc) · 6.2 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include "mz_strm.h"
#include "mz_strm_bzip.h"
#include "mz_strm_crypt.h"
#include "mz_strm_aes.h"
#include "mz_strm_zlib.h"
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
{
char buf[UINT16_MAX];
int16_t read = 0;
int16_t written = 0;
void *out_stream = NULL;
void *in_stream = NULL;
void *crypt_out_stream = NULL;
char filename[120];
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_OK)
{
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.encrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
crypt_create(&crypt_out_stream);
mz_stream_set_base(crypt_out_stream, out_stream);
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_WRITE) == MZ_OK)
{
written = mz_stream_write(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_delete(&crypt_out_stream);
mz_stream_os_close(out_stream);
printf("%s encrypted %d\n", filename, written);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_OK)
{
crypt_create(&crypt_out_stream);
mz_stream_set_base(crypt_out_stream, in_stream);
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_READ) == MZ_OK)
{
read = mz_stream_read(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_delete(&crypt_out_stream);
mz_stream_os_close(in_stream);
printf("%s decrypted %d\n", filename, read);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.decrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
}
void test_compress(char *method, mz_stream_create_cb create_compress)
{
char buf[UINT16_MAX];
int16_t read = 0;
uint64_t total_in = 0;
uint64_t total_out = 0;
void *crc_in_stream = NULL;
void *in_stream = NULL;
void *out_stream = NULL;
void *deflate_stream = NULL;
void *inflate_stream = NULL;
uint32_t crc32 = 0;
char filename[120];
printf("Testing compress %s\n", method);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_set_base(crc_in_stream, in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(crc_in_stream, buf, UINT16_MAX);
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_crc32_delete(&crc_in_stream);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
if (read < 0)
{
printf("Failed to read LICENSE\n");
return;
}
printf("LICENSE crc 0x%08x\n", crc32);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.deflate.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
create_compress(&deflate_stream);
mz_stream_set_base(deflate_stream, out_stream);
mz_stream_open(deflate_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_write(deflate_stream, buf, read);
mz_stream_close(deflate_stream);
total_in = mz_stream_get_total_in(deflate_stream);
total_out = mz_stream_get_total_out(deflate_stream);
mz_stream_delete(&deflate_stream);
printf("%s compressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_OK)
{
create_compress(&inflate_stream);
mz_stream_set_base(inflate_stream, in_stream);
mz_stream_open(inflate_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(inflate_stream, buf, UINT16_MAX);
mz_stream_close(inflate_stream);
total_in = mz_stream_get_total_in(inflate_stream);
total_out = mz_stream_get_total_out(inflate_stream);
mz_stream_delete(&inflate_stream);
mz_stream_os_close(in_stream);
printf("%s uncompressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.inflate.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_set_base(crc_in_stream, in_stream);
if (mz_stream_write(crc_in_stream, buf, read) != read)
printf("Failed to write %s\n", filename);
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_delete(&crc_in_stream);
mz_stream_os_close(out_stream);
printf("%s crc 0x%08x\n", filename, crc32);
}
mz_stream_os_delete(&out_stream);
}
void test_aes()
{
test_encrypt("aes", mz_stream_aes_create, "hello");
}
void test_crypt()
{
test_encrypt("crypt", mz_stream_crypt_create, "hello");
}
void test_zlib()
{
test_compress("zlib", mz_stream_zlib_create);
}
void test_bzip()
{
test_compress("bzip", mz_stream_bzip_create);
}