Skip to content

Commit

Permalink
Make uintFromBytes return uint instead of uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed May 3, 2017
1 parent 8e5fac6 commit d19f6d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func (d *decoder) sizeFromCtrlByte(ctrlByte byte, offset uint, typeNum dataType)

switch {
case size == 30:
size = 285 + uint(uintFromBytes(0, sizeBytes))
size = 285 + uintFromBytes(0, sizeBytes)
case size > 30:
size = uint(uintFromBytes(0, sizeBytes)) + 65821
size = uintFromBytes(0, sizeBytes) + 65821
}
return size, newOffset, nil
}
Expand Down Expand Up @@ -507,13 +507,13 @@ func (d *decoder) decodePointer(
return 0, 0, newOffsetError()
}
pointerBytes := d.buffer[offset:newOffset]
var prefix uint64
var prefix uint
if pointerSize == 4 {
prefix = 0
} else {
prefix = uint64(size & 0x7)
prefix = uint(size & 0x7)
}
unpacked := uint(uintFromBytes(prefix, pointerBytes))
unpacked := uintFromBytes(prefix, pointerBytes)

var pointerValueOffset uint
switch pointerSize {
Expand Down Expand Up @@ -640,8 +640,12 @@ func (d *decoder) decodeStruct(

func (d *decoder) decodeUint(size uint, offset uint) (uint64, uint, error) {
newOffset := offset + size
val := uintFromBytes(0, d.buffer[offset:newOffset])
bytes := d.buffer[offset:newOffset]

var val uint64
for _, b := range bytes {
val = (val << 8) | uint64(b)
}
return val, newOffset, nil
}

Expand All @@ -653,10 +657,10 @@ func (d *decoder) decodeUint128(size uint, offset uint) (*big.Int, uint, error)
return val, newOffset, nil
}

func uintFromBytes(prefix uint64, uintBytes []byte) uint64 {
func uintFromBytes(prefix uint, uintBytes []byte) uint {
val := prefix
for _, b := range uintBytes {
val = (val << 8) | uint64(b)
val = (val << 8) | uint(b)
}
return val
}
Expand Down
6 changes: 3 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ func (r *Reader) readNode(nodeNumber uint, index uint) (uint, error) {
baseOffset := nodeNumber * RecordSize / 4

var nodeBytes []byte
var prefix uint64
var prefix uint
switch RecordSize {
case 24:
offset := baseOffset + index*3
nodeBytes = r.buffer[offset : offset+3]
case 28:
prefix = uint64(r.buffer[baseOffset+3])
prefix = uint(r.buffer[baseOffset+3])
if index != 0 {
prefix &= 0x0F
} else {
Expand All @@ -225,7 +225,7 @@ func (r *Reader) readNode(nodeNumber uint, index uint) (uint, error) {
default:
return 0, newInvalidDatabaseError("unknown record size: %d", RecordSize)
}
return uint(uintFromBytes(prefix, nodeBytes)), nil
return uintFromBytes(prefix, nodeBytes), nil
}

func (r *Reader) retrieveData(pointer uint, result interface{}) error {
Expand Down

0 comments on commit d19f6d4

Please sign in to comment.