Skip to content

Commit 7c966f8

Browse files
committed
Nullable enabled for DiscUtils.Streams
1 parent cc2516a commit 7c966f8

Some content is hidden

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

44 files changed

+256
-366
lines changed

Library/DiscUtils.Core/Internal/Utilities.cs

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -36,143 +36,6 @@ namespace DiscUtils.Internal;
3636

3737
public static class Utilities
3838
{
39-
/// <summary>
40-
/// Indicates if two ranges overlap.
41-
/// </summary>
42-
/// <typeparam name="T">The type of the ordinals.</typeparam>
43-
/// <param name="xFirst">The lowest ordinal of the first range (inclusive).</param>
44-
/// <param name="xLast">The highest ordinal of the first range (exclusive).</param>
45-
/// <param name="yFirst">The lowest ordinal of the second range (inclusive).</param>
46-
/// <param name="yLast">The highest ordinal of the second range (exclusive).</param>
47-
/// <returns><c>true</c> if the ranges overlap, else <c>false</c>.</returns>
48-
public static bool RangesOverlap<T>(T xFirst, T xLast, T yFirst, T yLast) where T : IComparable<T>
49-
{
50-
return !((xLast.CompareTo(yFirst) <= 0) || (xFirst.CompareTo(yLast) >= 0));
51-
}
52-
53-
#region Bit Twiddling
54-
55-
public static bool IsAllZeros(byte[] buffer, int offset, int count)
56-
{
57-
#if NET8_0_OR_GREATER
58-
return !buffer.AsSpan(offset, count).ContainsAnyExcept((byte)0);
59-
#else
60-
var end = offset + count;
61-
for (var i = offset; i < end; ++i)
62-
{
63-
if (buffer[i] != 0)
64-
{
65-
return false;
66-
}
67-
}
68-
69-
return true;
70-
#endif
71-
}
72-
73-
public static bool IsAllZeros(ReadOnlySpan<byte> buffer)
74-
{
75-
#if NET8_0_OR_GREATER
76-
return !buffer.ContainsAnyExcept((byte)0);
77-
#else
78-
for (var i = 0; i < buffer.Length; ++i)
79-
{
80-
if (buffer[i] != 0)
81-
{
82-
return false;
83-
}
84-
}
85-
86-
return true;
87-
#endif
88-
}
89-
90-
public static bool IsPowerOfTwo(uint val)
91-
{
92-
if (val == 0)
93-
{
94-
return false;
95-
}
96-
97-
while ((val & 1) != 1)
98-
{
99-
val >>= 1;
100-
}
101-
102-
return val == 1;
103-
}
104-
105-
public static bool IsPowerOfTwo(long val)
106-
{
107-
if (val == 0)
108-
{
109-
return false;
110-
}
111-
112-
while ((val & 1) != 1)
113-
{
114-
val >>= 1;
115-
}
116-
117-
return val == 1;
118-
}
119-
120-
public static bool AreEqual(byte[] a, byte[] b)
121-
{
122-
if (a.Length != b.Length)
123-
{
124-
return false;
125-
}
126-
127-
if (ReferenceEquals(a, b))
128-
{
129-
return true;
130-
}
131-
132-
for (var i = 0; i < a.Length; ++i)
133-
{
134-
if (a[i] != b[i])
135-
{
136-
return false;
137-
}
138-
}
139-
140-
return true;
141-
}
142-
143-
public static ushort BitSwap(ushort value)
144-
{
145-
return (ushort)(((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8));
146-
}
147-
148-
public static uint BitSwap(uint value)
149-
{
150-
return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value & 0x00FF0000) >> 8) |
151-
((value & 0xFF000000) >> 24);
152-
}
153-
154-
public static ulong BitSwap(ulong value)
155-
{
156-
return ((ulong)BitSwap((uint)(value & 0xFFFFFFFF)) << 32) | BitSwap((uint)(value >> 32));
157-
}
158-
159-
public static short BitSwap(short value)
160-
{
161-
return (short)BitSwap((ushort)value);
162-
}
163-
164-
public static int BitSwap(int value)
165-
{
166-
return (int)BitSwap((uint)value);
167-
}
168-
169-
public static long BitSwap(long value)
170-
{
171-
return (long)BitSwap((ulong)value);
172-
}
173-
174-
#endregion
175-
17639
#region Path Manipulation
17740

17841
/// <summary>

