Skip to content

Add speculative support for zstd compressed chunks #1504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 SteamKit2/SteamKit2/Steam/CDN/DepotChunk.cs
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ public static int Process( DepotManifest.ChunkData info, ReadOnlySpan<byte> data

if ( buffer[ 0 ] == 'V' && buffer[ 1 ] == 'S' && buffer[ 2 ] == 'Z' && buffer[ 3 ] == 'a' ) // Zstd
{
throw new NotImplementedException( "Zstd compressed chunks are not yet implemented in SteamKit." );
writtenDecompressed = VZstdUtil.Decompress( buffer.AsSpan( 0, written ), destination );
}
else if ( buffer[ 0 ] == 'V' && buffer[ 1 ] == 'Z' && buffer[ 2 ] == 'a' ) // LZMA
{
1 change: 1 addition & 0 deletions SteamKit2/SteamKit2/SteamKit2.csproj
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
</PackageReference>
<PackageReference Include="protobuf-net" Version="3.2.46" />
<PackageReference Include="System.IO.Hashing" Version="9.0.2" />
<PackageReference Include="ZstdSharp.Port" Version="0.8.4" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions SteamKit2/SteamKit2/Util/VZstdUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Buffers;
using System.IO;
using System.Runtime.InteropServices;

namespace SteamKit2
{
static class VZstdUtil
{
private const uint VZstdHeader = 0x56535A61;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to 0x615A5356 since endianness is "reversed" in number literals, i.e the 2 rightmost characters is the value of the first byte


public static int Decompress( ReadOnlySpan<byte> buffer, byte[] destination )
{
if ( MemoryMarshal.Read<uint>( buffer ) != VZstdHeader )
{
throw new InvalidDataException( "Expecting VZstdHeader at start of stream" );
}

var sizeCompressed = MemoryMarshal.Read<int>( buffer[ ^15.. ] ); // TODO: I am not convinced this is correct -- maybe its the frame size
var sizeDecompressed = MemoryMarshal.Read<int>( buffer[ ^11.. ] );

if ( buffer[ ^3 ] != 'z' || buffer[ ^2 ] != 's' || buffer[ ^1 ] != 'v' )
{
throw new InvalidDataException( "Expecting VZstdFooter at end of stream" );
}

if ( destination.Length < sizeDecompressed )
{
throw new ArgumentException( "The destination buffer is smaller than the decompressed data size.", nameof( destination ) );
}

using var zstdDecompressor = new ZstdSharp.Decompressor();

var input = buffer[ 4..^12 ];

if ( !zstdDecompressor.TryUnwrap( input, destination, out var sizeWritten ) || sizeDecompressed != sizeWritten )
{
throw new InvalidDataException( $"Failed to decompress Zstd (expected {sizeDecompressed} bytes, got {sizeWritten})." );
}

return sizeDecompressed;
}
}
}