wok rev 23757

fusecloop/extract_compressed_fs: fix v2 convertion
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 02 17:31:45 2020 +0000 (2020-05-02)
parents 8c17196bcbf2
children c8f395bd169b
files fusecloop/stuff/fusecloop.u
line diff
     1.1 --- a/fusecloop/stuff/fusecloop.u	Sat May 02 16:58:52 2020 +0100
     1.2 +++ b/fusecloop/stuff/fusecloop.u	Sat May 02 17:31:45 2020 +0000
     1.3 @@ -1229,30 +1229,91 @@
     1.4  +
     1.5  --- extract_compressed_fs.c
     1.6  +++ extract_compressed_fs.c
     1.7 -@@ -3,14 +3,19 @@
     1.8 +@@ -3,14 +3,78 @@
     1.9   #include "common_header.h"
    1.10   #define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
    1.11   
    1.12  +#include "cloopunpack.c"
    1.13  +static char *packnames[CLOOP_COMPRESSOR_MAX+1] = { CLOOP_COMPRESSOR_NAMES };
    1.14  +
    1.15 ++static unsigned char *out;
    1.16 ++static unsigned long zblock_maxsize, block_size, num_blocks;
    1.17 ++static int handle, err;
    1.18 ++static unsigned char *buffer, *clear_buffer;
    1.19 ++static struct block_info *offsets;
    1.20 ++
    1.21 ++static uLongf get_block(unsigned int i, int argc)
    1.22 ++{
    1.23 ++	unsigned long len;
    1.24 ++	uLongf ulen;
    1.25 ++	int flags = offsets[i].flags;
    1.26 ++
    1.27 ++	if (flags > CLOOP_COMPRESSOR_MAX) {
    1.28 ++		fprintf(stderr, "Block %u: unsupported compression %d \n",
    1.29 ++			i, flags);
    1.30 ++		exit(1);
    1.31 ++	}
    1.32 ++
    1.33 ++	len = offsets[i].size;
    1.34 ++	if (len > zblock_maxsize) {
    1.35 ++		fprintf(stderr, 
    1.36 ++			"Size %lu for block %u (offset %Lu) too big\n",
    1.37 ++			len, i, offsets[i].offset);
    1.38 ++		exit(1);
    1.39 ++	}
    1.40 ++
    1.41 ++	if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
    1.42 ++		fprintf(stderr, "lseek to %Lu: %s\n",
    1.43 ++			offsets[i].offset, strerror(errno));
    1.44 ++		exit(1);
    1.45 ++	}
    1.46 ++                
    1.47 ++	read(handle, out = buffer, ulen = len);
    1.48 ++
    1.49 ++	if (flags != CLOOP_COMPRESSOR_ZLIB || argc <= 2) {
    1.50 ++		ulen = block_size;
    1.51 ++		err = unpack[flags](out = clear_buffer, &ulen, buffer, len);
    1.52 ++		if (err != Z_OK) {
    1.53 ++			fprintf(stderr, "Unpack %s block %u error %d \n",
    1.54 ++				packnames[flags], i, err);
    1.55 ++			exit(1);
    1.56 ++		}
    1.57 ++		if (ulen != block_size && i != num_blocks - 1) {
    1.58 ++			fprintf(stderr, "Uncomp %s: bad len %u (%lu not %lu)\n",
    1.59 ++				packnames[flags], i, ulen, block_size);
    1.60 ++			exit(1);
    1.61 ++		}
    1.62 ++		if (argc > 2) {
    1.63 ++			len = ulen;
    1.64 ++			ulen = zblock_maxsize;
    1.65 ++			err = compress2(out = buffer, &ulen, clear_buffer, len, Z_BEST_SPEED);
    1.66 ++			if (err != Z_OK) {
    1.67 ++				fprintf(stderr, "Compress %s block %u error %d \n",
    1.68 ++					packnames[flags], i, err);
    1.69 ++				exit(1);
    1.70 ++			}
    1.71 ++		}
    1.72 ++	}
    1.73 ++	return ulen;
    1.74 ++}
    1.75 ++
    1.76   int main(int argc, char *argv[])
    1.77   {
    1.78  -	int handle;
    1.79 -+	int handle, err;
    1.80 ++	int flags;
    1.81   	struct cloop_head head;
    1.82  -	unsigned int i;
    1.83  -	unsigned long num_blocks, block_size, zblock_maxsize, lastlen = 0;
    1.84 +-	unsigned char *buffer, *clear_buffer;
    1.85 +-	struct block_info *offsets;
    1.86  +	unsigned int i, v4_header_last, global_flags;
    1.87 -+	unsigned long n, num_blocks, block_size, zblock_maxsize, len;
    1.88 ++	unsigned long n, len;
    1.89  +	uLongf ulen;
    1.90 - 	unsigned char *buffer, *clear_buffer;
    1.91 - 	struct block_info *offsets;
    1.92  +	loff_t end;
    1.93   
    1.94   	if (argc < 2 || argv[1][0] == '-') {
    1.95   		fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
    1.96 -@@ -23,23 +28,35 @@
    1.97 +@@ -23,23 +87,35 @@
    1.98   		exit(1);
    1.99   	}
   1.100   
   1.101 @@ -1293,7 +1354,7 @@
   1.102   		if (lseek64(handle, end - sizeof(tail), SEEK_SET) < 0 ||
   1.103   		    read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
   1.104   		    lseek64(handle, end - sizeof(tail) - 
   1.105 -@@ -51,60 +68,91 @@
   1.106 +@@ -51,105 +127,108 @@
   1.107   		num_blocks = ntohl(head.num_blocks);
   1.108   		table_size = ntohl(tail.table_size);
   1.109   		table = malloc(table_size);
   1.110 @@ -1384,52 +1445,60 @@
   1.111   		
   1.112   		strcpy(head.preamble, CLOOP_PREAMBLE);
   1.113   		write(STDOUT_FILENO, &head, n = sizeof(head));
   1.114 --		for (i = 0; i < num_blocks; i++) {
   1.115 + 		for (i = 0; i < num_blocks; i++) {
   1.116  -			data = __be64_to_cpu(offset);
   1.117 -+		for (i = 0; i <= num_blocks; i++) {
   1.118  +			data = __be64_to_cpu(ofs);
   1.119   			write(STDOUT_FILENO, &data, sizeof(data));
   1.120   			n += sizeof(data);
   1.121  -			offset += offsets[i].size;
   1.122 -+			ofs += offsets[i].size;
   1.123 ++			if (offsets[i].flags == CLOOP_COMPRESSOR_ZLIB)
   1.124 ++				ofs += offsets[i].size;
   1.125 ++			else
   1.126 ++				ofs += get_block(i,argc);
   1.127   		}
   1.128  -		data = __be64_to_cpu(offset);
   1.129 --		write(STDOUT_FILENO, &data, sizeof(data));
   1.130 ++		data = __be64_to_cpu(ofs);
   1.131 + 		write(STDOUT_FILENO, &data, sizeof(data));
   1.132  -		for (i = 0; i < num_blocks && lseek64(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
   1.133  -			read(handle, buffer, offsets[i].size);
   1.134  -			write(STDOUT_FILENO, buffer, offsets[i].size);
   1.135  -			n += offsets[i].size;
   1.136  -		}
   1.137 --		n &= 0x1FF;
   1.138 --		if (n) {
   1.139 --			memset(buffer, 0, 512);
   1.140 --			write(STDOUT_FILENO, buffer, 512 - n);
   1.141 --		}
   1.142 ++	}
   1.143 ++	for (i = 0; i < num_blocks; i++) {
   1.144 ++	
   1.145 ++		fprintf(stderr, "Block %u %s at %llu length %lu ",
   1.146 ++			i, packnames[offsets[i].flags], offsets[i].offset, len);
   1.147 ++		ulen = get_block(i, argc);
   1.148 ++		fprintf(stderr, " => %lu\n", ulen);
   1.149 ++		write(STDOUT_FILENO, out, ulen);
   1.150 ++		n += ulen;
   1.151 ++	}
   1.152 ++	if (argc > 2) {
   1.153 + 		n &= 0x1FF;
   1.154 + 		if (n) {
   1.155 + 			memset(buffer, 0, 512);
   1.156 + 			write(STDOUT_FILENO, buffer, 512 - n);
   1.157 + 		}
   1.158  -		return 0;
   1.159 - 	}
   1.160 +-	}
   1.161  -	
   1.162 - 	for (i = 0; i < num_blocks; i++) {
   1.163 +-	for (i = 0; i < num_blocks; i++) {
   1.164  -		unsigned long destlen = block_size;
   1.165  -		unsigned int size = offsets[i].size;
   1.166 -+		unsigned char *out;
   1.167 -+		int flags = offsets[i].flags;
   1.168 - 
   1.169 - 		if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
   1.170 - 			fprintf(stderr, "lseek to %Lu: %s\n",
   1.171 -@@ -112,44 +160,61 @@
   1.172 - 			exit(1);
   1.173 - 		}
   1.174 -                 
   1.175 +-
   1.176 +-		if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
   1.177 +-			fprintf(stderr, "lseek to %Lu: %s\n",
   1.178 +-				offsets[i].offset, strerror(errno));
   1.179 +-			exit(1);
   1.180 +-		}
   1.181 +-                
   1.182  -		if (size > zblock_maxsize) {
   1.183 -+		len = offsets[i].size;
   1.184 -+		if (len > zblock_maxsize) {
   1.185 - 			fprintf(stderr, 
   1.186 +-			fprintf(stderr, 
   1.187  -				"Size %u for block %u (offset %Lu) too big\n",
   1.188  -				size, i, offsets[i].offset);
   1.189 -+				"Size %lu for block %u (offset %Lu) too big\n",
   1.190 -+				len, i, offsets[i].offset);
   1.191 - 			exit(1);
   1.192 - 		}
   1.193 +-			exit(1);
   1.194 +-		}
   1.195  -		read(handle, buffer, size);
   1.196  -
   1.197  -		fprintf(stderr, "Block %u at %llu length %u",
   1.198 @@ -1438,7 +1507,7 @@
   1.199  -				   buffer, size)) {
   1.200  -		case Z_OK:
   1.201  -			break;
   1.202 - 
   1.203 +-
   1.204  -		case Z_MEM_ERROR:
   1.205  -			fprintf(stderr, "Uncomp: oom block %u\n", i);
   1.206  -			exit(1);
   1.207 @@ -1449,61 +1518,18 @@
   1.208  -
   1.209  -		case Z_DATA_ERROR:
   1.210  -			fprintf(stderr, "Uncomp: input corrupt %u\n", i);
   1.211 -+		if (argc <= 2) {
   1.212 -+			fprintf(stderr, "Block %u at %llu length %lu ",
   1.213 -+				i, offsets[i].offset, len);
   1.214 -+		}
   1.215 -+
   1.216 -+		read(handle, out = buffer, ulen = len);
   1.217 -+
   1.218 -+		if (flags > CLOOP_COMPRESSOR_MAX) {
   1.219 -+			fprintf(stderr, "Block %u: unsupported compression %d \n",
   1.220 -+				i, flags);
   1.221 - 			exit(1);
   1.222 -+		}
   1.223 - 
   1.224 +-			exit(1);
   1.225 +-
   1.226  -		default:
   1.227  -			fprintf(stderr, "Uncomp: unknown error %u\n", i);
   1.228  -			exit(1);
   1.229 -+		if (flags != CLOOP_COMPRESSOR_ZLIB || argc <= 2) {
   1.230 -+			ulen = block_size;
   1.231 -+			err = unpack[flags](out = clear_buffer, &ulen, buffer, len);
   1.232 -+			if (err != Z_OK) {
   1.233 -+				fprintf(stderr, "Unpack %s block %u error %d \n",
   1.234 -+					packnames[flags], i, err);
   1.235 -+				exit(1);
   1.236 -+			}
   1.237 -+			if (argc > 2) {
   1.238 -+				err = compress2(out = buffer, &ulen, clear_buffer, ulen, Z_BEST_SPEED);
   1.239 -+				if (err != Z_OK) {
   1.240 -+					fprintf(stderr, "Compress %s block %u error %d \n",
   1.241 -+						packnames[flags], i, err);
   1.242 -+					exit(1);
   1.243 -+				}
   1.244 -+			}
   1.245 -+			else {
   1.246 -+				fprintf(stderr, "=> %lu\n", ulen);
   1.247 -+				if (ulen != block_size && i != num_blocks - 1) {
   1.248 -+					fprintf(stderr, "Uncomp %s: bad len %u (%lu not %lu)\n",
   1.249 -+						packnames[flags], i, ulen, block_size);
   1.250 -+					exit(1);
   1.251 -+				}
   1.252 -+			}
   1.253 - 		}
   1.254 +-		}
   1.255  -		fprintf(stderr, " => %lu\n", destlen);
   1.256  -		if (destlen != block_size && i != num_blocks - 1) {
   1.257  -			fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
   1.258  -				destlen, block_size);
   1.259  -			exit(1);
   1.260 -+		write(STDOUT_FILENO, out, ulen);
   1.261 -+		n += ulen;
   1.262 -+	}
   1.263 -+	if (argc > 2) {
   1.264 -+		n &= 0x1FF;
   1.265 -+		if (n) {
   1.266 -+			memset(buffer, 0, 512);
   1.267 -+			write(STDOUT_FILENO, buffer, 512 - n);
   1.268 - 		}
   1.269 +-		}
   1.270  -		write(STDOUT_FILENO, clear_buffer, (lastlen != 0 && (i+1) == num_blocks) ? lastlen : block_size);
   1.271   	}
   1.272   	return 0;