string.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 {
  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. func (s *String) readUnsigned(out *uint32, length int) bool {
  76. v := s.read(length)
  77. if v == nil {
  78. return false
  79. }
  80. var result uint32
  81. for i := 0; i < length; i++ {
  82. result <<= 8
  83. result |= uint32(v[i])
  84. }
  85. *out = result
  86. return true
  87. }
  88. func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
  89. lenBytes := s.read(lenLen)
  90. if lenBytes == nil {
  91. return false
  92. }
  93. var length uint32
  94. for _, b := range lenBytes {
  95. length = length << 8
  96. length = length | uint32(b)
  97. }
  98. if int(length) < 0 {
  99. // This currently cannot overflow because we read uint24 at most, but check
  100. // anyway in case that changes in the future.
  101. return false
  102. }
  103. v := s.read(int(length))
  104. if v == nil {
  105. return false
  106. }
  107. *outChild = v
  108. return true
  109. }
  110. // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
  111. // into out and advances over it. It reports whether the read was successful.
  112. func (s *String) ReadUint8LengthPrefixed(out *String) bool {
  113. return s.readLengthPrefixed(1, out)
  114. }
  115. // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
  116. // length-prefixed value into out and advances over it. It reports whether the
  117. // read was successful.
  118. func (s *String) ReadUint16LengthPrefixed(out *String) bool {
  119. return s.readLengthPrefixed(2, out)
  120. }
  121. // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
  122. // length-prefixed value into out and advances over it. It reports whether
  123. // the read was successful.
  124. func (s *String) ReadUint24LengthPrefixed(out *String) bool {
  125. return s.readLengthPrefixed(3, out)
  126. }
  127. // ReadBytes reads n bytes into out and advances over them. It reports
  128. // whether the read was successful.
  129. func (s *String) ReadBytes(out *[]byte, n int) bool {
  130. v := s.read(n)
  131. if v == nil {
  132. return false
  133. }
  134. *out = v
  135. return true
  136. }
  137. // CopyBytes copies len(out) bytes into out and advances over them. It reports
  138. // whether the copy operation was successful
  139. func (s *String) CopyBytes(out []byte) bool {
  140. n := len(out)
  141. v := s.read(n)
  142. if v == nil {
  143. return false
  144. }
  145. return copy(out, v) == n
  146. }
  147. // Empty reports whether the string does not contain any bytes.
  148. func (s String) Empty() bool {
  149. return len(s) == 0
  150. }