struct dist_tbl_t { uint16_t base_dist, ebits; };
extern struct dist_tbl_t dist_tbl[30];
+/* Table for computing CRC-32. */
+extern const uint32_t crc32_tbl[256];
+
#endif /* _TABLES_H */
#include "lib.h"
#include "report.h"
#include "stdint.h"
+#include "tables.h"
#include "unzip.h"
#include "vector.h"
}
+/**
+ * See Figure 14-7 in Hacker's Delight (2nd ed.), or
+ * crc_reflected() in https://www.zlib.net/crc_v3.txt
+ * or Garry S. Brown's implementation e.g. in
+ * https://opensource.apple.com/source/xnu/xnu-792.13.8/bsd/libkern/crc32.c
+ */
+uint32_t crc32 (FILE *fp) {
+
+ uint32_t crc = 0xffffffff;
+
+ unsigned char buf[32];
+ uint64_t i, n;
+
+ while ((n = fread (buf, 1, sizeof (buf), fp))) {
+
+ for (i = 0; i < n; i++) {
+ crc = (crc >> 8) ^ crc32_tbl[(crc ^ buf[i]) & UCHAR_MAX];
+ }
+
+ }
+
+ return crc ^ 0xffffffff;
+
+}
+
static void extract_zip (const char *path) {
struct eocdr eocdr = { 0 };
}
+ rewind (outfile);
+
+ if (crc32 (outfile) != cfh.crc32) {
+
+ report_at (program_name, 0, REPORT_ERROR, "%s: CRC-32 mismatch!", temp);
+ fclose (outfile);
+
+ free (data);
+ remove (temp);
+
+ break;
+
+ }
+
free (data);
fclose (outfile);