Skip to content

Commit 8adf176

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

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

KaitaiStream.cs

Lines changed: 35 additions & 21 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 by opening a file in read-only binary mode (FileStream).
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
{
@@ -285,7 +288,11 @@ public double ReadF8le()
285288
#region Unaligned bit values
286289

287290
/// <summary>
288-
/// ???
291+
/// Clears the temporary buffer which holds not-yet-consumed parts of
292+
/// the byte, which might have left over after last unaligned bit read
293+
/// operation. Effectively, aligns the pointer in the stream to next
294+
/// whole byte, discarding the rest of partially byte, if it existed.
295+
/// Does nothing if unaligned bit reading is not used.
289296
/// </summary>
290297
public void AlignToByte()
291298
{
@@ -294,8 +301,15 @@ public void AlignToByte()
294301
}
295302

296303
/// <summary>
297-
/// ???
304+
/// Read next n bits from the stream. By convention, starts from most
305+
/// significant bits first and goes to least significant bits.
298306
/// </summary>
307+
/// <remarks>
308+
/// If n is not a multiple of 8, then bits left over from partially
309+
/// consumed byte would be stored in stream internal buffer. Subsequent
310+
/// calls to this method would consume bits from that buffer first, and
311+
/// then proceed with fetching the new bytes from the stream.
312+
/// </remarks>
299313
public ulong ReadBitsInt(int n)
300314
{
301315
int bitsNeeded = n - BitsLeft;
@@ -518,7 +532,7 @@ public byte[] ProcessXor(byte[] data, byte[] key)
518532
{
519533
if (key.Length == 1)
520534
return ProcessXor(data, key[0]);
521-
if (key.Length <= 64 && ByteArrayZero(key))
535+
if (key.Length <= 64 && IsByteArrayZero(key))
522536
return data;
523537

524538
int dl = data.Length;
@@ -535,14 +549,14 @@ public byte[] ProcessXor(byte[] data, byte[] key)
535549
/// <summary>
536550
/// Used internally.
537551
/// </summary>
538-
private static byte[][] precomputed_single_rotations;
552+
private static byte[][] precomputedSingleRotations;
539553

540554
/// <summary>
541555
/// Used internally.
542556
/// </summary>
543-
private static void compute_single_rotations()
557+
private static void computeSingleRotations()
544558
{
545-
precomputed_single_rotations = new byte[8][];
559+
precomputedSingleRotations = new byte[8][];
546560
for (int amount = 1; amount < 8; amount++)
547561
{
548562
byte[] translate = new byte[256];
@@ -551,7 +565,7 @@ private static void compute_single_rotations()
551565
// formula taken from: http://stackoverflow.com/a/812039
552566
translate[i] = (byte) ((i << amount) | (i >> (8 - amount)));
553567
}
554-
precomputed_single_rotations[amount] = translate;
568+
precomputedSingleRotations[amount] = translate;
555569
}
556570
}
557571

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

571585
amount = Mod(amount, groupSize * 8);
572586
if (amount == 0)
@@ -578,7 +592,7 @@ public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
578592

579593
if (groupSize == 1)
580594
{
581-
byte[] translate = precomputed_single_rotations[amount];
595+
byte[] translate = precomputedSingleRotations[amount];
582596

583597
for (int i = 0; i < dl; i++)
584598
{
@@ -751,7 +765,7 @@ public static bool ByteArrayEqual(byte[] a, byte[] b)
751765
/// <summary>
752766
/// Check if byte array is all zeroes.
753767
/// </summary>
754-
public static bool ByteArrayZero(byte[] a)
768+
public static bool IsByteArrayZero(byte[] a)
755769
{
756770
foreach (byte x in a)
757771
if (x != 0)

0 commit comments

Comments
 (0)