From eed912a781f9b026674b21906d7356f2c5d798d2 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Mon, 22 Jan 2018 19:55:20 +0100 Subject: [PATCH 1/4] cleanup --- KaitaiStream.cs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 378c7ac..f0f1b6a 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -16,7 +16,6 @@ namespace Kaitai public KaitaiStream(Stream stream) : base(stream) { - } /// @@ -24,7 +23,6 @@ namespace Kaitai /// public KaitaiStream(string file) : base(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { - } /// @@ -32,7 +30,6 @@ namespace Kaitai /// public KaitaiStream(byte[] bytes) : base(new MemoryStream(bytes)) { - } private ulong Bits = 0; @@ -68,7 +65,7 @@ namespace Kaitai } /// - /// Get the total length of the stream + /// Get the total length of the stream (ie. file size) /// public long Size { @@ -322,14 +319,7 @@ namespace Kaitai private static ulong GetMaskOnes(int n) { - if (n == 64) - { - return 0xffffffffffffffffUL; - } - else - { - return (1UL << n) - 1; - } + return n == 64 ? 0xffffffffffffffffUL : (1UL << n) - 1; } #endregion @@ -360,9 +350,8 @@ namespace Kaitai { if (count > Int32.MaxValue) throw new ArgumentOutOfRangeException("requested " + count + " bytes, while only non-negative int32 amount of bytes possible"); - int cnt = (int)count; - byte[] bytes = base.ReadBytes(cnt); - if (bytes.Length < cnt) + byte[] bytes = base.ReadBytes((int)count); + if (bytes.Length < (int)count) throw new EndOfStreamException("requested " + count + " bytes, but got only " + bytes.Length + " bytes"); return bytes; } @@ -410,7 +399,7 @@ namespace Kaitai /// public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consumeTerminator, bool eosError) { - List bytes = new System.Collections.Generic.List(); + List bytes = new List(); while (true) { if (IsEof) From 5a9d8f36dd4c1870ae880e6871ff16b58430c74a Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Mon, 22 Jan 2018 19:56:02 +0100 Subject: [PATCH 2/4] ByteArrayCompare: array.Length field proper, not Count() LINQ --- KaitaiStream.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index f0f1b6a..99b6a25 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -633,8 +633,8 @@ namespace Kaitai { if (a == b) return 0; - int al = a.Count(); - int bl = b.Count(); + int al = a.Length; + int bl = b.Length; int minLen = al < bl ? al : bl; for (int i = 0; i < minLen; i++) { int cmp = a[i] - b[i]; From 77a748f2f92584aac6e6894e21ecc4a982e94c69 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Mon, 22 Jan 2018 20:00:29 +0100 Subject: [PATCH 3/4] EnsureFixedContents: array compare more proper style, better errormsg --- KaitaiStream.cs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 99b6a25..21749fc 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -428,26 +428,19 @@ namespace Kaitai public byte[] EnsureFixedContents(byte[] expected) { byte[] bytes = ReadBytes(expected.Length); - bool error = false; - if (bytes.Length == expected.Length) + + if (bytes.Length != expected.Length) { - for (int i = 0; i < bytes.Length; i++) + throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length)); + } + for (int i = 0; i < bytes.Length; i++) + { + if (bytes[i] != expected[i]) { - if (bytes[i] != expected[i]) - { - error = true; - break; - } + throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length)); } } - else - { - error = true; - } - if (error) - { - throw new Exception(string.Format("Expected bytes: {0}, Instead got: {1}", Convert.ToBase64String(expected), Convert.ToBase64String(bytes))); - } + return bytes; } From 632bde8f8a2f876ef4080ed80a70515e845e0c85 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Sun, 4 Feb 2018 15:34:18 +0100 Subject: [PATCH 4/4] using static IsLittleEndian (thanks @Arlorean) --- KaitaiStream.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 21749fc..ea2b48f 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -35,6 +35,8 @@ namespace Kaitai private ulong Bits = 0; private int BitsLeft = 0; + static readonly bool IsLittleEndian = BitConverter.IsLittleEndian; + #endregion #region Stream positioning @@ -364,7 +366,7 @@ namespace Kaitai protected byte[] ReadBytesNormalisedLittleEndian(int count) { byte[] bytes = ReadBytes(count); - if (!BitConverter.IsLittleEndian) Array.Reverse(bytes); + if (!IsLittleEndian) Array.Reverse(bytes); return bytes; } @@ -376,7 +378,7 @@ namespace Kaitai protected byte[] ReadBytesNormalisedBigEndian(int count) { byte[] bytes = ReadBytes(count); - if (BitConverter.IsLittleEndian) Array.Reverse(bytes); + if (IsLittleEndian) Array.Reverse(bytes); return bytes; }