Skip to content

Commit

Permalink
Support \n in /zip/.args files
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 4, 2023
1 parent 48e260e commit 585c86e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ EXAMPLES_DIRECTDEPS = \
THIRD_PARTY_VQSORT \
THIRD_PARTY_XED \
THIRD_PARTY_ZLIB \
TOOL_ARGS \
TOOL_BUILD_LIB \
TOOL_VIZ_LIB

Expand Down
3 changes: 3 additions & 0 deletions libc/str/iszipeocd32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 16 additions & 1 deletion tool/args/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 7 additions & 6 deletions tool/decode/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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</%s>\n", "LIMBO");
/* printf("\n/\t<%s>\n", "LIMBO"); */
/* disassemblehex(&map[*pos], off - *pos, stdout); */
/* printf("/\t</%s>\n", "LIMBO"); */
}
*pos = off;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 585c86e

Please sign in to comment.