diff --git a/CHDSharpBattleTest/CHDSharpBattleTest.csproj b/CHDSharpBattleTest/CHDSharpBattleTest.csproj index 2db9add..182f9f0 100644 --- a/CHDSharpBattleTest/CHDSharpBattleTest.csproj +++ b/CHDSharpBattleTest/CHDSharpBattleTest.csproj @@ -68,7 +68,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpBenchmark/CHDSharpBenchmark.csproj b/CHDSharpBenchmark/CHDSharpBenchmark.csproj index d977aa5..7c7507f 100644 --- a/CHDSharpBenchmark/CHDSharpBenchmark.csproj +++ b/CHDSharpBenchmark/CHDSharpBenchmark.csproj @@ -9,7 +9,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpCli/CHDSharpCli.csproj b/CHDSharpCli/CHDSharpCli.csproj index 88b58c7..3c75451 100644 --- a/CHDSharpCli/CHDSharpCli.csproj +++ b/CHDSharpCli/CHDSharpCli.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,7 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/CHDSharpCli/Program.cs b/CHDSharpCli/Program.cs index 0cdc7ca..930e43a 100644 --- a/CHDSharpCli/Program.cs +++ b/CHDSharpCli/Program.cs @@ -4969,6 +4969,14 @@ private static (string? input, string? output, string[] rest) ParseCreateArgs(st rest.RemoveAt(0); } + // An explicitly empty argument (`-o ""`) is a missing path, not a valid one: passing it + // through used to reach Path.GetDirectoryName("")/Directory.CreateDirectory("") and crash + // with ArgumentException instead of printing the usage error. + if (string.IsNullOrWhiteSpace(input)) + input = null; + if (string.IsNullOrWhiteSpace(output)) + output = null; + return (input, output, rest.ToArray()); } diff --git a/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj b/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj index 20f6d51..a4eceb8 100644 --- a/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj +++ b/CHDSharpEncoderTest/CHDSharpEncoderTest.csproj @@ -8,11 +8,11 @@ $(NoWarn);xUnit1051 - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpEncoderTest/CdflCodecTests.cs b/CHDSharpEncoderTest/CdflCodecTests.cs index b0ba063..d537085 100644 --- a/CHDSharpEncoderTest/CdflCodecTests.cs +++ b/CHDSharpEncoderTest/CdflCodecTests.cs @@ -73,6 +73,29 @@ public void CdflCodec_Silence_CompressesWell() Assert.True(compressed.Length < 512); } + [Fact] + public void CdflCodec_OneFrameHunk_EncodesWithoutOverflowingTheFrameBuffer() + { + // A CHD with one CD frame per hunk makes the FLAC blocksize 588 samples. Fixed + // subframes can need more than 64 bytes of frame metadata, which used to overflow the + // per-hunk buffer ("FLAC frame too large: 2419 bytes (buffer 2416)"). + var hunk = new byte[CdConstants.FrameSize]; + for (var s = 0; s < 588; s++) + { + var sample = (int)(Math.Sin(s * 0.19) * 30000); + var offset = s * 4; + hunk[offset] = (byte)(sample >> 8); + hunk[offset + 1] = (byte)sample; + hunk[offset + 2] = (byte)(sample >> 8); + hunk[offset + 3] = (byte)sample; + } + + var codec = new CdflCodec(CdConstants.FrameSize); + var compressed = codec.Compress(hunk); + + Assert.NotNull(compressed); + } + [Fact] public void EncodeCd_WithCdfl_RoundTripsThroughChdSharpLib() { diff --git a/CHDSharpLib/CHDSharpLib.csproj b/CHDSharpLib/CHDSharpLib.csproj index 58b3446..8787f5c 100644 --- a/CHDSharpLib/CHDSharpLib.csproj +++ b/CHDSharpLib/CHDSharpLib.csproj @@ -29,7 +29,7 @@ true - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpLib/Encoder/AvHuffEncoder.cs b/CHDSharpLib/Encoder/AvHuffEncoder.cs index 5a24455..a611d13 100644 --- a/CHDSharpLib/Encoder/AvHuffEncoder.cs +++ b/CHDSharpLib/Encoder/AvHuffEncoder.cs @@ -16,6 +16,13 @@ internal sealed class AvHuffEncoder { private const int RleAlphabetSize = 256 + 16; private const int MaxBits = 16; + + /// + /// Extra space reserved for a per-channel FLAC frame's header, subframe headers, Rice + /// parameters and footer. Fixed subframes can need more than 64 bytes (real CD audio + /// frames showed 67), so the earlier 64-byte allowance could overflow the encoder. + /// + private const int FlacFrameHeaderAllowance = 512; private readonly DeltaRleEncoder _cbContext = new(); private readonly DeltaRleEncoder _crContext = new(); @@ -171,7 +178,7 @@ int dstOffs dest[8] = 0xFF; dest[9] = 0xFF; - var flacOut = new byte[samples * 2 + 64]; + var flacOut = new byte[samples * 2 + FlacFrameHeaderAllowance]; var pcm = new short[samples]; for (var ch = 0; ch < channels; ch++) diff --git a/CHDSharpLib/Encoder/CdflCodec.cs b/CHDSharpLib/Encoder/CdflCodec.cs index a7e0266..4483619 100644 --- a/CHDSharpLib/Encoder/CdflCodec.cs +++ b/CHDSharpLib/Encoder/CdflCodec.cs @@ -14,6 +14,14 @@ namespace CHDSharp.Encoder; /// public sealed class CdflCodec : IChdCodec { + /// + /// Extra space reserved per FLAC frame for its header, subframe headers, Rice + /// partition parameters and footer. A frame's overhead can exceed 64 bytes (real + /// reports show 67 bytes for a 2352-byte CD frame with Fixed subframes), so 64 was + /// too tight and made valid audio fail with "FLAC frame too large". + /// + private const int FlacFrameHeaderAllowance = 512; + private readonly int _blockSize; private readonly int _dataBytes; private readonly byte[] _flacBuffer; @@ -38,8 +46,9 @@ public CdflCodec(uint hunkBytes) _blockSize /= 2; _leBuffer = new byte[_dataBytes]; - // worst case: verbatim subframes; add room for frame headers - _flacBuffer = new byte[_dataBytes + _framesPerHunk * 64]; + // Worst case: fixed/verbatim subframes; reserve room for the frame metadata of every + // frame (see FlacFrameHeaderAllowance). + _flacBuffer = new byte[_dataBytes + _framesPerHunk * FlacFrameHeaderAllowance]; } /// diff --git a/CHDSharpTest/CHDSharpTest.csproj b/CHDSharpTest/CHDSharpTest.csproj index 13533c7..43e5a9f 100644 --- a/CHDSharpTest/CHDSharpTest.csproj +++ b/CHDSharpTest/CHDSharpTest.csproj @@ -9,11 +9,11 @@ $(NoWarn);xUnit1051 - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpTest/CliIntegrationTests.cs b/CHDSharpTest/CliIntegrationTests.cs index 7f7ebf1..b757ac5 100644 --- a/CHDSharpTest/CliIntegrationTests.cs +++ b/CHDSharpTest/CliIntegrationTests.cs @@ -197,6 +197,23 @@ public void Createdvd_reports_usage_error_when_arguments_are_missing() ); } + [Fact] + public void Extractcd_reports_usage_error_for_an_empty_output_path() + { + // `-o ""` used to pass the null check, reach Directory.CreateDirectory("") and crash with + // an unhandled ArgumentException instead of printing the usage text. + var path = Path.Combine(TestDataDir, "v5_cd_default.chd"); + var (exitCode, log) = RunCli("extractcd", "--input", path, "--output", "\"\""); + + Assert.Equal(1, exitCode); + Assert.Contains( + "extractcd requires --input --output ", + log, + StringComparison.Ordinal + ); + Assert.DoesNotContain("Unhandled exception", log, StringComparison.Ordinal); + } + [Fact] public void Createcd_rejects_iso_size_that_is_not_sector_aligned() { diff --git a/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj b/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj index 678d746..b122c47 100644 --- a/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj +++ b/CHDSharpTestDataGeneration/CHDSharpTestDataGeneration.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CHDSharpTester/CHDSharpTester.csproj b/CHDSharpTester/CHDSharpTester.csproj index 462e055..b234b1b 100644 --- a/CHDSharpTester/CHDSharpTester.csproj +++ b/CHDSharpTester/CHDSharpTester.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,7 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/VendoredFlac/VendoredFlac.csproj b/VendoredFlac/VendoredFlac.csproj index 05a54a1..72177ee 100644 --- a/VendoredFlac/VendoredFlac.csproj +++ b/VendoredFlac/VendoredFlac.csproj @@ -10,7 +10,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredLZMA/VendoredLZMA.csproj b/VendoredLZMA/VendoredLZMA.csproj index 05e8820..f4f2c11 100644 --- a/VendoredLZMA/VendoredLZMA.csproj +++ b/VendoredLZMA/VendoredLZMA.csproj @@ -10,7 +10,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredZLib/VendoredZLib.csproj b/VendoredZLib/VendoredZLib.csproj index c44552f..f490f48 100644 --- a/VendoredZLib/VendoredZLib.csproj +++ b/VendoredZLib/VendoredZLib.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/VendoredZSTD/VendoredZSTD.csproj b/VendoredZSTD/VendoredZSTD.csproj index 10cb30a..5bd0f6e 100644 --- a/VendoredZSTD/VendoredZSTD.csproj +++ b/VendoredZSTD/VendoredZSTD.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive