From f0cdcb370ec7023428bc421e3a8b5f5cbf1de502 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 16 May 2017 19:06:05 +0100 Subject: My own CAB-maker, which compresses. lcab produces uncompressed CAB files, which is a bit low-quality for my taste - especially when it turns out CAB files have Deflate as one of their compression options, so I can write a compressed CAB-builder in only about 100 lines of Python using the zlib module! I'd expected to have to faff about finding an implementation of LZX, but there's really no need to bother. --- fake-winterop.c | 17 +++------ makecab.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 12 deletions(-) create mode 100755 makecab.py diff --git a/fake-winterop.c b/fake-winterop.c index eb891c4..cc69179 100644 --- a/fake-winterop.c +++ b/fake-winterop.c @@ -26,7 +26,6 @@ uint32_t ResetAcls(const char16_t **pwzFiles, uint32_t cFiles) } typedef struct CabCreateContext { - char *tempdir; char *outdir; char *outfile; @@ -43,14 +42,11 @@ uint32_t CreateCabBegin(const char16_t *wzCab, const char16_t *wzCabDir, ctx->outdir = ascii(wzCabDir, true); ctx->outfile = dupcat(ctx->outdir, "/", ascii(wzCab, true), (const char *)NULL); - ctx->tempdir = dupcat(ctx->outdir, "/cabXXXXXX", (const char *)NULL); - if (!mkdtemp(ctx->tempdir)) - err(1, "mkdtemp"); ctx->nargs = 0; ctx->argsize = 16; ctx->args = snewn(ctx->argsize, char *); - ctx->args[ctx->nargs++] = dupcat("lcab", (const char *)NULL); - ctx->args[ctx->nargs++] = dupcat("-n", (const char *)NULL); + ctx->args[ctx->nargs++] = dupcat("makecab.py", (const char *)NULL); + ctx->args[ctx->nargs++] = ctx->outfile; *out_ctx = ctx; return 0; } @@ -61,16 +57,14 @@ uint32_t CreateCabAddFile(const char16_t *wzFile, const char16_t *wzToken, char *file = ascii(wzFile, true); char *file_abs = realpath(file, NULL); char *cabname = ascii(wzToken, true); - char *cab_abs = dupcat(ctx->tempdir, "/", cabname, (const char *)NULL); printf("CreateCabAddFile: %s :: %s <- %s\n", ctx->outfile, cabname, file_abs); - if (symlink(file_abs, cab_abs) < 0) - err(1, "symlink"); if (ctx->nargs + 1 >= ctx->argsize) { ctx->argsize = ctx->nargs * 5 / 4 + 16; ctx->args = sresize(ctx->args, ctx->argsize, char *); } - ctx->args[ctx->nargs++] = cab_abs; + ctx->args[ctx->nargs++] = cabname; + ctx->args[ctx->nargs++] = file_abs; return 0; } @@ -86,11 +80,10 @@ uint32_t CreateCabAddFiles(const char16_t *const *pwzFiles, uint32_t CreateCabFinish(CabCreateContext *ctx, void (*split_callback)(void)) { - if (ctx->nargs + 2 >= ctx->argsize) { + if (ctx->nargs + 1 >= ctx->argsize) { ctx->argsize = ctx->nargs * 5 / 4 + 16; ctx->args = sresize(ctx->args, ctx->argsize, char *); } - ctx->args[ctx->nargs++] = ctx->outfile; ctx->args[ctx->nargs++] = NULL; system_argv_array(ctx->args); return 0; diff --git a/makecab.py b/makecab.py new file mode 100755 index 0000000..befa94a --- /dev/null +++ b/makecab.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python + +import sys +import os +import time +import zlib +import struct +from collections import namedtuple + +CFHEADER_s = struct.Struct("<4sLLLLLBBHHHHH") +CFHEADER = namedtuple("CFHEADER", "sig res0 size res1 firstfile res2 " + "verminor vermajor folders files flags setid icabinet") +CFHEADER_sig = "MSCF" + +CFFOLDER_s = struct.Struct("> 1)) + +def checksum(data): + data += "\0" * (3 & -len(data)) # pad to multiple of 4 bytes + toret = 0 + for offset in xrange(0, len(data), 4): + toret ^= struct.unpack_from(" 0: + cabname = args.pop(0) + filename = args.pop(0) + with open(filename, "rb") as f: + filedata = f.read() + files.append((cabname, filedata, os.stat(filename).st_mtime)) + cabdata = build_cab(files) + with open(outfile, "wb") as f: + f.write(cabdata) + +if __name__ == '__main__': + main() -- cgit v1.2.3-55-g6feb