stream/mts/psi: modified how update crc works so that we're only giving it data that's included in crc calc as well as the crc field itself. Added some consts to allow working with SyntaxSectionLen. Added trimPadding func so that we can easily get rid of any padding. Fixed createDescriptor func which was found to be broken from TestCreateDescriptor

This commit is contained in:
saxon 2019-01-29 14:16:33 +10:30
parent 3a14b64480
commit af239838d0
3 changed files with 31 additions and 14 deletions

View File

@ -37,13 +37,13 @@ import (
func addCrc(out []byte) []byte { func addCrc(out []byte) []byte {
t := make([]byte, len(out)+4) t := make([]byte, len(out)+4)
copy(t, out) copy(t, out)
updateCrc(t) updateCrc(t[1:])
return t return t
} }
// updateCrc updates the crc of bytes slice, writing the checksum into the last four bytes. // updateCrc updates the crc of bytes slice, writing the checksum into the last four bytes.
func updateCrc(b []byte) { func updateCrc(b []byte) {
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), b[1:len(b)-4]) crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), b[:len(b)-4])
binary.BigEndian.PutUint32(b[len(b)-4:], crc32) binary.BigEndian.PutUint32(b[len(b)-4:], crc32)
} }

View File

@ -64,15 +64,14 @@ func UpdateTime(dst []byte, t uint64) error {
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] { for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
dst[i+TimeDataIndx] = ts[i] dst[i+TimeDataIndx] = ts[i]
} }
updateCrc(dst) updateCrc(dst[1:])
return nil return nil
} }
// SyntaxSecLenFrom takes a byte slice representation of a psi and extracts // SyntaxSecLenFrom takes a byte slice representation of a psi and extracts
// it's syntax section length // it's syntax section length
func SyntaxSecLenFrom(p []byte) (l uint8) { func SyntaxSecLenFrom(p []byte) int {
l = uint8(p[syntaxSecLenIndx]) - crcSize return int(((p[SyntaxSecLenIdx1] & SyntaxSecLenMask1) << 8) | p[SyntaxSecLenIdx2])
return
} }
// TimeFrom takes a byte slice representation of a psi-pmt and extracts it's // TimeFrom takes a byte slice representation of a psi-pmt and extracts it's
@ -112,7 +111,7 @@ func UpdateLocation(d []byte, s string) error {
for i := range loc { for i := range loc {
loc[i] = 0 loc[i] = 0
} }
updateCrc(d) updateCrc(d[1:])
return nil return nil
} }

View File

@ -70,7 +70,9 @@ const (
// Other misc consts // Other misc consts
const ( const (
syntaxSecLenIndx = 3 SyntaxSecLenIdx1 = 2
SyntaxSecLenIdx2 = 3
SyntaxSecLenMask1 = 0x03
crcSize = 4 crcSize = 4
) )
@ -275,24 +277,40 @@ func (p *PSIBytes) descriptors() []byte {
func (p *PSIBytes) createDescriptor(tag int, data []byte) { func (p *PSIBytes) createDescriptor(tag int, data []byte) {
curProgLen := p.ProgramInfoLen() curProgLen := p.ProgramInfoLen()
oldSyntaxSectionLen := SyntaxSecLenFrom(*p)
dataLen := len(data) dataLen := len(data)
// Calculate the new descriptors index and length. // Calculate the new descriptors index and length.
newDescIdx := DescriptorsIdx + curProgLen newDescIdx := DescriptorsIdx + curProgLen
newDescLen := dataLen + 2 newDescLen := dataLen + 2
// Copy data down from newDescIdx to create room for the new descriptor. // Copy data down from newDescIdx to create room for the new descriptor.
copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:dataLen-newDescLen]) copy((*p)[newDescIdx+newDescLen:], (*p)[newDescIdx:newDescIdx+newDescLen])
// Set the tag, data len and data of the new desriptor. // Set the tag, data len and data of the new desriptor.
(*p)[newDescIdx] = byte(tag) (*p)[newDescIdx] = byte(tag)
(*p)[newDescIdx+1] = byte(dataLen) (*p)[newDescIdx+1] = byte(dataLen)
copy((*p)[newDescIdx+2:newDescIdx+2+dataLen], data) copy((*p)[newDescIdx+2:newDescIdx+2+dataLen], data)
// Update the program info length to account for the new descriptor. // Update the program info length to account for the new descriptor.
newProgInfoLen := curProgLen + dataLen // TODO: put this in function set program info length
(*p)[ProgramInfoLenIdx1] = byte(newProgInfoLen >> 8) addedLen := dataLen + 2
newProgInfoLen := curProgLen + addedLen
(*p)[ProgramInfoLenIdx1] &= 0xff ^ ProgramInfoLenMask1
(*p)[ProgramInfoLenIdx1] |= byte(newProgInfoLen>>8) & ProgramInfoLenMask1
(*p)[ProgramInfoLenIdx2] = byte(newProgInfoLen) (*p)[ProgramInfoLenIdx2] = byte(newProgInfoLen)
// set section length
// TODO: put this in func set program info length
newSyntaxSectionLen := int(oldSyntaxSectionLen) + addedLen
(*p)[SyntaxSecLenIdx1] &= 0xff ^ SyntaxSecLenMask1
(*p)[SyntaxSecLenIdx1] |= byte(newSyntaxSectionLen>>8) & SyntaxSecLenMask1
(*p)[SyntaxSecLenIdx2] = byte(newSyntaxSectionLen)
}
func (p *PSIBytes) trimPadding() []byte {
sectionLength := SyntaxSecLenFrom(*p)
paddingIdx := (4 + sectionLength)
o := (*p)[:paddingIdx]
return o
} }
func (d *Descriptor) update(data []byte) { func (d *Descriptor) update(data []byte) {