Files
Aaru/Aaru.Compression/AppleRle.cs

124 lines
3.6 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2018-01-21 19:36:15 +00:00
// Filename : AppleRle.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2018-12-29 17:34:38 +00:00
// Copyright © 2018-2019 David Ryskalczyk
// ****************************************************************************/
using System.IO;
2020-02-27 00:33:26 +00:00
namespace Aaru.Compression
{
public class AppleRle
{
const uint DART_CHUNK = 20960;
2020-07-20 21:11:32 +01:00
readonly Stream _inStream;
int _count;
bool _nextA; // true if A, false if B
2020-07-20 21:11:32 +01:00
byte _repeatedByteA, _repeatedByteB;
bool _repeatMode; // true if we're repeating, false if we're just copying
public AppleRle(Stream stream)
{
2020-07-20 21:11:32 +01:00
_inStream = stream;
Reset();
}
void Reset()
{
2020-07-20 21:11:32 +01:00
_repeatedByteA = _repeatedByteB = 0;
_count = 0;
_nextA = true;
_repeatMode = false;
}
public int ProduceByte()
{
2020-07-20 21:11:32 +01:00
if(_repeatMode && _count > 0)
{
2020-07-20 21:11:32 +01:00
_count--;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_nextA)
{
2020-07-20 21:11:32 +01:00
_nextA = false;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _repeatedByteA;
}
2020-07-20 21:11:32 +01:00
_nextA = true;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _repeatedByteB;
}
2020-07-20 21:11:32 +01:00
if(!_repeatMode &&
_count > 0)
{
2020-07-20 21:11:32 +01:00
_count--;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _inStream.ReadByte();
}
2020-07-20 21:11:32 +01:00
if(_inStream.Position == _inStream.Length)
2020-02-29 18:03:35 +00:00
return -1;
while(true)
{
2020-07-20 21:11:32 +01:00
byte b1 = (byte)_inStream.ReadByte();
byte b2 = (byte)_inStream.ReadByte();
short s = (short)((b1 << 8) | b2);
2020-02-29 18:03:35 +00:00
if(s == 0 ||
s >= DART_CHUNK ||
s <= -DART_CHUNK)
continue;
if(s < 0)
{
2020-07-20 21:11:32 +01:00
_repeatMode = true;
_repeatedByteA = (byte)_inStream.ReadByte();
_repeatedByteB = (byte)_inStream.ReadByte();
_count = (-s * 2) - 1;
_nextA = false;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _repeatedByteA;
}
2020-02-29 18:03:35 +00:00
if(s <= 0)
continue;
2020-07-20 21:11:32 +01:00
_repeatMode = false;
_count = (s * 2) - 1;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _inStream.ReadByte();
}
}
}
}