// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Dtf.Compression.Cab { using System; using System.IO; using System.Resources; using System.Globalization; using System.Runtime.Serialization; /// /// Exception class for cabinet operations. /// [Serializable] public class CabException : ArchiveException { private static ResourceManager errorResources; private int error; private int errorCode; /// /// Creates a new CabException with a specified error message and a reference to the /// inner exception that is the cause of this exception. /// /// The message that describes the error. /// The exception that is the cause of the current exception. If the /// innerException parameter is not a null reference (Nothing in Visual Basic), the current exception /// is raised in a catch block that handles the inner exception. public CabException(string message, Exception innerException) : this(0, 0, message, innerException) { } /// /// Creates a new CabException with a specified error message. /// /// The message that describes the error. public CabException(string message) : this(0, 0, message, null) { } /// /// Creates a new CabException. /// public CabException() : this(0, 0, null, null) { } internal CabException(int error, int errorCode, string message, Exception innerException) : base(message, innerException) { this.error = error; this.errorCode = errorCode; } internal CabException(int error, int errorCode, string message) : this(error, errorCode, message, null) { } /// /// Initializes a new instance of the CabException class with serialized data. /// /// The SerializationInfo that holds the serialized object data about the exception being thrown. /// The StreamingContext that contains contextual information about the source or destination. protected CabException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) { throw new ArgumentNullException("info"); } this.error = info.GetInt32("cabError"); this.errorCode = info.GetInt32("cabErrorCode"); } /// /// Gets the FCI or FDI cabinet engine error number. /// /// A cabinet engine error number, or 0 if the exception was /// not related to a cabinet engine error number. public int Error { get { return this.error; } } /// /// Gets the Win32 error code. /// /// A Win32 error code, or 0 if the exception was /// not related to a Win32 error. public int ErrorCode { get { return this.errorCode; } } internal static ResourceManager ErrorResources { get { if (errorResources == null) { errorResources = new ResourceManager( typeof(CabException).Namespace + ".Errors", typeof(CabException).Assembly); } return errorResources; } } /// /// Sets the SerializationInfo with information about the exception. /// /// The SerializationInfo that holds the serialized object data about the exception being thrown. /// The StreamingContext that contains contextual information about the source or destination. public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } info.AddValue("cabError", this.error); info.AddValue("cabErrorCode", this.errorCode); base.GetObjectData(info, context); } internal static string GetErrorMessage(int error, int errorCode, bool extracting) { const int FCI_ERROR_RESOURCE_OFFSET = 1000; const int FDI_ERROR_RESOURCE_OFFSET = 2000; int resourceOffset = (extracting ? FDI_ERROR_RESOURCE_OFFSET : FCI_ERROR_RESOURCE_OFFSET); string msg = CabException.ErrorResources.GetString( (resourceOffset + error).ToString(CultureInfo.InvariantCulture.NumberFormat), CultureInfo.CurrentCulture); if (msg == null) { msg = CabException.ErrorResources.GetString( resourceOffset.ToString(CultureInfo.InvariantCulture.NumberFormat), CultureInfo.CurrentCulture); } if (errorCode != 0) { const string GENERIC_ERROR_RESOURCE = "1"; string msg2 = CabException.ErrorResources.GetString(GENERIC_ERROR_RESOURCE, CultureInfo.CurrentCulture); msg = String.Format(CultureInfo.InvariantCulture, "{0} " + msg2, msg, errorCode); } return msg; } } }