Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Nov 8, 2024
1 parent 112e790 commit fc42ed8
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 88 deletions.
10 changes: 5 additions & 5 deletions ATL/AudioData/IO/MP4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ private static Uuid readUuid(Stream source)
if (result.size >= 16 + 8)
{
Span<byte> key = new byte[16];
source.Read(key);
if (source.Read(key) < key.Length) return result;
// Convert key to hex value
StringBuilder sbr = new StringBuilder();
for (int i = 0; i < 16; i++) sbr.Append(key[i].ToString("X2"));
Expand All @@ -1592,7 +1592,7 @@ private static Uuid readUuid(Stream source)
if (dataSize > 0)
{
Span<byte> data = new byte[dataSize];
source.Read(data);
if (source.Read(data) < dataSize) return result;
result.value = Encoding.UTF8.GetString(data);
}
}
Expand Down Expand Up @@ -1645,14 +1645,14 @@ private static Tuple<uint, int> navigateToAtomSize(Stream source, string atomKey
{
if (!first) source.Seek(atomSize - atomHeaderSize, SeekOrigin.Current);
atomHeaderSize = 8; // Default variant where size takes up 32-bit
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return new Tuple<uint, int>(0, atomHeaderSize);
atomSize = StreamUtils.DecodeBEUInt32(data);
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return new Tuple<uint, int>(0, atomHeaderSize);
atomHeader = Utils.Latin1Encoding.GetString(data, 0, 4);
if (1 == atomSize) // 64-bit size variant
{
atomHeaderSize += 8;
source.Read(data, 0, 8);
if (source.Read(data, 0, 8) < 8) return new Tuple<uint, int>(0, atomHeaderSize);
atomSize = StreamUtils.DecodeBEInt64(data);
}

Expand Down
22 changes: 10 additions & 12 deletions ATL/AudioData/IO/MPEGaudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private static VBRData getXingInfo(Stream source)
result.Found = true;
result.ID = VBR_ID_XING.ToCharArray();
source.Seek(4, SeekOrigin.Current);
source.Read(data, 0, 8);
if (source.Read(data, 0, 8) < 8) return result;

result.Frames =
data[0] * 0x1000000 +
Expand All @@ -408,9 +408,9 @@ private static VBRData getXingInfo(Stream source)

source.Seek(103, SeekOrigin.Current);

source.Read(data, 0, 1);
if (source.Read(data, 0, 1) < 1) return result;
result.Scale = data[0];
source.Read(data, 0, 8);
if (source.Read(data, 0, 8) < 8) return result;
result.VendorID = Utils.Latin1Encoding.GetString(data, 0, 8);

return result;
Expand All @@ -425,7 +425,7 @@ private static VBRData getFhGInfo(Stream source)
result.Found = true;
result.ID = VBR_ID_FHG.ToCharArray();
source.Seek(5, SeekOrigin.Current);
source.Read(data, 0, 9);
if (source.Read(data, 0, 9) < 9) return result;

result.Scale = data[0];
result.Bytes =
Expand All @@ -452,17 +452,15 @@ private static VBRData findVBR(Stream source, long position)
// Check for VBR header at given position
source.Seek(position, SeekOrigin.Begin);

source.Read(data, 0, 4);
string vbrId = Utils.Latin1Encoding.GetString(data);

if (VBR_ID_XING.Equals(vbrId)) result = getXingInfo(source);
else if (VBR_ID_FHG.Equals(vbrId)) result = getFhGInfo(source);
else
if (4 == source.Read(data, 0, 4))
{
result = new VBRData();
result.Reset();
string vbrId = Utils.Latin1Encoding.GetString(data);
if (VBR_ID_XING.Equals(vbrId)) return getXingInfo(source);
if (VBR_ID_FHG.Equals(vbrId)) return getFhGInfo(source);
}

result = new VBRData();
result.Reset();
return result;
}

Expand Down
12 changes: 7 additions & 5 deletions ATL/AudioData/IO/MPEGplus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private bool readHeader(Stream source, ref HeaderRecord header)
source.Seek(sizeInfo.ID3v2Size, SeekOrigin.Begin);

// Read header and get file size
source.Read(header.ByteArray, 0, header.ByteArray.Length);
if (source.Read(header.ByteArray, 0, header.ByteArray.Length) < header.ByteArray.Length) return false;

// if transfer is not complete
byte[] temp = new byte[4];
Expand All @@ -146,7 +146,7 @@ private bool readHeader(Stream source, ref HeaderRecord header)
while (!headerFound)
{
long initialPos = source.Position;
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) break;
packetKey = Utils.Latin1Encoding.GetString(buffer);

readVariableSizeInteger(source); // Packet size (unused)
Expand All @@ -160,10 +160,12 @@ private bool readHeader(Stream source, ref HeaderRecord header)
long sampleCount = readVariableSizeInteger(source);
readVariableSizeInteger(source); // Skip beginning silence

source.Read(buffer, 0, 1);// Sample frequency (3) + Max used bands (5)
// Sample frequency (3) + Max used bands (5)
if (source.Read(buffer, 0, 1) < 1) break;
SampleRate = MPP_SAMPLERATES[(buffer[0] & 0b11100000) >> 5]; // First 3 bits

source.Read(buffer, 0, 1); // Channel count (4) + Mid/Side Stereo used (1) + Audio block frames (3)
// Channel count (4) + Mid/Side Stereo used (1) + Audio block frames (3)
if (source.Read(buffer, 0, 1) < 1) break;
int channelCount = (buffer[0] & 0b11110000) >> 4; // First 4 bits
bool isMidSideStereo = (buffer[0] & 0b00001000) > 0; // First 4 bits
if (isMidSideStereo) ChannelsArrangement = JOINT_STEREO_MID_SIDE;
Expand Down Expand Up @@ -300,7 +302,7 @@ private static long readVariableSizeInteger(Stream source)
// Data is coded with a Big-endian, 7-byte variable-length record
while ((b & 128) > 0)
{
source.Read(buffer, 0, 1);
if (source.Read(buffer, 0, 1) < 1) break;
b = buffer[0];
result = (result << 7) + (b & 127); // Big-endian
}
Expand Down
16 changes: 8 additions & 8 deletions ATL/AudioData/IO/OptimFROG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,21 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
long initialPos = source.Position;
byte[] buffer = new byte[4];

source.Read(header.ID, 0, 4);
source.Read(buffer, 0, 4);
if (source.Read(header.ID, 0, 4) < 4) return false;
if (source.Read(buffer, 0, 4) < 4) return false;
header.Size = StreamUtils.DecodeUInt32(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
header.Length = StreamUtils.DecodeUInt32(buffer);
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
header.HiLength = StreamUtils.DecodeUInt16(buffer);
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
header.SampleType = buffer[0];
header.ChannelMode = buffer[1];
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
header.SampleRate = StreamUtils.DecodeInt32(buffer);
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
header.EncoderID = StreamUtils.DecodeUInt16(buffer);
source.Read(buffer, 0, 1);
if (source.Read(buffer, 0, 1) < 1) return false;
header.CompressionID = buffer[0];

if (IsValidHeader(header.ID))
Expand Down
12 changes: 6 additions & 6 deletions ATL/AudioData/IO/PSF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ public static bool IsValidHeader(byte[] data)
private static bool readHeader(Stream source, ref PSFHeader header)
{
byte[] buffer = new byte[4];
source.Read(header.FormatTag, 0, 3);
if (source.Read(header.FormatTag, 0, 3) < 3) return false;
if (IsValidHeader(header.FormatTag))
{
source.Read(buffer, 0, 1);
if (source.Read(buffer, 0, 1) < 1) return false;
header.VersionByte = buffer[0];
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
header.ReservedAreaLength = StreamUtils.DecodeUInt32(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
header.CompressedProgramLength = StreamUtils.DecodeUInt32(buffer);
return true;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ private static string readPSFLine(Stream source, Encoding encoding)
source.Seek(lineStart, SeekOrigin.Begin);

byte[] data = new byte[lineEnd - lineStart];
source.Read(data, 0, data.Length);
if (source.Read(data, 0, data.Length) < data.Length) return "";

for (int i = 0; i < data.Length; i++) if (data[i] < SPACE) data[i] = SPACE; // According to spec : "All characters 0x01-0x20 are considered whitespace"

Expand All @@ -241,7 +241,7 @@ private bool readTag(Stream source, ref PSFTag tag, ReadTagParams readTagParams)
Encoding encoding = Utils.Latin1Encoding;

byte[] buffer = new byte[5];
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return false;
tag.TagHeader = Utils.Latin1Encoding.GetString(buffer);

if (TAG_HEADER == tag.TagHeader)
Expand Down
33 changes: 16 additions & 17 deletions ATL/AudioData/IO/SPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ private static bool readHeader(Stream source, ref SpcHeader header)

long initialPosition = source.Position;
byte[] buffer = new byte[SPC_FORMAT_TAG.Length];
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return false;
if (IsValidHeader(buffer))
{
source.Seek(8, SeekOrigin.Current); // Remainder of header tag (version marker vX.XX + 2 bytes)
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
header.TagInHeader = buffer[0];
header.Size = source.Position - initialPosition;
return true;
Expand All @@ -271,13 +271,13 @@ private void readHeaderTags(Stream source, ref SpcHeader header, ReadTagParams r
byte[] buffer = new byte[32];
long initialPosition = source.Position;

source.Read(buffer, 0, 32);
if (source.Read(buffer, 0, 32) < 32) return;
SetMetaField(HEADER_TITLE.ToString(), Utils.Latin1Encoding.GetString(buffer).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
source.Read(buffer, 0, 32);
if (source.Read(buffer, 0, 32) < 32) return;
SetMetaField(HEADER_ALBUM.ToString(), Utils.Latin1Encoding.GetString(buffer).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
source.Read(buffer, 0, 16);
if (source.Read(buffer, 0, 16) < 16) return;
SetMetaField(HEADER_DUMPERNAME.ToString(), Utils.Latin1Encoding.GetString(buffer).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
source.Read(buffer, 0, 32);
if (source.Read(buffer, 0, 32) < 32) return;
SetMetaField(HEADER_COMMENT.ToString(), Utils.Latin1Encoding.GetString(buffer).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);

byte[] date = new byte[11];
Expand All @@ -286,12 +286,11 @@ private void readHeaderTags(Stream source, ref SpcHeader header, ReadTagParams r

// NB : Dump date is used to determine if the tag is binary or text-based.
// It won't be recorded as a property of TSPC
source.Read(date, 0, date.Length);
source.Read(song, 0, song.Length);
source.Read(fade, 0, fade.Length);

if (source.Read(date, 0, date.Length) < date.Length) return;
int dateRes = isText(date);
if (source.Read(song, 0, song.Length) < song.Length) return;
int songRes = isText(song);
if (source.Read(fade, 0, fade.Length) < fade.Length) return;
int fadeRes = isText(fade);

bool bin = true;
Expand Down Expand Up @@ -352,7 +351,7 @@ private void readHeaderTags(Stream source, ref SpcHeader header, ReadTagParams r
// if fadeval > 0 alone, the fade is applied on the default 3:00 duration without extending it
if (songVal > 0) Duration = fadeVal + songVal;

source.Read(buffer, 0, 32);
if (source.Read(buffer, 0, 32) < 32) return;
SetMetaField(HEADER_ARTIST.ToString(), Utils.Latin1Encoding.GetString(buffer).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
header.Size += source.Position - initialPosition;

Expand All @@ -378,11 +377,11 @@ private void readExtendedData(Stream source, ref SpcExTags footer, ReadTagParams
{
long initialPosition = source.Position;
byte[] buffer = new byte[4];
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return;
footer.FormatTag = Utils.Latin1Encoding.GetString(buffer);
if (XTENDED_TAG == footer.FormatTag)
{
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return;
footer.Size = StreamUtils.DecodeUInt32(buffer);

string strData = "";
Expand All @@ -392,10 +391,10 @@ private void readExtendedData(Stream source, ref SpcExTags footer, ReadTagParams
long dataPosition = source.Position;
while (source.Position < dataPosition + footer.Size - 4)
{
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) break;
var ID = buffer[0];
var type = buffer[1];
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) break;
var size = StreamUtils.DecodeUInt16(buffer);

switch (type)
Expand All @@ -421,14 +420,14 @@ private void readExtendedData(Stream source, ref SpcExTags footer, ReadTagParams
case XID6_TSTR:
intData = 0;
byte[] strDatab = new byte[size];
source.Read(strDatab, 0, size);
if (source.Read(strDatab, 0, size) < size) break;
strData = Utils.Latin1Encoding.GetString(strDatab).Replace("\0", "").Trim();

while (source.Position < source.Length && 0 == source.ReadByte()) ; // Skip parasite ending zeroes
if (source.Position < source.Length) source.Seek(-1, SeekOrigin.Current);
break;
case XID6_TINT:
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) break;
intData = StreamUtils.DecodeInt32(buffer);
strData = intData.ToString();
break;
Expand Down
13 changes: 6 additions & 7 deletions ATL/AudioData/IO/TAK.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ATL.Logging;
using System;
using System.IO;
using static ATL.AudioData.AudioDataManager;
Expand Down Expand Up @@ -83,7 +82,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
resetData();
source.Seek(sizeNfo.ID3v2Size, SeekOrigin.Begin);

source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
if (IsValidHeader(buffer))
{
result = true;
Expand All @@ -92,7 +91,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT

do // Loop metadata
{
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
var readData32 = StreamUtils.DecodeUInt32(buffer);

var metaType = readData32 & 0x7F;
Expand All @@ -103,11 +102,11 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
if (0 == metaType) doLoop = false; // End of metadata
else if (0x01 == metaType) // Stream info
{
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
var readData16 = StreamUtils.DecodeUInt16(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
readData32 = StreamUtils.DecodeUInt32(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
uint restOfData = StreamUtils.DecodeUInt32(buffer);

var sampleCount = (readData16 >> 14) + (readData32 << 2) + ((restOfData & 0x00000080) << 34);
Expand All @@ -124,7 +123,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
}
else if (0x04 == metaType) // Encoder info
{
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
readData32 = StreamUtils.DecodeUInt32(buffer);
}

Expand Down
10 changes: 5 additions & 5 deletions ATL/AudioData/IO/TTA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
bool result = false;

byte[] buffer = new byte[4];
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return false;
if (IsValidHeader(buffer))
{
AudioDataOffset = source.Position - 4;
AudioDataSize = sizeNfo.FileSize - sizeNfo.APESize - sizeNfo.ID3v1Size - AudioDataOffset;

source.Seek(2, SeekOrigin.Current); // audio format
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
ChannelsArrangement = GuessFromChannelNumber(StreamUtils.DecodeUInt16(buffer));
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;
bitsPerSample = StreamUtils.DecodeUInt16(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
sampleRate = StreamUtils.DecodeUInt32(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
samplesSize = StreamUtils.DecodeUInt32(buffer);
source.Seek(4, SeekOrigin.Current); // CRC

Expand Down
Loading

0 comments on commit fc42ed8

Please sign in to comment.