From fb4ed409fb4273d355d5f635275a0d8b1bb5dc04 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= <nils@piksel.se>
Date: Sun, 15 Jul 2018 21:14:33 +0200
Subject: [PATCH] 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
+{
+    /// <summary>
+    /// Indicates that an error occured during decoding of a input stream due to corrupt 
+	/// data or (unintentional) library incompability.
+    /// </summary>
+    public class StreamDecodingException: SharpZipBaseException
+    {
+		private const string GenericMessage = "Input stream could not be decoded";
+
+		/// <summary>
+		/// Initializes a new instance of the StreamDecodingException with a generic message
+		/// </summary>
+		public StreamDecodingException() : base(GenericMessage) { }
+
+		/// <summary>
+		/// Initializes a new instance of the StreamDecodingException class with a specified error message.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		public StreamDecodingException(string message) : base(message) { }
+
+
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		/// <param name="innerException">The inner exception</param>
+		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
+{
+	/// <summary>
+	/// Indicates that the input stream could not decoded due to known library incompability or missing features
+	/// </summary>
+	public class StreamUnsupportedException : StreamDecodingException
+	{
+		private const string GenericMessage = "Input stream is in a unsupported format";
+
+		/// <summary>
+		/// Initializes a new instance of the StreamUnsupportedException with a generic message
+		/// </summary>
+		public StreamUnsupportedException() : base(GenericMessage) { }
+
+		/// <summary>
+		/// Initializes a new instance of the StreamUnsupportedException class with a specified error message.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		public StreamUnsupportedException(string message) : base(message) { }
+
+
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		/// <param name="innerException">The inner exception</param>
+		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
+{
+	/// <summary>
+	/// Indicates that the input stream could not decoded due to the stream ending before enough data had been provided
+	/// </summary>
+	public class UnexpectedEndOfStreamException : StreamDecodingException
+	{
+		private const string GenericMessage = "Input stream ended unexpectedly";
+
+		/// <summary>
+		/// Initializes a new instance of the UnexpectedEndOfStreamException with a generic message
+		/// </summary>
+		public UnexpectedEndOfStreamException() : base(GenericMessage) { }
+
+		/// <summary>
+		/// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified error message.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		public UnexpectedEndOfStreamException(string message) : base(message) { }
+
+
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <param name="message">A message describing the exception.</param>
+		/// <param name="innerException">The inner exception</param>
+		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
+{
+
+	/// <summary>
+	/// Indicates that a value was outside of the expected range when decoding an input stream
+	/// </summary>
+	public class ValueOutOfRangeException : StreamDecodingException
+	{
+		/// <summary>
+		/// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable
+		/// </summary>
+		/// <param name="nameOfValue">Name of the variable, use: nameof()</param>
+		public ValueOutOfRangeException(string nameOfValue ) 
+			: base($"{nameOfValue} out of range") { }
+
+		/// <summary>
+		/// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable,
+		/// it's current value and expected range.
+		/// </summary>
+		/// <param name="nameOfValue">Name of the variable, use: nameof()</param>
+		/// <param name="value">The invalid value</param>
+		/// <param name="maxValue">Expected maximum value</param>
+		/// <param name="minValue">Expected minimum value</param>
+		public ValueOutOfRangeException(string nameOfValue, long value, long maxValue, long minValue = 0) 
+			: this(nameOfValue, value.ToString(), maxValue.ToString(), minValue.ToString()) { }
+
+		/// <summary>
+		/// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable,
+		/// it's current value and expected range.
+		/// </summary>
+		/// <param name="nameOfValue">Name of the variable, use: nameof()</param>
+		/// <param name="value">The invalid value</param>
+		/// <param name="maxValue">Expected maximum value</param>
+		/// <param name="minValue">Expected minimum value</param>
+		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) {}
+	}
+}
