Skip to content

Commit 3436623

Browse files
committed
Extract KaitaiStreamBase containing utility methods.
1 parent 9c85cf0 commit 3436623

File tree

5 files changed

+121
-204
lines changed

5 files changed

+121
-204
lines changed

Kaitai.Struct.Runtime/Interface/IKaitaiStream.cs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
namespace Kaitai
22
{
3-
public interface IKaitaiStream
3+
public interface IKaitaiStream : IKaitaiStreamBase
44
{
5-
/// <summary>
6-
/// Check if the stream position is at the end of the stream
7-
/// </summary>
8-
bool IsEof { get; }
9-
10-
/// <summary>
11-
/// Get the current position in the stream
12-
/// </summary>
13-
long Pos { get; }
14-
15-
/// <summary>
16-
/// Get the total length of the stream (ie. file size)
17-
/// </summary>
18-
long Size { get; }
19-
205
/// <summary>
216
/// Seek to a specific position from the beginning of the stream
227
/// </summary>
@@ -131,7 +116,6 @@ public interface IKaitaiStream
131116
/// <returns></returns>
132117
double ReadF8le();
133118

134-
void AlignToByte();
135119
ulong ReadBitsInt(int n);
136120
ulong ReadBitsIntLe(int n);
137121

@@ -171,39 +155,5 @@ public interface IKaitaiStream
171155
/// <param name="expected">The expected result</param>
172156
/// <returns></returns>
173157
byte[] EnsureFixedContents(byte[] expected);
174-
175-
/// <summary>
176-
/// Performs XOR processing with given data, XORing every byte of the input with a single value.
177-
/// </summary>
178-
/// <param name="value">The data toe process</param>
179-
/// <param name="key">The key value to XOR with</param>
180-
/// <returns>Processed data</returns>
181-
byte[] ProcessXor(byte[] value, int key);
182-
183-
/// <summary>
184-
/// Performs XOR processing with given data, XORing every byte of the input with a key
185-
/// array, repeating from the beginning of the key array if necessary
186-
/// </summary>
187-
/// <param name="value">The data toe process</param>
188-
/// <param name="key">The key array to XOR with</param>
189-
/// <returns>Processed data</returns>
190-
byte[] ProcessXor(byte[] value, byte[] key);
191-
192-
/// <summary>
193-
/// Performs a circular left rotation shift for a given buffer by a given amount of bits.
194-
/// Pass a negative amount to rotate right.
195-
/// </summary>
196-
/// <param name="data">The data to rotate</param>
197-
/// <param name="amount">The number of bytes to rotate by</param>
198-
/// <param name="groupSize"></param>
199-
/// <returns></returns>
200-
byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize);
201-
202-
/// <summary>
203-
/// Inflates a deflated zlib byte stream
204-
/// </summary>
205-
/// <param name="data">The data to deflate</param>
206-
/// <returns>The deflated result</returns>
207-
byte[] ProcessZlib(byte[] data);
208158
}
209159
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace Kaitai
2+
{
3+
public interface IKaitaiStreamBase
4+
{
5+
/// <summary>
6+
/// Check if the stream position is at the end of the stream
7+
/// </summary>
8+
bool IsEof { get; }
9+
10+
/// <summary>
11+
/// Get the current position in the stream
12+
/// </summary>
13+
long Pos { get; }
14+
15+
/// <summary>
16+
/// Get the total length of the stream (ie. file size)
17+
/// </summary>
18+
long Size { get; }
19+
20+
void AlignToByte();
21+
22+
/// <summary>
23+
/// Performs XOR processing with given data, XORing every byte of the input with a single value.
24+
/// </summary>
25+
/// <param name="value">The data toe process</param>
26+
/// <param name="key">The key value to XOR with</param>
27+
/// <returns>Processed data</returns>
28+
byte[] ProcessXor(byte[] value, int key);
29+
30+
/// <summary>
31+
/// Performs XOR processing with given data, XORing every byte of the input with a key
32+
/// array, repeating from the beginning of the key array if necessary
33+
/// </summary>
34+
/// <param name="value">The data toe process</param>
35+
/// <param name="key">The key array to XOR with</param>
36+
/// <returns>Processed data</returns>
37+
byte[] ProcessXor(byte[] value, byte[] key);
38+
39+
/// <summary>
40+
/// Performs a circular left rotation shift for a given buffer by a given amount of bits.
41+
/// Pass a negative amount to rotate right.
42+
/// </summary>
43+
/// <param name="data">The data to rotate</param>
44+
/// <param name="amount">The number of bytes to rotate by</param>
45+
/// <param name="groupSize"></param>
46+
/// <returns></returns>
47+
byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize);
48+
49+
/// <summary>
50+
/// Inflates a deflated zlib byte stream
51+
/// </summary>
52+
/// <param name="data">The data to deflate</param>
53+
/// <returns>The deflated result</returns>
54+
byte[] ProcessZlib(byte[] data);
55+
}
56+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=interface/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Kaitai.Struct.Runtime/KaitaiStream.cs

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.IO.Compression;
54

