string.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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
  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. 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. v := s.read(int(length))
  99. if v == nil {
  100. return false
  101. }
  102. *outChild = v
  103. return true
  104. }
  105. // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
  106. // into out and advances over it. It reports whether the read was successful.
  107. func (s *String) ReadUint8LengthPrefixed(out *String) bool {
  108. return s.readLengthPrefixed(1, out)
  109. }
  110. // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
  111. // length-prefixed value into out and advances over it. It reports whether the
  112. // read was successful.
  113. func (s *String) ReadUint16LengthPrefixed(out *String) bool {
  114. return s.readLengthPrefixed(2, out)
  115. }
  116. // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
  117. // length-prefixed value into out and advances over it. It reports whether
  118. // the read was successful.
  119. func (s *String) ReadUint24LengthPrefixed(out *String) bool {
  120. return s.readLengthPrefixed(3, out)
  121. }
  122. // ReadBytes reads n bytes into out and advances over them. It reports
  123. // whether the read was successful.
  124. func (s *String) ReadBytes(out *[]byte, n int) bool {
  125. v := s.read(n)
  126. if v == nil {
  127. return false
  128. }
  129. *out = v
  130. return true
  131. }
  132. // CopyBytes copies len(out) bytes into out and advances over them. It reports
  133. // whether the copy operation was successful
  134. func (s *String) CopyBytes(out []byte) bool {
  135. n := len(out)
  136. v := s.read(n)
  137. if v == nil {
  138. return false
  139. }
  140. return copy(out, v) == n
  141. }
  142. // Empty reports whether the string does not contain any bytes.
  143. func (s String) Empty() bool {
  144. return len(s) == 0
  145. }