Skip to content
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- A crafted ID3v2 tag of many small compressed frames that each declare a large decompressed size no longer makes a read or a save allocate without bound (#275). One read of a file now inflates at most 64 MiB in all, shared by every compressed frame of every ID3v2 tag it parses: top-level frames, those CHAP and CTOC embed, and every ID3 chunk of an AIFF and the tag chunks after a WAV or AIFF, which are recognised as tags without inflating anything; a save re-encodes embedded frames within 16 MiB. The per-frame limits stay 16 MiB and 1,100:1; there was no limit across the tag or the file, so 50 frames of 16 MiB allocated about 800 MB to read, 40 ID3 chunks in an AIFF about 1.9 GB, and a save of an 819 KB tag rendered 839 MB; that save now allocates about 165 MB. A frame past the budget is not decoded and is written back as stored, byte for byte. A save whose ID3v2 tag, or one of its frames, would be larger than an ID3v2 size can hold (0x0FFFFFFF) now fails with a result that says so and writes nothing, in MP3, raw AAC, MPEG-PS, WAV, AIFF, DSF, DFF and AVI, where it threw `ArgumentOutOfRangeException` from the size writer; the check renders the tag, with the padding the carrier writes it with, only when a bound on its size, taken from the frames without rendering them, could pass the limit. A save that renders the tag as it was read, to see whether the caller changed it (MPEG-PS, AVI, and WAV or AIFF with a tag chunk after the form), takes a tag as read that is too large to render as changed, so an edit that makes the tag smaller saves. `Id3v2Tag.Render` and a carrier's `Render` throw an `InvalidOperationException` with that message
- An ID3v2.3 frame that is both compressed and grouped reads (#272). ID3v2.3 §3.3.1 puts the extra bytes in the flags' order, the decompressed size before the group byte, and the group byte was read first, so the size was read from the wrong bytes and a frame that compresses well was rejected. ID3v2.4, which puts the group byte first, is unchanged. This reaches the frames CHAP and CTOC embed too
- An ID3v2 URL frame (W*** and the URL of WXXX) whose URL has characters outside ISO-8859-1 writes each of them percent-encoded as UTF-8, as an IRI maps to a URI (RFC 3987 §3.1), where each was written as `?` (#266). A URL that ISO-8859-1 holds is written as given, and a character from U+0080 to U+00FF stays one ISO-8859-1 byte; a read gives back the text written, in its encoded form. The frame's `Url` keeps what was set until the file is read again
- A save keeps every frame an ID3v2 CHAP or CTOC embeds, such as a chapter's subtitle (TIT3), link (WXXX) or image (APIC), and frames this library does not know (#265). Only the embedded title (TIT2) was kept, and every other embedded frame was dropped on any save. A save that does not change the chapter's title writes the CHAP or CTOC as it was read; a new title is written as a new TIT2 where the old one was, and the other frames as they were stored, with their flags. An embedded frame read from a v2.3 tag in a text encoding v2.3 does not define, such as a chapter title 0.6 and earlier wrote in UTF-8, is written again in ISO-8859-1 or UTF-16 with a BOM, as the tag's other frames are (#255), keeping its status flags and group byte but not its compression; deciding this inflates a compressed frame's first byte only, and a frame that cannot be re-encoded is kept as stored. A chapter moved to a tag of another version writes each embedded frame this library reads as that version writes it, and any other frame with its content as its format flags give it and no flags; an encrypted one is left out
- An ID3v2 frame in ISO-8859-1 given text that ISO-8859-1 cannot hold is written in an encoding that holds it, UTF-16 with a BOM in an ID3v2.3 tag and UTF-8 in an ID3v2.4 tag, where each such character was written as `?` (#262). This reaches a frame read from a file in ISO-8859-1 whose `Text`, `Description`, `Value`, `FileName` or pairs are then changed, and a frame built or set to `TextEncodingType.Latin1` with such text: text frames, `TXXX`, `COMM`, `USLT`, `WXXX` descriptions, `GEOB`, `SYLT`, the involved-people lists and `APIC` descriptions. A frame's own `RenderContent ()` writes such text in UTF-8, as a v2.4 tag does
- A chapter or table-of-contents title in an ID3v2.3 tag whose TIT2 is 128 bytes or longer reads back (#261). The TIT2 that CHAP and CTOC embed was written with a syncsafe size, which only ID3v2.4 uses, and read with a big-endian one in v2.3, so the title was lost. The embedded frames now take the tag's own frame header when written, and are read with the same frame-header and frame-flag rules as the tag's top-level frames, so an embedded TIT2 that is grouped, compressed, unsynchronised or has a data length indicator reads too. A v2.3 file an earlier version wrote reads, since a v2.3 size that does not fit is read as syncsafe, as for a top-level frame
- A frame written into an ID3v2.3 tag, or a v2.2 tag (which is written as v2.3), uses a text encoding ID3v2.3 defines: ISO-8859-1 when its text fits, otherwise UTF-16 with a BOM (#255). New frames start as UTF-8, which only ID3v2.4 defines, and were written with that encoding byte whatever the tag's version. So a picture added to an MP3 whose tag is ID3v2.3, as the QuickStart's "add album art" sample does, saved, but this library's own read of the file found no picture, because the v2.3 APIC reader rejects encoding 3. The text frames, `TXXX`, `COMM`, `USLT`, `WXXX`, `GEOB`, `SYLT`, the involved-people lists and the title inside `CHAP` and `CTOC` had the same encoding byte, which the other readers accepted. The tag's version now chooses the encoding when the tag is written, for every frame and every save path, in MP3, WAV, AIFF, DSF, DFF, raw AAC, MPEG-PS and AVI. A frame read from a v2.3 tag in UTF-8 or UTF-16BE is written back in ISO-8859-1 or UTF-16 too; one in ISO-8859-1 or UTF-16 keeps that encoding and is written as before. `SetCustomField` on a v2.3 tag now writes ISO-8859-1 when the values fit, where it always wrote UTF-16. An ID3v2.4 tag still writes UTF-8. A frame object keeps the encoding it was given, and its own `RenderContent ()` still writes that one. A v2.3 picture in UTF-8 or UTF-16BE, such as one an earlier version of this library wrote, is now read, as the other v2.3 frames are, and the next save rewrites it in an encoding v2.3 defines. The `Id3v2Tag` class example now compiles: its picture, custom-field and comment snippets used a wrong argument order and members that do not exist
- A Matroska save no longer grows quadratically with the pictures a file holds, and the Tags read is bounded (#247). Each picture now finds its own attachment by its content in an ordered index, the UIDs and names a save keeps are in ordered sets, and new pictures of one type are numbered in one pass, so no content a file holds, its attachments' data or UIDs included, can make a hash collide; setting the pictures back on a file with many image attachments took time quadratic in them. A read models at most 65,536 Tags and SimpleTags, as Chapters bounds its entries; the Tags past that are kept as their bytes and written back unread. A save that would write a Tag, a SimpleTag, a chapter or a chapter's value past what a read models (a new Tag or chapter in a file at the budget, or an edit that pushes the last one read, or only its title, language, times or flags, past it) now fails with a result saying so, and leaves the file and the object's edits as they were; it used to report success for a value no read could show. A chapter title edit at the budget counts too when the display was read without a language, as the save writes the default "eng". An edit that writes as many elements, such as a value replaced in place, is saved.
- A second save of a Matroska file from the same object writes a clear or an undone edit (#244). Read a file, set `Title`, save it over its source, then clear `Title` and save again: the file kept the title. A save left the `Tags` element as it was when the tags matched the ones the object first read, but after a save over the source the file holds the saved tags. The same held for every tag field, and for a chapter edit undone, or a chapter or edition added and then removed. A save now compares with the `Tags` and `Chapters` elements in the bytes it rewrites, on all six save entry points. A chapter read without a `ChapterUID`, or with 0, now reads `Uid` 0 and is written as it was (a read gave it a random UID, so no two reads of the file were the same); a save that writes the chapter changed gives it a new random UID, as RFC 9559 requires one that is not 0, and the chapter keeps it once a save over the source has written it. Setting `MatroskaChapter.Uid` to 0 now means the same, no UID yet, where it threw. After a save over its source the object holds the tags and chapters it wrote, as a read of the saved file would, so a later save writes what a fresh object would; a save that fails or goes elsewhere leaves it as it was. A UID the library draws for a new chapter, edition or picture attachment is now always 8 bytes wide, so the size of a save no longer depends on the draw. A save finds the empty Tags it keeps by their content in an ordered set, in O(log n) comparisons each whatever the Tags hold, with no hash a file could make collide.
- A Matroska file whose only title is a `TITLE` in a Tag with an empty Targets, such as the Matroska test suite's test1.mkv, no longer reads that title as its `Album` too (#221). A Tag with no `TargetTypeValue` is target 50 by default, but ffprobe, exiftool and MediaInfo show its `TITLE` as the title and no album, so `Album` now reads an explicit target-50 `TITLE`, then ffmpeg's `ALBUM`, and nothing else; clearing the album leaves that `TITLE`, and `AlbumSort` no longer reads the sort nested in it, which `TitleSort` now reads. Setting `Title` on such a file replaced nothing: it added a `TITLE` at target 30 and kept the old one, which exiftool, showing the first, still showed (and `Album` still read), with a segment title too, and when a save left out an Info/Title too long to rewrite in place. The new title now replaces the untargeted `TITLE` in place, in the language `Title` reads (a `TITLE` in another language stays), and the segment title with it; where a narrower `TITLE` or an explicit album `TITLE` could be the one a reader shows, the title is also written at target 30, with its languages and sort, so ffprobe, exiftool and MediaInfo show only the new title. An album edit keeps the title's sort.
Expand Down
164 changes: 97 additions & 67 deletions src/TagLibSharp2/Aac/AacFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,22 +495,27 @@ async Task<FileWriteResult> Adopt (Task<FileWriteResult> save, Written written,
/// <returns>A result indicating success or failure.</returns>
public FileWriteResult SaveToFile (string path, ReadOnlySpan<byte> originalData, IFileSystem? fileSystem = null)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);

if (Source.Refuse ("AAC", originalData.Length) is { } mismatch)
return mismatch;

var written = Plan (new AacTailSource (originalData));
var rendered = Assemble (originalData, written);
if (rendered.IsEmpty)
return FileWriteResult.Failure ("Failed to render AAC file");
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);

if (Source.Refuse ("AAC", originalData.Length) is { } mismatch)
return mismatch;

var written = Plan (new AacTailSource (originalData));
var rendered = Assemble (originalData, written);
if (rendered.IsEmpty)
return FileWriteResult.Failure ("Failed to render AAC file");

fileSystem ??= _sourceFileSystem;
return Adopt (AtomicFileWriter.Write (path, rendered.Span, fileSystem), written, fileSystem);
fileSystem ??= _sourceFileSystem;
return Adopt (AtomicFileWriter.Write (path, rendered.Span, fileSystem), written, fileSystem);
} catch (Id3v2TagTooLargeException tooLarge) {
return FileWriteResult.Failure (tooLarge.Message);
}
}

/// <summary>
Expand All @@ -529,22 +534,27 @@ public Task<FileWriteResult> SaveToFileAsync (
IFileSystem? fileSystem = null,
CancellationToken cancellationToken = default)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));

if (Source.Refuse ("AAC", originalData.Length) is { } mismatch)
return Task.FromResult (mismatch);

var written = Plan (new AacTailSource (originalData.Span));
var rendered = Assemble (originalData.Span, written);
if (rendered.IsEmpty)
return Task.FromResult (FileWriteResult.Failure ("Failed to render AAC file"));

fileSystem ??= _sourceFileSystem;
return Adopt (AtomicFileWriter.WriteAsync (path, rendered.Memory, fileSystem, cancellationToken), written, fileSystem);
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));

if (Source.Refuse ("AAC", originalData.Length) is { } mismatch)
return Task.FromResult (mismatch);

var written = Plan (new AacTailSource (originalData.Span));
var rendered = Assemble (originalData.Span, written);
if (rendered.IsEmpty)
return Task.FromResult (FileWriteResult.Failure ("Failed to render AAC file"));

fileSystem ??= _sourceFileSystem;
return Adopt (AtomicFileWriter.WriteAsync (path, rendered.Memory, fileSystem, cancellationToken), written, fileSystem);
} catch (Id3v2TagTooLargeException tooLarge) {
return Task.FromResult (FileWriteResult.Failure (tooLarge.Message));
}
}

