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 2a7f8a7 commit 97c1842
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 82 deletions.
4 changes: 2 additions & 2 deletions ATL/AudioData/AudioDataIOFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public IAudioDataIO GetFromStream(Stream s)
s.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[32];
long offset = 0;
s.Read(data, 0, 32);
if (s.Read(data, 0, 32) < 32) return getFromFormat("in-memory", new AudioFormat(Format.UNKNOWN_FORMAT));
// Hardcoded case of ID3v2 as it is the sole standard metadata system to appear at the beginning of file
if (ID3v2.IsValidHeader(data))
{
Expand All @@ -421,7 +421,7 @@ public IAudioDataIO GetFromStream(Stream s)
int id3v2Size = StreamUtils.DecodeSynchSafeInt32(data2) + 10; // 10 being the size of the header
s.Seek(id3v2Size, SeekOrigin.Begin);
offset = s.Position;
s.Read(data, 0, 32);
if (s.Read(data, 0, 32) < 32) return getFromFormat("in-memory", new AudioFormat(Format.UNKNOWN_FORMAT));
}
try
{
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/AAC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private byte recognizeHeaderType(Stream source)
byte[] header = new byte[4];

source.Seek(sizeInfo.ID3v2Size, SeekOrigin.Begin);
source.Read(header, 0, header.Length);
if (source.Read(header, 0, header.Length) < header.Length) return AAC_HEADER_TYPE_UNKNOWN;

byte result = recognizeHeaderType(header);
if (result != AAC_BITRATE_TYPE_UNKNOWN)
Expand Down
6 changes: 3 additions & 3 deletions ATL/AudioData/IO/AC3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT

byte[] buffer = new byte[2];
source.Seek(0, SeekOrigin.Begin);
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return false;

if (!IsValidHeader(buffer)) return false;

AudioDataOffset = source.Position - 2;
AudioDataSize = sizeNfo.FileSize - sizeNfo.APESize - sizeNfo.ID3v1Size - AudioDataOffset;

source.Seek(2, SeekOrigin.Current);
source.Read(buffer, 0, 1);
if (source.Read(buffer, 0, 1) < 1) return false;

sampleRate = (buffer[0] & 0xC0) switch
{
Expand All @@ -104,7 +104,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
BitRate = BITRATES[(buffer[0] & 0x3F) >> 1];

source.Seek(1, SeekOrigin.Current);
source.Read(buffer, 0, 1);
if (source.Read(buffer, 0, 1) < 1) return false;

channelsArrangement = (buffer[0] & 0xE0) switch
{
Expand Down
4 changes: 2 additions & 2 deletions ATL/AudioData/IO/AIFF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ private ChunkHeader seekNextChunkHeader(BufferedBinaryReader source, long limit,
byte[] aByte = new byte[1];
int previousChunkSizeCorrection = 0;

source.Read(aByte, 0, 1);
if (source.Read(aByte, 0, 1) < 1) return header;

// In case previous chunk has a padding byte, seek a suitable first character for an ID
if (aByte[0] != 40 && !char.IsLetter((char)aByte[0]) && source.Position <= limit)
{
previousChunkSizeCorrection++;
if (source.Position < limit) source.Read(aByte, 0, 1);
if (source.Position < limit && source.Read(aByte, 0, 1) < 1) return header;
}

// Update zone size (remove and replace zone with updated size)
Expand Down
16 changes: 8 additions & 8 deletions ATL/AudioData/IO/DSF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
resetData();

source.Seek(0, SeekOrigin.Begin);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
if (StreamUtils.ArrBeginsWith(buffer, DSD_ID))
{
source.Seek(16, SeekOrigin.Current); // Chunk size and file size
source.Read(buffer, 0, 8);
if (source.Read(buffer, 0, 8) < 8) return false;
id3v2Offset = StreamUtils.DecodeInt64(buffer);

source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
if (StreamUtils.ArrBeginsWith(buffer, FMT_ID))
{
source.Seek(8, SeekOrigin.Current); // Chunk size

source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
int formatVersion = StreamUtils.DecodeInt32(buffer);

if (formatVersion > 1)
Expand All @@ -137,7 +137,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT

source.Seek(8, SeekOrigin.Current); // Format ID (4), Channel type (4)

source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
uint channels = StreamUtils.DecodeUInt32(buffer);
channelsArrangement = channels switch
{
Expand All @@ -151,12 +151,12 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
_ => UNKNOWN
};

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;
bits = StreamUtils.DecodeUInt32(buffer);

source.Read(buffer, 0, 8);
if (source.Read(buffer, 0, 8) < 8) return false;
ulong sampleCount = StreamUtils.DecodeUInt64(buffer);

Duration = sampleCount * 1000.0 / sampleRate;
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/DTS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT

resetData();

source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return false;
if (!IsValidHeader(buffer)) return false;

AudioDataOffset = source.Position - 4;
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/FLAC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public bool Read(Stream source, ReadTagParams readTagParams)
bool isLast;
do // Read all metadata blocks
{
source.Read(metaDataBlockHeader, 0, 4);
if (source.Read(metaDataBlockHeader, 0, 4) < 4) return false;
isLast = (metaDataBlockHeader[0] & FLAG_LAST_METADATA_BLOCK) > 0; // last flag ( first bit == 1 )

blockIndex++;
Expand Down
31 changes: 16 additions & 15 deletions ATL/AudioData/IO/Helpers/BextTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
byte[] data = new byte[256];

// Description
source.Read(data, 0, 256);
if (source.Read(data, 0, 256) < 256) return;
var str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data).Trim());
if (str.Length > 0) meta.SetMetaField("bext.description", str, readTagParams.ReadAllMetaFrames);

// Originator
source.Read(data, 0, 32);
if (source.Read(data, 0, 32) < 32) return;
str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 32).Trim());
if (str.Length > 0) meta.SetMetaField("bext.originator", str, readTagParams.ReadAllMetaFrames);

// OriginatorReference
source.Read(data, 0, 32);
if (source.Read(data, 0, 32) < 32) return;
str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 32).Trim());
if (str.Length > 0) meta.SetMetaField("bext.originatorReference", str, readTagParams.ReadAllMetaFrames);

// OriginationDate
source.Read(data, 0, 10);
if (source.Read(data, 0, 10) < 10) return;
str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 10).Trim());
if (str.Length > 0) meta.SetMetaField("bext.originationDate", str, readTagParams.ReadAllMetaFrames);

