Skip to content

Commit ab97020

Browse files
committed
Nullable enabled for DiscUtils.Core
1 parent 7c966f8 commit ab97020

File tree

86 files changed

+377
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+377
-295
lines changed

Library/DiscUtils.Core/ApplePartitionMap/PartitionMapEntry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ namespace DiscUtils.ApplePartitionMap;
3030
internal sealed class PartitionMapEntry : PartitionInfo, IByteArraySerializable
3131
{
3232
private readonly Stream _diskStream;
33-
private readonly PartitionMap _map;
33+
private readonly PartitionMap? _map;
3434

3535
public uint BootBlock;
3636
public uint BootBytes;
3737
public uint Flags;
3838
public uint LogicalBlocks;
3939
public uint LogicalBlockStart;
4040
public uint MapEntries;
41-
public string Name;
41+
public string Name = null!;
4242
public uint PhysicalBlocks;
4343
public uint PhysicalBlockStart;
4444
public ushort Signature;
45-
public string Type;
45+
public string Type = null!;
4646

47-
public PartitionMapEntry(Stream diskStream, PartitionMap map)
47+
public PartitionMapEntry(Stream diskStream, PartitionMap? map)
4848
{
4949
_diskStream = diskStream;
5050
_map = map;
5151
}
5252

53-
public override PartitionTable Table => _map;
53+
public override PartitionTable? Table => _map;
5454

5555
public override byte BiosType => 0xAF;
5656

Library/DiscUtils.Core/ApplePartitionMap/PartitionMapFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override bool DetectIsPartitioned(Stream s)
5555
return initialPart.Signature == 0x504d;
5656
}
5757

