tlv.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package wire
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. )
  7. // TLV represents dynamically typed data in the OSCAR protocol. Each message
  8. // consists of a tag (or key) and a blob value. TLVs are typically grouped
  9. // together in arrays.
  10. type TLV struct {
  11. Tag uint16
  12. Value []byte `oscar:"len_prefix=uint16"`
  13. }
  14. // NewTLVBE creates a new TLV. Values are marshalled in big-endian order.
  15. func NewTLVBE(tag uint16, val any) TLV {
  16. return newTLV(tag, val, binary.BigEndian)
  17. }
  18. // NewTLVLE creates a new TLV. Values are marshalled in little-endian order.
  19. func NewTLVLE(tag uint16, val any) TLV {
  20. return newTLV(tag, val, binary.LittleEndian)
  21. }
  22. func newTLV(tag uint16, val any, order binary.ByteOrder) TLV {
  23. t := TLV{
  24. Tag: tag,
  25. }
  26. if _, ok := val.([]byte); ok {
  27. t.Value = val.([]byte)
  28. } else {
  29. buf := &bytes.Buffer{}
  30. switch order {
  31. case binary.BigEndian:
  32. if err := MarshalBE(val, buf); err != nil {
  33. panic(fmt.Sprintf("unable to create TLV: %s", err.Error()))
  34. }
  35. case binary.LittleEndian:
  36. if err := MarshalLE(val, buf); err != nil {
  37. panic(fmt.Sprintf("unable to create TLV: %s", err.Error()))
  38. }
  39. }
  40. t.Value = buf.Bytes()
  41. }
  42. return t
  43. }
  44. // TLVRestBlock is a type of TLV array that does not have any length
  45. // information encoded in the blob. This typically means that a given offset in
  46. // the SNAC payload, the TLV occupies the "rest" of the payload.
  47. type TLVRestBlock struct {
  48. TLVList
  49. }
  50. // TLVBlock is a type of TLV array that has the TLV element count encoded as a
  51. // 2-byte value at the beginning of the encoded blob.
  52. type TLVBlock struct {
  53. TLVList `oscar:"count_prefix=uint16"`
  54. }
  55. // TLVLBlock is a type of TLV array that has the TLV blob byte-length encoded
  56. // as a 2-byte value at the beginning of the encoded blob.
  57. type TLVLBlock struct {
  58. TLVList `oscar:"len_prefix=uint16"`
  59. }
  60. // TLVList is a list of TLV elements. It provides methods to append and access
  61. // TLVs in the array. It provides methods that decode the data blob into the
  62. // appropriate type at runtime. The caller assumes the TLV data type at runtime
  63. // based on the protocol specification. These methods are not safe for
  64. // read-write access by multiple goroutines.
  65. type TLVList []TLV
  66. // Append adds a TLV to the end of the TLV list.
  67. func (s *TLVList) Append(tlv TLV) {
  68. *s = append(*s, tlv)
  69. }
  70. // AppendList adds a TLV list to the end of the TLV list.
  71. func (s *TLVList) AppendList(tlvs []TLV) {
  72. *s = append(*s, tlvs...)
  73. }
  74. // HasTag indicates if a TLV list has a tag.
  75. func (s *TLVList) HasTag(tag uint16) bool {
  76. for _, tlv := range *s {
  77. if tag == tlv.Tag {
  78. return true
  79. }
  80. }
  81. return false
  82. }
  83. // String retrieves the string value associated with the specified tag from the
  84. // TLVList.
  85. //
  86. // If the specified tag is found, the function returns the associated string
  87. // value and true. If the tag is not found, the function returns an empty
  88. // string and false.
  89. func (s *TLVList) String(tag uint16) (string, bool) {
  90. for _, tlv := range *s {
  91. if tag == tlv.Tag {
  92. return string(tlv.Value), true
  93. }
  94. }
  95. return "", false
  96. }
  97. // ICQString retrieves the ICQ string value associated with the specified tag
  98. // from the TLVList.
  99. //
  100. // An ICQ string is a string that is prefixed with its length and ends with a
  101. // null terminator.
  102. //
  103. // If the specified tag is found, the function returns the extracted string
  104. // value and true. If the tag is not found or the string is malformed, the
  105. // function returns an empty string and false.
  106. func (s *TLVList) ICQString(tag uint16) (string, bool) {
  107. // Find the TLV entry with the specified tag
  108. for _, tlv := range *s {
  109. if tag != tlv.Tag {
  110. continue
  111. }
  112. // Ensure the value is long enough to contain a valid length prefix and value
  113. if len(tlv.Value) < 3 {
  114. break
  115. }
  116. // Extract the length prefix (first 2 bytes) as a uint16
  117. expectedLength := binary.LittleEndian.Uint16(tlv.Value[0:2])
  118. // Extract the actual string value, excluding the length prefix
  119. value := tlv.Value[2:]
  120. // Check if the length matches the value length (including the null terminator)
  121. if int(expectedLength) != len(value) {
  122. break
  123. }
  124. // Remove the null terminator
  125. return string(value[:len(value)-1]), true
  126. }
  127. // Tag not found
  128. return "", false
  129. }
  130. // Bytes retrieves the byte payload associated with the specified tag from the
  131. // TLVList.
  132. //
  133. // If the specified tag is found, the function returns the associated byte
  134. // slice and true. If the tag is not found, the function returns nil and false.
  135. func (s *TLVList) Bytes(tag uint16) ([]byte, bool) {
  136. for _, tlv := range *s {
  137. if tag == tlv.Tag {
  138. return tlv.Value, true
  139. }
  140. }
  141. return nil, false
  142. }
  143. // Uint8 retrieves a byte value from the TLVList associated with the specified
  144. // tag.
  145. //
  146. // If the specified tag is found, the function returns the associated value
  147. // as a uint8 and true. If the tag is not found, the function returns 0 and
  148. // false.
  149. func (s *TLVList) Uint8(tag uint16) (uint8, bool) {
  150. for _, tlv := range *s {
  151. if tag == tlv.Tag {
  152. if len(tlv.Value) > 0 {
  153. return tlv.Value[0], true
  154. }
  155. }
  156. }
  157. return 0, false
  158. }
  159. // Uint16BE retrieves a 16-bit unsigned integer value from the TLVList
  160. // associated with the specified tag, interpreting the bytes in big-endian
  161. // format.
  162. //
  163. // If the specified tag is found, the function returns the associated value
  164. // as a uint16 and true. If the tag is not found, the function returns 0 and
  165. // false.
  166. func (s *TLVList) Uint16BE(tag uint16) (uint16, bool) {
  167. return s.uint16(tag, binary.BigEndian)
  168. }
  169. // Uint16LE retrieves a 16-bit unsigned integer value from the TLVList
  170. // associated with the specified tag, interpreting the bytes in little-endian
  171. // format.
  172. //
  173. // If the specified tag is found, the function returns the associated value
  174. // as a uint16 and true. If the tag is not found, the function returns 0 and
  175. // false.
  176. func (s *TLVList) Uint16LE(tag uint16) (uint16, bool) {
  177. return s.uint16(tag, binary.LittleEndian)
  178. }
  179. func (s *TLVList) uint16(tag uint16, order binary.ByteOrder) (uint16, bool) {
  180. for _, tlv := range *s {
  181. if tag == tlv.Tag {
  182. return order.Uint16(tlv.Value), true
  183. }
  184. }
  185. return 0, false
  186. }
  187. // Uint32BE retrieves a 32-bit unsigned integer value from the TLVList
  188. // associated with the specified tag, interpreting the bytes in big-endian format.
  189. //
  190. // If the specified tag is found, the function returns the associated value
  191. // as a uint32 and true. If the tag is not found, the function returns 0 and false.
  192. func (s *TLVList) Uint32BE(tag uint16) (uint32, bool) {
  193. return s.uint32(tag, binary.BigEndian)
  194. }
  195. // Uint32LE retrieves a 32-bit unsigned integer value from the TLVList
  196. // associated with the specified tag, interpreting the bytes in little-endian format.
  197. //
  198. // If the specified tag is found, the function returns the associated value
  199. // as a uint32 and true. If the tag is not found, the function returns 0 and false.
  200. func (s *TLVList) Uint32LE(tag uint16) (uint32, bool) {
  201. return s.uint32(tag, binary.LittleEndian)
  202. }
  203. func (s *TLVList) uint32(tag uint16, order binary.ByteOrder) (uint32, bool) {
  204. for _, tlv := range *s {
  205. if tag == tlv.Tag {
  206. return order.Uint32(tlv.Value), true
  207. }
  208. }
  209. return 0, false
  210. }