1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package cryptobyte implements building and parsing of byte strings for
6 // DER-encoded ASN.1 and TLS messages. See the examples for the Builder and
7 // String types to get started.
8 package cryptobyte // import "golang.org/x/crypto/cryptobyte"
10 // String represents a string of bytes. It provides methods for parsing
11 // fixed-length and length-prefixed values from it.
14 // read advances a String by n bytes and returns them. If less than n bytes
15 // remain, it returns nil.
16 func (s *String) read(n int) []byte {
25 // Skip advances the String by n byte and reports whether it was successful.
26 func (s *String) Skip(n int) bool {
27 return s.read(n) != nil
30 // ReadUint8 decodes an 8-bit value into out and advances over it. It
31 // returns true on success and false on error.
32 func (s *String) ReadUint8(out *uint8) bool {
41 // ReadUint16 decodes a big-endian, 16-bit value into out and advances over it.
42 // It returns true on success and false on error.
43 func (s *String) ReadUint16(out *uint16) bool {
48 *out = uint16(v[0])<<8 | uint16(v[1])
52 // ReadUint24 decodes a big-endian, 24-bit value into out and advances over it.
53 // It returns true on success and false on error.
54 func (s *String) ReadUint24(out *uint32) bool {
59 *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
63 // ReadUint32 decodes a big-endian, 32-bit value into out and advances over it.
64 // It returns true on success and false on error.
65 func (s *String) ReadUint32(out *uint32) bool {
70 *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
74 func (s *String) readUnsigned(out *uint32, length int) bool {
80 for i := 0; i < length; i++ {
82 result |= uint32(v[i])
88 func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
89 lenBytes := s.read(lenLen)
94 for _, b := range lenBytes {
96 length = length | uint32(b)
99 // This currently cannot overflow because we read uint24 at most, but check
100 // anyway in case that changes in the future.
103 v := s.read(int(length))
111 // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
112 // into out and advances over it. It returns true on success and false on
114 func (s *String) ReadUint8LengthPrefixed(out *String) bool {
115 return s.readLengthPrefixed(1, out)
118 // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
119 // length-prefixed value into out and advances over it. It returns true on
120 // success and false on error.
121 func (s *String) ReadUint16LengthPrefixed(out *String) bool {
122 return s.readLengthPrefixed(2, out)
125 // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
126 // length-prefixed value into out and advances over it. It returns true on
127 // success and false on error.
128 func (s *String) ReadUint24LengthPrefixed(out *String) bool {
129 return s.readLengthPrefixed(3, out)
132 // ReadBytes reads n bytes into out and advances over them. It returns true on
133 // success and false and error.
134 func (s *String) ReadBytes(out *[]byte, n int) bool {
143 // CopyBytes copies len(out) bytes into out and advances over them. It returns
144 // true on success and false on error.
145 func (s *String) CopyBytes(out []byte) bool {
151 return copy(out, v) == n
154 // Empty reports whether the string does not contain any bytes.
155 func (s String) Empty() bool {