Implement program for finding start of zip content

This commit is contained in:
Justine Tunney 2023-01-06 19:55:00 -08:00
parent b38a442386
commit 5dab97b6d4
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
4 changed files with 82 additions and 5 deletions

76
tool/build/zipbase.c Normal file
View file

@ -0,0 +1,76 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/zip.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s FOO.COM\n", argv[0]);
fprintf(stderr,
"prints lowest file offset (in decimal bytes) at which zip\n"
"records exist within a .com, .com.dbg, or .zip file\n");
exit(1);
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
perror(argv[1]);
exit(1);
}
size_t n;
struct stat st;
CHECK_EQ(0, fstat(fd, &st));
n = st.st_size;
uint8_t *m;
CHECK_NE(MAP_FAILED, (m = mmap(0, n, PROT_READ, MAP_PRIVATE, fd, 0)));
uint8_t *b, *d, *p;
if ((p = FindEmbeddedApe(m, n))) {
b = p;
n -= p - m;
} else {
b = m;
}
if (!(d = GetZipCdir(b, n))) {
fprintf(stderr, "%s: couldn't locate central directory\n", argv[1]);
exit(1);
}
size_t zsize = n;
uint8_t *zmap = m;
uint8_t *zbase = b;
uint8_t *zcdir = d;
DCHECK(IsZipCdir32(zbase, zsize, zcdir - zbase) ||
IsZipCdir64(zbase, zsize, zcdir - zbase));
uint64_t cf;
uint64_t lf;
uint64_t minzipoffset;
n = GetZipCdirRecords(zcdir);
minzipoffset = cf = GetZipCdirOffset(zcdir);
for (; n--; cf += ZIP_CFILE_HDRSIZE(zbase + cf)) {
CHECK_EQ(kZipCfileHdrMagic, ZIP_CFILE_MAGIC(zbase + cf));
lf = GetZipCfileOffset(zbase + cf);
minzipoffset = MIN(minzipoffset, lf);
}
printf("%ld\n", (zbase + minzipoffset) - m);
}

View file

@ -16,6 +16,7 @@
"__GNUC_PATCHLEVEL__"
"__GNUC__"
"__APPLE__"
"__EMSCRIPTEN__"
"__ANDROID__"
"__FreeBSD__"
"__NetBSD__"

View file

@ -33,7 +33,7 @@ CXX="/opt/cosmo/o/third_party/gcc/bin/x86_64-linux-musl-g++"
CCFLAGS="-O2 -fdata-sections -ffunction-sections -fno-pie -pg -mnop-mcount -mno-tls-direct-seg-refs"
CXXFLAGS="-fno-exceptions -fuse-cxa-atexit -fno-threadsafe-statics"
CPPFLAGS="-DNDEBUG -nostdinc -iquote /opt/cosmo -isystem /opt/cosmos/include -isystem /opt/cosmo/libc/isystem -include libc/integral/normalize.inc"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64 -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -L/opt/cosmos/lib -Wl,-T,/opt/cosmo/o/ape/public/ape.lds /opt/cosmo/o/ape/ape-no-modify-self.o /opt/cosmo/o/libc/crt/crt.o"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-melf_x86_64 -Wl,--gc-sections -L/opt/cosmos/lib -Wl,-T,/opt/cosmo/o/ape/public/ape.lds /opt/cosmo/o/ape/ape-no-modify-self.o /opt/cosmo/o/libc/crt/crt.o"
LDLIBS="/opt/cosmo/o/third_party/libcxx/libcxx.a /opt/cosmo/o/cosmopolitan.a"
if [ ! -d $COSMO ]; then
@ -88,9 +88,9 @@ done
if [ "$HAS_E" = "1" ]; then
set -- $CPPFLAGS "$@"
elif [ "$HAS_C" = "1" ]; then
set -- $CCFLAGS $CXXFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer
set -- $CCFLAGS $CXXFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
else
set -- $LDFLAGS $CPPFLAGS "$@" $LDLIBS
set -- $LDFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096
fi
set -- "$CXX" "$@"

View file

@ -90,9 +90,9 @@ done
if [ "$HAS_E" = "1" ]; then
set -- $CPPFLAGS "$@"
elif [ "$HAS_C" = "1" ]; then
set -- $CFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer
set -- $CFLAGS $CPPFLAGS "$@" -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
else
set -- $LDFLAGS $CPPFLAGS "$@" $LDLIBS
set -- $LDFLAGS $CPPFLAGS "$@" $LDLIBS -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=4096
fi
set -- "$CC" "$@"