diff --git a/ATL/AudioData/AudioDataIOFactory.cs b/ATL/AudioData/AudioDataIOFactory.cs index 7c3232e3..461e02c4 100644 --- a/ATL/AudioData/AudioDataIOFactory.cs +++ b/ATL/AudioData/AudioDataIOFactory.cs @@ -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)) { @@ -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 { diff --git a/ATL/AudioData/IO/AAC.cs b/ATL/AudioData/IO/AAC.cs index f6e23968..609db882 100644 --- a/ATL/AudioData/IO/AAC.cs +++ b/ATL/AudioData/IO/AAC.cs @@ -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) diff --git a/ATL/AudioData/IO/AC3.cs b/ATL/AudioData/IO/AC3.cs index b0890ff7..f3b7dbdf 100644 --- a/ATL/AudioData/IO/AC3.cs +++ b/ATL/AudioData/IO/AC3.cs @@ -83,7 +83,7 @@ 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; @@ -91,7 +91,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT 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 { @@ -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 { diff --git a/ATL/AudioData/IO/AIFF.cs b/ATL/AudioData/IO/AIFF.cs index 1fda008d..0c8c397c 100644 --- a/ATL/AudioData/IO/AIFF.cs +++ b/ATL/AudioData/IO/AIFF.cs @@ -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) diff --git a/ATL/AudioData/IO/DSF.cs b/ATL/AudioData/IO/DSF.cs index e5626dad..d488ca92 100644 --- a/ATL/AudioData/IO/DSF.cs +++ b/ATL/AudioData/IO/DSF.cs @@ -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) @@ -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 { @@ -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; diff --git a/ATL/AudioData/IO/DTS.cs b/ATL/AudioData/IO/DTS.cs index 252a7e8d..c036c820 100644 --- a/ATL/AudioData/IO/DTS.cs +++ b/ATL/AudioData/IO/DTS.cs @@ -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; diff --git a/ATL/AudioData/IO/FLAC.cs b/ATL/AudioData/IO/FLAC.cs index 0ced5cc8..a1dc2039 100644 --- a/ATL/AudioData/IO/FLAC.cs +++ b/ATL/AudioData/IO/FLAC.cs @@ -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++; diff --git a/ATL/AudioData/IO/Helpers/BextTag.cs b/ATL/AudioData/IO/Helpers/BextTag.cs index c47aeee5..693fa7ab 100644 --- a/ATL/AudioData/IO/Helpers/BextTag.cs +++ b/ATL/AudioData/IO/Helpers/BextTag.cs @@ -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(); @@ -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); @@ -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); diff --git a/ATL/AudioData/IO/Helpers/CueTag.cs b/ATL/AudioData/IO/Helpers/CueTag.cs index 72198736..3d7e0fbe 100644 --- a/ATL/AudioData/IO/Helpers/CueTag.cs +++ b/ATL/AudioData/IO/Helpers/CueTag.cs @@ -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 diff --git a/ATL/AudioData/IO/Helpers/DispTag.cs b/ATL/AudioData/IO/Helpers/DispTag.cs index 17b5498c..72d60207 100644 --- a/ATL/AudioData/IO/Helpers/DispTag.cs +++ b/ATL/AudioData/IO/Helpers/DispTag.cs @@ -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); } diff --git a/ATL/AudioData/IO/Helpers/FlacHelper.cs b/ATL/AudioData/IO/Helpers/FlacHelper.cs index a4355cb1..f19b6088 100644 --- a/ATL/AudioData/IO/Helpers/FlacHelper.cs +++ b/ATL/AudioData/IO/Helpers/FlacHelper.cs @@ -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); } /// diff --git a/ATL/AudioData/IO/Helpers/ListTag.cs b/ATL/AudioData/IO/Helpers/ListTag.cs index 588f1e58..b0ec6a61 100644 --- a/ATL/AudioData/IO/Helpers/ListTag.cs +++ b/ATL/AudioData/IO/Helpers/ListTag.cs @@ -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 @@ -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); @@ -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; @@ -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 @@ -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 diff --git a/ATL/AudioData/IO/Helpers/SampleTag.cs b/ATL/AudioData/IO/Helpers/SampleTag.cs index aad36ae5..6c77b3e3 100644 --- a/ATL/AudioData/IO/Helpers/SampleTag.cs +++ b/ATL/AudioData/IO/Helpers/SampleTag.cs @@ -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); diff --git a/ATL/AudioData/IO/Helpers/WMAHelper.cs b/ATL/AudioData/IO/Helpers/WMAHelper.cs index c21cef1a..e0b04b78 100644 --- a/ATL/AudioData/IO/Helpers/WMAHelper.cs +++ b/ATL/AudioData/IO/Helpers/WMAHelper.cs @@ -28,29 +28,29 @@ public static IList> 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)); } diff --git a/ATL/AudioData/IO/Helpers/WavHelper.cs b/ATL/AudioData/IO/Helpers/WavHelper.cs index 8b87d264..311fef12 100644 --- a/ATL/AudioData/IO/Helpers/WavHelper.cs +++ b/ATL/AudioData/IO/Helpers/WavHelper.cs @@ -54,7 +54,7 @@ public static IList GetEligibleKeys(string prefix, ICollection k /// Read value public static int ReadInt32(Stream source, MetaDataIO meta, string fieldName, byte[] buffer, bool readAllMetaFrames) { - source.Read(buffer, 0, 4); + if (source.Read(buffer, 0, 4) < 4) return 0; int value = StreamUtils.DecodeInt32(buffer); meta.SetMetaField(fieldName, value.ToString(), readAllMetaFrames); return value; @@ -71,7 +71,7 @@ public static int ReadInt32(Stream source, MetaDataIO meta, string fieldName, by /// Read value public static void ReadInt16(Stream source, MetaDataIO meta, string fieldName, byte[] buffer, bool readAllMetaFrames) { - source.Read(buffer, 0, 2); + if (source.Read(buffer, 0, 2) < 2) return; int value = StreamUtils.DecodeInt16(buffer); meta.SetMetaField(fieldName, value.ToString(), readAllMetaFrames); } diff --git a/ATL/AudioData/IO/ID3v1.cs b/ATL/AudioData/IO/ID3v1.cs index 2ac729c3..75b0e763 100644 --- a/ATL/AudioData/IO/ID3v1.cs +++ b/ATL/AudioData/IO/ID3v1.cs @@ -282,7 +282,7 @@ private bool ReadTag(BufferedBinaryReader source) // ID3v1 tags are C-String(null-terminated)-based tags encoded in ASCII byte[] data = new byte[ID3V1_TAG_SIZE]; - source.Read(data, 0, ID3V1_TAG_SIZE); + if (source.Read(data, 0, ID3V1_TAG_SIZE) < ID3V1_TAG_SIZE) return false; string header = Utils.Latin1Encoding.GetString(data, 0, 3); if (!header.Equals(ID3V1_ID)) return false; diff --git a/ATL/AudioData/IO/MIDI.cs b/ATL/AudioData/IO/MIDI.cs index 05802ddc..449c0d30 100644 --- a/ATL/AudioData/IO/MIDI.cs +++ b/ATL/AudioData/IO/MIDI.cs @@ -439,7 +439,7 @@ protected override bool read(Stream source, ReadTagParams readTagParams) FindValidHeader(source); // Ready to read header data... - source.Read(buffer, 0, buffer.Length); + if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return false; if (buffer[0] != 0 || buffer[1] != 0 || buffer[2] != 0 || @@ -473,7 +473,7 @@ protected override bool read(Stream source, ReadTagParams readTagParams) // Ready to read track data... while (source.Position < sizeInfo.FileSize - 4) { - source.Read(buffer, 0, 4); + if (source.Read(buffer, 0, 4) < 4) return false; trigger = Utils.Latin1Encoding.GetString(buffer, 0, 4); if (trigger != MIDI_TRACK_HEADER) @@ -483,11 +483,11 @@ protected override bool read(Stream source, ReadTagParams readTagParams) } // trackSize is stored in big endian -> needs inverting - source.Read(buffer, 0, 4); + if (source.Read(buffer, 0, 4) < 4) return false; var trackSize = StreamUtils.DecodeBEInt32(buffer); byte[] trackData = new byte[trackSize]; - source.Read(trackData, 0, trackSize); + if (source.Read(trackData, 0, trackSize) < trackSize) return false; m_tracks.Add(parseTrack(trackData, nbTrack)); nbTrack++; } diff --git a/ATL/AudioData/IO/MP4.cs b/ATL/AudioData/IO/MP4.cs index f44fb494..06fc12ab 100644 --- a/ATL/AudioData/IO/MP4.cs +++ b/ATL/AudioData/IO/MP4.cs @@ -2598,7 +2598,7 @@ private int writeQTChaptersTrack(BinaryWriter w, int trackNum, IList integratePics3(IList targetPics) + private IList integratePics(IList targetPics) { IList resultPictures = new List(); IList targetPictures = new List(targetPics.Where(p => !p.MarkedForDeletion)); @@ -478,24 +478,10 @@ private IList integratePics3(IList targetPics) // Remove contradictory target pictures (same ID with both keep and delete flags) var deleteOrders = targetPics.Where(p => p.MarkedForDeletion).ToHashSet(); - /* - foreach (PictureInfo pic in targetPics) - { - var found = false; - foreach (PictureInfo del in deleteOrders) - { - if (!pic.EqualsProper(del)) continue; - found = true; - break; - } - if (!found) targetPictures.Add(pic); - } - */ foreach (PictureInfo del in deleteOrders) { foreach (PictureInfo pic in targetPictures.ToHashSet()) { - //if (!pic.EqualsProper(del)) continue; if (!pic.Equals(del)) continue; targetPictures.Remove(pic); break; @@ -578,12 +564,12 @@ private IList integratePics3(IList targetPics) return orderedResultPictures; } - private void registerPosition(PictureInfo picInfo, IList> positions) + private static void registerPosition(PictureInfo picInfo, IList> positions) { positions.Add(new KeyValuePair(picInfo.ToString(), picInfo.Position)); } - private int nextPosition(PictureInfo picInfo, IList> positions) + private static int nextPosition(PictureInfo picInfo, IList> positions) { string picId = picInfo.ToString(); bool found = false;