Skip to content

Commit 49e3667

Browse files
committed
zstd.go: Set ZSTD_LEGACY_SUPPORT=4 to decompress legacy payloads
The compressed output format was finalized with zstd version 0.8. To support decompression of previous versions, set the macro ZSTD_LEGACY_SUPPORT=4, which matches the documented default for the zstd command line tool: https://github.com/facebook/zstd/blob/dev/programs/README.md This made the compiled library about 20 kiB larger, but will allow this to be used with really old zstd payloads. .circleci/config.yml: Do not test with different versions of this macro, since the tests now include a check that it can decompress old payloads.
1 parent 4fa4b6b commit 49e3667

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

.circleci/config.yml

-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ jobs:
4141
- run: 'unzip mr.zip'
4242
- run: 'go build'
4343
- run: 'PAYLOAD=`pwd`/mr GODEBUG=efence=1 go test -v'
44-
"golang-zstd-legacy-support":
45-
docker:
46-
- image: circleci/golang:latest
47-
steps:
48-
- checkout
49-
- run: 'wget https://github.com/DataDog/zstd/files/2246767/mr.zip'
50-
- run: 'unzip mr.zip'
51-
- run: 'CGO_CFLAGS="-DZSTD_LEGACY_SUPPORT=1" go build'
52-
- run: 'PAYLOAD=`pwd`/mr CGO_CFLAGS="-DZSTD_LEGACY_SUPPORT=1" go test -v'
5344
"golang-i386":
5445
docker:
5546
- image: 32bit/ubuntu:16.04

zstd.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package zstd
22

33
/*
4+
// support decoding of "legacy" zstd payloads from versions [0.4, 0.8], matching the
5+
// default configuration of the zstd command line tool:
6+
// https://github.com/facebook/zstd/blob/dev/programs/README.md
7+
#cgo CFLAGS: -DZSTD_LEGACY_SUPPORT=4
8+
49
#define ZSTD_STATIC_LINKING_ONLY
510
#include "zstd.h"
611
*/

zstd_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io/ioutil"
88
"os"
9+
"strconv"
10+
"strings"
911
"testing"
1012
)
1113

@@ -259,6 +261,29 @@ func TestRealPayload(t *testing.T) {
259261
}
260262
}
261263

264+
func TestLegacy(t *testing.T) {
265+
// payloads compressed with zstd v0.5
266+
// needs ZSTD_LEGACY_SUPPORT=5 or less
267+
testCases := []struct {
268+
input string
269+
expected string
270+
}{
271+
{"%\xb5/\xfd\x00@\x00\x1bcompressed with legacy zstd\xc0\x00\x00", "compressed with legacy zstd"},
272+
{"%\xb5/\xfd\x00\x00\x00A\x11\x007\x14\xb0\xb5\x01@\x1aR\xb6iI7[FH\x022u\xe0O-\x18\xe3G\x9e2\xab\xd9\xea\xca\xee\x884\xbf\xe7\xdc\xe4@\xe1-\x9e\xac\xf0\xf2\x86\x0f\xf1r\xbb7\b\x81Z\x01\x00\x01\x00\xdf`\xfe\xc0\x00\x00", "compressed with legacy zstd"},
273+
}
274+
for i, testCase := range testCases {
275+
t.Run(strconv.Itoa(i), func(t *testing.T) {
276+
out, err := Decompress(nil, []byte(testCase.input))
277+
if err != nil {
278+
t.Fatal(err)
279+
}
280+
if !strings.Contains(string(out), testCase.expected) {
281+
t.Errorf("expected to find %#v; output=%#v", testCase.expected, string(out))
282+
}
283+
})
284+
}
285+
}
286+
262287
func BenchmarkCompression(b *testing.B) {
263288
if raw == nil {
264289
b.Fatal(ErrNoPayloadEnv)

0 commit comments

Comments
 (0)