github.com/puellanivis/breton@v0.2.16/lib/mpeg/ts/descriptor/raw.go (about)

     1  package descriptor
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  type raw struct {
    10  	tag  uint8
    11  	data []byte
    12  }
    13  
    14  func (d *raw) String() string {
    15  	return fmt.Sprintf("{tag:x%02X data[%d]}", d.tag, len(d.data))
    16  }
    17  
    18  func (d *raw) Tag() uint8 {
    19  	return d.tag
    20  }
    21  
    22  func (d *raw) Len() int {
    23  	return 2 + len(d.data)
    24  }
    25  
    26  func (d *raw) Unmarshal(b []byte) error {
    27  	d.tag = uint8(b[0])
    28  	l := int(b[1])
    29  
    30  	b = b[2:]
    31  
    32  	if len(b) < l {
    33  		return errors.Errorf("unexpected end of byte-slice: %d < %d", len(b), l)
    34  	}
    35  
    36  	d.data = make([]byte, l)
    37  	copy(d.data, b)
    38  
    39  	return nil
    40  }
    41  
    42  func (d *raw) Marshal() ([]byte, error) {
    43  	if len(d.data) > 0xff {
    44  		return nil, errors.Errorf("descriptor data field too large: %d", len(d.data))
    45  	}
    46  
    47  	b := make([]byte, d.Len())
    48  
    49  	b[0] = byte(d.tag)
    50  	b[1] = byte(len(d.data))
    51  	copy(b[2:], d.data)
    52  
    53  	return b, nil
    54  }