streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

commit b113263c7e5e7cb104c5fff7a9c61d0c10db31f4
parent 2c4c5d7ef79d88f67d5fc142395436952ee1a747
Author: Oliver Lowe <o@olowe.co>
Date:   Tue, 28 May 2024 12:17:45 +1000

scte35: document some of the magic values for crc32

While here remove unused validation functions; we have tests for this.

Diffstat:
Mscte35/crc_32.go | 53++++++++++++++---------------------------------------
Mscte35/splice_info.go | 2+-
2 files changed, 15 insertions(+), 40 deletions(-)

diff --git a/scte35/crc_32.go b/scte35/crc_32.go @@ -16,24 +16,18 @@ package scte35 -import ( - "encoding/binary" - "errors" -) +import "hash/crc32" -// ErrCRC32Invalid indicates that a splice_info_sections CRC_32 is -// invalid with respect to the binary payload. -var ErrCRC32Invalid = errors.New("CRC_32 not valid") +var crctab = makeCRC32Table(crc32PolyNormal) -// crc32Table provides values for calculating an SCTE35 object's CRC32 value. -var crc32Table = makeCRC32Table() +// The reverse of crc32.IEEE, from +// https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations +const crc32PolyNormal = 0x04C11DB7 -// makeCRC32Table generates CRC32/BZIP2 table -func makeCRC32Table() [256]uint32 { - const poly = 0x04C11DB7 - crctable := [256]uint32{} - - for i := range crctable { +// makeCRC32Table generates CRC32/BZIP2 table using poly. +func makeCRC32Table(poly uint32) crc32.Table { + var tab crc32.Table + for i := range tab { crc := uint32(i) << 24 for j := 0; j < 8; j++ { if crc&0x80000000 != 0 { @@ -42,35 +36,16 @@ func makeCRC32Table() [256]uint32 { crc = crc << 1 } } - crctable[i] = crc + tab[i] = crc } - return crctable + return tab } -// calculateCRC32 takes an byte array Writer containing all of the bytes of a -// Splice Info Section object up to the CRC32 value, calculates a CRC32 value -// matching the bits for those existing bytes, and returns it. -func calculateCRC32(b []byte) uint32 { +// checksum calculates the checksum for the given bytes using tab. +func checksum(b []byte, tab *crc32.Table) uint32 { crc := int32(-1) for i := range b { - crc = (crc << 8) ^ int32(crc32Table[((crc>>24)^int32(b[i]))&0xFF]) + crc = (crc << 8) ^ int32(tab[((crc>>24)^int32(b[i]))&0xFF]) } return uint32(crc) } - -// verifyCRC32 verifies the base-64 encoded binary string is valid by confirming -// the CRC_32 matches -func verifyCRC32(b []byte) error { - if len(b) < 4 { - return ErrCRC32Invalid - } - - // crc32 is the last 4 bytes. - payload := b[:len(b)-4] - crc := binary.BigEndian.Uint32(b[len(b)-4:]) - if calculateCRC32(payload) != crc { - return ErrCRC32Invalid - } - - return nil -} diff --git a/scte35/splice_info.go b/scte35/splice_info.go @@ -119,7 +119,7 @@ func EncodeSpliceInfo(info *SpliceInfo) ([]byte, error) { buf[1] |= byte(buflen >> 8) buf[2] = byte(buflen) - crc := calculateCRC32(buf) + crc := checksum(buf, &crctab) return binary.BigEndian.AppendUint32(buf, crc), nil }