From 585c86e2a439d5df23595b249eb28554764ec6b4 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 4 Nov 2023 07:19:37 -0700 Subject: [PATCH] Support \n in /zip/.args files --- examples/examples.mk | 1 + libc/str/iszipeocd32.c | 3 +++ tool/args/args.c | 17 ++++++++++++++++- tool/decode/zip.c | 13 +++++++------ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/examples/examples.mk b/examples/examples.mk index f8ef55984..fba46f3e0 100644 --- a/examples/examples.mk +++ b/examples/examples.mk @@ -93,6 +93,7 @@ EXAMPLES_DIRECTDEPS = \ THIRD_PARTY_VQSORT \ THIRD_PARTY_XED \ THIRD_PARTY_ZLIB \ + TOOL_ARGS \ TOOL_BUILD_LIB \ TOOL_VIZ_LIB diff --git a/libc/str/iszipeocd32.c b/libc/str/iszipeocd32.c index 30925daad..20c368504 100644 --- a/libc/str/iszipeocd32.c +++ b/libc/str/iszipeocd32.c @@ -42,6 +42,9 @@ int IsZipEocd32(const uint8_t *p, size_t n, size_t i) { if (ZIP_CDIR_RECORDS(p + i) * kZipCfileHdrMinSize > ZIP_CDIR_SIZE(p + i)) { return kZipErrorEocdRecordsOverflow; } + if (ZIP_CDIR_OFFSET(p + i) == 0xFFFFFFFFu) { + return kZipErrorEocdRecordsOverflow; + } if (ckd_add(&offset, ZIP_CDIR_OFFSET(p + i), ZIP_CDIR_SIZE(p + i))) { return kZipErrorEocdOffsetSizeOverflow; } diff --git a/tool/args/args.c b/tool/args/args.c index b9d10593a..5d9db1b81 100644 --- a/tool/args/args.c +++ b/tool/args/args.c @@ -38,6 +38,21 @@ static struct ZipArgs { char **oldargv; } g_zipargs; +// remap \n → newline +static char *Decode(char *arg) { + int i, j; + for (i = j = 0; arg[i]; ++i) { + if (arg[i] == '\\' && arg[i + 1] == 'n') { + arg[j++] = '\n'; + ++i; + } else { + arg[j++] = arg[i]; + } + } + arg[j] = 0; + return arg; +} + static void AddZipArg(int *argc, char ***argv, char *arg) { *argv = xrealloc(*argv, (++(*argc) + 1) * sizeof(*(*argv))); (*argv)[*argc - 1] = arg; @@ -72,7 +87,7 @@ int LoadZipArgsImpl(int *argc, char ***argv, char *data) { AddZipArg(&n, &args, (*argv)[i]); } } else { - AddZipArg(&n, &args, arg); + AddZipArg(&n, &args, Decode(arg)); } start = 0; } diff --git a/tool/decode/zip.c b/tool/decode/zip.c index 51b9df1c3..4370a44a0 100644 --- a/tool/decode/zip.c +++ b/tool/decode/zip.c @@ -48,6 +48,8 @@ * @fileoverview Zip File Disassembler. */ +uint8_t *map; + static __wur char *FormatDosDate(uint16_t dosdate) { return xasprintf("%04u-%02u-%02u", ((dosdate >> 9) & 0b1111111) + 1980, (dosdate >> 5) & 0b1111, dosdate & 0b11111); @@ -60,9 +62,9 @@ static __wur char *FormatDosTime(uint16_t dostime) { void AdvancePosition(uint8_t *map, size_t *pos, size_t off) { if (off > *pos) { - printf("\n/\t<%s>\n", "LIMBO"); - disassemblehex(&map[*pos], off - *pos, stdout); - printf("/\t\n", "LIMBO"); + /* printf("\n/\t<%s>\n", "LIMBO"); */ + /* disassemblehex(&map[*pos], off - *pos, stdout); */ + /* printf("/\t\n", "LIMBO"); */ } *pos = off; } @@ -288,8 +290,8 @@ void ShowLocalFileHeader(uint8_t *lf, uint16_t idx) { } void ShowCentralFileHeader(uint8_t *cf) { - printf("\n/\t%s (%zu %s)\n", "central directory file header", - ZIP_CFILE_HDRSIZE(cf), "bytes"); + printf("\n/\t%s (%zu %s @ %#lx)\n", "central directory file header", + ZIP_CFILE_HDRSIZE(cf), "bytes", cf - map); show(".ascii", format(b1, "%`'.*s", 4, cf), "magic"); show(".byte", _gc(xasprintf("%d", ZIP_CFILE_VERSIONMADE(cf))), "zip version made"); @@ -485,7 +487,6 @@ void DisassembleZip(const char *path, uint8_t *p, size_t n) { int main(int argc, char *argv[]) { int fd; - uint8_t *map; struct stat st; ShowCrashReports(); CHECK_EQ(2, argc);