Skip to content

Commit

Permalink
cryptobyte: add ReadUint64 and AddUint64
Browse files Browse the repository at this point in the history
Fixes golang/go#53481.

Change-Id: Ic00eef498d1d3b5b0ca5c9c526fac7c26de30cf2
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/421014
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Filippo Valsorda <[email protected]>
Run-TryBot: hopehook <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Reviewed-by: Roland Shoemaker <[email protected]>
  • Loading branch information
hopehook authored and maisem committed Nov 15, 2022
1 parent 6d88230 commit 0f742a5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cryptobyte/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (b *Builder) AddUint32(v uint32) {
b.add(byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}

// AddUint64 appends a big-endian, 64-bit value to the byte string.
func (b *Builder) AddUint64(v uint64) {
b.add(byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}

// AddBytes appends a sequence of bytes to the byte string.
func (b *Builder) AddBytes(v []byte) {
b.add(v...)
Expand Down
24 changes: 22 additions & 2 deletions cryptobyte/cryptobyte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestUint24(t *testing.T) {
var s String = b.BytesOrPanic()
var v uint32
if !s.ReadUint24(&v) {
t.Error("ReadUint8() = false, want true")
t.Error("ReadUint24() = false, want true")
}
if v != 0xfffefd {
t.Errorf("v = %d, want fffefd", v)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestUint32(t *testing.T) {
var s String = b.BytesOrPanic()
var v uint32
if !s.ReadUint32(&v) {
t.Error("ReadUint8() = false, want true")
t.Error("ReadUint32() = false, want true")
}
if v != 0xfffefdfc {
t.Errorf("v = %x, want fffefdfc", v)
Expand All @@ -179,6 +179,26 @@ func TestUint32(t *testing.T) {
}
}

func TestUint64(t *testing.T) {
var b Builder
b.AddUint64(0xf2fefefcff3cfdfc)
if err := builderBytesEq(&b, 242, 254, 254, 252, 255, 60, 253, 252); err != nil {
t.Error(err)
}

var s String = b.BytesOrPanic()
var v uint64
if !s.ReadUint64(&v) {
t.Error("ReadUint64() = false, want true")
}
if v != 0xf2fefefcff3cfdfc {
t.Errorf("v = %x, want f2fefefcff3cfdfc", v)
}
if len(s) != 0 {
t.Errorf("len(s) = %d, want 0", len(s))
}
}

func TestUMultiple(t *testing.T) {
var b Builder
b.AddUint8(23)
Expand Down
11 changes: 11 additions & 0 deletions cryptobyte/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ func (s *String) ReadUint32(out *uint32) bool {
return true
}

// ReadUint64 decodes a big-endian, 64-bit value into out and advances over it.
// It reports whether the read was successful.
func (s *String) ReadUint64(out *uint64) bool {
v := s.read(8)
if v == nil {
return false
}
*out = uint64(v[0])<<56 | uint64(v[1])<<48 | uint64(v[2])<<40 | uint64(v[3])<<32 | uint64(v[4])<<24 | uint64(v[5])<<16 | uint64(v[6])<<8 | uint64(v[7])
return true
}

func (s *String) readUnsigned(out *uint32, length int) bool {
v := s.read(length)
if v == nil {
Expand Down

0 comments on commit 0f742a5

Please sign in to comment.