From fb4ed409fb4273d355d5f635275a0d8b1bb5dc04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 15 Jul 2018 21:14:33 +0200 Subject: [PATCH 1/8] Add some generic decoding exception classes --- .../Exceptions}/SharpZipBaseException.cs | 0 .../Exceptions/StreamDecodingException.cs | 35 +++++++++++++++ .../Exceptions/StreamUnsupportedException.cs | 33 ++++++++++++++ .../UnexpectedEndOfStreamException.cs | 33 ++++++++++++++ .../Exceptions/ValueOutOfRangeException.cs | 45 +++++++++++++++++++ 5 files changed, 146 insertions(+) rename src/ICSharpCode.SharpZipLib/{ => Core/Exceptions}/SharpZipBaseException.cs (100%) create mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs create mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs create mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs create mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs diff --git a/src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs similarity index 100% rename from src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs rename to src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs new file mode 100644 index 000000000..4882a9f4d --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that an error occured during decoding of a input stream due to corrupt + /// data or (unintentional) library incompability. + /// + public class StreamDecodingException: SharpZipBaseException + { + private const string GenericMessage = "Input stream could not be decoded"; + + /// + /// Initializes a new instance of the StreamDecodingException with a generic message + /// + public StreamDecodingException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the StreamDecodingException class with a specified error message. + /// + /// A message describing the exception. + public StreamDecodingException(string message) : base(message) { } + + + /// + /// Initializes a new instance of the StreamDecodingException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public StreamDecodingException(string message, Exception innerException) : base(message, innerException) { } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs new file mode 100644 index 000000000..ff8579c4d --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs @@ -0,0 +1,33 @@ +using System; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that the input stream could not decoded due to known library incompability or missing features + /// + public class StreamUnsupportedException : StreamDecodingException + { + private const string GenericMessage = "Input stream is in a unsupported format"; + + /// + /// Initializes a new instance of the StreamUnsupportedException with a generic message + /// + public StreamUnsupportedException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the StreamUnsupportedException class with a specified error message. + /// + /// A message describing the exception. + public StreamUnsupportedException(string message) : base(message) { } + + + /// + /// Initializes a new instance of the StreamUnsupportedException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public StreamUnsupportedException(string message, Exception innerException) : base(message, innerException) { } + + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs new file mode 100644 index 000000000..975d6f796 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs @@ -0,0 +1,33 @@ +using System; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that the input stream could not decoded due to the stream ending before enough data had been provided + /// + public class UnexpectedEndOfStreamException : StreamDecodingException + { + private const string GenericMessage = "Input stream ended unexpectedly"; + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException with a generic message + /// + public UnexpectedEndOfStreamException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified error message. + /// + /// A message describing the exception. + public UnexpectedEndOfStreamException(string message) : base(message) { } + + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public UnexpectedEndOfStreamException(string message, Exception innerException) : base(message, innerException) { } + + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs new file mode 100644 index 000000000..47a3aaea5 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICSharpCode.SharpZipLib +{ + + /// + /// Indicates that a value was outside of the expected range when decoding an input stream + /// + public class ValueOutOfRangeException : StreamDecodingException + { + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable + /// + /// Name of the variable, use: nameof() + public ValueOutOfRangeException(string nameOfValue ) + : base($"{nameOfValue} out of range") { } + + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable, + /// it's current value and expected range. + /// + /// Name of the variable, use: nameof() + /// The invalid value + /// Expected maximum value + /// Expected minimum value + public ValueOutOfRangeException(string nameOfValue, long value, long maxValue, long minValue = 0) + : this(nameOfValue, value.ToString(), maxValue.ToString(), minValue.ToString()) { } + + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable, + /// it's current value and expected range. + /// + /// Name of the variable, use: nameof() + /// The invalid value + /// Expected maximum value + /// Expected minimum value + public ValueOutOfRangeException(string nameOfValue, string value, string maxValue, string minValue = "0") : + base($"{nameOfValue} out of range: {value}, should be {minValue}..{maxValue}") { } + + private ValueOutOfRangeException() { } + private ValueOutOfRangeException(string message, Exception innerException) : base(message, innerException) {} + } +} From 2f16effd85d9eadd1f9febf0c1bb856adab91126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sat, 16 Jun 2018 20:03:19 +0200 Subject: [PATCH 2/8] Fix infinite loop on bad literal data Returning true here just puts us in an infinite loop, not sure what was supposed to happen? Fixes #229. --- src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs index c2c0bbce0..f97eda391 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs @@ -273,7 +273,8 @@ private bool DecodeHuffman() while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) { outputWindow.Write(symbol); if (--free < 258) { - return true; + // If this happens we will be stuck in an infinite loop. + throw new SharpZipBaseException("Literal symbol count exceeds window size, input file may be corrupt."); } } From 3ff2b63f67ba45096b31f78b25cf4e3ba5437e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sat, 16 Jun 2018 22:39:01 +0200 Subject: [PATCH 3/8] Revert "Fix infinite loop on bad literal data" This reverts commit 2dcb6562518ecaed53efc6dde96538a0f61bb537. --- src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs index f97eda391..c2c0bbce0 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs @@ -273,8 +273,7 @@ private bool DecodeHuffman() while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) { outputWindow.Write(symbol); if (--free < 258) { - // If this happens we will be stuck in an infinite loop. - throw new SharpZipBaseException("Literal symbol count exceeds window size, input file may be corrupt."); + return true; } } From 184fae35a5a5b8df29cff8976951bb59e4cdbc8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 15 Jul 2018 21:19:23 +0200 Subject: [PATCH 4/8] Use IList for building Huffman Trees * Permits using ArraySegment instead of copying the source array * Formatting --- .../Zip/Compression/InflaterHuffmanTree.cs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs index 152a4029f..ffb141984 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; namespace ICSharpCode.SharpZipLib.Zip.Compression @@ -63,18 +64,18 @@ static InflaterHuffmanTree() /// /// the array of code lengths /// - public InflaterHuffmanTree(byte[] codeLengths) + public InflaterHuffmanTree(IList codeLengths) { BuildTree(codeLengths); } #endregion - void BuildTree(byte[] codeLengths) + void BuildTree(IList codeLengths) { int[] blCount = new int[MAX_BITLEN + 1]; int[] nextCode = new int[MAX_BITLEN + 1]; - for (int i = 0; i < codeLengths.Length; i++) { + for (int i = 0; i < codeLengths.Count; i++) { int bits = codeLengths[i]; if (bits > 0) { blCount[bits]++; @@ -115,7 +116,7 @@ void BuildTree(byte[] codeLengths) } } - for (int i = 0; i < codeLengths.Length; i++) { + for (int i = 0; i < codeLengths.Count; i++) { int bits = codeLengths[i]; if (bits == 0) { continue; @@ -154,36 +155,49 @@ void BuildTree(byte[] codeLengths) public int GetSymbol(StreamManipulator input) { int lookahead, symbol; - if ((lookahead = input.PeekBits(9)) >= 0) { - if ((symbol = tree[lookahead]) >= 0) { + 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) { + if ((lookahead = input.PeekBits(bitlen)) >= 0) + { symbol = tree[subtree | (lookahead >> 9)]; input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[subtree | (lookahead >> 9)]; - if ((symbol & 15) <= bits) { + if ((symbol & 15) <= bits) + { input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { return -1; } } - } else { + } + else // Less than 9 bits + { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[lookahead]; - if (symbol >= 0 && (symbol & 15) <= bits) { + if (symbol >= 0 && (symbol & 15) <= bits) + { input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { return -1; } } From cb538170627ebd2dd8abaed2ed390ecc351ca2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 15 Jul 2018 21:34:18 +0200 Subject: [PATCH 5/8] Update Inflater dynamic header reader to support partial reads * Uses Enumerable state machine * Skips two array copies by using ArraySegment * Throws usable exceptions when invalid values are being read --- .../Zip/Compression/Inflater.cs | 8 +- .../Zip/Compression/InflaterDynHeader.cs | 193 +++++++++++------- .../Compression/Streams/StreamManipulator.cs | 36 +++- 3 files changed, 154 insertions(+), 83 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs index c2c0bbce0..e68009fce 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs @@ -429,7 +429,7 @@ private bool Decode() mode = DECODE_HUFFMAN; break; case DeflaterConstants.DYN_TREES: - dynHeader = new InflaterDynHeader(); + dynHeader = new InflaterDynHeader(input); mode = DECODE_DYN_HEADER; break; default: @@ -470,12 +470,12 @@ private bool Decode() } case DECODE_DYN_HEADER: - if (!dynHeader.Decode(input)) { + if (!dynHeader.AttemptRead()) { return false; } - litlenTree = dynHeader.BuildLitLenTree(); - distTree = dynHeader.BuildDistTree(); + litlenTree = dynHeader.LiteralLengthTree; + distTree = dynHeader.DistanceTree; mode = DECODE_HUFFMAN; goto case DECODE_HUFFMAN; // fall through diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs index b7c5c3f33..b36b32c2b 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; namespace ICSharpCode.SharpZipLib.Zip.Compression @@ -7,100 +8,148 @@ class InflaterDynHeader { #region Constants - static readonly int[] BL_ORDER = - { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + // maximum number of literal/length codes + const int LITLEN_MAX = 286; + + // maximum number of distance codes + const int DIST_MAX = 30; + + // maximum data code lengths to read + const int CODELEN_MAX = LITLEN_MAX + DIST_MAX; + + // maximum meta code length codes to read + const int META_MAX = 19; + + static readonly int[] MetaCodeLengthIndex = + { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + #endregion - public bool Decode(StreamManipulator input) + /// + /// Continue decoding header from until more bits are needed or decoding has been completed + /// + /// Returns whether decoding could be completed + public bool AttemptRead() + => !state.MoveNext() || state.Current; + + public InflaterDynHeader(StreamManipulator input) { - try + this.input = input; + stateMachine = CreateStateMachine(); + state = stateMachine.GetEnumerator(); + } + + private IEnumerable CreateStateMachine() + { + + // Read initial code length counts from header + while (!input.TryGetBits(5, ref litLenCodeCount, 257)) yield return false; + while (!input.TryGetBits(5, ref distanceCodeCount, 1)) yield return false; + while (!input.TryGetBits(4, ref metaCodeCount, 4)) yield return false; + var dataCodeCount = litLenCodeCount + distanceCodeCount; + + if (litLenCodeCount > LITLEN_MAX) throw new ValueOutOfRangeException(nameof(litLenCodeCount)); + if (distanceCodeCount > DIST_MAX) throw new ValueOutOfRangeException(nameof(distanceCodeCount)); + if (metaCodeCount > META_MAX) throw new ValueOutOfRangeException(nameof(metaCodeCount)); + + // Load code lengths for the meta tree from the header bits + for (int i=0; i < metaCodeCount; i++) + { + while (!input.TryGetBits(3, ref codeLengths, MetaCodeLengthIndex[i])) yield return false; + } + + var metaCodeTree = new InflaterHuffmanTree(codeLengths); + + // Decompress the meta tree symbols into the data table code lengths + int index = 0; + while (index < dataCodeCount) { - lnum = input.GrabBits(5) + 257; - dnum = input.GrabBits(5) + 1; - blnum = input.GrabBits(4) + 4; - num = lnum + dnum; + byte codeLength; + int symbol; - lengths = new byte[19]; + while ((symbol = metaCodeTree.GetSymbol(input)) < 0) yield return false; - for (int i = 0; i < blnum; i++) + if (symbol < 16) { - lengths[BL_ORDER[i]] = (byte)input.GrabBits(3, true); + // append literal code length + codeLengths[index++] = (byte)symbol; } - blTree = new InflaterHuffmanTree(lengths); - lengths = new byte[num]; - - int index = 0; - while (index < lnum + dnum) + else { - byte len; - - int symbol = blTree.GetSymbol(input); - if (symbol < 0) - return false; - if (symbol < 16) - lengths[index++] = (byte)symbol; - else + int repeatCount = 0; + + if (symbol == 16) // Repeat last code length 3..6 times + { + + if (index == 0) + throw new StreamDecodingException("Cannot repeat previous code length when no other code length has been read"); + + codeLength = codeLengths[index - 1]; + + // 2 bits + 3, [3..6] + while(!input.TryGetBits(2, ref repeatCount, 3)) yield return false; + } + else if (symbol == 17) // Repeat zero 3..10 times { - len = 0; - if (symbol == 16) - { - if (index == 0) - return false; // No last length! - len = lengths[index - 1]; - symbol = input.GrabBits(2, true) + 3; - } - else if (symbol == 17) - { - // repeat zero 3..10 times - symbol = input.GrabBits(3, true) + 3; - } - else - { - // (symbol == 18), repeat zero 11..138 times - symbol = input.GrabBits(7, true) + 11; - } - - if (index + symbol > lnum + dnum) - return false; // too many lengths! - - // repeat last or zero symbol times - while (symbol-- > 0) - lengths[index++] = len; + codeLength = 0; + + // 3 bits + 3, [3..10] + while (!input.TryGetBits(3, ref repeatCount, 3)) yield return false; } - } + else // (symbol == 18), Repeat zero 11..138 times + { + codeLength = 0; - if (lengths[256] == 0) - return false; // No end-of-block code! + // 7 bits + 11, [11..138] + while (!input.TryGetBits(7, ref repeatCount, 11)) yield return false; + } - return true; - } - catch (Exception x) - { - return false; + if (index + repeatCount > dataCodeCount) + throw new StreamDecodingException("Cannot repeat code lengths past total number of data code lengths"); + + while (repeatCount-- > 0) + codeLengths[index++] = codeLength; + } } - } - public InflaterHuffmanTree BuildLitLenTree() - { - byte[] litlenLens = new byte[lnum]; - Array.Copy(lengths, 0, litlenLens, 0, lnum); - return new InflaterHuffmanTree(litlenLens); - } + if (codeLengths[256] == 0) + throw new StreamDecodingException("Inflater dynamic header end-of-block code missing"); - public InflaterHuffmanTree BuildDistTree() - { - byte[] distLens = new byte[dnum]; - Array.Copy(lengths, lnum, distLens, 0, dnum); - return new InflaterHuffmanTree(distLens); + litLenTree = new InflaterHuffmanTree(new ArraySegment(codeLengths, 0, litLenCodeCount)); + distTree = new InflaterHuffmanTree(new ArraySegment(codeLengths, litLenCodeCount, distanceCodeCount)); + + yield return true; } + /// + /// Get literal/length huffman tree, must not be used before has returned true + /// + /// If hader has not been successfully read by the state machine + public InflaterHuffmanTree LiteralLengthTree + => litLenTree ?? throw new StreamDecodingException("Header properties were accessed before header had been successfully read"); + + /// + /// Get distance huffman tree, must not be used before has returned true + /// + /// If hader has not been successfully read by the state machine + public InflaterHuffmanTree DistanceTree + => distTree ?? throw new StreamDecodingException("Header properties were accessed before header had been successfully read"); + #region Instance Fields - byte[] lengths; - InflaterHuffmanTree blTree; + private readonly StreamManipulator input; + private readonly IEnumerator state; + private readonly IEnumerable stateMachine; + + private byte[] codeLengths = new byte[CODELEN_MAX]; + + private InflaterHuffmanTree litLenTree; + private InflaterHuffmanTree distTree; + + int litLenCodeCount, distanceCodeCount, metaCodeCount; - int lnum, dnum, blnum, num; #endregion } + } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs index 80a26c428..f3850f5d0 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs @@ -42,16 +42,38 @@ public int PeekBits(int bitCount) } /// - /// Grabs the next n bits from the input and throws if is false and the result is 0. + /// Tries to grab the next bits from the input and + /// sets to the value, adding . /// - public int GrabBits(int bitCount, bool allowZero = false) + /// true if enough bits could be read, otherwise false + public bool TryGetBits(int bitCount, ref int output, int outputOffset = 0) { - var val = PeekBits(bitCount); - if (!allowZero && val == 0) - throw new SharpZipBaseException(bitCount + "-bit value cannot be zero"); + var bits = PeekBits(bitCount); + if (bits < 0) + { + return false; + } + output = bits + outputOffset; DropBits(bitCount); - return val; - } + return true; + } + + /// + /// Tries to grab the next bits from the input and + /// sets to the value, adding . + /// + /// true if enough bits could be read, otherwise false + public bool TryGetBits(int bitCount, ref byte[] array, int index) + { + var bits = PeekBits(bitCount); + if (bits < 0) + { + return false; + } + array[index] = (byte)bits; + DropBits(bitCount); + return true; + } /// /// Drops the next n bits from the input. You should have called PeekBits From 0e70f009d69cc742f288e67e6dbc1b908fbd6c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Fri, 13 Jul 2018 19:34:00 +0200 Subject: [PATCH 6/8] Temporarily merge appveyor.yml to allow package generation of PR --- appveyor.yml | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 000000000..20bff86e6 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,73 @@ +version: '{build}' +image: Visual Studio 2017 +configuration: +- Debug +- Release +dotnet_csproj: + patch: true + file: '**\*.csproj' + version: $(VERSION) + package_version: $(VERSION) + assembly_version: 1.0.0.999 + file_version: 1.0.0.999 + informational_version: $(VERSION) +install: +- ps: |- + $commit = $(git rev-parse --short HEAD) + + $masterBranches = @("master"); + + if ($masterBranches -contains $env:APPVEYOR_REPO_BRANCH) { + $branch = ""; + } else { + $branch = "-$env:APPVEYOR_REPO_BRANCH"; + } + + if ($env:APPVEYOR_PULL_REQUEST_NUMBER) { + $suffix = "-pr$env:APPVEYOR_PULL_REQUEST_NUMBER"; + } else { + $suffix = ""; + } + + $build = "_${env:APPVEYOR_BUILD_NUMBER}" + + $version = "1.0-git$commit"; + + $av_version = "$version$branch$suffix$build"; + $env:APPVEYOR_BUILD_VERSION=$av_version; + $env:VERSION=$version; + + write-host -n "new version: "; + write-host -f green $av_version; + + appveyor UpdateBuild -Version $av_version +nuget: + project_feed: true + disable_publish_on_pr: true +before_build: +- ps: nuget restore ICSharpCode.SharpZipLib.sln +build: + project: ICSharpCode.SharpZipLib.sln + publish_nuget: true + publish_nuget_symbols: true + verbosity: normal +test_script: +- ps: |- + $proj = ".\test\ICSharpCode.SharpZipLib.TestBootstrapper\ICSharpCode.SharpZipLib.TestBootstrapper.csproj"; + $resxml = ".\docs\nunit3-test-results-debug.xml"; + + # Nuget 3 Console runner: + #$tester = "nunit3-console .\test\ICSharpCode.SharpZipLib.Tests\bin\$($env:CONFIGURATION)\netcoreapp2.0\ICSharpCode.SharpZipLib.Tests.dll" + + # Bootstrapper: + $tester = "dotnet run -f netcoreapp2 -p $proj -c $env:CONFIGURATION"; + iex "$tester --explore=tests.xml"; + + [xml]$xml = Get-Content("tests.xml"); + $assembly = select-xml "/test-suite[@type='Assembly']" $xml | select -f 1 -exp Node; + $testcases = select-xml "//test-case" $xml | % { Add-AppveyorTest -Name $_.Node.fullname -Framework NUnit -Filename $assembly.name }; + + iex "$tester --result=$resxml"; + + $wc = New-Object 'System.Net.WebClient'; + $wc.UploadFile("https://ci.appveyor.com/api/testresults/nunit3/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $resxml)); From 99ed6045ac53a8de5caafb95af667e04bbe7554c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Thu, 19 Jul 2018 00:06:53 +0200 Subject: [PATCH 7/8] Revert "Temporarily merge appveyor.yml to allow package generation of PR" This reverts commit 0e70f009d69cc742f288e67e6dbc1b908fbd6c96. --- appveyor.yml | 73 ---------------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 20bff86e6..000000000 --- a/appveyor.yml +++ /dev/null @@ -1,73 +0,0 @@ -version: '{build}' -image: Visual Studio 2017 -configuration: -- Debug -- Release -dotnet_csproj: - patch: true - file: '**\*.csproj' - version: $(VERSION) - package_version: $(VERSION) - assembly_version: 1.0.0.999 - file_version: 1.0.0.999 - informational_version: $(VERSION) -install: -- ps: |- - $commit = $(git rev-parse --short HEAD) - - $masterBranches = @("master"); - - if ($masterBranches -contains $env:APPVEYOR_REPO_BRANCH) { - $branch = ""; - } else { - $branch = "-$env:APPVEYOR_REPO_BRANCH"; - } - - if ($env:APPVEYOR_PULL_REQUEST_NUMBER) { - $suffix = "-pr$env:APPVEYOR_PULL_REQUEST_NUMBER"; - } else { - $suffix = ""; - } - - $build = "_${env:APPVEYOR_BUILD_NUMBER}" - - $version = "1.0-git$commit"; - - $av_version = "$version$branch$suffix$build"; - $env:APPVEYOR_BUILD_VERSION=$av_version; - $env:VERSION=$version; - - write-host -n "new version: "; - write-host -f green $av_version; - - appveyor UpdateBuild -Version $av_version -nuget: - project_feed: true - disable_publish_on_pr: true -before_build: -- ps: nuget restore ICSharpCode.SharpZipLib.sln -build: - project: ICSharpCode.SharpZipLib.sln - publish_nuget: true - publish_nuget_symbols: true - verbosity: normal -test_script: -- ps: |- - $proj = ".\test\ICSharpCode.SharpZipLib.TestBootstrapper\ICSharpCode.SharpZipLib.TestBootstrapper.csproj"; - $resxml = ".\docs\nunit3-test-results-debug.xml"; - - # Nuget 3 Console runner: - #$tester = "nunit3-console .\test\ICSharpCode.SharpZipLib.Tests\bin\$($env:CONFIGURATION)\netcoreapp2.0\ICSharpCode.SharpZipLib.Tests.dll" - - # Bootstrapper: - $tester = "dotnet run -f netcoreapp2 -p $proj -c $env:CONFIGURATION"; - iex "$tester --explore=tests.xml"; - - [xml]$xml = Get-Content("tests.xml"); - $assembly = select-xml "/test-suite[@type='Assembly']" $xml | select -f 1 -exp Node; - $testcases = select-xml "//test-case" $xml | % { Add-AppveyorTest -Name $_.Node.fullname -Framework NUnit -Filename $assembly.name }; - - iex "$tester --result=$resxml"; - - $wc = New-Object 'System.Net.WebClient'; - $wc.UploadFile("https://ci.appveyor.com/api/testresults/nunit3/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $resxml)); From fee2accaa2a9e1c441c24dd04b1ee89cd31153bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Thu, 19 Jul 2018 00:19:09 +0200 Subject: [PATCH 8/8] Revert "Add some generic decoding exception classes" This reverts commit fb4ed409fb4273d355d5f635275a0d8b1bb5dc04. --- .../Exceptions/StreamDecodingException.cs | 35 --------------- .../Exceptions/StreamUnsupportedException.cs | 33 -------------- .../UnexpectedEndOfStreamException.cs | 33 -------------- .../Exceptions/ValueOutOfRangeException.cs | 45 ------------------- .../Exceptions => }/SharpZipBaseException.cs | 0 5 files changed, 146 deletions(-) delete mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs delete mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs delete mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs delete mode 100644 src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs rename src/ICSharpCode.SharpZipLib/{Core/Exceptions => }/SharpZipBaseException.cs (100%) diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs deleted file mode 100644 index 4882a9f4d..000000000 --- a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ICSharpCode.SharpZipLib -{ - /// - /// Indicates that an error occured during decoding of a input stream due to corrupt - /// data or (unintentional) library incompability. - /// - public class StreamDecodingException: SharpZipBaseException - { - private const string GenericMessage = "Input stream could not be decoded"; - - /// - /// Initializes a new instance of the StreamDecodingException with a generic message - /// - public StreamDecodingException() : base(GenericMessage) { } - - /// - /// Initializes a new instance of the StreamDecodingException class with a specified error message. - /// - /// A message describing the exception. - public StreamDecodingException(string message) : base(message) { } - - - /// - /// Initializes a new instance of the StreamDecodingException class with a specified - /// error message and a reference to the inner exception that is the cause of this exception. - /// - /// A message describing the exception. - /// The inner exception - public StreamDecodingException(string message, Exception innerException) : base(message, innerException) { } - } -} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs deleted file mode 100644 index ff8579c4d..000000000 --- a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace ICSharpCode.SharpZipLib -{ - /// - /// Indicates that the input stream could not decoded due to known library incompability or missing features - /// - public class StreamUnsupportedException : StreamDecodingException - { - private const string GenericMessage = "Input stream is in a unsupported format"; - - /// - /// Initializes a new instance of the StreamUnsupportedException with a generic message - /// - public StreamUnsupportedException() : base(GenericMessage) { } - - /// - /// Initializes a new instance of the StreamUnsupportedException class with a specified error message. - /// - /// A message describing the exception. - public StreamUnsupportedException(string message) : base(message) { } - - - /// - /// Initializes a new instance of the StreamUnsupportedException class with a specified - /// error message and a reference to the inner exception that is the cause of this exception. - /// - /// A message describing the exception. - /// The inner exception - public StreamUnsupportedException(string message, Exception innerException) : base(message, innerException) { } - - } -} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs deleted file mode 100644 index 975d6f796..000000000 --- a/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace ICSharpCode.SharpZipLib -{ - /// - /// Indicates that the input stream could not decoded due to the stream ending before enough data had been provided - /// - public class UnexpectedEndOfStreamException : StreamDecodingException - { - private const string GenericMessage = "Input stream ended unexpectedly"; - - /// - /// Initializes a new instance of the UnexpectedEndOfStreamException with a generic message - /// - public UnexpectedEndOfStreamException() : base(GenericMessage) { } - - /// - /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified error message. - /// - /// A message describing the exception. - public UnexpectedEndOfStreamException(string message) : base(message) { } - - - /// - /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified - /// error message and a reference to the inner exception that is the cause of this exception. - /// - /// A message describing the exception. - /// The inner exception - public UnexpectedEndOfStreamException(string message, Exception innerException) : base(message, innerException) { } - - } -} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs deleted file mode 100644 index 47a3aaea5..000000000 --- a/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ICSharpCode.SharpZipLib -{ - - /// - /// Indicates that a value was outside of the expected range when decoding an input stream - /// - public class ValueOutOfRangeException : StreamDecodingException - { - /// - /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable - /// - /// Name of the variable, use: nameof() - public ValueOutOfRangeException(string nameOfValue ) - : base($"{nameOfValue} out of range") { } - - /// - /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable, - /// it's current value and expected range. - /// - /// Name of the variable, use: nameof() - /// The invalid value - /// Expected maximum value - /// Expected minimum value - public ValueOutOfRangeException(string nameOfValue, long value, long maxValue, long minValue = 0) - : this(nameOfValue, value.ToString(), maxValue.ToString(), minValue.ToString()) { } - - /// - /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable, - /// it's current value and expected range. - /// - /// Name of the variable, use: nameof() - /// The invalid value - /// Expected maximum value - /// Expected minimum value - public ValueOutOfRangeException(string nameOfValue, string value, string maxValue, string minValue = "0") : - base($"{nameOfValue} out of range: {value}, should be {minValue}..{maxValue}") { } - - private ValueOutOfRangeException() { } - private ValueOutOfRangeException(string message, Exception innerException) : base(message, innerException) {} - } -} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs b/src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs similarity index 100% rename from src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs rename to src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs