extern void lzma_check_update(lzma_check_state *check, lzma_check type, const uint8_t *buf, size_t size) { switch (type) { #ifdef HAVE_CHECK_CRC32 case LZMA_CHECK_CRC32: check->state.crc32 = lzma_crc32(buf, size, check->state.crc32); break; #endif #ifdef HAVE_CHECK_CRC64 case LZMA_CHECK_CRC64: check->state.crc64 = lzma_crc64(buf, size, check->state.crc64); break; #endif #ifdef HAVE_CHECK_SHA256 case LZMA_CHECK_SHA256: lzma_sha256_update(buf, size, check); break; #endif default: break; } return; }
lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in) { // Magic if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0) return LZMA_FORMAT_ERROR; // Verify the CRC32 so we can distinguish between corrupt // and unsupported files. const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic), LZMA_STREAM_FLAGS_SIZE, 0); if (crc != unaligned_read32le(in + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE)) return LZMA_DATA_ERROR; // Stream Flags if (stream_flags_decode(options, in + sizeof(lzma_header_magic))) return LZMA_OPTIONS_ERROR; // Set Backward Size to indicate unknown value. That way // lzma_stream_flags_compare() can be used to compare Stream Header // and Stream Footer while keeping it useful also for comparing // two Stream Footers. options->backward_size = LZMA_VLI_UNKNOWN; return LZMA_OK; }
lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out) { assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic) == LZMA_STREAM_HEADER_SIZE); if (options->version != 0) return LZMA_OPTIONS_ERROR; // Backward Size if (!is_backward_size_valid(options)) return LZMA_PROG_ERROR; unaligned_write32le(out + 4, options->backward_size / 4 - 1); // Stream Flags if (stream_flags_encode(options, out + 2 * 4)) return LZMA_PROG_ERROR; // CRC32 const uint32_t crc = lzma_crc32( out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0); unaligned_write32le(out, crc); // Magic memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE, lzma_footer_magic, sizeof(lzma_footer_magic)); return LZMA_OK; }
lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in) { uint32_t crc; // Magic if (memcmp(in + sizeof(uint32_t) * 2 + LZMA_STREAM_FLAGS_SIZE, lzma_footer_magic, sizeof(lzma_footer_magic)) != 0) return LZMA_FORMAT_ERROR; // CRC32 crc = lzma_crc32(in + sizeof(uint32_t), sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0); if (crc != unaligned_read32le(in)) return LZMA_DATA_ERROR; // Stream Flags if (stream_flags_decode(options, in + sizeof(uint32_t) * 2)) return LZMA_OPTIONS_ERROR; // Backward Size options->backward_size = unaligned_read32le(in + sizeof(uint32_t)); options->backward_size = (options->backward_size + 1) * 4; return LZMA_OK; }
lzma_block_header_encode(const lzma_block *block, uint8_t *out) { // Validate everything but filters. if (lzma_block_unpadded_size(block) == 0 || !lzma_vli_is_valid(block->uncompressed_size)) return LZMA_PROG_ERROR; // Indicate the size of the buffer _excluding_ the CRC32 field. const size_t out_size = block->header_size - 4; // Store the Block Header Size. out[0] = out_size / 4; // We write Block Flags in pieces. out[1] = 0x00; size_t out_pos = 2; // Compressed Size if (block->compressed_size != LZMA_VLI_UNKNOWN) { return_if_error(lzma_vli_encode(block->compressed_size, NULL, out, &out_pos, out_size)); out[1] |= 0x40; } // Uncompressed Size if (block->uncompressed_size != LZMA_VLI_UNKNOWN) { return_if_error(lzma_vli_encode(block->uncompressed_size, NULL, out, &out_pos, out_size)); out[1] |= 0x80; } // Filter Flags if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN) return LZMA_PROG_ERROR; size_t filter_count = 0; do { // There can be a maximum of four filters. if (filter_count == LZMA_FILTERS_MAX) return LZMA_PROG_ERROR; return_if_error(lzma_filter_flags_encode( block->filters + filter_count, out, &out_pos, out_size)); } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN); out[1] |= filter_count - 1; // Padding memzero(out + out_pos, out_size - out_pos); // CRC32 unaligned_write32le(out + out_size, lzma_crc32(out, out_size, 0)); return LZMA_OK; }
static void test_decode_invalid(void) { known_flags.check = LZMA_CHECK_NONE; known_flags.backward_size = 1024; expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); // Test 1 (invalid Magic Bytes) buffer[5] ^= 1; succeed(test_header_decoder(LZMA_FORMAT_ERROR)); buffer[5] ^= 1; // Test 2a (valid CRC32) uint32_t crc = lzma_crc32(buffer + 6, 2, 0); unaligned_write32le(buffer + 8, crc); succeed(test_header_decoder(LZMA_OK)); // Test 2b (invalid Stream Flags with valid CRC32) buffer[6] ^= 0x20; crc = lzma_crc32(buffer + 6, 2, 0); unaligned_write32le(buffer + 8, crc); succeed(test_header_decoder(LZMA_OPTIONS_ERROR)); // Test 3 (invalid CRC32) expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); buffer[9] ^= 1; succeed(test_header_decoder(LZMA_DATA_ERROR)); // Test 4 (invalid Stream Flags with valid CRC32) expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); buffer[9] ^= 0x40; crc = lzma_crc32(buffer + 4, 6, 0); unaligned_write32le(buffer, crc); succeed(test_footer_decoder(LZMA_OPTIONS_ERROR)); // Test 5 (invalid Magic Bytes) expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); buffer[11] ^= 1; succeed(test_footer_decoder(LZMA_FORMAT_ERROR)); }
Handle<Value> lzmaCRC32(const Arguments& args) { HandleScope scope; Local<Integer> arg = Local<Integer>::Cast(args[1]); if (arg.IsEmpty() || args[1]->IsUndefined()) arg = Integer::New(0); const uint8_t* data; size_t datalen; if (!readBufferFromObj(args[0], data, datalen)) { ThrowException(Exception::TypeError(String::New("CRC32 expects Buffer as input"))); return scope.Close(Undefined()); } return scope.Close(Integer::NewFromUnsigned(lzma_crc32(data, datalen, arg->Value()))); }
/* * Write data to the compressed stream. */ static int archive_compressor_xz_write(struct archive_write_filter *f, const void *buff, size_t length) { struct private_data *data = (struct private_data *)f->data; int ret; /* Update statistics */ data->total_in += length; if (f->code == ARCHIVE_FILTER_LZIP) data->crc32 = lzma_crc32(buff, length, data->crc32); /* Compress input data to output buffer */ data->stream.next_in = buff; data->stream.avail_in = length; if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK) return (ret); return (ARCHIVE_OK); }
int main(void) { uint32_t crc = 0; do { uint8_t buf[BUFSIZ]; const size_t size = fread(buf, 1, sizeof(buf), stdin); crc = lzma_crc32(buf, size, crc); } while (!ferror(stdin) && !feof(stdin)); //printf("%08" PRIX32 "\n", crc); // I want it little endian so it's easy to work with hex editor. printf("%02" PRIX32 " ", crc & 0xFF); printf("%02" PRIX32 " ", (crc >> 8) & 0xFF); printf("%02" PRIX32 " ", (crc >> 16) & 0xFF); printf("%02" PRIX32 " ", crc >> 24); printf("\n"); return 0; }
lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out) { assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE + 4 == LZMA_STREAM_HEADER_SIZE); if (options->version != 0) return LZMA_OPTIONS_ERROR; // Magic memcpy(out, lzma_header_magic, sizeof(lzma_header_magic)); // Stream Flags if (stream_flags_encode(options, out + sizeof(lzma_header_magic))) return LZMA_PROG_ERROR; // CRC32 of the Stream Header const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic), LZMA_STREAM_FLAGS_SIZE, 0); unaligned_write32le(out + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE, crc); return LZMA_OK; }
lzma_block_header_decode(lzma_block *block, lzma_allocator *allocator, const uint8_t *in) { const size_t filter_count = (in[1] & 3) + 1; size_t in_size; size_t i; // Start after the Block Header Size and Block Flags fields. size_t in_pos = 2; // NOTE: We consider the header to be corrupt not only when the // CRC32 doesn't match, but also when variable-length integers // are invalid or over 63 bits, or if the header is too small // to contain the claimed information. // Initialize the filter options array. This way the caller can // safely free() the options even if an error occurs in this function. for (i = 0; i <= LZMA_FILTERS_MAX; ++i) { block->filters[i].id = LZMA_VLI_UNKNOWN; block->filters[i].options = NULL; } // Always zero for now. block->version = 0; // Validate Block Header Size and Check type. The caller must have // already set these, so it is a programming error if this test fails. if (lzma_block_header_size_decode(in[0]) != block->header_size || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX) return LZMA_PROG_ERROR; // Exclude the CRC32 field. in_size = block->header_size - 4; // Verify CRC32 if (lzma_crc32(in, in_size, 0) != unaligned_read32le(in + in_size)) return LZMA_DATA_ERROR; // Check for unsupported flags. if (in[1] & 0x3C) return LZMA_OPTIONS_ERROR; // Compressed Size if (in[1] & 0x40) { return_if_error(lzma_vli_decode(&block->compressed_size, NULL, in, &in_pos, in_size)); // Validate Compressed Size. This checks that it isn't zero // and that the total size of the Block is a valid VLI. if (lzma_block_unpadded_size(block) == 0) return LZMA_DATA_ERROR; } else { block->compressed_size = LZMA_VLI_UNKNOWN; } // Uncompressed Size if (in[1] & 0x80) return_if_error(lzma_vli_decode(&block->uncompressed_size, NULL, in, &in_pos, in_size)); else block->uncompressed_size = LZMA_VLI_UNKNOWN; // Filter Flags for (i = 0; i < filter_count; ++i) { const lzma_ret ret = lzma_filter_flags_decode( &block->filters[i], allocator, in, &in_pos, in_size); if (ret != LZMA_OK) { free_properties(block, allocator); return ret; } } // Padding while (in_pos < in_size) { if (in[in_pos++] != 0x00) { free_properties(block, allocator); // Possibly some new field present so use // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR. return LZMA_OPTIONS_ERROR; } } return LZMA_OK; }