@@ -23,6 +23,7 @@ import (
2323 "encoding/binary"
2424 "math"
2525 "math/big"
26+ "math/bits"
2627
2728 "github.com/pkg/errors"
2829)
@@ -145,54 +146,39 @@ func (rb *byteReadBuffer) ReadUint8(_ string, bitLength uint8, _ ...WithReaderAr
145146 return uint8 (res ), nil
146147}
147148
148- func (rb * byteReadBuffer ) ReadUint16 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (uint16 , error ) {
149- if rb .byteOrder == binary .LittleEndian {
150- // TODO: indirection till we have a native LE implementation
151- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
152- if err != nil {
153- return 0 , err
154- }
155- return uint16 (bigInt .Uint64 ()), nil
156- }
149+ func (rb * byteReadBuffer ) ReadUint16 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (uint16 , error ) {
157150 res , err := rb .bits .ReadBits (bitLength )
158151 if err != nil {
159152 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
160153 }
161154 rb .pos += uint64 (bitLength )
155+ if rb .byteOrder == binary .LittleEndian {
156+ return uint16 (bits .ReverseBytes64 (res ) >> (64 - bitLength )), nil
157+ }
162158 return uint16 (res ), nil
163159}
164160
165- func (rb * byteReadBuffer ) ReadUint32 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (uint32 , error ) {
166- if rb .byteOrder == binary .LittleEndian {
167- // TODO: indirection till we have a native LE implementation
168- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
169- if err != nil {
170- return 0 , err
171- }
172- return uint32 (bigInt .Uint64 ()), nil
173- }
161+ func (rb * byteReadBuffer ) ReadUint32 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (uint32 , error ) {
174162 res , err := rb .bits .ReadBits (bitLength )
175163 if err != nil {
176164 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
177165 }
178166 rb .pos += uint64 (bitLength )
167+ if rb .byteOrder == binary .LittleEndian {
168+ return uint32 (bits .ReverseBytes64 (res ) >> (64 - bitLength )), nil
169+ }
179170 return uint32 (res ), nil
180171}
181172
182- func (rb * byteReadBuffer ) ReadUint64 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (uint64 , error ) {
183- if rb .byteOrder == binary .LittleEndian {
184- // TODO: indirection till we have a native LE implementation
185- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
186- if err != nil {
187- return 0 , err
188- }
189- return bigInt .Uint64 (), nil
190- }
173+ func (rb * byteReadBuffer ) ReadUint64 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (uint64 , error ) {
191174 res , err := rb .bits .ReadBits (bitLength )
192175 if err != nil {
193176 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
194177 }
195178 rb .pos += uint64 (bitLength )
179+ if rb .byteOrder == binary .LittleEndian {
180+ return bits .ReverseBytes64 (res ) >> (64 - bitLength ), nil
181+ }
196182 return res , nil
197183}
198184
@@ -205,59 +191,43 @@ func (rb *byteReadBuffer) ReadInt8(_ string, bitLength uint8, _ ...WithReaderArg
205191 return int8 (res ), nil
206192}
207193
208- func (rb * byteReadBuffer ) ReadInt16 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (int16 , error ) {
209- if rb .byteOrder == binary .LittleEndian {
210- // TODO: indirection till we have a native LE implementation
211- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
212- if err != nil {
213- return 0 , err
214- }
215- return int16 (bigInt .Int64 ()), nil
216- }
194+ func (rb * byteReadBuffer ) ReadInt16 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (int16 , error ) {
217195 res , err := rb .bits .ReadBits (bitLength )
218196 if err != nil {
219197 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
220198 }
221199 rb .pos += uint64 (bitLength )
200+ if rb .byteOrder == binary .LittleEndian {
201+ return int16 (bits .ReverseBytes64 (res ) >> (64 - bitLength )), nil
202+ }
222203 return int16 (res ), nil
223204}
224205
225- func (rb * byteReadBuffer ) ReadInt32 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (int32 , error ) {
226- if rb .byteOrder == binary .LittleEndian {
227- // TODO: indirection till we have a native LE implementation
228- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
229- if err != nil {
230- return 0 , err
231- }
232- return int32 (bigInt .Int64 ()), nil
233- }
206+ func (rb * byteReadBuffer ) ReadInt32 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (int32 , error ) {
234207 res , err := rb .bits .ReadBits (bitLength )
235208 if err != nil {
236209 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
237210 }
238211 rb .pos += uint64 (bitLength )
212+ if rb .byteOrder == binary .LittleEndian {
213+ return int32 (bits .ReverseBytes64 (res ) >> (64 - bitLength )), nil
214+ }
239215 return int32 (res ), nil
240216}
241217
242- func (rb * byteReadBuffer ) ReadInt64 (logicalName string , bitLength uint8 , _ ... WithReaderArgs ) (int64 , error ) {
243- if rb .byteOrder == binary .LittleEndian {
244- // TODO: indirection till we have a native LE implementation
245- bigInt , err := rb .ReadBigInt (logicalName , uint64 (bitLength ))
246- if err != nil {
247- return 0 , err
248- }
249- return bigInt .Int64 (), nil
250- }
218+ func (rb * byteReadBuffer ) ReadInt64 (_ string , bitLength uint8 , _ ... WithReaderArgs ) (int64 , error ) {
251219 res , err := rb .bits .ReadBits (bitLength )
252220 if err != nil {
253221 return 0 , errors .Wrapf (err , "error reading %d bits at pos [%d]bit ([%d]byte)" , bitLength , rb .pos , rb .pos / 8 )
254222 }
255223 rb .pos += uint64 (bitLength )
224+ if rb .byteOrder == binary .LittleEndian {
225+ return int64 (bits .ReverseBytes64 (res ) >> (64 - bitLength )), nil
226+ }
256227 return int64 (res ), nil
257228}
258229
259230func (rb * byteReadBuffer ) ReadBigInt (_ string , bitLength uint64 , _ ... WithReaderArgs ) (* big.Int , error ) {
260- // TODO: highly experimental remove this comment when tested or verified
261231 rawBytes := make ([]byte , 0 , (bitLength + 7 )/ 8 )
262232
263233 fullBytes := bitLength / 8
0 commit comments