Skip to content

Commit dc6488a

Browse files
committed
suggestions from PR comments
1 parent 57e1b3b commit dc6488a

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

KaitaiStream.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Kaitai
88
{
99
/// <summary>
1010
/// The base Kaitai stream which exposes an API for the Kaitai Struct framework.
11-
/// It's based off a <code>BinaryReader</code>, which is a little-endian reader.
11+
/// It's based off a <c>BinaryReader</c>, which is a little-endian reader.
1212
/// </summary>
1313
public partial class KaitaiStream : BinaryReader
1414
{
@@ -22,7 +22,7 @@ public KaitaiStream(Stream stream) : base(stream)
2222
}
2323

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

3838
/// <summary>
39-
/// Used internally.
39+
/// Temporary 64-bit buffer for leftover bits left from an unaligned bit
40+
/// read operation. Following unaligned bit operations would consume bits
41+
/// left in this buffer first, and then, if needed, would continue
42+
/// consuming bytes from the stream.
4043
/// </summary>
4144
private ulong Bits = 0;
4245
/// <summary>
43-
/// Used internally.
46+
/// Number of bits left in <c>Bits</c> buffer.
4447
/// </summary>
4548
private int BitsLeft = 0;
4649

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

5356
static KaitaiStream()
5457
{
55-
compute_single_rotations();
58+
computeSingleRotations();
5659
}
5760

5861
#endregion
@@ -61,7 +64,7 @@ static KaitaiStream()
6164

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

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

8184
/// <summary>
8285
/// Get the current position within the stream.
83-
/// WARNING: This requires a tellable stream.
86+
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
8487
/// </summary>
8588
public long Pos
8689
{
@@ -89,7 +92,7 @@ public long Pos
8992

9093
/// <summary>
9194
/// Get the total length of the stream (ie. file size).
92-
/// WARNING: This requires a seekable and tellable stream.
95+
/// WARNING: This requires a stream that supports seeking (memory-based or file-based).
9396
/// </summary>
9497
public long Size
9598
{
@@ -518,7 +521,7 @@ public byte[] ProcessXor(byte[] data, byte[] key)
518521
{
519522
if (key.Length == 1)
520523
return ProcessXor(data, key[0]);
521-
if (key.Length <= 64 && ByteArrayZero(key))
524+
if (key.Length <= 64 && IsByteArrayZero(key))
522525
return data;
523526

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

540543
/// <summary>
541544
/// Used internally.
542545
/// </summary>
543-
private static void compute_single_rotations()
546+
private static void computeSingleRotations()
544547
{
545-
precomputed_single_rotations = new byte[8][];
548+
precomputedSingleRotations = new byte[8][];
546549
for (int amount = 1; amount < 8; amount++)
547550
{
548551
byte[] translate = new byte[256];
@@ -551,7 +554,7 @@ private static void compute_single_rotations()
551554
// formula taken from: http://stackoverflow.com/a/812039
552555
translate[i] = (byte) ((i << amount) | (i >> (8 - amount)));
553556
}
554-
precomputed_single_rotations[amount] = translate;
557+
precomputedSingleRotations[amount] = translate;
555558
}
556559
}
557560

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

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

579582
if (groupSize == 1)
580583
{
581-
byte[] translate = precomputed_single_rotations[amount];
584+
byte[] translate = precomputedSingleRotations[amount];
582585

583586
for (int i = 0; i < dl; i++)
584587
{
@@ -751,7 +754,7 @@ public static bool ByteArrayEqual(byte[] a, byte[] b)
751754
/// <summary>
752755
/// Check if byte array is all zeroes.
753756
/// </summary>
754-
public static bool ByteArrayZero(byte[] a)
757+
public static bool IsByteArrayZero(byte[] a)
755758
{
756759
foreach (byte x in a)
757760
if (x != 0)

0 commit comments

Comments
 (0)