tlv.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. func (t TLV) ICQString() string {
  15. // Ensure the value is long enough to contain a valid length prefix and value
  16. if len(t.Value) < 3 {
  17. return ""
  18. }
  19. // Extract the length prefix (first 2 bytes) as a uint16
  20. expectedLength := binary.LittleEndian.Uint16(t.Value[0:2])
  21. // Extract the actual string value, excluding the length prefix
  22. value := t.Value[2:]
  23. // Check if the length matches the value length (including the null terminator)
  24. if int(expectedLength) != len(value) {
  25. return ""
  26. }
  27. // Remove the null terminator
  28. return string(value[:len(value)-1])
  29. }
  30. // Uint8 returns a uint8 representation of the TLV.
  31. func (t TLV) Uint8() uint8 {
  32. if len(t.Value) > 0 {
  33. return t.Value[0]
  34. }
  35. return 0
  36. }
  37. // Uint16LE returns a uint16 little-endian representation of the TLV.
  38. func (t TLV) Uint16LE() uint16 {
  39. return binary.LittleEndian.Uint16(t.Value)
  40. }
  41. // Uint16BE returns a uint16 big-endian representation of the TLV.
  42. func (t TLV) Uint16BE() uint16 {
  43. return binary.BigEndian.Uint16(t.Value)
  44. }
  45. // Uint32LE returns a uint32 little-endian representation of the TLV.
  46. func (t TLV) Uint32LE() uint32 {
  47. return binary.LittleEndian.Uint32(t.Value)
  48. }
  49. // Uint32BE returns a uint32 big-endian representation of the TLV.
  50. func (t TLV) Uint32BE() uint32 {
  51. return binary.BigEndian.Uint32(t.Value)
  52. }
  53. // NewTLVBE creates a new TLV. Values are marshalled in big-endian order.
  54. func NewTLVBE(tag uint16, val any) TLV {
  55. return newTLV(tag, val, binary.BigEndian)
  56. }
  57. // NewTLVLE creates a new TLV. Values are marshalled in little-endian order.
  58. func NewTLVLE(tag uint16, val any) TLV {
  59. return newTLV(tag, val, binary.LittleEndian)
  60. }
  61. func newTLV(tag uint16, val any, order binary.ByteOrder) TLV {
  62. t := TLV{
  63. Tag: tag,
  64. }
  65. if _, ok := val.([]byte); ok {
  66. t.Value = val.([]byte)
  67. } else {
  68. buf := &bytes.Buffer{}
  69. switch order {
  70. case binary.BigEndian:
  71. if err := MarshalBE(val, buf); err != nil {
  72. panic(fmt.Sprintf("unable to create TLV: %s", err.Error()))
  73. }
  74. case binary.LittleEndian:
  75. if err := MarshalLE(val, buf); err != nil {
  76. panic(fmt.Sprintf("unable to create TLV: %s", err.Error()))
  77. }
  78. }
  79. t.Value = buf.Bytes()
  80. }
  81. return t
  82. }
  83. // TLVRestBlock is a type of TLV array that does not have any length
  84. // information encoded in the blob. This typically means that a given offset in
  85. // the SNAC payload, the TLV occupies the "rest" of the payload.
  86. type TLVRestBlock struct {
  87. TLVList
  88. }
  89. // TLVBlock is a type of TLV array that has the TLV element count encoded as a
  90. // 2-byte value at the beginning of the encoded blob.
  91. type TLVBlock struct {
  92. TLVList `oscar:"count_prefix=uint16"`
  93. }
  94. // TLVLBlock is a type of TLV array that has the TLV blob byte-length encoded
  95. // as a 2-byte value at the beginning of the encoded blob.
  96. type TLVLBlock struct {
  97. TLVList `oscar:"len_prefix=uint16"`
  98. }
  99. // TLVList is a list of TLV elements. It provides methods to append and access
  100. // TLVs in the array. It provides methods that decode the data blob into the
  101. // appropriate type at runtime. The caller assumes the TLV data type at runtime
  102. // based on the protocol specification. These methods are not safe for
  103. // read-write access by multiple goroutines.
  104. type TLVList []TLV
  105. // Append adds a TLV to the end of the TLV list.
  106. func (s *TLVList) Append(tlv TLV) {
  107. *s = append(*s, tlv)
  108. }
  109. // AppendList adds a TLV list to the end of the TLV list.
  110. func (s *TLVList) AppendList(tlvs []TLV) {
  111. *s = append(*s, tlvs...)
  112. }
  113. // HasTag indicates if a TLV list has a tag.
  114. func (s *TLVList) HasTag(tag uint16) bool {
  115. for _, tlv := range *s {
  116. if tag == tlv.Tag {
  117. return true
  118. }
  119. }
  120. return false
  121. }
  122. // Replace updates the values of TLVs in the list with the same tag as new. If
  123. // no matching tag is found, the list remains unchanged.
  124. func (s *TLVList) Replace(new TLV) {
  125. for i, old := range *s {
  126. if old.Tag == new.Tag {
  127. (*s)[i].Value = new.Value
  128. }
  129. }
  130. }
  131. // String retrieves the string value associated with the specified tag from the
  132. // TLVList.
  133. //
  134. // If the specified tag is found, the function returns the associated string
  135. // value and true. If the tag is not found, the function returns an empty
  136. // string and false.
  137. func (s *TLVList) String(tag uint16) (string, bool) {
  138. for _, tlv := range *s {
  139. if tag == tlv.Tag {
  140. return string(tlv.Value), true
  141. }
  142. }
  143. return "", false
  144. }
  145. // ICQString retrieves the ICQ string value associated with the specified tag
  146. // from the TLVList.
  147. //
  148. // An ICQ string is a string that is prefixed with its length and ends with a
  149. // null terminator.
  150. //
  151. // If the specified tag is found, the function returns the extracted string
  152. // value and true. If the tag is not found or the string is malformed, the
  153. // function returns an empty string and false.
  154. func (s *TLVList) ICQString(tag uint16) (string, bool) {
  155. // Find the TLV entry with the specified tag
  156. for _, tlv := range *s {
  157. if tag != tlv.Tag {
  158. continue
  159. }
  160. return tlv.ICQString(), true
  161. }
  162. // Tag not found
  163. return "", false
  164. }
  165. // Bytes retrieves the byte payload associated with the specified tag from the
  166. // TLVList.
  167. //
  168. // If the specified tag is found, the function returns the associated byte
  169. // slice and true. If the tag is not found, the function returns nil and false.
  170. func (s *TLVList) Bytes(tag uint16) ([]byte, bool) {
  171. for _, tlv := range *s {
  172. if tag == tlv.Tag {
  173. return tlv.Value, true
  174. }
  175. }
  176. return nil, false
  177. }
  178. // Uint8 retrieves a byte value from the TLVList associated with the specified
  179. // tag.
  180. //
  181. // If the specified tag is found, the function returns the associated value
  182. // as a uint8 and true. If the tag is not found, the function returns 0 and
  183. // false.
  184. func (s *TLVList) Uint8(tag uint16) (uint8, bool) {
  185. for _, tlv := range *s {
  186. if tag == tlv.Tag {
  187. return tlv.Uint8(), true
  188. }
  189. }
  190. return 0, false
  191. }
  192. // Uint16BE retrieves a 16-bit unsigned integer value from the TLVList
  193. // associated with the specified tag, interpreting the bytes in big-endian
  194. // format.
  195. //
  196. // If the specified tag is found, the function returns the associated value
  197. // as a uint16 and true. If the tag is not found, the function returns 0 and
  198. // false.
  199. func (s *TLVList) Uint16BE(tag uint16) (uint16, bool) {
  200. for _, tlv := range *s {
  201. if tag == tlv.Tag {
  202. return tlv.Uint16BE(), true
  203. }
  204. }
  205. return 0, false
  206. }
  207. // Uint16LE retrieves a 16-bit unsigned integer value from the TLVList
  208. // associated with the specified tag, interpreting the bytes in little-endian
  209. // format.
  210. //
  211. // If the specified tag is found, the function returns the associated value
  212. // as a uint16 and true. If the tag is not found, the function returns 0 and
  213. // false.
  214. func (s *TLVList) Uint16LE(tag uint16) (uint16, bool) {
  215. for _, tlv := range *s {
  216. if tag == tlv.Tag {
  217. return tlv.Uint16LE(), true
  218. }
  219. }
  220. return 0, false
  221. }
  222. // Uint32BE retrieves a 32-bit unsigned integer value from the TLVList
  223. // associated with the specified tag, interpreting the bytes in big-endian format.
  224. //
  225. // If the specified tag is found, the function returns the associated value
  226. // as a uint32 and true. If the tag is not found, the function returns 0 and false.
  227. func (s *TLVList) Uint32BE(tag uint16) (uint32, bool) {
  228. for _, tlv := range *s {
  229. if tag == tlv.Tag {
  230. return tlv.Uint32BE(), true
  231. }
  232. }
  233. return 0, false
  234. }
  235. // Uint32LE retrieves a 32-bit unsigned integer value from the TLVList
  236. // associated with the specified tag, interpreting the bytes in little-endian format.
  237. //
  238. // If the specified tag is found, the function returns the associated value
  239. // as a uint32 and true. If the tag is not found, the function returns 0 and false.
  240. func (s *TLVList) Uint32LE(tag uint16) (uint32, bool) {
  241. for _, tlv := range *s {
  242. if tag == tlv.Tag {
  243. return tlv.Uint32LE(), true
  244. }
  245. }
  246. return 0, false
  247. }
  248. // Uint16SliceBE retrieves a slice of 16-bit unsigned integer values from the TLVList
  249. // associated with the specified tag, interpreting the bytes in big-endian format.
  250. //
  251. // If the specificed tag is found, the function returns the associated value as a
  252. // uint16 slice and true. If the tag is not found, the function returns an empty
  253. // slice and false.
  254. func (s *TLVList) Uint16SliceBE(tag uint16) ([]uint16, bool) {
  255. for _, tlv := range *s {
  256. if tag == tlv.Tag {
  257. outputLen := len(tlv.Value) / 2
  258. outputSlice := make([]uint16, outputLen)
  259. for i := range outputLen {
  260. chunk := tlv.Value[i*2 : (i*2)+2]
  261. outputSlice[i] = binary.BigEndian.Uint16(chunk)
  262. }
  263. return outputSlice, true
  264. }
  265. }
  266. return []uint16{}, false
  267. }