// OriginationTime
source.Read(data, 0, 8);
if (source.Read(data, 0, 8) < 8) return;
str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 8).Trim());
if (str.Length > 0) meta.SetMetaField("bext.originationTime", str, readTagParams.ReadAllMetaFrames);

// TimeReference
source.Read(data, 0, 8);
if (source.Read(data, 0, 8) < 8) return;
ulong timeReference = StreamUtils.DecodeUInt64(data);
meta.SetMetaField("bext.timeReference", timeReference.ToString(), readTagParams.ReadAllMetaFrames);

// BEXT version
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
int intData = StreamUtils.DecodeUInt16(data);
meta.SetMetaField("bext.version", intData.ToString(), readTagParams.ReadAllMetaFrames);

// UMID
source.Read(data, 0, 64);
if (source.Read(data, 0, 64) < 64) return;
int usefulLength = 32; // "basic" UMID
if (data[12] > 19) usefulLength = 64; // data[12] gives the size of remaining UMID
StringBuilder sbr = new StringBuilder();
Expand All @@ -75,27 +75,27 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
meta.SetMetaField("bext.UMID", sbr.ToString(), readTagParams.ReadAllMetaFrames);

// LoudnessValue
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
intData = StreamUtils.DecodeInt16(data);
meta.SetMetaField("bext.loudnessValue", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames);

// LoudnessRange
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
intData = StreamUtils.DecodeInt16(data);
meta.SetMetaField("bext.loudnessRange", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames);

// MaxTruePeakLevel
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
intData = StreamUtils.DecodeInt16(data);
meta.SetMetaField("bext.maxTruePeakLevel", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames);

// MaxMomentaryLoudness
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
intData = StreamUtils.DecodeInt16(data);
meta.SetMetaField("bext.maxMomentaryLoudness", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames);

