Skip to content

Commit 30d3f5a

Browse files
committed
Nullable corrections for DiscUtils.Core
1 parent ab97020 commit 30d3f5a

File tree

17 files changed

+111
-91
lines changed

17 files changed

+111
-91
lines changed

Library/DiscUtils.Core/Compression/BZip2Randomizer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
//
2626

2727
using System;
28+
using System.Collections.Immutable;
2829

2930
namespace DiscUtils.Compression;
3031

3132
internal class BZip2Randomizer : DataBlockTransform
3233
{
33-
private static readonly int[] RandomVals =
34+
private static readonly ImmutableArray<int> RandomVals = ImmutableArray.Create(
3435
[
3536
619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
3637
985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
@@ -84,7 +85,7 @@ internal class BZip2Randomizer : DataBlockTransform
8485
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
8586
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
8687
936, 638
87-
];
88+
]);
8889

8990
protected override bool BuffersMustNotOverlap => false;
9091

Library/DiscUtils.Core/Compression/LZNT1.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
//
3030

3131
using System;
32+
using System.Collections.Immutable;
3233
using DiscUtils.Streams;
3334

3435
namespace DiscUtils.Compression;
@@ -50,7 +51,7 @@ internal sealed class LZNT1 : BlockCompressor
5051
// we assume each block is 4KB on decode also.
5152
private const int FixedBlockSize = 0x1000;
5253

53-
private static readonly byte[] _compressionBits = CalcCompressionBits();
54+
private static readonly ImmutableArray<byte> _compressionBits = CalcCompressionBits();
5455

5556
public LZNT1()
5657
{
@@ -298,13 +299,13 @@ public override int Decompress(ReadOnlySpan<byte> source, Span<byte> decompresse
298299
return destIdx;
299300
}
300301

301-
private static byte[] CalcCompressionBits()
302+
private static ImmutableArray<byte> CalcCompressionBits()
302303
{
303-
var result = StreamUtilities.GetUninitializedArray<byte>(4096);
304+
var result = ImmutableArray.CreateBuilder<byte>(4096);
304305
byte offsetBits = 0;
305306

306307
var y = 0x10;
307-
for (var x = 0; x < result.Length; x++)
308+
for (var x = 0; x < 4096; x++)
308309
{
309310
result[x] = (byte)(4 + offsetBits);
310311
if (x == y)
@@ -314,6 +315,6 @@ private static byte[] CalcCompressionBits()
314315
}
315316
}
316317

317-
return result;
318+
return result.ToImmutable();
318319
}
319320
}

Library/DiscUtils.Core/Compression/LzxStream.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222

2323
using System;
24+
using System.Collections.Immutable;
2425
using System.Diagnostics.CodeAnalysis;
2526
using System.IO;
2627
using 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;

Library/DiscUtils.Core/CoreCompat/EncodingHelper.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23

34
namespace DiscUtils.CoreCompat;
45

@@ -20,11 +21,9 @@ public static void RegisterEncodings()
2021
#endif
2122
}
2223

23-
private static readonly char[] _invalidFileNameChars = ['\\', '/', '*', '?', ':'];
24-
2524
public static string SanitizeFileName(this string fileName)
2625
{
27-
foreach (var c in _invalidFileNameChars)
26+
foreach (var c in stackalloc char[] { '\\', '/', '*', '?', ':' })
2827
{
2928
fileName = fileName.Replace(c, '_');
3029
}

Library/DiscUtils.Core/DiscUtils.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
</ItemGroup>
1313
<ItemGroup Condition="'$(TargetFramework.CompareTo(`net461`))' &lt; 0">
1414
<PackageReference Include="System.ValueTuple" Version="4.5.*" />
15+
<PackageReference Include="System.Collections.Immutable" Version="1.*" />
16+
</ItemGroup>
17+
<ItemGroup Condition="'$(TargetFramework.CompareTo(`net461`))' &gt;= 0">
18+
<PackageReference Include="System.Collections.Immutable" Version="*" />
1519
</ItemGroup>
1620
<ItemGroup Condition="'$(TargetFramework.CompareTo(`net5`))' &gt;= 0">
1721
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.*" />

Library/DiscUtils.Core/DiskImageBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static void InitializeMaps()
112112
var attr = type.GetCustomAttribute<VirtualDiskFactoryAttribute>(false);
113113
if (attr != null)
114114
{
115-
var factory = (VirtualDiskFactory)Activator.CreateInstance(type);
115+
var factory = (VirtualDiskFactory)Activator.CreateInstance(type)!;
116116
typeMap.Add(attr.Type, factory);
117117
}
118118
}

Library/DiscUtils.Core/FileTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal sealed class FileTransport : VirtualDiskTransport
3636
public override bool IsRawDisk => false;
3737

