brotli icon indicating copy to clipboard operation
brotli copied to clipboard

Why does this error `brotli: RESERVED` occur with some data?

Open rew1nter opened this issue 10 months ago • 2 comments

With some data, I'm seeing this error while it works with the rest. What does this error mean and how can I solve it?

This is my code:


// Compress text with the brotli compression algorithm
func Compress(s string) []byte {
	var b bytes.Buffer

	bw := brotli.NewWriter(nil)

	b.Reset()

	// Reset the compressor and encode from some input stream.
	bw.Reset(&b)
	if _, err := io.WriteString(bw, s); err != nil {
		log.Fatal(err)
	}
	if err := bw.Close(); err != nil {
		log.Fatal("failed to comporess:", err)
	}

	return b.Bytes()
}

// Decomporess brotli comporessed data
func Decomporess(data []byte) string {
	b := bytes.NewBuffer(data)
	br := brotli.NewReader(nil)

	// Reset the decompressor and decode to some output stream.
	if err := br.Reset(b); err != nil {
		log.Fatal(err)
	}

	// dst := os.Stdout
	dst := bytes.NewBuffer(nil)
	if _, err := io.Copy(dst, br); err != nil {
		log.Fatal("failed to decomporess:", err)
	}
	return dst.String()
}

rew1nter avatar Sep 03 '23 12:09 rew1nter

okay I figured out the error is caused on my end. But still curious when its shown

rew1nter avatar Sep 03 '23 12:09 rew1nter

In the brotli format, there is a bit in the metablock header that is reserved for future use. It's currently required to be a zero; if there is a one there, it indicates that an incompatible new feature of the format is being used. So an older decoder that doesn't understand the new feature needs to exit with an error. (Note that this new feature is only theoretical; it's not that there actually is a feature that uses that bit yet.)

andybalholm avatar Sep 04 '23 20:09 andybalholm