@@ -14,14 +14,13 @@ */ #include "config.h" #import "OFGZIPStream.h" -#import "OFInflateStream.h" +#import "OFCRC32.h" #import "OFDate.h" - -#import "crc32.h" +#import "OFInflateStream.h" #import "OFChecksumMismatchException.h" #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" @@ -29,24 +28,21 @@ @implementation OFGZIPStream @synthesize operatingSystemMadeOn = _operatingSystemMadeOn; @synthesize modificationDate = _modificationDate; -+ (instancetype)streamWithStream: (OFStream *)stream - mode: (OFString *)mode ++ (instancetype)streamWithStream: (OFStream *)stream mode: (OFString *)mode { - return [[[self alloc] initWithStream: stream - mode: mode] autorelease]; + return [[[self alloc] initWithStream: stream mode: mode] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } -- (instancetype)initWithStream: (OFStream *)stream - mode: (OFString *)mode +- (instancetype)initWithStream: (OFStream *)stream mode: (OFString *)mode { self = [super init]; @try { if (![mode isEqual: @"r"]) @@ -53,12 +49,11 @@ @throw [OFNotImplementedException exceptionWithSelector: _cmd object: nil]; _stream = [stream retain]; - _operatingSystemMadeOn = - OF_GZIP_STREAM_OPERATING_SYSTEM_UNKNOWN; + _operatingSystemMadeOn = OFGZIPStreamOperatingSystemUnknown; _CRC32 = ~0; } @catch (id e) { [self release]; @throw e; } @@ -75,52 +70,49 @@ [_modificationDate release]; [super dealloc]; } -- (size_t)lowlevelReadIntoBuffer: (void *)buffer - length: (size_t)length +- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; for (;;) { uint8_t byte; uint32_t CRC32, uncompressedSize; if (_stream.atEndOfStream) { - if (_state != OF_GZIP_STREAM_ID1) + if (_state != OFGZIPStreamStateID1) @throw [OFTruncatedDataException exception]; return 0; } switch (_state) { - case OF_GZIP_STREAM_ID1: - case OF_GZIP_STREAM_ID2: - case OF_GZIP_STREAM_COMPRESSION_METHOD: - if ([_stream readIntoBuffer: &byte - length: 1] < 1) + case OFGZIPStreamStateID1: + case OFGZIPStreamStateID2: + case OFGZIPStreamStateCompressionMethod: + if ([_stream readIntoBuffer: &byte length: 1] < 1) return 0; - if ((_state == OF_GZIP_STREAM_ID1 && byte != 0x1F) || - (_state == OF_GZIP_STREAM_ID2 && byte != 0x8B) || - (_state == OF_GZIP_STREAM_COMPRESSION_METHOD && + if ((_state == OFGZIPStreamStateID1 && byte != 0x1F) || + (_state == OFGZIPStreamStateID2 && byte != 0x8B) || + (_state == OFGZIPStreamStateCompressionMethod && byte != 8)) @throw [OFInvalidFormatException exception]; _state++; break; - case OF_GZIP_STREAM_FLAGS: - if ([_stream readIntoBuffer: &byte - length: 1] < 1) + case OFGZIPStreamStateFlags: + if ([_stream readIntoBuffer: &byte length: 1] < 1) return 0; _flags = byte; _state++; break; - case OF_GZIP_STREAM_MODIFICATION_TIME: + case OFGZIPStreamStateModificationDate: _bytesRead += [_stream readIntoBuffer: _buffer + _bytesRead length: 4 - _bytesRead]; if (_bytesRead < 4) @@ -135,28 +127,26 @@ (_buffer[1] << 8) | _buffer[0]]; _bytesRead = 0; _state++; break; - case OF_GZIP_STREAM_EXTRA_FLAGS: - if ([_stream readIntoBuffer: &byte - length: 1] < 1) + case OFGZIPStreamStateExtraFlags: + if ([_stream readIntoBuffer: &byte length: 1] < 1) return 0; _extraFlags = byte; _state++; break; - case OF_GZIP_STREAM_OPERATING_SYSTEM: - if ([_stream readIntoBuffer: &byte - length: 1] < 1) + case OFGZIPStreamStateOperatingSystem: + if ([_stream readIntoBuffer: &byte length: 1] < 1) return 0; _operatingSystemMadeOn = byte; _state++; break; - case OF_GZIP_STREAM_EXTRA_LENGTH: - if (!(_flags & OF_GZIP_STREAM_FLAG_EXTRA)) { + case OFGZIPStreamStateExtraLength: + if (!(_flags & OFGZIPStreamFlagExtra)) { _state += 2; break; } _bytesRead += [_stream @@ -168,11 +158,11 @@ _extraLength = (_buffer[1] << 8) | _buffer[0]; _bytesRead = 0; _state++; break; - case OF_GZIP_STREAM_EXTRA: + case OFGZIPStreamStateExtra: { char tmp[512]; size_t toRead = _extraLength - _bytesRead; if (toRead > 512) @@ -186,12 +176,12 @@ return 0; _bytesRead = 0; _state++; break; - case OF_GZIP_STREAM_NAME: - if (!(_flags & OF_GZIP_STREAM_FLAG_NAME)) { + case OFGZIPStreamStateName: + if (!(_flags & OFGZIPStreamFlagName)) { _state++; break; } do { @@ -200,12 +190,12 @@ return 0; } while (byte != 0); _state++; break; - case OF_GZIP_STREAM_COMMENT: - if (!(_flags & OF_GZIP_STREAM_FLAG_COMMENT)) { + case OFGZIPStreamStateComment: + if (!(_flags & OFGZIPStreamFlagComment)) { _state++; break; } do { @@ -214,12 +204,12 @@ return 0; } while (byte != 0); _state++; break; - case OF_GZIP_STREAM_HEADER_CRC16: - if (!(_flags & OF_GZIP_STREAM_FLAG_HEADER_CRC16)) { + case OFGZIPStreamStateHeaderCRC16: + if (!(_flags & OFGZIPStreamFlagHeaderCRC16)) { _state++; break; } _bytesRead += [_stream @@ -236,21 +226,21 @@ */ _bytesRead = 0; _state++; break; - case OF_GZIP_STREAM_DATA: + case OFGZIPStreamStateData: if (_inflateStream == nil) _inflateStream = [[OFInflateStream alloc] initWithStream: _stream]; if (!_inflateStream.atEndOfStream) { size_t bytesRead = [_inflateStream readIntoBuffer: buffer length: length]; - _CRC32 = of_crc32(_CRC32, buffer, bytesRead); + _CRC32 = OFCRC32(_CRC32, buffer, bytesRead); _uncompressedSize += bytesRead; return bytesRead; } @@ -257,11 +247,11 @@ [_inflateStream release]; _inflateStream = nil; _state++; break; - case OF_GZIP_STREAM_CRC32: + case OFGZIPStreamStateCRC32: _bytesRead += [_stream readIntoBuffer: _buffer length: 4 - _bytesRead]; if (_bytesRead < 4) return 0; @@ -281,11 +271,11 @@ _bytesRead = 0; _CRC32 = ~0; _state++; break; - case OF_GZIP_STREAM_UNCOMPRESSED_SIZE: + case OFGZIPStreamStateUncompressedSize: _bytesRead += [_stream readIntoBuffer: _buffer length: 4 - _bytesRead]; uncompressedSize = ((uint32_t)_buffer[3] << 24) | (_buffer[2] << 16) | (_buffer[1] << 8) | _buffer[0]; @@ -300,11 +290,11 @@ expectedChecksum: expected]; } _bytesRead = 0; _uncompressedSize = 0; - _state = OF_GZIP_STREAM_ID1; + _state = OFGZIPStreamStateID1; break; } } } @@ -316,11 +306,11 @@ return _stream.atEndOfStream; } - (bool)hasDataInReadBuffer { - if (_state == OF_GZIP_STREAM_DATA) + if (_state == OFGZIPStreamStateData) return (super.hasDataInReadBuffer || _inflateStream.hasDataInReadBuffer); return (super.hasDataInReadBuffer || _stream.hasDataInReadBuffer); }