3838
[MemberNotNull(nameof(_path), nameof(_extraInfo))]
39-
public override void Connect(Uri uri, string username, string password)
39+
public override void Connect(Uri uri, string? username, string? password)
4040
{
4141
_path = uri.LocalPath;
4242
_extraInfo = uri.Fragment.TrimStart('#');

Library/DiscUtils.Core/Internal/Crc32.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
//
2222

2323
using System;
24+
using System.Collections.Immutable;
2425

2526
namespace DiscUtils.Internal;
2627

2728
internal abstract class Crc32
2829
{
29-
protected readonly uint[] Table;
30+
protected readonly ImmutableArray<uint> Table;
3031
protected uint _value;
3132

32-
protected Crc32(uint[] table)
33+
protected Crc32(ImmutableArray<uint> table)
3334
{
3435
Table = table;
3536
_value = 0xFFFFFFFF;

Library/DiscUtils.Core/Internal/Crc32BigEndian.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222

2323
using System;
24+
using System.Collections.Immutable;
2425

2526
namespace DiscUtils.Internal;
2627

@@ -29,16 +30,18 @@ namespace DiscUtils.Internal;
2930
/// </summary>
3031
internal sealed class Crc32BigEndian : Crc32
3132
{
32-
private static readonly uint[][] Tables;
33+
private static readonly ImmutableArray<ImmutableArray<uint>> Tables;
3334

3435
static Crc32BigEndian()
3536
{
36-
Tables = new uint[4][];
37+
var tables = ImmutableArray.CreateBuilder<ImmutableArray<uint>>(4);
3738

38-
Tables[(int)Crc32Algorithm.Common] = CalcTable(0x04C11DB7);
39-
Tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x1EDC6F41);
40-
Tables[(int)Crc32Algorithm.Koopman] = CalcTable(0x741B8CD7);
41-
Tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0x814141AB);
39+
tables[(int)Crc32Algorithm.Common] = CalcTable(0x04C11DB7);
40+
tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x1EDC6F41);
41+
tables[(int)Crc32Algorithm.Koopman] = CalcTable(0x741B8CD7);
42+
tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0x814141AB);
43+
44+
Tables = tables.ToImmutable();
4245
}
4346

4447
public Crc32BigEndian(Crc32Algorithm algorithm)
@@ -59,9 +62,9 @@ public override void Process(ReadOnlySpan<byte> buffer)
5962
_value = Process(Table, _value, buffer);
6063
}
6164

62-
private static uint[] CalcTable(uint polynomial)
65+
private static ImmutableArray<uint> CalcTable(uint polynomial)
6366
{
64-
var table = new uint[256];
67+
var table = ImmutableArray.CreateBuilder<uint>(256);
6568

6669
for (uint i = 0; i < 256; ++i)
6770
{
@@ -79,20 +82,20 @@ private static uint[] CalcTable(uint polynomial)
7982
}
8083
}
8184

82-
table[i] = crc;
85+
table[(int)i] = crc;
8386
}
8487

85-
return table;
88+
return table.ToImmutable();
8689
}
8790

88-
private static uint Process(uint[] table, uint accumulator, ReadOnlySpan<byte> buffer)
91+
private static uint Process(ImmutableArray<uint> table, uint accumulator, ReadOnlySpan<byte> buffer)
8992
{
9093
var value = accumulator;
9194

9295
for (var i = 0; i < buffer.Length; ++i)
9396
{
9497
var b = buffer[i];
95-
value = table[(value >> 24) ^ b] ^ (value << 8);
98+
value = table[(int)((value >> 24) ^ b)] ^ (value << 8);
9699
}
97100

98101
return value;

Library/DiscUtils.Core/Internal/Crc32LittleEndian.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222

2323
using System;
24+
using System.Collections.Immutable;
2425

2526
namespace DiscUtils.Internal;
2627

@@ -29,16 +30,18 @@ namespace DiscUtils.Internal;
2930
/// </summary>
3031
internal sealed class Crc32LittleEndian : Crc32
3132
{
32-
private static readonly uint[][] Tables;
33+
private static readonly ImmutableArray<ImmutableArray<uint>> Tables;
3334

3435
static Crc32LittleEndian()
3536
{
36-
Tables = new uint[4][];
37+
var tables = ImmutableArray.CreateBuilder<ImmutableArray<uint>>(4);
3738

38-
Tables[(int)Crc32Algorithm.Common] = CalcTable(0xEDB88320);
39-
Tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x82F63B78);
40-
Tables[(int)Crc32Algorithm.Koopman] = CalcTable(0xEB31D82E);
41-
Tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0xD5828281);
39+
tables[(int)Crc32Algorithm.Common] = CalcTable(0xEDB88320);
40+
tables[(int)Crc32Algorithm.Castagnoli] = CalcTable(0x82F63B78);
41+
tables[(int)Crc32Algorithm.Koopman] = CalcTable(0xEB31D82E);
42+
tables[(int)Crc32Algorithm.Aeronautical] = CalcTable(0xD5828281);
43+
44+
Tables = tables.ToImmutable();
4245
}
4346

4447
public Crc32LittleEndian(Crc32Algorithm algorithm)
@@ -59,9 +62,9 @@ public override void Process(ReadOnlySpan<byte> buffer)
5962
_value = Process(Table, _value, buffer);
6063
}
6164

62-
private static uint[] CalcTable(uint polynomial)
65+
private static ImmutableArray<uint> CalcTable(uint polynomial)
6366
{
64-
var table = new uint[256];
67+
var table = ImmutableArray.CreateBuilder<uint>(256);
6568

6669
table[0] = 0;
6770
for (uint i = 0; i <= 255; ++i)
@@ -80,13 +83,13 @@ private static uint[] CalcTable(uint polynomial)
8083
}
8184
}
8285

83-
table[i] = crc;
86+
table[(int)i] = crc;
8487
}
8588

86-
return table;
89+
return table.ToImmutable();
8790
}
8891

89-
private static uint Process(uint[] table, uint accumulator, ReadOnlySpan<byte> buffer)
92+
private static uint Process(ImmutableArray<uint> table, uint accumulator, ReadOnlySpan<byte> buffer)
9093
{
9194
var value = accumulator;
9295

@@ -95,7 +98,7 @@ private static uint Process(uint[] table, uint accumulator, ReadOnlySpan<byte> b
9598
var b = buffer[i];
9699

97100
var temp1 = (value >> 8) & 0x00FFFFFF;
98-
var temp2 = table[(value ^ b) & 0xFF];
101+
var temp2 = table[(int)((value ^ b) & 0xFF)];
99102
value = temp1 ^ temp2;
100103
}
101104

0 commit comments

Comments
 (0)