static int verify_data(const uint8_t* data_ptr, int magic, int size) { int i = 0; /* This is how the test data inside test files was generated; * we are re-generating it here and we check if our re-generated * test data is the same as in the test file. If this test is * failing it's either because there's a bug in the test case, * or the unpacked data is corrupted. */ for(i = 0; i < size / 4; ++i) { const int k = i + 1; const signed int* lptr = (const signed int*) &data_ptr[i * 4]; signed int val = k * k - 3 * k + (1 + magic); if(val < 0) val = 0; /* *lptr is a value inside unpacked test file, val is the * value that should be in the unpacked test file. */ if(archive_le32dec(lptr) != (uint32_t) val) return 0; } return 1; }
/* * Test whether we can handle this data. */ static int zstd_bidder_bid(struct archive_read_filter_bidder *self, struct archive_read_filter *filter) { const unsigned char *buffer; ssize_t avail; unsigned prefix; /* Zstd frame magic values */ const unsigned zstd_magic = 0xFD2FB528U; (void) self; /* UNUSED */ buffer = __archive_read_filter_ahead(filter, 4, &avail); if (buffer == NULL) return (0); prefix = archive_le32dec(buffer); if (prefix == zstd_magic) return (32); return (0); }
/* * The extra data is stored as a list of * id1+size1+data1 + id2+size2+data2 ... * triplets. id and size are 2 bytes each. */ static void process_extra(const void* extra, struct zip* zip) { int offset = 0; const char *p = (const char *)extra; while (offset < zip->extra_length - 4) { unsigned short headerid = archive_le16dec(p + offset); unsigned short datasize = archive_le16dec(p + offset + 2); offset += 4; if (offset + datasize > zip->extra_length) break; #ifdef DEBUG fprintf(stderr, "Header id 0x%04x, length %d\n", headerid, datasize); #endif switch (headerid) { case 0x0001: /* Zip64 extended information extra field. */ if (datasize >= 8) zip->uncompressed_size = archive_le64dec(p + offset); if (datasize >= 16) zip->compressed_size = archive_le64dec(p + offset + 8); break; case 0x5455: { /* Extended time field "UT". */ int flags = p[offset]; offset++; datasize--; /* Flag bits indicate which dates are present. */ if (flags & 0x01) { #ifdef DEBUG fprintf(stderr, "mtime: %lld -> %d\n", (long long)zip->mtime, archive_le32dec(p + offset)); #endif if (datasize < 4) break; zip->mtime = archive_le32dec(p + offset); offset += 4; datasize -= 4; } if (flags & 0x02) { if (datasize < 4) break; zip->atime = archive_le32dec(p + offset); offset += 4; datasize -= 4; } if (flags & 0x04) { if (datasize < 4) break; zip->ctime = archive_le32dec(p + offset); offset += 4; datasize -= 4; } break; } case 0x7855: /* Info-ZIP Unix Extra Field (type 2) "Ux". */ #ifdef DEBUG fprintf(stderr, "uid %d gid %d\n", archive_le16dec(p + offset), archive_le16dec(p + offset + 2)); #endif if (datasize >= 2) zip->uid = archive_le16dec(p + offset); if (datasize >= 4) zip->gid = archive_le16dec(p + offset + 2); break; default: break; } offset += datasize; } #ifdef DEBUG if (offset != zip->extra_length) { fprintf(stderr, "Extra data field contents do not match reported size!"); } #endif }
int zip_read_file_header(struct archive_read *a, struct archive_entry *entry, struct zip *zip) { const struct zip_file_header *p; const void *h; if ((p = __archive_read_ahead(a, sizeof *p)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Truncated ZIP file header"); return (ARCHIVE_FATAL); } zip->version = p->version[0]; zip->system = p->version[1]; zip->flags = archive_le16dec(p->flags); zip->compression = archive_le16dec(p->compression); if (zip->compression < sizeof(compression_names)/sizeof(compression_names[0])) zip->compression_name = compression_names[zip->compression]; else zip->compression_name = "??"; zip->mtime = zip_time(p->timedate); zip->ctime = 0; zip->atime = 0; zip->mode = 0; zip->uid = 0; zip->gid = 0; zip->crc32 = archive_le32dec(p->crc32); zip->filename_length = archive_le16dec(p->filename_length); zip->extra_length = archive_le16dec(p->extra_length); zip->uncompressed_size = archive_le32dec(p->uncompressed_size); zip->compressed_size = archive_le32dec(p->compressed_size); (a->decompressor->consume)(a, sizeof(struct zip_file_header)); /* Read the filename. */ if ((h = __archive_read_ahead(a, zip->filename_length)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Truncated ZIP file header"); return (ARCHIVE_FATAL); } if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL) __archive_errx(1, "Out of memory"); archive_strncpy(&zip->pathname, h, zip->filename_length); (a->decompressor->consume)(a, zip->filename_length); archive_entry_set_pathname(entry, zip->pathname.s); if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/') zip->mode = AE_IFDIR | 0777; else zip->mode = AE_IFREG | 0777; /* Read the extra data. */ if ((h = __archive_read_ahead(a, zip->extra_length)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Truncated ZIP file header"); return (ARCHIVE_FATAL); } process_extra(h, zip); (a->decompressor->consume)(a, zip->extra_length); /* Populate some additional entry fields: */ archive_entry_set_mode(entry, zip->mode); archive_entry_set_uid(entry, zip->uid); archive_entry_set_gid(entry, zip->gid); archive_entry_set_mtime(entry, zip->mtime, 0); archive_entry_set_ctime(entry, zip->ctime, 0); archive_entry_set_atime(entry, zip->atime, 0); archive_entry_set_size(entry, zip->uncompressed_size); zip->entry_bytes_remaining = zip->compressed_size; zip->entry_offset = 0; /* If there's no body, force read_data() to return EOF immediately. */ if (0 == (zip->flags & ZIP_LENGTH_AT_END) && zip->entry_bytes_remaining < 1) zip->end_of_entry = 1; /* Set up a more descriptive format name. */ sprintf(zip->format_name, "ZIP %d.%d (%s)", zip->version / 10, zip->version % 10, zip->compression_name); a->archive.archive_format_name = zip->format_name; return (ARCHIVE_OK); }
static int archive_read_format_zip_read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset) { int r; struct zip *zip; zip = (struct zip *)(a->format->data); /* * If we hit end-of-entry last time, clean up and return * ARCHIVE_EOF this time. */ if (zip->end_of_entry) { if (!zip->end_of_entry_cleanup) { if (zip->flags & ZIP_LENGTH_AT_END) { const char *p; if ((p = __archive_read_ahead(a, 16)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Truncated ZIP end-of-file record"); return (ARCHIVE_FATAL); } zip->crc32 = archive_le32dec(p + 4); zip->compressed_size = archive_le32dec(p + 8); zip->uncompressed_size = archive_le32dec(p + 12); (a->decompressor->consume)(a, 16); } /* Check file size, CRC against these values. */ if (zip->compressed_size != zip->entry_compressed_bytes_read) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP compressed data is wrong size"); return (ARCHIVE_WARN); } /* Size field only stores the lower 32 bits of the actual size. */ if ((zip->uncompressed_size & UINT32_MAX) != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP uncompressed data is wrong size"); return (ARCHIVE_WARN); } /* TODO: Compute CRC. */ /* if (zip->crc32 != zip->entry_crc32_calculated) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP data CRC error"); return (ARCHIVE_WARN); } */ /* End-of-entry cleanup done. */ zip->end_of_entry_cleanup = 1; } *offset = zip->entry_uncompressed_bytes_read; *size = 0; *buff = NULL; return (ARCHIVE_EOF); } switch(zip->compression) { case 0: /* No compression. */ r = zip_read_data_none(a, buff, size, offset); break; case 8: /* Deflate compression. */ r = zip_read_data_deflate(a, buff, size, offset); break; default: /* Unsupported compression. */ *buff = NULL; *size = 0; *offset = 0; /* Return a warning. */ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unsupported ZIP compression method (%s)", zip->compression_name); if (zip->flags & ZIP_LENGTH_AT_END) { /* * ZIP_LENGTH_AT_END requires us to * decompress the entry in order to * skip it, but we don't know this * compression method, so we give up. */ r = ARCHIVE_FATAL; } else { /* We can't decompress this entry, but we will * be able to skip() it and try the next entry. */ r = ARCHIVE_WARN; } break; } return (r); }
static int slurp_central_directory(struct archive_read *a, struct zip *zip) { unsigned i; int64_t correction; static const struct archive_rb_tree_ops rb_ops = { &cmp_node, &cmp_key }; static const struct archive_rb_tree_ops rb_rsrc_ops = { &rsrc_cmp_node, &rsrc_cmp_key }; /* * Consider the archive file we are reading may be SFX. * So we have to calculate a SFX header size to revise * ZIP header offsets. */ correction = zip->end_of_central_directory_offset - (zip->central_directory_offset + zip->central_directory_size); /* The central directory offset is relative value, and so * we revise this offset for SFX. */ zip->central_directory_offset += correction; __archive_read_seek(a, zip->central_directory_offset, SEEK_SET); zip->offset = zip->central_directory_offset; __archive_rb_tree_init(&zip->tree, &rb_ops); __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops); zip->zip_entries = calloc(zip->central_directory_entries, sizeof(struct zip_entry)); for (i = 0; i < zip->central_directory_entries; ++i) { struct zip_entry *zip_entry = &zip->zip_entries[i]; size_t filename_length, extra_length, comment_length; uint32_t external_attributes; const char *name, *p, *r; if ((p = __archive_read_ahead(a, 46, NULL)) == NULL) return ARCHIVE_FATAL; if (memcmp(p, "PK\001\002", 4) != 0) { archive_set_error(&a->archive, -1, "Invalid central directory signature"); return ARCHIVE_FATAL; } zip->have_central_directory = 1; /* version = p[4]; */ zip_entry->system = p[5]; /* version_required = archive_le16dec(p + 6); */ zip_entry->flags = archive_le16dec(p + 8); zip_entry->compression = (char)archive_le16dec(p + 10); zip_entry->mtime = zip_time(p + 12); zip_entry->crc32 = archive_le32dec(p + 16); zip_entry->compressed_size = archive_le32dec(p + 20); zip_entry->uncompressed_size = archive_le32dec(p + 24); filename_length = archive_le16dec(p + 28); extra_length = archive_le16dec(p + 30); comment_length = archive_le16dec(p + 32); /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */ /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */ external_attributes = archive_le32dec(p + 38); zip_entry->local_header_offset = archive_le32dec(p + 42) + correction; /* If we can't guess the mode, leave it zero here; when we read the local file header we might get more information. */ zip_entry->mode = 0; if (zip_entry->system == 3) { zip_entry->mode = external_attributes >> 16; } /* * Mac resource fork files are stored under the * "__MACOSX/" directory, so we should check if * it is. */ /* Make sure we have the file name. */ if ((p = __archive_read_ahead(a, 46 + filename_length, NULL)) == NULL) return ARCHIVE_FATAL; name = p + 46; r = rsrc_basename(name, filename_length); if (filename_length >= 9 && strncmp("__MACOSX/", name, 9) == 0) { /* If this file is not a resource fork nor * a directory. We should treat it as a non * resource fork file to expose it. */ if (name[filename_length-1] != '/' && (r - name < 3 || r[0] != '.' || r[1] != '_')) { __archive_rb_tree_insert_node(&zip->tree, &zip_entry->node); /* Expose its parent directories. */ expose_parent_dirs(zip, name, filename_length); } else { /* This file is a resource fork file or * a directory. */ archive_strncpy(&(zip_entry->rsrcname), name, filename_length); __archive_rb_tree_insert_node(&zip->tree_rsrc, &zip_entry->node); } } else { /* Generate resource fork name to find its resource * file at zip->tree_rsrc. */ archive_strcpy(&(zip_entry->rsrcname), "__MACOSX/"); archive_strncat(&(zip_entry->rsrcname), name, r - name); archive_strcat(&(zip_entry->rsrcname), "._"); archive_strncat(&(zip_entry->rsrcname), name + (r - name), filename_length - (r - name)); /* Register an entry to RB tree to sort it by * file offset. */ __archive_rb_tree_insert_node(&zip->tree, &zip_entry->node); } /* We don't read the filename until we get to the local file header. Reading it here would speed up table-of-contents operations (removing the need to find and read local file header to get the filename) at the cost of requiring a lot of extra space. */ /* We don't read the extra block here. We assume it will be duplicated at the local file header. */ __archive_read_consume(a, 46 + filename_length + extra_length + comment_length); }
/* * TODO: This is a performance sink because it forces the read core to * drop buffered data from the start of file, which will then have to * be re-read again if this bidder loses. * * We workaround this a little by passing in the best bid so far so * that later bidders can do nothing if they know they'll never * outbid. But we can certainly do better... */ static int archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid) { struct zip *zip = (struct zip *)a->format->data; int64_t filesize; const char *p; /* If someone has already bid more than 32, then avoid trashing the look-ahead buffers with a seek. */ if (best_bid > 32) return (-1); filesize = __archive_read_seek(a, -22, SEEK_END); /* If we can't seek, then we can't bid. */ if (filesize <= 0) return 0; /* TODO: More robust search for end of central directory record. */ if ((p = __archive_read_ahead(a, 22, NULL)) == NULL) return 0; /* First four bytes are signature for end of central directory record. Four zero bytes ensure this isn't a multi-volume Zip file (which we don't yet support). */ if (memcmp(p, "PK\005\006\000\000\000\000", 8) != 0) { int64_t i, tail; int found; /* * If there is a comment in end of central directory * record, 22 bytes are too short. we have to read more * to properly detect the record. Hopefully, a length * of the comment is not longer than 16362 bytes(16K-22). */ if (filesize + 22 > 1024 * 16) { tail = 1024 * 16; filesize = __archive_read_seek(a, tail * -1, SEEK_END); } else { tail = filesize + 22; filesize = __archive_read_seek(a, 0, SEEK_SET); } if (filesize < 0) return 0; if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL) return 0; for (found = 0, i = 0;!found && i < tail - 22;) { switch (p[i]) { case 'P': if (memcmp(p+i, "PK\005\006\000\000\000\000", 8) == 0) { p += i; filesize += tail - (22 + archive_le16dec(p+20)); found = 1; } else i += 8; break; case 'K': i += 7; break; case 005: i += 6; break; case 006: i += 5; break; default: i += 1; break; } } if (!found) return 0; } /* Since we've already done the hard work of finding the end of central directory record, let's save the important information. */ zip->central_directory_entries = archive_le16dec(p + 10); zip->central_directory_size = archive_le32dec(p + 12); zip->central_directory_offset = archive_le32dec(p + 16); zip->end_of_central_directory_offset = filesize; /* Just one volume, so central dir must all be on this volume. */ if (zip->central_directory_entries != archive_le16dec(p + 8)) return 0; /* Central directory can't extend beyond end of this file. */ if (zip->central_directory_offset + (int64_t)zip->central_directory_size > filesize) return 0; /* This is just a tiny bit higher than the maximum returned by the streaming Zip bidder. This ensures that the more accurate seeking Zip parser wins whenever seek is available. */ return 32; }
/* * Test whether we can handle this data. * * <sigh> LZMA has a rather poor file signature. Zeros do not * make good signature bytes as a rule, and the only non-zero byte * here is an ASCII character. For example, an uncompressed tar * archive whose first file is ']' would satisfy this check. It may * be necessary to exclude LZMA from compression_all() because of * this. Clients of libarchive would then have to explicitly enable * LZMA checking instead of (or in addition to) compression_all() when * they have other evidence (file name, command-line option) to go on. */ static int lzma_bidder_bid(struct archive_read_filter_bidder *self, struct archive_read_filter *filter) { const unsigned char *buffer; ssize_t avail; uint32_t dicsize; uint64_t uncompressed_size; int bits_checked; (void)self; /* UNUSED */ buffer = __archive_read_filter_ahead(filter, 14, &avail); if (buffer == NULL) return (0); /* First byte of raw LZMA stream is commonly 0x5d. * The first byte is a special number, which consists of * three parameters of LZMA compression, a number of literal * context bits(which is from 0 to 8, default is 3), a number * of literal pos bits(which is from 0 to 4, default is 0), * a number of pos bits(which is from 0 to 4, default is 2). * The first byte is made by * (pos bits * 5 + literal pos bit) * 9 + * literal contest bit, * and so the default value in this field is * (2 * 5 + 0) * 9 + 3 = 0x5d. * lzma of LZMA SDK has options to change those parameters. * It means a range of this field is from 0 to 224. And lzma of * XZ Utils with option -e records 0x5e in this field. */ /* NOTE: If this checking of the first byte increases false * recognition, we should allow only 0x5d and 0x5e for the first * byte of LZMA stream. */ bits_checked = 0; if (buffer[0] > (4 * 5 + 4) * 9 + 8) return (0); /* Most likely value in the first byte of LZMA stream. */ if (buffer[0] == 0x5d || buffer[0] == 0x5e) bits_checked += 8; /* Sixth through fourteenth bytes are uncompressed size, * stored in little-endian order. `-1' means uncompressed * size is unknown and lzma of XZ Utils always records `-1' * in this field. */ uncompressed_size = archive_le64dec(buffer+5); if (uncompressed_size == (uint64_t)ARCHIVE_LITERAL_LL(-1)) bits_checked += 64; /* Second through fifth bytes are dictionary size, stored in * little-endian order. The minimum dictionary size is * 1 << 12(4KiB) which the lzma of LZMA SDK uses with option * -d12 and the maxinam dictionary size is 1 << 27(128MiB) * which the one uses with option -d27. * NOTE: A comment of LZMA SDK source code says this dictionary * range is from 1 << 12 to 1 << 30. */ dicsize = archive_le32dec(buffer+1); switch (dicsize) { case 0x00001000:/* lzma of LZMA SDK option -d12. */ case 0x00002000:/* lzma of LZMA SDK option -d13. */ case 0x00004000:/* lzma of LZMA SDK option -d14. */ case 0x00008000:/* lzma of LZMA SDK option -d15. */ case 0x00010000:/* lzma of XZ Utils option -0 and -1. * lzma of LZMA SDK option -d16. */ case 0x00020000:/* lzma of LZMA SDK option -d17. */ case 0x00040000:/* lzma of LZMA SDK option -d18. */ case 0x00080000:/* lzma of XZ Utils option -2. * lzma of LZMA SDK option -d19. */ case 0x00100000:/* lzma of XZ Utils option -3. * lzma of LZMA SDK option -d20. */ case 0x00200000:/* lzma of XZ Utils option -4. * lzma of LZMA SDK option -d21. */ case 0x00400000:/* lzma of XZ Utils option -5. * lzma of LZMA SDK option -d22. */ case 0x00800000:/* lzma of XZ Utils option -6. * lzma of LZMA SDK option -d23. */ case 0x01000000:/* lzma of XZ Utils option -7. * lzma of LZMA SDK option -d24. */ case 0x02000000:/* lzma of XZ Utils option -8. * lzma of LZMA SDK option -d25. */ case 0x04000000:/* lzma of XZ Utils option -9. * lzma of LZMA SDK option -d26. */ case 0x08000000:/* lzma of LZMA SDK option -d27. */ bits_checked += 32; break; default: /* If a memory usage for encoding was not enough on * the platform where LZMA stream was made, lzma of * XZ Utils automatically decreased the dictionary * size to enough memory for encoding by 1Mi bytes * (1 << 20).*/ if (dicsize <= 0x03F00000 && dicsize >= 0x00300000 && (dicsize & ((1 << 20)-1)) == 0 && bits_checked == 8 + 64) { bits_checked += 32; break; } /* Otherwise dictionary size is unlikely. But it is * possible that someone makes lzma stream with * liblzma/LZMA SDK in one's dictionary size. */ return (0); } /* TODO: The above test is still very weak. It would be * good to do better. */ return (bits_checked); }
static int archive_read_format_zip_read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset) { int r; struct zip *zip; zip = (struct zip *)(a->format->data); /* * If we hit end-of-entry last time, clean up and return * ARCHIVE_EOF this time. */ if (zip->end_of_entry) { *offset = zip->entry_uncompressed_bytes_read; *size = 0; *buff = NULL; return (ARCHIVE_EOF); } switch(zip->compression) { case 0: /* No compression. */ r = zip_read_data_none(a, buff, size, offset); break; case 8: /* Deflate compression. */ r = zip_read_data_deflate(a, buff, size, offset); break; default: /* Unsupported compression. */ *buff = NULL; *size = 0; *offset = 0; /* Return a warning. */ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Unsupported ZIP compression method (%s)", zip->compression_name); if (zip->flags & ZIP_LENGTH_AT_END) { /* * ZIP_LENGTH_AT_END requires us to * decompress the entry in order to * skip it, but we don't know this * compression method, so we give up. */ r = ARCHIVE_FATAL; } else { /* We can't decompress this entry, but we will * be able to skip() it and try the next entry. */ r = ARCHIVE_WARN; } break; } if (r != ARCHIVE_OK) return (r); /* Update checksum */ if (*size) zip->entry_crc32 = crc32(zip->entry_crc32, *buff, *size); /* If we hit the end, swallow any end-of-data marker. */ if (zip->end_of_entry) { if (zip->flags & ZIP_LENGTH_AT_END) { const char *p; if ((p = __archive_read_ahead(a, 16, NULL)) == NULL) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Truncated ZIP end-of-file record"); return (ARCHIVE_FATAL); } zip->crc32 = archive_le32dec(p + 4); zip->compressed_size = archive_le32dec(p + 8); zip->uncompressed_size = archive_le32dec(p + 12); __archive_read_consume(a, 16); } /* Check file size, CRC against these values. */ if (zip->compressed_size != zip->entry_compressed_bytes_read) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP compressed data is wrong size"); return (ARCHIVE_WARN); } /* Size field only stores the lower 32 bits of the actual size. */ if ((zip->uncompressed_size & UINT32_MAX) != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP uncompressed data is wrong size"); return (ARCHIVE_WARN); } /* Check computed CRC against header */ if (zip->crc32 != zip->entry_crc32) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "ZIP bad CRC: 0x%lx should be 0x%lx", zip->entry_crc32, zip->crc32); return (ARCHIVE_WARN); } } /* Return EOF immediately if this is a non-regular file. */ if (AE_IFREG != (zip->mode & AE_IFMT)) return (ARCHIVE_EOF); return (ARCHIVE_OK); }
/* * Read and verify the header. * * Returns zero if the header couldn't be validated, else returns * number of bytes in header. If pbits is non-NULL, it receives a * count of bits verified, suitable for use by bidder. */ static ssize_t peek_at_header(struct archive_read_filter *filter, int *pbits, struct private_data *state) { const unsigned char *p; ssize_t avail, len; int bits = 0; int header_flags; /* Start by looking at the first ten bytes of the header, which * is all fixed layout. */ len = 10; p = __archive_read_filter_ahead(filter, len, &avail); if (p == NULL || avail == 0) return (0); /* We only support deflation- third byte must be 0x08. */ if (memcmp(p, "\x1F\x8B\x08", 3) != 0) return (0); bits += 24; if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */ return (0); bits += 3; header_flags = p[3]; /* Bytes 4-7 are mod time in little endian. */ if (state) state->mtime = archive_le32dec(p + 4); /* Byte 8 is deflate flags. */ /* XXXX TODO: return deflate flags back to consume_header for use in initializing the decompressor. */ /* Byte 9 is OS. */ /* Optional extra data: 2 byte length plus variable body. */ if (header_flags & 4) { p = __archive_read_filter_ahead(filter, len + 2, &avail); if (p == NULL) return (0); len += ((int)p[len + 1] << 8) | (int)p[len]; len += 2; } /* Null-terminated optional filename. */ if (header_flags & 8) { ssize_t file_start = len; do { ++len; if (avail < len) p = __archive_read_filter_ahead(filter, len, &avail); if (p == NULL) return (0); } while (p[len - 1] != 0); if (state) { /* Reset the name in case of repeat header reads. */ free(state->name); state->name = strdup((const char *)&p[file_start]); } } /* Null-terminated optional comment. */ if (header_flags & 16) { do { ++len; if (avail < len) p = __archive_read_filter_ahead(filter, len, &avail); if (p == NULL) return (0); } while (p[len - 1] != 0); } /* Optional header CRC */ if ((header_flags & 2)) { p = __archive_read_filter_ahead(filter, len + 2, &avail); if (p == NULL) return (0); #if 0 int hcrc = ((int)p[len + 1] << 8) | (int)p[len]; int crc = /* XXX TODO: Compute header CRC. */; if (crc != hcrc) return (0); bits += 16; #endif len += 2; }