2018-01-21 15:58:12 +00:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-01-21 15:58:12 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-01-21 19:36:15 +00:00
|
|
|
|
// Filename : AppleRle.cs
|
2018-01-21 15:58:12 +00:00
|
|
|
|
// 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2018-12-29 17:34:38 +00:00
|
|
|
|
// Copyright © 2018-2019 David Ryskalczyk
|
2018-01-21 15:58:12 +00:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Compression;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Implements the Apple version of RLE</summary>
|
2024-05-01 05:36:13 +01:00
|
|
|
|
public static partial class AppleRle
|
2018-01-21 15:58:12 +00:00
|
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
|
const uint DART_CHUNK = 20960;
|
2023-10-03 22:51:02 +01:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public static bool IsSupported => true;
|
2021-11-04 23:28:44 +00:00
|
|
|
|
|
2024-05-01 05:36:13 +01:00
|
|
|
|
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
|
|
|
|
|
|
private static partial int AARU_apple_rle_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer,
|
|
|
|
|
|
int srcSize);
|
2021-11-04 17:55:37 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Decodes a buffer compressed with Apple RLE</summary>
|
|
|
|
|
|
/// <param name="source">Encoded buffer</param>
|
|
|
|
|
|
/// <param name="destination">Buffer where to write the decoded data</param>
|
|
|
|
|
|
/// <returns>The number of decoded bytes</returns>
|
|
|
|
|
|
public static int DecodeBuffer(byte[] source, byte[] destination)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Native.IsSupported)
|
|
|
|
|
|
return AARU_apple_rle_decode_buffer(destination, destination.Length, source, source.Length);
|
2021-11-04 17:55:37 +00:00
|
|
|
|
|
2023-10-03 22:51:02 +01:00
|
|
|
|
var count = 0;
|
|
|
|
|
|
var nextA = true; // true if A, false if B
|
2022-03-06 13:29:38 +00:00
|
|
|
|
byte repeatedByteA = 0, repeatedByteB = 0;
|
2023-10-03 22:51:02 +01:00
|
|
|
|
var repeatMode = false; // true if we're repeating, false if we're just copying
|
2022-03-06 13:29:38 +00:00
|
|
|
|
int inPosition = 0, outPosition = 0;
|
2021-10-17 01:10:23 +01:00
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
|
while(inPosition <= source.Length && outPosition <= destination.Length)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
|
|
|
|
|
switch(repeatMode)
|
2018-01-21 15:58:12 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case true when count > 0:
|
2018-01-21 15:58:12 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
count--;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(nextA)
|
|
|
|
|
|
{
|
|
|
|
|
|
nextA = false;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
destination[outPosition++] = repeatedByteA;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2021-10-17 01:10:23 +01:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
nextA = true;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
destination[outPosition++] = repeatedByteB;
|
2021-10-17 01:10:23 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
case false when count > 0:
|
|
|
|
|
|
count--;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
destination[outPosition++] = source[inPosition++];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(inPosition == source.Length) break;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
while(true)
|
|
|
|
|
|
{
|
2023-10-03 22:51:02 +01:00
|
|
|
|
byte b1 = source[inPosition++];
|
|
|
|
|
|
byte b2 = source[inPosition++];
|
|
|
|
|
|
var s = (short)(b1 << 8 | b2);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(s == 0 || s >= DART_CHUNK || s <= -DART_CHUNK) continue;
|
2021-10-17 01:10:23 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(s < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
repeatMode = true;
|
|
|
|
|
|
repeatedByteA = source[inPosition++];
|
|
|
|
|
|
repeatedByteB = source[inPosition++];
|
2023-10-03 22:51:02 +01:00
|
|
|
|
count = -s * 2 - 1;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
nextA = false;
|
2021-10-17 01:10:23 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
destination[outPosition++] = repeatedByteA;
|
2021-10-17 01:10:23 +01:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
repeatMode = false;
|
2023-10-03 22:51:02 +01:00
|
|
|
|
count = s * 2 - 1;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
destination[outPosition++] = source[inPosition++];
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-01-21 15:58:12 +00:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return outPosition;
|
2018-01-21 15:58:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|