Correct misunderstanding with zip64 extra records

This commit is contained in:
Justine Tunney 2023-11-18 14:35:57 -08:00
parent dbd8176ea8
commit 3e6d536822
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 96 additions and 78 deletions

View file

@ -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"
/**
@ -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" : "");
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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
@ -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);