2121//
2222
2323using System ;
24+ using System . Collections . Immutable ;
2425using System . Diagnostics . CodeAnalysis ;
2526using System . IO ;
2627using System . Threading ;
@@ -36,10 +37,10 @@ namespace DiscUtils.Compression;
3637/// <remarks>This is not a general purpose LZX decompressor - it makes
3738/// simplifying assumptions, such as being able to load the entire stream
3839/// contents into memory..</remarks>
39- internal class LzxStream : ReadOnlyCompatibilityStream
40+ public class LzxStream : ReadOnlyCompatibilityStream
4041{
41- private static readonly uint [ ] _positionSlots ;
42- private static readonly uint [ ] _extraBits ;
42+ private static readonly ImmutableArray < uint > _positionSlots ;
43+ private static readonly ImmutableArray < uint > _extraBits ;
4344 private HuffmanTree ? _alignedOffsetTree ;
4445
4546 private readonly LzxBitStream _bitStream ;
@@ -59,23 +60,27 @@ internal class LzxStream : ReadOnlyCompatibilityStream
5960
6061 static LzxStream ( )
6162 {
62- _positionSlots = new uint [ 50 ] ;
63- _extraBits = new uint [ 50 ] ;
63+ var positionSlots = ImmutableArray . CreateBuilder < uint > ( 50 ) ;
64+ var extraBits = ImmutableArray . CreateBuilder < uint > ( 50 ) ;
6465
6566 uint numBits = 0 ;
66- _positionSlots [ 1 ] = 1 ;
67+ positionSlots [ 1 ] = 1 ;
68+
6769 for ( var i = 2 ; i < 50 ; i += 2 )
6870 {
69- _extraBits [ i ] = numBits ;
70- _extraBits [ i + 1 ] = numBits ;
71- _positionSlots [ i ] = _positionSlots [ i - 1 ] + ( uint ) ( 1 << ( int ) _extraBits [ i - 1 ] ) ;
72- _positionSlots [ i + 1 ] = _positionSlots [ i ] + ( uint ) ( 1 << ( int ) numBits ) ;
71+ extraBits [ i ] = numBits ;
72+ extraBits [ i + 1 ] = numBits ;
73+ positionSlots [ i ] = positionSlots [ i - 1 ] + ( uint ) ( 1 << ( int ) extraBits [ i - 1 ] ) ;
74+ positionSlots [ i + 1 ] = positionSlots [ i ] + ( uint ) ( 1 << ( int ) numBits ) ;
7375
7476 if ( numBits < 17 )
7577 {
7678 numBits ++ ;
7779 }
7880 }
81+
82+ _positionSlots = positionSlots . ToImmutable ( ) ;
83+ _extraBits = extraBits . ToImmutable ( ) ;
7984 }
8085
8186 public LzxStream ( Stream stream , int windowBits , int fileSize )
@@ -285,7 +290,7 @@ private void DecodeCompressedBlock(BlockType blockType, int blockSize)
285290 }
286291 else
287292 {
288- var extra = ( int ) _extraBits [ positionSlot ] ;
293+ var extra = ( int ) _extraBits [ ( int ) positionSlot ] ;
289294
290295 uint formattedOffset ;
291296
@@ -304,13 +309,13 @@ private void DecodeCompressedBlock(BlockType blockType, int blockSize)
304309 verbatimBits = _bitStream . Read ( extra ) ;
305310 }
306311
307- formattedOffset = _positionSlots [ positionSlot ] + verbatimBits + alignedBits ;
312+ formattedOffset = _positionSlots [ ( int ) positionSlot ] + verbatimBits + alignedBits ;
308313 }
309314 else
310315 {
311316 var verbatimBits = extra > 0 ? _bitStream . Read ( extra ) : 0 ;
312317
313- formattedOffset = _positionSlots [ positionSlot ] + verbatimBits ;
318+ formattedOffset = _positionSlots [ ( int ) positionSlot ] + verbatimBits ;
314319 }
315320
316321 matchOffset = formattedOffset - 2 ;
0 commit comments