From a0079ef046daf900807baf10b9cef6a15d68d337 Mon Sep 17 00:00:00 2001 From: saxon Date: Mon, 7 Jan 2019 12:26:58 +1030 Subject: [PATCH] psi: created crc.go to put crc stuff in and renamed op.go to helpers.go --- stream/mts/psi/op.go | 145 ------------------------------------------ stream/mts/psi/psi.go | 27 -------- 2 files changed, 172 deletions(-) delete mode 100644 stream/mts/psi/op.go diff --git a/stream/mts/psi/op.go b/stream/mts/psi/op.go deleted file mode 100644 index e78cdac7..00000000 --- a/stream/mts/psi/op.go +++ /dev/null @@ -1,145 +0,0 @@ -/* -NAME - op.go -DESCRIPTION - op.go provides functionality for editing and reading bytes slices - directly in order to insert/read timestamp and location data in psi. - -AUTHOR - Saxon Milton - -LICENSE - op.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 ( - "bytes" - "encoding/binary" - "errors" - "hash/crc32" - "math/bits" -) - -// TimeBytes takes a timestamp as an uint64 and converts to an 8 byte slice - -// allows for updating of timestamp in pmt time descriptor. -func TimeBytes(t uint64) []byte { - var s [timeDataSize]byte - binary.BigEndian.PutUint64(s[:], t) - return s[:] -} - -// HasTime takes a psi as a byte slice and checks to see if it has a time descriptor -// - if so return nil, otherwise return error -func HasTime(p []byte) error { - if p[timeTagIndx] != timeDescTag { - return errors.New("PSI does not contain a time descriptor, cannot update") - } - return nil -} - -// HasLocation takes a psi as a byte slice and checks to see if it has a location descriptor -// - if so return nil, otherwise return error -func HasLocation(p []byte) error { - if p[locationTagIndx] != locationDescTag { - return errors.New("PSI does not contain a location descriptor, cannot update") - } - return nil -} - -// UpdateTime takes the byte slice representation of a psi-pmt as well as a time -// as an integer and attempts to update the time descriptor in the pmt with the -// given time if the time descriptor exists, otherwise an error is returned -func UpdateTime(dst []byte, t uint64) error { - err := HasTime(dst) - if err != nil { - return err - } - ts := TimeBytes(uint64(t)) - for i := range dst[timeDataIndx : timeDataIndx+timeDataSize] { - dst[i+timeDataIndx] = ts[i] - } - dst = updateCrc(dst) - return nil -} - -// TimeFrom takes a byte slice representation of a psi-pmt and extracts it's -// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil -// if it does not exist -func TimeFrom(p []byte) (t uint64, err error) { - err = HasTime(p) - if err != nil { - return 0, err - } - t = binary.BigEndian.Uint64(p[timeDataIndx : timeDataIndx+timeDataSize]) - return t, nil -} - -// LocationFrom takes a byte slice representation of a psi-pmt and extracts it's -// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil -// if it does not exist -func LocationFrom(p []byte) (g string, err error) { - err = HasLocation(p) - if err != nil { - return "", err - } - gBytes := p[locationDataIndx : locationDataIndx+locationDataSize] - gBytes = bytes.Trim(gBytes, "\x00") - g = string(gBytes) - return g, nil -} - -// LocationStrBytes take a string of location data and converts to a 32 byte slice - -// easy update of slice representation of a pmt with location descriptor -func LocationStrBytes(s string) []byte { - var b [locationDataSize]byte - copy(b[:], s) - return b[:] -} - -// UpdateLocation takes a byte slice representation of a psi-pmt containing a location -// descriptor and attempts to update the location data value with the passed string. -// If the psi does not contain a location descriptor, and error is returned. -func UpdateLocation(d []byte, s string) error { - err := HasLocation(d) - if err != nil { - return err - } - gb := LocationStrBytes(s) - for i := range d[locationDataIndx : locationDataIndx+locationDataSize] { - d[i+locationDataIndx] = gb[i] - } - d = updateCrc(d) - return nil -} - -// 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 -} diff --git a/stream/mts/psi/psi.go b/stream/mts/psi/psi.go index 56abc260..07d460bb 100644 --- a/stream/mts/psi/psi.go +++ b/stream/mts/psi/psi.go @@ -26,10 +26,6 @@ LICENSE package psi -import ( - "hash/crc32" -) - // Lengths of section definitions const ( ESSDDefLen = 5 @@ -308,26 +304,3 @@ func byteToBool(b byte) (o bool) { } return } - -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 -}