Skip to content

Commit 5757bc0

Browse files
hopehookgopherbot
authored andcommitted
cryptobyte: add ReadUint64 and AddUint64
Fixes golang/go#53481. Change-Id: Ic00eef498d1d3b5b0ca5c9c526fac7c26de30cf2 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/421014 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org> Run-TryBot: hopehook <hopehook@qq.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org>
1 parent bc19a97 commit 5757bc0

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

cryptobyte/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (b *Builder) AddUint32(v uint32) {
9595
b.add(byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
9696
}
9797

98+
// AddUint64 appends a big-endian, 64-bit value to the byte string.
99+
func (b *Builder) AddUint64(v uint64) {
100+
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))
101+
}
102+
98103
// AddBytes appends a sequence of bytes to the byte string.
99104
func (b *Builder) AddBytes(v []byte) {
100105
b.add(v...)

cryptobyte/cryptobyte_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestUint24(t *testing.T) {
141141
var s String = b.BytesOrPanic()
142142
var v uint32
143143
if !s.ReadUint24(&v) {
144-
t.Error("ReadUint8() = false, want true")
144+
t.Error("ReadUint24() = false, want true")
145145
}
146146
if v != 0xfffefd {
147147
t.Errorf("v = %d, want fffefd", v)
@@ -169,7 +169,7 @@ func TestUint32(t *testing.T) {
169169
var s String = b.BytesOrPanic()
170170
var v uint32
171171
if !s.ReadUint32(&v) {
172-
t.Error("ReadUint8() = false, want true")
172+
t.Error("ReadUint32() = false, want true")
173173
}
174174
if v != 0xfffefdfc {
175175
t.Errorf("v = %x, want fffefdfc", v)
@@ -179,6 +179,26 @@ func TestUint32(t *testing.T) {
179179
}
180180
}
181181

182+
func TestUint64(t *testing.T) {
183+
var b Builder
184+
b.AddUint64(0xf2fefefcff3cfdfc)
185+
if err := builderBytesEq(&b, 242, 254, 254, 252, 255, 60, 253, 252); err != nil {
186+
t.Error(err)
187+
}
188+
189+
var s String = b.BytesOrPanic()
190+
var v uint64
191+
if !s.ReadUint64(&v) {
192+
t.Error("ReadUint64() = false, want true")
193+
}
194+
if v != 0xf2fefefcff3cfdfc {
195+
t.Errorf("v = %x, want f2fefefcff3cfdfc", v)
196+
}
197+
if len(s) != 0 {
198+
t.Errorf("len(s) = %d, want 0", len(s))
199+
}
200+
}
201+
182202
func TestUMultiple(t *testing.T) {
183203
var b Builder
184204
b.AddUint8(23)

cryptobyte/string.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func (s *String) ReadUint32(out *uint32) bool {
8181
return true
8282
}
8383

84+
// ReadUint64 decodes a big-endian, 64-bit value into out and advances over it.
85+
// It reports whether the read was successful.
86+
func (s *String) ReadUint64(out *uint64) bool {
87+
v := s.read(8)
88+
if v == nil {
89+
return false
90+
}
91+
*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])
92+
return true
93+
}
94+
8495
func (s *String) readUnsigned(out *uint32, length int) bool {
8596
v := s.read(length)
8697
if v == nil {

0 commit comments

Comments
 (0)