From dce9d98885708550243bf4a6fb43a92546431db5 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 18 Nov 2024 02:45:07 -0500 Subject: [PATCH] Add GZIP models --- SabreTools.Models/GZIP/Archive.cs | 18 +++ SabreTools.Models/GZIP/Constants.cs | 10 ++ SabreTools.Models/GZIP/Enums.cs | 147 +++++++++++++++++++++++ SabreTools.Models/GZIP/ExtraFieldData.cs | 26 ++++ SabreTools.Models/GZIP/Header.cs | 88 ++++++++++++++ SabreTools.Models/GZIP/Trailer.cs | 26 ++++ 6 files changed, 315 insertions(+) create mode 100644 SabreTools.Models/GZIP/Archive.cs create mode 100644 SabreTools.Models/GZIP/Constants.cs create mode 100644 SabreTools.Models/GZIP/Enums.cs create mode 100644 SabreTools.Models/GZIP/ExtraFieldData.cs create mode 100644 SabreTools.Models/GZIP/Header.cs create mode 100644 SabreTools.Models/GZIP/Trailer.cs diff --git a/SabreTools.Models/GZIP/Archive.cs b/SabreTools.Models/GZIP/Archive.cs new file mode 100644 index 0000000..29b4c6a --- /dev/null +++ b/SabreTools.Models/GZIP/Archive.cs @@ -0,0 +1,18 @@ +namespace SabreTools.Models.GZIP +{ + /// + public sealed class Archive + { + /// + /// Header including optional fields + /// + public Header? Header; + + // Compressed blocks live here + + /// + /// Trailer + /// + public Trailer? Trailer; + } +} \ No newline at end of file diff --git a/SabreTools.Models/GZIP/Constants.cs b/SabreTools.Models/GZIP/Constants.cs new file mode 100644 index 0000000..592df16 --- /dev/null +++ b/SabreTools.Models/GZIP/Constants.cs @@ -0,0 +1,10 @@ +namespace SabreTools.Models.GZIP +{ + /// + public static class Constants + { + public const byte ID1 = 0x1F; + + public const byte ID2 = 0x8B; + } +} \ No newline at end of file diff --git a/SabreTools.Models/GZIP/Enums.cs b/SabreTools.Models/GZIP/Enums.cs new file mode 100644 index 0000000..1582806 --- /dev/null +++ b/SabreTools.Models/GZIP/Enums.cs @@ -0,0 +1,147 @@ +using System; + +namespace SabreTools.Models.GZIP +{ + /// + public enum CompressionMethod : byte + { + RESERVED0 = 0, + RESERVED1 = 1, + RESERVED2 = 2, + RESERVED3 = 3, + RESERVED4 = 4, + RESERVED5 = 5, + RESERVED6 = 6, + RESERVED7 = 7, + + Deflate = 8, + } + + /// + [Flags] + public enum ExtraFlags : byte + { + /// + /// Compressor used maximum compression, slowest algorithm + /// + MaximumCompression = 0x02, + + /// + /// Compressor used fastest algorithm + /// + FastestAlgorithm = 0x04, + } + + /// + [Flags] + public enum Flags : byte + { + /// + /// The file is probably ASCII text + /// + FTEXT = 0x01, + + /// + /// CRC-16 for the gzip header is present + /// + FHCRC = 0x02, + + /// + /// Optional extra fields are present + /// + FEXTRA = 0x04, + + /// + /// Original filename is present + /// + FNAME = 0x08, + + /// + /// Zero-terminated file comment is present + /// + FCOMMENT = 0x10, + + RESERVED5 = 0x20, + RESERVED6 = 0x40, + RESERVED7 = 0x80, + } + + /// + public enum OperatingSystem : byte + { + /// + /// FAT filesystem (MS-DOS, OS/2, NT/Win32) + /// + FAT = 0, + + /// + /// Amiga + /// + Amiga = 1, + + /// + /// VMS (or OpenVMS) + /// + VMS = 2, + + /// + /// Unix + /// + Unix = 3, + + /// + /// VM/CMS + /// + VMCMS = 4, + + /// + /// Atari TOS + /// + AtariTOS = 5, + + /// + /// HPFS filesystem (OS/2, NT) + /// + HPFS = 6, + + /// + /// Macintosh + /// + Macintosh = 7, + + /// + /// Z-System + /// + ZSystem = 8, + + /// + /// CP/M + /// + CPM = 9, + + /// + /// TOPS-20 + /// + TOPS20 = 10, + + /// + /// NTFS filesystem (NT) + /// + NTFS = 11, + + /// + /// QDOS + /// + QDOS = 12, + + /// + /// Acorn RISCOS + /// + AcornRISCOS = 13, + + /// + /// unknown + /// + Unknown = 255, + } +} \ No newline at end of file diff --git a/SabreTools.Models/GZIP/ExtraFieldData.cs b/SabreTools.Models/GZIP/ExtraFieldData.cs new file mode 100644 index 0000000..779ce35 --- /dev/null +++ b/SabreTools.Models/GZIP/ExtraFieldData.cs @@ -0,0 +1,26 @@ +namespace SabreTools.Models.GZIP +{ + /// + public sealed class ExtraFieldData + { + /// + /// SI1 and SI2 provide a subfield ID, typically two ASCII letters + /// with some mnemonic value. + /// + public byte SI1; + + /// + /// SI1 and SI2 provide a subfield ID, typically two ASCII letters + /// with some mnemonic value. + /// + public byte SI2; + + /// + /// LEN gives the length of the subfield data, excluding the 4 + /// initial bytes. + /// + public ushort LEN; + + public byte[]? Data; + } +} \ No newline at end of file diff --git a/SabreTools.Models/GZIP/Header.cs b/SabreTools.Models/GZIP/Header.cs new file mode 100644 index 0000000..f09e3b4 --- /dev/null +++ b/SabreTools.Models/GZIP/Header.cs @@ -0,0 +1,88 @@ +namespace SabreTools.Models.GZIP +{ + /// + public sealed class Header + { + /// + /// IDentification 1 (0x1F) + /// + public byte ID1; + + /// + /// IDentification 2 (0x8B) + /// + public byte ID2; + + /// + /// Compression Method + /// + public CompressionMethod CM; + + /// + /// FLaGs + /// + public Flags FLG; + + /// + /// Modification TIME + /// + /// This gives the most recent modification time of the original + /// file being compressed. The time is in Unix format, i.e., + /// seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this + /// may cause problems for MS-DOS and other systems that use + /// local rather than Universal time.) If the compressed data + /// did not come from a file, MTIME is set to the time at which + /// compression started. MTIME = 0 means no time stamp is + /// available. + /// + public uint MTIME; + + /// + /// eXtra FLags + /// + public ExtraFlags XFL; + + /// + /// Operating System + /// + /// This identifies the type of file system on which compression + /// took place. This may be useful in determining end-of-line + /// convention for text files. + /// + public OperatingSystem OS; + + /// + /// eXtra LENgth + /// + /// If FLG.FEXTRA is set, this gives the length of the optional + /// extra field. + /// + public ushort XLEN; + + /// + /// Extra field + /// + /// If the FLG.FEXTRA bit is set, an "extra field" is present in + /// the header, with total length XLEN bytes. It consists of a + /// series of subfields, each of the form . + /// + public ExtraFieldData[]? ExtraField; + + /// + /// Original filename before compression, zero-terminated + /// + public string? OriginalFileName; + + /// + /// File comment, zero terminated + /// + public string? FileComment; + + /// + /// The CRC16 consists of the two least significant bytes of the + /// CRC32 for all bytes of the gzip header up to and not including + /// the CRC16. + /// + public ushort? CRC16; + } +} \ No newline at end of file diff --git a/SabreTools.Models/GZIP/Trailer.cs b/SabreTools.Models/GZIP/Trailer.cs new file mode 100644 index 0000000..9a878ef --- /dev/null +++ b/SabreTools.Models/GZIP/Trailer.cs @@ -0,0 +1,26 @@ +namespace SabreTools.Models.GZIP +{ + /// + public sealed class Trailer + { + /// + /// CRC-32 + /// + /// This contains a Cyclic Redundancy Check value of the + /// uncompressed data computed according to CRC-32 algorithm + /// used in the ISO 3309 standard and in section 8.1.1.6.2 of + /// ITU-T recommendation V.42. (See http://www.iso.ch for + /// ordering ISO documents. See gopher://info.itu.ch for an + /// online version of ITU-T V.42.) + /// + public uint CRC32; + + /// + /// Input SIZE + /// + /// This contains the size of the original (uncompressed) input + /// data modulo 2^32. + /// + public uint ISIZE; + } +} \ No newline at end of file