mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 06d2d11922aa844dab91c857e540a6517eb88d45 Former-commit-id: 4aa397beff436e9fe6adb5640bad69230558cba8 Former-commit-id: 5ba4872f56b4db55b8c126ec9cefe5df19b28922af/merge-core
26 changed files with 55 additions and 6117 deletions
@ -1,31 +0,0 @@ |
|||
// <copyright file="DeflateStrategy.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Strategies for deflater
|
|||
/// </summary>
|
|||
public enum DeflateStrategy |
|||
{ |
|||
/// <summary>
|
|||
/// The default strategy
|
|||
/// </summary>
|
|||
Default = 0, |
|||
|
|||
/// <summary>
|
|||
/// This strategy will only allow longer string repetitions. It is
|
|||
/// useful for random data with a small character set.
|
|||
/// </summary>
|
|||
Filtered = 1, |
|||
|
|||
/// <summary>
|
|||
/// This strategy will not look for string repetitions at all. It
|
|||
/// only encodes with Huffman trees (which means, that more common
|
|||
/// characters get a smaller encoding.
|
|||
/// </summary>
|
|||
HuffmanOnly = 2 |
|||
} |
|||
} |
|||
@ -1,561 +0,0 @@ |
|||
// <copyright file="Deflater.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// This is the Deflater class. The deflater class compresses input
|
|||
/// with the deflate algorithm described in RFC 1951. It has several
|
|||
/// compression levels and three different strategies described below.
|
|||
///
|
|||
/// This class is <i>not</i> thread safe. This is inherent in the API, due
|
|||
/// to the split of deflate and setInput.
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class Deflater |
|||
{ |
|||
/* |
|||
* The Deflater can do the following state transitions: |
|||
* |
|||
* (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---. |
|||
* / | (2) (5) | |
|||
* / v (5) | |
|||
* (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3) |
|||
* \ | (3) | ,--------' |
|||
* | | | (3) / |
|||
* v v (5) v v |
|||
* (1) -> BUSY_STATE ----> FINISHING_STATE |
|||
* | (6) |
|||
* v |
|||
* FINISHED_STATE |
|||
* \_____________________________________/ |
|||
* | (7) |
|||
* v |
|||
* CLOSED_STATE |
|||
* |
|||
* (1) If we should produce a header we start in INIT_STATE, otherwise |
|||
* we start in BUSY_STATE. |
|||
* (2) A dictionary may be set only when we are in INIT_STATE, then |
|||
* we change the state as indicated. |
|||
* (3) Whether a dictionary is set or not, on the first call of deflate |
|||
* we change to BUSY_STATE. |
|||
* (4) -- intentionally left blank -- :) |
|||
* (5) FINISHING_STATE is entered, when flush() is called to indicate that |
|||
* there is no more INPUT. There are also states indicating, that |
|||
* the header wasn't written yet. |
|||
* (6) FINISHED_STATE is entered, when everything has been flushed to the |
|||
* internal pending output buffer. |
|||
* (7) At any time (7) |
|||
* |
|||
*/ |
|||
|
|||
/// <summary>
|
|||
/// The best and slowest compression level. This tries to find very
|
|||
/// long and distant string repetitions.
|
|||
/// </summary>
|
|||
public const int BestCompression = 9; |
|||
|
|||
/// <summary>
|
|||
/// The worst but fastest compression level.
|
|||
/// </summary>
|
|||
public const int BestSpeed = 1; |
|||
|
|||
/// <summary>
|
|||
/// The default compression level.
|
|||
/// </summary>
|
|||
public const int DefaultCompression = -1; |
|||
|
|||
/// <summary>
|
|||
/// This level won't compress at all but output uncompressed blocks.
|
|||
/// </summary>
|
|||
public const int NoCompression = 0; |
|||
|
|||
/// <summary>
|
|||
/// The compression method. This is the only method supported so far.
|
|||
/// There is no need to use this constant at all.
|
|||
/// </summary>
|
|||
public const int Deflated = 8; |
|||
|
|||
/// <summary>
|
|||
/// The is dictionary set flag.
|
|||
/// </summary>
|
|||
private const int IsSetdict = 0x01; |
|||
|
|||
/// <summary>
|
|||
/// Flags whether flushing.
|
|||
/// </summary>
|
|||
private const int IsFlushing = 0x04; |
|||
|
|||
/// <summary>
|
|||
/// Flags whether finishing.
|
|||
/// </summary>
|
|||
private const int IsFinishing = 0x08; |
|||
|
|||
/// <summary>
|
|||
/// The initial stat flag
|
|||
/// </summary>
|
|||
private const int InitState = 0x00; |
|||
|
|||
/// <summary>
|
|||
/// Flags setting the dictionary.
|
|||
/// </summary>
|
|||
private const int SetdictState = 0x01; |
|||
|
|||
/// <summary>
|
|||
/// The busy state flag.
|
|||
/// </summary>
|
|||
private const int BusyState = 0x10; |
|||
|
|||
/// <summary>
|
|||
/// The flushing state flag.
|
|||
/// </summary>
|
|||
private const int FlushingState = 0x14; |
|||
|
|||
/// <summary>
|
|||
/// The finishing state flag.
|
|||
/// </summary>
|
|||
private const int FinishingState = 0x1c; |
|||
|
|||
/// <summary>
|
|||
/// The finished state flag.
|
|||
/// </summary>
|
|||
private const int FinishedState = 0x1e; |
|||
|
|||
/// <summary>
|
|||
/// The closed state flag.
|
|||
/// </summary>
|
|||
private const int ClosedState = 0x7f; |
|||
|
|||
/// <summary>
|
|||
/// The pending output.
|
|||
/// </summary>
|
|||
private readonly DeflaterPending pending; |
|||
|
|||
/// <summary>
|
|||
/// If true no Zlib/RFC1950 headers or footers are generated
|
|||
/// </summary>
|
|||
private readonly bool noZlibHeaderOrFooter; |
|||
|
|||
/// <summary>
|
|||
/// The deflater engine.
|
|||
/// </summary>
|
|||
private readonly DeflaterEngine engine; |
|||
|
|||
/// <summary>
|
|||
/// Compression level.
|
|||
/// </summary>
|
|||
private int deflaterLevel; |
|||
|
|||
/// <summary>
|
|||
/// The current state.
|
|||
/// </summary>
|
|||
private int state; |
|||
|
|||
/// <summary>
|
|||
/// The total bytes of output written.
|
|||
/// </summary>
|
|||
private long totalOut; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Deflater"/> class with the default compression level.
|
|||
/// </summary>
|
|||
public Deflater() |
|||
: this(DefaultCompression, false) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Deflater"/> class with the given compressin level.
|
|||
/// </summary>
|
|||
/// <param name="level">
|
|||
/// The compression level, a value between NoCompression and BestCompression, or DefaultCompression.
|
|||
/// </param>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">If level is out of range.</exception>
|
|||
public Deflater(int level) |
|||
: this(level, false) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Deflater"/> class with the given compressin level.
|
|||
/// </summary>
|
|||
/// <param name="level">
|
|||
/// The compression level, a value between NoCompression and BestCompression, or DefaultCompression.
|
|||
/// </param>
|
|||
/// <param name="noZlibHeaderOrFooter">
|
|||
/// True, if we should suppress the Zlib/RFC1950 header at the
|
|||
/// beginning and the adler checksum at the end of the output. This is
|
|||
/// useful for the GZIP/PKZIP formats.
|
|||
/// </param>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
|
|||
public Deflater(int level, bool noZlibHeaderOrFooter) |
|||
{ |
|||
if (level == DefaultCompression) |
|||
{ |
|||
level = 6; |
|||
} |
|||
else if (level < NoCompression || level > BestCompression) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(level)); |
|||
} |
|||
|
|||
this.pending = new DeflaterPending(); |
|||
this.engine = new DeflaterEngine(this.pending); |
|||
this.noZlibHeaderOrFooter = noZlibHeaderOrFooter; |
|||
this.SetStrategy(DeflateStrategy.Default); |
|||
this.SetLevel(level); |
|||
this.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the current adler checksum of the data that was processed so far.
|
|||
/// </summary>
|
|||
public int Adler => this.engine.Adler; |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of input bytes processed so far.
|
|||
/// </summary>
|
|||
public long TotalIn => this.engine.TotalIn; |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of output bytes so far.
|
|||
/// </summary>
|
|||
public long TotalOut => this.totalOut; |
|||
|
|||
/// <summary>
|
|||
/// Returns true if the stream was finished and no more output bytes
|
|||
/// are available.
|
|||
/// </summary>
|
|||
public bool IsFinished => (this.state == FinishedState) && this.pending.IsFlushed; |
|||
|
|||
/// <summary>
|
|||
/// Returns true, if the input buffer is empty.
|
|||
/// You should then call setInput().
|
|||
/// NOTE: This method can also return true when the stream
|
|||
/// was finished.
|
|||
/// </summary>
|
|||
public bool IsNeedingInput => this.engine.NeedsInput(); |
|||
|
|||
/// <summary>
|
|||
/// Resets the deflater. The deflater acts afterwards as if it was
|
|||
/// just created with the same compression level and strategy as it
|
|||
/// had before.
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
this.state = this.noZlibHeaderOrFooter ? BusyState : InitState; |
|||
this.totalOut = 0; |
|||
this.pending.Reset(); |
|||
this.engine.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the current input block. Further calls to deflate() will
|
|||
/// produce enough output to inflate everything in the current input
|
|||
/// block. This is not part of Sun's JDK so I have made it package
|
|||
/// private. It is used by DeflaterOutputStream to implement
|
|||
/// flush().
|
|||
/// </summary>
|
|||
public void Flush() |
|||
{ |
|||
this.state |= IsFlushing; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Finishes the deflater with the current input block. It is an error
|
|||
/// to give more input after this method was called. This method must
|
|||
/// be called to force all bytes to be flushed.
|
|||
/// </summary>
|
|||
public void Finish() |
|||
{ |
|||
this.state |= IsFlushing | IsFinishing; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the data which should be compressed next. This should be only
|
|||
/// called when needsInput indicates that more input is needed.
|
|||
/// If you call setInput when needsInput() returns false, the
|
|||
/// previous input that is still pending will be thrown away.
|
|||
/// The given byte array should not be changed, before needsInput() returns
|
|||
/// true again.
|
|||
/// This call is equivalent to <code>setInput(input, 0, input.length)</code>.
|
|||
/// </summary>
|
|||
/// <param name="input">
|
|||
/// the buffer containing the input data.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// if the buffer was finished() or ended().
|
|||
/// </exception>
|
|||
public void SetInput(byte[] input) |
|||
{ |
|||
this.SetInput(input, 0, input.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the data which should be compressed next. This should be
|
|||
/// only called when needsInput indicates that more input is needed.
|
|||
/// The given byte array should not be changed, before needsInput() returns
|
|||
/// true again.
|
|||
/// </summary>
|
|||
/// <param name="input">
|
|||
/// the buffer containing the input data.
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// the start of the data.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// the number of data bytes of input.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// if the buffer was Finish()ed or if previous input is still pending.
|
|||
/// </exception>
|
|||
public void SetInput(byte[] input, int offset, int count) |
|||
{ |
|||
if ((this.state & IsFinishing) != 0) |
|||
{ |
|||
throw new InvalidOperationException("Finish() already called"); |
|||
} |
|||
|
|||
this.engine.SetInput(input, offset, count); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the compression level. There is no guarantee of the exact
|
|||
/// position of the change, but if you call this when needsInput is
|
|||
/// true the change of compression level will occur somewhere near
|
|||
/// before the end of the so far given input.
|
|||
/// </summary>
|
|||
/// <param name="level">
|
|||
/// the new compression level.
|
|||
/// </param>
|
|||
public void SetLevel(int level) |
|||
{ |
|||
if (level == DefaultCompression) |
|||
{ |
|||
level = 6; |
|||
} |
|||
else if (level < NoCompression || level > BestCompression) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(level)); |
|||
} |
|||
|
|||
if (this.deflaterLevel != level) |
|||
{ |
|||
this.deflaterLevel = level; |
|||
this.engine.SetLevel(level); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get current compression level
|
|||
/// </summary>
|
|||
/// <returns>Returns the current compression level</returns>
|
|||
public int GetLevel() |
|||
{ |
|||
return this.deflaterLevel; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the compression strategy. Strategy is one of
|
|||
/// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
|
|||
/// position where the strategy is changed, the same as for
|
|||
/// SetLevel() applies.
|
|||
/// </summary>
|
|||
/// <param name="strategy">
|
|||
/// The new compression strategy.
|
|||
/// </param>
|
|||
public void SetStrategy(DeflateStrategy strategy) |
|||
{ |
|||
this.engine.Strategy = strategy; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Deflates the current input block with to the given array.
|
|||
/// </summary>
|
|||
/// <param name="output">
|
|||
/// The buffer where compressed data is stored
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The number of compressed bytes added to the output, or 0 if either
|
|||
/// IsNeedingInput() or IsFinished returns true or length is zero.
|
|||
/// </returns>
|
|||
public int Deflate(byte[] output) |
|||
{ |
|||
return this.Deflate(output, 0, output.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Deflates the current input block to the given array.
|
|||
/// </summary>
|
|||
/// <param name="output">
|
|||
/// Buffer to store the compressed data.
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// Offset into the output array.
|
|||
/// </param>
|
|||
/// <param name="length">
|
|||
/// The maximum number of bytes that may be stored.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The number of compressed bytes added to the output, or 0 if either
|
|||
/// needsInput() or finished() returns true or length is zero.
|
|||
/// </returns>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// If Finish() was previously called.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">
|
|||
/// If offset or length don't match the array length.
|
|||
/// </exception>
|
|||
public int Deflate(byte[] output, int offset, int length) |
|||
{ |
|||
int origLength = length; |
|||
|
|||
if (this.state == ClosedState) |
|||
{ |
|||
throw new InvalidOperationException("Deflater closed"); |
|||
} |
|||
|
|||
if (this.state < BusyState) |
|||
{ |
|||
// output header
|
|||
int header = (Deflated + |
|||
((DeflaterConstants.MaxWbits - 8) << 4)) << 8; |
|||
int levelFlags = (this.deflaterLevel - 1) >> 1; |
|||
if (levelFlags < 0 || levelFlags > 3) |
|||
{ |
|||
levelFlags = 3; |
|||
} |
|||
|
|||
header |= levelFlags << 6; |
|||
if ((this.state & IsSetdict) != 0) |
|||
{ |
|||
// Dictionary was set
|
|||
header |= DeflaterConstants.PresetDict; |
|||
} |
|||
|
|||
header += 31 - (header % 31); |
|||
|
|||
this.pending.WriteShortMSB(header); |
|||
if ((this.state & IsSetdict) != 0) |
|||
{ |
|||
int chksum = this.engine.Adler; |
|||
this.engine.ResetAdler(); |
|||
this.pending.WriteShortMSB(chksum >> 16); |
|||
this.pending.WriteShortMSB(chksum & 0xffff); |
|||
} |
|||
|
|||
this.state = BusyState | (this.state & (IsFlushing | IsFinishing)); |
|||
} |
|||
|
|||
for (; ;) |
|||
{ |
|||
int count = this.pending.Flush(output, offset, length); |
|||
offset += count; |
|||
this.totalOut += count; |
|||
length -= count; |
|||
|
|||
if (length == 0 || this.state == FinishedState) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
if (!this.engine.Deflate((this.state & IsFlushing) != 0, (this.state & IsFinishing) != 0)) |
|||
{ |
|||
if (this.state == BusyState) |
|||
{ |
|||
// We need more input now
|
|||
return origLength - length; |
|||
} |
|||
else if (this.state == FlushingState) |
|||
{ |
|||
if (this.deflaterLevel != NoCompression) |
|||
{ |
|||
/* We have to supply some lookahead. 8 bit lookahead |
|||
* is needed by the zlib inflater, and we must fill |
|||
* the next byte, so that all bits are flushed. |
|||
*/ |
|||
int neededbits = 8 + ((-this.pending.BitCount) & 7); |
|||
while (neededbits > 0) |
|||
{ |
|||
/* write a static tree block consisting solely of |
|||
* an EOF: |
|||
*/ |
|||
this.pending.WriteBits(2, 10); |
|||
neededbits -= 10; |
|||
} |
|||
} |
|||
|
|||
this.state = BusyState; |
|||
} |
|||
else if (this.state == FinishingState) |
|||
{ |
|||
this.pending.AlignToByte(); |
|||
|
|||
// Compressed data is complete. Write footer information if required.
|
|||
if (!this.noZlibHeaderOrFooter) |
|||
{ |
|||
int adler = this.engine.Adler; |
|||
this.pending.WriteShortMSB(adler >> 16); |
|||
this.pending.WriteShortMSB(adler & 0xffff); |
|||
} |
|||
|
|||
this.state = FinishedState; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return origLength - length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the dictionary which should be used in the deflate process.
|
|||
/// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.
|
|||
/// </summary>
|
|||
/// <param name="dictionary">
|
|||
/// the dictionary.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// if SetInput () or Deflate () were already called or another dictionary was already set.
|
|||
/// </exception>
|
|||
public void SetDictionary(byte[] dictionary) |
|||
{ |
|||
this.SetDictionary(dictionary, 0, dictionary.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the dictionary which should be used in the deflate process.
|
|||
/// The dictionary is a byte array containing strings that are
|
|||
/// likely to occur in the data which should be compressed. The
|
|||
/// dictionary is not stored in the compressed output, only a
|
|||
/// checksum. To decompress the output you need to supply the same
|
|||
/// dictionary again.
|
|||
/// </summary>
|
|||
/// <param name="dictionary">
|
|||
/// The dictionary data
|
|||
/// </param>
|
|||
/// <param name="index">
|
|||
/// The index where dictionary information commences.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// The number of bytes in the dictionary.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// If SetInput () or Deflate() were already called or another dictionary was already set.
|
|||
/// </exception>
|
|||
public void SetDictionary(byte[] dictionary, int index, int count) |
|||
{ |
|||
if (this.state != InitState) |
|||
{ |
|||
throw new InvalidOperationException(); |
|||
} |
|||
|
|||
this.state = SetdictState; |
|||
this.engine.SetDictionary(dictionary, index, count); |
|||
} |
|||
} |
|||
} |
|||
@ -1,145 +0,0 @@ |
|||
// <copyright file="DeflaterConstants.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// This class contains constants used for deflation.
|
|||
/// </summary>
|
|||
public class DeflaterConstants |
|||
{ |
|||
/// <summary>
|
|||
/// Written to Zip file to identify a stored block
|
|||
/// </summary>
|
|||
public const int StoredBlock = 0; |
|||
|
|||
/// <summary>
|
|||
/// Identifies static tree in Zip file
|
|||
/// </summary>
|
|||
public const int StaticTrees = 1; |
|||
|
|||
/// <summary>
|
|||
/// Identifies dynamic tree in Zip file
|
|||
/// </summary>
|
|||
public const int DynTrees = 2; |
|||
|
|||
/// <summary>
|
|||
/// Header flag indicating a preset dictionary for deflation
|
|||
/// </summary>
|
|||
public const int PresetDict = 0x20; |
|||
|
|||
/// <summary>
|
|||
/// Sets internal buffer sizes for Huffman encoding
|
|||
/// </summary>
|
|||
public const int DefaultMemLevel = 8; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int MaxMatch = 258; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int MinMatch = 3; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int MaxWbits = 15; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int Wsize = 1 << MaxWbits; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int Wmask = Wsize - 1; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int HashBits = DefaultMemLevel + 7; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int HashSize = 1 << HashBits; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int HashMask = HashSize - 1; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int HashShift = (HashBits + MinMatch - 1) / MinMatch; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int MinLookahead = MaxMatch + MinMatch + 1; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int MaxDist = Wsize - MinLookahead; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int PendingBufSize = 1 << (DefaultMemLevel + 8); |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int Deflatestored = 0; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int Deflatefast = 1; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public const int Deflateslow = 2; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int MaxBlockSize => Math.Min(65535, PendingBufSize - 5); |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int[] GoodLength => new[] { 0, 4, 4, 4, 4, 8, 8, 8, 32, 32 }; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int[] MaxLazy => new[] { 0, 4, 5, 6, 4, 16, 16, 32, 128, 258 }; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int[] NiceLength => new[] { 0, 8, 16, 32, 16, 32, 128, 128, 258, 258 }; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int[] MaxChain => new[] { 0, 4, 8, 32, 16, 32, 128, 256, 1024, 4096 }; |
|||
|
|||
/// <summary>
|
|||
/// Internal compression engine constant
|
|||
/// </summary>
|
|||
public static int[] ComprFunc => new[] { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; |
|||
} |
|||
} |
|||
@ -1,810 +0,0 @@ |
|||
// <copyright file="DeflaterEngine.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Low level compression engine for deflate algorithm which uses a 32K sliding window
|
|||
/// with secondary compression from Huffman/Shannon-Fano codes.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// DEFLATE ALGORITHM:
|
|||
///
|
|||
/// The uncompressed stream is inserted into the window array. When
|
|||
/// the window array is full the first half is thrown away and the
|
|||
/// second half is copied to the beginning.
|
|||
///
|
|||
/// The head array is a hash table. Three characters build a hash value
|
|||
/// and they the value points to the corresponding index in window of
|
|||
/// the last string with this hash. The prev array implements a
|
|||
/// linked list of matches with the same hash: prev[index & WMASK] points
|
|||
/// to the previous index with the same hash.
|
|||
/// </remarks>
|
|||
public class DeflaterEngine : DeflaterConstants |
|||
{ |
|||
/// <summary>
|
|||
/// One more than the maximum upper bounds.
|
|||
/// </summary>
|
|||
private const int TooFar = 4096; |
|||
|
|||
/// <summary>
|
|||
/// <code>prev[index & WMASK]</code> points to the previous index that has the
|
|||
/// same hash code as the string starting at index. This way
|
|||
/// entries with the same hash code are in a linked list.
|
|||
/// Note that the array should really be unsigned short, so you need
|
|||
/// to and the values with 0xffff.
|
|||
/// </summary>
|
|||
private readonly short[] previousIndex; |
|||
|
|||
/// <summary>
|
|||
/// Hashtable, hashing three characters to an index for window, so
|
|||
/// that window[index]..window[index+2] have this hash code.
|
|||
/// Note that the array should really be unsigned short, so you need
|
|||
/// to and the values with 0xffff.
|
|||
/// </summary>
|
|||
private readonly short[] head; |
|||
|
|||
/// <summary>
|
|||
/// This array contains the part of the uncompressed stream that
|
|||
/// is of relevance. The current character is indexed by strstart.
|
|||
/// </summary>
|
|||
private readonly byte[] window; |
|||
|
|||
/// <summary>
|
|||
/// Stores the pending output of the deflator
|
|||
/// </summary>
|
|||
private readonly DeflaterPending pending; |
|||
|
|||
/// <summary>
|
|||
/// The huffman deflator
|
|||
/// </summary>
|
|||
private readonly DeflaterHuffman huffman; |
|||
|
|||
/// <summary>
|
|||
/// The adler checksum
|
|||
/// </summary>
|
|||
private readonly Adler32 adler; |
|||
|
|||
/// <summary>
|
|||
/// Hash index of string to be inserted.
|
|||
/// </summary>
|
|||
private int insertHashIndex; |
|||
|
|||
/// <summary>
|
|||
/// Index of the beginning of a match.
|
|||
/// </summary>
|
|||
private int matchStart; |
|||
|
|||
/// <summary>
|
|||
/// Length of best match
|
|||
/// </summary>
|
|||
private int matchLen; |
|||
|
|||
/// <summary>
|
|||
/// Set if previous match exists
|
|||
/// </summary>
|
|||
private bool prevAvailable; |
|||
|
|||
/// <summary>
|
|||
/// The index of the beinning of a block
|
|||
/// </summary>
|
|||
private int blockStart; |
|||
|
|||
/// <summary>
|
|||
/// Points to the current character in the window.
|
|||
/// </summary>
|
|||
private int strstart; |
|||
|
|||
/// <summary>
|
|||
/// lookahead is the number of characters starting at strstart in
|
|||
/// window that are valid.
|
|||
/// So window[strstart] until window[strstart+lookahead-1] are valid
|
|||
/// characters.
|
|||
/// </summary>
|
|||
private int lookahead; |
|||
|
|||
/// <summary>
|
|||
/// The maximum chain length
|
|||
/// </summary>
|
|||
private int maxChain; |
|||
|
|||
/// <summary>
|
|||
/// The maximum lazy length
|
|||
/// </summary>
|
|||
private int maxLazy; |
|||
|
|||
/// <summary>
|
|||
/// The nice length
|
|||
/// </summary>
|
|||
private int niceLength; |
|||
|
|||
/// <summary>
|
|||
/// The good length
|
|||
/// </summary>
|
|||
private int goodLength; |
|||
|
|||
/// <summary>
|
|||
/// The current compression function.
|
|||
/// </summary>
|
|||
private int compressionFunction; |
|||
|
|||
/// <summary>
|
|||
/// The input data for compression.
|
|||
/// </summary>
|
|||
private byte[] inputBuf; |
|||
|
|||
/// <summary>
|
|||
/// The total bytes of input read.
|
|||
/// </summary>
|
|||
private long totalIn; |
|||
|
|||
/// <summary>
|
|||
/// The offset into inputBuf, where input data starts.
|
|||
/// </summary>
|
|||
private int inputOff; |
|||
|
|||
/// <summary>
|
|||
/// The end offset of the input data.
|
|||
/// </summary>
|
|||
private int inputEnd; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterEngine"/> class with a pending buffer.
|
|||
/// </summary>
|
|||
/// <param name="pending">The pending buffer to use</param>>
|
|||
public DeflaterEngine(DeflaterPending pending) |
|||
{ |
|||
this.pending = pending; |
|||
this.huffman = new DeflaterHuffman(pending); |
|||
this.adler = new Adler32(); |
|||
|
|||
this.window = new byte[2 * Wsize]; |
|||
this.head = new short[HashSize]; |
|||
this.previousIndex = new short[Wsize]; |
|||
|
|||
// We start at index 1, to avoid an implementation deficiency, that
|
|||
// we cannot build a repeat pattern at index 0.
|
|||
this.blockStart = this.strstart = 1; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get current value of Adler checksum
|
|||
/// </summary>
|
|||
public int Adler => unchecked((int)this.adler.Value); |
|||
|
|||
/// <summary>
|
|||
/// Total data processed
|
|||
/// </summary>
|
|||
public long TotalIn => this.totalIn; |
|||
|
|||
/// <summary>
|
|||
/// Get or sets the <see cref="DeflateStrategy"/>
|
|||
/// </summary>
|
|||
public DeflateStrategy Strategy { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Deflate drives actual compression of data
|
|||
/// </summary>
|
|||
/// <param name="flush">True to flush input buffers</param>
|
|||
/// <param name="finish">Finish deflation with the current input.</param>
|
|||
/// <returns>Returns true if progress has been made.</returns>
|
|||
public bool Deflate(bool flush, bool finish) |
|||
{ |
|||
bool progress; |
|||
do |
|||
{ |
|||
this.FillWindow(); |
|||
bool canFlush = flush && (this.inputOff == this.inputEnd); |
|||
|
|||
switch (this.compressionFunction) |
|||
{ |
|||
case Deflatestored: |
|||
progress = this.DeflateStored(canFlush, finish); |
|||
break; |
|||
case Deflatefast: |
|||
progress = this.DeflateFast(canFlush, finish); |
|||
break; |
|||
case Deflateslow: |
|||
progress = this.DeflateSlow(canFlush, finish); |
|||
break; |
|||
default: |
|||
throw new InvalidOperationException("unknown compressionFunction"); |
|||
} |
|||
} |
|||
while (this.pending.IsFlushed && progress); // repeat while we have no pending output and progress was made
|
|||
return progress; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets input data to be deflated. Should only be called when <code>NeedsInput()</code>
|
|||
/// returns true
|
|||
/// </summary>
|
|||
/// <param name="buffer">The buffer containing input data.</param>
|
|||
/// <param name="offset">The offset of the first byte of data.</param>
|
|||
/// <param name="count">The number of bytes of data to use as input.</param>
|
|||
public void SetInput(byte[] buffer, int offset, int count) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
} |
|||
|
|||
if (offset < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(offset)); |
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(count)); |
|||
} |
|||
|
|||
if (this.inputOff < this.inputEnd) |
|||
{ |
|||
throw new InvalidOperationException("Old input was not completely processed"); |
|||
} |
|||
|
|||
int end = offset + count; |
|||
|
|||
// We want to throw an ArrayIndexOutOfBoundsException early. The
|
|||
// check is very tricky: it also handles integer wrap around.
|
|||
if ((offset > end) || (end > buffer.Length)) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(count)); |
|||
} |
|||
|
|||
this.inputBuf = buffer; |
|||
this.inputOff = offset; |
|||
this.inputEnd = end; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Determines if more <see cref="SetInput">input</see> is needed.
|
|||
/// </summary>
|
|||
/// <returns>Return true if input is needed via <see cref="SetInput">SetInput</see></returns>
|
|||
public bool NeedsInput() |
|||
{ |
|||
return this.inputEnd == this.inputOff; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Set compression dictionary
|
|||
/// </summary>
|
|||
/// <param name="buffer">The buffer containing the dictionary data</param>
|
|||
/// <param name="offset">The offset in the buffer for the first byte of data</param>
|
|||
/// <param name="length">The length of the dictionary data.</param>
|
|||
public void SetDictionary(byte[] buffer, int offset, int length) |
|||
{ |
|||
this.adler.Update(buffer, offset, length); |
|||
if (length < MinMatch) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (length > MaxDist) |
|||
{ |
|||
offset += length - MaxDist; |
|||
length = MaxDist; |
|||
} |
|||
|
|||
Array.Copy(buffer, offset, this.window, this.strstart, length); |
|||
|
|||
this.UpdateHash(); |
|||
--length; |
|||
while (--length > 0) |
|||
{ |
|||
this.InsertString(); |
|||
this.strstart++; |
|||
} |
|||
|
|||
this.strstart += 2; |
|||
this.blockStart = this.strstart; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reset internal state
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
this.huffman.Reset(); |
|||
this.adler.Reset(); |
|||
this.blockStart = this.strstart = 1; |
|||
this.lookahead = 0; |
|||
this.totalIn = 0; |
|||
this.prevAvailable = false; |
|||
this.matchLen = MinMatch - 1; |
|||
|
|||
for (int i = 0; i < HashSize; i++) |
|||
{ |
|||
this.head[i] = 0; |
|||
} |
|||
|
|||
for (int i = 0; i < Wsize; i++) |
|||
{ |
|||
this.previousIndex[i] = 0; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reset Adler checksum
|
|||
/// </summary>
|
|||
public void ResetAdler() |
|||
{ |
|||
this.adler.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Set the deflate level (0-9)
|
|||
/// </summary>
|
|||
/// <param name="level">The value to set the level to.</param>
|
|||
public void SetLevel(int level) |
|||
{ |
|||
if ((level < 0) || (level > 9)) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(level)); |
|||
} |
|||
|
|||
this.goodLength = GoodLength[level]; |
|||
this.maxLazy = MaxLazy[level]; |
|||
this.niceLength = NiceLength[level]; |
|||
this.maxChain = MaxChain[level]; |
|||
|
|||
if (ComprFunc[level] != this.compressionFunction) |
|||
{ |
|||
switch (this.compressionFunction) |
|||
{ |
|||
case Deflatestored: |
|||
if (this.strstart > this.blockStart) |
|||
{ |
|||
this.huffman.FlushStoredBlock(this.window, this.blockStart, this.strstart - this.blockStart, false); |
|||
this.blockStart = this.strstart; |
|||
} |
|||
|
|||
this.UpdateHash(); |
|||
break; |
|||
|
|||
case Deflatefast: |
|||
if (this.strstart > this.blockStart) |
|||
{ |
|||
this.huffman.FlushBlock(this.window, this.blockStart, this.strstart - this.blockStart, false); |
|||
this.blockStart = this.strstart; |
|||
} |
|||
|
|||
break; |
|||
|
|||
case Deflateslow: |
|||
if (this.prevAvailable) |
|||
{ |
|||
this.huffman.TallyLit(this.window[this.strstart - 1] & 0xff); |
|||
} |
|||
|
|||
if (this.strstart > this.blockStart) |
|||
{ |
|||
this.huffman.FlushBlock(this.window, this.blockStart, this.strstart - this.blockStart, false); |
|||
this.blockStart = this.strstart; |
|||
} |
|||
|
|||
this.prevAvailable = false; |
|||
this.matchLen = MinMatch - 1; |
|||
break; |
|||
} |
|||
|
|||
this.compressionFunction = ComprFunc[level]; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fills the window
|
|||
/// </summary>
|
|||
public void FillWindow() |
|||
{ |
|||
// If the window is almost full and there is insufficient lookahead,
|
|||
// move the upper half to the lower one to make room in the upper half.
|
|||
if (this.strstart >= Wsize + MaxDist) |
|||
{ |
|||
this.SlideWindow(); |
|||
} |
|||
|
|||
// If there is not enough lookahead, but still some input left,
|
|||
// read in the input
|
|||
while (this.lookahead < MinLookahead && this.inputOff < this.inputEnd) |
|||
{ |
|||
int more = (2 * Wsize) - this.lookahead - this.strstart; |
|||
|
|||
if (more > this.inputEnd - this.inputOff) |
|||
{ |
|||
more = this.inputEnd - this.inputOff; |
|||
} |
|||
|
|||
Array.Copy(this.inputBuf, this.inputOff, this.window, this.strstart + this.lookahead, more); |
|||
this.adler.Update(this.inputBuf, this.inputOff, more); |
|||
|
|||
this.inputOff += more; |
|||
this.totalIn += more; |
|||
this.lookahead += more; |
|||
} |
|||
|
|||
if (this.lookahead >= MinMatch) |
|||
{ |
|||
this.UpdateHash(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Updates this hash.
|
|||
/// </summary>
|
|||
private void UpdateHash() |
|||
{ |
|||
this.insertHashIndex = (this.window[this.strstart] << HashShift) ^ this.window[this.strstart + 1]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Inserts the current string in the head hash and returns the previous
|
|||
/// value for this hash.
|
|||
/// </summary>
|
|||
/// <returns>The previous hash value</returns>
|
|||
private int InsertString() |
|||
{ |
|||
short match; |
|||
int hash = ((this.insertHashIndex << HashShift) ^ this.window[this.strstart + (MinMatch - 1)]) & HashMask; |
|||
|
|||
this.previousIndex[this.strstart & Wmask] = match = this.head[hash]; |
|||
this.head[hash] = unchecked((short)this.strstart); |
|||
this.insertHashIndex = hash; |
|||
return match & 0xffff; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Slides the current byte window to the ewlefvent part of the uncompressed stream.
|
|||
/// </summary>
|
|||
private void SlideWindow() |
|||
{ |
|||
Array.Copy(this.window, Wsize, this.window, 0, Wsize); |
|||
this.matchStart -= Wsize; |
|||
this.strstart -= Wsize; |
|||
this.blockStart -= Wsize; |
|||
|
|||
// Slide the hash table (could be avoided with 32 bit values
|
|||
// at the expense of memory usage).
|
|||
for (int i = 0; i < HashSize; ++i) |
|||
{ |
|||
int m = this.head[i] & 0xffff; |
|||
this.head[i] = (short)(m >= Wsize ? (m - Wsize) : 0); |
|||
} |
|||
|
|||
// Slide the prev table.
|
|||
for (int i = 0; i < Wsize; i++) |
|||
{ |
|||
int m = this.previousIndex[i] & 0xffff; |
|||
this.previousIndex[i] = (short)(m >= Wsize ? (m - Wsize) : 0); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Find the best (longest) string in the window matching the
|
|||
/// string starting at strstart.
|
|||
///
|
|||
/// Preconditions:
|
|||
/// <code>
|
|||
/// strstart + MAX_MATCH <= window.length.</code>
|
|||
/// </summary>
|
|||
/// <param name="curMatch">The current match.</param>
|
|||
/// <returns>True if a match greater than the minimum length is found</returns>
|
|||
private bool FindLongestMatch(int curMatch) |
|||
{ |
|||
int chainLength = this.maxChain; |
|||
int length = this.niceLength; |
|||
short[] previous = this.previousIndex; |
|||
int scan = this.strstart; |
|||
int bestEnd = this.strstart + this.matchLen; |
|||
int bestLength = Math.Max(this.matchLen, MinMatch - 1); |
|||
|
|||
int limit = Math.Max(this.strstart - MaxDist, 0); |
|||
|
|||
int strend = this.strstart + MaxMatch - 1; |
|||
byte scanEnd1 = this.window[bestEnd - 1]; |
|||
byte scanEnd = this.window[bestEnd]; |
|||
|
|||
// Do not waste too much time if we already have a good match:
|
|||
if (bestLength >= this.goodLength) |
|||
{ |
|||
chainLength >>= 2; |
|||
} |
|||
|
|||
// Do not look for matches beyond the end of the input. This is necessary
|
|||
// to make deflate deterministic.
|
|||
if (length > this.lookahead) |
|||
{ |
|||
length = this.lookahead; |
|||
} |
|||
|
|||
do |
|||
{ |
|||
if (this.window[curMatch + bestLength] != scanEnd || |
|||
this.window[curMatch + bestLength - 1] != scanEnd1 || |
|||
this.window[curMatch] != this.window[scan] || |
|||
this.window[curMatch + 1] != this.window[scan + 1]) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
int match = curMatch + 2; |
|||
scan += 2; |
|||
|
|||
// We check for insufficient lookahead only every 8th comparison;
|
|||
// the 256th check will be made at strstart + 258.
|
|||
while ( |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
this.window[++scan] == this.window[++match] && |
|||
(scan < strend)) |
|||
{ |
|||
// Do nothing
|
|||
} |
|||
|
|||
if (scan > bestEnd) |
|||
{ |
|||
this.matchStart = curMatch; |
|||
bestEnd = scan; |
|||
bestLength = scan - this.strstart; |
|||
|
|||
if (bestLength >= length) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
scanEnd1 = this.window[bestEnd - 1]; |
|||
scanEnd = this.window[bestEnd]; |
|||
} |
|||
|
|||
scan = this.strstart; |
|||
} |
|||
while ((curMatch = previous[curMatch & Wmask] & 0xffff) > limit && --chainLength != 0); |
|||
|
|||
this.matchLen = Math.Min(bestLength, this.lookahead); |
|||
return this.matchLen >= MinMatch; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a value indicating whether the uncompressed block is stored.
|
|||
/// </summary>
|
|||
/// <param name="flush">Whether to flush the stream.</param>
|
|||
/// <param name="finish">Whether to finish the stream.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool DeflateStored(bool flush, bool finish) |
|||
{ |
|||
if (!flush && (this.lookahead == 0)) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.strstart += this.lookahead; |
|||
this.lookahead = 0; |
|||
|
|||
int storedLength = this.strstart - this.blockStart; |
|||
|
|||
if ((storedLength >= MaxBlockSize) || // Block is full
|
|||
(this.blockStart < Wsize && storedLength >= MaxDist) || // Block may move out of window
|
|||
flush) |
|||
{ |
|||
bool lastBlock = finish; |
|||
if (storedLength > MaxBlockSize) |
|||
{ |
|||
storedLength = MaxBlockSize; |
|||
lastBlock = false; |
|||
} |
|||
|
|||
this.huffman.FlushStoredBlock(this.window, this.blockStart, storedLength, lastBlock); |
|||
this.blockStart += storedLength; |
|||
return !lastBlock; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Performs a fast deflation of the input stream return a value to indicate succes.
|
|||
/// </summary>
|
|||
/// <param name="flush">Whether to flush the stream.</param>
|
|||
/// <param name="finish">Whether to finish the stream.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool DeflateFast(bool flush, bool finish) |
|||
{ |
|||
if (this.lookahead < MinLookahead && !flush) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
while (this.lookahead >= MinLookahead || flush) |
|||
{ |
|||
if (this.lookahead == 0) |
|||
{ |
|||
// We are flushing everything
|
|||
this.huffman.FlushBlock(this.window, this.blockStart, this.strstart - this.blockStart, finish); |
|||
this.blockStart = this.strstart; |
|||
return false; |
|||
} |
|||
|
|||
if (this.strstart > (2 * Wsize) - MinLookahead) |
|||
{ |
|||
// slide window, as FindLongestMatch needs this.
|
|||
// This should only happen when flushing and the window
|
|||
// is almost full.
|
|||
this.SlideWindow(); |
|||
} |
|||
|
|||
int hashHead; |
|||
if (this.lookahead >= MinMatch && |
|||
(hashHead = this.InsertString()) != 0 && |
|||
this.Strategy != DeflateStrategy.HuffmanOnly && |
|||
this.strstart - hashHead <= MaxDist && |
|||
this.FindLongestMatch(hashHead)) |
|||
{ |
|||
// longestMatch sets matchStart and matchLen
|
|||
bool full = this.huffman.TallyDist(this.strstart - this.matchStart, this.matchLen); |
|||
|
|||
this.lookahead -= this.matchLen; |
|||
if (this.matchLen <= this.maxLazy && this.lookahead >= MinMatch) |
|||
{ |
|||
while (--this.matchLen > 0) |
|||
{ |
|||
++this.strstart; |
|||
this.InsertString(); |
|||
} |
|||
|
|||
++this.strstart; |
|||
} |
|||
else |
|||
{ |
|||
this.strstart += this.matchLen; |
|||
if (this.lookahead >= MinMatch - 1) |
|||
{ |
|||
this.UpdateHash(); |
|||
} |
|||
} |
|||
|
|||
this.matchLen = MinMatch - 1; |
|||
if (!full) |
|||
{ |
|||
continue; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
// No match found
|
|||
this.huffman.TallyLit(this.window[this.strstart] & 0xff); |
|||
++this.strstart; |
|||
--this.lookahead; |
|||
} |
|||
|
|||
if (this.huffman.IsFull()) |
|||
{ |
|||
bool lastBlock = finish && (this.lookahead == 0); |
|||
this.huffman.FlushBlock(this.window, this.blockStart, this.strstart - this.blockStart, lastBlock); |
|||
this.blockStart = this.strstart; |
|||
return !lastBlock; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Performs a slow deflation of the input stream return a value to indicate succes.
|
|||
/// </summary>
|
|||
/// <param name="flush">Whether to flush the stream.</param>
|
|||
/// <param name="finish">Whether to finish the stream.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool DeflateSlow(bool flush, bool finish) |
|||
{ |
|||
if (this.lookahead < MinLookahead && !flush) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
while (this.lookahead >= MinLookahead || flush) |
|||
{ |
|||
if (this.lookahead == 0) |
|||
{ |
|||
if (this.prevAvailable) |
|||
{ |
|||
this.huffman.TallyLit(this.window[this.strstart - 1] & 0xff); |
|||
} |
|||
|
|||
this.prevAvailable = false; |
|||
|
|||
// We are flushing everything
|
|||
this.huffman.FlushBlock(this.window, this.blockStart, this.strstart - this.blockStart, finish); |
|||
this.blockStart = this.strstart; |
|||
return false; |
|||
} |
|||
|
|||
if (this.strstart >= (2 * Wsize) - MinLookahead) |
|||
{ |
|||
// slide window, as FindLongestMatch needs this.
|
|||
// This should only happen when flushing and the window
|
|||
// is almost full.
|
|||
this.SlideWindow(); |
|||
} |
|||
|
|||
int prevMatch = this.matchStart; |
|||
int prevLen = this.matchLen; |
|||
if (this.lookahead >= MinMatch) |
|||
{ |
|||
int hashHead = this.InsertString(); |
|||
|
|||
if (this.Strategy != DeflateStrategy.HuffmanOnly && |
|||
hashHead != 0 && |
|||
this.strstart - hashHead <= MaxDist && |
|||
this.FindLongestMatch(hashHead)) |
|||
{ |
|||
// longestMatch sets matchStart and matchLen
|
|||
// Discard match if too small and too far away
|
|||
if (this.matchLen <= 5 && (this.Strategy == DeflateStrategy.Filtered || (this.matchLen == MinMatch && this.strstart - this.matchStart > TooFar))) |
|||
{ |
|||
this.matchLen = MinMatch - 1; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// previous match was better
|
|||
if ((prevLen >= MinMatch) && (this.matchLen <= prevLen)) |
|||
{ |
|||
this.huffman.TallyDist(this.strstart - 1 - prevMatch, prevLen); |
|||
prevLen -= 2; |
|||
do |
|||
{ |
|||
this.strstart++; |
|||
this.lookahead--; |
|||
if (this.lookahead >= MinMatch) |
|||
{ |
|||
this.InsertString(); |
|||
} |
|||
} |
|||
while (--prevLen > 0); |
|||
|
|||
this.strstart++; |
|||
this.lookahead--; |
|||
this.prevAvailable = false; |
|||
this.matchLen = MinMatch - 1; |
|||
} |
|||
else |
|||
{ |
|||
if (this.prevAvailable) |
|||
{ |
|||
this.huffman.TallyLit(this.window[this.strstart - 1] & 0xff); |
|||
} |
|||
|
|||
this.prevAvailable = true; |
|||
this.strstart++; |
|||
this.lookahead--; |
|||
} |
|||
|
|||
if (this.huffman.IsFull()) |
|||
{ |
|||
int len = this.strstart - this.blockStart; |
|||
if (this.prevAvailable) |
|||
{ |
|||
len--; |
|||
} |
|||
|
|||
bool lastBlock = finish && (this.lookahead == 0) && !this.prevAvailable; |
|||
this.huffman.FlushBlock(this.window, this.blockStart, len, lastBlock); |
|||
this.blockStart += len; |
|||
return !lastBlock; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -1,889 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// This is the DeflaterHuffman class.
|
|||
///
|
|||
/// This class is <i>not</i> thread safe. This is inherent in the API, due
|
|||
/// to the split of Deflate and SetInput.
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class DeflaterHuffman |
|||
{ |
|||
/// <summary>
|
|||
/// The buffer size.
|
|||
/// </summary>
|
|||
private const int Buffersize = 1 << (DeflaterConstants.DefaultMemLevel + 6); |
|||
|
|||
/// <summary>
|
|||
/// The number of literals.
|
|||
/// </summary>
|
|||
private const int LiteralCount = 286; |
|||
|
|||
/// <summary>
|
|||
/// Number of distance codes
|
|||
/// </summary>
|
|||
private const int DistanceCodeCount = 30; |
|||
|
|||
/// <summary>
|
|||
/// Number of codes used to transfer bit lengths
|
|||
/// </summary>
|
|||
private const int BitLengthCount = 19; |
|||
|
|||
/// <summary>
|
|||
/// Repeat previous bit length 3-6 times (2 bits of repeat count)
|
|||
/// </summary>
|
|||
private const int Repeat3To6 = 16; |
|||
|
|||
/// <summary>
|
|||
/// Repeat a zero length 3-10 times (3 bits of repeat count)
|
|||
/// </summary>
|
|||
private const int Repeat3To10 = 17; |
|||
|
|||
/// <summary>
|
|||
/// Repeat a zero length 11-138 times (7 bits of repeat count)
|
|||
/// </summary>
|
|||
private const int Repeat11To138 = 18; |
|||
|
|||
/// <summary>
|
|||
/// The end of file flag.
|
|||
/// </summary>
|
|||
private const int Eof = 256; |
|||
|
|||
/// <summary>
|
|||
/// The lengths of the bit length codes are sent in order of decreasing
|
|||
/// probability, to avoid transmitting the lengths for unused bit length codes.
|
|||
/// </summary>
|
|||
private static readonly int[] BitLengthOrder = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; |
|||
|
|||
/// <summary>
|
|||
/// Bit data reversed.
|
|||
/// </summary>
|
|||
private static readonly byte[] Bit4Reverse = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 }; |
|||
|
|||
/// <summary>
|
|||
/// The literal codes.
|
|||
/// </summary>
|
|||
private static short[] staticLCodes; |
|||
private static byte[] staticLLength; |
|||
private static short[] staticDCodes; |
|||
private static byte[] staticDLength; |
|||
|
|||
/// <summary>
|
|||
/// A binary tree, with the property, that the parent node is smaller than both child nodes.
|
|||
/// </summary>
|
|||
private class Tree |
|||
{ |
|||
/// <summary>
|
|||
/// The minimum number of codes.
|
|||
/// </summary>
|
|||
private readonly int minimumNumberOfCodes; |
|||
|
|||
/// <summary>
|
|||
/// The array of codes.
|
|||
/// </summary>
|
|||
private short[] codes; |
|||
private readonly int[] blCounts; |
|||
private readonly int maxLength; |
|||
private readonly DeflaterHuffman deflater; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Tree"/> class.
|
|||
/// </summary>
|
|||
/// <param name="huffman">The <see cref="DeflaterHuffman"/></param>
|
|||
/// <param name="elems">The elements.</param>
|
|||
/// <param name="minCodes">The minimum number of codes.</param>
|
|||
/// <param name="maxLength">The maximum length.</param>
|
|||
public Tree(DeflaterHuffman huffman, int elems, int minCodes, int maxLength) |
|||
{ |
|||
this.deflater = huffman; |
|||
this.minimumNumberOfCodes = minCodes; |
|||
this.maxLength = maxLength; |
|||
this.Frequencies = new short[elems]; |
|||
this.blCounts = new int[maxLength]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of codes.
|
|||
/// </summary>
|
|||
public int NumberOfCodes { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the frequencies.
|
|||
/// </summary>
|
|||
public short[] Frequencies { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the length.
|
|||
/// </summary>
|
|||
public byte[] Length { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Resets the internal state of the tree
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
for (int i = 0; i < this.Frequencies.Length; i++) |
|||
{ |
|||
this.Frequencies[i] = 0; |
|||
} |
|||
|
|||
this.codes = null; |
|||
this.Length = null; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes a code symbol.
|
|||
/// </summary>
|
|||
/// <param name="code">The code index.</param>
|
|||
public void WriteSymbol(int code) |
|||
{ |
|||
this.deflater.pending.WriteBits(this.codes[code] & 0xffff, this.Length[code]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Set static codes and length
|
|||
/// </summary>
|
|||
/// <param name="staticCodes">new codes</param>
|
|||
/// <param name="staticLengths">length for new codes</param>
|
|||
public void SetStaticCodes(short[] staticCodes, byte[] staticLengths) |
|||
{ |
|||
this.codes = staticCodes; |
|||
this.Length = staticLengths; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Build dynamic codes and lengths
|
|||
/// </summary>
|
|||
public void BuildCodes() |
|||
{ |
|||
int numSymbols = this.Frequencies.Length; |
|||
int[] nextCode = new int[this.maxLength]; |
|||
int code = 0; |
|||
|
|||
this.codes = new short[numSymbols]; |
|||
|
|||
for (int bits = 0; bits < this.maxLength; bits++) |
|||
{ |
|||
nextCode[bits] = code; |
|||
code += this.blCounts[bits] << (15 - bits); |
|||
} |
|||
|
|||
for (int i = 0; i < this.NumberOfCodes; i++) |
|||
{ |
|||
int bits = this.Length[i]; |
|||
if (bits > 0) |
|||
{ |
|||
this.codes[i] = BitReverse(nextCode[bits - 1]); |
|||
nextCode[bits - 1] += 1 << (16 - bits); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void BuildTree() |
|||
{ |
|||
int numSymbols = this.Frequencies.Length; |
|||
|
|||
// Heap is a priority queue, sorted by frequency, least frequent
|
|||
// nodes first. The heap is a binary tree, with the property, that
|
|||
// the parent node is smaller than both child nodes. This assures
|
|||
// that the smallest node is the first parent.
|
|||
//
|
|||
// The binary tree is encoded in an array: 0 is root node and
|
|||
// the nodes 2//n+1, 2//n+2 are the child nodes of node n.
|
|||
int[] heap = new int[numSymbols]; |
|||
int heapLen = 0; |
|||
int maxCode = 0; |
|||
for (int n = 0; n < numSymbols; n++) |
|||
{ |
|||
int freq = this.Frequencies[n]; |
|||
if (freq != 0) |
|||
{ |
|||
// Insert n into heap
|
|||
int pos = heapLen++; |
|||
int ppos; |
|||
while (pos > 0 && this.Frequencies[heap[ppos = (pos - 1) / 2]] > freq) |
|||
{ |
|||
heap[pos] = heap[ppos]; |
|||
pos = ppos; |
|||
} |
|||
|
|||
heap[pos] = n; |
|||
|
|||
maxCode = n; |
|||
} |
|||
} |
|||
|
|||
// We could encode a single literal with 0 bits but then we
|
|||
// don't see the literals. Therefore we force at least two
|
|||
// literals to avoid this case. We don't care about order in
|
|||
// this case, both literals get a 1 bit code.
|
|||
while (heapLen < 2) |
|||
{ |
|||
int node = maxCode < 2 ? ++maxCode : 0; |
|||
heap[heapLen++] = node; |
|||
} |
|||
|
|||
this.NumberOfCodes = Math.Max(maxCode + 1, this.minimumNumberOfCodes); |
|||
|
|||
int numLeafs = heapLen; |
|||
int[] childs = new int[(4 * heapLen) - 2]; |
|||
int[] values = new int[(2 * heapLen) - 1]; |
|||
int numNodes = numLeafs; |
|||
for (int i = 0; i < heapLen; i++) |
|||
{ |
|||
int node = heap[i]; |
|||
childs[2 * i] = node; |
|||
childs[(2 * i) + 1] = -1; |
|||
values[i] = this.Frequencies[node] << 8; |
|||
heap[i] = i; |
|||
} |
|||
|
|||
// Construct the Huffman tree by repeatedly combining the least two
|
|||
// frequent nodes.
|
|||
do |
|||
{ |
|||
int first = heap[0]; |
|||
int last = heap[--heapLen]; |
|||
|
|||
// Propagate the hole to the leafs of the heap
|
|||
int ppos = 0; |
|||
int path = 1; |
|||
|
|||
while (path < heapLen) |
|||
{ |
|||
if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) |
|||
{ |
|||
path++; |
|||
} |
|||
|
|||
heap[ppos] = heap[path]; |
|||
ppos = path; |
|||
path = (path * 2) + 1; |
|||
} |
|||
|
|||
// Now propagate the last element down along path. Normally
|
|||
// it shouldn't go too deep.
|
|||
int lastVal = values[last]; |
|||
while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) |
|||
{ |
|||
heap[path] = heap[ppos]; |
|||
} |
|||
|
|||
heap[path] = last; |
|||
|
|||
int second = heap[0]; |
|||
|
|||
// Create a new node father of first and second
|
|||
last = numNodes++; |
|||
childs[2 * last] = first; |
|||
childs[(2 * last) + 1] = second; |
|||
int mindepth = Math.Min(values[first] & 0xff, values[second] & 0xff); |
|||
values[last] = lastVal = values[first] + values[second] - mindepth + 1; |
|||
|
|||
// Again, propagate the hole to the leafs
|
|||
ppos = 0; |
|||
path = 1; |
|||
|
|||
while (path < heapLen) |
|||
{ |
|||
if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) |
|||
{ |
|||
path++; |
|||
} |
|||
|
|||
heap[ppos] = heap[path]; |
|||
ppos = path; |
|||
path = (ppos * 2) + 1; |
|||
} |
|||
|
|||
// Now propagate the new element down along path
|
|||
while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) |
|||
{ |
|||
heap[path] = heap[ppos]; |
|||
} |
|||
|
|||
heap[path] = last; |
|||
} |
|||
while (heapLen > 1); |
|||
|
|||
if (heap[0] != (childs.Length / 2) - 1) |
|||
{ |
|||
throw new ImageFormatException("Heap invariant violated"); |
|||
} |
|||
|
|||
this.BuildLength(childs); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get encoded length
|
|||
/// </summary>
|
|||
/// <returns>Encoded length, the sum of frequencies * lengths</returns>
|
|||
public int GetEncodedLength() |
|||
{ |
|||
int len = 0; |
|||
for (int i = 0; i < this.Frequencies.Length; i++) |
|||
{ |
|||
len += this.Frequencies[i] * this.Length[i]; |
|||
} |
|||
|
|||
return len; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Scan a literal or distance tree to determine the frequencies of the codes
|
|||
/// in the bit length tree.
|
|||
/// </summary>
|
|||
public void CalcBLFreq(Tree blTree) |
|||
{ |
|||
int max_count; /* max repeat count */ |
|||
int min_count; /* min repeat count */ |
|||
int count; /* repeat count of the current code */ |
|||
int curlen = -1; /* length of current code */ |
|||
|
|||
int i = 0; |
|||
while (i < this.NumberOfCodes) |
|||
{ |
|||
count = 1; |
|||
int nextlen = this.Length[i]; |
|||
if (nextlen == 0) |
|||
{ |
|||
max_count = 138; |
|||
min_count = 3; |
|||
} |
|||
else |
|||
{ |
|||
max_count = 6; |
|||
min_count = 3; |
|||
if (curlen != nextlen) |
|||
{ |
|||
blTree.Frequencies[nextlen]++; |
|||
count = 0; |
|||
} |
|||
} |
|||
|
|||
curlen = nextlen; |
|||
i++; |
|||
|
|||
while (i < this.NumberOfCodes && curlen == this.Length[i]) |
|||
{ |
|||
i++; |
|||
if (++count >= max_count) |
|||
{ |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (count < min_count) |
|||
{ |
|||
blTree.Frequencies[curlen] += (short)count; |
|||
} |
|||
else if (curlen != 0) |
|||
{ |
|||
blTree.Frequencies[Repeat3To6]++; |
|||
} |
|||
else if (count <= 10) |
|||
{ |
|||
blTree.Frequencies[Repeat3To10]++; |
|||
} |
|||
else |
|||
{ |
|||
blTree.Frequencies[Repeat11To138]++; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write tree values
|
|||
/// </summary>
|
|||
/// <param name="blTree">Tree to write</param>
|
|||
public void WriteTree(Tree blTree) |
|||
{ |
|||
int max_count; // max repeat count
|
|||
int min_count; // min repeat count
|
|||
int count; // repeat count of the current code
|
|||
int curlen = -1; // length of current code
|
|||
|
|||
int i = 0; |
|||
while (i < this.NumberOfCodes) |
|||
{ |
|||
count = 1; |
|||
int nextlen = this.Length[i]; |
|||
if (nextlen == 0) |
|||
{ |
|||
max_count = 138; |
|||
min_count = 3; |
|||
} |
|||
else |
|||
{ |
|||
max_count = 6; |
|||
min_count = 3; |
|||
if (curlen != nextlen) |
|||
{ |
|||
blTree.WriteSymbol(nextlen); |
|||
count = 0; |
|||
} |
|||
} |
|||
|
|||
curlen = nextlen; |
|||
i++; |
|||
|
|||
while (i < this.NumberOfCodes && curlen == this.Length[i]) |
|||
{ |
|||
i++; |
|||
if (++count >= max_count) |
|||
{ |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (count < min_count) |
|||
{ |
|||
while (count-- > 0) |
|||
{ |
|||
blTree.WriteSymbol(curlen); |
|||
} |
|||
} |
|||
else if (curlen != 0) |
|||
{ |
|||
blTree.WriteSymbol(Repeat3To6); |
|||
this.deflater.pending.WriteBits(count - 3, 2); |
|||
} |
|||
else if (count <= 10) |
|||
{ |
|||
blTree.WriteSymbol(Repeat3To10); |
|||
this.deflater.pending.WriteBits(count - 3, 3); |
|||
} |
|||
else |
|||
{ |
|||
blTree.WriteSymbol(Repeat11To138); |
|||
this.deflater.pending.WriteBits(count - 11, 7); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void BuildLength(int[] childs) |
|||
{ |
|||
this.Length = new byte[this.Frequencies.Length]; |
|||
int numNodes = childs.Length / 2; |
|||
int numLeafs = (numNodes + 1) / 2; |
|||
int overflow = 0; |
|||
|
|||
for (int i = 0; i < this.maxLength; i++) |
|||
{ |
|||
this.blCounts[i] = 0; |
|||
} |
|||
|
|||
// First calculate optimal bit lengths
|
|||
int[] lengths = new int[numNodes]; |
|||
lengths[numNodes - 1] = 0; |
|||
|
|||
for (int i = numNodes - 1; i >= 0; i--) |
|||
{ |
|||
if (childs[(2 * i) + 1] != -1) |
|||
{ |
|||
int bitLength = lengths[i] + 1; |
|||
if (bitLength > this.maxLength) |
|||
{ |
|||
bitLength = this.maxLength; |
|||
overflow++; |
|||
} |
|||
|
|||
lengths[childs[2 * i]] = lengths[childs[(2 * i) + 1]] = bitLength; |
|||
} |
|||
else |
|||
{ |
|||
// A leaf node
|
|||
int bitLength = lengths[i]; |
|||
this.blCounts[bitLength - 1]++; |
|||
this.Length[childs[2 * i]] = (byte)lengths[i]; |
|||
} |
|||
} |
|||
|
|||
if (overflow == 0) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
int incrBitLen = this.maxLength - 1; |
|||
do |
|||
{ |
|||
// Find the first bit length which could increase:
|
|||
while (this.blCounts[--incrBitLen] == 0) |
|||
{ |
|||
} |
|||
|
|||
// Move this node one down and remove a corresponding
|
|||
// number of overflow nodes.
|
|||
do |
|||
{ |
|||
this.blCounts[incrBitLen]--; |
|||
this.blCounts[++incrBitLen]++; |
|||
overflow -= 1 << (this.maxLength - 1 - incrBitLen); |
|||
} |
|||
while (overflow > 0 && incrBitLen < this.maxLength - 1); |
|||
} |
|||
while (overflow > 0); |
|||
|
|||
// We may have overshot above. Move some nodes from maxLength to
|
|||
// maxLength-1 in that case.
|
|||
this.blCounts[this.maxLength - 1] += overflow; |
|||
this.blCounts[this.maxLength - 2] -= overflow; |
|||
|
|||
// Now recompute all bit lengths, scanning in increasing
|
|||
// frequency. It is simpler to reconstruct all lengths instead of
|
|||
// fixing only the wrong ones. This idea is taken from 'ar'
|
|||
// written by Haruhiko Okumura.
|
|||
// The nodes were inserted with decreasing frequency into the childs
|
|||
// array.
|
|||
int nodePtr = 2 * numLeafs; |
|||
for (int bits = this.maxLength; bits != 0; bits--) |
|||
{ |
|||
int n = this.blCounts[bits - 1]; |
|||
while (n > 0) |
|||
{ |
|||
int childPtr = 2 * childs[nodePtr++]; |
|||
if (childs[childPtr + 1] == -1) |
|||
{ |
|||
// We found another leaf
|
|||
this.Length[childs[childPtr]] = (byte)bits; |
|||
n--; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Pending buffer to use
|
|||
/// </summary>
|
|||
public DeflaterPending pending; |
|||
|
|||
Tree literalTree; |
|||
Tree distTree; |
|||
Tree blTree; |
|||
|
|||
// Buffer for distances
|
|||
short[] d_buf; |
|||
byte[] l_buf; |
|||
int last_lit; |
|||
int extra_bits; |
|||
|
|||
static DeflaterHuffman() |
|||
{ |
|||
// See RFC 1951 3.2.6
|
|||
// Literal codes
|
|||
staticLCodes = new short[LiteralCount]; |
|||
staticLLength = new byte[LiteralCount]; |
|||
|
|||
int i = 0; |
|||
while (i < 144) |
|||
{ |
|||
staticLCodes[i] = BitReverse((0x030 + i) << 8); |
|||
staticLLength[i++] = 8; |
|||
} |
|||
|
|||
while (i < 256) |
|||
{ |
|||
staticLCodes[i] = BitReverse((0x190 - 144 + i) << 7); |
|||
staticLLength[i++] = 9; |
|||
} |
|||
|
|||
while (i < 280) |
|||
{ |
|||
staticLCodes[i] = BitReverse((0x000 - 256 + i) << 9); |
|||
staticLLength[i++] = 7; |
|||
} |
|||
|
|||
while (i < LiteralCount) |
|||
{ |
|||
staticLCodes[i] = BitReverse((0x0c0 - 280 + i) << 8); |
|||
staticLLength[i++] = 8; |
|||
} |
|||
|
|||
// Distance codes
|
|||
staticDCodes = new short[DistanceCodeCount]; |
|||
staticDLength = new byte[DistanceCodeCount]; |
|||
for (i = 0; i < DistanceCodeCount; i++) |
|||
{ |
|||
staticDCodes[i] = BitReverse(i << 11); |
|||
staticDLength[i] = 5; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterHuffman"/> class with a pending buffer.
|
|||
/// </summary>
|
|||
/// <param name="pending">Pending buffer to use</param>
|
|||
public DeflaterHuffman(DeflaterPending pending) |
|||
{ |
|||
this.pending = pending; |
|||
|
|||
this.literalTree = new Tree(this, LiteralCount, 257, 15); |
|||
this.distTree = new Tree(this, DistanceCodeCount, 1, 15); |
|||
this.blTree = new Tree(this, BitLengthCount, 4, 7); |
|||
|
|||
this.d_buf = new short[Buffersize]; |
|||
this.l_buf = new byte[Buffersize]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reverse the bits of a 16 bit value.
|
|||
/// </summary>
|
|||
/// <param name="toReverse">Value to reverse bits</param>
|
|||
/// <returns>Value with bits reversed</returns>
|
|||
public static short BitReverse(int toReverse) |
|||
{ |
|||
return (short)(Bit4Reverse[toReverse & 0xF] << 12 | |
|||
Bit4Reverse[(toReverse >> 4) & 0xF] << 8 | |
|||
Bit4Reverse[(toReverse >> 8) & 0xF] << 4 | |
|||
Bit4Reverse[toReverse >> 12]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Resets the internal state
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
this.last_lit = 0; |
|||
this.extra_bits = 0; |
|||
this.literalTree.Reset(); |
|||
this.distTree.Reset(); |
|||
this.blTree.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write all trees to pending buffer
|
|||
/// </summary>
|
|||
/// <param name="blTreeCodes">The number/rank of treecodes to send.</param>
|
|||
public void SendAllTrees(int blTreeCodes) |
|||
{ |
|||
this.blTree.BuildCodes(); |
|||
this.literalTree.BuildCodes(); |
|||
this.distTree.BuildCodes(); |
|||
this.pending.WriteBits(this.literalTree.NumberOfCodes - 257, 5); |
|||
this.pending.WriteBits(this.distTree.NumberOfCodes - 1, 5); |
|||
this.pending.WriteBits(blTreeCodes - 4, 4); |
|||
|
|||
for (int rank = 0; rank < blTreeCodes; rank++) |
|||
{ |
|||
this.pending.WriteBits(this.blTree.Length[BitLengthOrder[rank]], 3); |
|||
} |
|||
|
|||
this.literalTree.WriteTree(this.blTree); |
|||
this.distTree.WriteTree(this.blTree); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Compress current buffer writing data to pending buffer
|
|||
/// </summary>
|
|||
public void CompressBlock() |
|||
{ |
|||
for (int i = 0; i < this.last_lit; i++) |
|||
{ |
|||
int litlen = this.l_buf[i] & 0xff; |
|||
int dist = this.d_buf[i]; |
|||
if (dist-- != 0) |
|||
{ |
|||
int lc = Lcode(litlen); |
|||
this.literalTree.WriteSymbol(lc); |
|||
|
|||
int bits = (lc - 261) / 4; |
|||
if (bits > 0 && bits <= 5) |
|||
{ |
|||
this.pending.WriteBits(litlen & ((1 << bits) - 1), bits); |
|||
} |
|||
|
|||
int dc = Dcode(dist); |
|||
this.distTree.WriteSymbol(dc); |
|||
|
|||
bits = (dc / 2) - 1; |
|||
if (bits > 0) |
|||
{ |
|||
this.pending.WriteBits(dist & ((1 << bits) - 1), bits); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
this.literalTree.WriteSymbol(litlen); |
|||
} |
|||
} |
|||
|
|||
this.literalTree.WriteSymbol(Eof); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flush block to output with no compression
|
|||
/// </summary>
|
|||
/// <param name="stored">Data to write</param>
|
|||
/// <param name="storedOffset">Index of first byte to write</param>
|
|||
/// <param name="storedLength">Count of bytes to write</param>
|
|||
/// <param name="lastBlock">True if this is the last block</param>
|
|||
public void FlushStoredBlock(byte[] stored, int storedOffset, int storedLength, bool lastBlock) |
|||
{ |
|||
this.pending.WriteBits((DeflaterConstants.StoredBlock << 1) + (lastBlock ? 1 : 0), 3); |
|||
this.pending.AlignToByte(); |
|||
this.pending.WriteShort(storedLength); |
|||
this.pending.WriteShort(~storedLength); |
|||
this.pending.WriteBlock(stored, storedOffset, storedLength); |
|||
this.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flush block to output with compression
|
|||
/// </summary>
|
|||
/// <param name="stored">Data to flush</param>
|
|||
/// <param name="storedOffset">Index of first byte to flush</param>
|
|||
/// <param name="storedLength">Count of bytes to flush</param>
|
|||
/// <param name="lastBlock">True if this is the last block</param>
|
|||
public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool lastBlock) |
|||
{ |
|||
this.literalTree.Frequencies[Eof]++; |
|||
|
|||
// Build trees
|
|||
this.literalTree.BuildTree(); |
|||
this.distTree.BuildTree(); |
|||
|
|||
// Calculate bitlen frequency
|
|||
this.literalTree.CalcBLFreq(this.blTree); |
|||
this.distTree.CalcBLFreq(this.blTree); |
|||
|
|||
// Build bitlen tree
|
|||
this.blTree.BuildTree(); |
|||
|
|||
int blTreeCodes = 4; |
|||
for (int i = 18; i > blTreeCodes; i--) |
|||
{ |
|||
if (this.blTree.Length[BitLengthOrder[i]] > 0) |
|||
{ |
|||
blTreeCodes = i + 1; |
|||
} |
|||
} |
|||
|
|||
int opt_len = 14 + (blTreeCodes * 3) + this.blTree.GetEncodedLength() + |
|||
this.literalTree.GetEncodedLength() + this.distTree.GetEncodedLength() + |
|||
this.extra_bits; |
|||
|
|||
int static_len = this.extra_bits; |
|||
for (int i = 0; i < LiteralCount; i++) |
|||
{ |
|||
static_len += this.literalTree.Frequencies[i] * staticLLength[i]; |
|||
} |
|||
|
|||
for (int i = 0; i < DistanceCodeCount; i++) |
|||
{ |
|||
static_len += this.distTree.Frequencies[i] * staticDLength[i]; |
|||
} |
|||
|
|||
if (opt_len >= static_len) |
|||
{ |
|||
// Force static trees
|
|||
opt_len = static_len; |
|||
} |
|||
|
|||
if (storedOffset >= 0 && storedLength + 4 < opt_len >> 3) |
|||
{ |
|||
// Store Block
|
|||
this.FlushStoredBlock(stored, storedOffset, storedLength, lastBlock); |
|||
} |
|||
else if (opt_len == static_len) |
|||
{ |
|||
// Encode with static tree
|
|||
this.pending.WriteBits((DeflaterConstants.StaticTrees << 1) + (lastBlock ? 1 : 0), 3); |
|||
this.literalTree.SetStaticCodes(staticLCodes, staticLLength); |
|||
this.distTree.SetStaticCodes(staticDCodes, staticDLength); |
|||
this.CompressBlock(); |
|||
this.Reset(); |
|||
} |
|||
else |
|||
{ |
|||
// Encode with dynamic tree
|
|||
this.pending.WriteBits((DeflaterConstants.DynTrees << 1) + (lastBlock ? 1 : 0), 3); |
|||
this.SendAllTrees(blTreeCodes); |
|||
this.CompressBlock(); |
|||
this.Reset(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get value indicating if internal buffer is full
|
|||
/// </summary>
|
|||
/// <returns>true if buffer is full</returns>
|
|||
public bool IsFull() |
|||
{ |
|||
return this.last_lit >= Buffersize; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Add literal to buffer
|
|||
/// </summary>
|
|||
/// <param name="literal">Literal value to add to buffer.</param>
|
|||
/// <returns>Value indicating internal buffer is full</returns>
|
|||
public bool TallyLit(int literal) |
|||
{ |
|||
this.d_buf[this.last_lit] = 0; |
|||
this.l_buf[this.last_lit++] = (byte)literal; |
|||
this.literalTree.Frequencies[literal]++; |
|||
return this.IsFull(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Add distance code and length to literal and distance trees
|
|||
/// </summary>
|
|||
/// <param name="distance">Distance code</param>
|
|||
/// <param name="length">Length</param>
|
|||
/// <returns>Value indicating if internal buffer is full</returns>
|
|||
public bool TallyDist(int distance, int length) |
|||
{ |
|||
this.d_buf[this.last_lit] = (short)distance; |
|||
this.l_buf[this.last_lit++] = (byte)(length - 3); |
|||
|
|||
int lc = Lcode(length - 3); |
|||
this.literalTree.Frequencies[lc]++; |
|||
if (lc >= 265 && lc < 285) |
|||
{ |
|||
this.extra_bits += (lc - 261) / 4; |
|||
} |
|||
|
|||
int dc = Dcode(distance - 1); |
|||
this.distTree.Frequencies[dc]++; |
|||
if (dc >= 4) |
|||
{ |
|||
this.extra_bits += (dc / 2) - 1; |
|||
} |
|||
|
|||
return this.IsFull(); |
|||
} |
|||
|
|||
private static int Lcode(int length) |
|||
{ |
|||
if (length == 255) |
|||
{ |
|||
return 285; |
|||
} |
|||
|
|||
int code = 257; |
|||
while (length >= 8) |
|||
{ |
|||
code += 4; |
|||
length >>= 1; |
|||
} |
|||
|
|||
return code + length; |
|||
} |
|||
|
|||
private static int Dcode(int distance) |
|||
{ |
|||
int code = 0; |
|||
while (distance >= 4) |
|||
{ |
|||
code += 2; |
|||
distance >>= 1; |
|||
} |
|||
|
|||
return code + distance; |
|||
} |
|||
} |
|||
} |
|||
@ -1,446 +0,0 @@ |
|||
// <copyright file="DeflaterOutputStream.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
/// <summary>
|
|||
/// A special stream deflating or compressing the bytes that are
|
|||
/// written to it. It uses a Deflater to perform actual deflating.<br/>
|
|||
/// Authors of the original java version : Tom Tromey, Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class DeflaterOutputStream : Stream |
|||
{ |
|||
/// <summary>
|
|||
/// The deflater which is used to deflate the stream.
|
|||
/// </summary>
|
|||
private readonly Deflater deflater; |
|||
|
|||
/// <summary>
|
|||
/// Base stream the deflater depends on.
|
|||
/// </summary>
|
|||
private readonly Stream baseOutputStream; |
|||
|
|||
/// <summary>
|
|||
/// This buffer is used temporarily to retrieve the bytes from the
|
|||
/// deflater and write them to the underlying output stream.
|
|||
/// </summary>
|
|||
private readonly byte[] bytebuffer; |
|||
|
|||
/// <summary>
|
|||
/// The password
|
|||
/// </summary>
|
|||
private string password; |
|||
|
|||
/// <summary>
|
|||
/// The keys
|
|||
/// </summary>
|
|||
private uint[] keys; |
|||
|
|||
/// <summary>
|
|||
/// Whether the stream is closed
|
|||
/// </summary>
|
|||
private bool isClosed; |
|||
|
|||
/// <summary>
|
|||
/// Whether dispose should close the underlying stream.
|
|||
/// </summary>
|
|||
private bool isStreamOwner = true; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterOutputStream"/> class
|
|||
/// with a default Deflater and default buffer size.
|
|||
/// </summary>
|
|||
/// <param name="baseOutputStream">
|
|||
/// the output stream where deflated output should be written.
|
|||
/// </param>
|
|||
public DeflaterOutputStream(Stream baseOutputStream) |
|||
: this(baseOutputStream, new Deflater(), 512) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterOutputStream"/> class
|
|||
/// with the given Deflater and default buffer size.
|
|||
/// </summary>
|
|||
/// <param name="baseOutputStream">
|
|||
/// the output stream where deflated output should be written.
|
|||
/// </param>
|
|||
/// <param name="deflater">
|
|||
/// the underlying deflater.
|
|||
/// </param>
|
|||
public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater) |
|||
: this(baseOutputStream, deflater, 512) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterOutputStream"/> class
|
|||
/// with the given Deflater and buffer size.
|
|||
/// </summary>
|
|||
/// <param name="baseOutputStream">
|
|||
/// The output stream where deflated output is written.
|
|||
/// </param>
|
|||
/// <param name="deflater">
|
|||
/// The underlying deflater to use
|
|||
/// </param>
|
|||
/// <param name="bufferSize">
|
|||
/// The buffer size in bytes to use when deflating (minimum value 512)
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentOutOfRangeException">buffersize is less than or equal to zero.</exception>
|
|||
/// <exception cref="ArgumentException">baseOutputStream does not support writing.</exception>
|
|||
/// <exception cref="ArgumentNullException">deflater instance is null.</exception>
|
|||
public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize) |
|||
{ |
|||
if (baseOutputStream == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(baseOutputStream)); |
|||
} |
|||
|
|||
if (baseOutputStream.CanWrite == false) |
|||
{ |
|||
throw new ArgumentException("Must support writing", nameof(baseOutputStream)); |
|||
} |
|||
|
|||
if (deflater == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(deflater)); |
|||
} |
|||
|
|||
if (bufferSize < 512) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(bufferSize)); |
|||
} |
|||
|
|||
this.baseOutputStream = baseOutputStream; |
|||
this.bytebuffer = new byte[bufferSize]; |
|||
this.deflater = deflater; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get/set flag indicating ownership of the underlying stream.
|
|||
/// When the flag is true <see cref="Dispose"></see> will close the underlying stream also.
|
|||
/// </summary>
|
|||
public bool IsStreamOwner |
|||
{ |
|||
get { return this.isStreamOwner; } |
|||
set { this.isStreamOwner = value; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Allows client to determine if an entry can be patched after its added
|
|||
/// </summary>
|
|||
public bool CanPatchEntries => this.baseOutputStream.CanSeek; |
|||
|
|||
/// <summary>
|
|||
/// Get/set the password used for encryption.
|
|||
/// </summary>
|
|||
/// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
|
|||
public string Password |
|||
{ |
|||
get |
|||
{ |
|||
return this.password; |
|||
} |
|||
|
|||
set |
|||
{ |
|||
if ((value != null) && (value.Length == 0)) |
|||
{ |
|||
this.password = null; |
|||
} |
|||
else |
|||
{ |
|||
this.password = value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets value indicating stream can be read from
|
|||
/// </summary>
|
|||
public override bool CanRead => false; |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating if seeking is supported for this stream
|
|||
/// This property always returns false
|
|||
/// </summary>
|
|||
public override bool CanSeek => false; |
|||
|
|||
/// <summary>
|
|||
/// Get value indicating if this stream supports writing
|
|||
/// </summary>
|
|||
public override bool CanWrite => this.baseOutputStream.CanWrite; |
|||
|
|||
/// <summary>
|
|||
/// Get current length of stream
|
|||
/// </summary>
|
|||
public override long Length => this.baseOutputStream.Length; |
|||
|
|||
/// <summary>
|
|||
/// Gets the current position within the stream.
|
|||
/// </summary>
|
|||
/// <exception cref="NotSupportedException">Any attempt to set position</exception>
|
|||
public override long Position |
|||
{ |
|||
get |
|||
{ |
|||
return this.baseOutputStream.Position; |
|||
} |
|||
|
|||
set |
|||
{ |
|||
throw new NotSupportedException("Position property not supported"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Finishes the stream by calling finish() on the deflater.
|
|||
/// </summary>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// Not all input is deflated
|
|||
/// </exception>
|
|||
public virtual void Finish() |
|||
{ |
|||
this.deflater.Finish(); |
|||
while (!this.deflater.IsFinished) |
|||
{ |
|||
int len = this.deflater.Deflate(this.bytebuffer, 0, this.bytebuffer.Length); |
|||
if (len <= 0) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
if (this.keys != null) |
|||
{ |
|||
this.EncryptBlock(this.bytebuffer, 0, len); |
|||
} |
|||
|
|||
this.baseOutputStream.Write(this.bytebuffer, 0, len); |
|||
} |
|||
|
|||
if (!this.deflater.IsFinished) |
|||
{ |
|||
throw new ImageFormatException("Can't deflate all input?"); |
|||
} |
|||
|
|||
this.baseOutputStream.Flush(); |
|||
this.keys = null; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the current position of this stream to the given value. Not supported by this class!
|
|||
/// </summary>
|
|||
/// <param name="offset">The offset relative to the <paramref name="origin"/> to seek.</param>
|
|||
/// <param name="origin">The <see cref="SeekOrigin"/> to seek from.</param>
|
|||
/// <returns>The new position in the stream.</returns>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override long Seek(long offset, SeekOrigin origin) |
|||
{ |
|||
throw new NotSupportedException("DeflaterOutputStream Seek not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the length of this stream to the given value. Not supported by this class!
|
|||
/// </summary>
|
|||
/// <param name="value">The new stream length.</param>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override void SetLength(long value) |
|||
{ |
|||
throw new NotSupportedException("DeflaterOutputStream SetLength not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a byte from stream advancing position by one
|
|||
/// </summary>
|
|||
/// <returns>The byte read cast to an int. THe value is -1 if at the end of the stream.</returns>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override int ReadByte() |
|||
{ |
|||
throw new NotSupportedException("DeflaterOutputStream ReadByte not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a block of bytes from stream
|
|||
/// </summary>
|
|||
/// <param name="buffer">The buffer to store read data in.</param>
|
|||
/// <param name="offset">The offset to start storing at.</param>
|
|||
/// <param name="count">The maximum number of bytes to read.</param>
|
|||
/// <returns>The actual number of bytes read. Zero if end of stream is detected.</returns>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override int Read(byte[] buffer, int offset, int count) |
|||
{ |
|||
throw new NotSupportedException("DeflaterOutputStream Read not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the stream by calling <see cref="DeflaterOutputStream.Flush">Flush</see> on the deflater and then
|
|||
/// on the underlying stream. This ensures that all bytes are flushed.
|
|||
/// </summary>
|
|||
public override void Flush() |
|||
{ |
|||
this.deflater.Flush(); |
|||
this.Deflate(); |
|||
this.baseOutputStream.Flush(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes a single byte to the compressed output stream.
|
|||
/// </summary>
|
|||
/// <param name="value">
|
|||
/// The byte value.
|
|||
/// </param>
|
|||
public override void WriteByte(byte value) |
|||
{ |
|||
byte[] b = new byte[1]; |
|||
b[0] = value; |
|||
this.Write(b, 0, 1); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes bytes from an array to the compressed stream.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// The byte array
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// The offset into the byte array where to start.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// The number of bytes to write.
|
|||
/// </param>
|
|||
public override void Write(byte[] buffer, int offset, int count) |
|||
{ |
|||
this.deflater.SetInput(buffer, offset, count); |
|||
this.Deflate(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Encrypt a block of data
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// Data to encrypt. NOTE the original contents of the buffer are lost
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// Offset of first byte in buffer to encrypt
|
|||
/// </param>
|
|||
/// <param name="length">
|
|||
/// Number of bytes in buffer to encrypt
|
|||
/// </param>
|
|||
protected void EncryptBlock(byte[] buffer, int offset, int length) |
|||
{ |
|||
for (int i = offset; i < offset + length; ++i) |
|||
{ |
|||
byte oldbyte = buffer[i]; |
|||
buffer[i] ^= this.EncryptByte(); |
|||
this.UpdateKeys(oldbyte); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes encryption keys based on given <paramref name="pssword"/>.
|
|||
/// </summary>
|
|||
/// <param name="pssword">The password.</param>
|
|||
protected void InitializePassword(string pssword) |
|||
{ |
|||
this.keys = new uint[] |
|||
{ |
|||
0x12345678, |
|||
0x23456789, |
|||
0x34567890 |
|||
}; |
|||
|
|||
byte[] rawPassword = ZipConstants.ConvertToArray(pssword); |
|||
|
|||
foreach (byte b in rawPassword) |
|||
{ |
|||
this.UpdateKeys(b); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Encrypt a single byte
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The encrypted value
|
|||
/// </returns>
|
|||
protected byte EncryptByte() |
|||
{ |
|||
uint temp = (this.keys[2] & 0xFFFF) | 2; |
|||
return (byte)((temp * (temp ^ 1)) >> 8); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Update encryption keys
|
|||
/// </summary>
|
|||
/// <param name="ch">The character.</param>
|
|||
protected void UpdateKeys(byte ch) |
|||
{ |
|||
this.keys[0] = Crc32.ComputeCrc32(this.keys[0], ch); |
|||
this.keys[1] = this.keys[1] + (byte)this.keys[0]; |
|||
this.keys[1] = (this.keys[1] * 134775813) + 1; |
|||
this.keys[2] = Crc32.ComputeCrc32(this.keys[2], (byte)(this.keys[1] >> 24)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Deflates everything in the input buffers. This will call
|
|||
/// <code>def.deflate()</code> until all bytes from the input buffers
|
|||
/// are processed.
|
|||
/// </summary>
|
|||
protected void Deflate() |
|||
{ |
|||
while (!this.deflater.IsNeedingInput) |
|||
{ |
|||
int deflateCount = this.deflater.Deflate(this.bytebuffer, 0, this.bytebuffer.Length); |
|||
|
|||
if (deflateCount <= 0) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
if (this.keys != null) |
|||
{ |
|||
this.EncryptBlock(this.bytebuffer, 0, deflateCount); |
|||
} |
|||
|
|||
this.baseOutputStream.Write(this.bytebuffer, 0, deflateCount); |
|||
} |
|||
|
|||
if (!this.deflater.IsNeedingInput) |
|||
{ |
|||
throw new ImageFormatException("DeflaterOutputStream can't deflate all input?"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Calls <see cref="Finish"/> and closes the underlying
|
|||
/// stream when <see cref="IsStreamOwner"></see> is true.
|
|||
/// </summary>
|
|||
/// <param name="disposing">If true, the object gets disposed.</param>
|
|||
protected override void Dispose(bool disposing) |
|||
{ |
|||
base.Dispose(disposing); |
|||
if (disposing && !this.isClosed) |
|||
{ |
|||
this.isClosed = true; |
|||
|
|||
try |
|||
{ |
|||
this.Finish(); |
|||
this.keys = null; |
|||
} |
|||
finally |
|||
{ |
|||
if (this.isStreamOwner) |
|||
{ |
|||
this.baseOutputStream.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
// <copyright file="DeflaterPending.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// This class stores the pending output of the Deflater.
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class DeflaterPending : PendingBuffer |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DeflaterPending"/> class.
|
|||
/// Construct instance with default buffer size
|
|||
/// </summary>
|
|||
public DeflaterPending() |
|||
: base(DeflaterConstants.PendingBufSize) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,97 +0,0 @@ |
|||
// <copyright file="GeneralBitFlags.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Defines the contents of the general bit flags field for an archive entry.
|
|||
/// </summary>
|
|||
[Flags] |
|||
public enum GeneralBitFlags |
|||
{ |
|||
/// <summary>
|
|||
/// Bit 0 if set indicates that the file is encrypted
|
|||
/// </summary>
|
|||
Encrypted = 0x0001, |
|||
|
|||
/// <summary>
|
|||
/// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating)
|
|||
/// </summary>
|
|||
Method = 0x0006, |
|||
|
|||
/// <summary>
|
|||
/// Bit 3 if set indicates a trailing data desciptor is appended to the entry data
|
|||
/// </summary>
|
|||
Descriptor = 0x0008, |
|||
|
|||
/// <summary>
|
|||
/// Bit 4 is reserved for use with method 8 for enhanced deflation
|
|||
/// </summary>
|
|||
ReservedPKware4 = 0x0010, |
|||
|
|||
/// <summary>
|
|||
/// Bit 5 if set indicates the file contains Pkzip compressed patched data.
|
|||
/// Requires version 2.7 or greater.
|
|||
/// </summary>
|
|||
Patched = 0x0020, |
|||
|
|||
/// <summary>
|
|||
/// Bit 6 if set indicates strong encryption has been used for this entry.
|
|||
/// </summary>
|
|||
StrongEncryption = 0x0040, |
|||
|
|||
/// <summary>
|
|||
/// Bit 7 is currently unused
|
|||
/// </summary>
|
|||
Unused7 = 0x0080, |
|||
|
|||
/// <summary>
|
|||
/// Bit 8 is currently unused
|
|||
/// </summary>
|
|||
Unused8 = 0x0100, |
|||
|
|||
/// <summary>
|
|||
/// Bit 9 is currently unused
|
|||
/// </summary>
|
|||
Unused9 = 0x0200, |
|||
|
|||
/// <summary>
|
|||
/// Bit 10 is currently unused
|
|||
/// </summary>
|
|||
Unused10 = 0x0400, |
|||
|
|||
/// <summary>
|
|||
/// Bit 11 if set indicates the filename and
|
|||
/// comment fields for this file must be encoded using UTF-8.
|
|||
/// </summary>
|
|||
UnicodeText = 0x0800, |
|||
|
|||
/// <summary>
|
|||
/// Bit 12 is documented as being reserved by PKware for enhanced compression.
|
|||
/// </summary>
|
|||
EnhancedCompress = 0x1000, |
|||
|
|||
/// <summary>
|
|||
/// Bit 13 if set indicates that values in the local header are masked to hide
|
|||
/// their actual values, and the central directory is encrypted.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Used when encrypting the central directory contents.
|
|||
/// </remarks>
|
|||
HeaderMasked = 0x2000, |
|||
|
|||
/// <summary>
|
|||
/// Bit 14 is documented as being reserved for use by PKware
|
|||
/// </summary>
|
|||
ReservedPkware14 = 0x4000, |
|||
|
|||
/// <summary>
|
|||
/// Bit 15 is documented as being reserved for use by PKware
|
|||
/// </summary>
|
|||
ReservedPkware15 = 0x8000 |
|||
} |
|||
} |
|||
@ -1,843 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Inflater is used to decompress data that has been compressed according
|
|||
/// to the "deflate" standard described in rfc1951.
|
|||
///
|
|||
/// By default Zlib (rfc1950) headers and footers are expected in the input.
|
|||
/// You can use constructor <code> public Inflater(bool noHeader)</code> passing true
|
|||
/// if there is no Zlib header information
|
|||
///
|
|||
/// The usage is as following. First you have to set some input with
|
|||
/// <code>SetInput()</code>, then Inflate() it. If inflate doesn't
|
|||
/// inflate any bytes there may be three reasons:
|
|||
/// <ul>
|
|||
/// <li>IsNeedingInput() returns true because the input buffer is empty.
|
|||
/// You have to provide more input with <code>SetInput()</code>.
|
|||
/// NOTE: IsNeedingInput() also returns true when, the stream is finished.
|
|||
/// </li>
|
|||
/// <li>IsNeedingDictionary() returns true, you have to provide a preset
|
|||
/// dictionary with <code>SetDictionary()</code>.</li>
|
|||
/// <li>IsFinished returns true, the inflater has finished.</li>
|
|||
/// </ul>
|
|||
/// Once the first output byte is produced, a dictionary will not be
|
|||
/// needed at a later stage.
|
|||
///
|
|||
/// author of the original java version : John Leuner, Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class Inflater |
|||
{ |
|||
/// <summary>
|
|||
/// Copy lengths for literal codes 257..285
|
|||
/// </summary>
|
|||
private static readonly int[] CPLENS = |
|||
{ |
|||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
|||
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 |
|||
}; |
|||
|
|||
/// <summary>
|
|||
/// Extra bits for literal codes 257..285
|
|||
/// </summary>
|
|||
private static readonly int[] CPLEXT = |
|||
{ |
|||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
|||
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 |
|||
}; |
|||
|
|||
/// <summary>
|
|||
/// Copy offsets for distance codes 0..29
|
|||
/// </summary>
|
|||
private static readonly int[] CPDIST = |
|||
{ |
|||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
|||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
|||
8193, 12289, 16385, 24577 |
|||
}; |
|||
|
|||
/// <summary>
|
|||
/// Extra bits for distance codes
|
|||
/// </summary>
|
|||
private static readonly int[] CPDEXT = |
|||
{ |
|||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
|||
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
|||
12, 12, 13, 13 |
|||
}; |
|||
|
|||
/// <summary>
|
|||
/// These are the possible states for an inflater
|
|||
/// </summary>
|
|||
private const int DECODE_HEADER = 0; |
|||
private const int DECODE_DICT = 1; |
|||
private const int DECODE_BLOCKS = 2; |
|||
private const int DECODE_STORED_LEN1 = 3; |
|||
private const int DECODE_STORED_LEN2 = 4; |
|||
private const int DECODE_STORED = 5; |
|||
private const int DECODE_DYN_HEADER = 6; |
|||
private const int DECODE_HUFFMAN = 7; |
|||
private const int DECODE_HUFFMAN_LENBITS = 8; |
|||
private const int DECODE_HUFFMAN_DIST = 9; |
|||
private const int DECODE_HUFFMAN_DISTBITS = 10; |
|||
private const int DECODE_CHKSUM = 11; |
|||
private const int FINISHED = 12; |
|||
|
|||
/// <summary>
|
|||
/// This variable contains the current state.
|
|||
/// </summary>
|
|||
private int mode; |
|||
|
|||
/// <summary>
|
|||
/// The adler checksum of the dictionary or of the decompressed
|
|||
/// stream, as it is written in the header resp. footer of the
|
|||
/// compressed stream.
|
|||
/// Only valid if mode is DECODE_DICT or DECODE_CHKSUM.
|
|||
/// </summary>
|
|||
private int readAdler; |
|||
|
|||
/// <summary>
|
|||
/// The number of bits needed to complete the current state. This
|
|||
/// is valid, if mode is DECODE_DICT, DECODE_CHKSUM,
|
|||
/// DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS.
|
|||
/// </summary>
|
|||
private int neededBits; |
|||
private int repLength; |
|||
private int repDist; |
|||
private int uncomprLen; |
|||
|
|||
/// <summary>
|
|||
/// True, if the last block flag was set in the last block of the
|
|||
/// inflated stream. This means that the stream ends after the
|
|||
/// current block.
|
|||
/// </summary>
|
|||
private bool isLastBlock; |
|||
|
|||
/// <summary>
|
|||
/// The total number of inflated bytes.
|
|||
/// </summary>
|
|||
private long totalOut; |
|||
|
|||
/// <summary>
|
|||
/// The total number of bytes set with setInput(). This is not the
|
|||
/// value returned by the TotalIn property, since this also includes the
|
|||
/// unprocessed input.
|
|||
/// </summary>
|
|||
private long totalIn; |
|||
|
|||
/// <summary>
|
|||
/// This variable stores the noHeader flag that was given to the constructor.
|
|||
/// True means, that the inflated stream doesn't contain a Zlib header or
|
|||
/// footer.
|
|||
/// </summary>
|
|||
private bool noHeader; |
|||
|
|||
private StreamManipulator input; |
|||
private OutputWindow outputWindow; |
|||
private InflaterDynHeader dynHeader; |
|||
private InflaterHuffmanTree litlenTree, distTree; |
|||
private Adler32 adler; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Inflater"/> class.
|
|||
/// RFC1950/Zlib headers and footers will be expected in the input data
|
|||
/// </summary>
|
|||
public Inflater() |
|||
: this(false) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Inflater"/> class.
|
|||
/// </summary>
|
|||
/// <param name="noHeader">
|
|||
/// True if no RFC1950/Zlib header and footer fields are expected in the input data
|
|||
///
|
|||
/// This is used for GZIPed/Zipped input.
|
|||
///
|
|||
/// For compatibility with
|
|||
/// Sun JDK you should provide one byte of input more than needed in
|
|||
/// this case.
|
|||
/// </param>
|
|||
public Inflater(bool noHeader) |
|||
{ |
|||
this.noHeader = noHeader; |
|||
this.adler = new Adler32(); |
|||
this.input = new StreamManipulator(); |
|||
this.outputWindow = new OutputWindow(); |
|||
this.mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Resets the inflater so that a new stream can be decompressed. All
|
|||
/// pending input and output will be discarded.
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
this.mode = this.noHeader ? DECODE_BLOCKS : DECODE_HEADER; |
|||
this.totalIn = 0; |
|||
this.totalOut = 0; |
|||
this.input.Reset(); |
|||
this.outputWindow.Reset(); |
|||
this.dynHeader = null; |
|||
this.litlenTree = null; |
|||
this.distTree = null; |
|||
this.isLastBlock = false; |
|||
this.adler.Reset(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Decodes a zlib/RFC1950 header.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// False if more input is needed.
|
|||
/// </returns>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// The header is invalid.
|
|||
/// </exception>
|
|||
private bool DecodeHeader() |
|||
{ |
|||
int header = this.input.PeekBits(16); |
|||
if (header < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(16); |
|||
|
|||
// The header is written in "wrong" byte order
|
|||
header = ((header << 8) | (header >> 8)) & 0xffff; |
|||
if (header % 31 != 0) |
|||
{ |
|||
throw new ImageFormatException("Header checksum illegal"); |
|||
} |
|||
|
|||
if ((header & 0x0f00) != (Deflater.Deflated << 8)) |
|||
{ |
|||
throw new ImageFormatException("Compression Method unknown"); |
|||
} |
|||
|
|||
/* Maximum size of the backwards window in bits. |
|||
* We currently ignore this, but we could use it to make the |
|||
* inflater window more space efficient. On the other hand the |
|||
* full window (15 bits) is needed most times, anyway. |
|||
int max_wbits = ((header & 0x7000) >> 12) + 8; |
|||
*/ |
|||
|
|||
if ((header & 0x0020) == 0) |
|||
{ // Dictionary flag?
|
|||
this.mode = DECODE_BLOCKS; |
|||
} |
|||
else |
|||
{ |
|||
this.mode = DECODE_DICT; |
|||
this.neededBits = 32; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Decodes the dictionary checksum after the deflate header.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// False if more input is needed.
|
|||
/// </returns>
|
|||
private bool DecodeDict() |
|||
{ |
|||
while (this.neededBits > 0) |
|||
{ |
|||
int dictByte = this.input.PeekBits(8); |
|||
if (dictByte < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(8); |
|||
this.readAdler = (this.readAdler << 8) | dictByte; |
|||
this.neededBits -= 8; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Decodes the huffman encoded symbols in the input stream.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// false if more input is needed, true if output window is
|
|||
/// full or the current block ends.
|
|||
/// </returns>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// if deflated stream is invalid.
|
|||
/// </exception>
|
|||
private bool DecodeHuffman() |
|||
{ |
|||
int free = this.outputWindow.GetFreeSpace(); |
|||
while (free >= 258) |
|||
{ |
|||
int symbol; |
|||
switch (this.mode) |
|||
{ |
|||
case DECODE_HUFFMAN: |
|||
// This is the inner loop so it is optimized a bit
|
|||
while (((symbol = this.litlenTree.GetSymbol(this.input)) & ~0xff) == 0) |
|||
{ |
|||
this.outputWindow.Write(symbol); |
|||
if (--free < 258) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
if (symbol < 257) |
|||
{ |
|||
if (symbol < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
else |
|||
{ |
|||
// symbol == 256: end of block
|
|||
this.distTree = null; |
|||
this.litlenTree = null; |
|||
this.mode = DECODE_BLOCKS; |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
try |
|||
{ |
|||
this.repLength = CPLENS[symbol - 257]; |
|||
this.neededBits = CPLEXT[symbol - 257]; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
throw new ImageFormatException("Illegal rep length code"); |
|||
} |
|||
|
|||
goto case DECODE_HUFFMAN_LENBITS; // fall through
|
|||
|
|||
case DECODE_HUFFMAN_LENBITS: |
|||
if (this.neededBits > 0) |
|||
{ |
|||
this.mode = DECODE_HUFFMAN_LENBITS; |
|||
int i = this.input.PeekBits(this.neededBits); |
|||
if (i < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(this.neededBits); |
|||
this.repLength += i; |
|||
} |
|||
|
|||
this.mode = DECODE_HUFFMAN_DIST; |
|||
goto case DECODE_HUFFMAN_DIST; // fall through
|
|||
|
|||
case DECODE_HUFFMAN_DIST: |
|||
symbol = this.distTree.GetSymbol(this.input); |
|||
if (symbol < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
this.repDist = CPDIST[symbol]; |
|||
this.neededBits = CPDEXT[symbol]; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
throw new ImageFormatException("Illegal rep dist code"); |
|||
} |
|||
|
|||
goto case DECODE_HUFFMAN_DISTBITS; // fall through
|
|||
|
|||
case DECODE_HUFFMAN_DISTBITS: |
|||
if (this.neededBits > 0) |
|||
{ |
|||
this.mode = DECODE_HUFFMAN_DISTBITS; |
|||
int i = this.input.PeekBits(this.neededBits); |
|||
if (i < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(this.neededBits); |
|||
this.repDist += i; |
|||
} |
|||
|
|||
this.outputWindow.Repeat(this.repLength, this.repDist); |
|||
free -= this.repLength; |
|||
this.mode = DECODE_HUFFMAN; |
|||
break; |
|||
|
|||
default: |
|||
throw new ImageFormatException("Inflater unknown mode"); |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Decodes the adler checksum after the deflate stream.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// false if more input is needed.
|
|||
/// </returns>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// If checksum doesn't match.
|
|||
/// </exception>
|
|||
private bool DecodeChksum() |
|||
{ |
|||
while (this.neededBits > 0) |
|||
{ |
|||
int chkByte = this.input.PeekBits(8); |
|||
if (chkByte < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(8); |
|||
this.readAdler = (this.readAdler << 8) | chkByte; |
|||
this.neededBits -= 8; |
|||
} |
|||
|
|||
if ((int)this.adler.Value != this.readAdler) |
|||
{ |
|||
throw new ImageFormatException("Adler chksum doesn't match: " + (int)this.adler.Value + " vs. " + this.readAdler); |
|||
} |
|||
|
|||
this.mode = FINISHED; |
|||
return false; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Decodes the deflated stream.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// false if more input is needed, or if finished.
|
|||
/// </returns>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// if deflated stream is invalid.
|
|||
/// </exception>
|
|||
private bool Decode() |
|||
{ |
|||
switch (this.mode) |
|||
{ |
|||
case DECODE_HEADER: |
|||
return this.DecodeHeader(); |
|||
|
|||
case DECODE_DICT: |
|||
return this.DecodeDict(); |
|||
|
|||
case DECODE_CHKSUM: |
|||
return this.DecodeChksum(); |
|||
|
|||
case DECODE_BLOCKS: |
|||
if (this.isLastBlock) |
|||
{ |
|||
if (this.noHeader) |
|||
{ |
|||
this.mode = FINISHED; |
|||
return false; |
|||
} |
|||
else |
|||
{ |
|||
this.input.SkipToByteBoundary(); |
|||
this.neededBits = 32; |
|||
this.mode = DECODE_CHKSUM; |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
int type = this.input.PeekBits(3); |
|||
if (type < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(3); |
|||
|
|||
if ((type & 1) != 0) |
|||
{ |
|||
this.isLastBlock = true; |
|||
} |
|||
|
|||
switch (type >> 1) |
|||
{ |
|||
case DeflaterConstants.StoredBlock: |
|||
this.input.SkipToByteBoundary(); |
|||
this.mode = DECODE_STORED_LEN1; |
|||
break; |
|||
case DeflaterConstants.StaticTrees: |
|||
this.litlenTree = InflaterHuffmanTree.defLitLenTree; |
|||
this.distTree = InflaterHuffmanTree.defDistTree; |
|||
this.mode = DECODE_HUFFMAN; |
|||
break; |
|||
case DeflaterConstants.DynTrees: |
|||
this.dynHeader = new InflaterDynHeader(); |
|||
this.mode = DECODE_DYN_HEADER; |
|||
break; |
|||
default: |
|||
throw new ImageFormatException("Unknown block type " + type); |
|||
} |
|||
|
|||
return true; |
|||
|
|||
case DECODE_STORED_LEN1: |
|||
{ |
|||
if ((this.uncomprLen = this.input.PeekBits(16)) < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(16); |
|||
this.mode = DECODE_STORED_LEN2; |
|||
} |
|||
|
|||
goto case DECODE_STORED_LEN2; // fall through
|
|||
|
|||
case DECODE_STORED_LEN2: |
|||
{ |
|||
int nlen = this.input.PeekBits(16); |
|||
if (nlen < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.input.DropBits(16); |
|||
if (nlen != (this.uncomprLen ^ 0xffff)) |
|||
{ |
|||
throw new ImageFormatException("broken uncompressed block"); |
|||
} |
|||
|
|||
this.mode = DECODE_STORED; |
|||
} |
|||
|
|||
goto case DECODE_STORED; // fall through
|
|||
|
|||
case DECODE_STORED: |
|||
{ |
|||
int more = this.outputWindow.CopyStored(this.input, this.uncomprLen); |
|||
this.uncomprLen -= more; |
|||
if (this.uncomprLen == 0) |
|||
{ |
|||
this.mode = DECODE_BLOCKS; |
|||
return true; |
|||
} |
|||
|
|||
return !this.input.IsNeedingInput; |
|||
} |
|||
|
|||
case DECODE_DYN_HEADER: |
|||
if (!this.dynHeader.Decode(this.input)) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
this.litlenTree = this.dynHeader.BuildLitLenTree(); |
|||
this.distTree = this.dynHeader.BuildDistTree(); |
|||
this.mode = DECODE_HUFFMAN; |
|||
goto case DECODE_HUFFMAN; // fall through
|
|||
|
|||
case DECODE_HUFFMAN: |
|||
case DECODE_HUFFMAN_LENBITS: |
|||
case DECODE_HUFFMAN_DIST: |
|||
case DECODE_HUFFMAN_DISTBITS: |
|||
return this.DecodeHuffman(); |
|||
|
|||
case FINISHED: |
|||
return false; |
|||
|
|||
default: |
|||
throw new ImageFormatException("Inflater.Decode unknown mode"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the preset dictionary. This should only be called, if
|
|||
/// needsDictionary() returns true and it should set the same
|
|||
/// dictionary, that was used for deflating. The getAdler()
|
|||
/// function returns the checksum of the dictionary needed.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// The dictionary.
|
|||
/// </param>
|
|||
public void SetDictionary(byte[] buffer) |
|||
{ |
|||
this.SetDictionary(buffer, 0, buffer.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the preset dictionary. This should only be called, if
|
|||
/// needsDictionary() returns true and it should set the same
|
|||
/// dictionary, that was used for deflating. The getAdler()
|
|||
/// function returns the checksum of the dictionary needed.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// The dictionary.
|
|||
/// </param>
|
|||
/// <param name="index">
|
|||
/// The index into buffer where the dictionary starts.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// The number of bytes in the dictionary.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// No dictionary is needed.
|
|||
/// </exception>
|
|||
/// <exception cref="ImageFormatException">
|
|||
/// The adler checksum for the buffer is invalid
|
|||
/// </exception>
|
|||
public void SetDictionary(byte[] buffer, int index, int count) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
} |
|||
|
|||
if (index < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(index)); |
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(count)); |
|||
} |
|||
|
|||
if (!this.IsNeedingDictionary) |
|||
{ |
|||
throw new InvalidOperationException("Dictionary is not needed"); |
|||
} |
|||
|
|||
this.adler.Update(buffer, index, count); |
|||
|
|||
if ((int)this.adler.Value != this.readAdler) |
|||
{ |
|||
throw new ImageFormatException("Wrong adler checksum"); |
|||
} |
|||
|
|||
this.adler.Reset(); |
|||
this.outputWindow.CopyDict(buffer, index, count); |
|||
this.mode = DECODE_BLOCKS; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the input. This should only be called, if needsInput()
|
|||
/// returns true.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// the input.
|
|||
/// </param>
|
|||
public void SetInput(byte[] buffer) |
|||
{ |
|||
this.SetInput(buffer, 0, buffer.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the input. This should only be called, if needsInput()
|
|||
/// returns true.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// The source of input data
|
|||
/// </param>
|
|||
/// <param name="index">
|
|||
/// The index into buffer where the input starts.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// The number of bytes of input to use.
|
|||
/// </param>
|
|||
/// <exception cref="System.InvalidOperationException">
|
|||
/// No input is needed.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">
|
|||
/// The index and/or count are wrong.
|
|||
/// </exception>
|
|||
public void SetInput(byte[] buffer, int index, int count) |
|||
{ |
|||
this.input.SetInput(buffer, index, count); |
|||
this.totalIn += (long)count; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Inflates the compressed stream to the output buffer. If this
|
|||
/// returns 0, you should check, whether IsNeedingDictionary(),
|
|||
/// IsNeedingInput() or IsFinished() returns true, to determine why no
|
|||
/// further output is produced.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// the output buffer.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The number of bytes written to the buffer, 0 if no further
|
|||
/// output can be produced.
|
|||
/// </returns>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">
|
|||
/// if buffer has length 0.
|
|||
/// </exception>
|
|||
/// <exception cref="System.FormatException">
|
|||
/// if deflated stream is invalid.
|
|||
/// </exception>
|
|||
public int Inflate(byte[] buffer) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
} |
|||
|
|||
return this.Inflate(buffer, 0, buffer.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Inflates the compressed stream to the output buffer. If this
|
|||
/// returns 0, you should check, whether needsDictionary(),
|
|||
/// needsInput() or finished() returns true, to determine why no
|
|||
/// further output is produced.
|
|||
/// </summary>
|
|||
/// <param name="buffer">
|
|||
/// the output buffer.
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// the offset in buffer where storing starts.
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// the maximum number of bytes to output.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// the number of bytes written to the buffer, 0 if no further output can be produced.
|
|||
/// </returns>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">
|
|||
/// if count is less than 0.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentOutOfRangeException">
|
|||
/// if the index and / or count are wrong.
|
|||
/// </exception>
|
|||
/// <exception cref="System.FormatException">
|
|||
/// if deflated stream is invalid.
|
|||
/// </exception>
|
|||
public int Inflate(byte[] buffer, int offset, int count) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(count), "count cannot be negative"); |
|||
} |
|||
|
|||
if (offset < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(offset), "offset cannot be negative"); |
|||
} |
|||
|
|||
if (offset + count > buffer.Length) |
|||
{ |
|||
throw new ArgumentException("count exceeds buffer bounds"); |
|||
} |
|||
|
|||
// Special case: count may be zero
|
|||
if (count == 0) |
|||
{ |
|||
if (!this.IsFinished) |
|||
{ // -jr- 08-Nov-2003 INFLATE_BUG fix..
|
|||
this.Decode(); |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
int bytesCopied = 0; |
|||
|
|||
do |
|||
{ |
|||
if (this.mode != DECODE_CHKSUM) |
|||
{ |
|||
/* Don't give away any output, if we are waiting for the |
|||
* checksum in the input stream. |
|||
* |
|||
* With this trick we have always: |
|||
* IsNeedingInput() and not IsFinished() |
|||
* implies more output can be produced. |
|||
*/ |
|||
int more = this.outputWindow.CopyOutput(buffer, offset, count); |
|||
if (more > 0) |
|||
{ |
|||
this.adler.Update(buffer, offset, more); |
|||
offset += more; |
|||
bytesCopied += more; |
|||
this.totalOut += (long)more; |
|||
count -= more; |
|||
if (count == 0) |
|||
{ |
|||
return bytesCopied; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
while (this.Decode() || ((this.outputWindow.GetAvailable() > 0) && (this.mode != DECODE_CHKSUM))); |
|||
return bytesCopied; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns true, if the input buffer is empty.
|
|||
/// You should then call setInput().
|
|||
/// NOTE: This method also returns true when the stream is finished.
|
|||
/// </summary>
|
|||
public bool IsNeedingInput => this.input.IsNeedingInput; |
|||
|
|||
/// <summary>
|
|||
/// Returns true, if a preset dictionary is needed to inflate the input.
|
|||
/// </summary>
|
|||
public bool IsNeedingDictionary => this.mode == DECODE_DICT && this.neededBits == 0; |
|||
|
|||
/// <summary>
|
|||
/// Returns true, if the inflater has finished. This means, that no
|
|||
/// input is needed and no output can be produced.
|
|||
/// </summary>
|
|||
public bool IsFinished => this.mode == FINISHED && this.outputWindow.GetAvailable() == 0; |
|||
|
|||
/// <summary>
|
|||
/// Gets the adler checksum. This is either the checksum of all
|
|||
/// uncompressed bytes returned by inflate(), or if needsDictionary()
|
|||
/// returns true (and thus no output was yet produced) this is the
|
|||
/// adler checksum of the expected dictionary.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// the adler checksum.
|
|||
/// </returns>
|
|||
public int Adler => this.IsNeedingDictionary ? this.readAdler : (int)this.adler.Value; |
|||
|
|||
/// <summary>
|
|||
/// Gets the total number of output bytes returned by Inflate().
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// the total number of output bytes.
|
|||
/// </returns>
|
|||
public long TotalOut => this.totalOut; |
|||
|
|||
/// <summary>
|
|||
/// Gets the total number of processed compressed input bytes.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The total number of bytes of processed input bytes.
|
|||
/// </returns>
|
|||
public long TotalIn => this.totalIn - (long)this.RemainingInput; |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of unprocessed input bytes. Useful, if the end of the
|
|||
/// stream is reached and you want to further process the bytes after
|
|||
/// the deflate stream.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The number of bytes of the input which have not been processed.
|
|||
/// </returns>
|
|||
public int RemainingInput => this.input.AvailableBytes; // TODO: Should this be a long?
|
|||
} |
|||
} |
|||
@ -1,196 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
class InflaterDynHeader |
|||
{ |
|||
#region Constants
|
|||
const int LNUM = 0; |
|||
const int DNUM = 1; |
|||
const int BLNUM = 2; |
|||
const int BLLENS = 3; |
|||
const int LENS = 4; |
|||
const int REPS = 5; |
|||
|
|||
static readonly int[] repMin = { 3, 3, 11 }; |
|||
static readonly int[] repBits = { 2, 3, 7 }; |
|||
|
|||
static readonly int[] BL_ORDER = |
|||
{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; |
|||
|
|||
#endregion
|
|||
|
|||
#region Constructors
|
|||
public InflaterDynHeader() |
|||
{ |
|||
} |
|||
#endregion
|
|||
|
|||
public bool Decode(StreamManipulator input) |
|||
{ |
|||
decode_loop: |
|||
for (;;) |
|||
{ |
|||
switch (mode) |
|||
{ |
|||
case LNUM: |
|||
lnum = input.PeekBits(5); |
|||
if (lnum < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
lnum += 257; |
|||
input.DropBits(5); |
|||
// System.err.println("LNUM: "+lnum);
|
|||
mode = DNUM; |
|||
goto case DNUM; // fall through
|
|||
case DNUM: |
|||
dnum = input.PeekBits(5); |
|||
if (dnum < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
dnum++; |
|||
input.DropBits(5); |
|||
// System.err.println("DNUM: "+dnum);
|
|||
num = lnum + dnum; |
|||
litdistLens = new byte[num]; |
|||
mode = BLNUM; |
|||
goto case BLNUM; // fall through
|
|||
case BLNUM: |
|||
blnum = input.PeekBits(4); |
|||
if (blnum < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
blnum += 4; |
|||
input.DropBits(4); |
|||
blLens = new byte[19]; |
|||
ptr = 0; |
|||
// System.err.println("BLNUM: "+blnum);
|
|||
mode = BLLENS; |
|||
goto case BLLENS; // fall through
|
|||
case BLLENS: |
|||
while (ptr < blnum) |
|||
{ |
|||
int len = input.PeekBits(3); |
|||
if (len < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
input.DropBits(3); |
|||
// System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
|
|||
blLens[BL_ORDER[ptr]] = (byte)len; |
|||
ptr++; |
|||
} |
|||
blTree = new InflaterHuffmanTree(blLens); |
|||
blLens = null; |
|||
ptr = 0; |
|||
mode = LENS; |
|||
goto case LENS; // fall through
|
|||
case LENS: |
|||
{ |
|||
int symbol; |
|||
while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) |
|||
{ |
|||
/* Normal case: symbol in [0..15] */ |
|||
|
|||
// System.err.println("litdistLens["+ptr+"]: "+symbol);
|
|||
litdistLens[ptr++] = lastLen = (byte)symbol; |
|||
|
|||
if (ptr == num) |
|||
{ |
|||
/* Finished */ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
/* need more input ? */ |
|||
if (symbol < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
/* otherwise repeat code */ |
|||
if (symbol >= 17) |
|||
{ |
|||
/* repeat zero */ |
|||
// System.err.println("repeating zero");
|
|||
lastLen = 0; |
|||
} |
|||
else |
|||
{ |
|||
if (ptr == 0) |
|||
{ |
|||
throw new ImageFormatException(); |
|||
} |
|||
} |
|||
repSymbol = symbol - 16; |
|||
} |
|||
mode = REPS; |
|||
goto case REPS; // fall through
|
|||
case REPS: |
|||
{ |
|||
int bits = repBits[repSymbol]; |
|||
int count = input.PeekBits(bits); |
|||
if (count < 0) |
|||
{ |
|||
return false; |
|||
} |
|||
input.DropBits(bits); |
|||
count += repMin[repSymbol]; |
|||
// System.err.println("litdistLens repeated: "+count);
|
|||
|
|||
if (ptr + count > num) |
|||
{ |
|||
throw new ImageFormatException(); |
|||
} |
|||
while (count-- > 0) |
|||
{ |
|||
litdistLens[ptr++] = lastLen; |
|||
} |
|||
|
|||
if (ptr == num) |
|||
{ |
|||
/* Finished */ |
|||
return true; |
|||
} |
|||
} |
|||
mode = LENS; |
|||
goto decode_loop; |
|||
} |
|||
} |
|||
} |
|||
|
|||
public InflaterHuffmanTree BuildLitLenTree() |
|||
{ |
|||
byte[] litlenLens = new byte[lnum]; |
|||
Array.Copy(litdistLens, 0, litlenLens, 0, lnum); |
|||
return new InflaterHuffmanTree(litlenLens); |
|||
} |
|||
|
|||
public InflaterHuffmanTree BuildDistTree() |
|||
{ |
|||
byte[] distLens = new byte[dnum]; |
|||
Array.Copy(litdistLens, lnum, distLens, 0, dnum); |
|||
return new InflaterHuffmanTree(distLens); |
|||
} |
|||
|
|||
#region Instance Fields
|
|||
byte[] blLens; |
|||
byte[] litdistLens; |
|||
|
|||
InflaterHuffmanTree blTree; |
|||
|
|||
/// <summary>
|
|||
/// The current decode mode
|
|||
/// </summary>
|
|||
int mode; |
|||
int lnum, dnum, blnum, num; |
|||
int repSymbol; |
|||
byte lastLen; |
|||
int ptr; |
|||
#endregion
|
|||
|
|||
} |
|||
} |
|||
@ -1,225 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Huffman tree used for inflation
|
|||
/// </summary>
|
|||
public class InflaterHuffmanTree |
|||
{ |
|||
#region Constants
|
|||
const int MAX_BITLEN = 15; |
|||
#endregion
|
|||
|
|||
#region Instance Fields
|
|||
short[] tree; |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Literal length tree
|
|||
/// </summary>
|
|||
public static InflaterHuffmanTree defLitLenTree; |
|||
|
|||
/// <summary>
|
|||
/// Distance tree
|
|||
/// </summary>
|
|||
public static InflaterHuffmanTree defDistTree; |
|||
|
|||
static InflaterHuffmanTree() |
|||
{ |
|||
try |
|||
{ |
|||
byte[] codeLengths = new byte[288]; |
|||
int i = 0; |
|||
while (i < 144) |
|||
{ |
|||
codeLengths[i++] = 8; |
|||
} |
|||
while (i < 256) |
|||
{ |
|||
codeLengths[i++] = 9; |
|||
} |
|||
while (i < 280) |
|||
{ |
|||
codeLengths[i++] = 7; |
|||
} |
|||
while (i < 288) |
|||
{ |
|||
codeLengths[i++] = 8; |
|||
} |
|||
defLitLenTree = new InflaterHuffmanTree(codeLengths); |
|||
|
|||
codeLengths = new byte[32]; |
|||
i = 0; |
|||
while (i < 32) |
|||
{ |
|||
codeLengths[i++] = 5; |
|||
} |
|||
defDistTree = new InflaterHuffmanTree(codeLengths); |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
throw new ImageFormatException("InflaterHuffmanTree: static tree length illegal"); |
|||
} |
|||
} |
|||
|
|||
#region Constructors
|
|||
/// <summary>
|
|||
/// Constructs a Huffman tree from the array of code lengths.
|
|||
/// </summary>
|
|||
/// <param name = "codeLengths">
|
|||
/// the array of code lengths
|
|||
/// </param>
|
|||
public InflaterHuffmanTree(byte[] codeLengths) |
|||
{ |
|||
BuildTree(codeLengths); |
|||
} |
|||
#endregion
|
|||
|
|||
void BuildTree(byte[] codeLengths) |
|||
{ |
|||
int[] blCount = new int[MAX_BITLEN + 1]; |
|||
int[] nextCode = new int[MAX_BITLEN + 1]; |
|||
|
|||
for (int i = 0; i < codeLengths.Length; i++) |
|||
{ |
|||
int bits = codeLengths[i]; |
|||
if (bits > 0) |
|||
{ |
|||
blCount[bits]++; |
|||
} |
|||
} |
|||
|
|||
int code = 0; |
|||
int treeSize = 512; |
|||
for (int bits = 1; bits <= MAX_BITLEN; bits++) |
|||
{ |
|||
nextCode[bits] = code; |
|||
code += blCount[bits] << (16 - bits); |
|||
if (bits >= 10) |
|||
{ |
|||
/* We need an extra table for bit lengths >= 10. */ |
|||
int start = nextCode[bits] & 0x1ff80; |
|||
int end = code & 0x1ff80; |
|||
treeSize += (end - start) >> (16 - bits); |
|||
} |
|||
} |
|||
|
|||
/* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g |
|||
if (code != 65536) |
|||
{ |
|||
throw new SharpZipBaseException("Code lengths don't add up properly."); |
|||
} |
|||
*/ |
|||
/* Now create and fill the extra tables from longest to shortest |
|||
* bit len. This way the sub trees will be aligned. |
|||
*/ |
|||
tree = new short[treeSize]; |
|||
int treePtr = 512; |
|||
for (int bits = MAX_BITLEN; bits >= 10; bits--) |
|||
{ |
|||
int end = code & 0x1ff80; |
|||
code -= blCount[bits] << (16 - bits); |
|||
int start = code & 0x1ff80; |
|||
for (int i = start; i < end; i += 1 << 7) |
|||
{ |
|||
tree[DeflaterHuffman.BitReverse(i)] = (short)((-treePtr << 4) | bits); |
|||
treePtr += 1 << (bits - 9); |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < codeLengths.Length; i++) |
|||
{ |
|||
int bits = codeLengths[i]; |
|||
if (bits == 0) |
|||
{ |
|||
continue; |
|||
} |
|||
code = nextCode[bits]; |
|||
int revcode = DeflaterHuffman.BitReverse(code); |
|||
if (bits <= 9) |
|||
{ |
|||
do |
|||
{ |
|||
tree[revcode] = (short)((i << 4) | bits); |
|||
revcode += 1 << bits; |
|||
} while (revcode < 512); |
|||
} |
|||
else |
|||
{ |
|||
int subTree = tree[revcode & 511]; |
|||
int treeLen = 1 << (subTree & 15); |
|||
subTree = -(subTree >> 4); |
|||
do |
|||
{ |
|||
tree[subTree | (revcode >> 9)] = (short)((i << 4) | bits); |
|||
revcode += 1 << bits; |
|||
} while (revcode < treeLen); |
|||
} |
|||
nextCode[bits] = code + (1 << (16 - bits)); |
|||
} |
|||
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the next symbol from input. The symbol is encoded using the
|
|||
/// huffman tree.
|
|||
/// </summary>
|
|||
/// <param name="input">
|
|||
/// input the input source.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// the next symbol, or -1 if not enough input is available.
|
|||
/// </returns>
|
|||
public int GetSymbol(StreamManipulator input) |
|||
{ |
|||
int lookahead, symbol; |
|||
if ((lookahead = input.PeekBits(9)) >= 0) |
|||
{ |
|||
if ((symbol = tree[lookahead]) >= 0) |
|||
{ |
|||
input.DropBits(symbol & 15); |
|||
return symbol >> 4; |
|||
} |
|||
int subtree = -(symbol >> 4); |
|||
int bitlen = symbol & 15; |
|||
if ((lookahead = input.PeekBits(bitlen)) >= 0) |
|||
{ |
|||
symbol = tree[subtree | (lookahead >> 9)]; |
|||
input.DropBits(symbol & 15); |
|||
return symbol >> 4; |
|||
} |
|||
else |
|||
{ |
|||
int bits = input.AvailableBits; |
|||
lookahead = input.PeekBits(bits); |
|||
symbol = tree[subtree | (lookahead >> 9)]; |
|||
if ((symbol & 15) <= bits) |
|||
{ |
|||
input.DropBits(symbol & 15); |
|||
return symbol >> 4; |
|||
} |
|||
else |
|||
{ |
|||
return -1; |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
int bits = input.AvailableBits; |
|||
lookahead = input.PeekBits(bits); |
|||
symbol = tree[lookahead]; |
|||
if (symbol >= 0 && (symbol & 15) <= bits) |
|||
{ |
|||
input.DropBits(symbol & 15); |
|||
return symbol >> 4; |
|||
} |
|||
else |
|||
{ |
|||
return -1; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,268 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
/// <summary>
|
|||
/// An input buffer customised for use by <see cref="InflaterInputStream"/>
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// The buffer supports decryption of incoming data.
|
|||
/// </remarks>
|
|||
public class InflaterInputBuffer |
|||
{ |
|||
/// <summary>
|
|||
/// Initialise a new instance of <see cref="InflaterInputBuffer"/> with a default buffer size
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to buffer.</param>
|
|||
public InflaterInputBuffer(Stream stream) : this(stream, 4096) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initialise a new instance of <see cref="InflaterInputBuffer"/>
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to buffer.</param>
|
|||
/// <param name="bufferSize">The size to use for the buffer</param>
|
|||
/// <remarks>A minimum buffer size of 1KB is permitted. Lower sizes are treated as 1KB.</remarks>
|
|||
public InflaterInputBuffer(Stream stream, int bufferSize) |
|||
{ |
|||
inputStream = stream; |
|||
if (bufferSize < 1024) |
|||
{ |
|||
bufferSize = 1024; |
|||
} |
|||
rawData = new byte[bufferSize]; |
|||
clearText = rawData; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the length of bytes bytes in the <see cref="RawData"/>
|
|||
/// </summary>
|
|||
public int RawLength |
|||
{ |
|||
get |
|||
{ |
|||
return rawLength; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the contents of the raw data buffer.
|
|||
/// </summary>
|
|||
/// <remarks>This may contain encrypted data.</remarks>
|
|||
public byte[] RawData |
|||
{ |
|||
get |
|||
{ |
|||
return rawData; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the number of useable bytes in <see cref="ClearText"/>
|
|||
/// </summary>
|
|||
public int ClearTextLength |
|||
{ |
|||
get |
|||
{ |
|||
return clearTextLength; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the contents of the clear text buffer.
|
|||
/// </summary>
|
|||
public byte[] ClearText |
|||
{ |
|||
get |
|||
{ |
|||
return clearText; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get/set the number of bytes available
|
|||
/// </summary>
|
|||
public int Available |
|||
{ |
|||
get { return available; } |
|||
set { available = value; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Call <see cref="Inflater.SetInput(byte[], int, int)"/> passing the current clear text buffer contents.
|
|||
/// </summary>
|
|||
/// <param name="inflater">The inflater to set input for.</param>
|
|||
public void SetInflaterInput(Inflater inflater) |
|||
{ |
|||
if (available > 0) |
|||
{ |
|||
inflater.SetInput(clearText, clearTextLength - available, available); |
|||
available = 0; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fill the buffer from the underlying input stream.
|
|||
/// </summary>
|
|||
public void Fill() |
|||
{ |
|||
rawLength = 0; |
|||
int toRead = rawData.Length; |
|||
|
|||
while (toRead > 0) |
|||
{ |
|||
int count = inputStream.Read(rawData, rawLength, toRead); |
|||
if (count <= 0) |
|||
{ |
|||
break; |
|||
} |
|||
rawLength += count; |
|||
toRead -= count; |
|||
} |
|||
|
|||
clearTextLength = rawLength; |
|||
available = clearTextLength; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a buffer directly from the input stream
|
|||
/// </summary>
|
|||
/// <param name="buffer">The buffer to fill</param>
|
|||
/// <returns>Returns the number of bytes read.</returns>
|
|||
public int ReadRawBuffer(byte[] buffer) |
|||
{ |
|||
return ReadRawBuffer(buffer, 0, buffer.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a buffer directly from the input stream
|
|||
/// </summary>
|
|||
/// <param name="outBuffer">The buffer to read into</param>
|
|||
/// <param name="offset">The offset to start reading data into.</param>
|
|||
/// <param name="length">The number of bytes to read.</param>
|
|||
/// <returns>Returns the number of bytes read.</returns>
|
|||
public int ReadRawBuffer(byte[] outBuffer, int offset, int length) |
|||
{ |
|||
if (length < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("length"); |
|||
} |
|||
|
|||
int currentOffset = offset; |
|||
int currentLength = length; |
|||
|
|||
while (currentLength > 0) |
|||
{ |
|||
if (available <= 0) |
|||
{ |
|||
Fill(); |
|||
if (available <= 0) |
|||
{ |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
int toCopy = Math.Min(currentLength, available); |
|||
Array.Copy(rawData, rawLength - (int)available, outBuffer, currentOffset, toCopy); |
|||
currentOffset += toCopy; |
|||
currentLength -= toCopy; |
|||
available -= toCopy; |
|||
} |
|||
|
|||
return length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read clear text data from the input stream.
|
|||
/// </summary>
|
|||
/// <param name="outBuffer">The buffer to add data to.</param>
|
|||
/// <param name="offset">The offset to start adding data at.</param>
|
|||
/// <param name="length">The number of bytes to read.</param>
|
|||
/// <returns>Returns the number of bytes actually read.</returns>
|
|||
public int ReadClearTextBuffer(byte[] outBuffer, int offset, int length) |
|||
{ |
|||
if (length < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("length"); |
|||
} |
|||
|
|||
int currentOffset = offset; |
|||
int currentLength = length; |
|||
|
|||
while (currentLength > 0) |
|||
{ |
|||
if (available <= 0) |
|||
{ |
|||
Fill(); |
|||
if (available <= 0) |
|||
{ |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
int toCopy = Math.Min(currentLength, available); |
|||
Array.Copy(clearText, clearTextLength - (int)available, outBuffer, currentOffset, toCopy); |
|||
currentOffset += toCopy; |
|||
currentLength -= toCopy; |
|||
available -= toCopy; |
|||
} |
|||
return length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a <see cref="byte"/> from the input stream.
|
|||
/// </summary>
|
|||
/// <returns>Returns the byte read.</returns>
|
|||
public int ReadLeByte() |
|||
{ |
|||
if (available <= 0) |
|||
{ |
|||
Fill(); |
|||
if (available <= 0) |
|||
{ |
|||
throw new ImageFormatException("EOF in header"); |
|||
} |
|||
} |
|||
byte result = rawData[rawLength - available]; |
|||
available -= 1; |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read an <see cref="short"/> in little endian byte order.
|
|||
/// </summary>
|
|||
/// <returns>The short value read case to an int.</returns>
|
|||
public int ReadLeShort() |
|||
{ |
|||
return ReadLeByte() | (ReadLeByte() << 8); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read an <see cref="int"/> in little endian byte order.
|
|||
/// </summary>
|
|||
/// <returns>The int value read.</returns>
|
|||
public int ReadLeInt() |
|||
{ |
|||
return ReadLeShort() | (ReadLeShort() << 16); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Read a <see cref="long"/> in little endian byte order.
|
|||
/// </summary>
|
|||
/// <returns>The long value read.</returns>
|
|||
public long ReadLeLong() |
|||
{ |
|||
return (uint)ReadLeInt() | ((long)ReadLeInt() << 32); |
|||
} |
|||
|
|||
int rawLength; |
|||
byte[] rawData; |
|||
|
|||
int clearTextLength; |
|||
byte[] clearText; |
|||
int available; |
|||
Stream inputStream; |
|||
} |
|||
} |
|||
@ -1,397 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
//using ICSharpCode.SharpZipLib;
|
|||
//using ICSharpCode.SharpZipLib.Zip;
|
|||
//using ICSharpCode.SharpZipLib.Zip.Compression;
|
|||
|
|||
/// <summary>
|
|||
/// This filter stream is used to decompress data compressed using the "deflate"
|
|||
/// format. The "deflate" format is described in RFC 1951.
|
|||
///
|
|||
/// This stream may form the basis for other decompression filters, such
|
|||
/// as the <see cref="ICSharpCode.SharpZipLib.GZip.GZipInputStream">GZipInputStream</see>.
|
|||
///
|
|||
/// Author of the original java version : John Leuner.
|
|||
/// </summary>
|
|||
public class InflaterInputStream : Stream |
|||
{ |
|||
#region Constructors
|
|||
/// <summary>
|
|||
/// Create an InflaterInputStream with the default decompressor
|
|||
/// and a default buffer size of 4KB.
|
|||
/// </summary>
|
|||
/// <param name = "baseInputStream">
|
|||
/// The InputStream to read bytes from
|
|||
/// </param>
|
|||
public InflaterInputStream(Stream baseInputStream) |
|||
: this(baseInputStream, new Inflater(), 4096) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Create an InflaterInputStream with the specified decompressor
|
|||
/// and a default buffer size of 4KB.
|
|||
/// </summary>
|
|||
/// <param name = "baseInputStream">
|
|||
/// The source of input data
|
|||
/// </param>
|
|||
/// <param name = "inf">
|
|||
/// The decompressor used to decompress data read from baseInputStream
|
|||
/// </param>
|
|||
public InflaterInputStream(Stream baseInputStream, Inflater inf) |
|||
: this(baseInputStream, inf, 4096) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Create an InflaterInputStream with the specified decompressor and the specified buffer size.
|
|||
/// </summary>
|
|||
/// <param name = "baseInputStream">
|
|||
/// The InputStream to read bytes from
|
|||
/// </param>
|
|||
/// <param name = "inflater">
|
|||
/// The decompressor to use
|
|||
/// </param>
|
|||
/// <param name = "bufferSize">
|
|||
/// Size of the buffer to use
|
|||
/// </param>
|
|||
public InflaterInputStream(Stream baseInputStream, Inflater inflater, int bufferSize) |
|||
{ |
|||
if (baseInputStream == null) |
|||
{ |
|||
throw new ArgumentNullException("baseInputStream"); |
|||
} |
|||
|
|||
if (inflater == null) |
|||
{ |
|||
throw new ArgumentNullException("inflater"); |
|||
} |
|||
|
|||
if (bufferSize <= 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("bufferSize"); |
|||
} |
|||
|
|||
this.baseInputStream = baseInputStream; |
|||
this.inf = inflater; |
|||
|
|||
inputBuffer = new InflaterInputBuffer(baseInputStream, bufferSize); |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Get/set flag indicating ownership of underlying stream.
|
|||
/// When the flag is true <see cref="Dispose"/> will close the underlying stream also.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// The default value is true.
|
|||
/// </remarks>
|
|||
public bool IsStreamOwner |
|||
{ |
|||
get { return isStreamOwner; } |
|||
set { isStreamOwner = value; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Skip specified number of bytes of uncompressed data
|
|||
/// </summary>
|
|||
/// <param name ="count">
|
|||
/// Number of bytes to skip
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The number of bytes skipped, zero if the end of
|
|||
/// stream has been reached
|
|||
/// </returns>
|
|||
/// <exception cref="ArgumentOutOfRangeException">
|
|||
/// <paramref name="count">The number of bytes</paramref> to skip is less than or equal to zero.
|
|||
/// </exception>
|
|||
public long Skip(long count) |
|||
{ |
|||
if (count <= 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("count"); |
|||
} |
|||
|
|||
// v0.80 Skip by seeking if underlying stream supports it...
|
|||
if (baseInputStream.CanSeek) |
|||
{ |
|||
baseInputStream.Seek(count, SeekOrigin.Current); |
|||
return count; |
|||
} |
|||
else |
|||
{ |
|||
int length = 2048; |
|||
if (count < length) |
|||
{ |
|||
length = (int)count; |
|||
} |
|||
|
|||
byte[] tmp = new byte[length]; |
|||
int readCount = 1; |
|||
long toSkip = count; |
|||
|
|||
while ((toSkip > 0) && (readCount > 0)) |
|||
{ |
|||
if (toSkip < length) |
|||
{ |
|||
length = (int)toSkip; |
|||
} |
|||
|
|||
readCount = baseInputStream.Read(tmp, 0, length); |
|||
toSkip -= readCount; |
|||
} |
|||
|
|||
return count - toSkip; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns 0 once the end of the stream (EOF) has been reached.
|
|||
/// Otherwise returns 1.
|
|||
/// </summary>
|
|||
public virtual int Available |
|||
{ |
|||
get |
|||
{ |
|||
return inf.IsFinished ? 0 : 1; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fills the buffer with more data to decompress.
|
|||
/// </summary>
|
|||
/// <exception cref="SharpZipBaseException">
|
|||
/// Stream ends early
|
|||
/// </exception>
|
|||
protected void Fill() |
|||
{ |
|||
// Protect against redundant calls
|
|||
if (inputBuffer.Available <= 0) |
|||
{ |
|||
inputBuffer.Fill(); |
|||
if (inputBuffer.Available <= 0) |
|||
{ |
|||
throw new ImageFormatException("Unexpected EOF"); |
|||
} |
|||
} |
|||
inputBuffer.SetInflaterInput(inf); |
|||
} |
|||
|
|||
#region Stream Overrides
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether the current stream supports reading
|
|||
/// </summary>
|
|||
public override bool CanRead |
|||
{ |
|||
get |
|||
{ |
|||
return baseInputStream.CanRead; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value of false indicating seeking is not supported for this stream.
|
|||
/// </summary>
|
|||
public override bool CanSeek |
|||
{ |
|||
get |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value of false indicating that this stream is not writeable.
|
|||
/// </summary>
|
|||
public override bool CanWrite |
|||
{ |
|||
get |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// A value representing the length of the stream in bytes.
|
|||
/// </summary>
|
|||
public override long Length |
|||
{ |
|||
get |
|||
{ |
|||
return inputBuffer.RawLength; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// The current position within the stream.
|
|||
/// Throws a NotSupportedException when attempting to set the position
|
|||
/// </summary>
|
|||
/// <exception cref="NotSupportedException">Attempting to set the position</exception>
|
|||
public override long Position |
|||
{ |
|||
get |
|||
{ |
|||
return baseInputStream.Position; |
|||
} |
|||
set |
|||
{ |
|||
throw new NotSupportedException("InflaterInputStream Position not supported"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the baseInputStream
|
|||
/// </summary>
|
|||
public override void Flush() |
|||
{ |
|||
baseInputStream.Flush(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the position within the current stream
|
|||
/// Always throws a NotSupportedException
|
|||
/// </summary>
|
|||
/// <param name="offset">The relative offset to seek to.</param>
|
|||
/// <param name="origin">The <see cref="SeekOrigin"/> defining where to seek from.</param>
|
|||
/// <returns>The new position in the stream.</returns>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override long Seek(long offset, SeekOrigin origin) |
|||
{ |
|||
throw new NotSupportedException("Seek not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Set the length of the current stream
|
|||
/// Always throws a NotSupportedException
|
|||
/// </summary>
|
|||
/// <param name="value">The new length value for the stream.</param>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override void SetLength(long value) |
|||
{ |
|||
throw new NotSupportedException("InflaterInputStream SetLength not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes a sequence of bytes to stream and advances the current position
|
|||
/// This method always throws a NotSupportedException
|
|||
/// </summary>
|
|||
/// <param name="buffer">Thew buffer containing data to write.</param>
|
|||
/// <param name="offset">The offset of the first byte to write.</param>
|
|||
/// <param name="count">The number of bytes to write.</param>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override void Write(byte[] buffer, int offset, int count) |
|||
{ |
|||
throw new NotSupportedException("InflaterInputStream Write not supported"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes one byte to the current stream and advances the current position
|
|||
/// Always throws a NotSupportedException
|
|||
/// </summary>
|
|||
/// <param name="value">The byte to write.</param>
|
|||
/// <exception cref="NotSupportedException">Any access</exception>
|
|||
public override void WriteByte(byte value) |
|||
{ |
|||
throw new NotSupportedException("InflaterInputStream WriteByte not supported"); |
|||
} |
|||
|
|||
protected override void Dispose(bool disposing) |
|||
{ |
|||
base.Dispose(disposing); |
|||
if (disposing && !isClosed) |
|||
{ |
|||
isClosed = true; |
|||
if (isStreamOwner) |
|||
{ |
|||
baseInputStream.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads decompressed data into the provided buffer byte array
|
|||
/// </summary>
|
|||
/// <param name ="buffer">
|
|||
/// The array to read and decompress data into
|
|||
/// </param>
|
|||
/// <param name ="offset">
|
|||
/// The offset indicating where the data should be placed
|
|||
/// </param>
|
|||
/// <param name ="count">
|
|||
/// The number of bytes to decompress
|
|||
/// </param>
|
|||
/// <returns>The number of bytes read. Zero signals the end of stream</returns>
|
|||
/// <exception cref="SharpZipBaseException">
|
|||
/// Inflater needs a dictionary
|
|||
/// </exception>
|
|||
public override int Read(byte[] buffer, int offset, int count) |
|||
{ |
|||
if (inf.IsNeedingDictionary) |
|||
{ |
|||
throw new ImageFormatException("Need a dictionary"); |
|||
} |
|||
|
|||
int remainingBytes = count; |
|||
while (true) |
|||
{ |
|||
int bytesRead = inf.Inflate(buffer, offset, remainingBytes); |
|||
offset += bytesRead; |
|||
remainingBytes -= bytesRead; |
|||
|
|||
if (remainingBytes == 0 || inf.IsFinished) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
if (inf.IsNeedingInput) |
|||
{ |
|||
Fill(); |
|||
} |
|||
else if (bytesRead == 0) |
|||
{ |
|||
throw new ImageFormatException("Dont know what to do"); |
|||
} |
|||
} |
|||
return count - remainingBytes; |
|||
} |
|||
#endregion
|
|||
|
|||
#region Instance Fields
|
|||
/// <summary>
|
|||
/// Decompressor for this stream
|
|||
/// </summary>
|
|||
protected Inflater inf; |
|||
|
|||
/// <summary>
|
|||
/// <see cref="InflaterInputBuffer">Input buffer</see> for this stream.
|
|||
/// </summary>
|
|||
protected InflaterInputBuffer inputBuffer; |
|||
|
|||
/// <summary>
|
|||
/// Base stream the inflater reads from.
|
|||
/// </summary>
|
|||
private Stream baseInputStream; |
|||
|
|||
/// <summary>
|
|||
/// The compressed size
|
|||
/// </summary>
|
|||
protected long csize; |
|||
|
|||
/// <summary>
|
|||
/// Flag indicating wether this instance has been closed or not.
|
|||
/// </summary>
|
|||
bool isClosed; |
|||
|
|||
/// <summary>
|
|||
/// Flag indicating wether this instance is designated the stream owner.
|
|||
/// When closing if this flag is true the underlying stream is closed.
|
|||
/// </summary>
|
|||
bool isStreamOwner = true; |
|||
#endregion
|
|||
} |
|||
} |
|||
|
|||
@ -1,217 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Contains the output from the Inflation process.
|
|||
/// We need to have a window so that we can refer backwards into the output stream
|
|||
/// to repeat stuff.<br/>
|
|||
/// Author of the original java version : John Leuner
|
|||
/// </summary>
|
|||
public class OutputWindow |
|||
{ |
|||
#region Constants
|
|||
const int WindowSize = 1 << 15; |
|||
const int WindowMask = WindowSize - 1; |
|||
#endregion
|
|||
|
|||
#region Instance Fields
|
|||
byte[] window = new byte[WindowSize]; //The window is 2^15 bytes
|
|||
int windowEnd; |
|||
int windowFilled; |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Write a byte to this output window
|
|||
/// </summary>
|
|||
/// <param name="value">value to write</param>
|
|||
/// <exception cref="InvalidOperationException">
|
|||
/// if window is full
|
|||
/// </exception>
|
|||
public void Write(int value) |
|||
{ |
|||
if (windowFilled++ == WindowSize) |
|||
{ |
|||
throw new InvalidOperationException("Window full"); |
|||
} |
|||
window[windowEnd++] = (byte)value; |
|||
windowEnd &= WindowMask; |
|||
} |
|||
|
|||
|
|||
private void SlowRepeat(int repStart, int length, int distance) |
|||
{ |
|||
while (length-- > 0) |
|||
{ |
|||
window[windowEnd++] = window[repStart++]; |
|||
windowEnd &= WindowMask; |
|||
repStart &= WindowMask; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Append a byte pattern already in the window itself
|
|||
/// </summary>
|
|||
/// <param name="length">length of pattern to copy</param>
|
|||
/// <param name="distance">distance from end of window pattern occurs</param>
|
|||
/// <exception cref="InvalidOperationException">
|
|||
/// If the repeated data overflows the window
|
|||
/// </exception>
|
|||
public void Repeat(int length, int distance) |
|||
{ |
|||
if ((windowFilled += length) > WindowSize) |
|||
{ |
|||
throw new InvalidOperationException("Window full"); |
|||
} |
|||
|
|||
int repStart = (windowEnd - distance) & WindowMask; |
|||
int border = WindowSize - length; |
|||
if ((repStart <= border) && (windowEnd < border)) |
|||
{ |
|||
if (length <= distance) |
|||
{ |
|||
System.Array.Copy(window, repStart, window, windowEnd, length); |
|||
windowEnd += length; |
|||
} |
|||
else |
|||
{ |
|||
// We have to copy manually, since the repeat pattern overlaps.
|
|||
while (length-- > 0) |
|||
{ |
|||
window[windowEnd++] = window[repStart++]; |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
SlowRepeat(repStart, length, distance); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copy from input manipulator to internal window
|
|||
/// </summary>
|
|||
/// <param name="input">source of data</param>
|
|||
/// <param name="length">length of data to copy</param>
|
|||
/// <returns>the number of bytes copied</returns>
|
|||
public int CopyStored(StreamManipulator input, int length) |
|||
{ |
|||
length = Math.Min(Math.Min(length, WindowSize - windowFilled), input.AvailableBytes); |
|||
int copied; |
|||
|
|||
int tailLen = WindowSize - windowEnd; |
|||
if (length > tailLen) |
|||
{ |
|||
copied = input.CopyBytes(window, windowEnd, tailLen); |
|||
if (copied == tailLen) |
|||
{ |
|||
copied += input.CopyBytes(window, 0, length - tailLen); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
copied = input.CopyBytes(window, windowEnd, length); |
|||
} |
|||
|
|||
windowEnd = (windowEnd + copied) & WindowMask; |
|||
windowFilled += copied; |
|||
return copied; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copy dictionary to window
|
|||
/// </summary>
|
|||
/// <param name="dictionary">source dictionary</param>
|
|||
/// <param name="offset">offset of start in source dictionary</param>
|
|||
/// <param name="length">length of dictionary</param>
|
|||
/// <exception cref="InvalidOperationException">
|
|||
/// If window isnt empty
|
|||
/// </exception>
|
|||
public void CopyDict(byte[] dictionary, int offset, int length) |
|||
{ |
|||
if (dictionary == null) |
|||
{ |
|||
throw new ArgumentNullException("dictionary"); |
|||
} |
|||
|
|||
if (windowFilled > 0) |
|||
{ |
|||
throw new InvalidOperationException(); |
|||
} |
|||
|
|||
if (length > WindowSize) |
|||
{ |
|||
offset += length - WindowSize; |
|||
length = WindowSize; |
|||
} |
|||
System.Array.Copy(dictionary, offset, window, 0, length); |
|||
windowEnd = length & WindowMask; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get remaining unfilled space in window
|
|||
/// </summary>
|
|||
/// <returns>Number of bytes left in window</returns>
|
|||
public int GetFreeSpace() |
|||
{ |
|||
return WindowSize - windowFilled; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get bytes available for output in window
|
|||
/// </summary>
|
|||
/// <returns>Number of bytes filled</returns>
|
|||
public int GetAvailable() |
|||
{ |
|||
return windowFilled; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copy contents of window to output
|
|||
/// </summary>
|
|||
/// <param name="output">buffer to copy to</param>
|
|||
/// <param name="offset">offset to start at</param>
|
|||
/// <param name="len">number of bytes to count</param>
|
|||
/// <returns>The number of bytes copied</returns>
|
|||
/// <exception cref="InvalidOperationException">
|
|||
/// If a window underflow occurs
|
|||
/// </exception>
|
|||
public int CopyOutput(byte[] output, int offset, int len) |
|||
{ |
|||
int copyEnd = windowEnd; |
|||
if (len > windowFilled) |
|||
{ |
|||
len = windowFilled; |
|||
} |
|||
else |
|||
{ |
|||
copyEnd = (windowEnd - windowFilled + len) & WindowMask; |
|||
} |
|||
|
|||
int copied = len; |
|||
int tailLen = len - copyEnd; |
|||
|
|||
if (tailLen > 0) |
|||
{ |
|||
System.Array.Copy(window, WindowSize - tailLen, output, offset, tailLen); |
|||
offset += tailLen; |
|||
len = copyEnd; |
|||
} |
|||
System.Array.Copy(window, copyEnd - len, output, offset, len); |
|||
windowFilled -= copied; |
|||
if (windowFilled < 0) |
|||
{ |
|||
throw new InvalidOperationException(); |
|||
} |
|||
return copied; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reset by clearing window so <see cref="GetAvailable">GetAvailable</see> returns 0
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
windowFilled = windowEnd = 0; |
|||
} |
|||
} |
|||
} |
|||
@ -1,217 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// This class is general purpose class for writing data to a buffer.
|
|||
///
|
|||
/// It allows you to write bits as well as bytes
|
|||
/// Based on DeflaterPending.java
|
|||
///
|
|||
/// author of the original java version : Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class PendingBuffer |
|||
{ |
|||
#region Instance Fields
|
|||
/// <summary>
|
|||
/// Internal work buffer
|
|||
/// </summary>
|
|||
byte[] buffer_; |
|||
|
|||
int start; |
|||
int end; |
|||
|
|||
uint bits; |
|||
int bitCount; |
|||
#endregion
|
|||
|
|||
#region Constructors
|
|||
/// <summary>
|
|||
/// construct instance using default buffer size of 4096
|
|||
/// </summary>
|
|||
public PendingBuffer() : this(4096) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// construct instance using specified buffer size
|
|||
/// </summary>
|
|||
/// <param name="bufferSize">
|
|||
/// size to use for internal buffer
|
|||
/// </param>
|
|||
public PendingBuffer(int bufferSize) |
|||
{ |
|||
buffer_ = new byte[bufferSize]; |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Clear internal state/buffers
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
start = end = bitCount = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a byte to buffer
|
|||
/// </summary>
|
|||
/// <param name="value">
|
|||
/// The value to write
|
|||
/// </param>
|
|||
public void WriteByte(int value) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to buffer LSB first
|
|||
/// </summary>
|
|||
/// <param name="value">
|
|||
/// The value to write.
|
|||
/// </param>
|
|||
public void WriteShort(int value) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)value); |
|||
buffer_[end++] = unchecked((byte)(value >> 8)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// write an integer LSB first
|
|||
/// </summary>
|
|||
/// <param name="value">The value to write.</param>
|
|||
public void WriteInt(int value) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)value); |
|||
buffer_[end++] = unchecked((byte)(value >> 8)); |
|||
buffer_[end++] = unchecked((byte)(value >> 16)); |
|||
buffer_[end++] = unchecked((byte)(value >> 24)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a block of data to buffer
|
|||
/// </summary>
|
|||
/// <param name="block">data to write</param>
|
|||
/// <param name="offset">offset of first byte to write</param>
|
|||
/// <param name="length">number of bytes to write</param>
|
|||
public void WriteBlock(byte[] block, int offset, int length) |
|||
{ |
|||
System.Array.Copy(block, offset, buffer_, end, length); |
|||
end += length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// The number of bits written to the buffer
|
|||
/// </summary>
|
|||
public int BitCount |
|||
{ |
|||
get |
|||
{ |
|||
return bitCount; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Align internal buffer on a byte boundary
|
|||
/// </summary>
|
|||
public void AlignToByte() |
|||
{ |
|||
if (bitCount > 0) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)bits); |
|||
if (bitCount > 8) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)(bits >> 8)); |
|||
} |
|||
} |
|||
bits = 0; |
|||
bitCount = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bits to internal buffer
|
|||
/// </summary>
|
|||
/// <param name="b">source of bits</param>
|
|||
/// <param name="count">number of bits to write</param>
|
|||
public void WriteBits(int b, int count) |
|||
{ |
|||
bits |= (uint)(b << bitCount); |
|||
bitCount += count; |
|||
if (bitCount >= 16) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)bits); |
|||
buffer_[end++] = unchecked((byte)(bits >> 8)); |
|||
bits >>= 16; |
|||
bitCount -= 16; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write a short value to internal buffer most significant byte first
|
|||
/// </summary>
|
|||
/// <param name="s">value to write</param>
|
|||
public void WriteShortMSB(int s) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)(s >> 8)); |
|||
buffer_[end++] = unchecked((byte)s); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Indicates if buffer has been flushed
|
|||
/// </summary>
|
|||
public bool IsFlushed |
|||
{ |
|||
get |
|||
{ |
|||
return end == 0; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flushes the pending buffer into the given output array. If the
|
|||
/// output array is to small, only a partial flush is done.
|
|||
/// </summary>
|
|||
/// <param name="output">The output array.</param>
|
|||
/// <param name="offset">The offset into output array.</param>
|
|||
/// <param name="length">The maximum number of bytes to store.</param>
|
|||
/// <returns>The number of bytes flushed.</returns>
|
|||
public int Flush(byte[] output, int offset, int length) |
|||
{ |
|||
if (bitCount >= 8) |
|||
{ |
|||
buffer_[end++] = unchecked((byte)bits); |
|||
bits >>= 8; |
|||
bitCount -= 8; |
|||
} |
|||
|
|||
if (length > end - start) |
|||
{ |
|||
length = end - start; |
|||
System.Array.Copy(buffer_, start, output, offset, length); |
|||
start = 0; |
|||
end = 0; |
|||
} |
|||
else |
|||
{ |
|||
System.Array.Copy(buffer_, start, output, offset, length); |
|||
start += length; |
|||
} |
|||
return length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert internal buffer to byte array.
|
|||
/// Buffer is empty on completion
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The internal buffer contents converted to a byte array.
|
|||
/// </returns>
|
|||
public byte[] ToByteArray() |
|||
{ |
|||
byte[] result = new byte[end - start]; |
|||
System.Array.Copy(buffer_, start, result, 0, result.Length); |
|||
start = 0; |
|||
end = 0; |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -1,2 +1,2 @@ |
|||
The contents of this folder have been copied from https://github.com/ygrenier/SharpZipLib.Portable |
|||
in order to allow the project to run the NET 4.6 portable classes. |
|||
Adler32.cs and Crc32.cs have been copied from |
|||
https://github.com/ygrenier/SharpZipLib.Portable |
|||
|
|||
@ -1,279 +0,0 @@ |
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// This class allows us to retrieve a specified number of bits from
|
|||
/// the input buffer, as well as copy big byte blocks.
|
|||
///
|
|||
/// It uses an int buffer to store up to 31 bits for direct
|
|||
/// manipulation. This guarantees that we can get at least 16 bits,
|
|||
/// but we only need at most 15, so this is all safe.
|
|||
///
|
|||
/// There are some optimizations in this class, for example, you must
|
|||
/// never peek more than 8 bits more than needed, and you must first
|
|||
/// peek bits before you may drop them. This is not a general purpose
|
|||
/// class but optimized for the behaviour of the Inflater.
|
|||
///
|
|||
/// authors of the original java version : John Leuner, Jochen Hoenicke
|
|||
/// </summary>
|
|||
public class StreamManipulator |
|||
{ |
|||
#region Constructors
|
|||
/// <summary>
|
|||
/// Constructs a default StreamManipulator with all buffers empty
|
|||
/// </summary>
|
|||
public StreamManipulator() |
|||
{ |
|||
} |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Get the next sequence of bits but don't increase input pointer. bitCount must be
|
|||
/// less or equal 16 and if this call succeeds, you must drop
|
|||
/// at least n - 8 bits in the next call.
|
|||
/// </summary>
|
|||
/// <param name="bitCount">The number of bits to peek.</param>
|
|||
/// <returns>
|
|||
/// the value of the bits, or -1 if not enough bits available. */
|
|||
/// </returns>
|
|||
public int PeekBits(int bitCount) |
|||
{ |
|||
if (bitsInBuffer_ < bitCount) |
|||
{ |
|||
if (windowStart_ == windowEnd_) |
|||
{ |
|||
return -1; // ok
|
|||
} |
|||
buffer_ |= (uint)((window_[windowStart_++] & 0xff | |
|||
(window_[windowStart_++] & 0xff) << 8) << bitsInBuffer_); |
|||
bitsInBuffer_ += 16; |
|||
} |
|||
return (int)(buffer_ & ((1 << bitCount) - 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Drops the next n bits from the input. You should have called PeekBits
|
|||
/// with a bigger or equal n before, to make sure that enough bits are in
|
|||
/// the bit buffer.
|
|||
/// </summary>
|
|||
/// <param name="bitCount">The number of bits to drop.</param>
|
|||
public void DropBits(int bitCount) |
|||
{ |
|||
buffer_ >>= bitCount; |
|||
bitsInBuffer_ -= bitCount; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the next n bits and increases input pointer. This is equivalent
|
|||
/// to <see cref="PeekBits"/> followed by <see cref="DropBits"/>, except for correct error handling.
|
|||
/// </summary>
|
|||
/// <param name="bitCount">The number of bits to retrieve.</param>
|
|||
/// <returns>
|
|||
/// the value of the bits, or -1 if not enough bits available.
|
|||
/// </returns>
|
|||
public int GetBits(int bitCount) |
|||
{ |
|||
int bits = PeekBits(bitCount); |
|||
if (bits >= 0) |
|||
{ |
|||
DropBits(bitCount); |
|||
} |
|||
return bits; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of bits available in the bit buffer. This must be
|
|||
/// only called when a previous PeekBits() returned -1.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// the number of bits available.
|
|||
/// </returns>
|
|||
public int AvailableBits |
|||
{ |
|||
get |
|||
{ |
|||
return bitsInBuffer_; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of bytes available.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The number of bytes available.
|
|||
/// </returns>
|
|||
public int AvailableBytes |
|||
{ |
|||
get |
|||
{ |
|||
return windowEnd_ - windowStart_ + (bitsInBuffer_ >> 3); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Skips to the next byte boundary.
|
|||
/// </summary>
|
|||
public void SkipToByteBoundary() |
|||
{ |
|||
buffer_ >>= (bitsInBuffer_ & 7); |
|||
bitsInBuffer_ &= ~7; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns true when SetInput can be called
|
|||
/// </summary>
|
|||
public bool IsNeedingInput |
|||
{ |
|||
get |
|||
{ |
|||
return windowStart_ == windowEnd_; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies bytes from input buffer to output buffer starting
|
|||
/// at output[offset]. You have to make sure, that the buffer is
|
|||
/// byte aligned. If not enough bytes are available, copies fewer
|
|||
/// bytes.
|
|||
/// </summary>
|
|||
/// <param name="output">
|
|||
/// The buffer to copy bytes to.
|
|||
/// </param>
|
|||
/// <param name="offset">
|
|||
/// The offset in the buffer at which copying starts
|
|||
/// </param>
|
|||
/// <param name="length">
|
|||
/// The length to copy, 0 is allowed.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The number of bytes copied, 0 if no bytes were available.
|
|||
/// </returns>
|
|||
/// <exception cref="ArgumentOutOfRangeException">
|
|||
/// Length is less than zero
|
|||
/// </exception>
|
|||
/// <exception cref="InvalidOperationException">
|
|||
/// Bit buffer isnt byte aligned
|
|||
/// </exception>
|
|||
public int CopyBytes(byte[] output, int offset, int length) |
|||
{ |
|||
if (length < 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("length"); |
|||
} |
|||
|
|||
if ((bitsInBuffer_ & 7) != 0) |
|||
{ |
|||
// bits_in_buffer may only be 0 or a multiple of 8
|
|||
throw new InvalidOperationException("Bit buffer is not byte aligned!"); |
|||
} |
|||
|
|||
int count = 0; |
|||
while ((bitsInBuffer_ > 0) && (length > 0)) |
|||
{ |
|||
output[offset++] = (byte)buffer_; |
|||
buffer_ >>= 8; |
|||
bitsInBuffer_ -= 8; |
|||
length--; |
|||
count++; |
|||
} |
|||
|
|||
if (length == 0) |
|||
{ |
|||
return count; |
|||
} |
|||
|
|||
int avail = windowEnd_ - windowStart_; |
|||
if (length > avail) |
|||
{ |
|||
length = avail; |
|||
} |
|||
System.Array.Copy(window_, windowStart_, output, offset, length); |
|||
windowStart_ += length; |
|||
|
|||
if (((windowStart_ - windowEnd_) & 1) != 0) |
|||
{ |
|||
// We always want an even number of bytes in input, see peekBits
|
|||
buffer_ = (uint)(window_[windowStart_++] & 0xff); |
|||
bitsInBuffer_ = 8; |
|||
} |
|||
return count + length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Resets state and empties internal buffers
|
|||
/// </summary>
|
|||
public void Reset() |
|||
{ |
|||
buffer_ = 0; |
|||
windowStart_ = windowEnd_ = bitsInBuffer_ = 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Add more input for consumption.
|
|||
/// Only call when IsNeedingInput returns true
|
|||
/// </summary>
|
|||
/// <param name="buffer">data to be input</param>
|
|||
/// <param name="offset">offset of first byte of input</param>
|
|||
/// <param name="count">number of bytes of input to add.</param>
|
|||
public void SetInput(byte[] buffer, int offset, int count) |
|||
{ |
|||
if (buffer == null) |
|||
{ |
|||
throw new ArgumentNullException("buffer"); |
|||
} |
|||
|
|||
if (offset < 0) |
|||
{ |
|||
#if NETCF_1_0
|
|||
throw new ArgumentOutOfRangeException("offset"); |
|||
#else
|
|||
throw new ArgumentOutOfRangeException("offset", "Cannot be negative"); |
|||
#endif
|
|||
} |
|||
|
|||
if (count < 0) |
|||
{ |
|||
#if NETCF_1_0
|
|||
throw new ArgumentOutOfRangeException("count"); |
|||
#else
|
|||
throw new ArgumentOutOfRangeException("count", "Cannot be negative"); |
|||
#endif
|
|||
} |
|||
|
|||
if (windowStart_ < windowEnd_) |
|||
{ |
|||
throw new InvalidOperationException("Old input was not completely processed"); |
|||
} |
|||
|
|||
int end = offset + count; |
|||
|
|||
// We want to throw an ArrayIndexOutOfBoundsException early.
|
|||
// Note the check also handles integer wrap around.
|
|||
if ((offset > end) || (end > buffer.Length)) |
|||
{ |
|||
throw new ArgumentOutOfRangeException("count"); |
|||
} |
|||
|
|||
if ((count & 1) != 0) |
|||
{ |
|||
// We always want an even number of bytes in input, see PeekBits
|
|||
buffer_ |= (uint)((buffer[offset++] & 0xff) << bitsInBuffer_); |
|||
bitsInBuffer_ += 8; |
|||
} |
|||
|
|||
window_ = buffer; |
|||
windowStart_ = offset; |
|||
windowEnd_ = end; |
|||
} |
|||
|
|||
#region Instance Fields
|
|||
private byte[] window_; |
|||
private int windowStart_; |
|||
private int windowEnd_; |
|||
|
|||
private uint buffer_; |
|||
private int bitsInBuffer_; |
|||
#endregion
|
|||
} |
|||
} |
|||
@ -1,263 +0,0 @@ |
|||
// <copyright file="ZipConstants.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Formats |
|||
{ |
|||
using System.Text; |
|||
|
|||
/// <summary>
|
|||
/// This class contains constants used for Zip format files
|
|||
/// </summary>
|
|||
public static class ZipConstants |
|||
{ |
|||
/// <summary>
|
|||
/// The version made by field for entries in the central header when created by this library
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// This is also the Zip version for the library when comparing against the version required to extract
|
|||
/// for an entry. </remarks>
|
|||
public const int VersionMadeBy = 51; // was 45 before AES
|
|||
|
|||
/// <summary>
|
|||
/// The minimum version required to support strong encryption
|
|||
/// </summary>
|
|||
public const int VersionStrongEncryption = 50; |
|||
|
|||
/// <summary>
|
|||
/// Version indicating AES encryption
|
|||
/// </summary>
|
|||
public const int VERSION_AES = 51; |
|||
|
|||
/// <summary>
|
|||
/// The version required for Zip64 extensions (4.5 or higher)
|
|||
/// </summary>
|
|||
public const int VersionZip64 = 45; |
|||
|
|||
/// <summary>
|
|||
/// Size of local entry header (excluding variable length fields at end)
|
|||
/// </summary>
|
|||
public const int LocalHeaderBaseSize = 30; |
|||
|
|||
/// <summary>
|
|||
/// Size of Zip64 data descriptor
|
|||
/// </summary>
|
|||
public const int Zip64DataDescriptorSize = 24; |
|||
|
|||
/// <summary>
|
|||
/// Size of data descriptor
|
|||
/// </summary>
|
|||
public const int DataDescriptorSize = 16; |
|||
|
|||
/// <summary>
|
|||
/// Size of central header entry (excluding variable fields)
|
|||
/// </summary>
|
|||
public const int CentralHeaderBaseSize = 46; |
|||
|
|||
/// <summary>
|
|||
/// Size of end of central record (excluding variable fields)
|
|||
/// </summary>
|
|||
public const int EndOfCentralRecordBaseSize = 22; |
|||
|
|||
/// <summary>
|
|||
/// Size of 'classic' cryptographic header stored before any entry data
|
|||
/// </summary>
|
|||
public const int CryptoHeaderSize = 12; |
|||
|
|||
/// <summary>
|
|||
/// Signature for local entry header
|
|||
/// </summary>
|
|||
public const int LocalHeaderSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for spanning entry
|
|||
/// </summary>
|
|||
public const int SpanningSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for temporary spanning entry
|
|||
/// </summary>
|
|||
public const int SpanningTempSignature = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for data descriptor
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// This is only used where the length, Crc, or compressed size isnt known when the
|
|||
/// entry is created and the output stream doesnt support seeking.
|
|||
/// The local entry cannot be 'patched' with the correct values in this case
|
|||
/// so the values are recorded after the data prefixed by this header, as well as in the central directory.
|
|||
/// </remarks>
|
|||
public const int DataDescriptorSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for central header
|
|||
/// </summary>
|
|||
public const int CentralHeaderSignature = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for Zip64 central file header
|
|||
/// </summary>
|
|||
public const int Zip64CentralFileHeaderSignature = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for Zip64 central directory locator
|
|||
/// </summary>
|
|||
public const int Zip64CentralDirLocatorSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Signature for archive extra data signature (were headers are encrypted).
|
|||
/// </summary>
|
|||
public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); |
|||
|
|||
/// <summary>
|
|||
/// Central header digitial signature
|
|||
/// </summary>
|
|||
public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); |
|||
|
|||
/// <summary>
|
|||
/// End of central directory record signature
|
|||
/// </summary>
|
|||
public const int EndOfCentralDirectorySignature = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); |
|||
|
|||
/// <summary>
|
|||
/// PCL don't support CodePage so we used Encoding instead of
|
|||
/// </summary>
|
|||
public static Encoding DefaultEncoding { get; set; } = Encoding.UTF8; |
|||
|
|||
/// <summary>
|
|||
/// Convert a portion of a byte array to a string.
|
|||
/// </summary>
|
|||
/// <param name="data">
|
|||
/// Data to convert to string
|
|||
/// </param>
|
|||
/// <param name="count">
|
|||
/// Number of bytes to convert starting from index 0
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// data[0]..data[count - 1] converted to a string
|
|||
/// </returns>
|
|||
public static string ConvertToString(byte[] data, int count) |
|||
{ |
|||
if (data == null) |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
|
|||
return DefaultEncoding.GetString(data, 0, count); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert a byte array to string
|
|||
/// </summary>
|
|||
/// <param name="data">
|
|||
/// Byte array to convert
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// <paramref name="data">data</paramref>converted to a string
|
|||
/// </returns>
|
|||
public static string ConvertToString(byte[] data) |
|||
{ |
|||
if (data == null) |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
|
|||
return ConvertToString(data, data.Length); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert a byte array to string
|
|||
/// </summary>
|
|||
/// <param name="flags">The applicable general purpose bits flags</param>
|
|||
/// <param name="data">
|
|||
/// Byte array to convert
|
|||
/// </param>
|
|||
/// <param name="count">The number of bytes to convert.</param>
|
|||
/// <returns>
|
|||
/// <paramref name="data">data</paramref>converted to a string
|
|||
/// </returns>
|
|||
public static string ConvertToStringExt(int flags, byte[] data, int count) |
|||
{ |
|||
if (data == null) |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
|
|||
if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) |
|||
{ |
|||
return Encoding.UTF8.GetString(data, 0, count); |
|||
} |
|||
else |
|||
{ |
|||
return ConvertToString(data, count); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert a byte array to string
|
|||
/// </summary>
|
|||
/// <param name="flags">The applicable general purpose bits flags</param>
|
|||
/// <param name="data">Byte array to convert</param>
|
|||
/// <returns>
|
|||
/// <paramref name="data">data</paramref>converted to a string
|
|||
/// </returns>
|
|||
public static string ConvertToStringExt(int flags, byte[] data) |
|||
{ |
|||
if (data == null) |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
|
|||
if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) |
|||
{ |
|||
return Encoding.UTF8.GetString(data, 0, data.Length); |
|||
} |
|||
else |
|||
{ |
|||
return ConvertToString(data, data.Length); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert a string to a byte array
|
|||
/// </summary>
|
|||
/// <param name="str">
|
|||
/// String to convert to an array
|
|||
/// </param>
|
|||
/// <returns>Converted array</returns>
|
|||
public static byte[] ConvertToArray(string str) |
|||
{ |
|||
if (str == null) |
|||
{ |
|||
return new byte[0]; |
|||
} |
|||
|
|||
return DefaultEncoding.GetBytes(str); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Convert a string to a byte array
|
|||
/// </summary>
|
|||
/// <param name="flags">The applicable <see cref="GeneralBitFlags">general purpose bits flags</see></param>
|
|||
/// <param name="str">
|
|||
/// String to convert to an array
|
|||
/// </param>
|
|||
/// <returns>Converted array</returns>
|
|||
public static byte[] ConvertToArray(int flags, string str) |
|||
{ |
|||
if (str == null) |
|||
{ |
|||
return new byte[0]; |
|||
} |
|||
|
|||
if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) |
|||
{ |
|||
return Encoding.UTF8.GetBytes(str); |
|||
} |
|||
|
|||
return ConvertToArray(str); |
|||
} |
|||
} |
|||
} |
|||
@ -1 +0,0 @@ |
|||
e51bffc5ceb300a70039c9c3ce4542eff47925ea |
|||
Loading…
Reference in new issue