mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-17 08:00:32 +00:00
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.
This commit is contained in:
parent
22f81a8d50
commit
23e235b7a5
272 changed files with 3491 additions and 4350 deletions
|
@ -34,13 +34,16 @@ typedef long long v2di __attribute__((__vector_size__(16), __aligned__(1)));
|
|||
*
|
||||
* @param p points to file memory
|
||||
* @param n is byte size of file
|
||||
* @return pointer to EOCD64 or EOCD, or NULL if not found
|
||||
* @param e may receive error code when null is returned
|
||||
* @return pointer to EOCD64 or EOCD, otherwise null
|
||||
*/
|
||||
void *GetZipCdir(const uint8_t *p, size_t n) {
|
||||
void *GetZipEocd(const uint8_t *p, size_t n, int *e) {
|
||||
v2di x;
|
||||
int err;
|
||||
size_t i, j;
|
||||
uint32_t magic;
|
||||
i = n - 4;
|
||||
err = kZipErrorEocdNotFound;
|
||||
do {
|
||||
#ifdef __x86_64__
|
||||
v8hi pk = {READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK"),
|
||||
|
@ -63,19 +66,21 @@ void *GetZipCdir(const uint8_t *p, size_t n) {
|
|||
--i;
|
||||
}
|
||||
if (magic == kZipCdir64LocatorMagic && i + kZipCdir64LocatorSize <= n &&
|
||||
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + i))) {
|
||||
(err = IsZipEocd64(p, n, ZIP_LOCATE64_OFFSET(p + i))) == kZipOk) {
|
||||
return p + ZIP_LOCATE64_OFFSET(p + i);
|
||||
} else if (magic == kZipCdirHdrMagic && IsZipCdir32(p, n, i)) {
|
||||
} else if (magic == kZipCdirHdrMagic &&
|
||||
(err = IsZipEocd32(p, n, i)) == kZipOk) {
|
||||
j = i;
|
||||
do {
|
||||
if (READ32LE(p + j) == kZipCdir64LocatorMagic &&
|
||||
j + kZipCdir64LocatorSize <= n &&
|
||||
IsZipCdir64(p, n, ZIP_LOCATE64_OFFSET(p + j))) {
|
||||
IsZipEocd64(p, n, ZIP_LOCATE64_OFFSET(p + j)) == kZipOk) {
|
||||
return p + ZIP_LOCATE64_OFFSET(p + j);
|
||||
}
|
||||
} while (j-- && i - j < 128);
|
||||
return p + i;
|
||||
}
|
||||
} while (i > 0 && i-- + 0x10000 + 0x1000 >= n);
|
||||
if (e) *e = err;
|
||||
return 0;
|
||||
}
|
|
@ -121,7 +121,7 @@ noasan bool _isutf8(const void *data, size_t size) {
|
|||
return false; // missing cont
|
||||
}
|
||||
default:
|
||||
unreachable;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -16,23 +16,32 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/zip.h"
|
||||
|
||||
/**
|
||||
* Returns true if zip end of central directory header seems legit.
|
||||
* Determines if ZIP EOCD record seems legit.
|
||||
*/
|
||||
bool IsZipCdir32(const uint8_t *p, size_t n, size_t i) {
|
||||
if (i > n || n - i < kZipCdirHdrMinSize) return false;
|
||||
if (READ32LE(p + i) != kZipCdirHdrMagic) return false;
|
||||
if (i + ZIP_CDIR_HDRSIZE(p + i) > n) return false;
|
||||
if (ZIP_CDIR_DISK(p + i) != ZIP_CDIR_STARTINGDISK(p + i)) return false;
|
||||
if (ZIP_CDIR_RECORDSONDISK(p + i) != ZIP_CDIR_RECORDS(p + i)) return false;
|
||||
int IsZipEocd32(const uint8_t *p, size_t n, size_t i) {
|
||||
if (i > n || n - i < kZipCdirHdrMinSize) {
|
||||
return kZipErrorEocdOffsetOverflow;
|
||||
}
|
||||
if (READ32LE(p + i) != kZipCdirHdrMagic) {
|
||||
return kZipErrorEocdMagicNotFound;
|
||||
}
|
||||
if (i + ZIP_CDIR_HDRSIZE(p + i) > n) {
|
||||
return kZipErrorEocdSizeOverflow;
|
||||
}
|
||||
if (ZIP_CDIR_DISK(p + i) != ZIP_CDIR_STARTINGDISK(p + i)) {
|
||||
return kZipErrorEocdDiskMismatch;
|
||||
}
|
||||
if (ZIP_CDIR_RECORDSONDISK(p + i) != ZIP_CDIR_RECORDS(p + i)) {
|
||||
return kZipErrorCdirRecordsMismatch;
|
||||
}
|
||||
if (ZIP_CDIR_RECORDS(p + i) * kZipCfileHdrMinSize > ZIP_CDIR_SIZE(p + i)) {
|
||||
return false;
|
||||
return kZipErrorCdirRecordsOverflow;
|
||||
}
|
||||
if (ZIP_CDIR_OFFSET(p + i) + ZIP_CDIR_SIZE(p + i) > i) {
|
||||
return false;
|
||||
return kZipErrorCdirOffsetPastEocd;
|
||||
}
|
||||
return true;
|
||||
return kZipOk;
|
||||
}
|
|
@ -16,31 +16,34 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/zip.h"
|
||||
|
||||
/**
|
||||
* Returns true if zip64 end of central directory header seems legit.
|
||||
* Returns kZipOk if zip64 end of central directory header seems legit.
|
||||
*/
|
||||
bool IsZipCdir64(const uint8_t *p, size_t n, size_t i) {
|
||||
if (i + kZipCdir64HdrMinSize > n) return false;
|
||||
if (READ32LE(p + i) != kZipCdir64HdrMagic) return false;
|
||||
int IsZipEocd64(const uint8_t *p, size_t n, size_t i) {
|
||||
if (i + kZipCdir64HdrMinSize > n) {
|
||||
return kZipErrorEocdOffsetOverflow;
|
||||
}
|
||||
if (READ32LE(p + i) != kZipCdir64HdrMagic) {
|
||||
return kZipErrorEocdMagicNotFound;
|
||||
}
|
||||
if (i + ZIP_CDIR64_HDRSIZE(p + i) + kZipCdir64LocatorSize > n) {
|
||||
return false;
|
||||
return kZipErrorEocdSizeOverflow;
|
||||
}
|
||||
if (ZIP_LOCATE64_MAGIC(p + i + ZIP_CDIR64_HDRSIZE(p + i)) !=
|
||||
kZipCdir64LocatorMagic) {
|
||||
return false;
|
||||
return kZipErrorCdirLocatorMagic;
|
||||
}
|
||||
if (ZIP_LOCATE64_OFFSET(p + i + ZIP_CDIR64_HDRSIZE(p + i)) != i) {
|
||||
return false;
|
||||
return kZipErrorCdirLocatorOffset;
|
||||
}
|
||||
if (ZIP_CDIR64_RECORDS(p + i) * kZipCfileHdrMinSize >
|
||||
ZIP_CDIR64_SIZE(p + i)) {
|
||||
return false;
|
||||
return kZipErrorCdirRecordsOverflow;
|
||||
}
|
||||
if (ZIP_CDIR64_OFFSET(p + i) + ZIP_CDIR64_SIZE(p + i) > i) {
|
||||
return false;
|
||||
return kZipErrorCdirOffsetPastEocd;
|
||||
}
|
||||
return true;
|
||||
return kZipOk;
|
||||
}
|
|
@ -54,7 +54,7 @@ o/$(MODE)/libc/str/dosdatetimetounix.o: private \
|
|||
CFLAGS += \
|
||||
-O3
|
||||
|
||||
o/$(MODE)/libc/str/getzipcdir.o \
|
||||
o/$(MODE)/libc/str/getzipeocd.o \
|
||||
o/$(MODE)/libc/str/getzipcdircomment.o \
|
||||
o/$(MODE)/libc/str/getzipcdircommentsize.o \
|
||||
o/$(MODE)/libc/str/getzipcdiroffset.o \
|
||||
|
|
|
@ -83,7 +83,7 @@ int strnwidth(const char *s, size_t n, size_t o) {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
unreachable;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue