wok view fusecloop/stuff/fusecloop.u @ rev 20257

Add giflossy
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Mar 13 23:27:32 2018 +0100 (22 months ago)
parents 792674d97726
children 63cdeb5a1e77
line source
1 --- compressed_loop.h
2 +++ compressed_loop.h
3 @@ -41,6 +41,80 @@
4 /* data_index (num_blocks 64bit pointers, network order)... */
5 /* compressed data (gzip block compressed format)... */
7 +struct cloop_tail
8 +{
9 + u_int32_t table_size;
10 + u_int32_t index_size;
11 + u_int32_t num_blocks;
12 +};
13 +
14 +struct block_info
15 +{
16 + loff_t offset; /* 64-bit offsets of compressed block */
17 + u_int32_t size; /* 32-bit compressed block size */
18 + u_int32_t optidx; /* 32-bit index number */
19 +};
20 +
21 +static inline char *build_index(struct block_info *offsets, unsigned long n)
22 +{
23 + u_int32_t *ofs32 = (u_int32_t *) offsets;
24 + loff_t *ofs64 = (loff_t *) offsets;
25 +
26 + if (ofs32[0] == 0) {
27 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
28 + while (n--) {
29 + offsets[n].offset = __be64_to_cpu(offsets[n].offset);
30 + offsets[n].size = ntohl(offsets[n].size);
31 + }
32 + return (char *) "128BE accelerated knoppix 1.0";
33 + }
34 + else { /* V2.0 */
35 + loff_t last = __be64_to_cpu(ofs64[n]);
36 + while (n--) {
37 + offsets[n].size = last -
38 + (offsets[n].offset = __be64_to_cpu(ofs64[n]));
39 + last = offsets[n].offset;
40 + }
41 + return (char *) "64BE v2.0";
42 + }
43 + }
44 + else if (ofs32[1] == 0) { /* V1.0 */
45 + loff_t last = __le64_to_cpu(ofs64[n]);
46 + while (n--) {
47 + offsets[n].size = last -
48 + (offsets[n].offset = __le64_to_cpu(ofs64[n]));
49 + last = offsets[n].offset;
50 + }
51 + return (char *) "64LE v1.0";
52 + }
53 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
54 + loff_t last = ntohl(ofs32[n]);
55 + while (n--) {
56 + offsets[n].size = last -
57 + (offsets[n].offset = ntohl(ofs32[n]));
58 + last = offsets[n].offset;
59 + }
60 + return (char *) "32BE v0.68";
61 + }
62 + else { /* V3.0 */
63 + unsigned long i;
64 + loff_t j;
65 +
66 + for (i = n; i-- != 0; )
67 + offsets[i].size = ntohl(ofs32[i]);
68 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
69 + offsets[i].offset = j;
70 + if (offsets[i].size & 0x80000000) {
71 + unsigned long k = offsets[i].size & 0x7FFFFFFF;
72 + offsets[i].offset = offsets[k].offset;
73 + offsets[i].size = offsets[k].size;
74 + }
75 + else j += offsets[i].size;
76 + }
77 + return (char *) "32BE v3.0";
78 + }
79 +}
80 +
81 /* Cloop suspend IOCTL */
82 #define CLOOP_SUSPEND 0x4C07
85 --- cloopreader.h
86 +++ cloopreader.h
87 @@ -33,7 +33,7 @@
88 int numblocks;
89 ulong blocksize;
91 - loff_t* toc; /* Data index */
92 + struct block_info *toc; /* Data index */
93 size_t tocsize;
95 unsigned char* cblock; /* Compressed block */
97 --- cloopreader.c
98 +++ cloopreader.c
99 @@ -59,10 +59,32 @@
101 ALLOC(c->pblock,c->blocksize);
103 - c->tocsize=sizeof *c->toc * (c->numblocks+1); /* One extra address is position of EOF */
104 - ALLOC(c->toc,c->tocsize);
105 + if (c->numblocks + 1 == 0) {
106 + struct cloop_tail tail;
107 + loff_t end = lseek(c->fh,0,SEEK_END); /* lseek(,-n,SEEK_END) buggy ? */
108 + void *p;
109 + ulong toclen, len;
111 - OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
112 + OP(lseek(c->fh, end - sizeof(tail), SEEK_SET));
113 + OP(read_all(c->fh, &tail, sizeof(tail)));
114 + c->numblocks = ntohl(tail.num_blocks);
115 + c->tocsize = sizeof(*c->toc) * c->numblocks;
116 + len = ntohl(tail.table_size);
117 + toclen = (ntohl(tail.index_size) & 255) * c->numblocks;
118 + OP(lseek(c->fh, end - sizeof(tail) - len, SEEK_SET));
119 + ALLOC(c->toc, sizeof(*c->toc) * c->numblocks);
120 + ALLOC(p,len);
121 + OP(read_all(c->fh,p,len)); /* read Data Index */
122 + if (uncompress((void *)c->toc,&toclen,p,len) != Z_OK)
123 + exit(1);
124 + free(p);
125 + }
126 + else {
127 + c->tocsize = sizeof(*c->toc) * c->numblocks;
128 + ALLOC(c->toc,c->tocsize);
129 + OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
130 + }
131 + build_index(c->toc, c->numblocks);
132 c->cblocksizecur=0;
133 c->curblock=-1;
134 return 0;
135 @@ -79,10 +101,10 @@
136 if(page>=c->numblocks){errno=EFAULT;return -1;}
137 c->curblock=page;
139 - bprintf("Seeking to 0x%Lx\n",btc(c->toc[page]));
140 - OP(lseek(c->fh,btc(c->toc[page]), SEEK_SET));
141 + bprintf("Seeking to 0x%Lx\n",c->toc[page].offset);
142 + OP(lseek(c->fh,c->toc[page].offset, SEEK_SET));
144 - c->cblocksize=btc(c->toc[page+1]) - btc(c->toc[page]);
145 + c->cblocksize=c->toc[page].size;
146 bprintf("Compressed size=%lu\n",c->cblocksize);
147 if(c->cblocksize > c->cblocksizecur){
148 if(c->cblocksizecur)free(c->cblock);
150 --- extract_compressed_fs.c
151 +++ extract_compressed_fs.c
152 @@ -1,19 +1,23 @@
153 /* Extracts a filesystem back from a compressed fs file */
154 +#define _LARGEFILE64_SOURCE
155 #include "common_header.h"
156 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
158 int main(int argc, char *argv[])
159 {
160 int handle;
161 struct cloop_head head;
162 unsigned int i;
163 + unsigned long num_blocks, block_size, zblock_maxsize, lastlen = 0;
164 unsigned char *buffer, *clear_buffer;
165 + struct block_info *offsets;
167 - if (argc != 2) {
168 - fprintf(stderr, "Need filename\n");
169 + if (argc < 2 || argv[1][0] == '-') {
170 + fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
171 exit(1);
172 }
174 - handle = open(argv[1], O_RDONLY);
175 + handle = open(argv[1], O_RDONLY|O_LARGEFILE);
176 if (handle < 0) {
177 perror("Opening compressed file\n");
178 exit(1);
179 @@ -24,66 +28,100 @@
180 exit(1);
181 }
183 - buffer = malloc(ntohl(head.block_size) + ntohl(head.block_size)/1000
184 - + 12 + 4);
185 - clear_buffer = malloc(ntohl(head.block_size));
186 - fprintf(stderr, "%u blocks of size %u. Preamble:\n%s\n",
187 - ntohl(head.num_blocks), ntohl(head.block_size), head.preamble);
188 + num_blocks = ntohl(head.num_blocks);
189 + block_size = ntohl(head.block_size);
190 + zblock_maxsize = block_size + block_size/1000 + 12 + 4;
191 + buffer = malloc(zblock_maxsize);
192 + clear_buffer = malloc(block_size);
194 - for (i = 0; i < ntohl(head.num_blocks); i++) {
195 - int currpos;
196 - unsigned long destlen = ntohl(head.block_size);
197 - loff_t offset[2];
198 - unsigned int size;
199 + if (num_blocks == (unsigned long) -1) {
200 + void *table;
201 + struct cloop_tail tail;
202 + unsigned long len, table_size;
203 + loff_t end = lseek64(handle, 0, SEEK_END);
204 +
205 + if (lseek64(handle, end - sizeof(tail), SEEK_SET) < 0 ||
206 + read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
207 + lseek64(handle, end - sizeof(tail) -
208 + ntohl(tail.table_size), SEEK_SET) < 0) {
209 + perror("Reading tail\n");
210 + exit(1);
211 + }
212 + head.num_blocks = tail.num_blocks;
213 + num_blocks = ntohl(head.num_blocks);
214 + table_size = ntohl(tail.table_size);
215 + table = malloc(table_size);
216 + len = i = num_blocks * (ntohl(tail.index_size) & 255);
217 + lastlen = ntohl(tail.index_size) / 256;
218 + offsets = malloc(num_blocks * sizeof(*offsets));
219 + if (!table || !offsets ||
220 + read(handle, table, table_size) != table_size ||
221 + uncompress((void *)offsets, &len, table, table_size) != Z_OK ||
222 + len != i) {
223 + perror("Reading index\n");
224 + exit(1);
225 + }
226 + free(table);
227 + }
228 + else {
229 + offsets = malloc(i = num_blocks * sizeof(*offsets));
230 + if (!offsets || read(handle, offsets, i) != i) {
231 + perror("Reading index\n");
232 + exit(1);
233 + }
234 + }
235 +
236 + fprintf(stderr, "%lu blocks of size %lu. Preamble:\n%s\n",
237 + num_blocks, block_size, head.preamble);
238 + fprintf(stderr, "Index %s.\n", build_index(offsets, num_blocks));
239 +
240 + if (argc > 2) {
241 + unsigned n;
242 + loff_t data, offset = ((num_blocks + 1) * sizeof(offset)) + sizeof(head);
243 +
244 + strcpy(head.preamble, CLOOP_PREAMBLE);
245 + write(STDOUT_FILENO, &head, n = sizeof(head));
246 + for (i = 0; i < num_blocks; i++) {
247 + data = __be64_to_cpu(offset);
248 + write(STDOUT_FILENO, &data, sizeof(data));
249 + n += sizeof(data);
250 + offset += offsets[i].size;
251 + }
252 + data = __be64_to_cpu(offset);
253 + write(STDOUT_FILENO, &data, sizeof(data));
254 + for (i = 0; i < num_blocks && lseek64(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
255 + read(handle, buffer, offsets[i].size);
256 + write(STDOUT_FILENO, buffer, offsets[i].size);
257 + n += offsets[i].size;
258 + }
259 + n &= 0x1FF;
260 + if (n) {
261 + memset(buffer, 0, 512);
262 + write(STDOUT_FILENO, buffer, 512 - n);
263 + }
264 + return 0;
265 + }
266 +
267 + for (i = 0; i < num_blocks; i++) {
268 + unsigned long destlen = block_size;
269 + unsigned int size = offsets[i].size;
271 - read(handle, &offset, 2*sizeof(loff_t));
272 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
273 -
274 - currpos = lseek(handle, 0, SEEK_CUR);
275 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
276 + if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
277 fprintf(stderr, "lseek to %Lu: %s\n",
278 - __be64_to_cpu(offset[0]), strerror(errno));
279 + offsets[i].offset, strerror(errno));
280 exit(1);
281 }
283 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
284 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
285 - + 12 + 4) {
286 + if (size > zblock_maxsize) {
287 fprintf(stderr,
288 "Size %u for block %u (offset %Lu) too big\n",
289 - size, i, __be64_to_cpu(offset[0]));
290 + size, i, offsets[i].offset);
291 exit(1);
292 }
293 read(handle, buffer, size);
294 - if (lseek(handle, currpos, SEEK_SET) < 0) {
295 - perror("seeking");
296 - exit(1);
297 - }
299 - fprintf(stderr, "Block %u length %u => %lu\n",
300 - i, size, destlen);
301 - if (i == 3) {
302 - fprintf(stderr,
303 - "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
304 - buffer[0],
305 - buffer[1],
306 - buffer[2],
307 - buffer[3],
308 - buffer[4],
309 - buffer[5],
310 - buffer[6],
311 - buffer[7]);
312 - fprintf(stderr,
313 - "Block tail:%02X%02X%02X%02X%02X%02X%02X%02X\n",
314 - buffer[3063],
315 - buffer[3064],
316 - buffer[3065],
317 - buffer[3066],
318 - buffer[3067],
319 - buffer[3068],
320 - buffer[3069],
321 - buffer[3070]);
322 - }
323 + fprintf(stderr, "Block %u at %llu length %u",
324 + i, offsets[i].offset, size);
325 switch (uncompress(clear_buffer, &destlen,
326 buffer, size)) {
327 case Z_OK:
328 @@ -105,12 +143,13 @@
329 fprintf(stderr, "Uncomp: unknown error %u\n", i);
330 exit(1);
331 }
332 - if (destlen != ntohl(head.block_size)) {
333 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
334 - destlen, ntohl(head.block_size));
335 + fprintf(stderr, " => %lu\n", destlen);
336 + if (destlen != block_size && i != num_blocks - 1) {
337 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
338 + destlen, block_size);
339 exit(1);
340 }
341 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
342 + write(STDOUT_FILENO, clear_buffer, (lastlen != 0 && (i+1) == num_blocks) ? lastlen : block_size);
343 }
344 return 0;
345 }
346 --- Makefile
347 +++ Makefile
348 @@ -1,16 +1,19 @@
349 PROGNAME=fusecloop
350 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
351 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
352 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
353 FUSECFLAGS=`pkg-config fuse --cflags`
354 FUSELDFLAGS=`pkg-config fuse --libs`
356 CFLAGS= -Wall
358 -all: fusecloop extract_compressed_fs
359 +all: fusecloop extract_compressed_fs create_compressed_fs
361 extract_compressed_fs: extract_compressed_fs.c
362 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
364 +create_compressed_fs: create_compressed_fs.c md5sum.c
365 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
366 +
367 fusecloop: fusecloop.c cloopreader.o strver debug.o
368 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
372 --- md5sum.c
373 +++ md5sum.c
374 @@ -0,0 +1,246 @@
375 +/*
376 + * Based on busybox code.
377 + *
378 + * Compute MD5 checksum of strings according to the
379 + * definition of MD5 in RFC 1321 from April 1992.
380 + *
381 + * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
382 + *
383 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
384 + * Copyright (C) 2001 Manuel Novoa III
385 + * Copyright (C) 2003 Glenn L. McGrath
386 + * Copyright (C) 2003 Erik Andersen
387 + * Copyright (C) 2010 Denys Vlasenko
388 + * Copyright (C) 2012 Pascal Bellard
389 + *
390 + * Licensed under GPLv2 or later
391 + */
392 +
393 +#define ALIGN1
394 +
395 +static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
396 +static uint64_t total64; /* must be directly before hash[] */
397 +static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
398 +
399 +/* Emit a string of hex representation of bytes */
400 +static char* bin2hex(char *p)
401 +{
402 + static const char bb_hexdigits_upcase[] ALIGN1 = "0123456789abcdef";
403 + int count = 16;
404 + const char *cp = (const char *) hash;
405 + while (count) {
406 + unsigned char c = *cp++;
407 + /* put lowercase hex digits */
408 + *p++ = bb_hexdigits_upcase[c >> 4];
409 + *p++ = bb_hexdigits_upcase[c & 0xf];
410 + count--;
411 + }
412 + return p;
413 +}
414 +
415 +//#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
416 +static uint32_t rotl32(uint32_t x, unsigned n)
417 +{
418 + return (x << n) | (x >> (32 - n));
419 +}
420 +
421 +static void md5_process_block64(void);
422 +
423 +/* Feed data through a temporary buffer.
424 + * The internal buffer remembers previous data until it has 64
425 + * bytes worth to pass on.
426 + */
427 +static void common64_hash(const void *buffer, size_t len)
428 +{
429 + unsigned bufpos = total64 & 63;
430 +
431 + total64 += len;
432 +
433 + while (1) {
434 + unsigned remaining = 64 - bufpos;
435 + if (remaining > len)
436 + remaining = len;
437 + /* Copy data into aligned buffer */
438 + memcpy(wbuffer + bufpos, buffer, remaining);
439 + len -= remaining;
440 + buffer = (const char *)buffer + remaining;
441 + bufpos += remaining;
442 + /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
443 + bufpos -= 64;
444 + if (bufpos != 0)
445 + break;
446 + /* Buffer is filled up, process it */
447 + md5_process_block64();
448 + /*bufpos = 0; - already is */
449 + }
450 +}
451 +
452 +/* Process the remaining bytes in the buffer */
453 +static void common64_end(void)
454 +{
455 + unsigned bufpos = total64 & 63;
456 + /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
457 + wbuffer[bufpos++] = 0x80;
458 +
459 + /* This loop iterates either once or twice, no more, no less */
460 + while (1) {
461 + unsigned remaining = 64 - bufpos;
462 + memset(wbuffer + bufpos, 0, remaining);
463 + /* Do we have enough space for the length count? */
464 + if (remaining >= 8) {
465 + /* Store the 64-bit counter of bits in the buffer */
466 + uint64_t t = total64 << 3;
467 + /* wbuffer is suitably aligned for this */
468 + *(uint64_t *) (&wbuffer[64 - 8]) = t;
469 + }
470 + md5_process_block64();
471 + if (remaining >= 8)
472 + break;
473 + bufpos = 0;
474 + }
475 +}
476 +
477 +/* These are the four functions used in the four steps of the MD5 algorithm
478 + * and defined in the RFC 1321. The first function is a little bit optimized
479 + * (as found in Colin Plumbs public domain implementation).
480 + * #define FF(b, c, d) ((b & c) | (~b & d))
481 + */
482 +#undef FF
483 +#undef FG
484 +#undef FH
485 +#undef FI
486 +#define FF(b, c, d) (d ^ (b & (c ^ d)))
487 +#define FG(b, c, d) FF(d, b, c)
488 +#define FH(b, c, d) (b ^ c ^ d)
489 +#define FI(b, c, d) (c ^ (b | ~d))
490 +
491 +/* Hash a single block, 64 bytes long and 4-byte aligned */
492 +static void md5_process_block64(void)
493 +{
494 + /* Before we start, one word to the strange constants.
495 + They are defined in RFC 1321 as
496 + T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
497 + */
498 + static const uint32_t C_array[] = {
499 + /* round 1 */
500 + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
501 + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
502 + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
503 + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
504 + /* round 2 */
505 + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
506 + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
507 + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
508 + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
509 + /* round 3 */
510 + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
511 + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
512 + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
513 + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
514 + /* round 4 */
515 + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
516 + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
517 + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
518 + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
519 + };
520 + static const char P_array[] ALIGN1 = {
521 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
522 + 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
523 + 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
524 + 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
525 + };
526 + uint32_t *words = (uint32_t *) wbuffer;
527 + uint32_t A = hash[0];
528 + uint32_t B = hash[1];
529 + uint32_t C = hash[2];
530 + uint32_t D = hash[3];
531 +
532 + static const char S_array[] ALIGN1 = {
533 + 7, 12, 17, 22,
534 + 5, 9, 14, 20,
535 + 4, 11, 16, 23,
536 + 6, 10, 15, 21
537 + };
538 + const uint32_t *pc;
539 + const char *pp;
540 + const char *ps;
541 + int i;
542 + uint32_t temp;
543 +
544 +
545 + pc = C_array;
546 + pp = P_array;
547 + ps = S_array - 4;
548 +
549 + for (i = 0; i < 64; i++) {
550 + if ((i & 0x0f) == 0)
551 + ps += 4;
552 + temp = A;
553 + switch (i >> 4) {
554 + case 0:
555 + temp += FF(B, C, D);
556 + break;
557 + case 1:
558 + temp += FG(B, C, D);
559 + break;
560 + case 2:
561 + temp += FH(B, C, D);
562 + break;
563 + case 3:
564 + temp += FI(B, C, D);
565 + }
566 + temp += words[(int) (*pp++)] + *pc++;
567 + temp = rotl32(temp, ps[i & 3]);
568 + temp += B;
569 + A = D;
570 + D = C;
571 + C = B;
572 + B = temp;
573 + }
574 + /* Add checksum to the starting values */
575 + hash[0] += A;
576 + hash[1] += B;
577 + hash[2] += C;
578 + hash[3] += D;
579 +
580 +}
581 +#undef FF
582 +#undef FG
583 +#undef FH
584 +#undef FI
585 +
586 +/* Initialize structure containing state of computation.
587 + * (RFC 1321, 3.3: Step 3)
588 + */
589 +static void md5_begin(void)
590 +{
591 + hash[0] = 0x67452301;
592 + hash[1] = 0xefcdab89;
593 + hash[2] = 0x98badcfe;
594 + hash[3] = 0x10325476;
595 + total64 = 0;
596 +}
597 +
598 +/* Used also for sha1 and sha256 */
599 +#define md5_hash common64_hash
600 +
601 +/* Process the remaining bytes in the buffer and put result from CTX
602 + * in first 16 bytes following RESBUF. The result is always in little
603 + * endian byte order, so that a byte-wise output yields to the wanted
604 + * ASCII representation of the message digest.
605 + */
606 +#define md5_end common64_end
607 +
608 +typedef struct { char hash[16]; } md5hash;
609 +
610 +static md5hash md5sum(uint8_t *buffer, int len)
611 +{
612 + md5hash val;
613 +
614 + md5_begin();
615 + md5_hash(buffer, len);
616 + md5_end();
617 + memcpy(&val, hash, 16);
618 +
619 + return val;
620 +}
621 --- create_compressed_fs.c
622 +++ create_compressed_fs.c
623 @@ -0,0 +1,225 @@
624 +#ifdef FIND_BEST_COMPRESSION
625 +#include <compress.h>
626 +extern "C" {
627 +#include <stdlib.h>
628 +#include <string.h>
629 +
630 +#define ZMAX 9
631 +static shrink_t level;
632 +static int pass, iter;
633 +static int best_compress(unsigned char *compressed,
634 + unsigned long *compressed_len,
635 + unsigned char *uncompressed,
636 + unsigned long uncompressed_len)
637 +{
638 + int i, j, err;
639 + unsigned char *buf[2];
640 + unsigned len;
641 + unsigned long llen, best = *compressed_len * 2;
642 + static unsigned char *buffer;
643 + static unsigned long buffersz;
644 +
645 + if (buffersz < *compressed_len) {
646 + if (buffer) free(buffer);
647 + buffer = (unsigned char *) malloc(buffersz = *compressed_len);
648 + if (!buffer) return Z_MEM_ERROR;
649 + }
650 + buf[0] = compressed;
651 + buf[1] = buffer;
652 + for (i = j = 0; i <= ZMAX+3 && (pass == 0 || i < pass); i++) {
653 + llen = len = *compressed_len;
654 + if (i >= ZMAX+1) {
655 + level.level = (i == ZMAX+1) ? shrink_normal :
656 + (i == ZMAX+2) ? shrink_extra : shrink_insane;
657 + err = (compress_zlib(level, buf[j],
658 + len, uncompressed,
659 + uncompressed_len)) ? Z_OK : Z_DATA_ERROR;
660 + } else {
661 + err = compress2(buf[j], &llen, uncompressed,
662 + uncompressed_len, i);
663 + len = llen;
664 + }
665 + if (err != Z_OK) return err;
666 + if (len < best) {
667 + best = len;
668 + j = 1 - j;
669 + }
670 + }
671 + *compressed_len = best;
672 + if (j == 0)
673 + memcpy(compressed, buffer, best);
674 + return err;
675 +}
676 +#define compress2(a,b,c,d,e) best_compress(a,b,c,d)
677 +#endif
678 +
679 +#include <signal.h>
680 +
681 +/* Creates a compressed file */
682 +#include "common_header.h"
683 +
684 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V3.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
685 +#define CHUNK 65536
686 +#define DEFAULT_BLOCKSIZE 65536
687 +
688 +static void quit(const char *s)
689 +{
690 + fprintf(stderr, "%s\n", s);
691 + exit(1);
692 +}
693 +
694 +static int readblock(unsigned char *buffer, int n)
695 +{
696 + int i;
697 +
698 + memset(buffer, 0, n);
699 + for (i = 0 ; i < n;) {
700 + int j = read(STDIN_FILENO, buffer + i, n - i);
701 + if (j < 0 && errno == EINTR) continue;
702 + if (j <= 0) break;
703 + i += j;
704 + }
705 + return i;
706 +}
707 +
708 +#ifdef FIND_BEST_COMPRESSION
709 +#include "md5sum.c"
710 +#endif
711 +
712 +static unsigned n;
713 +static unsigned long lastlen, pos, *block_index;
714 +static unsigned char *compressed;
715 +static unsigned long block_size = 0;
716 +static void flush_index(int sig)
717 +{
718 + static char padding[512];
719 + struct cloop_tail tail;
720 + unsigned long len;
721 +
722 + fprintf(stderr, "Write index for %lu blocks\n", n);
723 + if (block_size >= 0x1000000) lastlen = 0;
724 + tail.index_size = ntohl(sizeof(*block_index) + 256*(lastlen % 0xFFffFF));
725 + tail.num_blocks = ntohl(n);
726 + n *= sizeof(*block_index);
727 + len = n + n/1000 + 12;
728 + compressed = (unsigned char *) realloc(compressed, len);
729 + if (!compressed || compress2(compressed, &len, (unsigned char *) block_index,
730 + n, Z_BEST_SPEED) != Z_OK)
731 + quit("Index compression failed");
732 + tail.table_size = ntohl(len);
733 + pos += len + sizeof(tail);
734 + n = pos & 511;
735 + if (n) write(STDOUT_FILENO, padding, 512 - n);
736 + write(STDOUT_FILENO, compressed, len);
737 + write(STDOUT_FILENO, &tail, sizeof(tail));
738 + exit(sig != 0);
739 +}
740 +
741 +int main(int argc, char *argv[])
742 +{
743 + struct cloop_head head;
744 + unsigned char *uncompressed;
745 + unsigned long len;
746 + unsigned indexmax, zlenmax;
747 +#ifdef FIND_BEST_COMPRESSION
748 + unsigned i, j, hashmax;
749 + md5hash *hash;
750 +#endif
751 +
752 +#ifdef FIND_BEST_COMPRESSION
753 + while (argc > 1) {
754 + if (argv[1][0] == '-') {
755 + int *p = &pass;
756 + switch (argv[1][1]) {
757 + case 'i' : p = &iter;
758 + case 'n' : *p = atoi(argv[2]);
759 + argc -= 2;
760 + argv += 2;
761 + continue;
762 + }
763 + }
764 + argc--;
765 + if (argv[1][0] < '0' || argv[1][0] > '9')
766 + quit("Usage : create_compressed_fs [-n <pass>][ -i <iter>] [block size] < input > output");
767 +#else
768 + if (argc > 1) {
769 + if (argv[1][0] < '0' || argv[1][0] > '9')
770 + quit("Usage : create_compressed_fs [block size] < input > output");
771 +#endif
772 + block_size = atoi(argv[1]);
773 + }
774 + if (block_size < 4096)
775 + block_size = DEFAULT_BLOCKSIZE;
776 + fprintf(stderr, "Block size is %lu\n", block_size);
777 + zlenmax = block_size + block_size/1000 + 12;
778 +
779 + memset(&head, 0, sizeof(head));
780 + strcpy(head.preamble, CLOOP_PREAMBLE);
781 + head.num_blocks = -1;
782 + head.block_size = htonl(block_size);
783 + write(STDOUT_FILENO, &head, sizeof(head));
784 + pos = sizeof(head);
785 +
786 + compressed = (unsigned char *) malloc(zlenmax);
787 + uncompressed = (unsigned char *) malloc(block_size);
788 + block_index = (unsigned long *) malloc(indexmax = CHUNK);
789 +#ifdef FIND_BEST_COMPRESSION
790 + hash = (md5hash *) malloc(hashmax = CHUNK);
791 + if (!compressed || !uncompressed || !block_index || !hash)
792 +#else
793 + if (!compressed || !uncompressed || !block_index)
794 +#endif
795 + quit("Malloc failed");
796 +
797 + signal(SIGINT,flush_index);
798 + signal(SIGQUIT,flush_index);
799 + signal(SIGTERM,flush_index);
800 +
801 + for (n = 0; (len = readblock(uncompressed, block_size)) != 0; n++) {
802 + lastlen = len;
803 + if (n * sizeof(*block_index) >= indexmax) {
804 + block_index = (unsigned long *) realloc(block_index,
805 + indexmax += CHUNK);
806 + if (!block_index)
807 + quit("Realloc");
808 + }
809 +#ifdef FIND_BEST_COMPRESSION
810 + if (n * sizeof(*hash) >= hashmax) {
811 + hash = (md5hash *) realloc(hash, hashmax += CHUNK);
812 + if (!hash)
813 + quit("Realloc hash");
814 + }
815 + hash[n] = md5sum(uncompressed, len);
816 + j = 0x7FFFFFFF;
817 + if (n < j)
818 + j = n;
819 + for (i = 0; i < j; i++) {
820 + if (* (uint32_t *) &hash[i] == * (uint32_t *) &hash[n]
821 + && !memcmp(&hash[i],&hash[n],sizeof(*hash)))
822 + break;
823 + }
824 + if (i != j) {
825 + block_index[n] = ntohl(0x80000000 | i);
826 + fprintf(stderr, "Block %u length %lu => duplicate %lu\n",
827 + n, block_size, i);
828 + }
829 + else
830 +#endif
831 + {
832 + len = zlenmax;
833 + if (compress2(compressed, &len, uncompressed, lastlen,
834 + Z_BEST_SPEED) != Z_OK)
835 + quit("Compression failed");
836 + fprintf(stderr, "Block %u length %lu => %lu\n",
837 + n, block_size, len);
838 + write(STDOUT_FILENO, compressed, len);
839 + pos += len;
840 + block_index[n] = ntohl(len);
841 + }
842 + }
843 + flush_index(0);
844 + return 0;
845 +}
846 +#ifdef FIND_BEST_COMPRESSION
847 +}
848 +#endif
849 --- fusecloop.c
850 +++ fusecloop.c
851 @@ -65,7 +65,7 @@
853 memcpy(stbuf,&stb,sizeof stb);
854 stbuf->st_mode&=~0222;
855 - stbuf->st_size = cd.blocksize * cd.numblocks;
856 + stbuf->st_size = (loff_t) cd.blocksize * cd.numblocks;
857 /*
858 stbuf->st_mode = S_IFREG | 0444;
859 stbuf->st_nlink = 1;