@@ -27,12 +27,14 @@ #endif #import "OFStream.h" #import "OFSeekableStream.h" #import "OFString.h" +#import "crc16.h" #import "huffman_tree.h" +#import "OFChecksumFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" #import "OFTruncatedDataException.h" @@ -69,10 +71,11 @@ @interface OFLHAArchive_FileReadStream: OFStream { OF_KINDOF(OFStream *) _stream; uint32_t _toRead; + uint16_t _expectedCRC16, _CRC16; bool _atEndOfStream; } - (instancetype)of_initWithStream: (OFStream *)stream entry: (OFLHAArchiveEntry *)entry; @@ -726,10 +729,12 @@ [method isEqual: @"-lz4-"] || [method isEqual: @"-lh5-"] || [method isEqual: @"-lh6-"] || [method isEqual: @"-lh6-"]) _toRead = [entry uncompressedSize]; else _toRead = [entry compressedSize]; + + _expectedCRC16 = [entry CRC16]; } @catch (id e) { [self release]; @throw e; } @@ -759,13 +764,18 @@ ret = [_stream readIntoBuffer: buffer length: length]; _toRead -= ret; + _CRC16 = of_crc16(_CRC16, buffer, ret); - if (_toRead == 0) + if (_toRead == 0) { _atEndOfStream = true; + + if (_CRC16 != _expectedCRC16) + @throw [OFChecksumFailedException exception]; + } return ret; } - (bool)lowlevelIsAtEndOfStream