From: Jon Feldman Date: Sun, 11 Jun 2017 23:42:27 +0000 (-0400) Subject: rewrite code to use a zopfli-based deflate impl X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;p=console%2Fciso.git rewrite code to use a zopfli-based deflate impl --- diff --git a/Makefile b/Makefile index c1268cd..8fd7d32 100644 --- a/Makefile +++ b/Makefile @@ -8,13 +8,13 @@ INSTALL = install all : ciso ciso : ciso.o - gcc -o ciso ciso.o -lz + $(CC) -g -o ciso ciso.o -lz -lzopfli ciso.o : ciso.c - gcc -o ciso.o -c ciso.c + $(LD) -g -o ciso.o -c ciso.c install : $(INSTALL) -m 755 ciso $(USRBINDIR)/ciso clean: - rm -rf *.o + rm -rf *.o ciso diff --git a/README.markdown b/README.markdown index ceae2db..2e6702f 100644 --- a/README.markdown +++ b/README.markdown @@ -1,6 +1,10 @@ ciso is a simple commandline utility to compress PSP iso files. -This package is an OSX port of a package provided by Ubuntu: http://packages.ubuntu.com/search?keywords=ciso +This package was made to be compatible with OSX; it was a port of a package provided by Ubuntu: http://packages.ubuntu.com/search?keywords=ciso + +This variant of ciso requires libzopfli for better compression ratio; it is however, far slower. The resultant ciso files work on PPSSPP and are compatible to ones produced by the original zlib-based ciso. I'm unsure if they will work on an actual PSP due to memory constraints. + +You'll need libzopfli to build this: https://github.com/google/zopfli # Installation @@ -15,7 +19,6 @@ To decompress a cso file: To compress an iso file: - ciso level infile.iso outfile.cso + ciso iter infile.iso outfile.cso -where level ranges from 1 (fast, poor compression) to 9 (slow, high -compression). +where iter is an integer that is the number of iterations per block for zopfli to compute. For quick use, 1 is recommended. For a bit more compression, 5 is a good bet; anything higher is going to be slow and have an insignificant ratio decrease (or none at all.) Going over 10 for iterations is not recommended unless you'd like a 185MB game to take 25h to compress. diff --git a/ciso.c b/ciso.c index 9f75421..3d0d290 100644 --- a/ciso.c +++ b/ciso.c @@ -15,7 +15,6 @@ along with Foobar; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - Copyright 2005 BOOSTER */ @@ -26,6 +25,8 @@ #include /* /usr(/local)/include/zlib.h */ #include +#include + #include "ciso.h" const char *fname_in,*fname_out; @@ -163,7 +164,7 @@ int decomp_ciso(void) if (inflateInit2(&z,-15) != Z_OK) { - printf("deflateInit : %s\n", (z.msg) ? z.msg : "???"); + printf("inflateInit : %s\n", (z.msg) ? z.msg : "???"); return 1; } @@ -249,6 +250,8 @@ int comp_ciso(int level) int percent_period; int percent_cnt; int align,align_b,align_m; + unsigned char ptr; + ZopfliOptions opts; file_size = check_file_size(fin); if(file_size==(uint64_t)-1LL) @@ -273,17 +276,24 @@ int comp_ciso(int level) memset(crc_buf,0,index_size); memset(buf4,0,sizeof(buf4)); + ZopfliInitOptions(&opts); +// opts.verbose = 1; +// opts.verbose_more = 1; + opts.numiterations = level; + opts.blocksplitting = 0; // Nope. + /* init zlib */ z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL; /* show info */ - printf("Compress '%s' to '%s'\n",fname_in,fname_out); - printf("Total File Size %lld bytes\n",ciso.total_bytes); - printf("block size %d bytes\n",ciso.block_size); - printf("index align %d\n",1<>(ciso.align); /* read buffer */ - z.next_out = block_buf2; - z.avail_out = ciso.block_size*2; - z.next_in = block_buf1; - z.avail_in = fread(block_buf1, 1, ciso.block_size , fin); - if(z.avail_in != ciso.block_size) + unsigned char* next_out = 0; + size_t avail_out = 0; // Unlike zlib, this is filled in. + unsigned char* next_in = block_buf1; + size_t avail_in = fread(block_buf1, 1, ciso.block_size, fin); + if(avail_in != ciso.block_size) { printf("block=%d : read error\n",block); return 1; } - /* compress block - status = deflate(&z, Z_FULL_FLUSH);*/ - status = deflate(&z, Z_FINISH); - if (status != Z_STREAM_END) - /* if (status != Z_OK) */ - { - printf("block %d:deflate : %s[%d]\n", block,(z.msg) ? z.msg : "error",status); - return 1; - } + // Deflate with zopfli + ptr = 0; + ZopfliDeflatePart(&opts, 2, 1, next_in, 0, avail_in, &ptr, &next_out, &avail_out); - cmp_size = ciso.block_size*2 - z.avail_out; + cmp_size = avail_out; /* choise plain / compress */ if(cmp_size >= ciso.block_size) { cmp_size = ciso.block_size; - memcpy(block_buf2,block_buf1,cmp_size); + next_out = block_buf1; /* plain block mark */ index_buf[block] |= 0x80000000; } /* write compressed block */ - if(fwrite(block_buf2, 1,cmp_size , fout) != cmp_size) + if(fwrite(next_out, 1, cmp_size, fout) != cmp_size) { - printf("block %d : Write error\n",block); + printf("block %d : Write error\n", block); return 1; } + if (next_out != block_buf1) + free(next_out); + /* mark next index */ write_pos += cmp_size; - - /* term zlib */ - if (deflateEnd(&z) != Z_OK) - { - printf("deflateEnd : %s\n", (z.msg) ? z.msg : "error"); - return 1; - } } /* last position (total size)*/ @@ -403,16 +397,17 @@ int main(int argc, char *argv[]) int result; fprintf(stderr, "Compressed ISO9660 converter Ver.1.02 by BOOSTER\n"); + fprintf(stderr, " + Now with zopfli compressor support\n"); if (argc != 4) { printf("Usage: ciso level infile outfile\n"); - printf(" level: 1-9 compress ISO to CSO (1=fast/large - 9=small/slow\n"); - printf(" 0 decompress CSO to ISO\n"); + printf(" iter: {1-N} compress ISO to CSO (with N iterations per block)\n"); + printf(" 0 decompress CSO to ISO\n"); return 0; } - level = argv[1][0] - '0'; - if(level < 0 || level > 9) + sscanf(argv[1], "%d", &level); + if(level < 0) { printf("Unknown mode: %c\n", argv[1][0]); return 1;