// SPDX-License-Identifier: WTFPL #include #include #include typedef uint32_t UW; typedef int32_t W; typedef uint8_t UB; #define IMPORT extern #include "bzcomp.h" /* use $(BD)/include/bzcomp.h */ static size_t filesize; static UB *buf; static int loadfile(char *filename) { FILE *fp; fp = fopen(filename, "r"); if (fp == NULL) { printf("loadfile: %s open error\n", filename); return -1; } fseek(fp, 0, SEEK_END); filesize = ftell(fp); fseek(fp, 0, SEEK_SET); buf = malloc(filesize); if (buf == NULL) goto fin0; fread(buf, filesize, 1, fp); fin0: fclose(fp); return 0; } static int savefile(char *filename, void *buffer, size_t size) { FILE *fp; fp = fopen(filename, "w"); if (fp == NULL) { printf("savefile: %s open error\n", filename); return -1; } fwrite(buffer, size, 1, fp); fclose(fp); return 0; } int main(int argc, char *argv[]) { UB *out; Bz_HDR *h; if (argc < 3) { printf("%s: usage [infile] [outfile]\n", argv[0]); goto fin0; } if (loadfile(argv[1]) < 0 || buf == NULL) { printf("file read error\n"); goto fin0; } h = (Bz_HDR *)buf; if ((h->magic1 != BzMAGIC1 && h->magic1 != BzMAGIC1S) || h->magic2 != BzMAGIC2 || h->magic3 != BzMAGIC3) { printf("invalid file header\n"); goto fin0; } if ((out = malloc(h->expsz)) == NULL) { printf("malloc failed (out)\n"); goto fin1; } bz_uncompress(out, buf + sizeof(Bz_HDR), h->expsz, filesize - sizeof(Bz_HDR)); savefile(argv[2], out, h->expsz); free(out); fin1: free(buf); fin0: return 0; }