psi: completed writing Bytes() for PSI table

This commit is contained in:
saxon 2018-12-06 12:16:26 +10:30
parent 42038a8cb9
commit ba35615964
1 changed files with 35 additions and 2 deletions

View File

@ -1,5 +1,10 @@
package psi package psi
import (
"hash/crc32"
"math/bits"
)
const ( const (
ESSDDefLen = 5 ESSDDefLen = 5
DescDefLen = 2 DescDefLen = 2
@ -81,8 +86,13 @@ func (p *PSI) Bytes() []byte {
out[l+1] = 0x80 | 0x40 | 0x30 | (0x03 & byte(p.sl>>8)) out[l+1] = 0x80 | 0x40 | 0x30 | (0x03 & byte(p.sl>>8))
out[l+2] = byte(p.sl) out[l+2] = byte(p.sl)
out = append(out, p.tss.Bytes()...) out = append(out, p.tss.Bytes()...)
crc32 := crc32_op() crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[l:])
return nil out = append(out, make([]byte, 0, 4)...)
out = append(out, byte(crc32>>24))
out = append(out, byte(crc32>>16))
out = append(out, byte(crc32>>8))
out = append(out, byte(crc32))
return out
} }
func (t *TSS) Bytes() []byte { func (t *TSS) Bytes() []byte {
@ -147,3 +157,26 @@ func boolToByte(b bool) byte {
} }
return 0x00 return 0x00
} }
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
}