/// <summary>
Expand All @@ -559,15 +569,20 @@ public Task<FileWriteResult> SaveToFileAsync (
/// <returns>A result indicating success or failure.</returns>
public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);
if (string.IsNullOrEmpty (SourcePath))
return FileWriteResult.Failure ("No source path available. File was not read from disk.");

return SaveFromSource (path, fileSystem ?? _sourceFileSystem);
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);
if (string.IsNullOrEmpty (SourcePath))
return FileWriteResult.Failure ("No source path available. File was not read from disk.");

return SaveFromSource (path, fileSystem ?? _sourceFileSystem);
} catch (Id3v2TagTooLargeException tooLarge) {
return FileWriteResult.Failure (tooLarge.Message);
}
}

/// <summary>
Expand All @@ -577,15 +592,20 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null)
/// <returns>A result indicating success or failure.</returns>
public FileWriteResult SaveToFile (IFileSystem? fileSystem = null)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);
if (string.IsNullOrEmpty (SourcePath))
return FileWriteResult.Failure ("No source path available. File was not read from disk.");

return SaveToFile (SourcePath!, fileSystem);
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return denied;
if (Id3v1Refusal () is string refusal)
return FileWriteResult.Failure (refusal);
if (string.IsNullOrEmpty (SourcePath))
return FileWriteResult.Failure ("No source path available. File was not read from disk.");