65
namespace Kaitai
76
{
87
/// <summary>
98
/// The base Kaitai stream which exposes an API for the Kaitai Struct framework.
109
/// It's based off a <code>BinaryReader</code>, which is a little-endian reader.
1110
/// </summary>
12-
public partial class KaitaiStream : IKaitaiStream
11+
public partial class KaitaiStream : KaitaiStreamBase, IKaitaiStream
1312
{
1413
#region Constructors
15-
16-
static readonly bool IsLittleEndian = BitConverter.IsLittleEndian;
17-
1814
private ulong Bits;
1915
private int BitsLeft;
2016
private BinaryReader m_binaryReader;
@@ -53,7 +49,7 @@ protected BinaryReader BinaryReader
5349
/// <summary>
5450
/// Check if the stream position is at the end of the stream
5551
/// </summary>
56-
public bool IsEof
52+
public override bool IsEof
5753
{
5854
get { return BaseStream.Position >= BaseStream.Length && BitsLeft == 0; }
5955
}
@@ -70,15 +66,15 @@ public void Seek(long position)
7066
/// <summary>
7167
/// Get the current position in the stream
7268
/// </summary>
73-
public long Pos
69+
public override long Pos
7470
{
7571
get { return BaseStream.Position; }
7672
}
7773

7874
/// <summary>
7975
/// Get the total length of the stream (ie. file size)
8076
/// </summary>
81-
public long Size
77+
public override long Size
8278
{
8379
get { return BaseStream.Length; }
8480
}
@@ -289,7 +285,7 @@ public double ReadF8le()
289285

290286
#region Unaligned bit values
291287

292-
public void AlignToByte()
288+
public override void AlignToByte()
293289
{
294290
Bits = 0;
295291
BitsLeft = 0;
@@ -361,11 +357,6 @@ public ulong ReadBitsIntLe(int n)
361357
return res;
362358
}
363359

364-
private static ulong GetMaskOnes(int n)
365-
{
366-
return n == 64 ? 0xffffffffffffffffUL : (1UL << n) - 1;
367-
}
368-
369360
#endregion
370361

371362
#region Byte arrays
@@ -441,7 +432,10 @@ public byte[] ReadBytesFull()
441432
/// <param name="consumeTerminator">True to consume the terminator byte before returning</param>
442433
/// <param name="eosError">True to throw an error when the EOS was reached before the terminator</param>
443434
/// <returns></returns>
444-
public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consumeTerminator, bool eosError)
435+
public byte[] ReadBytesTerm(byte terminator,
436+
bool includeTerminator,
437+
bool consumeTerminator,
438+
bool eosError)
445439
{
446440
List<byte> bytes = new List<byte>();
447441
while (true)
@@ -492,112 +486,6 @@ public byte[] EnsureFixedContents(byte[] expected)
492486
return bytes;
493487
}
494488

495-
public static byte[] BytesStripRight(byte[] src, byte padByte)
496-
{
497-
return Utilities.BytesStripRight(src, padByte);
498-
}
499-
500-
public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTerminator)
501-
{
502-
return Utilities.BytesTerminate(src, terminator, includeTerminator);
503-
}
504-
#endregion
505-
506-
#region Byte array processing
507-
508-
/// <summary>
509-
/// Performs XOR processing with given data, XORing every byte of the input with a single value.
510-
/// </summary>
511-
/// <param name="value">The data toe process</param>
512-
/// <param name="key">The key value to XOR with</param>
513-
/// <returns>Processed data</returns>
514-
public byte[] ProcessXor(byte[] value, int key)
515-
{
516-
return Utilities.ProcessXor(value, key);
517-
}
518-
519-
/// <summary>
520-
/// Performs XOR processing with given data, XORing every byte of the input with a key
521-
/// array, repeating from the beginning of the key array if necessary
522-
/// </summary>
523-
/// <param name="value">The data toe process</param>
524-
/// <param name="key">The key array to XOR with</param>
525-
/// <returns>Processed data</returns>
526-
public byte[] ProcessXor(byte[] value, byte[] key)
527-
{
528-
return Utilities.ProcessXor(value, key);
529-
}
530-
531-
/// <summary>
532-
/// Performs a circular left rotation shift for a given buffer by a given amount of bits.
533-
/// Pass a negative amount to rotate right.
534-
/// </summary>
535-
/// <param name="data">The data to rotate</param>
536-
/// <param name="amount">The number of bytes to rotate by</param>
537-
/// <param name="groupSize"></param>
538-
/// <returns></returns>
539-
public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
540-
{
541-
return Utilities.ProcessRotateLeft(data, amount, groupSize);
542-
}
543-
544-
/// <summary>
545-
/// Inflates a deflated zlib byte stream
546-
/// </summary>
547-
/// <param name="data">The data to deflate</param>
548-
/// <returns>The deflated result</returns>
549-
public byte[] ProcessZlib(byte[] data)
550-
{
551-
return Utilities.ProcessZlib(data);
552-
}
553-
554-
#endregion
555-
556-
#region Misc utility methods
557-
558-
/// <summary>
559-
/// Performs modulo operation between two integers.
560-
/// </summary>
561-
/// <remarks>
562-
/// This method is required because C# lacks a "true" modulo
563-
/// operator, the % operator rather being the "remainder"
564-
/// operator. We want mod operations to always be positive.
565-
/// </remarks>
566-
/// <param name="a">The value to be divided</param>
567-
/// <param name="b">The value to divide by. Must be greater than zero.</param>
568-
/// <returns>The result of the modulo opertion. Will always be positive.</returns>
569-
public static int Mod(int a, int b)
570-
{
571-
return Utilities.Mod(a, b);
572-
}
573-
574-
/// <summary>
575-
/// Performs modulo operation between two integers.
576-
/// </summary>
577-
/// <remarks>
578-
/// This method is required because C# lacks a "true" modulo
579-
/// operator, the % operator rather being the "remainder"
580-
/// operator. We want mod operations to always be positive.
581-
/// </remarks>
582-
/// <param name="a">The value to be divided</param>
583-
/// <param name="b">The value to divide by. Must be greater than zero.</param>
584-
/// <returns>The result of the modulo opertion. Will always be positive.</returns>
585-
public static long Mod(long a, long b)
586-
{
587-
return Utilities.Mod(a, b);
588-
}
589-
590-
/// <summary>
591-
/// Compares two byte arrays in lexicographical order.
592-
/// </summary>
593-
/// <returns>negative number if a is less than b, <c>0</c> if a is equal to b, positive number if a is greater than b.</returns>
594-
/// <param name="a">First byte array to compare</param>
595-
/// <param name="b">Second byte array to compare.</param>
596-
public static int ByteArrayCompare(byte[] a, byte[] b)
597-
{
598-
return Utilities.ByteArrayCompare(a, b);
599-
}
600-
601489
#endregion
602490
}
603491
}

0 commit comments

Comments
 (0)