// MaxShortTermLoudness
source.Read(data, 0, 2);
if (source.Read(data, 0, 2) < 2) return;
intData = StreamUtils.DecodeInt16(data);
meta.SetMetaField("bext.maxShortTermLoudness", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames);

Expand All @@ -109,8 +109,9 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
long endPos = source.Position - 2;
source.Seek(initialPos, SeekOrigin.Begin);

if (data.Length < (int)(endPos - initialPos)) data = new byte[(int)(endPos - initialPos)];
source.Read(data, 0, (int)(endPos - initialPos));
int size = (int)(endPos - initialPos);
if (data.Length < size) data = new byte[size];
if (source.Read(data, 0, size) < size) return;

str = Utils.StripEndingZeroChars(Utils.Latin1Encoding.GetString(data, 0, (int)(endPos - initialPos)).Trim());
if (str.Length > 0) meta.SetMetaField("bext.codingHistory", str, readTagParams.ReadAllMetaFrames);
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/Helpers/CueTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
WavHelper.ReadInt32(source, meta, "cue.CuePoints[" + i + "].Position", data, readTagParams.ReadAllMetaFrames);

// RIFF ID of corresponding data chunk
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
meta.SetMetaField("cue.CuePoints[" + i + "].DataChunkId", Utils.Latin1Encoding.GetString(data, 0, 4), readTagParams.ReadAllMetaFrames);

// Byte Offset of Data Chunk
Expand Down
4 changes: 2 additions & 2 deletions ATL/AudioData/IO/Helpers/DispTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
int index = keys.Count;

// Type
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
int type = StreamUtils.DecodeInt32(data);
meta.SetMetaField("disp.entry[" + index + "].type", getCfLabel(type), readTagParams.ReadAllMetaFrames);

