Skip to content

Commit

Permalink
[Core] Fixed ZLib validation of LightAppEntity.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 14, 2024
1 parent 0ed2cb3 commit bf0b93b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Lagrange.Core/Message/Entity/LightAppEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ IEnumerable<Elem> IMessageEntity.PackElement()
{
if (elems.LightAppElem is { } lightApp)
{
var payload = ZCompression.ZDecompress(lightApp.Data[1..]);
var payload = ZCompression.ZDecompress(lightApp.Data[1..], false);
string json = Encoding.UTF8.GetString(payload);
string? app = JsonNode.Parse(json)?["app"]?.ToString();

Expand Down
5 changes: 3 additions & 2 deletions Lagrange.Core/Utility/Binary/Compression/ZCompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public static byte[] ZCompress(byte[] data, byte[]? header = null)

public static byte[] ZCompress(string data, byte[]? header = null) => ZCompress(Encoding.UTF8.GetBytes(data), header);

public static byte[] ZDecompress(byte[] data)
public static byte[] ZDecompress(byte[] data, bool validate = true)
{
var checksum = data[^4..];

var inflate = Common.Inflate(data[2..^4]);
return checksum.SequenceEqual(Adler32(inflate)) ? inflate : throw new Exception("Checksum mismatch");
if (validate) return checksum.SequenceEqual(Adler32(inflate)) ? inflate : throw new Exception("Checksum mismatch");
return inflate;
}

private static byte[] Adler32(byte[] data)
Expand Down

0 comments on commit bf0b93b

Please sign in to comment.