Skip to content

Commit

Permalink
Correct misunderstanding with zip64 extra records
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 18, 2023
1 parent dbd8176 commit 3e6d536
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 78 deletions.
8 changes: 3 additions & 5 deletions libc/log/startfatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/log/internal.h"
#include "libc/errno.h"
#include "libc/thread/thread.h"

/**
Expand All @@ -30,8 +30,6 @@
relegated void __start_fatal(const char *file, int line) {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
__restore_tty();
kprintf("%r%serror%s:%s:%d:%s%s: ", !__nocolor ? "\e[J\e[30;101m" : "",
!__nocolor ? "\e[94;49m" : "", file, line,
firstnonnull(program_invocation_short_name, "unknown"),
!__nocolor ? "\e[0m" : "");
kprintf("%r%serror%s:%s:%d%s: ", !__nocolor ? "\e[J\e[30;101m" : "",
!__nocolor ? "\e[94;49m" : "", file, line, !__nocolor ? "\e[0m" : "");
}
21 changes: 11 additions & 10 deletions libc/str/getzipcfilecompressedsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
/**
* Returns compressed size in bytes from zip central directory header.
*/
uint64_t GetZipCfileCompressedSize(const uint8_t *z) {
uint64_t x;
const uint8_t *p, *pe;
if ((x = ZIP_CFILE_COMPRESSEDSIZE(z)) == 0xFFFFFFFF) {
for (p = ZIP_CFILE_EXTRA(z), pe = p + ZIP_CFILE_EXTRASIZE(z); p < pe;
p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 &&
8 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + 8);
int64_t GetZipCfileCompressedSize(const uint8_t *z) {
if (ZIP_CFILE_COMPRESSEDSIZE(z) != 0xFFFFFFFFu) {
return ZIP_CFILE_COMPRESSEDSIZE(z);
}
const uint8_t *p = ZIP_CFILE_EXTRA(z);
const uint8_t *pe = p + ZIP_CFILE_EXTRASIZE(z);
for (; p < pe; p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {
if (8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p));
}
}
}
return x;
return -1;
}
28 changes: 18 additions & 10 deletions libc/str/getzipcfileoffset.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@
/**
* Returns offset of local file header.
*/
uint64_t GetZipCfileOffset(const uint8_t *z) {
uint64_t x;
const uint8_t *p, *pe;
if ((x = ZIP_CFILE_OFFSET(z)) == 0xFFFFFFFF) {
for (p = ZIP_CFILE_EXTRA(z), pe = p + ZIP_CFILE_EXTRASIZE(z); p < pe;
p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 &&
16 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + 16);
int64_t GetZipCfileOffset(const uint8_t *z) {
if (ZIP_CFILE_OFFSET(z) != 0xFFFFFFFFu) {
return ZIP_CFILE_OFFSET(z);
}
const uint8_t *p = ZIP_CFILE_EXTRA(z);
const uint8_t *pe = p + ZIP_CFILE_EXTRASIZE(z);
for (; p < pe; p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {
int offset = 0;
if (ZIP_CFILE_COMPRESSEDSIZE(z) == 0xFFFFFFFFu) {
offset += 8;
}
if (ZIP_CFILE_UNCOMPRESSEDSIZE(z) == 0xFFFFFFFFu) {
offset += 8;
}
if (offset + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + offset);
}
}
}
return x;
return -1;
}
25 changes: 15 additions & 10 deletions libc/str/getzipcfileuncompressedsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@
/**
* Returns uncompressed size in bytes from zip central directory header.
*/
uint64_t GetZipCfileUncompressedSize(const uint8_t *z) {
uint64_t x;
const uint8_t *p, *pe;
if ((x = ZIP_CFILE_UNCOMPRESSEDSIZE(z)) == 0xFFFFFFFF) {
for (p = ZIP_CFILE_EXTRA(z), pe = p + ZIP_CFILE_EXTRASIZE(z); p < pe;
p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 &&
0 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + 0);
int64_t GetZipCfileUncompressedSize(const uint8_t *z) {
if (ZIP_CFILE_UNCOMPRESSEDSIZE(z) != 0xFFFFFFFFu) {
return ZIP_CFILE_UNCOMPRESSEDSIZE(z);
}
const uint8_t *p = ZIP_CFILE_EXTRA(z);
const uint8_t *pe = p + ZIP_CFILE_EXTRASIZE(z);
for (; p < pe; p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {
int offset = 0;
if (ZIP_CFILE_COMPRESSEDSIZE(z) == 0xFFFFFFFFu) {
offset += 8;
}
if (offset + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + offset);
}
}
}
return x;
return -1;
}
21 changes: 11 additions & 10 deletions libc/str/getziplfilecompressedsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
/**
* Returns compressed size in bytes from zip local file header.
*/
uint64_t GetZipLfileCompressedSize(const uint8_t *z) {
uint64_t x;
const uint8_t *p, *pe;
if ((x = ZIP_LFILE_COMPRESSEDSIZE(z)) == 0xFFFFFFFF) {
for (p = ZIP_LFILE_EXTRA(z), pe = p + ZIP_LFILE_EXTRASIZE(z); p < pe;
p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 &&
8 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + 8);
int64_t GetZipLfileCompressedSize(const uint8_t *z) {
if (ZIP_LFILE_COMPRESSEDSIZE(z) != 0xFFFFFFFFu) {
return ZIP_LFILE_COMPRESSEDSIZE(z);
}
const uint8_t *p = ZIP_LFILE_EXTRA(z);
const uint8_t *pe = p + ZIP_LFILE_EXTRASIZE(z);
for (; p < pe; p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {
if (8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p));
}
}
}
return x;
return -1;
}
26 changes: 15 additions & 11 deletions libc/str/getziplfileuncompressedsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
/**
* Returns uncompressed size in bytes from zip local file header.
*/
uint64_t GetZipLfileUncompressedSize(const uint8_t *z) {
uint64_t x;
const uint8_t *p, *pe;
x = ZIP_LFILE_UNCOMPRESSEDSIZE(z);
if (x == 0xFFFFFFFF) {
for (p = ZIP_LFILE_EXTRA(z), pe = p + ZIP_LFILE_EXTRASIZE(z); p < pe;
p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64 &&
0 + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + 0);
int64_t GetZipLfileUncompressedSize(const uint8_t *z) {
if (ZIP_LFILE_UNCOMPRESSEDSIZE(z) != 0xFFFFFFFFu) {
return ZIP_LFILE_UNCOMPRESSEDSIZE(z);
}
const uint8_t *p = ZIP_LFILE_EXTRA(z);
const uint8_t *pe = p + ZIP_LFILE_EXTRASIZE(z);
for (; p < pe; p += ZIP_EXTRA_SIZE(p)) {
if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {
int offset = 0;
if (ZIP_LFILE_COMPRESSEDSIZE(z) == 0xFFFFFFFFu) {
offset += 8;
}
if (offset + 8 <= ZIP_EXTRA_CONTENTSIZE(p)) {
return READ64LE(ZIP_EXTRA_CONTENT(p) + offset);
}
}
}
return x;
return -1;
}
11 changes: 6 additions & 5 deletions libc/zip.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

#define kZipLfileHdrMagic ZM_(0x04034b50) /* PK♥♦ "PK\3\4" */
#define kZipLfileHdrMinSize 30
#define kZipLfileOffsetVersionNeeded 4
#define kZipLfileOffsetGeneralflag 6
#define kZipLfileOffsetCompressionmethod 8
#define kZipLfileOffsetLastmodifiedtime 10
Expand Down Expand Up @@ -228,11 +229,11 @@ uint64_t GetZipCdirRecords(const uint8_t *);
const void *GetZipCdirComment(const uint8_t *);
uint64_t GetZipCdirSize(const uint8_t *);
uint64_t GetZipCdirCommentSize(const uint8_t *);
uint64_t GetZipCfileUncompressedSize(const uint8_t *);
uint64_t GetZipCfileCompressedSize(const uint8_t *);
uint64_t GetZipCfileOffset(const uint8_t *);
uint64_t GetZipLfileUncompressedSize(const uint8_t *);
uint64_t GetZipLfileCompressedSize(const uint8_t *);
int64_t GetZipCfileCompressedSize(const uint8_t *);
int64_t GetZipCfileUncompressedSize(const uint8_t *);
int64_t GetZipCfileOffset(const uint8_t *);
int64_t GetZipLfileCompressedSize(const uint8_t *);
int64_t GetZipLfileUncompressedSize(const uint8_t *);
void GetZipCfileTimestamps(const uint8_t *, struct timespec *,
struct timespec *, struct timespec *, int);

Expand Down
6 changes: 3 additions & 3 deletions tool/build/apelink.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ static void LoadSymbols(Elf64_Ehdr *e, Elf64_Off size, const char *path) {
WRITE32LE(cfile, kZipCfileHdrMagic);
cfile[4] = kZipCosmopolitanVersion;
cfile[5] = kZipOsUnix;
cfile[6] = kZipEra1993;
cfile[6] = kZipEra2001;
WRITE16LE(cfile + kZipCfileOffsetCompressionmethod, kZipCompressionDeflate);
WRITE16LE(cfile + kZipCfileOffsetLastmodifieddate, DOS_DATE(2023, 7, 29));
WRITE16LE(cfile + kZipCfileOffsetLastmodifiedtime, DOS_TIME(0, 0, 0));
Expand All @@ -690,8 +690,8 @@ static void LoadSymbols(Elf64_Ehdr *e, Elf64_Off size, const char *path) {
unsigned char *lfile = Malloc(lfile_size);
bzero(lfile, lfile_size);
WRITE32LE(lfile, kZipLfileHdrMagic);
cfile[4] = kZipEra1993;
cfile[5] = kZipOsDos;
WRITE16LE(lfile + kZipLfileOffsetVersionNeeded, kZipEra2001);
WRITE16LE(lfile + kZipLfileOffsetGeneralflag, kZipGflagUtf8);
WRITE16LE(lfile + kZipLfileOffsetCompressionmethod, kZipCompressionDeflate);
WRITE16LE(lfile + kZipLfileOffsetLastmodifieddate, DOS_DATE(2023, 7, 29));
WRITE16LE(lfile + kZipLfileOffsetLastmodifiedtime, DOS_TIME(0, 0, 0));
Expand Down
28 changes: 14 additions & 14 deletions tool/net/redbean.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ struct Strings {
struct String {
size_t n;
const char *s;
} * p;
} *p;
};

struct DeflateGenerator {
Expand Down Expand Up @@ -291,7 +291,7 @@ static struct Servers {
struct Server {
int fd;
struct sockaddr_in addr;
} * p;
} *p;
} servers;

static struct Freelist {
Expand All @@ -305,7 +305,7 @@ static struct Unmaplist {
int f;
void *p;
size_t n;
} * p;
} *p;
} unmaplist;

static struct Psks {
Expand All @@ -316,7 +316,7 @@ static struct Psks {
char *identity;
size_t identity_len;
char *s;
} * p;
} *p;
} psks;

static struct Suites {
Expand All @@ -335,7 +335,7 @@ static struct Redirects {
int code;
struct String path;
struct String location;
} * p;
} *p;
} redirects;

static struct Assets {
Expand All @@ -350,16 +350,16 @@ static struct Assets {
struct File {
struct String path;
struct stat st;
} * file;
} * p;
} *file;
} *p;
} assets;

static struct TrustedIps {
size_t n;
struct TrustedIp {
uint32_t ip;
uint32_t mask;
} * p;
} *p;
} trustedips;

struct TokenBucket {
Expand Down Expand Up @@ -393,7 +393,7 @@ static struct Shared {
#undef C
} c;
pthread_spinlock_t montermlock;
} * shared;
} *shared;

static const char kCounterNames[] =
#define C(x) #x "\0"
Expand Down Expand Up @@ -3695,8 +3695,8 @@ static void StoreAsset(const char *path, size_t pathlen, const char *data,
p = WRITE16LE(p, mtime);
p = WRITE16LE(p, mdate);
p = WRITE32LE(p, crc);
p = WRITE32LE(p, MIN(uselen, 0xffffffff));
p = WRITE32LE(p, MIN(datalen, 0xffffffff));
p = WRITE32LE(p, 0xffffffffu);
p = WRITE32LE(p, 0xffffffffu);
p = WRITE16LE(p, pathlen);
p = WRITE16LE(p, v[2].iov_len);
v[1].iov_len = pathlen;
Expand Down Expand Up @@ -3755,16 +3755,16 @@ static void StoreAsset(const char *path, size_t pathlen, const char *data,
p = WRITE16LE(p, mtime);
p = WRITE16LE(p, mdate);
p = WRITE32LE(p, crc);
p = WRITE32LE(p, MIN(uselen, 0xffffffff));
p = WRITE32LE(p, MIN(datalen, 0xffffffff));
p = WRITE32LE(p, 0xffffffffu);
p = WRITE32LE(p, 0xffffffffu);
p = WRITE16LE(p, pathlen);
p = WRITE16LE(p, v[8].iov_len + v[9].iov_len);
p = WRITE16LE(p, 0);
p = WRITE16LE(p, disk);
p = WRITE16LE(p, iattrs);
p = WRITE16LE(p, dosmode);
p = WRITE16LE(p, mode);
p = WRITE32LE(p, MIN(zsize, 0xffffffff));
p = WRITE32LE(p, 0xffffffffu);
v[7].iov_len = pathlen;
v[7].iov_base = (void *)path;
// zip64 end of central directory
Expand Down

0 comments on commit 3e6d536

Please sign in to comment.