// Data
source.Read(data, 0, (int)chunkSize - 4);
if (source.Read(data, 0, (int)chunkSize - 4) < chunkSize - 4) return;
var dataStr = Utils.Latin1Encoding.GetString(CF_TEXT == type ? data : Utils.EncodeTo64(data));
meta.SetMetaField("disp.entry[" + index + "].value", dataStr, readTagParams.ReadAllMetaFrames);
}
Expand Down
10 changes: 6 additions & 4 deletions ATL/AudioData/IO/Helpers/FlacHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public void Reset()
public void fromStream(Stream source)
{
Offset = source.Position;
source.Read(StreamMarker, 0, 4);
source.Read(MetaDataBlockHeader, 0, 4);
source.Read(Info, 0, 18); // METADATA_BLOCK_STREAMINFO
source.Seek(16, SeekOrigin.Current); // MD5 sum for audio data
if (source.Read(StreamMarker, 0, 4) < 4) return;
if (source.Read(MetaDataBlockHeader, 0, 4) < 4) return;
// METADATA_BLOCK_STREAMINFO
if (source.Read(Info, 0, 18) < 18) return;
// MD5 sum for audio data
source.Seek(16, SeekOrigin.Current);
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions ATL/AudioData/IO/Helpers/ListTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static string FromStream(Stream source, MetaDataIO meta, ReadTagParams re

// Purpose
byte[] data = new byte[4];
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return "";
string typeId = Utils.Latin1Encoding.GetString(data, 0, 4);

long maxPos = initialPos + chunkSize - 4; // 4 being the purpose 32bits tag that belongs to the chunk
Expand All @@ -47,15 +47,15 @@ private static void readInfoPurpose(Stream source, MetaDataIO meta, ReadTagParam
while (source.Position < maxPos)
{
// Key
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
var key = Utils.Latin1Encoding.GetString(data, 0, 4);
// Size
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
var size = StreamUtils.DecodeInt32(data);
// Do _NOT_ use StreamUtils.ReadNullTerminatedString because non-textual fields may be found here (e.g. NITR)
if (size > 0)
{
source.Read(data, 0, size);
if (source.Read(data, 0, size) < size) return;
// Manage parasite zeroes at the end of data
if (source.Position < maxPos && source.ReadByte() != 0) source.Seek(-1, SeekOrigin.Current);
var value = Encoding.UTF8.GetString(data, 0, size);
Expand All @@ -76,10 +76,10 @@ private static void readDataListPurpose(Stream source, MetaDataIO meta, ReadTagP
while (source.Position < maxPos)
{
// Sub-chunk ID
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
id = Utils.Latin1Encoding.GetString(data, 0, 4);
// Size
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
size = StreamUtils.DecodeInt32(data);
if (size <= 0) continue;

Expand All @@ -98,7 +98,7 @@ private static void readLabelSubChunk(Stream source, MetaDataIO meta, int positi
byte[] data = new byte[Math.Max(4, size - 4)];
WavHelper.ReadInt32(source, meta, "adtl.Labels[" + position + "].CuePointId", data, readTagParams.ReadAllMetaFrames);

source.Read(data, 0, size - 4);
if (source.Read(data, 0, size - 4) < size - 4) return;
string value = Encoding.UTF8.GetString(data, 0, size - 4);
value = Utils.StripEndingZeroChars(value); // Not ideal but effortslessly handles the ending zero

Expand All @@ -116,7 +116,7 @@ private static void readLabeledTextSubChunk(Stream source, MetaDataIO meta, int
WavHelper.ReadInt16(source, meta, "adtl.Labels[" + position + "].Dialect", data, readTagParams.ReadAllMetaFrames);
WavHelper.ReadInt16(source, meta, "adtl.Labels[" + position + "].CodePage", data, readTagParams.ReadAllMetaFrames);

source.Read(data, 0, size - 20);
if (source.Read(data, 0, size - 20) < size - 20) return;
string value = Encoding.UTF8.GetString(data, 0, size - 20);
value = Utils.StripEndingZeroChars(value); // Not ideal but effortslessly handles the ending zero

Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/Helpers/SampleTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read
WavHelper.ReadInt32(source, meta, "sample.SMPTEFormat", data, readTagParams.ReadAllMetaFrames);

// SMPTE offsets
source.Read(data, 0, 4);
if (source.Read(data, 0, 4) < 4) return;
meta.SetMetaField("sample.SMPTEOffset.Hours", ((sbyte)data[0]).ToString(), readTagParams.ReadAllMetaFrames);
meta.SetMetaField("sample.SMPTEOffset.Minutes", data[1].ToString(), readTagParams.ReadAllMetaFrames);
meta.SetMetaField("sample.SMPTEOffset.Seconds", data[2].ToString(), readTagParams.ReadAllMetaFrames);
Expand Down
14 changes: 7 additions & 7 deletions ATL/AudioData/IO/Helpers/WMAHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ public static IList<KeyValuePair<string, string>> ReadFields(Stream source, long
long pos = initialPos;
while (pos < initialPos + atomDataSize)
{
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return result;
int fieldSize = StreamUtils.DecodeBEInt32(buffer);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return result;
int stringDataSize = StreamUtils.DecodeBEInt32(buffer);
byte[] data = new byte[stringDataSize];
source.Read(data, 0, stringDataSize);
if (source.Read(data, 0, stringDataSize) < stringDataSize) return result;
string fieldName = Utils.Latin1Encoding.GetString(data);
source.Seek(4, SeekOrigin.Current);
source.Read(buffer, 0, 4);
if (source.Read(buffer, 0, 4) < 4) return result;
stringDataSize = StreamUtils.DecodeBEInt32(buffer);

string fieldValue;
source.Read(buffer, 0, 2);
if (source.Read(buffer, 0, 2) < 2) return result;
int fieldType = StreamUtils.DecodeBEInt16(buffer);
if (19 == fieldType) // Numeric
{
source.Read(buffer, 0, 8);
if (source.Read(buffer, 0, 8) < 8) return result;
fieldValue = StreamUtils.DecodeInt64(buffer) + "";
}
else
{
data = new byte[stringDataSize - 6];
source.Read(data, 0, stringDataSize - 6);
if (source.Read(data, 0, stringDataSize - 6) < stringDataSize - 6) return result;
fieldValue = Utils.StripEndingZeroChars(Encoding.Unicode.GetString(data, 0, stringDataSize - 6));
}

Expand Down
Loading

0 comments on commit 97c1842

Please sign in to comment.