X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Flzx-decompress.c;h=513944ee18525ab69715bb0341188b2044ad2322;hb=d20508c65ee45d265f48964ba312f10f8c67a9c8;hp=1e8532741b3b9f2240db9a3a69246ed4049e0df6;hpb=3d8ef754a66f76c8f7121b65a4e466bce6a75f0f;p=wimlib diff --git a/src/lzx-decompress.c b/src/lzx-decompress.c index 1e853274..513944ee 100644 --- a/src/lzx-decompress.c +++ b/src/lzx-decompress.c @@ -378,22 +378,15 @@ lzx_read_block_header(struct input_bitstream *istream, * *already* aligned, the correct thing to do is to throw away * the next 16 bits. */ - if (istream->bitsleft == 0) { - if (istream->data_bytes_left < 14) - return -1; - istream->data += 2; - istream->data_bytes_left -= 2; - } else { - if (istream->data_bytes_left < 12) - return -1; - istream->bitsleft = 0; - istream->bitbuf = 0; - } - queue->R[0] = le32_to_cpu(*(le32*)(istream->data + 0)); - queue->R[1] = le32_to_cpu(*(le32*)(istream->data + 4)); - queue->R[2] = le32_to_cpu(*(le32*)(istream->data + 8)); - istream->data += 12; - istream->data_bytes_left -= 12; + bitstream_ensure_bits(istream, 1); + bitstream_align(istream); + queue->R[0] = bitstream_read_u32(istream); + queue->R[1] = bitstream_read_u32(istream); + queue->R[2] = bitstream_read_u32(istream); + + /* Offsets of 0 are invalid. */ + if (queue->R[0] == 0 || queue->R[1] == 0 || queue->R[2] == 0) + return -1; break; default: @@ -644,21 +637,19 @@ lzx_decompress(const void *compressed_data, size_t compressed_size, } else { /* Uncompressed block. */ + const u8 *p; - if (istream.data_bytes_left < block_size) + p = bitstream_read_bytes(&istream, block_size); + if (!p) return -1; - memcpy(&((u8*)uncompressed_data)[window_pos], istream.data, - block_size); - istream.data += block_size; - istream.data_bytes_left -= block_size; + memcpy(&((u8*)uncompressed_data)[window_pos], p, block_size); /* Re-align the bitstream if an odd number of bytes was * read. */ - if (istream.data_bytes_left && (block_size & 1)) { - istream.data_bytes_left--; - istream.data++; - } + if (block_size & 1) + bitstream_read_byte(&istream); + may_have_e8_byte = true; } }