Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHDSharpBattleTest/CHDSharpBattleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion CHDSharpBenchmark/CHDSharpBenchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions CHDSharpCli/CHDSharpCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ProjectReference Include="..\CHDSharpLib\CHDSharpLib.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -20,7 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="4.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="11.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions CHDSharpCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions CHDSharpEncoderTest/CHDSharpEncoderTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.1.0">
<PackageReference Include="coverlet.collector" Version="10.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
23 changes: 23 additions & 0 deletions CHDSharpEncoderTest/CdflCodecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion CHDSharpLib/CHDSharpLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
9 changes: 8 additions & 1 deletion CHDSharpLib/Encoder/AvHuffEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ internal sealed class AvHuffEncoder
{
private const int RleAlphabetSize = 256 + 16;
private const int MaxBits = 16;

/// <summary>
/// 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.
/// </summary>
private const int FlacFrameHeaderAllowance = 512;
private readonly DeltaRleEncoder _cbContext = new();
private readonly DeltaRleEncoder _crContext = new();

Expand Down Expand Up @@ -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++)
Expand Down
13 changes: 11 additions & 2 deletions CHDSharpLib/Encoder/CdflCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ namespace CHDSharp.Encoder;
/// </summary>
public sealed class CdflCodec : IChdCodec
{
/// <summary>
/// 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".
/// </summary>
private const int FlacFrameHeaderAllowance = 512;

private readonly int _blockSize;
private readonly int _dataBytes;
private readonly byte[] _flacBuffer;
Expand All @@ -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];
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions CHDSharpTest/CHDSharpTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.1.0">
<PackageReference Include="coverlet.collector" Version="10.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
17 changes: 17 additions & 0 deletions CHDSharpTest/CliIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file> --output <file>",
log,
StringComparison.Ordinal
);
Assert.DoesNotContain("Unhandled exception", log, StringComparison.Ordinal);
}

[Fact]
public void Createcd_rejects_iso_size_that_is_not_sector_aligned()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Using Include="CHDSharp.Models" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions CHDSharpTester/CHDSharpTester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ProjectReference Include="..\CHDSharpLib\CHDSharpLib.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -20,7 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="4.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="11.0.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageReference Include="WPF-UI" Version="4.3.0" />
Expand Down
2 changes: 1 addition & 1 deletion VendoredFlac/VendoredFlac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>
<ItemGroup></ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion VendoredLZMA/VendoredLZMA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>
<ItemGroup></ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion VendoredZLib/VendoredZLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>
<ItemGroup></ItemGroup>
<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion VendoredZSTD/VendoredZSTD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PackageReference>
<PackageReference Include="InlineIL.Fody" Version="1.10.2" PrivateAssets="all" />
<PackageReference Include="InlineMethod.Fody" Version="0.8.7" PrivateAssets="all" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.339">
<PackageReference Include="Meziantou.Analyzer" Version="3.0.341">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading