diff --git a/stream/mts/psi/psi.go b/stream/mts/psi/psi.go index 05cd2c3e..608aedec 100644 --- a/stream/mts/psi/psi.go +++ b/stream/mts/psi/psi.go @@ -1,5 +1,10 @@ package psi +import ( + "hash/crc32" + "math/bits" +) + const ( ESSDDefLen = 5 DescDefLen = 2 @@ -81,8 +86,13 @@ func (p *PSI) Bytes() []byte { out[l+1] = 0x80 | 0x40 | 0x30 | (0x03 & byte(p.sl>>8)) out[l+2] = byte(p.sl) out = append(out, p.tss.Bytes()...) - crc32 := crc32_op() - return nil + crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[l:]) + 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 { @@ -147,3 +157,26 @@ func boolToByte(b bool) byte { } 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 +}