return SaveToFile (SourcePath!, fileSystem);
} catch (Id3v2TagTooLargeException tooLarge) {
return FileWriteResult.Failure (tooLarge.Message);
}
}

/// <summary>
Expand All @@ -604,15 +624,20 @@ public Task<FileWriteResult> SaveToFileAsync (
IFileSystem? fileSystem = null,
CancellationToken cancellationToken = default)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));
if (string.IsNullOrEmpty (SourcePath))
return Task.FromResult (FileWriteResult.Failure ("No source path available. File was not read from disk."));

return Task.FromResult (SaveFromSource (path, fileSystem ?? _sourceFileSystem, cancellationToken));
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));
if (string.IsNullOrEmpty (SourcePath))
return Task.FromResult (FileWriteResult.Failure ("No source path available. File was not read from disk."));

return Task.FromResult (SaveFromSource (path, fileSystem ?? _sourceFileSystem, cancellationToken));
} catch (Id3v2TagTooLargeException tooLarge) {
return Task.FromResult (FileWriteResult.Failure (tooLarge.Message));
}
}

// Same bytes as Render. The audio and any Lyrics3 block are copied; the tags are rendered.
Expand Down Expand Up @@ -673,15 +698,20 @@ public Task<FileWriteResult> SaveToFileAsync (
IFileSystem? fileSystem = null,
CancellationToken cancellationToken = default)
{
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));
if (string.IsNullOrEmpty (SourcePath))
return Task.FromResult (FileWriteResult.Failure ("No source path available. File was not read from disk."));

return SaveToFileAsync (SourcePath!, fileSystem, cancellationToken);
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
_creation?.Settle ([Id3v2Tag, ApeTag, Id3v1Tag]);
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return Task.FromResult (denied);
if (Id3v1Refusal () is string refusal)
return Task.FromResult (FileWriteResult.Failure (refusal));
if (string.IsNullOrEmpty (SourcePath))
return Task.FromResult (FileWriteResult.Failure ("No source path available. File was not read from disk."));

return SaveToFileAsync (SourcePath!, fileSystem, cancellationToken);
} catch (Id3v2TagTooLargeException tooLarge) {
return Task.FromResult (FileWriteResult.Failure (tooLarge.Message));
}
}

/// <summary>
Expand Down
Loading
Loading