Files
UnifiedFlashingPlatform/DecompressedStream.cs
T

135 lines
5.3 KiB
C#
Raw Normal View History

2024-03-04 21:50:33 +01:00
// Copyright (c) 2018, Rene Lergner - @Heathcliff74xda
//
// 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.Collections;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace UnifiedFlashingPlatform
{
// For reading a compressed stream or normal stream
internal class DecompressedStream : Stream
{
private readonly Stream UnderlyingStream;
private readonly bool IsSourceCompressed;
2024-03-06 23:05:17 +01:00
private readonly ulong DecompressedLength;
private long ReadPosition = 0;
2024-03-04 21:50:33 +01:00
// For reading a compressed stream
internal DecompressedStream(Stream InputStream)
{
UnderlyingStream = new ReadSeekableStream(InputStream, 0x100);
byte[] Signature = new byte["CompressedPartition".Length + 2];
Signature[0x00] = 0xFF;
Buffer.BlockCopy(Encoding.ASCII.GetBytes("CompressedPartition"), 0, Signature, 0x01, "CompressedPartition".Length);
Signature["CompressedPartition".Length + 1] = 0x00;
2024-03-06 23:05:17 +01:00
_ = 0x0A + "CompressedPartition".Length;
2024-03-04 21:50:33 +01:00
byte[] SignatureRead = new byte[Signature.Length];
2024-03-06 23:05:17 +01:00
_ = UnderlyingStream.Read(SignatureRead, 0, Signature.Length);
2024-03-04 21:50:33 +01:00
IsSourceCompressed = StructuralComparisons.StructuralEqualityComparer.Equals(Signature, SignatureRead);
if (IsSourceCompressed)
{
byte[] FormatVersionBytes = new byte[4];
2024-03-06 23:05:17 +01:00
_ = UnderlyingStream.Read(FormatVersionBytes, 0, 4);
2024-03-04 21:50:33 +01:00
if (BitConverter.ToUInt32(FormatVersionBytes, 0) > 1) // Max supported format version = 1
{
throw new InvalidDataException();
}
byte[] HeaderSizeBytes = new byte[4];
2024-03-06 23:05:17 +01:00
_ = UnderlyingStream.Read(HeaderSizeBytes, 0, 4);
uint HeaderSize = BitConverter.ToUInt32(HeaderSizeBytes, 0);
2024-03-04 21:50:33 +01:00
if (HeaderSize >= (Signature.Length + 0x10))
{
byte[] DecompressedLengthBytes = new byte[8];
2024-03-06 23:05:17 +01:00
_ = UnderlyingStream.Read(DecompressedLengthBytes, 0, 8);
2024-03-04 21:50:33 +01:00
DecompressedLength = BitConverter.ToUInt64(DecompressedLengthBytes, 0);
}
else
{
throw new InvalidDataException();
}
2024-03-06 23:05:17 +01:00
uint HeaderBytesRemaining = (uint)(HeaderSize - Signature.Length - 0x10);
2024-03-04 21:50:33 +01:00
if (HeaderBytesRemaining > 0)
{
byte[] HeaderBytes = new byte[HeaderBytesRemaining];
2024-03-06 23:05:17 +01:00
_ = UnderlyingStream.Read(HeaderBytes, 0, (int)HeaderBytesRemaining);
2024-03-04 21:50:33 +01:00
}
UnderlyingStream = new GZipStream(UnderlyingStream, CompressionMode.Decompress, false);
}
else
{
UnderlyingStream.Position = 0;
}
}
2024-03-06 23:05:17 +01:00
public override bool CanRead => true;
public override bool CanSeek => false;
2024-03-04 21:50:33 +01:00
public override int Read(byte[] buffer, int offset, int count)
{
int RealCount = UnderlyingStream.Read(buffer, offset, count);
ReadPosition += RealCount;
return RealCount;
}
2024-03-06 23:05:17 +01:00
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
2024-03-04 21:50:33 +01:00
public override long Position
{
2024-03-06 23:05:17 +01:00
get => ReadPosition;
set => throw new NotSupportedException();
2024-03-04 21:50:33 +01:00
}
2024-03-06 23:05:17 +01:00
public override bool CanTimeout => UnderlyingStream.CanTimeout;
public override bool CanWrite => true;
public override long Length => IsSourceCompressed ? (long)DecompressedLength : UnderlyingStream.Length;
public override void SetLength(long value)
2024-03-04 21:50:33 +01:00
{
2024-03-06 23:05:17 +01:00
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Flush()
{
UnderlyingStream.Flush();
}
public override void Close()
{
UnderlyingStream.Close();
2024-03-04 21:50:33 +01:00
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
2024-03-06 23:05:17 +01:00
UnderlyingStream.Dispose();
2024-03-04 21:50:33 +01:00
}
}
}
}