// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : AppleRle.cs // Author(s) : Natalia Portillo // // Component : Compression algorithms. // // --[ Description ] ---------------------------------------------------------- // // Decompress Apple variant of RLE. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2021 Natalia Portillo // Copyright © 2018-2019 David Ryskalczyk // ****************************************************************************/ using System.IO; namespace Aaru.Compression { /// /// Implements the Apple version of RLE /// public class AppleRle { const uint DART_CHUNK = 20960; readonly Stream _inStream; int _count; bool _nextA; // true if A, false if B byte _repeatedByteA, _repeatedByteB; bool _repeatMode; // true if we're repeating, false if we're just copying /// /// Initializes a decompressor for the specified stream /// /// Stream containing the compressed data public AppleRle(Stream stream) { _inStream = stream; Reset(); } void Reset() { _repeatedByteA = _repeatedByteB = 0; _count = 0; _nextA = true; _repeatMode = false; } /// /// Decompresses a byte /// /// Decompressed byte public int ProduceByte() { if(_repeatMode && _count > 0) { _count--; if(_nextA) { _nextA = false; return _repeatedByteA; } _nextA = true; return _repeatedByteB; } if(!_repeatMode && _count > 0) { _count--; return _inStream.ReadByte(); } if(_inStream.Position == _inStream.Length) return -1; while(true) { byte b1 = (byte)_inStream.ReadByte(); byte b2 = (byte)_inStream.ReadByte(); short s = (short)((b1 << 8) | b2); if(s == 0 || s >= DART_CHUNK || s <= -DART_CHUNK) continue; if(s < 0) { _repeatMode = true; _repeatedByteA = (byte)_inStream.ReadByte(); _repeatedByteB = (byte)_inStream.ReadByte(); _count = (-s * 2) - 1; _nextA = false; return _repeatedByteA; } if(s <= 0) continue; _repeatMode = false; _count = (s * 2) - 1; return _inStream.ReadByte(); } } } }