string.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.
  4. // Package cryptobyte contains types that help with parsing and constructing
  5. // length-prefixed, binary messages, including ASN.1 DER. (The asn1 subpackage
  6. // contains useful ASN.1 constants.)
  7. //
  8. // The String type is for parsing. It wraps a []byte slice and provides helper
  9. // functions for consuming structures, value by value.
  10. //
  11. // The Builder type is for constructing messages. It providers helper functions
  12. // for appending values and also for appending length-prefixed submessages –
  13. // without having to worry about calculating the length prefix ahead of time.
  14. //
  15. // See the documentation and examples for the Builder and String types to get
  16. // started.
  17. package cryptobyte // import "golang.org/x/crypto/cryptobyte"
  18. // String represents a string of bytes. It provides methods for parsing
  19. // fixed-length and length-prefixed values from it.
  20. type String []byte
  21. // read advances a String by n bytes and returns them. If less than n bytes
  22. // remain, it returns nil.
  23. func (s *String) read(n int) []byte {
  24. if len(*s) < n || n < 0 {
  25. return nil
  26. }
  27. v := (*s)[:n]
  28. *s = (*s)[n:]
  29. return v
  30. }
  31. // Skip advances the String by n byte and reports whether it was successful.
  32. func (s *String) Skip(n int) bool {
  33. return s.read(n) != nil
  34. }
  35. // ReadUint8 decodes an 8-bit value into out and advances over it.
  36. // It reports whether the read was successful.
  37. func (s *String) ReadUint8(out *uint8) bool {
  38. v := s.read(1)
  39. if v == nil {
  40. return false
  41. }
  42. *out = uint8(v[0])
  43. return true
  44. }
  45. // ReadUint16 decodes a big-endian, 16-bit value into out and advances over it.
  46. // It reports whether the read was successful.
  47. func (s *String) ReadUint16(out *uint16) bool {
  48. v := s.read(2)
  49. if v == nil {
  50. return false
  51. }
  52. *out = uint16(v[0])<<8 | uint16(v[1])
  53. return true
  54. }
  55. // ReadUint24 decodes a big-endian, 24-bit value into out and advances over it.
  56. // It reports whether the read was successful.
  57. func (s *String) ReadUint24(out *uint32) bool {
  58. v := s.read(3)
  59. if v == nil {
  60. return false
  61. }
  62. *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
  63. return true
  64. }
  65. // ReadUint32 decodes a big-endian, 32-bit value into out and advances over it.
  66. // It reports whether the read was successful.
  67. func (s *String) ReadUint32(out *uint32) bool {
  68. v := s.read(4)
  69. if v == nil {
  70. return false
  71. }
  72. *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
  73. return true
  74. }
  75. // ReadUint64 decodes a big-endian, 64-bit value into out and advances over it.
  76. // It reports whether the read was successful.
  77. func (s *String) ReadUint64(out *uint64) bool {
  78. v := s.read(8)
  79. if v == nil {
  80. return false
  81. }
  82. *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])
  83. return true
  84. }
  85. func (s *String) readUnsigned(out *uint32, length int) bool {
  86. v := s.read(length)
  87. if v == nil {
  88. return false
  89. }
  90. var result uint32
  91. for i := 0; i < length; i++ {
  92. result <<= 8
  93. result |= uint32(v[i])
  94. }
  95. *out = result
  96. return true
  97. }
  98. func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
  99. lenBytes := s.read(lenLen)
  100. if lenBytes == nil {
  101. return false
  102. }
  103. var length uint32
  104. for _, b := range lenBytes {
  105. length = length << 8
  106. length = length | uint32(b)
  107. }
  108. v := s.read(int(length))
  109. if v == nil {
  110. return false
  111. }
  112. *outChild = v
  113. return true
  114. }
  115. // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
  116. // into out and advances over it. It reports whether the read was successful.
  117. func (s *String) ReadUint8LengthPrefixed(out *String) bool {
  118. return s.readLengthPrefixed(1, out)
  119. }
  120. // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
  121. // length-prefixed value into out and advances over it. It reports whether the
  122. // read was successful.
  123. func (s *String) ReadUint16LengthPrefixed(out *String) bool {
  124. return s.readLengthPrefixed(2, out)
  125. }
  126. // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
  127. // length-prefixed value into out and advances over it. It reports whether
  128. // the read was successful.
  129. func (s *String) ReadUint24LengthPrefixed(out *String) bool {
  130. return s.readLengthPrefixed(3, out)
  131. }
  132. // ReadBytes reads n bytes into out and advances over them. It reports
  133. // whether the read was successful.
  134. func (s *String) ReadBytes(out *[]byte, n int) bool {
  135. v := s.read(n)
  136. if v == nil {
  137. return false
  138. }
  139. *out = v
  140. return true
  141. }
  142. // CopyBytes copies len(out) bytes into out and advances over them. It reports
  143. // whether the copy operation was successful
  144. func (s *String) CopyBytes(out []byte) bool {
  145. n := len(out)
  146. v := s.read(n)
  147. if v == nil {
  148. return false
  149. }
  150. return copy(out, v) == n
  151. }
  152. // Empty reports whether the string does not contain any bytes.
  153. func (s String) Empty() bool {
  154. return len(s) == 0
  155. }