string.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 implements building and parsing of byte strings for
  5. // DER-encoded ASN.1 and TLS messages. See the examples for the Builder and
  6. // String types to get started.
  7. package cryptobyte // import "golang.org/x/crypto/cryptobyte"
  8. // String represents a string of bytes. It provides methods for parsing
  9. // fixed-length and length-prefixed values from it.
  10. type String []byte
  11. // read advances a String by n bytes and returns them. If less than n bytes
  12. // remain, it returns nil.
  13. func (s *String) read(n int) []byte {
  14. if len(*s) < n {
  15. return nil
  16. }
  17. v := (*s)[:n]
  18. *s = (*s)[n:]
  19. return v
  20. }
  21. // Skip advances the String by n byte and reports whether it was successful.
  22. func (s *String) Skip(n int) bool {
  23. return s.read(n) != nil
  24. }
  25. // ReadUint8 decodes an 8-bit value into out and advances over it. It
  26. // returns true on success and false on error.
  27. func (s *String) ReadUint8(out *uint8) bool {
  28. v := s.read(1)
  29. if v == nil {
  30. return false
  31. }
  32. *out = uint8(v[0])
  33. return true
  34. }
  35. // ReadUint16 decodes a big-endian, 16-bit value into out and advances over it.
  36. // It returns true on success and false on error.
  37. func (s *String) ReadUint16(out *uint16) bool {
  38. v := s.read(2)
  39. if v == nil {
  40. return false
  41. }
  42. *out = uint16(v[0])<<8 | uint16(v[1])
  43. return true
  44. }
  45. // ReadUint24 decodes a big-endian, 24-bit value into out and advances over it.
  46. // It returns true on success and false on error.
  47. func (s *String) ReadUint24(out *uint32) bool {
  48. v := s.read(3)
  49. if v == nil {
  50. return false
  51. }
  52. *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
  53. return true
  54. }
  55. // ReadUint32 decodes a big-endian, 32-bit value into out and advances over it.
  56. // It returns true on success and false on error.
  57. func (s *String) ReadUint32(out *uint32) bool {
  58. v := s.read(4)
  59. if v == nil {
  60. return false
  61. }
  62. *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
  63. return true
  64. }
  65. func (s *String) readUnsigned(out *uint32, length int) bool {
  66. v := s.read(length)
  67. if v == nil {
  68. return false
  69. }
  70. var result uint32
  71. for i := 0; i < length; i++ {
  72. result <<= 8
  73. result |= uint32(v[i])
  74. }
  75. *out = result
  76. return true
  77. }
  78. func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
  79. lenBytes := s.read(lenLen)
  80. if lenBytes == nil {
  81. return false
  82. }
  83. var length uint32
  84. for _, b := range lenBytes {
  85. length = length << 8
  86. length = length | uint32(b)
  87. }
  88. if int(length) < 0 {
  89. // This currently cannot overflow because we read uint24 at most, but check
  90. // anyway in case that changes in the future.
  91. return false
  92. }
  93. v := s.read(int(length))
  94. if v == nil {
  95. return false
  96. }
  97. *outChild = v
  98. return true
  99. }
  100. // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
  101. // into out and advances over it. It returns true on success and false on
  102. // error.
  103. func (s *String) ReadUint8LengthPrefixed(out *String) bool {
  104. return s.readLengthPrefixed(1, out)
  105. }
  106. // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
  107. // length-prefixed value into out and advances over it. It returns true on
  108. // success and false on error.
  109. func (s *String) ReadUint16LengthPrefixed(out *String) bool {
  110. return s.readLengthPrefixed(2, out)
  111. }
  112. // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
  113. // length-prefixed value into out and advances over it. It returns true on
  114. // success and false on error.
  115. func (s *String) ReadUint24LengthPrefixed(out *String) bool {
  116. return s.readLengthPrefixed(3, out)
  117. }
  118. // ReadBytes reads n bytes into out and advances over them. It returns true on
  119. // success and false and error.
  120. func (s *String) ReadBytes(out *[]byte, n int) bool {
  121. v := s.read(n)
  122. if v == nil {
  123. return false
  124. }
  125. *out = v
  126. return true
  127. }
  128. // CopyBytes copies len(out) bytes into out and advances over them. It returns
  129. // true on success and false on error.
  130. func (s *String) CopyBytes(out []byte) bool {
  131. n := len(out)
  132. v := s.read(n)
  133. if v == nil {
  134. return false
  135. }
  136. return copy(out, v) == n
  137. }
  138. // Empty reports whether the string does not contain any bytes.
  139. func (s String) Empty() bool {
  140. return len(s) == 0
  141. }