@@ -18,30 +18,30 @@ #include #import "OFLHADecompressingStream.h" #import "OFKernelEventObserver.h" -#import "huffman_tree.h" +#import "OFHuffmanTree.h" #import "OFInvalidFormatException.h" #import "OFNotOpenException.h" -enum state { - STATE_BLOCK_HEADER, - STATE_CODE_LEN_CODES_COUNT, - STATE_CODE_LEN_TREE, - STATE_CODE_LEN_TREE_SINGLE, - STATE_LITLEN_CODES_COUNT, - STATE_LITLEN_TREE, - STATE_LITLEN_TREE_SINGLE, - STATE_DIST_CODES_COUNT, - STATE_DIST_TREE, - STATE_DIST_TREE_SINGLE, - STATE_BLOCK_LITLEN, - STATE_BLOCK_DIST_LENGTH, - STATE_BLOCK_DIST_LENGTH_EXTRA, - STATE_BLOCK_LEN_DIST_PAIR +enum State { + stateBlockHeader, + stateCodeLenCodesCount, + stateCodeLenTree, + stateCodeLenTreeSingle, + stateLitLenCodesCount, + stateLitLenTree, + stateLitLenTreeSingle, + stateDistCodesCount, + stateDistTree, + stateDistTreeSingle, + stateBlockLitLen, + stateBlockDistLength, + stateBlockDistLengthExtra, + stateBlockLenDistPair }; @implementation OFLHADecompressingStream @synthesize bytesConsumed = _bytesConsumed; @@ -58,11 +58,11 @@ stream->_bufferLength) stream->_byte = stream->_buffer[stream->_bufferIndex++]; else { const size_t bufferLength = - OF_LHA_DECOMPRESSING_STREAM_BUFFER_SIZE; + OFLHADecompressingStreamBufferSize; size_t length = [stream->_stream readIntoBuffer: stream->_buffer length: bufferLength]; stream->_bytesConsumed += (uint32_t)length; @@ -106,11 +106,11 @@ _distanceBits = distanceBits; _dictionaryBits = dictionaryBits; _slidingWindowMask = (1u << dictionaryBits) - 1; - _slidingWindow = of_alloc(_slidingWindowMask + 1, 1); + _slidingWindow = OFAllocMemory(_slidingWindowMask + 1, 1); memset(_slidingWindow, ' ', _slidingWindowMask + 1); } @catch (id e) { [self release]; @throw e; } @@ -121,20 +121,20 @@ - (void)dealloc { if (_stream != nil) [self close]; - free(_slidingWindow); + OFFreeMemory(_slidingWindow); if (_codeLenTree != NULL) - of_huffman_tree_release(_codeLenTree); + OFHuffmanTreeFree(_codeLenTree); if (_litLenTree != NULL) - of_huffman_tree_release(_litLenTree); + OFHuffmanTreeFree(_litLenTree); if (_distTree != NULL) - of_huffman_tree_release(_distTree); + OFHuffmanTreeFree(_distTree); - free(_codesLengths); + OFFreeMemory(_codesLengths); [super dealloc]; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer_ @@ -146,43 +146,43 @@ if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; if (_stream.atEndOfStream && _bufferLength - _bufferIndex == 0 && - _state == STATE_BLOCK_HEADER) + _state == stateBlockHeader) return 0; start: - switch ((enum state)_state) { - case STATE_BLOCK_HEADER: + switch ((enum State)_state) { + case stateBlockHeader: if OF_UNLIKELY (!tryReadBits(self, &bits, 16)) return bytesWritten; _symbolsLeft = bits; - _state = STATE_CODE_LEN_CODES_COUNT; + _state = stateCodeLenCodesCount; goto start; - case STATE_CODE_LEN_CODES_COUNT: + case stateCodeLenCodesCount: if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) return bytesWritten; if OF_UNLIKELY (bits > 20) @throw [OFInvalidFormatException exception]; if OF_UNLIKELY (bits == 0) { - _state = STATE_CODE_LEN_TREE_SINGLE; + _state = stateCodeLenTreeSingle; goto start; } _codesCount = bits; _codesReceived = 0; - _codesLengths = of_alloc_zeroed(bits, 1); + _codesLengths = OFAllocZeroedMemory(bits, 1); _skip = true; - _state = STATE_CODE_LEN_TREE; + _state = stateCodeLenTree; goto start; - case STATE_CODE_LEN_TREE: + case stateCodeLenTree: while (_codesReceived < _codesCount) { if OF_UNLIKELY (_currentIsExtendedLength) { if OF_UNLIKELY (!tryReadBits(self, &bits, 1)) return bytesWritten; @@ -222,49 +222,48 @@ continue; } else _codesReceived++; } - _codeLenTree = of_huffman_tree_construct(_codesLengths, - _codesCount); - free(_codesLengths); + _codeLenTree = OFHuffmanTreeNew(_codesLengths, _codesCount); + OFFreeMemory(_codesLengths); _codesLengths = NULL; - _state = STATE_LITLEN_CODES_COUNT; + _state = stateLitLenCodesCount; goto start; - case STATE_CODE_LEN_TREE_SINGLE: + case stateCodeLenTreeSingle: if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) return bytesWritten; - _codeLenTree = of_huffman_tree_construct_single(bits); + _codeLenTree = OFHuffmanTreeNewSingle(bits); - _state = STATE_LITLEN_CODES_COUNT; + _state = stateLitLenCodesCount; goto start; - case STATE_LITLEN_CODES_COUNT: + case stateLitLenCodesCount: if OF_UNLIKELY (!tryReadBits(self, &bits, 9)) return bytesWritten; if OF_UNLIKELY (bits > 510) @throw [OFInvalidFormatException exception]; if OF_UNLIKELY (bits == 0) { - of_huffman_tree_release(_codeLenTree); + OFHuffmanTreeFree(_codeLenTree); _codeLenTree = NULL; - _state = STATE_LITLEN_TREE_SINGLE; + _state = stateLitLenTreeSingle; goto start; } _codesCount = bits; _codesReceived = 0; - _codesLengths = of_alloc_zeroed(bits, 1); + _codesLengths = OFAllocZeroedMemory(bits, 1); _skip = false; _treeIter = _codeLenTree; - _state = STATE_LITLEN_TREE; + _state = stateLitLenTree; goto start; - case STATE_LITLEN_TREE: + case stateLitLenTree: while (_codesReceived < _codesCount) { if OF_UNLIKELY (_skip) { uint16_t skipCount; switch (_codesLengths[_codesReceived]) { @@ -284,11 +283,11 @@ return bytesWritten; skipCount = bits + 20; break; default: - OF_ENSURE(0); + OFEnsure(0); } if OF_UNLIKELY (_codesReceived + skipCount > _codesCount) @throw [OFInvalidFormatException @@ -299,12 +298,12 @@ _skip = false; continue; } - if (!of_huffman_tree_walk(self, tryReadBits, - &_treeIter, &value)) + if (!OFHuffmanTreeWalk(self, tryReadBits, &_treeIter, + &value)) return bytesWritten; _treeIter = _codeLenTree; if (value < 3) { @@ -312,48 +311,47 @@ _skip = true; } else _codesLengths[_codesReceived++] = value - 2; } - _litLenTree = of_huffman_tree_construct(_codesLengths, - _codesCount); - free(_codesLengths); + _litLenTree = OFHuffmanTreeNew(_codesLengths, _codesCount); + OFFreeMemory(_codesLengths); _codesLengths = NULL; - of_huffman_tree_release(_codeLenTree); + OFHuffmanTreeFree(_codeLenTree); _codeLenTree = NULL; - _state = STATE_DIST_CODES_COUNT; + _state = stateDistCodesCount; goto start; - case STATE_LITLEN_TREE_SINGLE: + case stateLitLenTreeSingle: if OF_UNLIKELY (!tryReadBits(self, &bits, 9)) return bytesWritten; - _litLenTree = of_huffman_tree_construct_single(bits); + _litLenTree = OFHuffmanTreeNewSingle(bits); - _state = STATE_DIST_CODES_COUNT; + _state = stateDistCodesCount; goto start; - case STATE_DIST_CODES_COUNT: + case stateDistCodesCount: if OF_UNLIKELY (!tryReadBits(self, &bits, _distanceBits)) return bytesWritten; if OF_UNLIKELY (bits > _dictionaryBits) @throw [OFInvalidFormatException exception]; if OF_UNLIKELY (bits == 0) { - _state = STATE_DIST_TREE_SINGLE; + _state = stateDistTreeSingle; goto start; } _codesCount = bits; _codesReceived = 0; - _codesLengths = of_alloc_zeroed(bits, 1); + _codesLengths = OFAllocZeroedMemory(bits, 1); _treeIter = _codeLenTree; - _state = STATE_DIST_TREE; + _state = stateDistTree; goto start; - case STATE_DIST_TREE: + case stateDistTree: while (_codesReceived < _codesCount) { if OF_UNLIKELY (_currentIsExtendedLength) { if OF_UNLIKELY (!tryReadBits(self, &bits, 1)) return bytesWritten; @@ -377,34 +375,33 @@ continue; } else _codesReceived++; } - _distTree = of_huffman_tree_construct(_codesLengths, - _codesCount); - free(_codesLengths); + _distTree = OFHuffmanTreeNew(_codesLengths, _codesCount); + OFFreeMemory(_codesLengths); _codesLengths = NULL; _treeIter = _litLenTree; - _state = STATE_BLOCK_LITLEN; + _state = stateBlockLitLen; goto start; - case STATE_DIST_TREE_SINGLE: + case stateDistTreeSingle: if OF_UNLIKELY (!tryReadBits(self, &bits, _distanceBits)) return bytesWritten; - _distTree = of_huffman_tree_construct_single(bits); + _distTree = OFHuffmanTreeNewSingle(bits); _treeIter = _litLenTree; - _state = STATE_BLOCK_LITLEN; + _state = stateBlockLitLen; goto start; - case STATE_BLOCK_LITLEN: + case stateBlockLitLen: if OF_UNLIKELY (_symbolsLeft == 0) { - of_huffman_tree_release(_litLenTree); - of_huffman_tree_release(_distTree); + OFHuffmanTreeFree(_litLenTree); + OFHuffmanTreeFree(_distTree); _litLenTree = _distTree = NULL; - _state = STATE_BLOCK_HEADER; + _state = stateBlockHeader; /* * We must return here, as there is no indication * whether this was the last block. Whoever called this * method needs to check if everything has been read @@ -425,11 +422,11 @@ } if OF_UNLIKELY (length == 0) return bytesWritten; - if OF_UNLIKELY (!of_huffman_tree_walk(self, tryReadBits, + if OF_UNLIKELY (!OFHuffmanTreeWalk(self, tryReadBits, &_treeIter, &value)) return bytesWritten; if OF_LIKELY (value < 256) { buffer[bytesWritten++] = value; @@ -442,34 +439,33 @@ _symbolsLeft--; _treeIter = _litLenTree; } else { _length = value - 253; _treeIter = _distTree; - _state = STATE_BLOCK_DIST_LENGTH; + _state = stateBlockDistLength; } goto start; - case STATE_BLOCK_DIST_LENGTH: - if OF_UNLIKELY (!of_huffman_tree_walk(self, tryReadBits, + case stateBlockDistLength: + if OF_UNLIKELY (!OFHuffmanTreeWalk(self, tryReadBits, &_treeIter, &value)) return bytesWritten; _distance = value; _state = (value < 2 - ? STATE_BLOCK_LEN_DIST_PAIR - : STATE_BLOCK_DIST_LENGTH_EXTRA); + ? stateBlockLenDistPair : stateBlockDistLengthExtra); goto start; - case STATE_BLOCK_DIST_LENGTH_EXTRA: + case stateBlockDistLengthExtra: if OF_UNLIKELY (!tryReadBits(self, &bits, _distance - 1)) return bytesWritten; _distance = bits + (1u << (_distance - 1)); - _state = STATE_BLOCK_LEN_DIST_PAIR; + _state = stateBlockLenDistPair; goto start; - case STATE_BLOCK_LEN_DIST_PAIR: + case stateBlockLenDistPair: for (uint_fast16_t i = 0; i < _length; i++) { uint32_t idx; if OF_UNLIKELY (length == 0) { _length -= i; @@ -489,11 +485,11 @@ } _symbolsLeft--; _treeIter = _litLenTree; - _state = STATE_BLOCK_LITLEN; + _state = stateBlockLitLen; goto start; } OF_UNREACHABLE } @@ -502,11 +498,11 @@ { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; return (_stream.atEndOfStream && - _bufferLength - _bufferIndex == 0 && _state == STATE_BLOCK_HEADER); + _bufferLength - _bufferIndex == 0 && _state == stateBlockHeader); } - (int)fileDescriptorForReading { return ((id )_stream)