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 f912c61
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 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 Down Expand Up @@ -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 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 f912c61

Please sign in to comment.