Skip to content

Fix checksum code, some of the time it did not match Go/Python#18

Open
protolambda wants to merge 1 commit intokesla:masterfrom
protolambda:fix-checksum
Open

Fix checksum code, some of the time it did not match Go/Python#18
protolambda wants to merge 1 commit intokesla:masterfrom
protolambda:fix-checksum

Conversation

@protolambda
Copy link
Copy Markdown

Checksums were wrong, here's some Go code to compare, along with examples of the JS checksums not matching, but only some of the time:

package main

import (
	"fmt"
)

func main() {
	// Go: https://github.com/golang/snappy/blob/2e65f85255dbc3072edf28d6b5b8efc472979f5a/snappy.go#L97
	// Python: https://github.com/andrix/python-snappy/blob/602e9c10d743f71bef0bac5e4c4dffa17340d7b3/snappy/snappy.py#L70
	
	// Did not match JS before
	// x = 3008619490
	// Old JS: ((((x >> 15) | (x << 17)) + 0xa282ead8)) >>> 0
	//   Result: 2726449535
	// New JS: ((((x >>> 15) | ((x & 0x7fff) << 17)) + 0xa282ead8)) >>> 0
	//   Result: 1783124351
	x := uint32(3008619490)
	checksum := uint32(uint32(x>>15|x<<17) + 0xa282ead8)
	fmt.Printf("Checksum: %d\n", checksum)
	
	// Did match js before
	// x = 1
	// Old JS: ((((x >> 15) | (x << 17)) + 0xa282ead8)) >>> 0
	//   Result: 2726619864
	// New JS: ((((x >>> 15) | ((x & 0x7fff) << 17)) + 0xa282ead8)) >>> 0
	//   Result: 2726619864
	x = uint32(1)
	checksum = uint32(uint32(x>>15|x<<17) + 0xa282ead8)
	fmt.Printf("Checksum: %d\n", checksum)
}

See https://play.golang.org/p/AzfhAHeV1bn for a live example.
Some previous checksums were incorrect.

Checksums test file also updated, I just set the UPDATE_EXPECTED env variable to get it updated.

…hon snappy checksums due to bitshift problems
@mint-leaf mint-leaf mentioned this pull request May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant