mirror of https://bitbucket.org/ausocean/av.git
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
|
/*
|
||
|
NAME
|
||
|
crc.go
|
||
|
DESCRIPTION
|
||
|
See Readme.md
|
||
|
|
||
|
AUTHOR
|
||
|
Saxon Milton <saxon@ausocean.org>
|
||
|
|
||
|
LICENSE
|
||
|
crc.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
||
|
|
||
|
It is free software: you can redistribute it and/or modify them
|
||
|
under the terms of the GNU General Public License as published by the
|
||
|
Free Software Foundation, either version 3 of the License, or (at your
|
||
|
option) any later version.
|
||
|
|
||
|
It is distributed in the hope that it will be useful, but WITHOUT
|
||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||
|
for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
||
|
*/
|
||
|
|
||
|
package psi
|
||
|
|
||
|
import (
|
||
|
"hash/crc32"
|
||
|
"math/bits"
|
||
|
)
|
||
|
|
||
|
// addCrc appends a crc table to a given psi table in bytes
|
||
|
func addCrc(out []byte) []byte {
|
||
|
out = append(out, make([]byte, 4)...)
|
||
|
out = updateCrc(out)
|
||
|
return out
|
||
|
}
|
||
|
|
||
|
// updateCrc updates the crc of psi bytes slice that may have been modified
|
||
|
func updateCrc(out []byte) []byte {
|
||
|
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[1:len(out)-4])
|
||
|
out[len(out)-4] = byte(crc32 >> 24)
|
||
|
out[len(out)-3] = byte(crc32 >> 16)
|
||
|
out[len(out)-2] = byte(crc32 >> 8)
|
||
|
out[len(out)-1] = byte(crc32)
|
||
|
return out
|
||
|
}
|
||
|
|
||
|
func crc32_MakeTable(poly uint32) *crc32.Table {
|
||
|
var t crc32.Table
|
||
|
for i := range t {
|
||
|
crc := uint32(i) << 24
|
||
|
for j := 0; j < 8; j++ {
|
||
|
if crc&0x80000000 != 0 {
|
||
|
crc = (crc << 1) ^ poly
|
||
|
} else {
|
||
|
crc <<= 1
|
||
|
}
|
||
|
}
|
||
|
t[i] = crc
|
||
|
}
|
||
|
return &t
|
||
|
}
|
||
|
|
||
|
func crc32_Update(crc uint32, tab *crc32.Table, p []byte) uint32 {
|
||
|
for _, v := range p {
|
||
|
crc = tab[byte(crc>>24)^v] ^ (crc << 8)
|
||
|
}
|
||
|
return crc
|
||
|
}
|