diff options
Diffstat (limited to 'mktext.c')
| -rw-r--r-- | mktext.c | 48 |
1 files changed, 35 insertions, 13 deletions
@@ -8,8 +8,6 @@ * usage: mktext verses.keys corpus.tsv out.lz * out.lz = [uint32 LE rawlen][LZSS-packed "chap:verse text\n" lines]. */ -#define LZ_PACK -#include "lz.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -121,17 +119,41 @@ int main(int argc, char **argv) { if (missing) fprintf(stderr, "mktext: warning: %zu of %zu verses not in corpus (blank)\n", missing, nkeys); - size_t plen; - unsigned char *packed = lz_pack((unsigned char *)out, olen, &plen); + /* Pack: gzip the raw text, then strip the gzip container to the bare DEFLATE + * stream (10-byte header + 8-byte trailer) that clectio's puff() decodes. + * out.lz = [uint32 LE raw length][raw DEFLATE]. */ + { + char rawpath[4096], gzpath[4200], cmd[4300]; + FILE *rf, *of; + char *gz; + size_t gzn, deflen; + unsigned char hdr[4]; - FILE *of = fopen(argv[3], "wb"); - if (!of) { fprintf(stderr, "mktext: cannot write %s\n", argv[3]); return 1; } - unsigned char hdr[4] = { (unsigned char)olen, (unsigned char)(olen >> 8), - (unsigned char)(olen >> 16), (unsigned char)(olen >> 24) }; - fwrite(hdr, 1, 4, of); - fwrite(packed, 1, plen, of); - fclose(of); - fprintf(stderr, "mktext: %zu verses, %zu -> %zu bytes (%.1fx)\n", - nkeys, olen, plen, olen ? (double)olen / (double)plen : 0); + snprintf(rawpath, sizeof rawpath, "%s.raw", argv[3]); + rf = fopen(rawpath, "wb"); + if (!rf) { fprintf(stderr, "mktext: cannot write %s\n", rawpath); return 1; } + fwrite(out, 1, olen, rf); + fclose(rf); + + snprintf(cmd, sizeof cmd, "gzip -9nf '%s'", rawpath); + if (system(cmd) != 0) { fprintf(stderr, "mktext: gzip failed (is gzip installed?)\n"); return 1; } + snprintf(gzpath, sizeof gzpath, "%s.gz", rawpath); + gz = slurp(gzpath, &gzn); + remove(gzpath); + if (gzn < 18) { fprintf(stderr, "mktext: unexpected gzip output\n"); return 1; } + deflen = gzn - 18; /* drop 10-byte gzip header + 8-byte trailer */ + + of = fopen(argv[3], "wb"); + if (!of) { fprintf(stderr, "mktext: cannot write %s\n", argv[3]); return 1; } + hdr[0] = (unsigned char)olen; + hdr[1] = (unsigned char)(olen >> 8); + hdr[2] = (unsigned char)(olen >> 16); + hdr[3] = (unsigned char)(olen >> 24); + fwrite(hdr, 1, 4, of); + fwrite(gz + 10, 1, deflen, of); + fclose(of); + fprintf(stderr, "mktext: %zu verses, %zu -> %zu bytes (%.1fx)\n", + nkeys, olen, deflen + 4, olen ? (double)olen / (double)(deflen + 4) : 0); + } return 0; } |