Library/DiscUtils.Core/Partitions/BiosPartitionTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public int CreatePrimaryBySector(long first, long last, byte type, bool markActi
539539
// First check for overlap with existing partition...
540540
foreach (var r in existing)
541541
{
542-
if (Utilities.RangesOverlap((uint)first, (uint)last + 1, r.LBAStartAbsolute,
542+
if (BufferUtilities.RangesOverlap((uint)first, (uint)last + 1, r.LBAStartAbsolute,
543543
r.LBAStartAbsolute + r.LBALength))
544544
{
545545
throw new IOException("New partition overlaps with existing partition");
@@ -824,7 +824,7 @@ private int FindCylinderGap(int numCylinders)
824824
}
825825

826826
if (
827-
!Utilities.RangesOverlap(startCylinder, startCylinder + numCylinders - 1, existingStart, existingEnd))
827+
!BufferUtilities.RangesOverlap(startCylinder, startCylinder + numCylinders - 1, existingStart, existingEnd))
828828
{
829829
break;
830830
}
@@ -852,7 +852,7 @@ private long FindGap(long numSectors, long alignmentSectors)
852852
entry = list[idx];
853853
}
854854

855-
if (Utilities.RangesOverlap(startSector, startSector + numSectors, entry.LBAStartAbsolute,
855+
if (BufferUtilities.RangesOverlap(startSector, startSector + numSectors, entry.LBAStartAbsolute,
856856
entry.LBAStartAbsolute + entry.LBALength))
857857
{
858858
startSector = MathUtilities.RoundUp(entry.LBAStartAbsolute + entry.LBALength, alignmentSectors);

Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ private long FindGap(long numSectors, long alignmentSectors)
482482
foreach (var entry in list)
483483
{
484484
if (
485-
!Utilities.RangesOverlap(startSector, startSector + numSectors - 1, entry.FirstUsedLogicalBlock,
485+
!BufferUtilities.RangesOverlap(startSector, startSector + numSectors - 1, entry.FirstUsedLogicalBlock,
486486
entry.LastUsedLogicalBlock))
487487
{
488488
break;

Library/DiscUtils.Iso9660/CommonVolumeDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public CommonVolumeDescriptor(ReadOnlySpan<byte> src, Encoding enc)
7070
PathTableSize = IsoUtilities.ToUInt32FromBoth(src.Slice(132));
7171
TypeLPathTableLocation = EndianUtilities.ToUInt32LittleEndian(src.Slice(140));
7272
OptionalTypeLPathTableLocation = EndianUtilities.ToUInt32LittleEndian(src.Slice(144));
73-
TypeMPathTableLocation = Utilities.BitSwap(EndianUtilities.ToUInt32LittleEndian(src.Slice(148)));
74-
OptionalTypeMPathTableLocation = Utilities.BitSwap(EndianUtilities.ToUInt32LittleEndian(src.Slice(152)));
73+
TypeMPathTableLocation = EndianUtilities.ToUInt32BigEndian(src.Slice(148));
74+
OptionalTypeMPathTableLocation = EndianUtilities.ToUInt32BigEndian(src.Slice(152));
7575
DirectoryRecord.ReadFrom(src.Slice(156), CharacterEncoding, out RootDirectory);
7676
VolumeSetIdentifier = IsoUtilities.ReadChars(src.Slice(190, 318 - 190), CharacterEncoding);
7777
PublisherIdentifier = IsoUtilities.ReadChars(src.Slice(318, 446 - 318), CharacterEncoding);

Library/DiscUtils.Iso9660/PathTableRecord.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
using System;
2424
using System.Text;
25-
using DiscUtils.Internal;
25+
using DiscUtils.Streams;
2626

2727
namespace DiscUtils.Iso9660;
2828

@@ -64,9 +64,9 @@ internal int Write(bool byteSwap, Encoding enc, Span<byte> buffer)
6464
buffer[0] = (byte)nameBytes;
6565
buffer[1] = 0; // ExtendedAttributeRecordLength;
6666
IsoUtilities.ToBytesFromUInt32(buffer.Slice(2),
67-
byteSwap ? Utilities.BitSwap(LocationOfExtent) : LocationOfExtent);
67+
byteSwap ? BufferUtilities.BitSwap(LocationOfExtent) : LocationOfExtent);
6868
IsoUtilities.ToBytesFromUInt16(buffer.Slice(6),
69-
byteSwap ? Utilities.BitSwap(ParentDirectoryNumber) : ParentDirectoryNumber);
69+
byteSwap ? BufferUtilities.BitSwap(ParentDirectoryNumber) : ParentDirectoryNumber);
7070
IsoUtilities.WriteString(buffer.Slice(8, nameBytes), pad: false, DirectoryIdentifier.AsSpan(), enc);
7171
if ((nameBytes & 1) == 1)
7272
{

Library/DiscUtils.Iso9660/PrimaryVolumeDescriptor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System;
2424
using System.Text;
2525
using DiscUtils.Internal;
26+
using DiscUtils.Streams;
2627

2728
namespace DiscUtils.Iso9660;
2829

@@ -55,8 +56,8 @@ internal override void WriteTo(Span<byte> buffer)
5556
IsoUtilities.ToBothFromUInt32(buffer.Slice(132), PathTableSize);
5657
IsoUtilities.ToBytesFromUInt32(buffer.Slice(140), TypeLPathTableLocation);
5758
IsoUtilities.ToBytesFromUInt32(buffer.Slice(144), OptionalTypeLPathTableLocation);
58-
IsoUtilities.ToBytesFromUInt32(buffer.Slice(148), Utilities.BitSwap(TypeMPathTableLocation));
59-
IsoUtilities.ToBytesFromUInt32(buffer.Slice(152), Utilities.BitSwap(OptionalTypeMPathTableLocation));
59+
EndianUtilities.WriteBytesBigEndian(TypeMPathTableLocation, buffer.Slice(148));
60+
EndianUtilities.WriteBytesBigEndian(OptionalTypeMPathTableLocation, buffer.Slice(152));
6061
RootDirectory.WriteTo(buffer.Slice(156), Encoding.ASCII);
6162
IsoUtilities.WriteDChars(buffer.Slice(190, 129), VolumeSetIdentifier.AsSpan());
6263
IsoUtilities.WriteAChars(buffer.Slice(318, 129), PublisherIdentifier.AsSpan());

Library/DiscUtils.Iso9660/SupplementaryVolumeDescriptor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System;
2424
using System.Text;
2525
using DiscUtils.Internal;
26+
using DiscUtils.Streams;
2627

2728
namespace DiscUtils.Iso9660;
2829

@@ -57,8 +58,8 @@ internal override void WriteTo(Span<byte> buffer)
5758
IsoUtilities.ToBothFromUInt32(buffer.Slice(132), PathTableSize);
5859
IsoUtilities.ToBytesFromUInt32(buffer.Slice(140), TypeLPathTableLocation);
5960
IsoUtilities.ToBytesFromUInt32(buffer.Slice(144), OptionalTypeLPathTableLocation);
60-
IsoUtilities.ToBytesFromUInt32(buffer.Slice(148), Utilities.BitSwap(TypeMPathTableLocation));
61-
IsoUtilities.ToBytesFromUInt32(buffer.Slice(152), Utilities.BitSwap(OptionalTypeMPathTableLocation));
61+
EndianUtilities.WriteBytesBigEndian(TypeMPathTableLocation, buffer.Slice(148));
62+
EndianUtilities.WriteBytesBigEndian(OptionalTypeMPathTableLocation, buffer.Slice(152));
6263
RootDirectory.WriteTo(buffer.Slice(156), CharacterEncoding);
6364
IsoUtilities.WriteD1Chars(buffer.Slice(190, 129), VolumeSetIdentifier.AsSpan(), CharacterEncoding);
6465
IsoUtilities.WriteA1Chars(buffer.Slice(318, 129), PublisherIdentifier.AsSpan(), CharacterEncoding);

Library/DiscUtils.Streams/AligningStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class AligningStream : WrappingMappedStream<SparseStream>
3939
private long _position;
4040

4141
public AligningStream(SparseStream toWrap, Ownership ownership, int blockSize)
42-
: base(toWrap, ownership, null)
42+
: base(toWrap, ownership, extents: null)
4343
{
4444
_blockSize = blockSize;
4545
_alignmentBuffer = StreamUtilities.GetUninitializedArray<byte>(blockSize);

Library/DiscUtils.Streams/Block/Block.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
namespace DiscUtils.Streams;
2424

25-
public class Block
25+
public class Block()
2626
{
2727
public int Available { get; set; }
2828

29-
public byte[] Data { get; set; }
29+
public byte[] Data { get; set; } = null!;
3030

3131
public long Position { get; set; }
3232

Library/DiscUtils.Streams/Block/BlockCache.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
// DEALINGS IN THE SOFTWARE.
2121
//
2222

23+
using System;
2324
using System.Collections.Generic;
25+
using System.Diagnostics.CodeAnalysis;
2426

2527
namespace DiscUtils.Streams;
2628

@@ -54,7 +56,7 @@ public bool ContainsBlock(long position)
5456
return _blocks.ContainsKey(position);
5557
}
5658

57-
public bool TryGetBlock(long position, out T block)
59+
public bool TryGetBlock(long position, [NotNullWhen(true)] out T? block)
5860
{
5961
if (_blocks.TryGetValue(position, out block))
6062
{
@@ -119,12 +121,16 @@ private T GetFreeBlock()
119121
_blocksCreated++;
120122
FreeBlockCount--;
121123
}
122-
else
124+
else if (_lru.Last is { } last)
123125
{
124-
block = _lru.Last.Value;
126+
block = last.Value;
125127
_lru.RemoveLast();
126128
_blocks.Remove(block.Position);
127129
}
130+
else
131+
{
132+
throw new InvalidOperationException("No blocks available in cache");
133+
}
128134

129135
return block;
130136
}

0 commit comments

Comments
 (0)