Skip to content

Commit

Permalink
suggestions from PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Apr 6, 2018
1 parent 57e1b3b commit dc6488a
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions KaitaiStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Kaitai
{
/// <summary>
/// The base Kaitai stream which exposes an API for the Kaitai Struct framework.
/// It's based off a <code>BinaryReader</code>, which is a little-endian reader.
/// It's based off a <c>BinaryReader</c>, which is a little-endian reader.
/// </summary>
public partial class KaitaiStream : BinaryReader
{
Expand All @@ -22,7 +22,7 @@ public KaitaiStream(Stream stream) : base(stream)
}

/// <summary>
/// Create a KaitaiStream backed by a closed file (in read-only binary-mode).
/// Create a KaitaiStream backed by a currently closed file (in read-only binary-mode).
/// </summary>
public KaitaiStream(string filename) : base(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Expand All @@ -36,23 +36,26 @@ public KaitaiStream(byte[] data) : base(new MemoryStream(data))
}

/// <summary>
/// Used internally.
/// Temporary 64-bit buffer for leftover bits left from an unaligned bit
/// read operation. Following unaligned bit operations would consume bits
/// left in this buffer first, and then, if needed, would continue
/// consuming bytes from the stream.
/// </summary>
private ulong Bits = 0;
/// <summary>
/// Used internally.
/// Number of bits left in <c>Bits</c> buffer.
/// </summary>
private int BitsLeft = 0;

/// <summary>
/// Used internally.
/// Caches the current plaftorm endianness and allows emited bytecode to be optimised. Thanks to @Arlorean.
/// Caches the current platform endianness and allows emitted bytecode to be optimized. Thanks to @Arlorean.
/// https://github.com/kaitai-io/kaitai_struct_csharp_runtime/pull/9
/// </summary>
static readonly bool IsLittleEndian = BitConverter.IsLittleEndian;

static KaitaiStream()
{
compute_single_rotations();
computeSingleRotations();
}

#endregion
Expand All @@ -61,7 +64,7 @@ static KaitaiStream()

/// <summary>
/// Check if the stream position is at the end of the stream (at EOF).
/// WARNING: This requires a seekable and tellable stream.
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
/// </summary>
public bool IsEof
{
Expand All @@ -70,7 +73,7 @@ public bool IsEof

/// <summary>
/// Move the stream to a specified absolute position.
/// WARNING: This requires a seekable stream.
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
/// </summary>
/// <param name="position">The position to seek to, as non-negative integer</param>
public void Seek(long position)
Expand All @@ -80,7 +83,7 @@ public void Seek(long position)

/// <summary>
/// Get the current position within the stream.
/// WARNING: This requires a tellable stream.
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
/// </summary>
public long Pos
{
Expand All @@ -89,7 +92,7 @@ public long Pos

/// <summary>
/// Get the total length of the stream (ie. file size).
/// WARNING: This requires a seekable and tellable stream.
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
/// </summary>
public long Size
{
Expand Down Expand Up @@ -518,7 +521,7 @@ public byte[] ProcessXor(byte[] data, byte[] key)
{
if (key.Length == 1)
return ProcessXor(data, key[0]);
if (key.Length <= 64 && ByteArrayZero(key))
if (key.Length <= 64 && IsByteArrayZero(key))
return data;

int dl = data.Length;
Expand All @@ -535,14 +538,14 @@ public byte[] ProcessXor(byte[] data, byte[] key)
/// <summary>
/// Used internally.
/// </summary>
private static byte[][] precomputed_single_rotations;
private static byte[][] precomputedSingleRotations;

/// <summary>
/// Used internally.
/// </summary>
private static void compute_single_rotations()
private static void computeSingleRotations()
{
precomputed_single_rotations = new byte[8][];
precomputedSingleRotations = new byte[8][];
for (int amount = 1; amount < 8; amount++)
{
byte[] translate = new byte[256];
Expand All @@ -551,7 +554,7 @@ private static void compute_single_rotations()
// formula taken from: http://stackoverflow.com/a/812039
translate[i] = (byte) ((i << amount) | (i >> (8 - amount)));
}
precomputed_single_rotations[amount] = translate;
precomputedSingleRotations[amount] = translate;
}
}

Expand All @@ -566,7 +569,7 @@ private static void compute_single_rotations()
public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
{
if (groupSize < 1)
throw new Exception("group size must be at least 1 to be valid");
throw new ArgumentException("group size must be at least 1 to be valid", "groupSize");

amount = Mod(amount, groupSize * 8);
if (amount == 0)
Expand All @@ -578,7 +581,7 @@ public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)

if (groupSize == 1)
{
byte[] translate = precomputed_single_rotations[amount];
byte[] translate = precomputedSingleRotations[amount];

for (int i = 0; i < dl; i++)
{
Expand Down Expand Up @@ -751,7 +754,7 @@ public static bool ByteArrayEqual(byte[] a, byte[] b)
/// <summary>
/// Check if byte array is all zeroes.
/// </summary>
public static bool ByteArrayZero(byte[] a)
public static bool IsByteArrayZero(byte[] a)
{
foreach (byte x in a)
if (x != 0)
Expand Down

0 comments on commit dc6488a

Please sign in to comment.