mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
🐛Implement Apple RLE algorithm, fix #120, thanks to David Ryskalczyk.
This commit is contained in:
1
.idea/.idea.DiscImageChef/.idea/contentModel.xml
generated
1
.idea/.idea.DiscImageChef/.idea/contentModel.xml
generated
@@ -85,6 +85,7 @@
|
|||||||
<e p="packages.config" t="Include" />
|
<e p="packages.config" t="Include" />
|
||||||
</e>
|
</e>
|
||||||
<e p="DiscImageChef.Compression" t="IncludeRecursive">
|
<e p="DiscImageChef.Compression" t="IncludeRecursive">
|
||||||
|
<e p="AppleRle.cs" t="Include" />
|
||||||
<e p="DiscImageChef.Compression.csproj" t="IncludeRecursive" />
|
<e p="DiscImageChef.Compression.csproj" t="IncludeRecursive" />
|
||||||
<e p="Properties" t="Include">
|
<e p="Properties" t="Include">
|
||||||
<e p="AssemblyInfo.cs" t="Include" />
|
<e p="AssemblyInfo.cs" t="Include" />
|
||||||
|
|||||||
112
DiscImageChef.Compression/AppleRle.cs
Normal file
112
DiscImageChef.Compression/AppleRle.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
// /***************************************************************************
|
||||||
|
// The Disc Image Chef
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Filename : TeleDiskLzh.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/>.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Copyright © 2011-2018 Natalia Portillo
|
||||||
|
// Copyright © 2018 David Ryskalczyk
|
||||||
|
// ****************************************************************************/
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace DiscImageChef.Compression
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
public AppleRle(Stream stream)
|
||||||
|
{
|
||||||
|
inStream = stream;
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reset()
|
||||||
|
{
|
||||||
|
repeatedbyteA = repeatedbyteB = 0;
|
||||||
|
count = 0;
|
||||||
|
nextA = true;
|
||||||
|
repeatMode = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="AppleRle.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="TeleDiskLzh.cs" />
|
<Compile Include="TeleDiskLzh.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ using System.Text;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Claunia.RsrcFork;
|
using Claunia.RsrcFork;
|
||||||
using DiscImageChef.CommonTypes;
|
using DiscImageChef.CommonTypes;
|
||||||
|
using DiscImageChef.Compression;
|
||||||
using DiscImageChef.Console;
|
using DiscImageChef.Console;
|
||||||
using DiscImageChef.Filters;
|
using DiscImageChef.Filters;
|
||||||
using Version = Resources.Version;
|
using Version = Resources.Version;
|
||||||
@@ -267,12 +268,18 @@ namespace DiscImageChef.DiscImages
|
|||||||
{
|
{
|
||||||
temp = new byte[l * 2];
|
temp = new byte[l * 2];
|
||||||
stream.Read(temp, 0, temp.Length);
|
stream.Read(temp, 0, temp.Length);
|
||||||
throw new ImageNotSupportedException("Compressed images not yet supported");
|
AppleRle rle = new AppleRle(new MemoryStream(temp));
|
||||||
|
buffer = new byte[BUFFER_SIZE];
|
||||||
|
for(int i = 0; i < BUFFER_SIZE; i++) buffer[i] = (byte)rle.ProduceByte();
|
||||||
|
dataMs.Write(buffer, 0, DATA_SIZE);
|
||||||
|
tagMs.Write(buffer, DATA_SIZE, TAG_SIZE);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
temp = new byte[l];
|
temp = new byte[l];
|
||||||
stream.Read(temp, 0, temp.Length);
|
stream.Read(temp, 0, temp.Length);
|
||||||
throw new ImageNotSupportedException("Compressed images not yet supported");
|
throw new ImageNotSupportedException("LZH Compressed images not yet supported");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,6 +368,7 @@ namespace DiscImageChef.DiscImages
|
|||||||
byte[] tagChk = cksmRsrc.GetResource(1);
|
byte[] tagChk = cksmRsrc.GetResource(1);
|
||||||
tagChecksum = BigEndianBitConverter.ToUInt32(tagChk, 0);
|
tagChecksum = BigEndianBitConverter.ToUInt32(tagChk, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cksmRsrc?.ContainsId(2) == true)
|
if(cksmRsrc?.ContainsId(2) == true)
|
||||||
{
|
{
|
||||||
byte[] dataChk = cksmRsrc.GetResource(1);
|
byte[] dataChk = cksmRsrc.GetResource(1);
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ using System.Runtime.InteropServices;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Claunia.RsrcFork;
|
using Claunia.RsrcFork;
|
||||||
using DiscImageChef.CommonTypes;
|
using DiscImageChef.CommonTypes;
|
||||||
|
using DiscImageChef.Compression;
|
||||||
using DiscImageChef.Console;
|
using DiscImageChef.Console;
|
||||||
using DiscImageChef.Filters;
|
using DiscImageChef.Filters;
|
||||||
using SharpCompress.Compressors.ADC;
|
using SharpCompress.Compressors.ADC;
|
||||||
@@ -236,8 +237,6 @@ namespace DiscImageChef.DiscImages
|
|||||||
case CHUNK_TYPE_KENCODE:
|
case CHUNK_TYPE_KENCODE:
|
||||||
throw new
|
throw new
|
||||||
ImageNotSupportedException("Chunks compressed with KenCode are not yet supported.");
|
ImageNotSupportedException("Chunks compressed with KenCode are not yet supported.");
|
||||||
case CHUNK_TYPE_RLE:
|
|
||||||
throw new ImageNotSupportedException("Chunks compressed with RLE are not yet supported.");
|
|
||||||
case CHUNK_TYPE_LZH:
|
case CHUNK_TYPE_LZH:
|
||||||
throw new ImageNotSupportedException("Chunks compressed with LZH are not yet supported.");
|
throw new ImageNotSupportedException("Chunks compressed with LZH are not yet supported.");
|
||||||
case CHUNK_TYPE_STUFFIT:
|
case CHUNK_TYPE_STUFFIT:
|
||||||
@@ -411,15 +410,41 @@ namespace DiscImageChef.DiscImages
|
|||||||
imageStream.Seek(currentChunk.offset, SeekOrigin.Begin);
|
imageStream.Seek(currentChunk.offset, SeekOrigin.Begin);
|
||||||
imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
|
imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
|
||||||
MemoryStream cmpMs = new MemoryStream(cmpBuffer);
|
MemoryStream cmpMs = new MemoryStream(cmpBuffer);
|
||||||
Stream decStream;
|
int realSize = 0;
|
||||||
|
|
||||||
if(currentChunk.type == CHUNK_TYPE_ADC) decStream = new ADCStream(cmpMs);
|
|
||||||
else throw new ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
|
||||||
|
|
||||||
|
switch(currentChunk.type)
|
||||||
|
{
|
||||||
|
case CHUNK_TYPE_ADC:
|
||||||
|
{
|
||||||
|
Stream decStream = new ADCStream(cmpMs);
|
||||||
byte[] tmpBuffer = new byte[buffersize];
|
byte[] tmpBuffer = new byte[buffersize];
|
||||||
int realSize = decStream.Read(tmpBuffer, 0, (int)buffersize);
|
realSize = decStream.Read(tmpBuffer, 0, (int)buffersize);
|
||||||
buffer = new byte[realSize];
|
buffer = new byte[realSize];
|
||||||
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CHUNK_TYPE_RLE:
|
||||||
|
{
|
||||||
|
byte[] tmpBuffer = new byte[buffersize];
|
||||||
|
realSize = 0;
|
||||||
|
AppleRle rle = new AppleRle(cmpMs);
|
||||||
|
for(int i = 0; i < buffersize; i++)
|
||||||
|
{
|
||||||
|
int b = rle.ProduceByte();
|
||||||
|
if(b == -1) break;
|
||||||
|
|
||||||
|
tmpBuffer[i] = (byte)b;
|
||||||
|
realSize++;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = new byte[realSize];
|
||||||
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw new
|
||||||
|
ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
||||||
|
}
|
||||||
|
|
||||||
if(currentChunkCacheSize + realSize > MAX_CACHE_SIZE)
|
if(currentChunkCacheSize + realSize > MAX_CACHE_SIZE)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ using Claunia.PropertyList;
|
|||||||
using Claunia.RsrcFork;
|
using Claunia.RsrcFork;
|
||||||
using DiscImageChef.Checksums;
|
using DiscImageChef.Checksums;
|
||||||
using DiscImageChef.CommonTypes;
|
using DiscImageChef.CommonTypes;
|
||||||
|
using DiscImageChef.Compression;
|
||||||
using DiscImageChef.Console;
|
using DiscImageChef.Console;
|
||||||
using DiscImageChef.Filters;
|
using DiscImageChef.Filters;
|
||||||
using Ionic.Zlib;
|
using Ionic.Zlib;
|
||||||
@@ -508,7 +509,7 @@ namespace DiscImageChef.DiscImages
|
|||||||
imageStream.Seek((long)currentChunk.offset, SeekOrigin.Begin);
|
imageStream.Seek((long)currentChunk.offset, SeekOrigin.Begin);
|
||||||
imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
|
imageStream.Read(cmpBuffer, 0, cmpBuffer.Length);
|
||||||
MemoryStream cmpMs = new MemoryStream(cmpBuffer);
|
MemoryStream cmpMs = new MemoryStream(cmpBuffer);
|
||||||
Stream decStream;
|
Stream decStream = null;
|
||||||
|
|
||||||
switch(currentChunk.type)
|
switch(currentChunk.type)
|
||||||
{
|
{
|
||||||
@@ -521,6 +522,7 @@ namespace DiscImageChef.DiscImages
|
|||||||
case CHUNK_TYPE_BZIP:
|
case CHUNK_TYPE_BZIP:
|
||||||
decStream = new BZip2Stream(cmpMs, SharpCompress.Compressors.CompressionMode.Decompress);
|
decStream = new BZip2Stream(cmpMs, SharpCompress.Compressors.CompressionMode.Decompress);
|
||||||
break;
|
break;
|
||||||
|
case CHUNK_TYPE_RLE: break;
|
||||||
default:
|
default:
|
||||||
throw new
|
throw new
|
||||||
ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
ImageNotSupportedException($"Unsupported chunk type 0x{currentChunk.type:X8} found");
|
||||||
@@ -530,8 +532,15 @@ namespace DiscImageChef.DiscImages
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
byte[] tmpBuffer = new byte[buffersize];
|
byte[] tmpBuffer;
|
||||||
int realSize = decStream.Read(tmpBuffer, 0, (int)buffersize);
|
int realSize;
|
||||||
|
switch(currentChunk.type)
|
||||||
|
{
|
||||||
|
case CHUNK_TYPE_ADC:
|
||||||
|
case CHUNK_TYPE_ZLIB:
|
||||||
|
case CHUNK_TYPE_BZIP:
|
||||||
|
tmpBuffer = new byte[buffersize];
|
||||||
|
realSize = decStream.Read(tmpBuffer, 0, (int)buffersize);
|
||||||
buffer = new byte[realSize];
|
buffer = new byte[realSize];
|
||||||
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
||||||
|
|
||||||
@@ -543,6 +552,24 @@ namespace DiscImageChef.DiscImages
|
|||||||
|
|
||||||
chunkCache.Add(chunkStartSector, buffer);
|
chunkCache.Add(chunkStartSector, buffer);
|
||||||
currentChunkCacheSize += (uint)realSize;
|
currentChunkCacheSize += (uint)realSize;
|
||||||
|
break;
|
||||||
|
case CHUNK_TYPE_RLE:
|
||||||
|
tmpBuffer = new byte[buffersize];
|
||||||
|
realSize = 0;
|
||||||
|
AppleRle rle = new AppleRle(cmpMs);
|
||||||
|
for(int i = 0; i < buffersize; i++)
|
||||||
|
{
|
||||||
|
int b = rle.ProduceByte();
|
||||||
|
if(b == -1) break;
|
||||||
|
|
||||||
|
tmpBuffer[i] = (byte)b;
|
||||||
|
realSize++;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = new byte[realSize];
|
||||||
|
Array.Copy(tmpBuffer, 0, buffer, 0, realSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
}
|
}
|
||||||
catch(ZlibException)
|
catch(ZlibException)
|
||||||
|
|||||||
Reference in New Issue
Block a user