wok-current view fusecloop/stuff/fusecloop.u @ rev 13445
Add: claws-mail-clamd
author | Eric Joseph-Alexandre <erjo@slitaz.org> |
---|---|
date | Sun Oct 07 01:25:22 2012 +0200 (2012-10-07) |
parents | 071c9a026a01 |
children | 1e434243dc31 |
line source
1 --- compressed_loop.h
2 +++ compressed_loop.h
3 @@ -41,6 +41,74 @@
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 + int i, j;
64 +
65 + for (i = n; i-- > 0; )
66 + offsets[i].size = ntohl(ofs32[i]);
67 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
68 + offsets[i].offset = j;
69 + j += offsets[i].size;
70 + }
71 + return (char *) "32BE v3.0";
72 + }
73 +}
74 +
75 /* Cloop suspend IOCTL */
76 #define CLOOP_SUSPEND 0x4C07
79 --- cloopreader.h
80 +++ cloopreader.h
81 @@ -33,7 +33,7 @@
82 int numblocks;
83 ulong blocksize;
85 - loff_t* toc; /* Data index */
86 + struct block_info *toc; /* Data index */
87 size_t tocsize;
89 unsigned char* cblock; /* Compressed block */
91 --- cloopreader.c
92 +++ cloopreader.c
93 @@ -59,10 +59,32 @@
95 ALLOC(c->pblock,c->blocksize);
97 - c->tocsize=sizeof *c->toc * (c->numblocks+1); /* One extra address is position of EOF */
98 - ALLOC(c->toc,c->tocsize);
99 + if (c->numblocks + 1 == 0) {
100 + struct cloop_tail tail;
101 + loff_t end = lseek(c->fh,0,SEEK_END); /* lseek(,-n,SEEK_END) buggy ? */
102 + void *p;
103 + ulong toclen, len;
105 - OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
106 + OP(lseek(c->fh, end - sizeof(tail), SEEK_SET));
107 + OP(read_all(c->fh, &tail, sizeof(tail)));
108 + c->numblocks = ntohl(tail.num_blocks);
109 + c->tocsize = sizeof(*c->toc) * c->numblocks;
110 + len = ntohl(tail.table_size);
111 + toclen = (ntohl(tail.index_size) & 255) * c->numblocks;
112 + OP(lseek(c->fh, end - sizeof(tail) - len, SEEK_SET));
113 + ALLOC(c->toc,c->tocsize);
114 + ALLOC(p,len);
115 + OP(read_all(c->fh,p,len)); /* read Data Index */
116 + if (uncompress((void *)c->toc,&toclen,p,len) != Z_OK)
117 + exit(1);
118 + free(p);
119 + }
120 + else {
121 + c->tocsize = sizeof(*c->toc) * c->numblocks;
122 + ALLOC(c->toc,c->tocsize);
123 + OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
124 + }
125 + build_index(c->toc, c->numblocks);
126 c->cblocksizecur=0;
127 c->curblock=-1;
128 return 0;
129 @@ -79,10 +101,10 @@
130 if(page>=c->numblocks){errno=EFAULT;return -1;}
131 c->curblock=page;
133 - bprintf("Seeking to 0x%Lx\n",btc(c->toc[page]));
134 - OP(lseek(c->fh,btc(c->toc[page]), SEEK_SET));
135 + bprintf("Seeking to 0x%Lx\n",c->toc[page].offset);
136 + OP(lseek(c->fh,c->toc[page].offset, SEEK_SET));
138 - c->cblocksize=btc(c->toc[page+1]) - btc(c->toc[page]);
139 + c->cblocksize=c->toc[page].size;
140 bprintf("Compressed size=%lu\n",c->cblocksize);
141 if(c->cblocksize > c->cblocksizecur){
142 if(c->cblocksizecur)free(c->cblock);
144 --- extract_compressed_fs.c
145 +++ extract_compressed_fs.c
146 @@ -1,15 +1,19 @@
147 /* Extracts a filesystem back from a compressed fs file */
148 +#define _LARGEFILE64_SOURCE
149 #include "common_header.h"
150 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
152 int main(int argc, char *argv[])
153 {
154 int handle;
155 struct cloop_head head;
156 unsigned int i;
157 + unsigned long num_blocks, block_size, zblock_maxsize;
158 unsigned char *buffer, *clear_buffer;
159 + struct block_info *offsets;
161 - if (argc != 2) {
162 - fprintf(stderr, "Need filename\n");
163 + if (argc < 2 || argv[1][0] == '-') {
164 + fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
165 exit(1);
166 }
168 @@ -24,44 +28,91 @@
169 exit(1);
170 }
172 - buffer = malloc(ntohl(head.block_size) + ntohl(head.block_size)/1000
173 - + 12 + 4);
174 - clear_buffer = malloc(ntohl(head.block_size));
175 - fprintf(stderr, "%u blocks of size %u. Preamble:\n%s\n",
176 - ntohl(head.num_blocks), ntohl(head.block_size), head.preamble);
177 + num_blocks = ntohl(head.num_blocks);
178 + block_size = ntohl(head.block_size);
179 + zblock_maxsize = block_size + block_size/1000 + 12 + 4;
180 + buffer = malloc(zblock_maxsize);
181 + clear_buffer = malloc(block_size);
183 - for (i = 0; i < ntohl(head.num_blocks); i++) {
184 - int currpos;
185 - unsigned long destlen = ntohl(head.block_size);
186 - loff_t offset[2];
187 - unsigned int size;
188 + if (num_blocks == (unsigned long) -1) {
189 + void *table;
190 + struct cloop_tail tail;
191 + unsigned long len, table_size;
192 + loff_t end = lseek64(handle, 0, SEEK_END);
193 +
194 + if (lseek64(handle, end - sizeof(tail), SEEK_SET) < 0 ||
195 + read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
196 + lseek64(handle, end - sizeof(tail) -
197 + ntohl(tail.table_size), SEEK_SET) < 0) {
198 + perror("Reading tail\n");
199 + exit(1);
200 + }
201 + head.num_blocks = tail.num_blocks;
202 + num_blocks = ntohl(head.num_blocks);
203 + table_size = ntohl(tail.table_size);
204 + table = malloc(table_size);
205 + len = i = num_blocks * (ntohl(tail.index_size) & 255);
206 + offsets = malloc(num_blocks * sizeof(*offsets));
207 + if (!table || !offsets ||
208 + read(handle, table, table_size) != table_size ||
209 + uncompress((void *)offsets, &len, table, table_size) != Z_OK ||
210 + len != i) {
211 + perror("Reading index\n");
212 + exit(1);
213 + }
214 + free(table);
215 + }
216 + else {
217 + offsets = malloc(i = num_blocks * sizeof(*offsets));
218 + if (!offsets || read(handle, offsets, i) != i) {
219 + perror("Reading index\n");
220 + exit(1);
221 + }
222 + }
223 +
224 + fprintf(stderr, "%lu blocks of size %lu. Preamble:\n%s\n",
225 + num_blocks, block_size, head.preamble);
226 + fprintf(stderr, "Index %s.\n", build_index(offsets, num_blocks));
227 +
228 + if (argc > 2) {
229 + loff_t data, offset = ((num_blocks + 1) * sizeof(offset)) + sizeof(head);
230 +
231 + strcpy(head.preamble, CLOOP_PREAMBLE);
232 + write(STDOUT_FILENO, &head, sizeof(head));
233 + for (i = 0; i < num_blocks; i++) {
234 + data = __be64_to_cpu(offset);
235 + write(STDOUT_FILENO, &data, sizeof(data));
236 + offset += offsets[i].size;
237 + }
238 + data = __be64_to_cpu(offset);
239 + write(STDOUT_FILENO, &data, sizeof(data));
240 + for (i = 0; i < num_blocks && lseek64(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
241 + read(handle, buffer, offsets[i].size);
242 + write(STDOUT_FILENO, buffer, offsets[i].size);
243 + }
244 + return 0;
245 + }
246 +
247 + for (i = 0; i < num_blocks; i++) {
248 + unsigned long destlen = block_size;
249 + unsigned int size = offsets[i].size;
251 - read(handle, &offset, 2*sizeof(loff_t));
252 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
253 -
254 - currpos = lseek(handle, 0, SEEK_CUR);
255 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
256 + if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
257 fprintf(stderr, "lseek to %Lu: %s\n",
258 - __be64_to_cpu(offset[0]), strerror(errno));
259 + offsets[i].offset, strerror(errno));
260 exit(1);
261 }
263 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
264 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
265 - + 12 + 4) {
266 + if (size > zblock_maxsize) {
267 fprintf(stderr,
268 "Size %u for block %u (offset %Lu) too big\n",
269 - size, i, __be64_to_cpu(offset[0]));
270 + size, i, offsets[i].offset);
271 exit(1);
272 }
273 read(handle, buffer, size);
274 - if (lseek(handle, currpos, SEEK_SET) < 0) {
275 - perror("seeking");
276 - exit(1);
277 - }
279 - fprintf(stderr, "Block %u length %u => %lu\n",
280 - i, size, destlen);
281 + fprintf(stderr, "Block %u at %llu length %u => %lu\n",
282 + i, offsets[i].offset, size, destlen);
283 if (i == 3) {
284 fprintf(stderr,
285 "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
286 @@ -105,12 +156,12 @@
287 fprintf(stderr, "Uncomp: unknown error %u\n", i);
288 exit(1);
289 }
290 - if (destlen != ntohl(head.block_size)) {
291 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
292 - destlen, ntohl(head.block_size));
293 + if (destlen != block_size) {
294 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
295 + destlen, block_size);
296 exit(1);
297 }
298 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
299 + write(STDOUT_FILENO, clear_buffer, block_size);
300 }
301 return 0;
302 }
304 --- Makefile
305 +++ Makefile
306 @@ -1,16 +1,19 @@
307 PROGNAME=fusecloop
308 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
309 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
310 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
311 FUSECFLAGS=`pkg-config fuse --cflags`
312 FUSELDFLAGS=`pkg-config fuse --libs`
314 CFLAGS= -Wall
316 -all: fusecloop extract_compressed_fs
317 +all: fusecloop extract_compressed_fs create_compressed_fs
319 extract_compressed_fs: extract_compressed_fs.c
320 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
322 +create_compressed_fs: create_compressed_fs.c
323 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
324 +
325 fusecloop: fusecloop.c cloopreader.o strver debug.o
326 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
330 --- create_compressed_fs.c
331 +++ create_compressed_fs.c
332 @@ -0,0 +1,147 @@
333 +#ifdef FIND_BEST_COMPRESSION
334 +#include <compress.h>
335 +extern "C" {
336 +#include <stdlib.h>
337 +#include <string.h>
338 +
339 +static int best_compress(unsigned char *compressed,
340 + unsigned long *compressed_len,
341 + unsigned char *uncompressed,
342 + unsigned long uncompressed_len)
343 +{
344 + int i, j, err;
345 + unsigned char *buf[2];
346 + unsigned len;
347 + unsigned long llen, best = *compressed_len * 2;
348 + static unsigned char *buffer;
349 + static unsigned long buffersz;
350 +
351 + if (buffersz < *compressed_len) {
352 + if (buffer) free(buffer);
353 + buffer = (unsigned char *) malloc(buffersz = *compressed_len);
354 + if (!buffer) return Z_MEM_ERROR;
355 + }
356 + buf[0] = compressed;
357 + buf[1] = buffer;
358 + for (i = j = 0; i <= 10; i++) {
359 + llen = len = *compressed_len;
360 + if (i == 10)
361 + err = (compress_zlib(shrink_extreme, buf[j],
362 + len, uncompressed,
363 + uncompressed_len)) ? Z_OK : Z_DATA_ERROR;
364 + else {
365 + err = compress2(buf[j], &llen, uncompressed,
366 + uncompressed_len, i);
367 + len = llen;
368 + }
369 + if (err != Z_OK) return err;
370 + if (len < best) {
371 + best = len;
372 + j = 1 - j;
373 + }
374 + }
375 + *compressed_len = best;
376 + if (j == 0)
377 + memcpy(compressed, buffer, best);
378 + return err;
379 +}
380 +#define compress2(a,b,c,d,e) best_compress(a,b,c,d)
381 +#endif
382 +
383 +/* Creates a compressed file */
384 +#include "common_header.h"
385 +
386 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V3.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
387 +#define CHUNK 65536
388 +#define DEFAULT_BLOCKSIZE 65536
389 +
390 +static void quit(const char *s)
391 +{
392 + fprintf(stderr, "%s\n", s);
393 + exit(1);
394 +}
395 +
396 +static int readblock(unsigned char *buffer, int n)
397 +{
398 + int i;
399 +
400 + memset(buffer, 0, n);
401 + for (i = 0 ; i < n;) {
402 + int j = read(STDIN_FILENO, buffer + i, n - i);
403 + if (j < 0 && errno == EINTR) continue;
404 + if (j <= 0) break;
405 + i += j;
406 + }
407 + return i;
408 +}
409 +
410 +int main(int argc, char *argv[])
411 +{
412 + struct cloop_head head;
413 + struct cloop_tail tail;
414 + unsigned long block_size = 0;
415 + unsigned char *compressed, *uncompressed;
416 + unsigned long *index;
417 + int n, indexmax, zlenmax;
418 + unsigned long len, pos;
419 + static char padding[512];
420 +
421 + if (argc > 1) {
422 + if (argv[1][0] < '0' || argv[1][0] > '9')
423 + quit("Usage : create_compressed_fs [block size] < input > output");
424 + block_size = atoi(argv[1]);
425 + }
426 + if (block_size < 4096)
427 + block_size = DEFAULT_BLOCKSIZE;
428 + fprintf(stderr, "Block size is %lu\n", block_size);
429 + zlenmax = block_size + block_size/1000 + 12;
430 +
431 + memset(&head, 0, sizeof(head));
432 + strcpy(head.preamble, CLOOP_PREAMBLE);
433 + head.num_blocks = -1;
434 + head.block_size = htonl(block_size);
435 + write(STDOUT_FILENO, &head, sizeof(head));
436 + pos = sizeof(head);
437 +
438 + compressed = (unsigned char *) malloc(zlenmax);
439 + uncompressed = (unsigned char *) malloc(block_size);
440 + index = (unsigned long *) malloc(indexmax = CHUNK);
441 + if (!compressed || !uncompressed || !index)
442 + quit("Malloc failed");
443 +
444 + for (n = 0; readblock(uncompressed, block_size); n++) {
445 + len = zlenmax;
446 + if (compress2(compressed, &len, uncompressed, block_size,
447 + Z_BEST_COMPRESSION) != Z_OK)
448 + quit("Compression failed");
449 + fprintf(stderr, "Block %u length %lu => %lu\n",
450 + n, block_size, len);
451 + write(STDOUT_FILENO, compressed, len);
452 + pos += len;
453 + if (n * sizeof(*index) >= indexmax) {
454 + index = (unsigned long *) realloc(index,
455 + indexmax += CHUNK);
456 + if (!index)
457 + quit("Realloc");
458 + }
459 + index[n] = ntohl(len);
460 + }
461 + tail.index_size = ntohl(sizeof(*index));
462 + tail.num_blocks = ntohl(n);
463 + n *= sizeof(*index);
464 + len = n + n/1000 + 12;
465 + compressed = (unsigned char *) realloc(compressed, n);
466 + if (!compressed || compress2(compressed, &len, (unsigned char *) index,
467 + n, Z_BEST_COMPRESSION) != Z_OK)
468 + quit("Index compression failed");
469 + tail.table_size = ntohl(len);
470 + pos += len + sizeof(tail);
471 + n = pos & 511;
472 + if (n) write(STDOUT_FILENO, padding, 512 - n);
473 + write(STDOUT_FILENO, compressed, len);
474 + write(STDOUT_FILENO, &tail, sizeof(tail));
475 + return 0;
476 +}
477 +#ifdef FIND_BEST_COMPRESSION
478 +}
479 +#endif
481 --- fusecloop.c
482 +++ fusecloop.c
483 @@ -65,7 +65,7 @@
485 memcpy(stbuf,&stb,sizeof stb);
486 stbuf->st_mode&=~0222;
487 - stbuf->st_size = cd.blocksize * cd.numblocks;
488 + stbuf->st_size = (loff_t) cd.blocksize * cd.numblocks;
489 /*
490 stbuf->st_mode = S_IFREG | 0444;
491 stbuf->st_nlink = 1;