58-
public override PartitionTable DetectPartitionTable(VirtualDisk disk)
58+
public override PartitionTable? DetectPartitionTable(VirtualDisk disk)
5959
{
6060
if (!DetectIsPartitioned(disk.Content))
6161
{

Library/DiscUtils.Core/Archives/TarFile.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System;
2626
using System.Buffers;
2727
using System.Collections.Generic;
28+
using System.Diagnostics.CodeAnalysis;
2829
using System.IO;
2930
using System.Runtime.CompilerServices;
3031
using System.Threading;
@@ -53,7 +54,7 @@ public TarFile(Stream fileStream)
5354

5455
Span<byte> hdrBuf = stackalloc byte[512];
5556

56-
string long_path = null;
57+
string? long_path = null;
5758

5859
for (;;)
5960
{
@@ -109,7 +110,7 @@ public TarFile(Stream fileStream)
109110
/// <param name="path">The path to the file within the archive.</param>
110111
/// <param name="stream">A stream containing the file contents, or null.</param>
111112
/// <returns><c>true</c> if the file could be opened, else <c>false</c>.</returns>
112-
public bool TryOpenFile(string path, out Stream stream)
113+
public bool TryOpenFile(string path, [NotNullWhen(true)] out Stream? stream)
113114
{
114115
if (_files.TryGetValue(path, out var file))
115116
{
@@ -198,8 +199,8 @@ public static IEnumerable<TarFileData> EnumerateFiles(Stream archive)
198199
{
199200
var hdrBuf = StreamUtilities.GetUninitializedArray<byte>(512);
200201

201-
string long_path = null;
202-
string long_link_path = null;
202+
string? long_path = null;
203+
string? long_link_path = null;
203204

204205
for (;;)
205206
{
@@ -332,8 +333,8 @@ public static async IAsyncEnumerable<TarFileData> EnumerateFilesAsync(Stream arc
332333
{
333334
var hdrBuf = StreamUtilities.GetUninitializedArray<byte>(512);
334335

335-
string long_path = null;
336-
string long_link_path = null;
336+
string? long_path = null;
337+
string? long_link_path = null;
337338

338339
for (; ; )
339340
{

Library/DiscUtils.Core/Archives/TarFileData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ public sealed class TarFileData
66
{
77
public TarHeader Header { get; }
88

9-
private Stream _source;
9+
private Stream? _source;
1010

1111
public string Name => Header.FileName;
1212

1313
public long Length => Header.FileLength;
1414

15-
public TarFileData(TarHeader header, Stream source)
15+
public TarFileData(TarHeader header, Stream? source)
1616
{
1717
Header = header;
1818
_source = source;
1919
}
2020

21-
public Stream GetStream() => _source;
21+
public Stream? GetStream() => _source;
2222

2323
public override string ToString() => Name;
2424
}

Library/DiscUtils.Core/Archives/TarHeader.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,16 @@ public sealed class TarHeader
3737
public DateTimeOffset ModificationTime { get; }
3838
public int CheckSum { get; }
3939
public TarFileType FileType { get; }
40-
public string LinkName { get; internal set; }
41-
public string Magic { get; }
40+
public string? LinkName { get; internal set; }
41+
public string? Magic { get; }
4242
public int Version { get; }
43-
public string OwnerName { get; }
44-
public string GroupName { get; }
43+
public string? OwnerName { get; }
44+
public string? GroupName { get; }
4545
public int DevMajor { get; }
4646
public int DevMinor { get; }
4747
public DateTimeOffset LastAccessTime { get; }
4848
public DateTimeOffset CreationTime { get; }
4949

50-
public TarHeader()
51-
{
52-
}
53-
5450
public TarHeader(string fileName, long fileLength, UnixFilePermissions fileMode, int ownerId, int groupId, DateTime modificationTime)
5551
{
5652
FileName = fileName;

Library/DiscUtils.Core/ChsAddress.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ChsAddress(int cylinder, int head, int sector)
7272
public bool Equals(ChsAddress other)
7373
=> Cylinder == other.Cylinder && Head == other.Head && Sector == other.Sector;
7474

75-
public override bool Equals(object obj)
75+
public override bool Equals(object? obj)
7676
=> obj is ChsAddress other && Equals(other);
7777

7878
public static bool operator ==(ChsAddress a, ChsAddress b) => a.Equals(b);

Library/DiscUtils.Core/Compression/BZip2CombinedHuffmanTrees.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//
2626

2727
using DiscUtils.Streams;
28+
using System.Diagnostics.CodeAnalysis;
2829
using System.IO;
2930

3031
namespace DiscUtils.Compression;
@@ -35,7 +36,7 @@ namespace DiscUtils.Compression;
3536
/// </summary>
3637
internal class BZip2CombinedHuffmanTrees
3738
{
38-
private HuffmanTree _activeTree;
39+
private HuffmanTree? _activeTree;
3940
private readonly BitStream _bitstream;
4041
private int _nextSelector;
4142
private byte[] _selectors;
@@ -51,7 +52,7 @@ public BZip2CombinedHuffmanTrees(BitStream bitstream, int maxSymbols)
5152

5253
public uint NextSymbol()
5354
{
54-
if (_symbolsToNextSelector == 0)
55+
if (_symbolsToNextSelector == 0 || _activeTree is null)
5556
{
5657
_symbolsToNextSelector = 50;
5758
_activeTree = _trees[_selectors[_nextSelector]];
@@ -63,6 +64,7 @@ public uint NextSymbol()
6364
return _activeTree.NextSymbol(_bitstream);
6465
}
6566

67+
[MemberNotNull(nameof(_selectors), nameof(_trees))]
6668
private void Initialize(int maxSymbols)
6769
{
6870
var numTrees = (int)_bitstream.Read(3);

Library/DiscUtils.Core/Compression/BZip2DecoderStream.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//
2626

2727
using System;
28+
using System.Diagnostics.CodeAnalysis;
2829
using System.IO;
2930
using System.Threading;
3031
using System.Threading.Tasks;
@@ -44,7 +45,7 @@ public sealed class BZip2DecoderStream : ReadOnlyCompatibilityStream
4445
private readonly byte[] _blockBuffer;
4546
private uint _blockCrc;
4647
private readonly BZip2BlockDecoder _blockDecoder;
47-
private Crc32 _calcBlockCrc;
48+
private Crc32? _calcBlockCrc;
4849
private uint _calcCompoundCrc;
4950
private uint _compoundCrc;
5051
private Stream _compressedStream;
@@ -173,7 +174,7 @@ public override int Read(Span<byte> buffer)
173174
numRead = _rleStream.Read(buffer);
174175
}
175176

176-
_calcBlockCrc.Process(buffer.Slice(0, numRead));
177+
_calcBlockCrc?.Process(buffer.Slice(0, numRead));
177178

178179
// Pre-read next block, so a client that knows the decompressed length will still
179180
// have the overall CRC calculated.
@@ -241,7 +242,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
241242
numRead = await _rleStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
242243
}
243244

244-
_calcBlockCrc.Process(buffer.Span.Slice(0, numRead));
245+
_calcBlockCrc?.Process(buffer.Span.Slice(0, numRead));
245246

246247
// Pre-read next block, so a client that knows the decompressed length will still
247248
// have the overall CRC calculated.
@@ -299,12 +300,12 @@ protected override void Dispose(bool disposing)
299300
_compressedStream.Dispose();
300301
}
301302

302-
_compressedStream = null;
303+
_compressedStream = null!;
303304

304305
if (_rleStream != null)
305306
{
306307
_rleStream.Dispose();
307-
_rleStream = null;
308+
_rleStream = null!;
308309
}
309310
}
310311
}

Library/DiscUtils.Core/Compression/BZip2RleStream.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
using DiscUtils.Streams.Compatibility;
2828
using System;
29+
using System.Diagnostics.CodeAnalysis;
2930
using System.IO;
3031
using System.Threading;
3132
using System.Threading.Tasks;
@@ -34,7 +35,7 @@ namespace DiscUtils.Compression;
3435

3536
internal class BZip2RleStream : ReadOnlyCompatibilityStream
3637
{
37-
private byte[] _blockBuffer;
38+
private byte[]? _blockBuffer;
3839
private int _blockOffset;
3940
private int _blockRemaining;
4041
private byte _lastByte;
@@ -57,6 +58,7 @@ public override long Position
5758
set => throw new NotSupportedException();
5859
}
5960

61+
[MemberNotNull(nameof(_blockBuffer))]
6062
public void Reset(byte[] buffer, int offset, int count)
6163
{
6264
_position = 0;
@@ -79,6 +81,11 @@ public override int Read(byte[] buffer, int offset, int count)
7981

8082
public override int Read(Span<byte> buffer)
8183
{
84+
if (_blockBuffer == null)
85+
{
86+
throw new InvalidOperationException("Stream not initialized");
87+
}
88+
8289
var numRead = 0;
8390

8491
while (numRead < buffer.Length && _runBytesOutstanding > 0)

Library/DiscUtils.Core/Compression/LzxStream.cs

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

2323
using System;
24+
using System.Diagnostics.CodeAnalysis;
2425
using System.IO;
2526
using System.Threading;
2627
using System.Threading.Tasks;
@@ -39,17 +40,17 @@ internal class LzxStream : ReadOnlyCompatibilityStream
3940
{
4041
private static readonly uint[] _positionSlots;
4142
private static readonly uint[] _extraBits;
42-
private HuffmanTree _alignedOffsetTree;
43+
private HuffmanTree? _alignedOffsetTree;
4344

4445
private readonly LzxBitStream _bitStream;
4546

4647
private byte[] _buffer;
4748
private int _bufferCount;
4849
private readonly int _fileSize;
49-
private HuffmanTree _lengthTree;
50+
private HuffmanTree? _lengthTree;
5051

5152
// Block state
52-
private HuffmanTree _mainTree;
53+
private HuffmanTree? _mainTree;
5354
private readonly int _numPositionSlots;
5455

5556
private long _position;
@@ -162,7 +163,6 @@ private void ReadBlocks()
162163
{
163164
var blockType = (BlockType)_bitStream.Read(3);
164165

165-
_buffer = new byte[32768];
166166
_bufferCount = 0;
167167

168168
while (blockType != BlockType.None)
@@ -240,6 +240,7 @@ private void DecodeUncompressedBlock(int blockSize)
240240
}
241241
}
242242

243+
[MemberNotNull(nameof(_mainTree), nameof(_lengthTree))]
243244
private void DecodeCompressedBlock(BlockType blockType, int blockSize)
244245
{
245246
if (blockType == BlockType.AlignedOffset)
@@ -296,7 +297,7 @@ private void DecodeCompressedBlock(BlockType blockType, int blockSize)
296297
if (extra >= 3)
297298
{
298299
verbatimBits = _bitStream.Read(extra - 3) << 3;
299-
alignedBits = _alignedOffsetTree.NextSymbol(_bitStream);
300+
alignedBits = _alignedOffsetTree!.NextSymbol(_bitStream);
300301
}
301302
else if (extra > 0)
302303
{
@@ -331,6 +332,7 @@ private void DecodeCompressedBlock(BlockType blockType, int blockSize)
331332
}
332333
}
333334

335+
[MemberNotNull(nameof(_mainTree))]
334336
private void ReadMainTree()
335337
{
336338
uint[] lengths;
@@ -352,6 +354,7 @@ private void ReadMainTree()
352354
_mainTree = new HuffmanTree(lengths);
353355
}
354356

357+
[MemberNotNull(nameof(_lengthTree))]
355358
private void ReadLengthTree()
356359
{
357360
var preTree = ReadFixedHuffmanTree(20, 4);
@@ -369,7 +372,7 @@ private HuffmanTree ReadFixedHuffmanTree(int count, int bits)
369372
return new HuffmanTree(treeLengths);
370373
}
371374

372-
private HuffmanTree ReadDynamicHuffmanTree(int count, HuffmanTree preTree, HuffmanTree oldTree)
375+
private HuffmanTree ReadDynamicHuffmanTree(int count, HuffmanTree preTree, HuffmanTree? oldTree)
373376
{
374377
uint[] lengths;
375378

0 commit comments

Comments
 (0)