// ZipConstants.cs // // Copyright (C) 2001 Mike Krueger // Copyright (C) 2004 John Reilly // // This file was translated from java, it was part of the GNU Classpath // Copyright (C) 2001 Free Software Foundation, Inc. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // Linking this library statically or dynamically with other modules is // making a combined work based on this library. Thus, the terms and // conditions of the GNU General Public License cover the whole // combination. // // As a special exception, the copyright holders of this library give you // permission to link this library with independent modules to produce an // executable, regardless of the license terms of these independent // modules, and to copy and distribute the resulting executable under // terms of your choice, provided that you also meet, for each linked // independent module, the terms and conditions of the license of that // module. An independent module is a module which is not derived from // or based on this library. If you modify this library, you may extend // this exception to your version of the library, but you are not // obligated to do so. If you do not wish to do so, delete this // exception statement from your version. // HISTORY // 22-12-2009 DavidPierson Added AES support using System; using System.Globalization; using System.Text; using System.Threading; #if NETCF_1_0 || NETCF_2_0 using System.Globalization; #endif namespace PdfSharp.SharpZipLib.Zip { #region Enumerations /// /// Determines how entries are tested to see if they should use Zip64 extensions or not. /// public enum UseZip64 { /// /// Zip64 will not be forced on entries during processing. /// /// An entry can have this overridden if required ZipEntry.ForceZip64" Off, /// /// Zip64 should always be used. /// On, /// /// #ZipLib will determine use based on entry values when added to archive. /// Dynamic, } /// /// The kind of compression used for an entry in an archive /// public enum CompressionMethod { /// /// A direct copy of the file contents is held in the archive /// Stored = 0, /// /// Common Zip compression method using a sliding dictionary /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees /// Deflated = 8, /// /// An extension to deflate with a 64KB window. Not supported by #Zip currently /// Deflate64 = 9, /// /// BZip2 compression. Not supported by #Zip. /// BZip2 = 11, /// /// WinZip special for AES encryption, Now supported by #Zip. /// WinZipAES = 99, } /// /// Identifies the encryption algorithm used for an entry /// public enum EncryptionAlgorithm { /// /// No encryption has been used. /// None = 0, /// /// Encrypted using PKZIP 2.0 or 'classic' encryption. /// PkzipClassic = 1, /// /// DES encryption has been used. /// Des = 0x6601, /// /// RC2 encryption has been used for encryption. /// RC2 = 0x6602, /// /// Triple DES encryption with 168 bit keys has been used for this entry. /// TripleDes168 = 0x6603, /// /// Triple DES with 112 bit keys has been used for this entry. /// TripleDes112 = 0x6609, /// /// AES 128 has been used for encryption. /// Aes128 = 0x660e, /// /// AES 192 has been used for encryption. /// Aes192 = 0x660f, /// /// AES 256 has been used for encryption. /// Aes256 = 0x6610, /// /// RC2 corrected has been used for encryption. /// RC2Corrected = 0x6702, /// /// Blowfish has been used for encryption. /// Blowfish = 0x6720, /// /// Twofish has been used for encryption. /// Twofish = 0x6721, /// /// RC4 has been used for encryption. /// RC4 = 0x6801, /// /// An unknown algorithm has been used for encryption. /// Unknown = 0xffff } /// /// Defines the contents of the general bit flags field for an archive entry. /// [Flags] public enum GeneralBitFlags : int { /// /// Bit 0 if set indicates that the file is encrypted /// Encrypted = 0x0001, /// /// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating) /// Method = 0x0006, /// /// Bit 3 if set indicates a trailing data desciptor is appended to the entry data /// Descriptor = 0x0008, /// /// Bit 4 is reserved for use with method 8 for enhanced deflation /// ReservedPKware4 = 0x0010, /// /// Bit 5 if set indicates the file contains Pkzip compressed patched data. /// Requires version 2.7 or greater. /// Patched = 0x0020, /// /// Bit 6 if set indicates strong encryption has been used for this entry. /// StrongEncryption = 0x0040, /// /// Bit 7 is currently unused /// Unused7 = 0x0080, /// /// Bit 8 is currently unused /// Unused8 = 0x0100, /// /// Bit 9 is currently unused /// Unused9 = 0x0200, /// /// Bit 10 is currently unused /// Unused10 = 0x0400, /// /// Bit 11 if set indicates the filename and /// comment fields for this file must be encoded using UTF-8. /// UnicodeText = 0x0800, /// /// Bit 12 is documented as being reserved by PKware for enhanced compression. /// EnhancedCompress = 0x1000, /// /// Bit 13 if set indicates that values in the local header are masked to hide /// their actual values, and the central directory is encrypted. /// /// /// Used when encrypting the central directory contents. /// HeaderMasked = 0x2000, /// /// Bit 14 is documented as being reserved for use by PKware /// ReservedPkware14 = 0x4000, /// /// Bit 15 is documented as being reserved for use by PKware /// ReservedPkware15 = 0x8000 } #endregion /// /// This class contains constants used for Zip format files /// internal sealed class ZipConstants { #region Versions /// /// The version made by field for entries in the central header when created by this library /// /// /// This is also the Zip version for the library when comparing against the version required to extract /// for an entry. See ZipEntry.CanDecompress. /// public const int VersionMadeBy = 51; // was 45 before AES /// /// The version made by field for entries in the central header when created by this library /// /// /// This is also the Zip version for the library when comparing against the version required to extract /// for an entry. See ZipInputStream.CanDecompressEntry. /// [Obsolete("Use VersionMadeBy instead")] public const int VERSION_MADE_BY = 51; /// /// The minimum version required to support strong encryption /// public const int VersionStrongEncryption = 50; /// /// The minimum version required to support strong encryption /// [Obsolete("Use VersionStrongEncryption instead")] public const int VERSION_STRONG_ENCRYPTION = 50; /// /// Version indicating AES encryption /// public const int VERSION_AES = 51; /// /// The version required for Zip64 extensions (4.5 or higher) /// public const int VersionZip64 = 45; #endregion #region Header Sizes /// /// Size of local entry header (excluding variable length fields at end) /// public const int LocalHeaderBaseSize = 30; /// /// Size of local entry header (excluding variable length fields at end) /// [Obsolete("Use LocalHeaderBaseSize instead")] public const int LOCHDR = 30; /// /// Size of Zip64 data descriptor /// public const int Zip64DataDescriptorSize = 24; /// /// Size of data descriptor /// public const int DataDescriptorSize = 16; /// /// Size of data descriptor /// [Obsolete("Use DataDescriptorSize instead")] public const int EXTHDR = 16; /// /// Size of central header entry (excluding variable fields) /// public const int CentralHeaderBaseSize = 46; /// /// Size of central header entry /// [Obsolete("Use CentralHeaderBaseSize instead")] public const int CENHDR = 46; /// /// Size of end of central record (excluding variable fields) /// public const int EndOfCentralRecordBaseSize = 22; /// /// Size of end of central record (excluding variable fields) /// [Obsolete("Use EndOfCentralRecordBaseSize instead")] public const int ENDHDR = 22; /// /// Size of 'classic' cryptographic header stored before any entry data /// public const int CryptoHeaderSize = 12; /// /// Size of cryptographic header stored before entry data /// [Obsolete("Use CryptoHeaderSize instead")] public const int CRYPTO_HEADER_SIZE = 12; #endregion #region Header Signatures /// /// Signature for local entry header /// public const int LocalHeaderSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); /// /// Signature for local entry header /// [Obsolete("Use LocalHeaderSignature instead")] public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); /// /// Signature for spanning entry /// public const int SpanningSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for spanning entry /// [Obsolete("Use SpanningSignature instead")] public const int SPANNINGSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for temporary spanning entry /// public const int SpanningTempSignature = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); /// /// Signature for temporary spanning entry /// [Obsolete("Use SpanningTempSignature instead")] public const int SPANTEMPSIG = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); /// /// Signature for data descriptor /// /// /// 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. /// public const int DataDescriptorSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for data descriptor /// /// /// 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. /// [Obsolete("Use DataDescriptorSignature instead")] public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for central header /// [Obsolete("Use CentralHeaderSignature instead")] public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); /// /// Signature for central header /// public const int CentralHeaderSignature = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); /// /// Signature for Zip64 central file header /// public const int Zip64CentralFileHeaderSignature = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); /// /// Signature for Zip64 central file header /// [Obsolete("Use Zip64CentralFileHeaderSignature instead")] public const int CENSIG64 = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); /// /// Signature for Zip64 central directory locator /// public const int Zip64CentralDirLocatorSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); /// /// Signature for archive extra data signature (were headers are encrypted). /// public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); /// /// Central header digitial signature /// public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); /// /// Central header digitial signature /// [Obsolete("Use CentralHeaderDigitalSignaure instead")] public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); /// /// End of central directory record signature /// public const int EndOfCentralDirectorySignature = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); /// /// End of central directory record signature /// [Obsolete("Use EndOfCentralDirectorySignature instead")] public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); #endregion #if true//NETCF_1_0 || NETCF_2_0 // This isn't so great but is better than nothing. // Trying to work out an appropriate OEM code page would be good. // 850 is a good default for English speakers particularly in Europe. #if SILVERLIGHT || NETFX_CORE || UWP // TODO Do we need this for PDFsharp? If so, make it work. static int defaultCodePage = 65001; #else static int defaultCodePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage; #endif #else /// /// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc. /// But sometimes it yields the special value of 1 which is nicknamed CodePageNoOEM in sources (might also mean CP_OEMCP, but Encoding puts it so). /// This was observed on Ukranian and Hindu systems. /// Given this value, throws an . /// So replace it with some fallback, e.g. 437 which is the default codepage in a console in a default Windows installation. /// static int defaultCodePage = // these values cause ArgumentException in subsequent calls to Encoding::GetEncoding() ((Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 1) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 2) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 3) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 42)) ? 437 // The default OEM encoding in a console in a default Windows installation, as a fallback. : Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage; #endif /// /// Default encoding used for string conversion. 0 gives the default system OEM code page. /// Dont use unicode encodings if you want to be Zip compatible! /// Using the default code page isnt the full solution necessarily /// there are many variable factors, codepage 850 is often a good choice for /// European users, however be careful about compatibility. /// public static int DefaultCodePage { get { return defaultCodePage; } set { if ((value < 0) || (value > 65535) || (value == 1) || (value == 2) || (value == 3) || (value == 42)) { throw new ArgumentOutOfRangeException("value"); } defaultCodePage = value; } } /// /// Convert a portion of a byte array to a string. /// /// /// Data to convert to string /// /// /// Number of bytes to convert starting from index 0 /// /// /// data[0]..data[count - 1] converted to a string /// public static string ConvertToString(byte[] data, int count) { if (data == null) { return string.Empty; } #if SILVERLIGHT || NETFX_CORE return Encoding.GetEncoding("utf-8").GetString(data, 0, count); #else return Encoding.GetEncoding(DefaultCodePage).GetString(data, 0, count); #endif } /// /// Convert a byte array to string /// /// /// Byte array to convert /// /// /// dataconverted to a string /// public static string ConvertToString(byte[] data) { if (data == null) { return string.Empty; } return ConvertToString(data, data.Length); } /// /// Convert a byte array to string /// /// The applicable general purpose bits flags /// /// Byte array to convert /// /// The number of bytes to convert. /// /// dataconverted to a string /// 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); } } /// /// Convert a byte array to string /// /// /// Byte array to convert /// /// The applicable general purpose bits flags /// /// dataconverted to a string /// 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); } } /// /// Convert a string to a byte array /// /// /// String to convert to an array /// /// Converted array public static byte[] ConvertToArray(string str) { if (str == null) { return new byte[0]; } #if SILVERLIGHT || NETFX_CORE return Encoding.GetEncoding("utf-8").GetBytes(str); #else return Encoding.GetEncoding(DefaultCodePage).GetBytes(str); #endif } /// /// Convert a string to a byte array /// /// The applicable general purpose bits flags /// /// String to convert to an array /// /// Converted array 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); } else { return ConvertToArray(str); } } /// /// Initialise default instance of ZipConstants /// /// /// Private to prevent instances being created. /// ZipConstants() { // Do nothing } } }