diff --git a/Directory.Packages.props b/Directory.Packages.props index a12962b..0d5a043 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -12,5 +12,6 @@ + diff --git a/TagLibSharp2.slnx b/TagLibSharp2.slnx index 63381f6..034ac81 100644 --- a/TagLibSharp2.slnx +++ b/TagLibSharp2.slnx @@ -4,4 +4,5 @@ + diff --git a/benchmarks/TagLibSharp2.Benchmarks/MediaFileReadBenchmark.cs b/benchmarks/TagLibSharp2.Benchmarks/MediaFileReadBenchmark.cs new file mode 100644 index 0000000..347a73c --- /dev/null +++ b/benchmarks/TagLibSharp2.Benchmarks/MediaFileReadBenchmark.cs @@ -0,0 +1,48 @@ +using System.Diagnostics.CodeAnalysis; +using BenchmarkDotNet.Attributes; +using TagLibSharp2.Core; + +namespace TagLibSharp2.Benchmarks; + +/// +/// One of a file whose payload is 1 MiB. +/// The number to watch is Allocated: a windowed read stays far under that payload. +/// +[MemoryDiagnoser] +[SuppressMessage ("Performance", "CA1822", Justification = "BenchmarkDotNet requires instance methods")] +public class MediaFileReadBenchmark +{ + const int PayloadBytes = 1024 * 1024; + + [Params ("mp3", "wav", "flac")] + public string Format { get; set; } = ""; + + string _directory = ""; + string _path = ""; + + [GlobalSetup] + public void Setup () + { + _directory = Path.Combine (Path.GetTempPath (), "tls-bench-" + Guid.NewGuid ().ToString ("N")); + Directory.CreateDirectory (_directory); + _path = Path.Combine (_directory, "song." + Format); + File.WriteAllBytes (_path, SampleFiles.Create (Format, PayloadBytes)); + } + + [GlobalCleanup] + public void Cleanup () + { + if (Directory.Exists (_directory)) + Directory.Delete (_directory, true); + } + + [Benchmark] + public int Read () + { + var result = MediaFile.Read (_path); + var mark = result.IsSuccess ? (result.Tag?.Title?.Length ?? 0) + 1 : 0; + if (result.File is IDisposable disposable) + disposable.Dispose (); + return mark; + } +} diff --git a/benchmarks/TagLibSharp2.Benchmarks/Program.cs b/benchmarks/TagLibSharp2.Benchmarks/Program.cs new file mode 100644 index 0000000..beebe0d --- /dev/null +++ b/benchmarks/TagLibSharp2.Benchmarks/Program.cs @@ -0,0 +1,8 @@ +using BenchmarkDotNet.Running; +using TagLibSharp2.Benchmarks; + +if (args.Length > 0 && args[0] == "--scan") + return ScanDriver.Run (args); + +BenchmarkSwitcher.FromAssembly (typeof (Program).Assembly).Run (args); +return 0; diff --git a/benchmarks/TagLibSharp2.Benchmarks/SampleFiles.cs b/benchmarks/TagLibSharp2.Benchmarks/SampleFiles.cs new file mode 100644 index 0000000..d50ea59 --- /dev/null +++ b/benchmarks/TagLibSharp2.Benchmarks/SampleFiles.cs @@ -0,0 +1,99 @@ +using System.Buffers.Binary; +using TagLibSharp2.Id3; +using TagLibSharp2.Id3.Id3v2; + +namespace TagLibSharp2.Benchmarks; + +static class SampleFiles +{ + public static readonly string[] Formats = ["mp3", "wav", "flac"]; + + public static byte[] Create (string format, int payloadBytes) => format switch { + "mp3" => Mp3 (payloadBytes), + "wav" => Wav (payloadBytes), + "flac" => Flac (payloadBytes), + _ => throw new ArgumentOutOfRangeException (nameof (format), format, "Unknown sample format") + }; + + static byte[] Mp3 (int payloadBytes) + { + var id3v2 = new Id3v2Tag { Title = "Window" }.Render ().ToArray (); + var header = new byte[] { 0xFF, 0xFB, 0x90, 0x00 }; + const int FrameSize = 417; + var audio = new byte[FrameSize + 4]; + header.CopyTo (audio, 0); + header.CopyTo (audio, FrameSize); + var id3v1 = new Id3v1Tag { Title = "Tail" }.Render ().ToArray (); + var file = new byte[id3v2.Length + audio.Length + payloadBytes + id3v1.Length]; + var offset = 0; + id3v2.CopyTo (file, offset); + offset += id3v2.Length; + audio.CopyTo (file, offset); + offset += audio.Length + payloadBytes; + id3v1.CopyTo (file, offset); + return file; + } + + static byte[] Wav (int payloadBytes) + { + var title = "Window\0"u8; + var listBody = 4 + 4 + 4 + title.Length; + var form = 4 + (8 + 16) + (8 + payloadBytes) + (8 + listBody); + var file = new byte[8 + form]; + var span = file.AsSpan (); + "RIFF"u8.CopyTo (span); + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (4), (uint)form); + var o = 8; + "WAVE"u8.CopyTo (span.Slice (o)); + o += 4; + "fmt "u8.CopyTo (span.Slice (o)); + o += 4; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 16); + o += 4; + BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1); + o += 2; + BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1); + o += 2; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 44100); + o += 4; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), 44100); + o += 4; + BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 1); + o += 2; + BinaryPrimitives.WriteUInt16LittleEndian (span.Slice (o), 8); + o += 2; + "data"u8.CopyTo (span.Slice (o)); + o += 4; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)payloadBytes); + o += 4 + payloadBytes; + "LIST"u8.CopyTo (span.Slice (o)); + o += 4; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)listBody); + o += 4; + "INFO"u8.CopyTo (span.Slice (o)); + o += 4; + "INAM"u8.CopyTo (span.Slice (o)); + o += 4; + BinaryPrimitives.WriteUInt32LittleEndian (span.Slice (o), (uint)title.Length); + o += 4; + title.CopyTo (span.Slice (o)); + return file; + } + + static byte[] Flac (int payloadBytes) + { + var file = new byte[4 + 4 + 34 + payloadBytes]; + "fLaC"u8.CopyTo (file); + file[4] = 0x80; + file[7] = 34; + var info = file.AsSpan (8, 34); + const int SampleRate = 44100; + info[10] = (byte)((SampleRate >> 12) & 0xFF); + info[11] = (byte)((SampleRate >> 4) & 0xFF); + info[12] = (byte)((SampleRate & 0x0F) << 4); + info[12] |= 0x02; + info[13] = 0xF0; + BinaryPrimitives.WriteUInt32BigEndian (info.Slice (14), 44100); + return file; + } +} diff --git a/benchmarks/TagLibSharp2.Benchmarks/ScanDriver.cs b/benchmarks/TagLibSharp2.Benchmarks/ScanDriver.cs new file mode 100644 index 0000000..009a24c --- /dev/null +++ b/benchmarks/TagLibSharp2.Benchmarks/ScanDriver.cs @@ -0,0 +1,58 @@ +using System.Diagnostics; +using TagLibSharp2.Core; + +namespace TagLibSharp2.Benchmarks; + +static class ScanDriver +{ + public static int Run (string[] args) + { + var retain = args.Contains ("--retain"); + var directory = Path.Combine (Path.GetTempPath (), "tls-scan-" + Guid.NewGuid ().ToString ("N")); + Directory.CreateDirectory (directory); + var paths = new string[SampleFiles.Formats.Length]; + for (var i = 0; i < paths.Length; i++) { + paths[i] = Path.Combine (directory, "song." + SampleFiles.Formats[i]); + File.WriteAllBytes (paths[i], SampleFiles.Create (SampleFiles.Formats[i], 1024 * 1024)); + var check = MediaFile.Read (paths[i]); + Console.WriteLine ($"{SampleFiles.Formats[i]} success={check.IsSuccess} title={check.Tag?.Title} error={check.Error}"); + if (!check.IsSuccess) + return 1; + if (check.File is IDisposable disposable) + disposable.Dispose (); + } + + var rounds = retain ? 40 : 300; + foreach (var arg in args) { + if (int.TryParse (arg, out var parsed) && parsed > 0) + rounds = parsed; + } + var held = new List (retain ? rounds * paths.Length : 0); + var clock = Stopwatch.StartNew (); + var reads = 0; + for (var round = 0; round < rounds; round++) { + foreach (var path in paths) { + var result = MediaFile.Read (path); + reads++; + if (!result.IsSuccess) { + Console.Error.WriteLine (result.Error); + return 1; + } + + if (retain) + held.Add (result); + else if (result.File is IDisposable disposable) + disposable.Dispose (); + } + } + + clock.Stop (); + Console.WriteLine ($"reads={reads} ms={clock.ElapsedMilliseconds} retained={held.Count}"); + if (!retain) + return 0; + + Console.WriteLine ("READY"); + Thread.Sleep (TimeSpan.FromSeconds (20)); + return held.Count > 0 ? 0 : 1; + } +} diff --git a/benchmarks/TagLibSharp2.Benchmarks/TagLibSharp2.Benchmarks.csproj b/benchmarks/TagLibSharp2.Benchmarks/TagLibSharp2.Benchmarks.csproj new file mode 100644 index 0000000..40df914 --- /dev/null +++ b/benchmarks/TagLibSharp2.Benchmarks/TagLibSharp2.Benchmarks.csproj @@ -0,0 +1,22 @@ + + + + Exe + + net8.0;net10.0 + $(TargetFrameworks);net11.0 + false + false + $(NoWarn);CA1515;CA1303 + TagLibSharp2.Benchmarks + + + + + + + + + + + diff --git a/src/TagLibSharp2/Aiff/AiffChunk.cs b/src/TagLibSharp2/Aiff/AiffChunk.cs index 1ea12b4..a5fcb4b 100644 --- a/src/TagLibSharp2/Aiff/AiffChunk.cs +++ b/src/TagLibSharp2/Aiff/AiffChunk.cs @@ -55,6 +55,12 @@ public class AiffChunk Data = data; } + /// + /// A chunk whose declared size is kept when the bytes were left in the file. + /// + internal static AiffChunk WithDeclaredSize (string fourCC, uint size, BinaryData data) => + new (fourCC, size, data); + /// /// Creates a new AiffChunk with the specified FourCC and data. /// diff --git a/src/TagLibSharp2/Aiff/AiffFile.cs b/src/TagLibSharp2/Aiff/AiffFile.cs index a00e283..64420a6 100644 --- a/src/TagLibSharp2/Aiff/AiffFile.cs +++ b/src/TagLibSharp2/Aiff/AiffFile.cs @@ -126,6 +126,23 @@ public sealed class AiffFile : IMediaFile public MediaFormat Format => MediaFormat.Aiff; readonly List _chunks = []; + readonly List _skipped = []; + + readonly struct SkippedChunk + { + public SkippedChunk (int index, long offset, uint presentBytes) + { + Index = index; + Offset = offset; + PresentBytes = presentBytes; + } + + public int Index { get; } + + public long Offset { get; } + + public uint PresentBytes { get; } + } /// /// Releases resources used by this instance. @@ -229,6 +246,88 @@ public static AiffFileReadResult Read (ReadOnlySpan data) return AiffFileReadResult.Success (file); } + /// + /// Reads an AIFF file from a stream. SSND sample bytes stay in the file. + /// + internal static AiffFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return Read (StreamBytes.ReadToEnd (stream)); + + if (stream.Position != 0) + stream.Position = 0; + + var header = StreamBytes.ReadAtMost (stream, HeaderSize); + if (header.Length < HeaderSize || header[0] != (byte)'F' || header[1] != (byte)'O' || header[2] != (byte)'R' || header[3] != (byte)'M') + return AiffFileReadResult.Failure ("Invalid AIFF file: missing magic (expected 'FORM')"); + + AiffFile? file = new () { + FileSize = ReadUInt32BE (header, 4), + FormType = FourCC (header, 8) + }; + if (file.FormType != "AIFF" && file.FormType != "AIFC") { + var form = file.FormType; + file.Dispose (); + return AiffFileReadResult.Failure ($"Invalid AIFF file: invalid form type (expected 'AIFF' or 'AIFC', got '{form}')"); + } + + file.IsValid = true; + var limit = file.FileSize == 0xFFFFFFFF + ? stream.Length + : Math.Min (stream.Length, 8L + file.FileSize); + + while (stream.Position + 8 <= limit) { + var chunkHeader = StreamBytes.ReadAtMost (stream, 8); + if (chunkHeader.Length < 8) + break; + + var id = FourCC (chunkHeader, 0); + var size = ReadUInt32BE (chunkHeader, 4); + var dataPos = stream.Position; + var available = limit - dataPos; + if (available < 0) + available = 0; + var present = available < size ? (uint)available : size; + var pad = (size & 1) == 1 && present == size ? 1 : 0; + if (id == "SSND" || size > int.MaxValue) { + if (present > 0 && present <= int.MaxValue) + file._skipped.Add (new SkippedChunk (file._chunks.Count, dataPos, present)); + file._chunks.Add (AiffChunk.WithDeclaredSize (id, size, BinaryData.Empty)); + var skip = (long)present + pad; + var room = stream.Length - stream.Position; + if (skip > room) + skip = room < 0 ? 0 : room; + stream.Position += skip; + if (present < size) + break; + continue; + } + + var data = StreamBytes.ReadAtMost (stream, (int)present); + if (pad == 1 && stream.Position < limit) + StreamBytes.ReadAtMost (stream, 1); + var chunk = present == size + ? new AiffChunk (id, new BinaryData (data)) + : AiffChunk.WithDeclaredSize (id, size, new BinaryData (data)); + file._chunks.Add (chunk); + file.ProcessChunk (chunk); + if (present < size) + break; + } + + file.SourcePath = sourcePath; + file._sourceFileSystem = fileSystem; + var parsed = file; + file = null; + return AiffFileReadResult.Success (parsed); + } + + static uint ReadUInt32BE (byte[] data, int offset) => + ((uint)data[offset] << 24) | ((uint)data[offset + 1] << 16) | ((uint)data[offset + 2] << 8) | data[offset + 3]; + + static string FourCC (byte[] data, int offset) => + new ([(char)data[offset], (char)data[offset + 1], (char)data[offset + 2], (char)data[offset + 3]]); + /// /// Reads an AIFF file from a file path. /// @@ -361,6 +460,9 @@ public BinaryData Render () if (!IsValid) throw new InvalidOperationException ("Cannot render invalid AIFF file"); + if (!TryLoadSkipped (null, out var skipped)) + throw new InvalidOperationException (skipped); + // Calculate total size of all chunks. TotalSize is a long file length, not a wrapped int. long chunksSize = 0; foreach (var chunk in _chunks) { @@ -421,9 +523,50 @@ public BinaryData Render () /// The write result. public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) { + var fs = fileSystem ?? _sourceFileSystem; + if (!TryLoadSkipped (fs, out var skipped)) + return FileWriteResult.Failure (skipped!); + var data = Render (); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.Write (path, data.Span, fileSystem); + return AtomicFileWriter.Write (path, data.Span, fs); + } + + bool TryLoadSkipped (IFileSystem? fileSystem, out string? error) + { + error = null; + if (_skipped.Count == 0) + return true; + + foreach (var chunk in _skipped) { + if (chunk.PresentBytes > int.MaxValue) { + error = "Cannot save an AIFF file whose sound data does not fit in memory."; + return false; + } + } + + if (SourcePath is not { Length: > 0 } path) { + error = "Cannot save an AIFF file whose sound data was not loaded."; + return false; + } + + var fs = fileSystem ?? _sourceFileSystem ?? DefaultFileSystem.Instance; + using var stream = fs.OpenRead (path); + foreach (var chunk in _skipped) { + if (stream.Length < chunk.Offset + chunk.PresentBytes) { + error = "Cannot save an AIFF file whose sound data is no longer in the source file."; + return false; + } + } + + foreach (var chunk in _skipped) { + stream.Position = chunk.Offset; + var samples = StreamBytes.ReadAtMost (stream, (int)chunk.PresentBytes); + var existing = _chunks[chunk.Index]; + _chunks[chunk.Index] = AiffChunk.WithDeclaredSize (existing.FourCC, existing.Size, new BinaryData (samples)); + } + + _skipped.Clear (); + return true; } /// @@ -438,9 +581,12 @@ public Task SaveToFileAsync ( IFileSystem? fileSystem = null, CancellationToken cancellationToken = default) { + var fs = fileSystem ?? _sourceFileSystem; + if (!TryLoadSkipped (fs, out var skipped)) + return Task.FromResult (FileWriteResult.Failure (skipped!)); + var data = Render (); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.WriteAsync (path, data.Memory, fileSystem, cancellationToken); + return AtomicFileWriter.WriteAsync (path, data.Memory, fs, cancellationToken); } /// diff --git a/src/TagLibSharp2/Ape/ApeTagTail.cs b/src/TagLibSharp2/Ape/ApeTagTail.cs new file mode 100644 index 0000000..8e0de31 --- /dev/null +++ b/src/TagLibSharp2/Ape/ApeTagTail.cs @@ -0,0 +1,36 @@ +// Copyright (c) 2025-2026 Stephen Shaw and contributors +// Licensed under the MIT License. See LICENSE file in the project root for full license information. + +using TagLibSharp2.Core; + +namespace TagLibSharp2.Ape; + +/// +/// Reads an APEv2 tag from the end of a seekable stream without the bytes in front of it. +/// +internal static class ApeTagTail +{ + public static bool TryRead (Stream stream, out byte[] tag, out long offset) + { + tag = []; + offset = stream.Length; + if (stream.Length < ApeTagFooter.Size) + return false; + + stream.Position = stream.Length - ApeTagFooter.Size; + var footerBytes = StreamBytes.ReadAtMost (stream, ApeTagFooter.Size); + var parsed = ApeTagFooter.Parse (footerBytes); + if (!parsed.IsSuccess || parsed.Footer is not { } footer || footer.IsHeader) + return false; + + var header = (footer.Flags & 0x80000000) != 0 ? ApeTagFooter.Size : 0; + var total = (long)footer.TagSize + header; + if (total < ApeTagFooter.Size || total > stream.Length || total > int.MaxValue) + return false; + + offset = stream.Length - total; + stream.Position = offset; + tag = StreamBytes.ReadAtMost (stream, (int)total); + return tag.Length == total; + } +} diff --git a/src/TagLibSharp2/Ape/MonkeysAudioFile.cs b/src/TagLibSharp2/Ape/MonkeysAudioFile.cs index f240452..cc81ee1 100644 --- a/src/TagLibSharp2/Ape/MonkeysAudioFile.cs +++ b/src/TagLibSharp2/Ape/MonkeysAudioFile.cs @@ -100,6 +100,7 @@ public sealed class MonkeysAudioFile : IMediaFile private const int MinNewFormatSize = 4 + DescriptorSize + HeaderSize; private byte[] _originalData = Array.Empty (); + private long _fileLength; /// /// Gets the source file path if the file was read from disk. @@ -204,6 +205,89 @@ public static MonkeysAudioFileReadResult Read (ReadOnlySpan data) return MonkeysAudioFileReadResult.Success (file); } + /// + /// Reads the descriptor and header, then the trailing APE tag. Audio bytes stay on disk. + /// + internal static MonkeysAudioFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return Finish (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + + stream.Position = 0; + var prefix = StreamBytes.ReadAtMost (stream, 16); + if (prefix.Length < MagicSize + 2) + return Finish (Read (prefix), sourcePath, fileSystem, keepPayload: true); + + var version = BinaryPrimitives.ReadUInt16LittleEndian (prefix.AsSpan (MagicSize, 2)); + int need; + if (version >= NewFormatVersion) { + if (prefix.Length < 16) + return MonkeysAudioFileReadResult.Failure ("Invalid Monkey's Audio file: truncated descriptor"); + var descriptor = BinaryPrimitives.ReadUInt32LittleEndian (prefix.AsSpan (8, 4)); + need = (int)descriptor + HeaderSize; + if (need < MinNewFormatSize) + need = MinNewFormatSize; + } else { + need = MinOldFormatSize; + } + + if (need > 1024 * 1024) + return MonkeysAudioFileReadResult.Failure ("Invalid Monkey's Audio file: header is too large"); + + byte[] header; + if (need <= prefix.Length) { + header = prefix.AsSpan (0, need).ToArray (); + } else { + var rest = StreamBytes.ReadAtMost (stream, need - prefix.Length); + header = new byte[prefix.Length + rest.Length]; + prefix.CopyTo (header, 0); + rest.CopyTo (header, prefix.Length); + } + + ApeTagTail.TryRead (stream, out var tag, out var tagAt); + if (tagAt < header.Length) + return Finish (Read (ReadWhole (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + + return Finish (Read (Join (header, tag)), sourcePath, fileSystem, keepPayload: false, fileLength: stream.Length); + } + + static byte[] ReadWhole (Stream stream) + { + stream.Position = 0; + return StreamBytes.ReadToEnd (stream); + } + + static byte[] Join (byte[] left, byte[] right) + { + if (right.Length == 0) + return left; + var combined = new byte[left.Length + right.Length]; + left.CopyTo (combined, 0); + right.CopyTo (combined, left.Length); + return combined; + } + + static MonkeysAudioFileReadResult Finish ( + MonkeysAudioFileReadResult result, + string? sourcePath, + IFileSystem? fileSystem, + bool keepPayload, + long? fileLength = null) + { + if (!result.IsSuccess || result.File is null) + return result; + + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + if (!keepPayload) { + result.File._fileLength = fileLength ?? 0; + result.File._originalData = []; + result.File.CalculateProperties (); + } + + return result; + } + /// /// Attempts to read a Monkey's Audio file from binary data. /// @@ -378,9 +462,9 @@ private void CalculateProperties () // Calculate bitrate from file size if we have original data var bitrate = 0; - if (_originalData.Length > 0 && durationSeconds > 0) { - bitrate = (int)(_originalData.Length * 8 / durationSeconds / 1000); - } + var length = _fileLength > 0 ? _fileLength : _originalData.Length; + if (length > 0 && durationSeconds > 0) + bitrate = (int)(length * 8 / durationSeconds / 1000); // AudioProperties: duration, bitrate, sampleRate, bitsPerSample, channels, codec Properties = new AudioProperties ( @@ -487,8 +571,19 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, /// /// Save the file to a new path using internally stored data. /// - public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) => - SaveToFile (path, _originalData, fileSystem); + public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return SaveToFile (path, _originalData, fs); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return SaveToFile (path, read.Data!.Value.Span, fs); + } /// /// Save the file back to its source path. @@ -523,11 +618,22 @@ public async Task SaveToFileAsync ( /// /// Save the file asynchronously using internally stored data. /// - public Task SaveToFileAsync ( + public async Task SaveToFileAsync ( string path, IFileSystem? fileSystem = null, - CancellationToken cancellationToken = default) => - SaveToFileAsync (path, _originalData, fileSystem, cancellationToken); + CancellationToken cancellationToken = default) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return await SaveToFileAsync (path, _originalData, fs, cancellationToken).ConfigureAwait (false); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return await SaveToFileAsync (path, read.Data!.Value, fs, cancellationToken).ConfigureAwait (false); + } /// /// Save the file back to its source path asynchronously. diff --git a/src/TagLibSharp2/Asf/AsfFile.cs b/src/TagLibSharp2/Asf/AsfFile.cs index 98daf8e..94fed0e 100644 --- a/src/TagLibSharp2/Asf/AsfFile.cs +++ b/src/TagLibSharp2/Asf/AsfFile.cs @@ -189,6 +189,43 @@ public static AsfFileReadResult Read (ReadOnlySpan data) return AsfFileReadResult.Success (file); } + /// + /// Reads the header object. The data object that follows is not copied. + /// + internal static AsfFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return Finish (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem, dropPayload: sourcePath is not null); + + stream.Position = 0; + var head = StreamBytes.ReadAtMost (stream, 30); + if (head.Length < 30) + return Finish (Read (head), sourcePath, fileSystem, dropPayload: false); + + var headerSize = BinaryPrimitives.ReadUInt64LittleEndian (head.AsSpan (16)); + if (headerSize < 30 || headerSize > int.MaxValue) + return AsfFileReadResult.Failure ("Invalid ASF file: header size is out of range"); + + var rest = StreamBytes.ReadAtMost (stream, (int)headerSize - head.Length); + var header = new byte[head.Length + rest.Length]; + head.CopyTo (header, 0); + rest.CopyTo (header, head.Length); + var coversFile = headerSize >= (ulong)stream.Length; + return Finish (Read (header), sourcePath, fileSystem, dropPayload: !coversFile); + } + + static AsfFileReadResult Finish (AsfFileReadResult result, string? sourcePath, IFileSystem? fileSystem, bool dropPayload) + { + if (!result.IsSuccess || result.File is null) + return result; + + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + if (dropPayload) + result.File._originalData = []; + return result; + } + /// /// Attempts to read an ASF file from binary data. /// @@ -487,8 +524,19 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, /// The file path to save to. /// Optional file system abstraction. /// The result of the write operation. - public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) => - SaveToFile (path, _originalData, fileSystem); + public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return SaveToFile (path, _originalData, fs); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. Use SaveToFile(path) instead."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return SaveToFile (path, read.Data!.Value.Span, fs); + } /// /// Saves the file back to its source path. @@ -529,11 +577,22 @@ public async Task SaveToFileAsync ( /// Optional file system abstraction. /// Cancellation token. /// The result of the write operation. - public Task SaveToFileAsync ( + public async Task SaveToFileAsync ( string path, IFileSystem? fileSystem = null, - CancellationToken cancellationToken = default) => - SaveToFileAsync (path, _originalData, fileSystem, cancellationToken); + CancellationToken cancellationToken = default) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return await SaveToFileAsync (path, _originalData, fs, cancellationToken).ConfigureAwait (false); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. Use SaveToFileAsync(path) instead."); + + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return await SaveToFileAsync (path, read.Data!.Value, fs, cancellationToken).ConfigureAwait (false); + } /// /// Saves the file back to its source path asynchronously. diff --git a/src/TagLibSharp2/Core/MediaFile.cs b/src/TagLibSharp2/Core/MediaFile.cs index d0f31df..af46987 100644 --- a/src/TagLibSharp2/Core/MediaFile.cs +++ b/src/TagLibSharp2/Core/MediaFile.cs @@ -149,12 +149,13 @@ public static MediaFileResult Read (string path, IFileSystem? fileSystem = null) #endif var fs = fileSystem ?? DefaultFileSystem.Instance; - if (!fs.FileExists (path)) - return MediaFileResult.Failure ($"File not found: {path}"); - try { using var stream = fs.OpenRead (path); return Read (stream, path, path, fs); + } catch (FileNotFoundException) { + return MediaFileResult.Failure ($"File not found: {path}"); + } catch (DirectoryNotFoundException) { + return MediaFileResult.Failure ($"File not found: {path}"); } catch (IOException ex) { return MediaFileResult.Failure ($"Failed to read file: {ex.Message}"); } catch (UnauthorizedAccessException ex) { @@ -169,8 +170,8 @@ public static MediaFileResult Read (string path, IFileSystem? fileSystem = null) /// Optional file name used when the bytes do not identify a format. /// A result containing the parsed file or an error. /// - /// A seekable stream is read by the ranges the format needs. MP3, FLAC, and WAV - /// do not read the audio payload. A stream that cannot seek is copied once. + /// A seekable stream is read by the ranges the format needs and does not read the + /// audio or video payload. A stream that cannot seek is copied once. /// public static MediaFileResult Read (Stream stream, string? pathHint = null) { @@ -207,12 +208,13 @@ public static async Task ReadAsync ( #endif var fs = fileSystem ?? DefaultFileSystem.Instance; - if (!fs.FileExists (path)) - return MediaFileResult.Failure ($"File not found: {path}"); - try { using var stream = fs.OpenRead (path); return await ReadAsync (stream, path, path, fs, cancellationToken).ConfigureAwait (false); + } catch (FileNotFoundException) { + return MediaFileResult.Failure ($"File not found: {path}"); + } catch (DirectoryNotFoundException) { + return MediaFileResult.Failure ($"File not found: {path}"); } catch (IOException ex) { return MediaFileResult.Failure ($"Failed to read file: {ex.Message}"); } catch (UnauthorizedAccessException ex) { @@ -230,8 +232,8 @@ public static async Task ReadAsync ( /// Cancellation token. /// A result containing the parsed file or an error. /// - /// A seekable stream is read by the ranges the format needs. MP3, FLAC, and WAV - /// do not read the audio payload. A stream that cannot seek is copied once. + /// A seekable stream is read by the ranges the format needs and does not read the + /// audio or video payload. A stream that cannot seek is copied once. /// public static Task ReadAsync ( Stream stream, @@ -265,7 +267,7 @@ static MediaFileResult Read (Stream stream, string? pathHint, string? sourcePath if (stream.Position != 0) stream.Position = 0; - var peek = StreamBytes.ReadAtMost (stream, 4096); + var peek = ReadPeek (stream, pathHint); stream.Position = 0; return RememberMatroskaSource ( OpenDetected (DetectFormat (peek, pathHint), stream, sourcePath, fileSystem, pathHint), @@ -273,6 +275,41 @@ static MediaFileResult Read (Stream stream, string? pathHint, string? sourcePath fileSystem); } + // A read smaller than FileStream's 4096-byte buffer is cached, so the parser + // reuses it. Ogg and unrecognized files still get a longer prefix. + const int ShortPeekBytes = 32; + const int LongPeekBytes = 4095; + + static byte[] ReadPeek (Stream stream, string? pathHint) + { + var peek = StreamBytes.ReadAtMost (stream, ShortPeekBytes); + if (!NeedsLongerPeek (peek, pathHint)) + return peek; + + stream.Position = 0; + return StreamBytes.ReadAtMost (stream, LongPeekBytes); + } + + static async Task ReadPeekAsync (Stream stream, string? pathHint, CancellationToken cancellationToken) + { + var peek = await StreamBytes.ReadAtMostAsync (stream, ShortPeekBytes, cancellationToken).ConfigureAwait (false); + if (!NeedsLongerPeek (peek, pathHint)) + return peek; + + stream.Position = 0; + return await StreamBytes.ReadAtMostAsync (stream, LongPeekBytes, cancellationToken).ConfigureAwait (false); + } + + static bool NeedsLongerPeek (ReadOnlySpan peek, string? pathHint) => + IsOgg (peek) || DetectFormat (peek, pathHint) == MediaFormat.Unknown; + + static bool IsOgg (ReadOnlySpan peek) => + peek.Length >= OggMagic.Length + && peek[0] == OggMagic[0] + && peek[1] == OggMagic[1] + && peek[2] == OggMagic[2] + && peek[3] == OggMagic[3]; + static async Task ReadAsync ( Stream stream, string? pathHint, @@ -290,14 +327,14 @@ static async Task ReadAsync ( if (stream.Position != 0) stream.Position = 0; - var peek = await StreamBytes.ReadAtMostAsync (stream, 4096, cancellationToken).ConfigureAwait (false); + var peek = await ReadPeekAsync (stream, pathHint, cancellationToken).ConfigureAwait (false); stream.Position = 0; var format = DetectFormat (peek, pathHint); var result = format switch { MediaFormat.Mp3 => FromMp3 (await Mp3File.ReadAsync (stream, cancellationToken).ConfigureAwait (false)), MediaFormat.Flac => FromFlac (await FlacFile.ReadAsync (stream, cancellationToken).ConfigureAwait (false)), MediaFormat.Wav => FromWav (WavFile.Read (stream, sourcePath, fileSystem)), - _ => ReadFromData (await StreamBytes.ReadToEndAsync (stream, cancellationToken).ConfigureAwait (false), pathHint) + _ => OpenDetected (format, stream, sourcePath, fileSystem, pathHint) }; return RememberMatroskaSource (result, sourcePath, fileSystem); } @@ -307,6 +344,18 @@ static MediaFileResult OpenDetected (MediaFormat format, Stream stream, string? MediaFormat.Mp3 => FromMp3 (Mp3File.Read (stream)), MediaFormat.Flac => FromFlac (FlacFile.Read (stream)), MediaFormat.Wav => FromWav (WavFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Mp4 => FromMp4 (Mp4File.Read (stream, sourcePath, fileSystem)), + MediaFormat.Matroska => FromMatroska (MatroskaFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.OggVorbis => FromOggVorbis (OggVorbisFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Opus => FromOpus (OggOpusFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.OggFlac => FromOggFlac (OggFlacFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Aiff => FromAiff (AiffFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.WavPack => FromWavPack (WavPackFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.MonkeysAudio => FromMonkeys (MonkeysAudioFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Musepack => FromMusepack (MusepackFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Dsf => FromDsf (DsfFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Dff => FromDff (DffFile.Read (stream, sourcePath, fileSystem)), + MediaFormat.Asf => FromAsf (AsfFile.Read (stream, sourcePath, fileSystem)), _ => ReadFromData (StreamBytes.ReadToEnd (stream), pathHint) }; @@ -332,6 +381,90 @@ static MediaFileResult FromFlac (FlacFileReadResult result) return MediaFileResult.Success (result.File, result.File.Tag, MediaFormat.Flac); } + static MediaFileResult FromMp4 (Mp4FileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse MP4 file"); + return MediaFileResult.Success (result.File, result.File.Tag, MediaFormat.Mp4); + } + + static MediaFileResult FromMatroska (MatroskaFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Matroska file"); + return MediaFileResult.Success (result.File, result.File.Tag, MediaFormat.Matroska); + } + + static MediaFileResult FromOggVorbis (OggVorbisFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Ogg Vorbis file"); + return MediaFileResult.Success (result.File, result.File.VorbisComment, MediaFormat.OggVorbis); + } + + static MediaFileResult FromOpus (OggOpusFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Opus file"); + return MediaFileResult.Success (result.File, result.File.VorbisComment, MediaFormat.Opus); + } + + static MediaFileResult FromOggFlac (OggFlacFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Ogg FLAC file"); + return MediaFileResult.Success (result.File, result.File.VorbisComment, MediaFormat.OggFlac); + } + + static MediaFileResult FromAiff (AiffFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse AIFF file"); + return MediaFileResult.Success (result.File, result.File.Tag, MediaFormat.Aiff); + } + + static MediaFileResult FromWavPack (WavPackFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse WavPack file"); + return MediaFileResult.Success (result.File, result.File.ApeTag, MediaFormat.WavPack); + } + + static MediaFileResult FromMonkeys (MonkeysAudioFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Monkey's Audio file"); + return MediaFileResult.Success (result.File, result.File.ApeTag, MediaFormat.MonkeysAudio); + } + + static MediaFileResult FromMusepack (MusepackFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse Musepack file"); + return MediaFileResult.Success (result.File, result.File.ApeTag, MediaFormat.Musepack); + } + + static MediaFileResult FromDsf (DsfFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse DSF file"); + return MediaFileResult.Success (result.File, result.File.Id3v2Tag, MediaFormat.Dsf); + } + + static MediaFileResult FromDff (DffFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse DFF file"); + return MediaFileResult.Success (result.File, result.File.Id3v2Tag, MediaFormat.Dff); + } + + static MediaFileResult FromAsf (AsfFileReadResult result) + { + if (!result.IsSuccess || result.File is null) + return MediaFileResult.Failure (result.Error ?? "Failed to parse ASF file"); + return MediaFileResult.Success (result.File, result.File.Tag, MediaFormat.Asf); + } + static MediaFileResult FromWav (WavFileReadResult result) { if (!result.IsSuccess || result.File is null) diff --git a/src/TagLibSharp2/Core/StreamBytes.cs b/src/TagLibSharp2/Core/StreamBytes.cs index 3b0e30d..b2986e9 100644 --- a/src/TagLibSharp2/Core/StreamBytes.cs +++ b/src/TagLibSharp2/Core/StreamBytes.cs @@ -8,6 +8,13 @@ namespace TagLibSharp2.Core; /// internal static class StreamBytes { + /// + /// Writes with the overload. + /// has no span write on netstandard2.0. + /// + public static void Append (MemoryStream stream, byte[] data) => + stream.Write (data, 0, data.Length); + public static byte[] ReadAtMost (Stream stream, int count) { if (count <= 0) @@ -60,7 +67,7 @@ public static async Task ReadToEndAsync (Stream stream, CancellationToke return copy.ToArray (); } - static int Read (Stream stream, byte[] buffer, int offset, int count) + internal static int Read (Stream stream, byte[] buffer, int offset, int count) { var read = 0; while (read < count) { @@ -73,7 +80,7 @@ static int Read (Stream stream, byte[] buffer, int offset, int count) return read; } - static async Task ReadAsync (Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken) + internal static async Task ReadAsync (Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { var read = 0; while (read < count) { @@ -90,6 +97,23 @@ static async Task ReadAsync (Stream stream, byte[] buffer, int offset, int return read; } + /// + /// Moves forward without copying. A seekable stream jumps. Anything else is not skipped. + /// + public static void Skip (Stream stream, long count) + { + if (count <= 0) + return; + + if (!stream.CanSeek) + return; + + var room = stream.Length - stream.Position; + if (count > room) + count = room < 0 ? 0 : room; + stream.Position += count; + } + static byte[] Trim (byte[] buffer, int length) { if (length == 0) diff --git a/src/TagLibSharp2/Dff/DffFile.cs b/src/TagLibSharp2/Dff/DffFile.cs index 64a92e7..6e9f2d8 100644 --- a/src/TagLibSharp2/Dff/DffFile.cs +++ b/src/TagLibSharp2/Dff/DffFile.cs @@ -363,6 +363,97 @@ public static DffFileReadResult Read (ReadOnlySpan data) return DffFileReadResult.Success (file); } + /// + /// Reads metadata chunks and records audio chunk sizes. DSD and DST payloads are not copied. + /// + internal static DffFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) { + var copied = Read (StreamBytes.ReadToEnd (stream)); + if (sourcePath is not null && copied.IsSuccess && copied.File is not null) + copied.File._originalData = null; + return WithSource (copied, sourcePath, fileSystem); + } + + if (stream.Position != 0) + stream.Position = 0; + + var head = StreamBytes.ReadAtMost (stream, 16); + if (head.Length < 16) + return DffFileReadResult.Failure ($"Data too short for DFF file: {stream.Length} bytes"); + + using var kept = new MemoryStream (); + StreamBytes.Append (kept, head); + using var audioHeaders = new MemoryStream (); + long pos = 16; + var length = stream.Length; + while (pos + 12 <= length) { + stream.Position = pos; + var chunkHeader = StreamBytes.ReadAtMost (stream, 12); + if (chunkHeader.Length < 12) + break; + + var id = System.Text.Encoding.ASCII.GetString (chunkHeader, 0, 4); + var size = BinaryPrimitives.ReadUInt64BigEndian (chunkHeader.AsSpan (4)); + var dataEnd = pos + 12 + (long)size; + var pad = (size & 1) == 1 ? 1 : 0; + if (id is "DSD " or "DST ") { + StreamBytes.Append (audioHeaders, chunkHeader); + if (dataEnd < pos) + break; + pos = dataEnd + pad; + if (pos > length) + pos = length; + continue; + } + + var chunkEnd = dataEnd + pad; + if (chunkEnd < pos || dataEnd > length) { + var rest = (int)Math.Min (length - pos, int.MaxValue); + stream.Position = pos; + StreamBytes.Append (kept, StreamBytes.ReadAtMost (stream, rest)); + break; + } + + var chunkLen = chunkEnd - pos; + if (chunkLen > int.MaxValue) + break; + stream.Position = pos; + StreamBytes.Append (kept, StreamBytes.ReadAtMost (stream, (int)chunkLen)); + pos = chunkEnd; + } + + var audio = audioHeaders.ToArray (); + if (audio.Length > 0) + StreamBytes.Append (kept, audio); + + var result = Read (kept.ToArray ()); + if (result.IsSuccess && result.File is not null) + result.File._originalData = null; + return WithSource (result, sourcePath, fileSystem); + } + + static DffFileReadResult WithSource (DffFileReadResult result, string? sourcePath, IFileSystem? fileSystem) + { + if (result.IsSuccess && result.File is not null) { + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + } + + return result; + } + + BinaryData RenderFrom (byte[] original) + { + var previous = _originalData; + _originalData = original; + try { + return Render (); + } finally { + _originalData = previous; + } + } + /// /// Attempts to read a DFF file from binary data. /// @@ -584,10 +675,10 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, if (_disposed) throw new ObjectDisposedException (nameof (DffFile)); + var fs = fileSystem ?? _sourceFileSystem; try { - var rendered = Render (); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.Write (path, rendered.Span, fileSystem); + var rendered = _originalData is null ? RenderFrom (originalData.ToArray ()) : Render (); + return AtomicFileWriter.Write (path, rendered.Span, fs); } catch (Exception ex) { return FileWriteResult.Failure ($"Failed to save file: {ex.Message}"); } @@ -619,16 +710,23 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (_disposed) throw new ObjectDisposedException (nameof (DffFile)); - if (_originalData is null) - return FileWriteResult.Failure ("No original data available"); - - try { - var rendered = Render (); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.Write (path, rendered.Span, fileSystem); - } catch (Exception ex) { - return FileWriteResult.Failure ($"Failed to save file: {ex.Message}"); + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData is not null) { + try { + var rendered = Render (); + return AtomicFileWriter.Write (path, rendered.Span, fs); + } catch (Exception ex) { + return FileWriteResult.Failure ($"Failed to save file: {ex.Message}"); + } } + + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return SaveToFile (path, read.Data!.Value.Span, fs); } /// @@ -687,11 +785,21 @@ private async Task SaveToFileAsyncCore ( IFileSystem? fileSystem, CancellationToken cancellationToken) { + var fs = fileSystem ?? _sourceFileSystem; try { - var rendered = Render (); - fileSystem ??= _sourceFileSystem; - return await AtomicFileWriter.WriteAsync (path, rendered.Memory, fileSystem, cancellationToken) - .ConfigureAwait (false); + BinaryData rendered; + if (_originalData is not null) { + rendered = Render (); + } else { + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + rendered = RenderFrom (read.Data!.Value.ToArray ()); + } + + return await AtomicFileWriter.WriteAsync (path, rendered.Memory, fs, cancellationToken).ConfigureAwait (false); } catch (Exception ex) { return FileWriteResult.Failure ($"Failed to save file: {ex.Message}"); } diff --git a/src/TagLibSharp2/Dsf/DsfFile.cs b/src/TagLibSharp2/Dsf/DsfFile.cs index 4f7fa0e..74e6922 100644 --- a/src/TagLibSharp2/Dsf/DsfFile.cs +++ b/src/TagLibSharp2/Dsf/DsfFile.cs @@ -224,7 +224,7 @@ public sealed class DsfFile : IMediaFile public MediaFormat Format => MediaFormat.Dsf; private DsfFile ( - byte[] originalData, + byte[]? originalData, DsfDsdChunk dsdChunk, DsfFmtChunk fmtChunk, DsfDataChunk dataChunk, @@ -322,6 +322,80 @@ public static DsfFileReadResult Read (ReadOnlySpan data) return DsfFileReadResult.Success (file); } + /// + /// Reads the DSD, fmt, and data headers, then the ID3 tag. Sample bytes stay on disk. + /// + internal static DsfFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) { + var copied = Read (StreamBytes.ReadToEnd (stream)); + if (sourcePath is not null && copied.IsSuccess && copied.File is not null) + copied.File._originalData = null; + return WithSource (copied, sourcePath, fileSystem); + } + + stream.Position = 0; + var headerBytes = DsfDsdChunk.Size + DsfFmtChunk.Size + DsfDataChunk.HeaderSize; + var headers = StreamBytes.ReadAtMost (stream, headerBytes); + if (headers.Length < headerBytes) + return DsfFileReadResult.Failure ($"Data too short for DSF file: {stream.Length} bytes, need at least {headerBytes}"); + + var dsdResult = DsfDsdChunk.Parse (headers); + if (!dsdResult.IsSuccess) + return DsfFileReadResult.Failure ($"Failed to parse DSD chunk: {dsdResult.Error}"); + var dsdChunk = dsdResult.Chunk!; + + var fmtResult = DsfFmtChunk.Parse (headers.AsSpan (DsfDsdChunk.Size)); + if (!fmtResult.IsSuccess) + return DsfFileReadResult.Failure ($"Failed to parse format chunk: {fmtResult.Error}"); + var fmtChunk = fmtResult.Chunk!; + + var dataOffset = DsfDsdChunk.Size + DsfFmtChunk.Size; + var dataResult = DsfDataChunk.Parse (headers.AsSpan (dataOffset)); + if (!dataResult.IsSuccess) + return DsfFileReadResult.Failure ($"Failed to parse data chunk: {dataResult.Error}"); + var dataChunk = dataResult.Chunk!; + var audioDataOffset = dataOffset + DsfDataChunk.HeaderSize; + var dataEnd = (ulong)audioDataOffset + dataChunk.AudioDataSize; + if (dsdChunk.HasMetadata) { + if (dataEnd > dsdChunk.MetadataOffset) + return DsfFileReadResult.Failure ( + $"Data chunk extends beyond metadata offset: data ends at {dataEnd}, metadata at {dsdChunk.MetadataOffset}"); + if (dsdChunk.MetadataOffset > (ulong)stream.Length) + return DsfFileReadResult.Failure ( + $"Metadata offset {dsdChunk.MetadataOffset} beyond file size {stream.Length}"); + } else if (dataEnd > (ulong)stream.Length) { + return DsfFileReadResult.Failure ( + $"Data chunk size exceeds available data: claims {dataChunk.AudioDataSize} bytes, but only {stream.Length - audioDataOffset} available"); + } + + Id3v2Tag? tag = null; + if (dsdChunk.HasMetadata) { + stream.Position = (long)dsdChunk.MetadataOffset; + var id3 = StreamBytes.ReadToEnd (stream); + var id3Result = Id3v2Tag.Read (id3); + if (id3Result.IsSuccess) + tag = id3Result.Tag; + } + + var parsed = new DsfFile (null, dsdChunk, fmtChunk, dataChunk, audioDataOffset, tag) { + Properties = new DsfAudioProperties (fmtChunk), + SourcePath = sourcePath, + _sourceFileSystem = fileSystem + }; + return DsfFileReadResult.Success (parsed); + } + + static DsfFileReadResult WithSource (DsfFileReadResult result, string? sourcePath, IFileSystem? fileSystem) + { + if (result.IsSuccess && result.File is not null) { + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + } + + return result; + } + /// /// Attempts to read a DSF file from binary data. /// @@ -490,12 +564,25 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, if (_disposed) throw new ObjectDisposedException (nameof (DsfFile)); - var rendered = Render (); + var fs = fileSystem ?? _sourceFileSystem; + var rendered = RenderWith (originalData); if (rendered.IsEmpty) return FileWriteResult.Failure ("Failed to render DSF file"); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.Write (path, rendered.Span, fileSystem); + return AtomicFileWriter.Write (path, rendered.Span, fs); + } + + BinaryData RenderWith (ReadOnlySpan originalData) + { + if (_originalData is not null) + return Render (); + + _originalData = originalData.ToArray (); + try { + return Render (); + } finally { + _originalData = null; + } } /// @@ -517,12 +604,12 @@ public Task SaveToFileAsync ( cancellationToken.ThrowIfCancellationRequested (); - var rendered = Render (); + var fs = fileSystem ?? _sourceFileSystem; + var rendered = RenderWith (originalData.Span); if (rendered.IsEmpty) return Task.FromResult (FileWriteResult.Failure ("Failed to render DSF file")); - fileSystem ??= _sourceFileSystem; - return AtomicFileWriter.WriteAsync (path, rendered.Memory, fileSystem, cancellationToken); + return AtomicFileWriter.WriteAsync (path, rendered.Memory, fs, cancellationToken); } /// @@ -544,7 +631,7 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return SaveToFile (path, readResult.Data!.Value.Span, fileSystem); + return SaveToFile (path, readResult.Data!.Value.Span, fs); } /// @@ -586,7 +673,7 @@ public async Task SaveToFileAsync ( if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return await SaveToFileAsync (path, readResult.Data!.Value, fileSystem, cancellationToken).ConfigureAwait (false); + return await SaveToFileAsync (path, readResult.Data!.Value, fs, cancellationToken).ConfigureAwait (false); } /// diff --git a/src/TagLibSharp2/Matroska/EbmlReader.cs b/src/TagLibSharp2/Matroska/EbmlReader.cs index 1b4e4bf..69550c8 100644 --- a/src/TagLibSharp2/Matroska/EbmlReader.cs +++ b/src/TagLibSharp2/Matroska/EbmlReader.cs @@ -38,6 +38,26 @@ public EbmlElement (ulong id, int position, int headerLength, int dataOffset, lo /// internal static class EbmlReader { + /// + /// Reads an element header. The payload does not have to be in . + /// is -1 when the size is unknown. + /// + public static bool TryPeekHeader (ReadOnlySpan data, out ulong id, out int headerLength, out long dataLength) + { + id = 0; + headerLength = 0; + dataLength = -1; + if (!TryReadVint (data, 0, data.Length, keepMarker: true, maxWidth: 4, out id, out var idWidth, out _)) + return false; + if (!TryReadVint (data, idWidth, data.Length, keepMarker: false, maxWidth: 8, out var size, out var sizeWidth, out var unknown)) + return false; + + headerLength = idWidth + sizeWidth; + if (!unknown) + dataLength = (long)size; + return true; + } + public static bool TryRead (ReadOnlySpan data, int offset, int limit, out EbmlElement element) { element = default; diff --git a/src/TagLibSharp2/Matroska/MatroskaFile.cs b/src/TagLibSharp2/Matroska/MatroskaFile.cs index 283c40e..0b9e4b9 100644 --- a/src/TagLibSharp2/Matroska/MatroskaFile.cs +++ b/src/TagLibSharp2/Matroska/MatroskaFile.cs @@ -195,6 +195,156 @@ public static MatroskaFileReadResult Read (ReadOnlySpan data) return MatroskaFileReadResult.Success (new MatroskaFile (tag, duration, layout)); } + /// + /// Reads tags and duration from a stream. Cluster payloads stay on disk. + /// An unknown-size element ends at the next segment child, the same way a byte read does. + /// The bytes in between are not copied. + /// + internal static MatroskaFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return WithSource (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem); + + if (stream.Position != 0) + stream.Position = 0; + + using var kept = new MemoryStream (); + if (!TryCollect (stream, kept, out var error)) + return MatroskaFileReadResult.Failure (error); + + return WithSource (Read (kept.GetBuffer ().AsSpan (0, (int)kept.Length)), sourcePath, fileSystem); + } + + static MatroskaFileReadResult WithSource (MatroskaFileReadResult result, string? sourcePath, IFileSystem? fileSystem) + { + if (result.IsSuccess && result.File is not null) + result.File.SetSourcePath (sourcePath, fileSystem); + return result; + } + + static bool TryCollect (Stream stream, MemoryStream kept, out string error) + { + error = ""; + var length = stream.Length; + long pos = 0; + while (pos + 2 <= length) { + if (!TryPeek (stream, pos, out var id, out var headerLength, out var dataLength)) + break; + + var known = dataLength >= 0; + if (id == MatroskaIds.Segment) { + StreamBytes.Append (kept, new byte[] { 0x18, 0x53, 0x80, 0x67, 0xFF }); + var childEnd = known ? pos + headerLength + dataLength : length; + if (childEnd > length) + childEnd = length; + CollectChildren (stream, pos + headerLength, childEnd, kept); + return true; + } + + if (!known) { + error = "Matroska element size is unknown before Segment"; + return false; + } + + var end = pos + headerLength + dataLength; + if (end > length || end < pos) + break; + if (!Copy (stream, pos, end, kept)) + break; + pos = end; + } + + error = "Matroska file has no Segment"; + return false; + } + + static void CollectChildren (Stream stream, long start, long end, MemoryStream kept) + { + var pos = start; + while (pos + 2 <= end) { + if (!TryPeek (stream, pos, out var id, out var headerLength, out var dataLength)) + break; + if (id == MatroskaIds.Cluster) { + // Known clusters are seeked past. An unknown-size cluster ends at the next + // segment child, matching the byte reader, and its payload is not copied. + var clusterEnd = dataLength < 0 + ? NextSegmentChild (stream, pos + headerLength, end) + : pos + headerLength + dataLength; + if (clusterEnd > end || clusterEnd <= pos) + break; + pos = clusterEnd; + continue; + } + + if (dataLength < 0) { + var bounded = NextSegmentChild (stream, pos + headerLength, end); + if (bounded > end || bounded <= pos) + break; + if (!Copy (stream, pos, bounded, kept)) + break; + pos = bounded; + continue; + } + + var childEnd = pos + headerLength + dataLength; + if (childEnd > end || childEnd < pos) + break; + if (!Copy (stream, pos, childEnd, kept)) + break; + pos = childEnd; + } + } + + static bool TryPeek (Stream stream, long pos, out ulong id, out int headerLength, out long dataLength) + { + id = 0; + headerLength = 0; + dataLength = -1; + if (pos < 0 || pos >= stream.Length) + return false; + + stream.Position = pos; + var probe = StreamBytes.ReadAtMost (stream, 12); + return EbmlReader.TryPeekHeader (probe, out id, out headerLength, out dataLength); + } + + /// + /// The next segment-level element at or after . + /// Matches : a payload that is not an element ends the walk. + /// + static long NextSegmentChild (Stream stream, long start, long end) + { + var cursor = start; + while (cursor + 2 <= end) { + if (!TryPeek (stream, cursor, out var id, out var headerLength, out var dataLength)) + return end; + if (MatroskaIds.IsSegmentChild (id)) + return cursor; + if (dataLength < 0) + return end; + var childEnd = cursor + headerLength + dataLength; + if (childEnd > end || childEnd <= cursor) + return end; + cursor = childEnd; + } + + return end; + } + + static bool Copy (Stream stream, long start, long end, MemoryStream kept) + { + var length = end - start; + if (length < 0 || length > int.MaxValue) + return false; + + stream.Position = start; + var bytes = StreamBytes.ReadAtMost (stream, (int)length); + if (bytes.Length != length) + return false; + StreamBytes.Append (kept, bytes); + return true; + } + /// Reads a file without an error string. public static bool TryRead (ReadOnlySpan data, out MatroskaFile? file) { @@ -231,7 +381,7 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (!readResult.IsSuccess) return FileWriteResult.Failure ("Failed to re-read source file: " + readResult.Error); - return SaveToFile (path, readResult.Data!.Value.Span, fileSystem); + return SaveToFile (path, readResult.Data!.Value.Span, fs); } /// Saves tags back to . @@ -271,7 +421,7 @@ public async Task SaveToFileAsync ( if (!readResult.IsSuccess) return FileWriteResult.Failure ("Failed to re-read source file: " + readResult.Error); - return await SaveToFileAsync (path, readResult.Data!.Value, fileSystem, cancellationToken).ConfigureAwait (false); + return await SaveToFileAsync (path, readResult.Data!.Value, fs, cancellationToken).ConfigureAwait (false); } /// Saves tags back to . diff --git a/src/TagLibSharp2/Mp4/Mp4File.cs b/src/TagLibSharp2/Mp4/Mp4File.cs index 1d242f3..8b00b71 100644 --- a/src/TagLibSharp2/Mp4/Mp4File.cs +++ b/src/TagLibSharp2/Mp4/Mp4File.cs @@ -39,7 +39,7 @@ public sealed class Mp4File : IMediaFile /// /// /// This is set when using or . - /// It is null when the file was read from binary data using . + /// It is null when the file was read from binary data using . /// public string? SourcePath { get; private set; } @@ -679,15 +679,23 @@ public static Mp4FileReadResult Read (ReadOnlySpan data) if (data.Length < Mp4Box.HeaderSize) return Mp4FileReadResult.Failure ("Invalid MP4 file: data too short"); - // Parse all top-level boxes + // Parse top-level boxes. mdat is the media payload: keep its size, not its bytes. var boxes = new List (); var offset = 0; while (offset < data.Length) { - var remaining = data[offset..]; - if (remaining.Length < Mp4Box.HeaderSize) + var remaining = data.Length - offset; + if (remaining < Mp4Box.HeaderSize) break; + if (!TryMeasure (data.Slice (offset, remaining), out var type, out var boxSize, out _)) + break; + if (boxSize > remaining) + break; + if (type == "mdat") { + offset += (int)boxSize; + continue; + } - var result = Mp4Box.Parse (remaining); + var result = Mp4Box.Parse (data[offset..]); if (!result.IsSuccess) break; @@ -1088,6 +1096,118 @@ static void ParseFreeformAtom (Mp4Box freeformBox, Mp4Tag tag) } } + /// + /// Reads an MP4 file from a stream. mdat payloads are not copied. + /// + internal static Mp4FileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return WithSource (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem); + + if (stream.Position != 0) + stream.Position = 0; + + using var kept = new MemoryStream (); + while (stream.Position + Mp4Box.HeaderSize <= stream.Length) { + var start = stream.Position; + var probe = StreamBytes.ReadAtMost (stream, Mp4Box.ExtendedHeaderSize); + if (!TryMeasureStream (probe, start, stream.Length, out var type, out var boxEnd)) + break; + if (boxEnd < start) + break; + + var end = boxEnd > stream.Length ? stream.Length : boxEnd; + if (type == "mdat") { + stream.Position = end; + continue; + } + + var length = end - start; + if (length > int.MaxValue) + break; + + stream.Position = start; + var box = StreamBytes.ReadAtMost (stream, (int)length); + if (box.Length == 0) + break; + StreamBytes.Append (kept, box); + if (box.Length < length) + break; + } + + return WithSource (Read (kept.ToArray ()), sourcePath, fileSystem); + } + + static Mp4FileReadResult WithSource (Mp4FileReadResult result, string? sourcePath, IFileSystem? fileSystem) + { + if (result.IsSuccess && result.File is not null) { + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + } + + return result; + } + + static bool TryMeasure (ReadOnlySpan data, out string type, out long boxSize, out int headerSize) + { + type = ""; + boxSize = 0; + headerSize = 0; + if (data.Length < Mp4Box.HeaderSize) + return false; + + var size32 = BinaryPrimitives.ReadUInt32BigEndian (data); + type = FourCC (data.Slice (4, 4)); + if (size32 == 1) { + if (data.Length < Mp4Box.ExtendedHeaderSize) + return false; + boxSize = (long)BinaryPrimitives.ReadUInt64BigEndian (data.Slice (8, 8)); + headerSize = Mp4Box.ExtendedHeaderSize; + } else if (size32 == 0) { + boxSize = data.Length; + headerSize = Mp4Box.HeaderSize; + } else { + boxSize = size32; + headerSize = Mp4Box.HeaderSize; + } + + return boxSize >= headerSize; + } + + static bool TryMeasureStream (ReadOnlySpan probe, long start, long fileLength, out string type, out long boxEnd) + { + type = ""; + boxEnd = start; + if (probe.Length < Mp4Box.HeaderSize) + return false; + + var size32 = BinaryPrimitives.ReadUInt32BigEndian (probe); + type = FourCC (probe.Slice (4, 4)); + if (size32 == 1) { + if (probe.Length < Mp4Box.ExtendedHeaderSize) + return false; + var boxSize = (long)BinaryPrimitives.ReadUInt64BigEndian (probe.Slice (8, 8)); + if (boxSize < Mp4Box.ExtendedHeaderSize) + return false; + boxEnd = start + boxSize; + return true; + } + + if (size32 == 0) { + boxEnd = fileLength; + return true; + } + + if (size32 < Mp4Box.HeaderSize) + return false; + + boxEnd = start + size32; + return true; + } + + static string FourCC (ReadOnlySpan data) => + new ([(char)data[0], (char)data[1], (char)data[2], (char)data[3]]); + /// /// Attempts to read an MP4 file from a file path. /// @@ -2182,7 +2302,7 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return SaveToFile (path, readResult.Data!.Value.Span, fileSystem); + return SaveToFile (path, readResult.Data!.Value.Span, fs); } /// @@ -2224,7 +2344,7 @@ public async Task SaveToFileAsync ( if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return await SaveToFileAsync (path, readResult.Data!.Value, fileSystem, cancellationToken) + return await SaveToFileAsync (path, readResult.Data!.Value, fs, cancellationToken) .ConfigureAwait (false); } diff --git a/src/TagLibSharp2/Mpeg/Mp3File.cs b/src/TagLibSharp2/Mpeg/Mp3File.cs index cea88c4..b304ea8 100644 --- a/src/TagLibSharp2/Mpeg/Mp3File.cs +++ b/src/TagLibSharp2/Mpeg/Mp3File.cs @@ -387,7 +387,7 @@ internal static async Task ReadAsync (Stream stream, Cancella cancellationToken.ThrowIfCancellationRequested (); switch (window.Next (stream.Position, out var count, out var seek)) { case Mp3WindowStep.ReadPrefix: - window.AddPrefix (await StreamBytes.ReadAtMostAsync (stream, count, cancellationToken).ConfigureAwait (false)); + await FillPrefixAsync (window, stream, count, cancellationToken).ConfigureAwait (false); break; case Mp3WindowStep.ReadTail: var asyncTail = await StreamBytes.ReadAtMostAsync (stream, count, cancellationToken).ConfigureAwait (false); @@ -405,6 +405,22 @@ internal static async Task ReadAsync (Stream stream, Cancella } } + static void FillPrefix (Mp3Window window, Stream stream, int count) + { + window.Prepare (count, out var buffer, out var offset); + var read = count <= 0 ? 0 : StreamBytes.Read (stream, buffer, offset, count); + window.Commit (read); + } + + static async Task FillPrefixAsync (Mp3Window window, Stream stream, int count, CancellationToken cancellationToken) + { + window.Prepare (count, out var buffer, out var offset); + var read = count <= 0 + ? 0 + : await StreamBytes.ReadAsync (stream, buffer, offset, count, cancellationToken).ConfigureAwait (false); + window.Commit (read); + } + static Mp3FileReadResult ReadWindow (Stream stream, Func read) { if (stream.Position != 0) @@ -418,7 +434,7 @@ static Mp3FileReadResult ReadWindow (Stream stream, Func re while (true) { switch (window.Next (stream.Position, out var count, out var seek)) { case Mp3WindowStep.ReadPrefix: - window.AddPrefix (read (stream, count)); + FillPrefix (window, stream, count); break; case Mp3WindowStep.ReadTail: if (TryFinishWindow (window, read (stream, count), out var parsed)) @@ -457,7 +473,8 @@ sealed class Mp3Window { const int AudioProbeBytes = 32 * 1024; - readonly List _prefix = []; + byte[] _prefix = []; + int _count; readonly long _length; int _stage; @@ -465,7 +482,43 @@ sealed class Mp3Window public long Length => _length; - public byte[] Prefix () => _prefix.ToArray (); + public byte[] Prefix () + { + if (_count == _prefix.Length) + return _prefix; + if (_count == 0) + return []; + + var exact = new byte[_count]; + Array.Copy (_prefix, exact, _count); + return exact; + } + + public void Prepare (int count, out byte[] buffer, out int offset) + { + offset = _count; + if (count <= 0) { + buffer = []; + return; + } + + var needed = offset + count; + if (_prefix.Length < needed) { + var next = new byte[needed]; + if (_count > 0) + Array.Copy (_prefix, next, _count); + _prefix = next; + } + + buffer = _prefix; + } + + public void Commit (int read) + { + if (read > 0) + _count += read; + _stage++; + } public Mp3WindowStep Next (long position, out int count, out long seek) { @@ -476,9 +529,19 @@ public Mp3WindowStep Next (long position, out int count, out long seek) count = Id3v2Header.HeaderSize; return Mp3WindowStep.ReadPrefix; case 1: - count = Id3Remainder (); - if (count > 0) + var remainder = Id3Remainder (); + var afterTag = (long)_count + remainder; + var room = _length - afterTag; + var probe = room > AudioProbeBytes ? AudioProbeBytes : (int)Math.Max (0, room); + if (remainder > 0) { + count = remainder + probe; + // The probe is contiguous with the tag, so one read fills both. + // Commit then lands on the footer instead of reading the probe again. + if (probe > 0) + _stage = 2; return Mp3WindowStep.ReadPrefix; + } + _stage = 2; goto case 2; case 2: @@ -503,22 +566,15 @@ public Mp3WindowStep Next (long position, out int count, out long seek) } } - public void AddPrefix (byte[] data) - { - if (data.Length > 0) - _prefix.AddRange (data); - _stage++; - } - public void NoteSeek () => _stage++; int Id3Remainder () { - var header = Id3v2Header.Read (_prefix.ToArray ()); + var header = Id3v2Header.Read (_prefix.AsSpan (0, _count)); if (!header.IsSuccess) return 0; var total = Math.Min ((long)header.Header.TotalSize, _length); - return (int)Math.Max (0, total - _prefix.Count); + return (int)Math.Max (0, total - _count); } } @@ -538,7 +594,7 @@ static Mp3FileReadResult ReadWindows (byte[] prefix, byte[] tail, long fileLengt file.Id3v1Tag = id3v1Result.Tag; } - if (MpegAudioProperties.TryParse (new BinaryData (prefix), file.Id3v2Size, fileLength, out var audioProps, out needsEntireFile)) + if (MpegAudioProperties.TryParse (BinaryData.WrapUnsafe (prefix), file.Id3v2Size, fileLength, out var audioProps, out needsEntireFile)) file.Properties = audioProps; else if (needsEntireFile) { file.Dispose (); diff --git a/src/TagLibSharp2/Mpeg/MpegAudioProperties.cs b/src/TagLibSharp2/Mpeg/MpegAudioProperties.cs index 82e63e6..8185848 100644 --- a/src/TagLibSharp2/Mpeg/MpegAudioProperties.cs +++ b/src/TagLibSharp2/Mpeg/MpegAudioProperties.cs @@ -116,8 +116,10 @@ public static bool TryParse (BinaryData data, int audioOffset, out MpegAudioProp /// /// Parses audio properties when is the real file /// and is only the tag plus the first audio frames. - /// is set when a Xing or VBRI byte count does - /// not fit that file, so the frame count has to be walked from the audio itself. + /// is set when a Xing or VBRI byte count runs + /// past the end of the file, so the frame count has to be walked from the audio + /// that is actually present. The Xing byte count is the stream size. LAME writes + /// the file size, which includes bytes before the first audio frame. /// internal static bool TryParse ( BinaryData data, @@ -164,11 +166,10 @@ internal static bool TryParse ( uint? frameCount = null; var audioDataSize = fileLength - audioOffset; - var bytesFromFrame = fileLength - frameOffset; var trustVbri = xingHeader is null && vbriHeader is not null - && DeclaredSizeFits (vbriHeader.ByteCount > 0 ? vbriHeader.ByteCount : null, bytesFromFrame); + && DeclaredSizeFits (vbriHeader.ByteCount > 0 ? vbriHeader.ByteCount : null, fileLength); - if (xingHeader is { FrameCount: uint xingFrames } && DeclaredSizeFits (xingHeader.ByteCount, bytesFromFrame)) { + if (xingHeader is { FrameCount: uint xingFrames } && DeclaredSizeFits (xingHeader.ByteCount, fileLength)) { // VBR with Xing header - accurate duration frameCount = xingFrames; var totalSamples = (double)xingFrames * frame.SamplesPerFrame; @@ -282,11 +283,11 @@ static bool TryReadXing (BinaryData data, int offset, out XingHeader? header) /// A missing byte count makes no size claim. A count past the bytes after the /// first frame belongs to a longer file than the one in hand. /// - static bool DeclaredSizeFits (uint? byteCount, long bytesFromFrame) + static bool DeclaredSizeFits (uint? byteCount, long fileLength) { if (byteCount is null) return true; - return bytesFromFrame >= 0 && byteCount.Value <= bytesFromFrame; + return fileLength >= 0 && byteCount.Value <= fileLength; } static int CountFrames (BinaryData data, int firstOffset) diff --git a/src/TagLibSharp2/Musepack/MusepackFile.cs b/src/TagLibSharp2/Musepack/MusepackFile.cs index 5e3dd91..887df83 100644 --- a/src/TagLibSharp2/Musepack/MusepackFile.cs +++ b/src/TagLibSharp2/Musepack/MusepackFile.cs @@ -96,6 +96,7 @@ public sealed class MusepackFile : IMediaFile private const int SamplesPerFrameSV7 = 1152; private byte[] _originalData = []; + private long _fileLength; /// /// Gets the source file path if the file was read from disk. @@ -280,6 +281,102 @@ private static MusepackFileReadResult ParseSV8 (ReadOnlySpan data) return MusepackFileReadResult.Success (file); } + /// + /// Reads the stream header and the trailing APE tag. Audio packets stay on disk. + /// + internal static MusepackFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return Finish (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + + stream.Position = 0; + var magic = StreamBytes.ReadAtMost (stream, 4); + using var kept = new MemoryStream (); + StreamBytes.Append (kept, magic); + if (magic.Length >= 3 && magic[0] == (byte)'M' && magic[1] == (byte)'P' && magic[2] == (byte)'+') { + var rest = StreamBytes.ReadAtMost (stream, 12); + StreamBytes.Append (kept, rest); + } else { + while (stream.Position + 3 <= stream.Length) { + var key = StreamBytes.ReadAtMost (stream, 2); + if (key.Length < 2 || !TryReadVarInt (stream, out var packetSize, out var sizeBytes)) + break; + var payloadLen = (long)packetSize - 2 - sizeBytes.Length; + if (payloadLen < 0 || payloadLen > 1024 * 1024) + break; + var payload = StreamBytes.ReadAtMost (stream, (int)payloadLen); + StreamBytes.Append (kept, key); + StreamBytes.Append (kept, sizeBytes); + StreamBytes.Append (kept, payload); + if (key[0] == (byte)'S' && key[1] == (byte)'H') + break; + } + } + + var header = kept.ToArray (); + ApeTagTail.TryRead (stream, out var tag, out var tagAt); + if (tagAt < header.Length) { + stream.Position = 0; + return Finish (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + } + + return Finish (Read (Join (header, tag)), sourcePath, fileSystem, keepPayload: false, fileLength: stream.Length); + } + + static bool TryReadVarInt (Stream stream, out ulong value, out byte[] raw) + { + var bytes = new List (4); + value = 0; + for (var i = 0; i < 9; i++) { + var one = StreamBytes.ReadAtMost (stream, 1); + if (one.Length == 0) { + raw = []; + return false; + } + + bytes.Add (one[0]); + value = (value << 7) | (uint)(one[0] & 0x7F); + if ((one[0] & 0x80) == 0) { + raw = bytes.ToArray (); + return true; + } + } + + raw = []; + return false; + } + + static byte[] Join (byte[] left, byte[] right) + { + if (right.Length == 0) + return left; + var combined = new byte[left.Length + right.Length]; + left.CopyTo (combined, 0); + right.CopyTo (combined, left.Length); + return combined; + } + + static MusepackFileReadResult Finish ( + MusepackFileReadResult result, + string? sourcePath, + IFileSystem? fileSystem, + bool keepPayload, + long? fileLength = null) + { + if (!result.IsSuccess || result.File is null) + return result; + + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + if (!keepPayload) { + result.File._fileLength = fileLength ?? 0; + result.File._originalData = []; + result.File.CalculateProperties (); + } + + return result; + } + /// /// Attempts to read a Musepack file from binary data. /// @@ -413,9 +510,9 @@ private void CalculateProperties () // Calculate bitrate from file size var bitrate = 0; - if (_originalData.Length > 0 && durationSeconds > 0) { - bitrate = (int)(_originalData.Length * 8 / durationSeconds / 1000); - } + var length = _fileLength > 0 ? _fileLength : _originalData.Length; + if (length > 0 && durationSeconds > 0) + bitrate = (int)(length * 8 / durationSeconds / 1000); Properties = new AudioProperties ( duration, @@ -520,8 +617,19 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, /// /// Save the file to a new path using internally stored data. /// - public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) => - SaveToFile (path, _originalData, fileSystem); + public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return SaveToFile (path, _originalData, fs); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return SaveToFile (path, read.Data!.Value.Span, fs); + } /// /// Save the file back to its source path. @@ -556,11 +664,22 @@ public async Task SaveToFileAsync ( /// /// Save the file asynchronously using internally stored data. /// - public Task SaveToFileAsync ( + public async Task SaveToFileAsync ( string path, IFileSystem? fileSystem = null, - CancellationToken cancellationToken = default) => - SaveToFileAsync (path, _originalData, fileSystem, cancellationToken); + CancellationToken cancellationToken = default) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return await SaveToFileAsync (path, _originalData, fs, cancellationToken).ConfigureAwait (false); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return await SaveToFileAsync (path, read.Data!.Value, fs, cancellationToken).ConfigureAwait (false); + } /// /// Save the file back to its source path asynchronously. diff --git a/src/TagLibSharp2/Ogg/OggFlacFile.cs b/src/TagLibSharp2/Ogg/OggFlacFile.cs index 2cef2a7..7f21dd5 100644 --- a/src/TagLibSharp2/Ogg/OggFlacFile.cs +++ b/src/TagLibSharp2/Ogg/OggFlacFile.cs @@ -93,6 +93,7 @@ public sealed class OggFlacFile : IMediaFile private static readonly byte[] FlacMagic = "FLAC"u8.ToArray (); private byte[] _originalData = Array.Empty (); + private long _fileLength; private PacketWalkResult? _cachedWalk; // Counts packet walks. A cache hit does not increment. Test hook only. @@ -252,6 +253,39 @@ public static OggFlacFileReadResult Read (ReadOnlySpan data) return OggFlacFileReadResult.Success (file); } + /// + /// Reads header pages and stops at the first FLAC audio page. Cluster-sized audio is not copied. + /// + internal static OggFlacFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) { + var copied = Read (StreamBytes.ReadToEnd (stream)); + return WithSource (copied, sourcePath, fileSystem, sourcePath is null ? null : stream.CanSeek ? stream.Length : null); + } + + var headers = OggWindow.ReadHeaders (stream, OggHeaderKind.Flac); + var result = Read (headers); + return WithSource (result, sourcePath, fileSystem, stream.Length); + } + + static OggFlacFileReadResult WithSource (OggFlacFileReadResult result, string? sourcePath, IFileSystem? fileSystem, long? fileLength) + { + if (!result.IsSuccess || result.File is null) + return result; + + var file = result.File; + file._sourcePath = sourcePath; + file._sourceFileSystem = fileSystem; + if (fileLength is long length) { + file._fileLength = length; + file._originalData = []; + file._cachedWalk = null; + file.CalculateProperties (); + } + + return result; + } + /// /// Attempts to read an Ogg FLAC file from binary data. /// @@ -338,9 +372,9 @@ private void CalculateProperties () var duration = TimeSpan.FromSeconds (durationSeconds); var bitrate = 0; - if (_originalData.Length > 0 && durationSeconds > 0) { - bitrate = (int)(_originalData.Length * 8 / durationSeconds / 1000); - } + var length = _fileLength > 0 ? _fileLength : _originalData.Length; + if (length > 0 && durationSeconds > 0) + bitrate = (int)(length * 8 / durationSeconds / 1000); Properties = new AudioProperties ( duration, @@ -712,8 +746,21 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, /// /// Save the file to a new path using internally stored data. /// - public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) => - SaveToFile (path, _originalData, fileSystem); + public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return SaveToFile (path, _originalData, fs); + + if (_sourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + + return SaveToFile (path, read.Data!.Value.Span, fs); + } /// /// Save the file back to its source path. @@ -751,11 +798,24 @@ public async Task SaveToFileAsync ( /// /// Save the file asynchronously using internally stored data. /// - public Task SaveToFileAsync ( + public async Task SaveToFileAsync ( string path, IFileSystem? fileSystem = null, - CancellationToken cancellationToken = default) => - SaveToFileAsync (path, _originalData, fileSystem, cancellationToken); + CancellationToken cancellationToken = default) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return await SaveToFileAsync (path, _originalData, fs, cancellationToken).ConfigureAwait (false); + + if (_sourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + + return await SaveToFileAsync (path, read.Data!.Value, fs, cancellationToken).ConfigureAwait (false); + } /// /// Save the file back to its source path asynchronously. diff --git a/src/TagLibSharp2/Ogg/OggOpusFile.cs b/src/TagLibSharp2/Ogg/OggOpusFile.cs index 5246441..b30a937 100644 --- a/src/TagLibSharp2/Ogg/OggOpusFile.cs +++ b/src/TagLibSharp2/Ogg/OggOpusFile.cs @@ -36,7 +36,7 @@ public sealed class OggOpusFile : IMediaFile /// /// /// This is set when using or . - /// It is null when the file was read from binary data using . + /// It is null when the file was read from binary data using . /// public string? SourcePath { get; private set; } @@ -289,6 +289,56 @@ public static OggOpusFileReadResult Read (ReadOnlySpan data, bool validate return OggOpusFileReadResult.Success (file, data.Length); } + /// + /// Reads the Opus header pages and the last page. Audio pages between them are not copied. + /// + internal static OggOpusFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem, bool validateCrc = false) + { + if (!stream.CanSeek) + return WithSource (Read (StreamBytes.ReadToEnd (stream), validateCrc), sourcePath, fileSystem, stream.CanSeek ? stream.Length : null); + + var headers = OggWindow.ReadHeaders (stream, OggHeaderKind.Opus); + var last = OggWindow.ReadLastPage (stream); + var window = last.Length == 0 ? headers : Concat (headers, last); + return WithSource (Read (window, validateCrc), sourcePath, fileSystem, stream.Length); + } + + static OggOpusFileReadResult WithSource (OggOpusFileReadResult result, string? sourcePath, IFileSystem? fileSystem, long? fileLength) + { + if (result.IsSuccess && result.File is not null) { + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + if (fileLength is long length && length > 0) + result.File.UseFileLength (length); + } + + return result; + } + + void UseFileLength (long length) + { + _fileSize = length; + if (Properties.Duration.TotalSeconds <= 0) + return; + + var bitrate = (int)(length * 8 / Properties.Duration.TotalSeconds / 1000); + Properties = new AudioProperties ( + Properties.Duration, + bitrate, + Properties.SampleRate, + Properties.BitsPerSample, + Properties.Channels, + Properties.Codec); + } + + static byte[] Concat (byte[] left, byte[] right) + { + var combined = new byte[left.Length + right.Length]; + left.CopyTo (combined, 0); + right.CopyTo (combined, left.Length); + return combined; + } + /// /// Attempts to read an Ogg Opus file from binary data. /// @@ -632,7 +682,7 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return SaveToFile (path, readResult.Data!.Value.Span, fileSystem); + return SaveToFile (path, readResult.Data!.Value.Span, fs); } /// @@ -674,7 +724,7 @@ public async Task SaveToFileAsync ( if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return await SaveToFileAsync (path, readResult.Data!.Value, fileSystem, cancellationToken) + return await SaveToFileAsync (path, readResult.Data!.Value, fs, cancellationToken) .ConfigureAwait (false); } diff --git a/src/TagLibSharp2/Ogg/OggVorbisFile.cs b/src/TagLibSharp2/Ogg/OggVorbisFile.cs index d15223a..a91db84 100644 --- a/src/TagLibSharp2/Ogg/OggVorbisFile.cs +++ b/src/TagLibSharp2/Ogg/OggVorbisFile.cs @@ -34,7 +34,7 @@ public sealed class OggVorbisFile : IMediaFile /// /// /// This is set when using or . - /// It is null when the file was read from binary data using . + /// It is null when the file was read from binary data using . /// public string? SourcePath { get; private set; } @@ -236,6 +236,38 @@ public static OggVorbisFileReadResult Read (ReadOnlySpan data, bool valida return OggVorbisFileReadResult.Success (file, data.Length); } + /// + /// Reads header pages and the last page. Audio pages between them are not copied. + /// + internal static OggVorbisFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem, bool validateCrc = false) + { + if (!stream.CanSeek) + return WithSource (Read (StreamBytes.ReadToEnd (stream), validateCrc), sourcePath, fileSystem); + + var headers = OggWindow.ReadHeaders (stream, OggHeaderKind.Vorbis); + var last = OggWindow.ReadLastPage (stream); + var window = last.Length == 0 ? headers : Concat (headers, last); + return WithSource (Read (window, validateCrc), sourcePath, fileSystem); + } + + static OggVorbisFileReadResult WithSource (OggVorbisFileReadResult result, string? sourcePath, IFileSystem? fileSystem) + { + if (result.IsSuccess && result.File is not null) { + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + } + + return result; + } + + static byte[] Concat (byte[] left, byte[] right) + { + var combined = new byte[left.Length + right.Length]; + left.CopyTo (combined, 0); + right.CopyTo (combined, left.Length); + return combined; + } + /// /// Attempts to read an Ogg Vorbis file from binary data. /// @@ -542,7 +574,7 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return SaveToFile (path, readResult.Data!.Value.Span, fileSystem); + return SaveToFile (path, readResult.Data!.Value.Span, fs); } /// @@ -584,7 +616,7 @@ public async Task SaveToFileAsync ( if (!readResult.IsSuccess) return FileWriteResult.Failure ($"Failed to re-read source file: {readResult.Error}"); - return await SaveToFileAsync (path, readResult.Data!.Value, fileSystem, cancellationToken) + return await SaveToFileAsync (path, readResult.Data!.Value, fs, cancellationToken) .ConfigureAwait (false); } diff --git a/src/TagLibSharp2/Ogg/OggWindow.cs b/src/TagLibSharp2/Ogg/OggWindow.cs new file mode 100644 index 0000000..864b530 --- /dev/null +++ b/src/TagLibSharp2/Ogg/OggWindow.cs @@ -0,0 +1,188 @@ +// Copyright (c) 2025-2026 Stephen Shaw and contributors +// Licensed under the MIT License. See LICENSE file in the project root for full license information. + +using TagLibSharp2.Core; + +namespace TagLibSharp2.Ogg; + +/// Which header packets a windowed Ogg read keeps. +enum OggHeaderKind +{ + Vorbis, + Opus, + Flac +} + +/// +/// Reads Ogg header pages and, when duration needs it, the last page. +/// Audio pages in between are not copied. +/// +internal static class OggWindow +{ + public const int MaxPageBytes = 27 + 255 + (255 * 255); + + readonly struct PageHeader + { + public PageHeader (byte[] prefix, int bodySize, bool continued, byte[] lacing) + { + Prefix = prefix; + BodySize = bodySize; + Continued = continued; + Lacing = lacing; + } + + public byte[] Prefix { get; } + + public int BodySize { get; } + + public bool Continued { get; } + + public byte[] Lacing { get; } + } + + public static byte[] ReadHeaders (Stream stream, OggHeaderKind kind) + { + if (stream.Position != 0) + stream.Position = 0; + + using var kept = new MemoryStream (); + var complete = 0; + var open = false; + while (stream.Position < stream.Length) { + var pageAt = stream.Position; + if (!TryReadHeader (stream, out var header)) + break; + + var bodyAt = stream.Position; + if (ShouldStop (stream, header, kind, complete, open)) { + stream.Position = pageAt; + break; + } + + stream.Position = bodyAt; + var body = StreamBytes.ReadAtMost (stream, header.BodySize); + if (body.Length != header.BodySize) + break; + + StreamBytes.Append (kept, header.Prefix); + StreamBytes.Append (kept, body); + CountPackets (header, ref complete, ref open); + } + + return kept.ToArray (); + } + + /// + /// The Ogg page that ends at EOF, or empty when the tail is not a page. + /// At most one maximum page is read. + /// + public static byte[] ReadLastPage (Stream stream) + { + if (stream.Length < 27) + return []; + + var window = (int)Math.Min (stream.Length, MaxPageBytes); + stream.Position = stream.Length - window; + var tail = StreamBytes.ReadAtMost (stream, window); + for (var i = tail.Length - 27; i >= 0; i--) { + if (tail[i] != (byte)'O' || tail[i + 1] != (byte)'g' || tail[i + 2] != (byte)'g' || tail[i + 3] != (byte)'S') + continue; + if (!TryPageLength (tail, i, out var length)) + continue; + if (i + length == tail.Length) + return tail.AsSpan (i, length).ToArray (); + } + + return []; + } + + static bool ShouldStop (Stream stream, PageHeader header, OggHeaderKind kind, int complete, bool open) + { + if (header.Continued || open) + return false; + if (kind == OggHeaderKind.Opus) + return complete >= 2; + if (kind == OggHeaderKind.Vorbis && complete < 2) + return false; + if (header.BodySize <= 0) + return kind == OggHeaderKind.Vorbis; + + var one = StreamBytes.ReadAtMost (stream, 1); + if (one.Length == 0) + return true; + if (kind == OggHeaderKind.Flac) + return one[0] == 0xFF; + return one[0] is not (1 or 3 or 5); + } + + static void CountPackets (PageHeader header, ref int complete, ref bool open) + { + var index = 0; + if (header.Continued) { + if (!open || header.Lacing.Length == 0) + return; + if (header.Lacing[0] < 255) { + complete++; + open = false; + } + + index = 1; + } else if (open) { + open = false; + } + + for (; index < header.Lacing.Length; index++) { + if (header.Lacing[index] < 255) { + complete++; + open = false; + } else { + open = true; + } + } + } + + static bool TryReadHeader (Stream stream, out PageHeader header) + { + header = default; + var start = stream.Position; + var head = StreamBytes.ReadAtMost (stream, 27); + if (head.Length < 27 || head[0] != (byte)'O' || head[1] != (byte)'g' || head[2] != (byte)'g' || head[3] != (byte)'S' || head[4] != 0) { + stream.Position = start; + return false; + } + + var segments = head[26]; + var table = StreamBytes.ReadAtMost (stream, segments); + if (table.Length != segments) { + stream.Position = start; + return false; + } + + var body = 0; + for (var i = 0; i < table.Length; i++) + body += table[i]; + + var prefix = new byte[27 + segments]; + head.CopyTo (prefix, 0); + table.CopyTo (prefix, 27); + header = new PageHeader (prefix, body, (head[5] & 0x01) != 0, table); + return true; + } + + static bool TryPageLength (byte[] data, int offset, out int length) + { + length = 0; + if (offset + 27 > data.Length) + return false; + + var segments = data[offset + 26]; + if (offset + 27 + segments > data.Length) + return false; + + var body = 0; + for (var i = 0; i < segments; i++) + body += data[offset + 27 + i]; + length = 27 + segments + body; + return offset + length <= data.Length; + } +} diff --git a/src/TagLibSharp2/WavPack/WavPackFile.cs b/src/TagLibSharp2/WavPack/WavPackFile.cs index 9d5bd15..ae7e0ce 100644 --- a/src/TagLibSharp2/WavPack/WavPackFile.cs +++ b/src/TagLibSharp2/WavPack/WavPackFile.cs @@ -98,6 +98,7 @@ public sealed class WavPackFile : IMediaFile private const byte MetaIdChannelInfo = 0x0D; // Multi-channel info private byte[] _originalData = Array.Empty (); + private long _fileLength; /// /// Gets the source file path if the file was read from disk. @@ -226,6 +227,106 @@ public static WavPackFileReadResult Read (ReadOnlySpan data) return WavPackFileReadResult.Success (file); } + /// + /// Reads the first block header and the trailing APE tag. Later blocks stay on disk. + /// + internal static WavPackFileReadResult Read (Stream stream, string? sourcePath, IFileSystem? fileSystem) + { + if (!stream.CanSeek) + return Finish (Read (StreamBytes.ReadToEnd (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + + stream.Position = 0; + var header = StreamBytes.ReadAtMost (stream, BlockHeaderSize); + if (header.Length < BlockHeaderSize) + return Finish (Read (header), sourcePath, fileSystem, keepPayload: true); + + var blockSize = BinaryPrimitives.ReadUInt32LittleEndian (header.AsSpan (4, 4)); + var block = ReadFirstBlock (stream, header, blockSize); + ApeTagTail.TryRead (stream, out var tag, out var tagAt); + if (tagAt < block.Length) + return Finish (Read (ReadWhole (stream)), sourcePath, fileSystem, keepPayload: sourcePath is null); + + return Finish (Read (Join (block, tag)), sourcePath, fileSystem, keepPayload: false, fileLength: stream.Length); + } + + static byte[] ReadWhole (Stream stream) + { + stream.Position = 0; + return StreamBytes.ReadToEnd (stream); + } + + static byte[] ReadFirstBlock (Stream stream, byte[] header, uint blockSize) + { + var total = 8L + blockSize; + if (total <= header.Length) + return header.AsSpan (0, (int)Math.Min (header.Length, Math.Max (total, 0))).ToArray (); + if (total <= 64 * 1024) { + var rest = StreamBytes.ReadAtMost (stream, (int)(total - header.Length)); + return Join (header, rest); + } + + // The audio bitstream lives in this block. Keep small metadata sub-blocks and seek over the rest. + using var kept = new MemoryStream (); + kept.Write (header, 0, Math.Min (header.Length, BlockHeaderSize)); + var end = Math.Min (stream.Length, total); + stream.Position = BlockHeaderSize; + while (stream.Position + 2 <= end) { + var idByte = StreamBytes.ReadAtMost (stream, 1); + if (idByte.Length == 0) + break; + var large = (idByte[0] & 0x80) != 0; + var sizeRaw = StreamBytes.ReadAtMost (stream, large ? 3 : 1); + if (sizeRaw.Length != (large ? 3 : 1)) + break; + var words = large + ? sizeRaw[0] | (sizeRaw[1] << 8) | (sizeRaw[2] << 16) + : sizeRaw[0]; + var dataBytes = (long)words * 2; + var actualId = (byte)(idByte[0] & 0x1F); + var bitstream = actualId is 0x0A or 0x0B or 0x0C or 0x0E; + if (bitstream || dataBytes > 4096) { + StreamBytes.Skip (stream, dataBytes); + continue; + } + + var data = StreamBytes.ReadAtMost (stream, (int)dataBytes); + StreamBytes.Append (kept, idByte); + StreamBytes.Append (kept, sizeRaw); + StreamBytes.Append (kept, data); + } + + var bytes = kept.ToArray (); + if (bytes.Length >= 8) + BinaryPrimitives.WriteUInt32LittleEndian (bytes.AsSpan (4), (uint)(bytes.Length - 8)); + return bytes; + } + + static byte[] Join (byte[] left, byte[] right) + { + if (right.Length == 0) + return left; + var combined = new byte[left.Length + right.Length]; + left.CopyTo (combined, 0); + right.CopyTo (combined, left.Length); + return combined; + } + + static WavPackFileReadResult Finish (WavPackFileReadResult result, string? sourcePath, IFileSystem? fileSystem, bool keepPayload, long? fileLength = null) + { + if (!result.IsSuccess || result.File is null) + return result; + + result.File.SourcePath = sourcePath; + result.File._sourceFileSystem = fileSystem; + if (!keepPayload) { + result.File._fileLength = fileLength ?? 0; + result.File._originalData = []; + result.File.CalculateProperties (); + } + + return result; + } + /// /// Attempts to read a WavPack file from binary data. /// @@ -275,9 +376,9 @@ private void CalculateProperties () // Calculate bitrate from file size var bitrate = 0; - if (_originalData.Length > 0 && durationSeconds > 0) { - bitrate = (int)(_originalData.Length * 8 / durationSeconds / 1000); - } + var length = _fileLength > 0 ? _fileLength : _originalData.Length; + if (length > 0 && durationSeconds > 0) + bitrate = (int)(length * 8 / durationSeconds / 1000); Properties = new AudioProperties ( duration, @@ -512,8 +613,19 @@ public FileWriteResult SaveToFile (string path, ReadOnlySpan originalData, /// /// Save the file to a new path using internally stored data. /// - public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) => - SaveToFile (path, _originalData, fileSystem); + public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return SaveToFile (path, _originalData, fs); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = FileHelper.SafeReadAllBytes (source, fs); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return SaveToFile (path, read.Data!.Value.Span, fs); + } /// /// Save the file back to its source path. @@ -548,11 +660,22 @@ public async Task SaveToFileAsync ( /// /// Save the file asynchronously using internally stored data. /// - public Task SaveToFileAsync ( + public async Task SaveToFileAsync ( string path, IFileSystem? fileSystem = null, - CancellationToken cancellationToken = default) => - SaveToFileAsync (path, _originalData, fileSystem, cancellationToken); + CancellationToken cancellationToken = default) + { + var fs = fileSystem ?? _sourceFileSystem; + if (_originalData.Length > 0) + return await SaveToFileAsync (path, _originalData, fs, cancellationToken).ConfigureAwait (false); + if (SourcePath is not { Length: > 0 } source) + return FileWriteResult.Failure ("No source path available. File was not read from disk."); + + var read = await FileHelper.SafeReadAllBytesAsync (source, fs, cancellationToken).ConfigureAwait (false); + if (!read.IsSuccess) + return FileWriteResult.Failure ($"Failed to re-read source file: {read.Error}"); + return await SaveToFileAsync (path, read.Data!.Value, fs, cancellationToken).ConfigureAwait (false); + } /// /// Save the file back to its source path asynchronously. diff --git a/tests/TagLibSharp2.Tests/Core/ScanWithoutAudioTests.cs b/tests/TagLibSharp2.Tests/Core/ScanWithoutAudioTests.cs new file mode 100644 index 0000000..38dbfe4 --- /dev/null +++ b/tests/TagLibSharp2.Tests/Core/ScanWithoutAudioTests.cs @@ -0,0 +1,632 @@ +// Copyright (c) 2025-2026 Stephen Shaw and contributors +// Licensed under the MIT License. See LICENSE file in the project root for full license information. + +using System.Buffers.Binary; +using System.Text; +using TagLibSharp2.Aiff; +using TagLibSharp2.Ape; +using TagLibSharp2.Asf; +using TagLibSharp2.Core; +using TagLibSharp2.Dff; +using TagLibSharp2.Dsf; +using TagLibSharp2.Id3.Id3v2; +using TagLibSharp2.Matroska; +using TagLibSharp2.Mp4; +using TagLibSharp2.Musepack; +using TagLibSharp2.Ogg; +using TagLibSharp2.Tests; +using TagLibSharp2.Tests.Asf; +using TagLibSharp2.WavPack; +using TagLibSharp2.Xiph; + +namespace TagLibSharp2.Tests.Core; + +/// +/// A directory scan must read tags and headers, not the audio or video payload. +/// +[TestClass] +[TestCategory ("Unit")] +public class ScanWithoutAudioTests +{ + const int PayloadBytes = 1024 * 1024; + + [TestMethod] + public void Read_Mp4_SkipsMdat () => AssertScan (Mp4WithMdat (), "song.m4a"); + + [TestMethod] + public async Task ReadAsync_Mp4_SkipsMdat () => await AssertScanAsync (Mp4WithMdat (), "song.m4a"); + + [TestMethod] + public void Read_Matroska_SkipsClusters () => AssertScan (MatroskaWithCluster (), "song.mkv"); + + [TestMethod] + public async Task CountingFileSystem_ForwardsTheMembersAScanDoesNotUse () + { + var files = new CountingFileSystem (); + files.AddFile ("/song.bin", new byte[] { 1, 2, 3, 4 }); + Assert.AreEqual ("song.bin", files.GetFileName ("/song.bin")); + + await using (var created = files.Create ("/fresh.bin")) + Assert.AreEqual (0, created.Length); + Assert.IsTrue (files.FileExists ("/fresh.bin")); + files.Delete ("/fresh.bin"); + Assert.IsFalse (files.FileExists ("/fresh.bin")); + + await using var writable = files.OpenReadWrite ("/song.bin"); + writable.WriteByte (9); + using var stream = files.OpenRead ("/song.bin"); + Assert.IsTrue (stream.CanRead); + Assert.IsFalse (stream.CanWrite); + stream.Flush (); + var span = new byte[4]; + Assert.AreEqual (4, stream.Read (span)); + Assert.AreEqual (9, span[0]); + stream.Position = 1; + Assert.AreEqual (1, stream.Position); + Assert.AreEqual (0, stream.Seek (0, SeekOrigin.Begin)); + Assert.AreEqual (1, await stream.ReadAsync (span.AsMemory (0, 1))); + var denied = Assert.ThrowsExactly (() => stream.Write (span, 0, 1)); + Assert.IsNotNull (denied); + var resize = Assert.ThrowsExactly (() => stream.SetLength (1)); + Assert.IsNotNull (resize); + + var saved = Save ("not a media file", "/x", files); + Assert.IsFalse (saved.IsSuccess); + StringAssert.Contains (saved.Error, "Unsupported"); + var asyncSaved = await SaveAsync (42, "/x", files); + Assert.IsFalse (asyncSaved.IsSuccess); + StringAssert.Contains (asyncSaved.Error, "Unsupported"); + } + + [TestMethod] + public void Element_PastOneSizeByte_UsesAWiderLength () + { + var two = Element (0xEC, new byte[200]); + Assert.AreEqual (0x40, two[1] & 0xC0); + var three = Element (0xEC, new byte[20_000]); + Assert.AreEqual (0x20, three[1] & 0xE0); + Assert.AreEqual (-1, IndexOf (new byte[] { 1 }, ReadOnlySpan.Empty)); + Assert.AreEqual (-1, IndexOf (new byte[] { 1, 2 }, new byte[] { 9 })); + Assert.AreEqual (-1, LastIndexOf (new byte[] { 1 }, ReadOnlySpan.Empty)); + Assert.AreEqual (-1, LastIndexOf (new byte[] { 1, 2 }, new byte[] { 9, 9 })); + } + + [TestMethod] + public async Task ReadAsync_Matroska_SkipsClusters () => await AssertScanAsync (MatroskaWithCluster (), "song.mkv"); + + [TestMethod] + public void Read_OggVorbis_SkipsAudioPages () => AssertScan (VorbisWithAudioPages (), "song.ogg", contiguous: false); + + [TestMethod] + public async Task ReadAsync_OggVorbis_SkipsAudioPages () => await AssertScanAsync (VorbisWithAudioPages (), "song.ogg", contiguous: false); + + [TestMethod] + public void Read_Opus_SkipsAudioPages () => AssertScan (OpusWithAudioPages (), "song.opus", contiguous: false); + + [TestMethod] + public async Task ReadAsync_Opus_SkipsAudioPages () => await AssertScanAsync (OpusWithAudioPages (), "song.opus", contiguous: false); + + [TestMethod] + public void Read_OggFlac_SkipsAudioPages () => AssertScan (OggFlacWithAudioPages (), "song.oga", contiguous: false); + + [TestMethod] + public async Task ReadAsync_OggFlac_SkipsAudioPages () => await AssertScanAsync (OggFlacWithAudioPages (), "song.oga", contiguous: false); + + [TestMethod] + public void Read_Aiff_SkipsSoundData () => AssertScan (AiffWithSound (), "song.aiff"); + + [TestMethod] + public async Task ReadAsync_Aiff_SkipsSoundData () => await AssertScanAsync (AiffWithSound (), "song.aiff"); + + [TestMethod] + public void Read_WavPack_SkipsBlocks () => AssertScan (InsertBeforeApe (TestBuilders.WavPack.CreateWithMetadata ("Window")), "song.wv"); + + [TestMethod] + public async Task ReadAsync_WavPack_SkipsBlocks () => + await AssertScanAsync (InsertBeforeApe (TestBuilders.WavPack.CreateWithMetadata ("Window")), "song.wv"); + + [TestMethod] + public void Read_MonkeysAudio_SkipsAudio () => + AssertScan (InsertBeforeApe (TestBuilders.MonkeysAudio.CreateWithMetadata ("Window")), "song.ape"); + + [TestMethod] + public async Task ReadAsync_MonkeysAudio_SkipsAudio () => + await AssertScanAsync (InsertBeforeApe (TestBuilders.MonkeysAudio.CreateWithMetadata ("Window")), "song.ape"); + + [TestMethod] + public void Read_Musepack_SkipsPackets () => + AssertScan (InsertBeforeApe (TestBuilders.Musepack.CreateWithMetadata ("Window")), "song.mpc"); + + [TestMethod] + public async Task ReadAsync_Musepack_SkipsPackets () => + await AssertScanAsync (InsertBeforeApe (TestBuilders.Musepack.CreateWithMetadata ("Window")), "song.mpc"); + + [TestMethod] + public void Read_Dsf_SkipsSampleData () => AssertScan (DsfWithSamples (), "song.dsf"); + + [TestMethod] + public async Task ReadAsync_Dsf_SkipsSampleData () => await AssertScanAsync (DsfWithSamples (), "song.dsf"); + + [TestMethod] + public void Read_Dff_SkipsSampleData () => AssertScan (DffWithSamples (), "song.dff"); + + [TestMethod] + public async Task ReadAsync_Dff_SkipsSampleData () => await AssertScanAsync (DffWithSamples (), "song.dff"); + + [TestMethod] + public void Read_Asf_SkipsDataObject () => AssertScan (AsfWithPackets (), "song.wma"); + + [TestMethod] + public async Task ReadAsync_Asf_SkipsDataObject () => await AssertScanAsync (AsfWithPackets (), "song.wma"); + + static void AssertScan (byte[] file, string name, bool contiguous = true) + { + var loaded = MediaFile.ReadFromData (file, name); + var files = new CountingFileSystem (); + files.AddFile ("/" + name, file); + + var streamed = MediaFile.Read ("/" + name, files); + + AssertSame (loaded, streamed, files.BytesRead, file, contiguous); + AssertSaved (streamed, files, "/" + name, file, contiguous); + AssertStreamWithoutAPathDoesNotWrite (file, name); + } + + static async Task AssertScanAsync (byte[] file, string name, bool contiguous = true) + { + var loaded = MediaFile.ReadFromData (file, name); + var files = new CountingFileSystem (); + files.AddFile ("/" + name, file); + + var streamed = await MediaFile.ReadAsync ("/" + name, files); + + AssertSame (loaded, streamed, files.BytesRead, file, contiguous); + Assert.IsNotNull (streamed.File); + streamed.Tag!.Title = "After"; + var saved = await SaveAsync (streamed.File, "/" + name, files); + Assert.IsTrue (saved.IsSuccess, saved.Error); + AssertSavedBytes (files.ReadAllBytes ("/" + name), file, contiguous); + Assert.AreEqual ("After", MediaFile.ReadFromData (files.ReadAllBytes ("/" + name), name).Tag!.Title); + } + + static void AssertSame (MediaFileResult loaded, MediaFileResult streamed, long bytesRead, byte[] file, bool contiguous) + { + Assert.IsTrue (loaded.IsSuccess, loaded.Error); + Assert.IsTrue (streamed.IsSuccess, streamed.Error); + Assert.AreEqual ("Window", streamed.Tag?.Title, $"loaded='{loaded.Tag?.Title}' error='{streamed.Error}'"); + var full = Properties (loaded); + var window = Properties (streamed); + Assert.IsNotNull (full); + Assert.IsNotNull (window); + Assert.AreEqual (full!.Duration, window!.Duration); + Assert.AreEqual (full.Bitrate, window.Bitrate); + Assert.AreEqual (full.SampleRate, window.SampleRate); + Assert.AreEqual (full.Channels, window.Channels); + Assert.IsTrue (bytesRead < file.Length - 500_000, $"read {bytesRead} of {file.Length}"); + if (contiguous) + Assert.IsTrue (IndexOf (file, Payload ()) >= 0); + } + + static void AssertSaved (MediaFileResult streamed, CountingFileSystem files, string path, byte[] original, bool contiguous) + { + streamed.Tag!.Title = "After"; + var saved = Save (streamed.File!, path, files); + Assert.IsTrue (saved.IsSuccess, saved.Error); + var written = files.ReadAllBytes (path); + AssertSavedBytes (written, original, contiguous); + Assert.AreEqual ("After", MediaFile.ReadFromData (written, path).Tag!.Title); + } + + static void AssertSavedBytes (byte[] written, byte[] original, bool contiguous) + { + Assert.IsTrue (written.Length > original.Length / 2, $"saved {written.Length} of {original.Length}"); + if (contiguous) { + Assert.IsTrue (IndexOf (written, Payload ()) >= 0, "payload bytes were not copied"); + } else { + Assert.IsTrue (IndexOf (written, Marker ()) >= 0, "audio page body was not copied"); + } + } + + static void AssertStreamWithoutAPathDoesNotWrite (byte[] file, string name) + { + var opened = MediaFile.Read (new MemoryStream (file), name); + Assert.IsTrue (opened.IsSuccess, opened.Error); + var files = new MockFileSystem (); + var saved = Save (opened.File!, "/orphan-" + name, files); + Assert.IsFalse (saved.IsSuccess, saved.Error); + Assert.IsFalse (files.FileExists ("/orphan-" + name)); + } + + static IMediaProperties? Properties (MediaFileResult result) => + ((IMediaFile)result.File!).AudioProperties; + + static FileWriteResult Save (object file, string path, IFileSystem fileSystem) => file switch { + Mp4File mp4 => mp4.SaveToFile (path, fileSystem), + MatroskaFile matroska => matroska.SaveToFile (path, fileSystem), + OggVorbisFile vorbis => vorbis.SaveToFile (path, fileSystem), + OggOpusFile opus => opus.SaveToFile (path, fileSystem), + OggFlacFile oggFlac => oggFlac.SaveToFile (path, fileSystem), + AiffFile aiff => aiff.SaveToFile (path, fileSystem), + WavPackFile wavPack => wavPack.SaveToFile (path, fileSystem), + MonkeysAudioFile ape => ape.SaveToFile (path, fileSystem), + MusepackFile musepack => musepack.SaveToFile (path, fileSystem), + DsfFile dsf => dsf.SaveToFile (path, fileSystem), + DffFile dff => dff.SaveToFile (path, fileSystem), + AsfFile asf => asf.SaveToFile (path, fileSystem), + _ => FileWriteResult.Failure ("Unsupported file " + file.GetType ().Name) + }; + + static Task SaveAsync (object file, string path, IFileSystem fileSystem) => file switch { + Mp4File mp4 => mp4.SaveToFileAsync (path, fileSystem), + MatroskaFile matroska => matroska.SaveToFileAsync (path, fileSystem), + OggVorbisFile vorbis => vorbis.SaveToFileAsync (path, fileSystem), + OggOpusFile opus => opus.SaveToFileAsync (path, fileSystem), + OggFlacFile oggFlac => oggFlac.SaveToFileAsync (path, fileSystem), + AiffFile aiff => aiff.SaveToFileAsync (path, fileSystem), + WavPackFile wavPack => wavPack.SaveToFileAsync (path, fileSystem), + MonkeysAudioFile ape => ape.SaveToFileAsync (path, fileSystem), + MusepackFile musepack => musepack.SaveToFileAsync (path, fileSystem), + DsfFile dsf => dsf.SaveToFileAsync (path, fileSystem), + DffFile dff => dff.SaveToFileAsync (path, fileSystem), + AsfFile asf => asf.SaveToFileAsync (path, fileSystem), + _ => Task.FromResult (FileWriteResult.Failure ("Unsupported file " + file.GetType ().Name)) + }; + + static byte[] Payload () + { + var payload = new byte[PayloadBytes]; + for (var i = 0; i < payload.Length; i++) + payload[i] = (byte)(0xA5 ^ (i * 13)); + payload[0] = 0xFE; + payload[1] = 0xED; + payload[^2] = 0xBE; + payload[^1] = 0xEF; + return payload; + } + + static byte[] Marker () => [0xFE, 0xED, 0xA5, 0x5A, 0x3C, 0xC3, 0x11, 0x22, 0x33, 0x44, 0xBE, 0xEF]; + + static byte[] Mp4WithMdat () + { + var small = TestBuilders.Mp4.CreateWithMetadata ("Window"); + var ftypSize = BinaryPrimitives.ReadInt32BigEndian (small); + var ftyp = small.AsSpan (0, ftypSize).ToArray (); + var moovSize = BinaryPrimitives.ReadInt32BigEndian (small.AsSpan (ftypSize)); + var moov = small.AsSpan (ftypSize, moovSize).ToArray (); + return Concat (ftyp, Box ("mdat", Payload ()), moov); + } + + static byte[] Box (string type, byte[] data) + { + var box = new byte[8 + data.Length]; + BinaryPrimitives.WriteUInt32BigEndian (box, (uint)box.Length); + Encoding.ASCII.GetBytes (type).CopyTo (box, 4); + data.CopyTo (box, 8); + return box; + } + + static byte[] MatroskaWithCluster () + { + var duration = new byte[8]; + BinaryPrimitives.WriteDoubleBigEndian (duration, 1_000_000_000d); + var rate = new byte[4]; + BinaryPrimitives.WriteSingleBigEndian (rate, 44100f); + var info = Master (0x1549A966, Element (0x4489, duration)); + var track = Master (0x1654AE6B, Master (0xAE, Concat ( + Element (0x83, [2]), + Element (0x86, "A_VORBIS"u8), + Master (0xE1, Concat (Element (0xB5, rate), Element (0x9F, [2])))))); + var tags = Master (0x1254C367, Master (0x7373, Concat ( + Master (0x63C0, Element (0x68CA, [30])), + Master (0x67C8, Concat ( + Element (0x45A3, "TITLE"u8), + Element (0x4487, "Window"u8)))))); + var cluster = Element (0x1F43B675, Payload ()); + var ebml = Master (0x1A45DFA3, Element (0x4282, "matroska"u8)); + return Concat (ebml, Element (0x18538067, Concat (info, track, tags, cluster))); + } + + static byte[] VorbisWithAudioPages () + { + var comment = new VorbisComment (TestConstants.Vendors.TagLibSharp2) { Title = "Window" }; + var head = TestBuilders.Ogg.CreatePage ( + TestBuilders.Ogg.CreateIdentificationPacket (), 0, OggPageFlags.BeginOfStream); + var tags = TestBuilders.Ogg.CreatePage ( + TestBuilders.Ogg.CreateCommentPacket (comment), 1, OggPageFlags.None); + var setup = TestBuilders.Ogg.CreatePage ( + TestBuilders.Ogg.CreateSetupPacket (), 2, OggPageFlags.None); + return Concat (head, tags, setup, AudioPages (3, 44100)); + } + + static byte[] OpusWithAudioPages () + { + var comment = new VorbisComment (TestConstants.Vendors.TagLibSharp2) { Title = "Window" }; + var head = TestBuilders.Ogg.CreatePage ( + TestBuilders.Opus.CreateOpusHeadPacket (), 0, OggPageFlags.BeginOfStream); + var tags = TestBuilders.Ogg.CreatePage ( + TestBuilders.Opus.CreateOpusTagsPacket (comment), 1, OggPageFlags.None); + return Concat (head, tags, AudioPages (2, 48000 + 312)); + } + + static byte[] OggFlacWithAudioPages () + { + var comment = TestBuilders.OggFlac.CreateWithMetadata ("Window"); + // The builder's last page is EOS. Audio pages follow it; duration comes from STREAMINFO. + return Concat (comment, AudioPages (2, 0, firstByte: 0xFF)); + } + + static byte[] AudioPages (uint sequence, ulong granule, byte firstByte = 0x11) + { + // 254 * 255 fits in one page (255 lacing values). 255 * 255 needs a 256th segment. + var body = new byte[254 * 255]; + Array.Fill (body, (byte)0x11); + body[0] = firstByte; + Marker ().CopyTo (body, 10); + using var pages = new MemoryStream (); + for (var i = 0; i < 16; i++) { + var flags = i == 15 ? OggPageFlags.EndOfStream : OggPageFlags.None; + var page = TestBuilders.Ogg.CreatePage (body, sequence + (uint)i, flags); + if (i == 15) + BinaryPrimitives.WriteUInt64LittleEndian (page.AsSpan (6), granule); + pages.Write (page); + } + + return pages.ToArray (); + } + + static byte[] AiffWithSound () + { + var id3 = new Id3v2Tag { Title = "Window" }.Render ().ToArray (); + using var body = new BinaryDataBuilder (); + body.AddStringLatin1 ("COMM"); + body.AddUInt32BE (18); + body.AddUInt16BE (1); + body.AddUInt32BE (44100); + body.AddUInt16BE (16); + body.Add (new byte[] { 0x40, 0x0E, 0xAC, 0x44, 0, 0, 0, 0, 0, 0 }); + body.AddStringLatin1 ("SSND"); + body.AddUInt32BE ((uint)(8 + PayloadBytes)); + body.AddUInt32BE (0); + body.AddUInt32BE (0); + body.Add (Payload ()); + body.AddStringLatin1 ("ID3 "); + body.AddUInt32BE ((uint)id3.Length); + body.Add (id3); + if ((id3.Length & 1) == 1) + body.Add (0); + var chunks = body.ToBinaryData (); + using var file = new BinaryDataBuilder (); + file.AddStringLatin1 ("FORM"); + file.AddUInt32BE ((uint)(4 + chunks.Length)); + file.AddStringLatin1 ("AIFF"); + file.Add (chunks); + return file.ToBinaryData ().ToArray (); + } + + static byte[] InsertBeforeApe (byte[] file) + { + var at = IndexOf (file, "APETAGEX"u8); + Assert.IsTrue (at > 0); + var payload = Payload (); + var result = new byte[file.Length + payload.Length]; + Buffer.BlockCopy (file, 0, result, 0, at); + Buffer.BlockCopy (payload, 0, result, at, payload.Length); + Buffer.BlockCopy (file, at, result, at + payload.Length, file.Length - at); + return result; + } + + static byte[] DsfWithSamples () + { + var file = TestBuilders.Dsf.CreateWithMetadata ("Window"); + var meta = (int)BinaryPrimitives.ReadUInt64LittleEndian (file.AsSpan (20)); + var payload = Payload (); + var result = new byte[file.Length + payload.Length]; + Buffer.BlockCopy (file, 0, result, 0, meta); + Buffer.BlockCopy (payload, 0, result, meta, payload.Length); + Buffer.BlockCopy (file, meta, result, meta + payload.Length, file.Length - meta); + BinaryPrimitives.WriteUInt64LittleEndian (result.AsSpan (12), (ulong)result.Length); + BinaryPrimitives.WriteUInt64LittleEndian (result.AsSpan (20), (ulong)(meta + payload.Length)); + var dataSize = BinaryPrimitives.ReadUInt64LittleEndian (file.AsSpan (84)); + BinaryPrimitives.WriteUInt64LittleEndian (result.AsSpan (84), dataSize + (ulong)payload.Length); + return result; + } + + static byte[] DffWithSamples () + { + var file = TestBuilders.Dff.CreateWithId3v2 ("Window"); + // Form type and CMPR also contain "DSD ". The audio chunk is the last one. + var at = LastIndexOf (file, "DSD "u8); + Assert.IsTrue (at > 16); + var size = BinaryPrimitives.ReadUInt64BigEndian (file.AsSpan (at + 4)); + var dataStart = at + 12; + var payload = Payload (); + var result = new byte[file.Length + payload.Length]; + Buffer.BlockCopy (file, 0, result, 0, dataStart); + Buffer.BlockCopy (payload, 0, result, dataStart, payload.Length); + Buffer.BlockCopy (file, dataStart, result, dataStart + payload.Length, file.Length - dataStart); + BinaryPrimitives.WriteUInt64BigEndian (result.AsSpan (at + 4), size + (ulong)payload.Length); + var form = BinaryPrimitives.ReadUInt64BigEndian (result.AsSpan (4)); + BinaryPrimitives.WriteUInt64BigEndian (result.AsSpan (4), form + (ulong)payload.Length); + return result; + } + + static byte[] AsfWithPackets () + { + var file = AsfTestBuilder.CreateMinimalWma ("Window"); + var headerSize = (int)BinaryPrimitives.ReadUInt64LittleEndian (file.AsSpan (16)); + var payload = Payload (); + var result = new byte[file.Length + payload.Length]; + Buffer.BlockCopy (file, 0, result, 0, file.Length); + Buffer.BlockCopy (payload, 0, result, file.Length, payload.Length); + var dataSize = BinaryPrimitives.ReadUInt64LittleEndian (result.AsSpan (headerSize + 16)); + BinaryPrimitives.WriteUInt64LittleEndian (result.AsSpan (headerSize + 16), dataSize + (ulong)payload.Length); + return result; + } + + static byte[] Element (ulong id, ReadOnlySpan payload) + { + var idBytes = Id (id); + var size = EbmlSize (payload.Length); + var result = new byte[idBytes.Length + size.Length + payload.Length]; + idBytes.CopyTo (result, 0); + size.CopyTo (result, idBytes.Length); + payload.CopyTo (result.AsSpan (idBytes.Length + size.Length)); + return result; + } + + static byte[] Master (ulong id, params byte[][] children) => Element (id, Concat (children)); + + static byte[] EbmlSize (int length) + { + if (length < 0x7F) + return [(byte)(0x80 | length)]; + if (length < 0x3FFF) + return [(byte)(0x40 | (length >> 8)), (byte)length]; + if (length < 0x1F_FFFF) + return [(byte)(0x20 | (length >> 16)), (byte)(length >> 8), (byte)length]; + return [(byte)(0x10 | (length >> 24)), (byte)(length >> 16), (byte)(length >> 8), (byte)length]; + } + + static byte[] Id (ulong id) + { + var width = id > 0xFFFFFF ? 4 : id > 0xFFFF ? 3 : id > 0xFF ? 2 : 1; + var bytes = new byte[width]; + for (var i = width - 1; i >= 0; i--) { + bytes[i] = (byte)id; + id >>= 8; + } + + return bytes; + } + + static byte[] Concat (params byte[][] parts) + { + var length = 0; + foreach (var part in parts) + length += part.Length; + var result = new byte[length]; + var offset = 0; + foreach (var part in parts) { + part.CopyTo (result, offset); + offset += part.Length; + } + + return result; + } + + static int LastIndexOf (byte[] haystack, ReadOnlySpan needle) + { + if (needle.Length == 0 || needle.Length > haystack.Length) + return -1; + for (var i = haystack.Length - needle.Length; i >= 0; i--) { + if (haystack[i] == needle[0] && haystack.AsSpan (i, needle.Length).SequenceEqual (needle)) + return i; + } + + return -1; + } + + static int IndexOf (byte[] haystack, ReadOnlySpan needle) + { + if (needle.Length == 0 || needle.Length > haystack.Length) + return -1; + for (var i = 0; i <= haystack.Length - needle.Length; i++) { + if (haystack[i] == needle[0] && haystack.AsSpan (i, needle.Length).SequenceEqual (needle)) + return i; + } + + return -1; + } + + sealed class CountingFileSystem : IFileSystem + { + readonly MockFileSystem _inner = new (); + long _bytesRead; + + public long BytesRead => _bytesRead; + + public void AddFile (string path, byte[] data) => _inner.AddFile (path, data); + + public bool FileExists (string path) => _inner.FileExists (path); + + public Stream OpenRead (string path) => new CountingStream (_inner.OpenRead (path), read => _bytesRead += read); + + public Stream OpenReadWrite (string path) => _inner.OpenReadWrite (path); + + public Stream Create (string path) => _inner.Create (path); + + public byte[] ReadAllBytes (string path) => _inner.ReadAllBytes (path); + + public Task ReadAllBytesAsync (string path, CancellationToken cancellationToken = default) => + _inner.ReadAllBytesAsync (path, cancellationToken); + + public void WriteAllBytes (string path, ReadOnlySpan data) => _inner.WriteAllBytes (path, data); + + public Task WriteAllBytesAsync (string path, ReadOnlyMemory data, CancellationToken cancellationToken = default) => + _inner.WriteAllBytesAsync (path, data, cancellationToken); + + public void Delete (string path) => _inner.Delete (path); + + public void Move (string sourcePath, string destinationPath) => _inner.Move (sourcePath, destinationPath); + + public string? GetDirectoryName (string path) => _inner.GetDirectoryName (path); + + public string GetFileName (string path) => _inner.GetFileName (path); + + public string CombinePath (string path1, string path2) => _inner.CombinePath (path1, path2); + } + + sealed class CountingStream : Stream + { + readonly Stream _inner; + readonly Action _count; + + public CountingStream (Stream inner, Action count) + { + _inner = inner; + _count = count; + } + + public override bool CanRead => _inner.CanRead; + + public override bool CanSeek => _inner.CanSeek; + + public override bool CanWrite => false; + + public override long Length => _inner.Length; + + public override long Position { + get => _inner.Position; + set => _inner.Position = value; + } + + public override void Flush () => _inner.Flush (); + + public override int Read (byte[] buffer, int offset, int count) + { + var read = _inner.Read (buffer, offset, count); + _count (read); + return read; + } + + public override int Read (Span buffer) + { + var read = _inner.Read (buffer); + _count (read); + return read; + } + + public override ValueTask ReadAsync (Memory buffer, CancellationToken cancellationToken = default) + { + var read = _inner.Read (buffer.Span); + _count (read); + return new ValueTask (read); + } + + public override long Seek (long offset, SeekOrigin origin) => _inner.Seek (offset, origin); + + public override void SetLength (long value) => throw new NotSupportedException (); + + public override void Write (byte[] buffer, int offset, int count) => throw new NotSupportedException (); + } +} diff --git a/tests/TagLibSharp2.Tests/Core/WindowedEdgeTests.cs b/tests/TagLibSharp2.Tests/Core/WindowedEdgeTests.cs new file mode 100644 index 0000000..e3c4e56 --- /dev/null +++ b/tests/TagLibSharp2.Tests/Core/WindowedEdgeTests.cs @@ -0,0 +1,544 @@ +// Copyright (c) 2025-2026 Stephen Shaw and contributors +// Licensed under the MIT License. See LICENSE file in the project root for full license information. + +using System.Buffers.Binary; +using System.Text; +using TagLibSharp2.Aiff; +using TagLibSharp2.Ape; +using TagLibSharp2.Asf; +using TagLibSharp2.Core; +using TagLibSharp2.Dff; +using TagLibSharp2.Dsf; +using TagLibSharp2.Matroska; +using TagLibSharp2.Mp4; +using TagLibSharp2.Musepack; +using TagLibSharp2.Ogg; +using TagLibSharp2.Tests; +using TagLibSharp2.Tests.Asf; +using TagLibSharp2.WavPack; + +namespace TagLibSharp2.Tests.Core; + +[TestClass] +[TestCategory ("Unit")] +public class WindowedEdgeTests +{ + [TestMethod] + [DataRow ("song.m4a")] + [DataRow ("song.mkv")] + [DataRow ("song.ogg")] + [DataRow ("song.opus")] + [DataRow ("song.oga")] + [DataRow ("song.aiff")] + [DataRow ("song.wv")] + [DataRow ("song.ape")] + [DataRow ("song.mpc")] + [DataRow ("song.dsf")] + [DataRow ("song.dff")] + [DataRow ("song.wma")] + public void Read_ExtensionWithNoHeader_Fails (string name) + { + var opened = MediaFile.Read (new MemoryStream (new byte[] { 1, 2 }), name); + + Assert.IsFalse (opened.IsSuccess, name); + Assert.IsFalse (string.IsNullOrEmpty (opened.Error), name); + } + + [TestMethod] + public void Read_MissingDirectory_ReportsTheFileAsMissing () + { + var files = new MissingDirectory (); + var opened = MediaFile.Read ("/gone/song.mp3", files); + + Assert.IsFalse (opened.IsSuccess); + Assert.AreEqual ("File not found: /gone/song.mp3", opened.Error); + } + + [TestMethod] + public async Task ReadAsync_MissingDirectory_ReportsTheFileAsMissing () + { + var files = new MissingDirectory (); + var opened = await MediaFile.ReadAsync ("/gone/song.mp3", files); + + Assert.IsFalse (opened.IsSuccess); + Assert.AreEqual ("File not found: /gone/song.mp3", opened.Error); + } + + [TestMethod] + public void Skip_NonPositiveNonSeekableAndPastTheEnd_DoesNotReadPastTheBuffer () + { + var stream = new MemoryStream (new byte[] { 1, 2, 3, 4 }) { Position = 1 }; + StreamBytes.Skip (stream, 0); + StreamBytes.Skip (stream, -4); + Assert.AreEqual (1, stream.Position); + StreamBytes.Skip (stream, 100); + Assert.AreEqual (4, stream.Position); + + var copied = new NonSeekableStream (new MemoryStream (new byte[] { 9, 8 })); + StreamBytes.Skip (copied, 5); + var buffer = new byte[2]; + Assert.AreEqual (2, copied.Read (buffer, 0, 2)); + CollectionAssert.AreEqual (new byte[] { 9, 8 }, buffer); + } + + [TestMethod] + public void Read_NonSeekableCopy_MatchesTheSeekableRead () + { + SameWhenCopied (TestBuilders.Aiff.CreateMinimal (), "song.aiff"); + SameWhenCopied (TestBuilders.WavPack.CreateMinimal (), "song.wv"); + SameWhenCopied (TestBuilders.MonkeysAudio.CreateMinimal (), "song.ape"); + SameWhenCopied (TestBuilders.Musepack.CreateMinimal (), "song.mpc"); + SameWhenCopied (TestBuilders.Dsf.CreateMinimal (), "song.dsf"); + SameWhenCopied (TestBuilders.Dff.CreateMinimal (), "song.dff"); + SameWhenCopied (TestBuilders.Mp4.CreateMinimalM4a (Mp4CodecType.Aac), "song.m4a"); + SameWhenCopied (TestBuilders.Ogg.CreateMinimalFile (), "song.ogg"); + SameWhenCopied (TestBuilders.Opus.CreateMinimalFile (), "song.opus"); + SameWhenCopied (TestBuilders.OggFlac.CreateMinimal (), "song.oga"); + SameWhenCopied (AsfTestBuilder.CreateMinimalWma ("Window"), "song.wma"); + SameWhenCopied (MatroskaAlbum (), "song.mkv"); + } + + [TestMethod] + public void Read_WithASourcePath_StillParsesANonSeekableCopy () + { + var files = new MockFileSystem (); + AssertCopied (AiffFile.Read (Copy (TestBuilders.Aiff.CreateMinimal ()), "/song.aiff", files)); + AssertCopied (WavPackFile.Read (Copy (TestBuilders.WavPack.CreateMinimal ()), "/song.wv", files)); + AssertCopied (MonkeysAudioFile.Read (Copy (TestBuilders.MonkeysAudio.CreateMinimal ()), "/song.ape", files)); + AssertCopied (MusepackFile.Read (Copy (TestBuilders.Musepack.CreateMinimal ()), "/song.mpc", files)); + AssertCopied (DsfFile.Read (Copy (TestBuilders.Dsf.CreateMinimal ()), "/song.dsf", files)); + AssertCopied (DffFile.Read (Copy (TestBuilders.Dff.CreateMinimal ()), "/song.dff", files)); + AssertCopied (Mp4File.Read (Copy (TestBuilders.Mp4.CreateMinimalM4a (Mp4CodecType.Aac)), "/song.m4a", files)); + AssertCopied (OggVorbisFile.Read (Copy (TestBuilders.Ogg.CreateMinimalFile ()), "/song.ogg", files)); + AssertCopied (OggOpusFile.Read (Copy (TestBuilders.Opus.CreateMinimalFile ()), "/song.opus", files)); + AssertCopied (OggFlacFile.Read (Copy (TestBuilders.OggFlac.CreateMinimal ()), "/song.oga", files)); + AssertCopied (AsfFile.Read (Copy (AsfTestBuilder.CreateMinimalWma ("Window")), "/song.wma", files)); + AssertCopied (MatroskaFile.Read (Copy (MatroskaAlbum ()), "/song.mkv", files)); + } + + [TestMethod] + public async Task Save_WithoutBytesOrASourcePath_FailsClosed () + { + await ExpectNoSource (WavPackFile.Read (Copy (TestBuilders.WavPack.CreateMinimal ()), "", null).File!, static file => file.SaveToFileAsync ("/copy.wv")); + await ExpectNoSource (MonkeysAudioFile.Read (Copy (TestBuilders.MonkeysAudio.CreateMinimal ()), "", null).File!, static file => file.SaveToFileAsync ("/copy.ape")); + await ExpectNoSource (MusepackFile.Read (Copy (TestBuilders.Musepack.CreateMinimal ()), "", null).File!, static file => file.SaveToFileAsync ("/copy.mpc")); + await ExpectNoSource (AsfFile.Read (Copy (AsfTestBuilder.CreateMinimalWma ("Window")), "", null).File!, static file => file.SaveToFileAsync ("/copy.wma")); + await ExpectNoSource (DffFile.Read (Copy (TestBuilders.Dff.CreateMinimal ()), "", null).File!, static file => file.SaveToFileAsync ("/copy.dff")); + } + + [TestMethod] + public async Task Save_WhenTheSourceDisappears_ReportsTheRereadFailure () + { + var files = new MockFileSystem (); + await ExpectReread (WavPackFile.Read (AtStart (TestBuilders.WavPack.CreateMinimal ()), "/song.wv", files).File!, static file => file.SaveToFile ("/copy.wv"), static file => file.SaveToFileAsync ("/copy.wv")); + await ExpectReread (MonkeysAudioFile.Read (AtStart (TestBuilders.MonkeysAudio.CreateMinimal ()), "/song.ape", files).File!, static file => file.SaveToFile ("/copy.ape"), static file => file.SaveToFileAsync ("/copy.ape")); + await ExpectReread (MusepackFile.Read (AtStart (TestBuilders.Musepack.CreateMinimal ()), "/song.mpc", files).File!, static file => file.SaveToFile ("/copy.mpc"), static file => file.SaveToFileAsync ("/copy.mpc")); + await ExpectReread (AsfFile.Read (AtStart (AsfTestBuilder.CreateMinimalWma ("Window")), "/song.wma", files).File!, static file => file.SaveToFile ("/copy.wma"), static file => file.SaveToFileAsync ("/copy.wma")); + await ExpectReread (OggFlacFile.Read (AtStart (TestBuilders.OggFlac.CreateMinimal ()), "/song.oga", files).File!, static file => file.SaveToFile ("/copy.oga"), static file => file.SaveToFileAsync ("/copy.oga")); + await ExpectReread (DffFile.Read (AtStart (TestBuilders.Dff.CreateMinimal ()), "/song.dff", files).File!, static file => file.SaveToFile ("/copy.dff"), static file => file.SaveToFileAsync ("/copy.dff")); + await ExpectReread (Mp4File.Read (AtStart (TestBuilders.Mp4.CreateMinimalM4a (Mp4CodecType.Aac)), "/song.m4a", files).File!, static file => file.SaveToFile ("/copy.m4a"), static file => file.SaveToFileAsync ("/copy.m4a")); + await ExpectReread (DsfFile.Read (AtStart (TestBuilders.Dsf.CreateMinimal ()), "/song.dsf", files).File!, static file => file.SaveToFile ("/copy.dsf"), static file => file.SaveToFileAsync ("/copy.dsf")); + await ExpectReread (MatroskaFile.Read (AtStart (MatroskaAlbum ()), "/song.mkv", files).File!, static file => file.SaveToFile ("/copy.mkv"), static file => file.SaveToFileAsync ("/copy.mkv")); + } + + [TestMethod] + public void Read_WavPackBlockLargerThanTheMetadataWindow_KeepsSmallSubBlocks () + { + var body = new byte[32 + 4 + 42 + 1]; + "wvpk"u8.CopyTo (body); + BinaryPrimitives.WriteUInt32LittleEndian (body.AsSpan (4), 70_000); + BinaryPrimitives.WriteUInt16LittleEndian (body.AsSpan (8), 0x0410); + body[32] = 0x01; + body[33] = 1; + body[34] = 0x11; + body[35] = 0x22; + body[36] = 0x0A; + body[37] = 20; + body[78] = 0x02; + using var stream = new ReportedStream (body, 8 + 70_000); + + var opened = WavPackFile.Read (stream, "/song.wv", new MockFileSystem ()); + + Assert.IsTrue (opened.IsSuccess, opened.Error); + } + + [TestMethod] + public void Read_OldAndOversizedApeHeaders_FollowTheVersion () + { + var old = MonkeysAudioFile.Read (AtStart (TestBuilders.MonkeysAudio.CreateMinimal (version: 3800)), "/old.ape", null); + Assert.IsTrue (old.IsSuccess, old.Error); + + var truncated = new byte[10]; + "MAC "u8.CopyTo (truncated); + BinaryPrimitives.WriteUInt16LittleEndian (truncated.AsSpan (4), 3990); + var shortDescriptor = MonkeysAudioFile.Read (AtStart (truncated), null, null); + Assert.IsFalse (shortDescriptor.IsSuccess); + StringAssert.Contains (shortDescriptor.Error, "truncated descriptor"); + + var huge = new byte[16]; + "MAC "u8.CopyTo (huge); + BinaryPrimitives.WriteUInt16LittleEndian (huge.AsSpan (4), 3990); + BinaryPrimitives.WriteUInt32LittleEndian (huge.AsSpan (8), 2_000_000); + var oversized = MonkeysAudioFile.Read (AtStart (huge), null, null); + Assert.IsFalse (oversized.IsSuccess); + StringAssert.Contains (oversized.Error, "too large"); + } + + [TestMethod] + public void ApeTail_RejectsAShortGarbageOrOversizedFooter () + { + Assert.IsFalse (ApeTagTail.TryRead (new MemoryStream (new byte[8]), out _, out _)); + Assert.IsFalse (ApeTagTail.TryRead (new MemoryStream (new byte[40]), out _, out _)); + + var footer = new byte[32]; + "APETAGEX"u8.CopyTo (footer); + BinaryPrimitives.WriteUInt32LittleEndian (footer.AsSpan (8), 2000); + BinaryPrimitives.WriteUInt32LittleEndian (footer.AsSpan (12), uint.MaxValue); + Assert.IsFalse (ApeTagTail.TryRead (new MemoryStream (footer), out _, out _)); + + BinaryPrimitives.WriteUInt32LittleEndian (footer.AsSpan (12), 32); + BinaryPrimitives.WriteUInt32LittleEndian (footer.AsSpan (20), 0x20000000); + Assert.IsFalse (ApeTagTail.TryRead (new MemoryStream (footer), out _, out _)); + } + + [TestMethod] + public void Read_AiffFormThatIsNotAudio_Fails () + { + var bytes = new byte[12]; + "FORM"u8.CopyTo (bytes); + "WAVE"u8.CopyTo (bytes.AsSpan (8)); + + var opened = AiffFile.Read (AtStart (bytes), null, null); + + Assert.IsFalse (opened.IsSuccess); + StringAssert.Contains (opened.Error, "form type"); + } + + [TestMethod] + public void Read_AsfHeaderSizeOutOfRange_Fails () + { + var head = new byte[30]; + new byte[] { 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }.CopyTo (head, 0); + BinaryPrimitives.WriteUInt64LittleEndian (head.AsSpan (16), 10); + + var opened = AsfFile.Read (AtStart (head), null, null); + + Assert.IsFalse (opened.IsSuccess); + StringAssert.Contains (opened.Error, "out of range"); + } + + [TestMethod] + public void Read_Mp4SizeForms_StopOrMeasureTheBox () + { + Assert.IsNotNull (Mp4File.Read (AtStart (Box (4, "ftyp")), null, null).Error); + Assert.IsNotNull (Mp4File.Read (AtStart (Box (1, "ftyp")), null, null).Error); + + var shortExtended = new byte[16]; + BinaryPrimitives.WriteUInt32BigEndian (shortExtended, 1); + "ftyp"u8.CopyTo (shortExtended.AsSpan (4)); + BinaryPrimitives.WriteUInt64BigEndian (shortExtended.AsSpan (8), 8); + Assert.IsNotNull (Mp4File.Read (AtStart (shortExtended), null, null).Error); + + var toEnd = new byte[16]; + "free"u8.CopyTo (toEnd.AsSpan (4)); + Assert.IsNotNull (Mp4File.Read (AtStart (toEnd), null, null).Error); + + var extended = new byte[32]; + BinaryPrimitives.WriteUInt32BigEndian (extended, 1); + "free"u8.CopyTo (extended.AsSpan (4)); + BinaryPrimitives.WriteUInt64BigEndian (extended.AsSpan (8), 32); + Assert.IsNotNull (Mp4File.Read (AtStart (extended), null, null).Error); + } + + [TestMethod] + public void Read_MatroskaWithoutASegment_Fails () + { + var unknown = MatroskaFile.Read (AtStart (new byte[] { 0x1A, 0x45, 0xDF, 0xA3, 0xFF }), null, null); + Assert.IsFalse (unknown.IsSuccess); + StringAssert.Contains (unknown.Error, "unknown"); + + var none = MatroskaFile.Read (AtStart (new byte[] { 0x1A, 0x45, 0xDF, 0xA3, 0x81, 0x00 }), null, null); + Assert.IsFalse (none.IsSuccess); + StringAssert.Contains (none.Error, "no Segment"); + + var oversized = new byte[] { 0x18, 0x53, 0x80, 0x67, 0x43, 0xE8 }; + var clamped = MatroskaFile.Read (AtStart (oversized), null, null); + Assert.IsFalse (clamped.IsSuccess); + StringAssert.Contains (clamped.Error, "EBML"); + } + + [TestMethod] + public void Ebml_HeaderShorterThanAnIdOrASize_IsNotAnElement () + { + Assert.IsFalse (EbmlReader.TryPeekHeader (ReadOnlySpan.Empty, out _, out _, out _)); + Assert.IsFalse (EbmlReader.TryPeekHeader (new byte[] { 0x80 }, out _, out _, out _)); + } + + [TestMethod] + public void Read_DsfMetadataAndDataThatDisagree_Fail () + { + var pastEnd = TestBuilders.Dsf.CreateMinimal (); + BinaryPrimitives.WriteUInt64LittleEndian (pastEnd.AsSpan (20), (ulong)pastEnd.Length + 50); + var metadata = DsfFile.Read (AtStart (pastEnd), null, null); + Assert.IsFalse (metadata.IsSuccess); + StringAssert.Contains (metadata.Error, "Metadata offset"); + + var overlap = TestBuilders.Dsf.CreateMinimal (); + BinaryPrimitives.WriteUInt64LittleEndian (overlap.AsSpan (20), 90); + var crossed = DsfFile.Read (AtStart (overlap), null, null); + Assert.IsFalse (crossed.IsSuccess); + StringAssert.Contains (crossed.Error, "metadata"); + + var inflated = TestBuilders.Dsf.CreateMinimal (); + BinaryPrimitives.WriteUInt64LittleEndian (inflated.AsSpan (84), 50_000); + var data = DsfFile.Read (AtStart (inflated), null, null); + Assert.IsFalse (data.IsSuccess); + Assert.IsFalse (string.IsNullOrEmpty (data.Error)); + } + + [TestMethod] + public void Read_DffChunkPastTheEnd_Stops () + { + var bytes = new byte[28]; + "FRM8"u8.CopyTo (bytes); + "DSD "u8.CopyTo (bytes.AsSpan (12)); + "PROP"u8.CopyTo (bytes.AsSpan (16)); + BinaryPrimitives.WriteUInt64BigEndian (bytes.AsSpan (20), 1000); + + var opened = DffFile.Read (AtStart (bytes), "/song.dff", new MockFileSystem ()); + + Assert.IsFalse (opened.IsSuccess); + StringAssert.Contains (opened.Error, "FVER"); + } + + [TestMethod] + public void Read_MusepackSv7_ReadsTheFixedHeader () + { + var bytes = new byte[32]; + "MP+"u8.CopyTo (bytes); + + var opened = MusepackFile.Read (AtStart (bytes), null, null); + + Assert.IsFalse (opened.IsSuccess); + StringAssert.Contains (opened.Error, "unsupported SV7 version"); + } + + [TestMethod] + public void OggWindow_ContinuedAndTruncatedPages_Stop () + { + var continued = Concat ( + OggPage (0x00, new byte[] { 255 }, new byte[255]), + OggPage (0x01, new byte[] { 10 }, new byte[10]), + OggPage (0x00, new byte[] { 4 }, new byte[4])); + using var stream = new MemoryStream (continued) { Position = 2 }; + var headers = OggWindow.ReadHeaders (stream, OggHeaderKind.Vorbis); + Assert.IsTrue (headers.Length > 0); + + Assert.IsEmpty (OggWindow.ReadLastPage (new MemoryStream (new byte[8]))); + Assert.IsEmpty (OggWindow.ReadLastPage (new MemoryStream (new byte[40]))); + + var cut = new byte[27]; + "OggS"u8.CopyTo (cut); + cut[26] = 5; + Assert.IsEmpty (OggWindow.ReadHeaders (AtStart (cut), OggHeaderKind.Opus)); + + var tail = new byte[30]; + "OggS"u8.CopyTo (tail.AsSpan (20)); + Assert.IsEmpty (OggWindow.ReadLastPage (AtStart (tail))); + } + + static void SameWhenCopied (byte[] file, string name) + { + var direct = MediaFile.Read (new MemoryStream (file) { Position = 3 }, name); + var copied = MediaFile.Read (new NonSeekableStream (new MemoryStream (file)), name); + Assert.IsTrue (direct.IsSuccess, name + " " + direct.Error); + Assert.IsTrue (copied.IsSuccess, name + " " + copied.Error); + Assert.AreEqual (direct.Tag?.Title, copied.Tag?.Title, name); + var left = ((IMediaFile)direct.File!).AudioProperties?.Duration; + var right = ((IMediaFile)copied.File!).AudioProperties?.Duration; + Assert.AreEqual (left, right, name); + } + + static void AssertCopied (AiffFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (WavPackFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (MonkeysAudioFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (MusepackFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (DsfFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (DffFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (Mp4FileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (OggVorbisFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (OggOpusFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (OggFlacFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (AsfFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static void AssertCopied (MatroskaFileReadResult result) => Assert.IsTrue (result.IsSuccess, result.Error); + + static async Task ExpectNoSource (T? file, Func> save) + where T : class + { + Assert.IsNotNull (file); + var saved = await save (file); + Assert.IsFalse (saved.IsSuccess, saved.Error); + StringAssert.Contains (saved.Error, "No source path"); + } + + static async Task ExpectReread (T? file, Func save, Func> saveAsync) + where T : class + { + Assert.IsNotNull (file); + var sync = save (file); + Assert.IsFalse (sync.IsSuccess, sync.Error); + StringAssert.Contains (sync.Error, "re-read"); + var async = await saveAsync (file); + Assert.IsFalse (async.IsSuccess, async.Error); + StringAssert.Contains (async.Error, "re-read"); + } + + static MemoryStream AtStart (byte[] data) => new (data); + + static NonSeekableStream Copy (byte[] data) => new (new MemoryStream (data)); + + static byte[] Box (uint size, string type) + { + var bytes = new byte[8]; + BinaryPrimitives.WriteUInt32BigEndian (bytes, size); + Encoding.ASCII.GetBytes (type).CopyTo (bytes, 4); + return bytes; + } + + static byte[] MatroskaAlbum () + { + var title = Element (0x67C8, Concat (Element (0x45A3, "TITLE"u8), Element (0x4487, "Album"u8))); + var targets = Element (0x63C0, Element (0x68CA, new byte[] { 50 })); + var tag = Element (0x7373, Concat (targets, title)); + var tags = Element (0x1254C367, tag); + var segment = Element (0x18538067, tags); + var ebml = Element (0x1A45DFA3, Element (0x4282, "matroska"u8)); + return Concat (ebml, segment); + } + + static byte[] Element (ulong id, ReadOnlySpan payload) + { + var size = payload.Length < 0x7F + ? new[] { (byte)(0x80 | payload.Length) } + : new[] { (byte)(0x40 | (payload.Length >> 8)), (byte)payload.Length }; + var idBytes = new byte[4]; + for (var i = 3; i >= 0; i--) { + idBytes[i] = (byte)id; + id >>= 8; + } + + var width = idBytes[0] == 0 ? idBytes[1] == 0 ? idBytes[2] == 0 ? 1 : 2 : 3 : 4; + var result = new byte[width + size.Length + payload.Length]; + idBytes.AsSpan (4 - width).CopyTo (result); + size.CopyTo (result, width); + payload.CopyTo (result.AsSpan (width + size.Length)); + return result; + } + + static byte[] Concat (params byte[][] parts) + { + var length = 0; + foreach (var part in parts) + length += part.Length; + var result = new byte[length]; + var offset = 0; + foreach (var part in parts) { + part.CopyTo (result, offset); + offset += part.Length; + } + + return result; + } + + static byte[] OggPage (byte flags, byte[] segments, byte[] body) + { + var page = new byte[27 + segments.Length + body.Length]; + "OggS"u8.CopyTo (page); + page[5] = flags; + page[26] = (byte)segments.Length; + segments.CopyTo (page, 27); + body.CopyTo (page, 27 + segments.Length); + return page; + } + + sealed class ReportedStream : MemoryStream + { + readonly long _length; + + public ReportedStream (byte[] content, long length) : base (content) => _length = length; + + public override long Length => _length; + } + + sealed class NonSeekableStream : Stream + { + readonly Stream _inner; + + public NonSeekableStream (Stream inner) => _inner = inner; + + public override bool CanRead => true; + + public override bool CanSeek => false; + + public override bool CanWrite => false; + + public override long Length => throw new NotSupportedException (); + + public override long Position { + get => throw new NotSupportedException (); + set => throw new NotSupportedException (); + } + + public override void Flush () { } + + public override int Read (byte[] buffer, int offset, int count) => _inner.Read (buffer, offset, count); + + public override long Seek (long offset, SeekOrigin origin) => throw new NotSupportedException (); + + public override void SetLength (long value) => throw new NotSupportedException (); + + public override void Write (byte[] buffer, int offset, int count) => throw new NotSupportedException (); + } + + sealed class MissingDirectory : IFileSystem + { + public bool FileExists (string path) => true; + + public Stream OpenRead (string path) => throw new DirectoryNotFoundException (path); + + public Stream OpenReadWrite (string path) => throw new DirectoryNotFoundException (path); + + public Stream Create (string path) => throw new DirectoryNotFoundException (path); + + public byte[] ReadAllBytes (string path) => throw new DirectoryNotFoundException (path); + + public Task ReadAllBytesAsync (string path, CancellationToken cancellationToken = default) => + throw new DirectoryNotFoundException (path); + + public void WriteAllBytes (string path, ReadOnlySpan data) => throw new DirectoryNotFoundException (path); + + public Task WriteAllBytesAsync (string path, ReadOnlyMemory data, CancellationToken cancellationToken = default) => + throw new DirectoryNotFoundException (path); + + public void Delete (string path) => throw new DirectoryNotFoundException (path); + + public void Move (string sourcePath, string destinationPath) => throw new DirectoryNotFoundException (sourcePath); + + public string? GetDirectoryName (string path) => "/gone"; + + public string GetFileName (string path) => "song.mp3"; + + public string CombinePath (string path1, string path2) => path1 + path2; + } +} diff --git a/tests/TagLibSharp2.Tests/Core/WindowedReadEdgeTests.cs b/tests/TagLibSharp2.Tests/Core/WindowedReadEdgeTests.cs index 4cb4491..83ec2a1 100644 --- a/tests/TagLibSharp2.Tests/Core/WindowedReadEdgeTests.cs +++ b/tests/TagLibSharp2.Tests/Core/WindowedReadEdgeTests.cs @@ -1,9 +1,11 @@ // Copyright (c) 2025-2026 Stephen Shaw and contributors // Licensed under the MIT License. See LICENSE file in the project root for full license information. +using TagLibSharp2.Aiff; using TagLibSharp2.Core; using TagLibSharp2.Mpeg; using TagLibSharp2.Riff; +using TagLibSharp2.Tests; using TagLibSharp2.Xiph; namespace TagLibSharp2.Tests.Core; @@ -314,6 +316,23 @@ public async Task SaveToFileAsync_SkippedSamplesWithoutAPath_Fails () Assert.AreEqual ("Cannot save a WAV file whose sample data was not loaded.", saved.Error); } + [TestMethod] + public void Render_AiffSkippedSoundWithoutAPath_Throws () + { + var parsed = AiffFile.Read (new MemoryStream (AiffWithSound ()), null, null); + var error = Assert.ThrowsExactly (() => parsed.File!.Render ()); + Assert.AreEqual ("Cannot save an AIFF file whose sound data was not loaded.", error.Message); + } + + [TestMethod] + public async Task SaveToFileAsync_AiffSkippedSoundWithoutAPath_Fails () + { + var parsed = AiffFile.Read (new MemoryStream (AiffWithSound ()), null, null); + var saved = await parsed.File!.SaveToFileAsync ("/song.aiff"); + Assert.IsFalse (saved.IsSuccess); + Assert.AreEqual ("Cannot save an AIFF file whose sound data was not loaded.", saved.Error); + } + [TestMethod] public void Save_SourceShorterThanTheSkippedSamples_DoesNotWrite () { @@ -416,25 +435,25 @@ public void Save_DataChunkJustOverTheArrayLimit_FailsClosed () } [TestMethod] - public void Read_StreamLongerThanAnArray_ReturnsFailure () + public void Read_SeekableMatroskaLongerThanAnArray_DoesNotLoadTheFile () { using var stream = new ReportedLengthStream ((long)int.MaxValue + 8); var opened = MediaFile.Read (stream, "clip.mkv"); Assert.IsFalse (opened.IsSuccess); - StringAssert.Contains (opened.Error, "too large"); + StringAssert.Contains (opened.Error, "no Segment"); } [TestMethod] - public async Task ReadAsync_StreamLongerThanAnArray_ReturnsFailure () + public async Task ReadAsync_SeekableMatroskaLongerThanAnArray_DoesNotLoadTheFile () { using var stream = new ReportedLengthStream ((long)int.MaxValue + 8); var opened = await MediaFile.ReadAsync (stream, "clip.mkv"); Assert.IsFalse (opened.IsSuccess); - StringAssert.Contains (opened.Error, "too large"); + StringAssert.Contains (opened.Error, "no Segment"); } [TestMethod] @@ -581,6 +600,17 @@ static int IndexOf (byte[] data, byte[] pattern) return -1; } + static byte[] AiffWithSound () + { + var data = TestBuilders.Aiff.CreateMinimal (); + var size = (uint)(data.Length - 8); + data[4] = (byte)(size >> 24); + data[5] = (byte)(size >> 16); + data[6] = (byte)(size >> 8); + data[7] = (byte)size; + return data; + } + sealed class ThrowingFileSystem : IFileSystem { readonly Exception _error; diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs index 645583d..354b5de 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs @@ -618,6 +618,42 @@ public void Read_UnknownSizeCluster_DoesNotSwallowTheFollowingTags () Assert.AreEqual ("After", MatroskaFile.Read (file).File!.Tag.Title); } + [TestMethod] + public void Read_Stream_UnknownSizeCluster_KeepsTheFollowingTitle () + { + var cluster = Concat (Id (0x1F43B675), new byte[] { 0xFF }, Element (0xEC, new byte[4])); + var file = Matroska (Master (0x18538067, Concat (cluster, Tags (Tag (30, Simple ("TITLE", "After")))))); + + var opened = MatroskaFile.Read (new MemoryStream (file), null, null); + + Assert.IsTrue (opened.IsSuccess, opened.Error); + Assert.AreEqual ("After", opened.File!.Tag.Title); + } + + [TestMethod] + public void Read_Stream_UnknownSizeSeekHead_KeepsTheFollowingTitle () + { + var seek = Concat (Id (MatroskaIds.SeekHead), new byte[] { 0xFF }); + var file = Matroska (Master (0x18538067, Concat (seek, Tags (Tag (30, Simple ("TITLE", "After")))))); + + var opened = MatroskaFile.Read (new MemoryStream (file), null, null); + + Assert.IsTrue (opened.IsSuccess, opened.Error); + Assert.AreEqual ("After", opened.File!.Tag.Title); + } + + [TestMethod] + public void Read_Stream_KnownCluster_KeepsTheFollowingTitle () + { + var cluster = Master (0x1F43B675, Element (0xEC, new byte[4])); + var file = Matroska (Master (0x18538067, Concat (cluster, Tags (Tag (30, Simple ("TITLE", "After")))))); + + var opened = MatroskaFile.Read (new MemoryStream (file), null, null); + + Assert.IsTrue (opened.IsSuccess, opened.Error); + Assert.AreEqual ("After", opened.File!.Tag.Title); + } + [TestMethod] public void Read_WebM_ReadsTags () { diff --git a/tests/TagLibSharp2.Tests/Mpeg/MpegAudioPropertiesTests.cs b/tests/TagLibSharp2.Tests/Mpeg/MpegAudioPropertiesTests.cs index c1d4b87..d043ac5 100644 --- a/tests/TagLibSharp2.Tests/Mpeg/MpegAudioPropertiesTests.cs +++ b/tests/TagLibSharp2.Tests/Mpeg/MpegAudioPropertiesTests.cs @@ -407,6 +407,40 @@ public void Duration_XingByteCountPastEnd_UsesFramesPresent () Assert.AreEqual ((uint)framesPresent, props.FrameCount); } + [TestMethod] + public void Duration_XingByteCountCoversTheTag_UsesFrameCount () + { + // LAME stores the Xing byte count as the whole stream size, so it includes + // the bytes before the first audio frame. That count is past the audio that + // follows the frame and within the file. 100 * 1152 / 44100 = 2.612 s. + // Three frames are physically present; the header's frame count stands. + const int tagBytes = 200; + const int frameSize = 417; + const int framesPresent = 3; + const int xingFrames = 100; + var fileLength = tagBytes + frameSize * framesPresent; + var builder = new BinaryDataBuilder (); + builder.Add (new byte[tagBytes]); + builder.Add (0xFF, 0xFB, 0x90, 0x00); + builder.Add (new byte[32]); + builder.Add (0x58, 0x69, 0x6E, 0x67); // "Xing" + builder.AddUInt32BE (0x03); + builder.AddUInt32BE (xingFrames); + builder.AddUInt32BE ((uint)fileLength); + while (builder.Length < fileLength) + builder.Add (0); + StampFrameHeader (builder, tagBytes + frameSize); + StampFrameHeader (builder, tagBytes + frameSize * 2); + + var parsed = MpegAudioProperties.TryParse ( + builder.ToBinaryData (), tagBytes, fileLength, out var props, out var needsEntireFile); + + Assert.IsTrue (parsed); + Assert.IsFalse (needsEntireFile); + Assert.AreEqual (100 * 1152.0 / 44100, props!.Duration.TotalSeconds, 0.001); + Assert.AreEqual ((uint)xingFrames, props.FrameCount); + } + [TestMethod] public void Duration_LongTruncatedXing_CountsTheFramesThatArePresent () {