github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/reader/reader.go (about) 1 // Package reader : Reader for bytestream buf 2 package reader 3 4 import ( 5 "encoding/binary" 6 "errors" 7 ) 8 9 // Reader : Struct to store bytestream 10 // and read Position 11 type Reader struct { 12 data []byte 13 Pos uint16 14 Length uint16 15 } 16 17 //var errReader = errors.New("read data out-of-boundary") 18 19 // NewReader : construct function 20 func NewReader(b []byte, n int) *Reader { 21 return &Reader{ 22 data: b, 23 Pos: 0, 24 Length: uint16(n), 25 } 26 } 27 28 func (r *Reader) movePos(num uint16) { 29 r.data = r.data[num:] 30 r.Pos += num 31 } 32 33 // ReadUint8 : read 1 byte 34 func (r *Reader) ReadUint8() (uint8, error) { 35 const Length = 1 36 if r.Pos+Length > r.Length { 37 return 0, errors.New("read UINT8 data out-of-boundary") 38 } 39 40 d := r.data[0] 41 //binary.BigEndian does not have uint8 func 42 //so directly return bytes slice 43 r.movePos(Length) 44 45 return d, nil 46 } 47 48 // ReadUint16 : BigEndian uint16 function 49 func (r *Reader) ReadUint16() (uint16, error) { 50 const Length = 2 51 if r.Pos+Length > r.Length { 52 return 0, errors.New("read UINT16 data out-of-boundary") 53 } 54 d := binary.BigEndian.Uint16(r.data[0:Length]) 55 r.movePos(Length) 56 57 return d, nil 58 } 59 60 // ReadUint32 : BigEndian uint32 function 61 func (r *Reader) ReadUint32() (uint32, error) { 62 const Length = 4 63 if r.Pos+Length > r.Length { 64 return 0, errors.New("read UINT32 data out-of-boundary") 65 } 66 d := binary.BigEndian.Uint32(r.data[0:Length]) 67 r.movePos(Length) 68 69 return d, nil 70 } 71 72 // ReadUint64 : BigEndian uint64 function 73 func (r *Reader) ReadUint64() (uint64, error) { 74 const Length = 8 75 if r.Pos+Length > r.Length { 76 return 0, errors.New("read UINT64 data out-of-boundary") 77 } 78 d := binary.BigEndian.Uint64(r.data[0:Length]) 79 r.movePos(Length) 80 81 return d, nil 82 } 83 84 // ReadN : Read N-bytes, for var-Length record type 85 func (r *Reader) ReadN(Length uint16) ([]byte, error) { 86 if Length > 0 { 87 if r.Pos+Length > r.Length { 88 return []byte{}, errors.New("read N-Bytes data out-of-boundary") 89 } 90 d := r.data[0:Length] 91 r.movePos(Length) 92 93 return d, nil 94 } 95 96 return []byte{}, errors.New("read N-Bytes data out-of-boundary,Length Zero") 97 98 } 99 100 // FetchUint16 : BigEndian uint16 function 101 func (r *Reader) FetchUint16() (uint16, error) { 102 const Length = 2 103 if r.Pos+Length > r.Length { 104 return 0, errors.New("fetch UINT16 data out-of-boundary") 105 } 106 d := binary.BigEndian.Uint16(r.data[0:Length]) 107 108 return d, nil 109 } 110 111 // FetchUint32 : BigEndian uint32 function 112 func (r *Reader) FetchUint32() (uint32, error) { 113 const Length = 4 114 if r.Pos+Length > r.Length { 115 return 0, errors.New("fetch UINT32 data out-of-boundary") 116 } 117 d := binary.BigEndian.Uint32(r.data[0:Length]) 118 119 return d, nil 120 } 121 122 // FetchUint64 : BigEndian uint64 function 123 func (r *Reader) FetchUint64() (uint64, error) { 124 const Length = 8 125 if r.Pos+Length > r.Length { 126 return 0, errors.New("fetch UINT64 data out-of-boundary") 127 } 128 d := binary.BigEndian.Uint64(r.data[0:Length]) 129 130 return d, nil 131 } 132 133 // FetchN : Return N bytes, but does not move Pos 134 func (r *Reader) FetchN(Length uint16) ([]byte, error) { 135 if r.Pos+Length > r.Length { 136 return []byte{}, errors.New("fetch N-Bytes data out-of-boundary") 137 } 138 d := r.data[0:Length] 139 return d, nil 140 }