Prefer ReadUInt8 over ReadByte.

The former will throw when the end of the stream is reached, rather than requiring the caller to check for -1.
This commit is contained in:
RoosterDragon
2020-10-18 11:35:21 +01:00
committed by abcdefg30
parent f5f2f58664
commit aac1bae899
10 changed files with 39 additions and 39 deletions

View File

@@ -53,33 +53,33 @@ namespace OpenRA
using (var s = new MemoryStream(data))
{
// SEQUENCE
s.ReadByte();
s.ReadUInt8();
ReadTLVLength(s);
// SEQUENCE -> fixed header junk
s.ReadByte();
s.ReadUInt8();
var headerLength = ReadTLVLength(s);
s.Position += headerLength;
// SEQUENCE -> BIT_STRING
s.ReadByte();
s.ReadUInt8();
ReadTLVLength(s);
s.ReadByte();
s.ReadUInt8();
// SEQUENCE -> BIT_STRING -> SEQUENCE
s.ReadByte();
s.ReadUInt8();
ReadTLVLength(s);
// SEQUENCE -> BIT_STRING -> SEQUENCE -> INTEGER (modulus)
s.ReadByte();
s.ReadUInt8();
var modulusLength = ReadTLVLength(s);
s.ReadByte();
s.ReadUInt8();
var modulus = s.ReadBytes(modulusLength - 1);
// SEQUENCE -> BIT_STRING -> SEQUENCE -> INTEGER (exponent)
s.ReadByte();
s.ReadUInt8();
var exponentLength = ReadTLVLength(s);
s.ReadByte();
s.ReadUInt8();
var exponent = s.ReadBytes(exponentLength - 1);
return new RSAParameters
@@ -158,7 +158,7 @@ namespace OpenRA
static int ReadTLVLength(Stream s)
{
var length = s.ReadByte();
var length = s.ReadUInt8();
if (length < 0x80)
return length;

View File

@@ -150,7 +150,7 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
var block = default(VocBlock);
try
{
block.Code = stream.ReadByte();
block.Code = stream.ReadUInt8();
block.Length = 0;
}
catch (EndOfStreamException)
@@ -162,9 +162,9 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
if (block.Code == 0 || block.Code > 9)
break;
block.Length = stream.ReadByte();
block.Length |= stream.ReadByte() << 8;
block.Length |= stream.ReadByte() << 16;
block.Length = stream.ReadUInt8();
block.Length |= stream.ReadUInt8() << 8;
block.Length |= stream.ReadUInt8() << 16;
var skip = 0;
switch (block.Code)
@@ -174,9 +174,9 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
{
if (block.Length < 2)
throw new InvalidDataException("Invalid sound data block length in voc file");
var freqDiv = stream.ReadByte();
var freqDiv = stream.ReadUInt8();
block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
var codec = stream.ReadByte();
var codec = stream.ReadUInt8();
if (codec != 0)
throw new InvalidDataException("Unhandled codec used in voc file");
skip = block.Length - 2;
@@ -205,7 +205,7 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
throw new InvalidDataException("Invalid silence block length in voc file");
block.SampleBlock.Offset = 0;
block.SampleBlock.Samples = stream.ReadUInt16() + 1;
var freqDiv = stream.ReadByte();
var freqDiv = stream.ReadUInt8();
block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
break;
}
@@ -231,10 +231,10 @@ namespace OpenRA.Mods.Cnc.AudioLoaders
int freqDiv = stream.ReadUInt16();
if (freqDiv == 65536)
throw new InvalidDataException("Invalid frequency divisor 65536 in voc file");
var codec = stream.ReadByte();
var codec = stream.ReadUInt8();
if (codec != 0)
throw new InvalidDataException("Unhandled codec used in voc file");
var channels = stream.ReadByte() + 1;
var channels = stream.ReadUInt8() + 1;
if (channels != 1)
throw new InvalidDataException("Unhandled number of channels in voc file");
block.SampleBlock.Offset = 0;

View File

@@ -18,13 +18,13 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.Cnc.FileFormats
{
[Flags]
enum SoundFlags
enum SoundFlags : byte
{
Stereo = 0x1,
_16Bit = 0x2,
}
enum SoundFormat
enum SoundFormat : byte
{
WestwoodCompressed = 1,
ImaAdpcm = 99,
@@ -59,12 +59,12 @@ namespace OpenRA.Mods.Cnc.FileFormats
sampleRate = s.ReadUInt16();
var dataSize = s.ReadInt32();
var outputSize = s.ReadInt32();
var audioFlags = (SoundFlags)s.ReadByte();
var audioFlags = (SoundFlags)s.ReadUInt8();
sampleBits = (audioFlags & SoundFlags._16Bit) == 0 ? 8 : 16;
channels = (audioFlags & SoundFlags.Stereo) == 0 ? 1 : 2;
lengthInSeconds = (float)(outputSize * 8) / (channels * sampleBits * sampleRate);
var readFormat = s.ReadByte();
var readFormat = s.ReadUInt8();
if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
return false;

View File

@@ -93,8 +93,8 @@ namespace OpenRA.Mods.Cnc.FileFormats
// Audio
SampleRate = stream.ReadUInt16();
AudioChannels = stream.ReadByte();
SampleBits = stream.ReadByte();
AudioChannels = stream.ReadUInt8();
SampleBits = stream.ReadUInt8();
/*var unknown3 =*/stream.ReadUInt32();
/*var unknown4 =*/stream.ReadUInt16();
@@ -221,7 +221,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
}
// Chunks are aligned on even bytes; advance by a byte if the next one is null
if (stream.Peek() == 0) stream.ReadByte();
if (stream.Peek() == 0) stream.ReadUInt8();
}
}
@@ -300,7 +300,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
DecodeVQFR(stream);
break;
case "\0VQF":
stream.ReadByte();
stream.ReadUInt8();
DecodeVQFR(stream);
break;
case "VQFL":
@@ -313,7 +313,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
}
// Chunks are aligned on even bytes; advance by a byte if the next one is null
if (stream.Peek() == 0) stream.ReadByte();
if (stream.Peek() == 0) stream.ReadUInt8();
}
// Now that the frame data has been loaded (in the relevant private fields), decode it into CurrentFrameData.
@@ -340,7 +340,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
while (true)
{
// Chunks are aligned on even bytes; may be padded with a single null
if (s.Peek() == 0) s.ReadByte();
if (s.Peek() == 0) s.ReadUInt8();
var type = s.ReadASCII(4);
var subchunkLength = (int)int2.Swap(s.ReadUInt32());

View File

@@ -15,7 +15,7 @@ using System.IO;
namespace OpenRA.Mods.Cnc.FileFormats
{
public enum NormalType { TiberianSun = 2, RedAlert2 = 4 }
public enum NormalType : byte { TiberianSun = 2, RedAlert2 = 4 }
public readonly struct VxlElement
{
public readonly byte Color;
@@ -148,7 +148,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
for (var j = 0; j < 6; j++)
Limbs[i].Bounds[j] = s.ReadFloat();
Limbs[i].Size = s.ReadBytes(3);
Limbs[i].Type = (NormalType)s.ReadByte();
Limbs[i].Type = (NormalType)s.ReadUInt8();
}
for (var i = 0; i < LimbCount; i++)

View File

@@ -63,9 +63,9 @@ namespace OpenRA.Mods.Cnc.FileFormats
paletteBytes = new byte[1024];
for (var i = 0; i < paletteBytes.Length;)
{
var r = (byte)(stream.ReadByte() << 2);
var g = (byte)(stream.ReadByte() << 2);
var b = (byte)(stream.ReadByte() << 2);
var r = (byte)(stream.ReadUInt8() << 2);
var g = (byte)(stream.ReadUInt8() << 2);
var b = (byte)(stream.ReadUInt8() << 2);
// Replicate high bits into the (currently zero) low bits.
r |= (byte)(r >> 6);

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
public class ShpD2Loader : ISpriteLoader
{
[Flags]
enum FormatFlags : int
enum FormatFlags : ushort
{
PaletteTable = 1,
NotLCWCompressed = 2,

View File

@@ -73,7 +73,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
public class ShpTDSprite
{
enum Format { XORPrev = 0x20, XORLCW = 0x40, LCW = 0x80 }
enum Format : ushort { XORPrev = 0x20, XORLCW = 0x40, LCW = 0x80 }
sealed class ImageHeader : ISpriteFrame
{

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Mods.Common.FileFormats
{
public static class WavReader
{
enum WaveType { Pcm = 0x1, MsAdpcm = 0x2, ImaAdpcm = 0x11 }
enum WaveType : short { Pcm = 0x1, MsAdpcm = 0x2, ImaAdpcm = 0x11 }
public static bool LoadSound(Stream s, out Func<Stream> result, out short channels, out int sampleBits, out int sampleRate, out float lengthInSeconds)
{
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.FileFormats
while (s.Position < s.Length)
{
if ((s.Position & 1) == 1)
s.ReadByte(); // Alignment
s.ReadUInt8(); // Alignment
if (s.Position == s.Length)
break; // Break if we aligned with end of stream

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.FileSystem
s.Position += 12;
var chunkSize = s.ReadUInt16();
s.Position += 4;
var nameLength = s.ReadByte();
var nameLength = s.ReadUInt8();
var fileName = dirName + "\\" + s.ReadASCII(nameLength);
// Use index syntax to overwrite any duplicate entries with the last value