// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package cryptobyte contains types that help with parsing and constructing // length-prefixed, binary messages, including ASN.1 DER. (The asn1 subpackage // contains useful ASN.1 constants.) // // The String type is for parsing. It wraps a []byte slice and provides helper // functions for consuming structures, value by value. // // The Builder type is for constructing messages. It providers helper functions // for appending values and also for appending length-prefixed submessages – // without having to worry about calculating the length prefix ahead of time. // // See the documentation and examples for the Builder and String types to get // started.
package cryptobyte // import "golang.org/x/crypto/cryptobyte" // String represents a string of bytes. It provides methods for parsing // fixed-length and length-prefixed values from it. type String []byte // read advances a String by n bytes and returns them. If less than n bytes // remain, it returns nil. func ( *String) ( int) []byte { if len(*) < || < 0 { return nil } := (*)[:] * = (*)[:] return } // Skip advances the String by n byte and reports whether it was successful. func ( *String) ( int) bool { return .read() != nil } // ReadUint8 decodes an 8-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint8) bool { := .read(1) if == nil { return false } * = uint8([0]) return true } // ReadUint16 decodes a big-endian, 16-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint16) bool { := .read(2) if == nil { return false } * = uint16([0])<<8 | uint16([1]) return true } // ReadUint24 decodes a big-endian, 24-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint32) bool { := .read(3) if == nil { return false } * = uint32([0])<<16 | uint32([1])<<8 | uint32([2]) return true } // ReadUint32 decodes a big-endian, 32-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint32) bool { := .read(4) if == nil { return false } * = uint32([0])<<24 | uint32([1])<<16 | uint32([2])<<8 | uint32([3]) return true } // ReadUint48 decodes a big-endian, 48-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint64) bool { := .read(6) if == nil { return false } * = uint64([0])<<40 | uint64([1])<<32 | uint64([2])<<24 | uint64([3])<<16 | uint64([4])<<8 | uint64([5]) return true } // ReadUint64 decodes a big-endian, 64-bit value into out and advances over it. // It reports whether the read was successful. func ( *String) ( *uint64) bool { := .read(8) if == nil { return false } * = uint64([0])<<56 | uint64([1])<<48 | uint64([2])<<40 | uint64([3])<<32 | uint64([4])<<24 | uint64([5])<<16 | uint64([6])<<8 | uint64([7]) return true } func ( *String) ( *uint32, int) bool { := .read() if == nil { return false } var uint32 for := 0; < ; ++ { <<= 8 |= uint32([]) } * = return true } func ( *String) ( int, *String) bool { := .read() if == nil { return false } var uint32 for , := range { = << 8 = | uint32() } := .read(int()) if == nil { return false } * = return true } // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value // into out and advances over it. It reports whether the read was successful. func ( *String) ( *String) bool { return .readLengthPrefixed(1, ) } // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit // length-prefixed value into out and advances over it. It reports whether the // read was successful. func ( *String) ( *String) bool { return .readLengthPrefixed(2, ) } // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit // length-prefixed value into out and advances over it. It reports whether // the read was successful. func ( *String) ( *String) bool { return .readLengthPrefixed(3, ) } // ReadBytes reads n bytes into out and advances over them. It reports // whether the read was successful. func ( *String) ( *[]byte, int) bool { := .read() if == nil { return false } * = return true } // CopyBytes copies len(out) bytes into out and advances over them. It reports // whether the copy operation was successful func ( *String) ( []byte) bool { := len() := .read() if == nil { return false } return copy(, ) == } // Empty reports whether the string does not contain any bytes. func ( String) () bool { return len() == 0 }