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