tlv.go 8.8 KB

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