2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . IO . Compression ;
5
- using System . Linq ;
6
5
7
6
namespace Kaitai
8
7
{
@@ -16,8 +15,8 @@ public partial class KaitaiStream : IKaitaiStream
16
15
17
16
static readonly bool IsLittleEndian = BitConverter . IsLittleEndian ;
18
17
19
- private ulong Bits = 0 ;
20
- private int BitsLeft = 0 ;
18
+ private ulong Bits ;
19
+ private int BitsLeft ;
21
20
private BinaryReader m_binaryReader ;
22
21
23
22
protected Stream BaseStream ;
@@ -40,7 +39,7 @@ public KaitaiStream(string file) : this(File.Open(file, FileMode.Open, FileAcces
40
39
public KaitaiStream ( byte [ ] bytes ) : this ( new MemoryStream ( bytes ) )
41
40
{
42
41
}
43
-
42
+
44
43
protected BinaryReader BinaryReader
45
44
{
46
45
get => m_binaryReader ?? ( BinaryReader = new BinaryReader ( BaseStream ) ) ;
@@ -304,7 +303,7 @@ public ulong ReadBitsInt(int n)
304
303
// 1 bit => 1 byte
305
304
// 8 bits => 1 byte
306
305
// 9 bits => 2 bytes
307
- int bytesNeeded = ( ( bitsNeeded - 1 ) / 8 ) + 1 ;
306
+ int bytesNeeded = ( bitsNeeded - 1 ) / 8 + 1 ;
308
307
byte [ ] buf = ReadBytes ( bytesNeeded ) ;
309
308
for ( int i = 0 ; i < buf . Length ; i ++ )
310
309
{
@@ -339,11 +338,11 @@ public ulong ReadBitsIntLe(int n)
339
338
// 1 bit => 1 byte
340
339
// 8 bits => 1 byte
341
340
// 9 bits => 2 bytes
342
- int bytesNeeded = ( ( bitsNeeded - 1 ) / 8 ) + 1 ;
341
+ int bytesNeeded = ( bitsNeeded - 1 ) / 8 + 1 ;
343
342
byte [ ] buf = ReadBytes ( bytesNeeded ) ;
344
343
for ( int i = 0 ; i < buf . Length ; i ++ )
345
344
{
346
- ulong v = ( ulong ) ( ( ulong ) buf [ i ] << BitsLeft ) ;
345
+ ulong v = ( ulong ) buf [ i ] << BitsLeft ;
347
346
Bits |= v ;
348
347
BitsLeft += 8 ;
349
348
}
@@ -353,7 +352,7 @@ public ulong ReadBitsIntLe(int n)
353
352
ulong mask = GetMaskOnes ( n ) ;
354
353
355
354
// derive reading result
356
- ulong res = ( Bits & mask ) ;
355
+ ulong res = Bits & mask ;
357
356
358
357
// remove bottom bits that we've just read by shifting
359
358
Bits >>= n ;
@@ -379,10 +378,10 @@ private static ulong GetMaskOnes(int n)
379
378
public byte [ ] ReadBytes ( long count )
380
379
{
381
380
if ( count < 0 || count > Int32 . MaxValue )
382
- throw new ArgumentOutOfRangeException ( "requested " + count + " bytes, while only non-negative int32 amount of bytes possible") ;
383
- byte [ ] bytes = BinaryReader . ReadBytes ( ( int ) count ) ;
381
+ throw new ArgumentOutOfRangeException ( $ "requested { count } bytes, while only non-negative int32 amount of bytes possible") ;
382
+ byte [ ] bytes = BinaryReader . ReadBytes ( ( int ) count ) ;
384
383
if ( bytes . Length < count )
385
- throw new EndOfStreamException ( "requested " + count + " bytes, but got only " + bytes . Length + " bytes") ;
384
+ throw new EndOfStreamException ( $ "requested { count } bytes, but got only { bytes . Length } bytes") ;
386
385
return bytes ;
387
386
}
388
387
@@ -394,10 +393,10 @@ public byte[] ReadBytes(long count)
394
393
public byte [ ] ReadBytes ( ulong count )
395
394
{
396
395
if ( count > Int32 . MaxValue )
397
- throw new ArgumentOutOfRangeException ( "requested " + count + " bytes, while only non-negative int32 amount of bytes possible") ;
396
+ throw new ArgumentOutOfRangeException ( $ "requested { count } bytes, while only non-negative int32 amount of bytes possible") ;
398
397
byte [ ] bytes = BinaryReader . ReadBytes ( ( int ) count ) ;
399
398
if ( bytes . Length < ( int ) count )
400
- throw new EndOfStreamException ( "requested " + count + " bytes, but got only " + bytes . Length + " bytes") ;
399
+ throw new EndOfStreamException ( $ "requested { count } bytes, but got only { bytes . Length } bytes") ;
401
400
return bytes ;
402
401
}
403
402
@@ -449,7 +448,8 @@ public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consum
449
448
{
450
449
if ( IsEof )
451
450
{
452
- if ( eosError ) throw new EndOfStreamException ( string . Format ( "End of stream reached, but no terminator `{0}` found" , terminator ) ) ;
451
+ if ( eosError )
452
+ throw new EndOfStreamException ( $ "End of stream reached, but no terminator `{ terminator } ` found") ;
453
453
break ;
454
454
}
455
455
@@ -460,8 +460,10 @@ public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consum
460
460
if ( ! consumeTerminator ) Seek ( Pos - 1 ) ;
461
461
break ;
462
462
}
463
+
463
464
bytes . Add ( b ) ;
464
465
}
466
+
465
467
return bytes . ToArray ( ) ;
466
468
}
467
469
@@ -476,13 +478,14 @@ public byte[] EnsureFixedContents(byte[] expected)
476
478
477
479
if ( bytes . Length != expected . Length )
478
480
{
479
- throw new Exception ( string . Format ( "Expected bytes: {0 } ({1 } bytes), Instead got: {2} ({3} bytes)" , Convert . ToBase64String ( expected ) , expected . Length , Convert . ToBase64String ( bytes ) , bytes . Length ) ) ;
481
+ throw new Exception ( $ "Expected bytes: { Convert . ToBase64String ( expected ) } ({ expected . Length } bytes), Instead got: { Convert . ToBase64String ( bytes ) } ( { bytes . Length } bytes)" ) ;
480
482
}
483
+
481
484
for ( int i = 0 ; i < bytes . Length ; i ++ )
482
485
{
483
486
if ( bytes [ i ] != expected [ i ] )
484
487
{
485
- throw new Exception ( string . Format ( "Expected bytes: {0 } ({1 } bytes), Instead got: {2} ({3} bytes)" , Convert . ToBase64String ( expected ) , expected . Length , Convert . ToBase64String ( bytes ) , bytes . Length ) ) ;
488
+ throw new Exception ( $ "Expected bytes: { Convert . ToBase64String ( expected ) } ({ expected . Length } bytes), Instead got: { Convert . ToBase64String ( bytes ) } ( { bytes . Length } bytes)" ) ;
486
489
}
487
490
}
488
491
@@ -533,6 +536,7 @@ public byte[] ProcessXor(byte[] value, int key)
533
536
{
534
537
result [ i ] = ( byte ) ( value [ i ] ^ key ) ;
535
538
}
539
+
536
540
return result ;
537
541
}
538
542
@@ -551,6 +555,7 @@ public byte[] ProcessXor(byte[] value, byte[] key)
551
555
{
552
556
result [ i ] = ( byte ) ( value [ i ] ^ key [ j ] ) ;
553
557
}
558
+
554
559
return result ;
555
560
}
556
561
@@ -564,7 +569,8 @@ public byte[] ProcessXor(byte[] value, byte[] key)
564
569
/// <returns></returns>
565
570
public byte [ ] ProcessRotateLeft ( byte [ ] data , int amount , int groupSize )
566
571
{
567
- if ( amount > 7 || amount < - 7 ) throw new ArgumentException ( "Rotation of more than 7 cannot be performed." , "amount" ) ;
572
+ if ( amount > 7 || amount < - 7 )
573
+ throw new ArgumentException ( "Rotation of more than 7 cannot be performed." , "amount" ) ;
568
574
if ( amount < 0 ) amount += 8 ; // Rotation of -2 is the same as rotation of +6
569
575
570
576
byte [ ] r = new byte [ data . Length ] ;
@@ -575,12 +581,14 @@ public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
575
581
{
576
582
byte bits = data [ i ] ;
577
583
// http://stackoverflow.com/a/812039
578
- r [ i ] = ( byte ) ( ( bits << amount ) | ( bits >> ( 8 - amount ) ) ) ;
584
+ r [ i ] = ( byte ) ( ( bits << amount ) | ( bits >> ( 8 - amount ) ) ) ;
579
585
}
586
+
580
587
break ;
581
588
default :
582
- throw new NotImplementedException ( string . Format ( "Unable to rotate a group of {0 } bytes yet" , groupSize ) ) ;
589
+ throw new NotImplementedException ( $ "Unable to rotate a group of { groupSize } bytes yet") ;
583
590
}
591
+
584
592
return r ;
585
593
}
586
594
@@ -597,7 +605,8 @@ public byte[] ProcessZlib(byte[] data)
597
605
// There's also 4 checksum bytes at the end of the stream.
598
606
599
607
byte zlibCmf = data [ 0 ] ;
600
- if ( ( zlibCmf & 0x0F ) != 0x08 ) throw new NotSupportedException ( "Only the DEFLATE algorithm is supported for zlib data." ) ;
608
+ if ( ( zlibCmf & 0x0F ) != 0x08 )
609
+ throw new NotSupportedException ( "Only the DEFLATE algorithm is supported for zlib data." ) ;
601
610
602
611
const int zlibFooter = 4 ;
603
612
int zlibHeader = 2 ;
@@ -674,18 +683,20 @@ public static int ByteArrayCompare(byte[] a, byte[] b)
674
683
int al = a . Length ;
675
684
int bl = b . Length ;
676
685
int minLen = al < bl ? al : bl ;
677
- for ( int i = 0 ; i < minLen ; i ++ ) {
686
+ for ( int i = 0 ; i < minLen ; i ++ )
687
+ {
678
688
int cmp = a [ i ] - b [ i ] ;
679
689
if ( cmp != 0 )
680
690
return cmp ;
681
691
}
682
692
683
693
// Reached the end of at least one of the arrays
684
- if ( al == bl ) {
694
+ if ( al == bl )
695
+ {
685
696
return 0 ;
686
- } else {
687
- return al - bl ;
688
697
}
698
+
699
+ return al - bl ;
689
700
}
690
701
691
702
#endregion
0 commit comments