123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package cryptobyte
- type String []byte
- func (s *String) read(n int) []byte {
- if len(*s) < n {
- return nil
- }
- v := (*s)[:n]
- *s = (*s)[n:]
- return v
- }
- func (s *String) Skip(n int) bool {
- return s.read(n) != nil
- }
- func (s *String) ReadUint8(out *uint8) bool {
- v := s.read(1)
- if v == nil {
- return false
- }
- *out = uint8(v[0])
- return true
- }
- func (s *String) ReadUint16(out *uint16) bool {
- v := s.read(2)
- if v == nil {
- return false
- }
- *out = uint16(v[0])<<8 | uint16(v[1])
- return true
- }
- func (s *String) ReadUint24(out *uint32) bool {
- v := s.read(3)
- if v == nil {
- return false
- }
- *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
- return true
- }
- func (s *String) ReadUint32(out *uint32) bool {
- v := s.read(4)
- if v == nil {
- return false
- }
- *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
- return true
- }
- func (s *String) readUnsigned(out *uint32, length int) bool {
- v := s.read(length)
- if v == nil {
- return false
- }
- var result uint32
- for i := 0; i < length; i++ {
- result <<= 8
- result |= uint32(v[i])
- }
- *out = result
- return true
- }
- func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
- lenBytes := s.read(lenLen)
- if lenBytes == nil {
- return false
- }
- var length uint32
- for _, b := range lenBytes {
- length = length << 8
- length = length | uint32(b)
- }
- if int(length) < 0 {
-
-
- return false
- }
- v := s.read(int(length))
- if v == nil {
- return false
- }
- *outChild = v
- return true
- }
- func (s *String) ReadUint8LengthPrefixed(out *String) bool {
- return s.readLengthPrefixed(1, out)
- }
- func (s *String) ReadUint16LengthPrefixed(out *String) bool {
- return s.readLengthPrefixed(2, out)
- }
- func (s *String) ReadUint24LengthPrefixed(out *String) bool {
- return s.readLengthPrefixed(3, out)
- }
- func (s *String) ReadBytes(out *[]byte, n int) bool {
- v := s.read(n)
- if v == nil {
- return false
- }
- *out = v
- return true
- }
- func (s *String) CopyBytes(out []byte) bool {
- n := len(out)
- v := s.read(n)
- if v == nil {
- return false
- }
- return copy(out, v) == n
- }
- func (s String) Empty() bool {
- return len(s) == 0
- }
|