~bitfehler/xbscli

e084309f54c504100af4d8fcb856e1bc422c2d9e — Conrad Hoffmann 2 years ago cf9a074
Handle self-referential sequences

Well, this took a while to track down...

Sequences can be self-referential, i.e. referring to bytes that will
only exist after one has started decoding the sequence. See this
upstream for some details:

https://github.com/rotemdan/lzutf8.js/issues/34#issuecomment-787852328

This probably also explains why the compression implemented here is
sometimes a tiny bit worse than the upstream implementation. It
definitely doesn't use this feature.
1 files changed, 7 insertions(+), 2 deletions(-)

M xbs/lzutf8.go
M xbs/lzutf8.go => xbs/lzutf8.go +7 -2
@@ 61,8 61,13 @@ func Decompress(input []byte) (string, error) {

			idx := len(output) - distance
			end := idx + length
			ref := output[idx:end]
			output = append(output, ref...)

			// Not using a for loop might seem like a good idea, but it isn't.
			// The sequences can be self-referential, i.e. reference bytes that
			// are not actually there yet. They have to be added one at a time.
			for ; idx < end; idx++ {
				output = append(output, output[idx])
			}
		} else {
			output = append(output, b1)
		}