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

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