-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for Apple DART's RLE, corrects comments and copyright messages. #183
Open
claunia
wants to merge
5
commits into
adamhathcock:master
Choose a base branch
from
claunia:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a420299
Added decompressor for Apple DART's RLE algorithm.
claunia 9e4dbc4
Corrected comments on ADC algorithm.
claunia 8f02446
Corrected copyright messages.
claunia 4e7ef54
Added ADC and DART's RLE to formats list.
claunia 6a1ba48
Update FORMATS.md
claunia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
// | ||
// RLEStream.cs | ||
// | ||
// Author: | ||
// Natalia Portillo <claunia@claunia.com> | ||
// | ||
// Copyright (c) 2016 Natalia Portillo | ||
// Based on libdc42.c (c) 1998-2010 Ray A. Arachelian | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
using System; | ||
using System.IO; | ||
|
||
namespace SharpCompress.Compressors.DART | ||
{ | ||
/// <summary> | ||
/// This class handles the RLE algorithm used by Apple's DART. | ||
/// Compression should be easy, but it's not implemented. | ||
/// Basically it's simply a big-endian 16-bit run-length encoding. | ||
/// </summary> | ||
public class RLEStream : Stream | ||
{ | ||
/// <summary> | ||
/// This stream holds the compressed data | ||
/// </summary> | ||
private readonly Stream stream; | ||
|
||
/// <summary> | ||
/// Is this instance disposed? | ||
/// </summary> | ||
private bool isDisposed; | ||
|
||
/// <summary> | ||
/// Position in decompressed data | ||
/// </summary> | ||
private long position; | ||
|
||
/// <summary> | ||
/// Buffer with currently used chunk of decompressed data | ||
/// </summary> | ||
private byte[] cmpBuffer; | ||
|
||
/// <summary> | ||
/// Position in buffer of decompressed data | ||
/// </summary> | ||
private int bufferPosition; | ||
|
||
/// <summary> | ||
/// Initializates a stream that decompresses ADC data on the fly | ||
/// </summary> | ||
/// <param name="stream">Stream that contains the compressed data</param> | ||
/// <param name="compressionMode">Must be set to <see cref="CompressionMode.Decompress"/> because compression is not implemented</param> | ||
public RLEStream(Stream stream, CompressionMode compressionMode = CompressionMode.Decompress) | ||
{ | ||
if (compressionMode == CompressionMode.Compress) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
this.stream = stream; | ||
} | ||
|
||
public override bool CanRead | ||
{ | ||
get | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
public override bool CanSeek | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public override bool CanWrite | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public override long Length | ||
{ | ||
get | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
|
||
public override long Position | ||
{ | ||
get | ||
{ | ||
return position; | ||
} | ||
|
||
set | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
|
||
public override void Flush() | ||
{ | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (isDisposed) | ||
{ | ||
return; | ||
} | ||
isDisposed = true; | ||
base.Dispose(disposing); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
if (count == 0) | ||
{ | ||
return 0; | ||
} | ||
if (buffer == null) | ||
{ | ||
throw new ArgumentNullException(nameof(buffer)); | ||
} | ||
if (count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
if (offset < buffer.GetLowerBound(0)) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(offset)); | ||
} | ||
if ((offset + count) > buffer.GetLength(0)) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
|
||
int copied = 0; | ||
|
||
fillBuffer: | ||
if (cmpBuffer == null || bufferPosition == cmpBuffer.Length) | ||
{ | ||
if (stream.Position + 4 >= stream.Length) | ||
return copied; | ||
|
||
byte c1 = (byte)stream.ReadByte(); | ||
byte c2 = (byte)stream.ReadByte(); | ||
short size = (short)((c1 << 8) | c2); | ||
|
||
if (size == 0) | ||
{ | ||
goto fillBuffer; | ||
} | ||
else if (size < 0) | ||
{ | ||
size *= -1; | ||
cmpBuffer = new byte[size * 2]; | ||
byte w1 = (byte)stream.ReadByte(); | ||
byte w2 = (byte)stream.ReadByte(); | ||
for (int i = 0; i < cmpBuffer.Length; i += 2) | ||
{ | ||
cmpBuffer[i] = w1; | ||
cmpBuffer[i + 1] = w2; | ||
} | ||
} | ||
else | ||
{ | ||
cmpBuffer = new byte[size * 2]; | ||
stream.Read(cmpBuffer, 0, cmpBuffer.Length); | ||
} | ||
|
||
bufferPosition = 0; | ||
} | ||
|
||
if (bufferPosition + count <= cmpBuffer.Length) | ||
{ | ||
Array.Copy(cmpBuffer, bufferPosition, buffer, offset, count); | ||
bufferPosition += count; | ||
copied += count; | ||
} | ||
else | ||
{ | ||
int rest = cmpBuffer.Length - bufferPosition; | ||
Array.Copy(cmpBuffer, bufferPosition, buffer, offset, rest); | ||
count -= rest; | ||
offset += rest; | ||
copied += rest; | ||
cmpBuffer = null; | ||
goto fillBuffer; | ||
} | ||
|
||
position += copied; | ||
|
||
return copied; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// DARTTest.cs | ||
// | ||
// Author: | ||
// Natalia Portillo <claunia@claunia.com> | ||
// | ||
// Copyright (c) 2016 Natalia Portillo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
using SharpCompress.Compressors; | ||
using SharpCompress.Compressors.DART; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace SharpCompress.Test | ||
{ | ||
public class DARTTest : TestBase | ||
{ | ||
[Fact] | ||
public void TestRLEStream() | ||
{ | ||
using (FileStream decFs = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "dart_rle_decompressed.bin"))) | ||
{ | ||
byte[] decompressed = new byte[decFs.Length]; | ||
decFs.Read(decompressed, 0, decompressed.Length); | ||
|
||
using (FileStream cmpFs = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "dart_rle_compressed.bin"))) | ||
{ | ||
using (RLEStream decStream = new RLEStream(cmpFs, CompressionMode.Decompress)) | ||
{ | ||
byte[] test = new byte[419200]; | ||
|
||
decStream.Read(test, 0, test.Length); | ||
|
||
Assert.Equal(decompressed, test); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway you can do this without the
goto
and label? Just bad C# style.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mayyyyyyybe, I need to test.
And while most C# people tend to ignore gotos, they're used extensively in .NET frameworks and the IL optimizes them easy.
But if you prefer no goto I'll try to remove it, don't worry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a big deal. I may refactor later if you don't want to :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry I'll do it myself this week and with some luck I add compression.