cosmopolitan/tool/build/zipbase.c
Justine Tunney 23e235b7a5
Fix bugs in cosmocc toolchain
This change integrates e58abc1110b335a3341e8ad5821ad8e3880d9bb2 from
https://github.com/ahgamut/musl-cross-make/ which fixes the issues we
were having with our C language extension for symbolic constants. This
change also performs some code cleanup and bug fixes to getaddrinfo().
It's now possible to compile projects like ncurses, readline and python
without needing to patch anything upstream, except maybe a line or two.
Pretty soon it should be possible to build a Linux distro on Cosmo.
2023-06-08 23:44:03 -07:00

78 lines
2.5 KiB
C

#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)));
int err;
uint8_t *b, *d, *p;
if ((p = FindEmbeddedApe(m, n))) {
b = p;
n -= p - m;
} else {
b = m;
}
if (!(d = GetZipEocd(b, n, &err))) {
fprintf(stderr, "%s: couldn't locate central directory [zip error %d]\n",
argv[1], err);
exit(1);
}
size_t zsize = n;
uint8_t *zmap = m;
uint8_t *zbase = b;
uint8_t *zcdir = d;
DCHECK(IsZipEocd32(zbase, zsize, zcdir - zbase) == kZipOk ||
IsZipEocd64(zbase, zsize, zcdir - zbase) == kZipOk);
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);
}