Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Fix warnings reported by unzip utility
Justine Tunneydf6bd5828c4d
diff --git a/README.md b/README.mdindex 1ae78d7b..04c53985 100644--- a/README.md+++ b/README.md@@ -89,7 +89,7 @@ it to your path.```shmkdir -p cosmocccd cosmocc-curl https://cosmo.zip/pub/cosmocc/cosmocc.zip >cosmocc.zip+curl https://github.com/jart/cosmopolitan/releases/download/3.1/cosmocc-3.1.zip >cosmocc.zipunzip cosmocc.zipcd ..export PATH="$PWD/cosmocc/bin:$PATH"@@ -114,26 +114,183 @@ o//llama.cpp/main/main \-p $'```c\nvoid *memcpy_sse2(char *dst, const char *src, size_t size) {\n'```-Here's an example of how to run the HTTP server. This example includes-its own [`.args` file](llama.cpp/server/.args) which, by default,-assumes `llava-v1.5-7b-Q8_0.gguf` and `llava-v1.5-7b-mmproj-Q8_0.gguf`-have been added to either (1) the current directory, or (2) placed-inside the PKZIP file structure of the executable. It's important that-you add large files to your zip executable archive using a tool we wrote-called `zipalign`. Our command goes 10x faster than the `zip` command.-It also inserts the weights without compression so they're aligned on a-page size boundary. That way, the metal GPU is able to map your weights-directly from the ZIP archive.+Here's an example of how to run the HTTP server in such a way that the+weights are embedded inside the executable.```shmake -j8-o//llamafile/zipalign -j \++o//llamafile/zipalign -j0 \o//llama.cpp/server/server \~/weights/llava-v1.5-7b-Q8_0.gguf \~/weights/llava-v1.5-7b-mmproj-Q8_0.gguf-o//llama.cpp/server/server++o//llama.cpp/server/server \+ -m llava-v1.5-7b-Q8_0.gguf \+ --mmproj llava-v1.5-7b-mmproj-Q8_0.gguf \+ --host 0.0.0.0```The above command will launch a browser tab on your personal computer to-display a web interface that lets you chat with your LLM and upload+display a web interface. It lets you chat with your LLM and uploadimages to it.++If you want to be able to just say:++```+./server+```++And have it run the web server without having to specify arguments (for+the paths you already know are in there), then you can add a special+`.args` to the zip archive, which specifies the default arguments. In+this case, we're going to try our luck with the normal `zip` command,+which requires we temporarily rename the file. First, let's create the+arguments file:++```+cat <<EOF >.args+-m+llava-v1.5-7b-Q8_0.gguf+--mmproj+llava-v1.5-7b-mmproj-Q8_0.gguf+--host+0.0.0.0+...+EOF+```++As we can see above, there's one argument per line. The `...` argument+optionally specifies where any additional CLI arguments passed by the+user are to be inserted. Next, we'll add the argument file to the+executable:++```+mv o//llama.cpp/server/server server.com+zip server.com .args+mv server.com server+./server+```++Congratulations. You've just made your own LLM server that's easy to+share with your friends.++## zipalign documentation++```+SYNOPSIS++ o//llamafile/zipalign ZIP FILE...++DESCRIPTION++ Adds aligned uncompressed files to PKZIP archive++ This tool is designed to concatenate gigabytes of LLM weights to an+ executable. This command goes 10x faster than `zip -j0`. Unlike zip+ you are not required to use the .com file extension for it to work.+ But most importantly, this tool has a flag that lets you insert zip+ files that are aligned on a specific boundary. The result is things+ like GPUs that have specific memory alignment requirements will now+ be able to perform math directly on the zip file's mmap()'d weights++FLAGS++ -h help+ -N nondeterministic mode+ -a INT alignment (default 65536)+ -j strip directory components+ -0 store uncompressed (currently default)+```++## Technical Details++Here is a succinct overview of the tricks we used to create the fattest+executable format ever. The long story short is llamafile is a shell+script that launches itself and runs inference on embedded weights in+milliseconds without needing to be copied or installed. What makes that+possible is mmap(). Both the llama.cpp executable and the weights are+concatenated onto the shell script. A tiny loader program is then+extracted by the shell script, which maps the executable into memory.+The llama.cpp executable then opens the shell script again as a file,+and calls mmap() again to pull the weights into memory and make them+directly accessible to both the CPU and GPU.++### ZIP Weights Embedding++The trick to embedding weights inside llama.cpp executables is to ensure+the local file is aligned on a page size boundary. That way, assuming+the zip file is uncompressed, once it's mmap()'d into memory we can pass+pointers directly to GPUs like Apple Metal, which require that data be+page size aligned. Since no existing ZIP archiving tool has an alignment+flag, we had to write about [400 lines of code](llamafile/zipalign.c) to+insert the ZIP files ourselves. However, once there, every existing ZIP+program should be able to read them, provided they support ZIP64. This+makes the weights much more easily accessible than they otherwise would+have been, had we invented our own file format for concatenated files.++### Microarchitectural Portability++On Intel and AMD microprocessors, llama.cpp spends most of its time in+the matmul quants, which are usually written thrice for SSSE3, AVX, and+AVX2. llamafile pulls each of these functions out into a separate file+that can be `#include`ed multiple times, with varying+`__attribute__((__target__("arch")))` function attributes. Then, a+wrapper function is added which uses Cosmopolitan's `X86_HAVE(FOO)`+feature to runtime dispatch to the appropriate implementation.++### Architecture Portability++llamafile solves architecture portability by building llama.cpp twice:+once for AMD64 and again for ARM64. It then wraps them with a shell+script which has an MZ prefix. On Windows, it'll run as a native binary.+On Linux, it'll extract a small 8kb executable called [APE+Loader](https://github.com/jart/cosmopolitan/blob/master/ape/loader.c)+to `${TMPDIR:-${HOME:-.}}/.ape` that'll map the binary portions of the+shell script into memory. It's possible to avoid this process by running+the+[`assimilate`](https://github.com/jart/cosmopolitan/blob/master/tool/build/assimilate.c)+program that comes included with the `cosmocc` compiler. What the+`assimilate` program does, is it turns the shell script executable into+the host platform's native executable format. This guarantees a fallback+path exists for traditonal release processes when it's needed.++### GPU Support++Cosmopolitan Libc uses static linking, since that's the only way to get+the same executable to run on six OSes. This presents a challenge for+llama.cpp, because it's not possible to statically link GPU support. The+way we solve that is by checking if a compiler is installed on the host+system. For Apple, that would be xcode, and for other platforms, that+would be `nvcc`. llama.cpp has a single file implementation of each GPU+module, named `ggml-metal.m` (Objective C) and `ggml-cuda.cu` (Nvidia+C). llamafile embeds those source files within the zip archive and asks+the platform compiler to build them at runtime, targeting the native GPU+microarchitecture. If it works, then it's linked with platform C library+dlopen() implementation. See [llamafile/cuda.c](llamafile/cuda.c) and+[llamafile/metal.c](llamafile/metal.c).++In order to use the platform-specific dlopen() function, we need to ask+the platform-specific compiler to build a small executable that exposes+these interfaces. On ELF platforms, Cosmopolitan Libc maps this helper+executable into memory along with the platform's ELF interpreter. The+platform C library then takes care of linking all the GPU libraries, and+then runs the helper program which longjmp()'s back into Cosmopolitan.+The executable program is now in a weird hybrid state where two separate+C libraries exist which have different ABIs. For example, thread local+storage works differently on each operating system, and programs will+crash if the TLS register doesn't point to the appropriate memory. The+way Cosmopolitan Libc solves that is by JITing a trampoline around each+dlsym() import, which blocks signals using `sigprocmask()` and changes+the TLS register using `arch_prctl()`. Under normal circumstances,+aspecting each function call with four additional system calls would be+prohibitively expensive, but for llama.cpp that cost is infinitesimal+compared to the amount of compute used for LLM inference. Our technique+has no noticeable slowdown. The major tradeoff is that, right now, you+can't pass callback pointers to the dlopen()'d module. Only one such+function needed to be removed from the llama.cpp codebase, which was an+API intended for customizing logging. In the future, Cosmoplitan will+just trampoline signal handlers and code morph the TLS instructions to+avoid these tradeoffs entirely. See+[cosmopolitan/dlopen.c](https://github.com/jart/cosmopolitan/blob/master/libc/dlopen/dlopen.c)+for further details.diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.cindex ca635796..2232a00e 100644--- a/llama.cpp/ggml.c+++ b/llama.cpp/ggml.c@@ -19400,19 +19400,11 @@ void gguf_get_meta_data(const struct gguf_context * ctx, void * data) {////////////////////////////////////////////////////////////////////////////////int ggml_cpu_has_avx(void) {-#if defined(__AVX__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(AVX);}int ggml_cpu_has_avx2(void) {-#if defined(__AVX2__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(AVX2);}int ggml_cpu_has_avx512(void) {@@ -19440,11 +19432,7 @@ int ggml_cpu_has_avx512_vnni(void) {}int ggml_cpu_has_fma(void) {-#if defined(__FMA__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(FMA);}int ggml_cpu_has_neon(void) {@@ -19468,11 +19456,7 @@ int ggml_cpu_has_metal(void) {}int ggml_cpu_has_f16c(void) {-#if defined(__F16C__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(F16C);}int ggml_cpu_has_fp16_va(void) {@@ -19516,19 +19500,11 @@ int ggml_cpu_has_gpublas(void) {}int ggml_cpu_has_sse3(void) {-#if defined(__SSE3__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(SSE3);}int ggml_cpu_has_ssse3(void) {-#if defined(__SSSE3__)- return 1;-#else- return 0;-#endif+ return X86_HAVE(SSSE3);}int ggml_cpu_has_vsx(void) {diff --git a/llamafile/llamafile.c b/llamafile/llamafile.cindex 4f0df41c..d4b6ea9a 100644--- a/llamafile/llamafile.c+++ b/llamafile/llamafile.c@@ -40,34 +40,43 @@ struct llamafile {size_t mapsize;};-static 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 ZIP_READ64(ZIP_EXTRA_CONTENT(p) + 16);+static 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 + ZIP_EXTRA_SIZE(p) <= pe; p += ZIP_EXTRA_SIZE(p)) {+ if (ZIP_EXTRA_HEADERID(p) == kZipExtraZip64) {+ if (8 <= ZIP_EXTRA_CONTENTSIZE(p)) {+ return ZIP_READ64(ZIP_EXTRA_CONTENT(p));}}}- return x;+ return -1;}-static 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 ZIP_READ64(ZIP_EXTRA_CONTENT(p) + 8);+static 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 + ZIP_EXTRA_SIZE(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 ZIP_READ64(ZIP_EXTRA_CONTENT(p) + offset);}}}- return x;+ return -1;}struct llamafile *llamafile_open(const char *fname, const char *mode) {@@ -108,7 +117,7 @@ struct llamafile *llamafile_open(const char *fname, const char *mode) {// read the last 64kb of file// the zip file format magic can be anywhere in thereint amt;- off_t off;+ uint64_t off;if (file->size <= 65536) {off = 0;amt = file->size;@@ -216,7 +225,7 @@ struct llamafile *llamafile_open(const char *fname, const char *mode) {off_t mapoff = off & -pagesz;long skew = off - mapoff;file->mapsize = skew + file->size;- file->mapping = mmap(0, file->mapsize, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, mapoff);+ file->mapping = mmap(0, file->mapsize, PROT_READ, MAP_SHARED, fd, mapoff);if (file->mapping == MAP_FAILED) {fprintf(stderr, "warning: failed to map %s from %s: %s\n",fname, prog, strerror(errno));@@ -259,12 +268,8 @@ size_t llamafile_tell(struct llamafile *file) {if (!file->fp) {return file->position;}-#ifdef _WIN32- __int64 ret = _ftelli64(file->fp);-#elselong ret = ftell(file->fp);-#endif- unassert(ret != -1); // this really shouldn't fail+ unassert(ret != -1); // shouldn't fail because we seeked earlierreturn (size_t) ret;}diff --git a/llamafile/zip.h b/llamafile/zip.hindex e73f7d35..8341c97e 100644--- a/llamafile/zip.h+++ b/llamafile/zip.h@@ -98,6 +98,7 @@#define kZipLfileHdrMagic ZM_(0x04034b50) /* PK♥♦ "PK\3\4" */#define kZipLfileHdrMinSize 30+#define kZipLfileOffsetVersionNeeded 4#define kZipLfileOffsetGeneralflag 6#define kZipLfileOffsetCompressionmethod 8#define kZipLfileOffsetLastmodifiedtime 10diff --git a/llamafile/zipalign.c b/llamafile/zipalign.cindex 7f188643..104e474a 100644--- a/llamafile/zipalign.c+++ b/llamafile/zipalign.c@@ -24,17 +24,26 @@#include <stdint.h>#include <stdlib.h>#include <libgen.h>-#include <sys/uio.h>#include <getopt.h>+#include <stdbool.h>+#include <sys/uio.h>#include <third_party/zlib/zlib.h>#include "zip.h"#define USAGE \" ZIP FILE...\n\\n\-SYNOPSIS\n\+DESCRIPTION\n\\n\- Adds aligned uncompressed files to PKZIP archives.\n\+ Adds aligned uncompressed files to PKZIP archive\n\+\n\+ This tool is designed to concatenate gigabytes of LLM weights to an\n\+ executable. This command goes 10x faster than `zip -j0`. Unlike zip\n\+ you are not required to use the .com file extension for it to work.\n\+ But most importantly, this tool has a flag that lets you insert zip\n\+ files that are aligned on a specific boundary. The result is things\n\+ like GPUs that have specific memory alignment requirements will now\n\+ be able to perform math directly on the zip file's mmap()'d weights\n\\n\FLAGS\n\\n\@@ -42,6 +51,7 @@ FLAGS\n\-N nondeterministic mode\n\-a INT alignment (default 65536)\n\-j strip directory components\n\+ -0 store uncompressed (currently default)\n\\n"#define Min(a, b) ((a) < (b) ? (a) : (b))@@ -51,9 +61,9 @@ FLAGS\n\((HOUR) << 11 | (MINUTE) << 5 | (SECOND) >> 1)static const char *prog;-static int alignment = 65536;-static bool nondeterministic;-static int strip_directory_components;+static int FLAG_junk;+static int FLAG_alignment = 65536;+static bool FLAG_nondeterministic;static wontreturn void Die(const char *thing, const char *reason) {tinyprint(2, thing, ": ", reason, "\n", NULL);@@ -87,37 +97,10 @@ static void *Realloc(void *p, size_t n) {}static wontreturn void PrintUsage(int fd, int rc) {- tinyprint(fd, "USAGE\n\n ", prog, USAGE, NULL);+ tinyprint(fd, "SYNOPSIS\n\n ", prog, USAGE, NULL);exit(rc);}-static void GetOpts(int argc, char *argv[]) {- int opt;- while ((opt = getopt(argc, argv, "hjNa:")) != -1) {- switch (opt) {- case 'N':- nondeterministic = true;- break;- case 'j':- strip_directory_components = true;- break;- case 'a':- alignment = atoi(optarg);- if (alignment < 1) {- Die(prog, "alignment must be at least 1");- }- if (alignment & (alignment - 1)) {- Die(prog, "alignment must be two power");- }- break;- case 'h':- PrintUsage(1, 0);- default:- PrintUsage(2, 1);- }- }-}-static void GetDosLocalTime(int64_t utcunixts,uint16_t *out_time,uint16_t *out_date) {@@ -134,9 +117,34 @@ int main(int argc, char *argv[]) {if (!prog) prog = "zipalign";// parse flags- GetOpts(argc, argv);+ int opt;+ while ((opt = getopt(argc, argv, "hj0Na:")) != -1) {+ switch (opt) {+ case '0':+ break;+ case 'j':+ FLAG_junk = true;+ break;+ case 'N':+ FLAG_nondeterministic = true;+ break;+ case 'a':+ FLAG_alignment = atoi(optarg);+ if (FLAG_alignment < 1) {+ Die(prog, "FLAG_alignment must be at least 1");+ }+ if (FLAG_alignment & (FLAG_alignment - 1)) {+ Die(prog, "FLAG_alignment must be two power");+ }+ break;+ case 'h':+ PrintUsage(1, 0);+ default:+ PrintUsage(2, 1);+ }+ }if (optind == argc) {- Die(prog, "missing output zip");+ Die(prog, "missing output argument");}// open output file@@ -160,7 +168,7 @@ int main(int argc, char *argv[]) {off = zsize - 65536;amt = zsize - off;}- char *last64 = Malloc(amt);+ static char last64[65536];if (pread(zfd, last64, amt, off) != amt) {DieSys(zpath);}@@ -195,7 +203,6 @@ int main(int argc, char *argv[]) {if (!cnt) {amt = 0;}- free(last64);// read central directoryuint8_t *cdir = Malloc(amt);@@ -205,13 +212,12 @@ int main(int argc, char *argv[]) {// get timestruct timespec now;- if (nondeterministic) {+ uint16_t mtime, mdate;+ if (FLAG_nondeterministic) {now = timespec_real();} else {now = timespec_fromseconds(1700000000);}- int64_t ft = (now.tv_sec + 11644473600) * 10000000;- uint16_t mtime, mdate;GetDosLocalTime(now.tv_sec, &mtime, &mdate);// add inputs@@ -230,15 +236,19 @@ int main(int argc, char *argv[]) {// construct zip entry namechar *name = StrDup(path);- if (strip_directory_components) {+ if (FLAG_junk) {name = basename(name);+ } else {+ while (*name == '/') {+ ++name;+ }}// determine size and alignment of local file headersize_t namlen = strlen(name);- size_t extlen = (2 + 2 + 8 + 8) + (2 + 2 + 4 + 2 + 2 + 8 + 8 + 8);+ size_t extlen = (2 + 2 + 8 + 8);size_t hdrlen = kZipLfileHdrMinSize + namlen + extlen;- while ((zsize + hdrlen) & (alignment - 1)) ++zsize;+ while ((zsize + hdrlen) & (FLAG_alignment - 1)) ++zsize;// copy filessize_t rc;@@ -265,8 +275,8 @@ int main(int argc, char *argv[]) {p = ZIP_WRITE16(p, mtime);p = ZIP_WRITE16(p, mdate);p = ZIP_WRITE32(p, crc);- p = ZIP_WRITE32(p, Min(size, 0xffffffffu)); // compressed size- p = ZIP_WRITE32(p, Min(size, 0xffffffffu)); // uncompressed size+ p = ZIP_WRITE32(p, 0xffffffffu); // compressed size+ p = ZIP_WRITE32(p, 0xffffffffu); // uncompressed sizep = ZIP_WRITE16(p, namlen);p = ZIP_WRITE16(p, extlen);p = mempcpy(p, name, namlen);@@ -276,15 +286,6 @@ int main(int argc, char *argv[]) {p = ZIP_WRITE64(p, size); // uncompressed sizep = ZIP_WRITE64(p, size); // compressed size- p = ZIP_WRITE16(p, kZipExtraNtfs);- p = ZIP_WRITE16(p, 4 + 2 + 2 + 8 + 8 + 8);- p = ZIP_WRITE32(p, 0); // reserved- p = ZIP_WRITE16(p, 1); // attribute tag- p = ZIP_WRITE16(p, 8 + 8 + 8); // size of attribute- p = ZIP_WRITE64(p, ft); // modified time- p = ZIP_WRITE64(p, ft); // access time- p = ZIP_WRITE64(p, ft); // creation time-unassert(p == lochdr + hdrlen);if (pwrite(zfd, lochdr, hdrlen, zsize) != hdrlen) {DieSys(zpath);@@ -292,7 +293,7 @@ int main(int argc, char *argv[]) {free(lochdr);// create central directory entry- extlen = (2 + 2 + 8 + 8 + 8) + (2 + 2 + 4 + 2 + 2 + 8 + 8 + 8);+ extlen = (2 + 2 + 8 + 8 + 8);hdrlen = kZipCfileHdrMinSize + namlen + extlen;cdir = Realloc(cdir, amt + hdrlen);uint8_t *cdirhdr = cdir + amt;@@ -307,15 +308,15 @@ int main(int argc, char *argv[]) {p = ZIP_WRITE16(p, mtime);p = ZIP_WRITE16(p, mdate);p = ZIP_WRITE32(p, crc);- p = ZIP_WRITE32(p, Min(size, 0xffffffffu)); // compressed size- p = ZIP_WRITE32(p, Min(size, 0xffffffffu)); // uncompressed size+ p = ZIP_WRITE32(p, 0xffffffffu); // compressed size+ p = ZIP_WRITE32(p, 0xffffffffu); // uncompressed sizep = ZIP_WRITE16(p, namlen);p = ZIP_WRITE16(p, extlen);p = ZIP_WRITE16(p, 0); // comment lengthp = ZIP_WRITE16(p, 0); // disk number startp = ZIP_WRITE16(p, kZipIattrBinary);p = ZIP_WRITE32(p, 0100644u << 16); // external file attributes- p = ZIP_WRITE32(p, Min(zsize, 0xffffffffu));+ p = ZIP_WRITE32(p, 0xffffffffu); // lfile offsetp = mempcpy(p, name, namlen);p = ZIP_WRITE16(p, kZipExtraZip64);@@ -323,16 +324,6 @@ int main(int argc, char *argv[]) {p = ZIP_WRITE64(p, size); // uncompressed sizep = ZIP_WRITE64(p, size); // compressed sizep = ZIP_WRITE64(p, zsize); // lfile offset-- p = ZIP_WRITE16(p, kZipExtraNtfs);- p = ZIP_WRITE16(p, 4 + 2 + 2 + 8 + 8 + 8);- p = ZIP_WRITE32(p, 0); // reserved- p = ZIP_WRITE16(p, 1); // attribute tag- p = ZIP_WRITE16(p, 8 + 8 + 8); // size of attribute- p = ZIP_WRITE64(p, ft); // modified time- p = ZIP_WRITE64(p, ft); // access time- p = ZIP_WRITE64(p, ft); // creation time-unassert(p == cdirhdr + hdrlen);// finish up@@ -343,30 +334,17 @@ int main(int argc, char *argv[]) {}}- // write out the central directory+ // write out central directoryif (pwrite(zfd, cdir, amt, zsize) != amt) {DieSys(zpath);}free(cdir);- char eocd[kZipCdirHdrMinSize];- char *p = eocd;- p = ZIP_WRITE32(p, kZipCdirHdrMagic);- p = ZIP_WRITE16(p, 0); // number of this disk- p = ZIP_WRITE16(p, 0); // number of disks- p = ZIP_WRITE16(p, cnt); // number of records on disk- p = ZIP_WRITE16(p, cnt); // number of records- p = ZIP_WRITE32(p, amt); // size of central directory- p = ZIP_WRITE32(p, Min(zsize, 0xffffffffu)); // offset of central directory- p = ZIP_WRITE16(p, 0); // comment length- if (pwrite(zfd, eocd, kZipCdirHdrMinSize, zsize + amt) != kZipCdirHdrMinSize) {- DieSys(zpath);- }-- char eocd64[kZipCdir64HdrMinSize];- p = eocd64;+ // write out end of central directory+ uint8_t eocd[kZipCdirHdrMinSize + kZipCdir64HdrMinSize + kZipCdir64LocatorSize];+ uint8_t *p = eocd;p = ZIP_WRITE32(p, kZipCdir64HdrMagic);- p = ZIP_WRITE64(p, sizeof(eocd64) - 12); // size of eocd64+ p = ZIP_WRITE64(p, kZipCdir64HdrMinSize - 12); // size of eocd64p = ZIP_WRITE16(p, kZipOsUnix << 8 | kZipEra2001); // version made byp = ZIP_WRITE16(p, kZipEra2001); // version needed to extractp = ZIP_WRITE32(p, 0); // number of this disk@@ -375,17 +353,20 @@ int main(int argc, char *argv[]) {p = ZIP_WRITE64(p, cnt); // number of recordsp = ZIP_WRITE64(p, amt); // size of central directoryp = ZIP_WRITE64(p, zsize); // offset of start of central directory- if (pwrite(zfd, eocd64, kZipCdir64HdrMinSize, zsize + amt + kZipCdirHdrMinSize) != kZipCdir64HdrMinSize) {- DieSys(zpath);- }-- char locate64[kZipCdir64LocatorSize];- p = locate64;p = ZIP_WRITE32(p, kZipCdir64LocatorMagic);p = ZIP_WRITE32(p, 0); // number of disk with eocd64p = ZIP_WRITE64(p, zsize + amt); // offset of eocd64p = ZIP_WRITE32(p, 1); // total number of disks- if (pwrite(zfd, locate64, kZipCdir64LocatorSize, zsize + amt + kZipCdirHdrMinSize + kZipCdir64HdrMinSize) != kZipCdir64LocatorSize) {+ p = ZIP_WRITE32(p, kZipCdirHdrMagic);+ p = ZIP_WRITE16(p, 0); // number of this disk+ p = ZIP_WRITE16(p, 0); // number of disks+ p = ZIP_WRITE16(p, cnt); // number of records on disk+ p = ZIP_WRITE16(p, cnt); // number of records+ p = ZIP_WRITE32(p, amt); // size of central directory+ p = ZIP_WRITE32(p, 0xffffffffu); // offset of central directory+ p = ZIP_WRITE16(p, 0); // comment length+ unassert(p == eocd + sizeof(eocd));+ if (pwrite(zfd, eocd, sizeof(eocd), zsize + amt) != sizeof(eocd)) {DieSys(zpath);}