defs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // This package is the support library for the msgp code generator (http://github.com/tinylib/msgp).
  2. //
  3. // This package defines the utilites used by the msgp code generator for encoding and decoding MessagePack
  4. // from []byte and io.Reader/io.Writer types. Much of this package is devoted to helping the msgp code
  5. // generator implement the Marshaler/Unmarshaler and Encodable/Decodable interfaces.
  6. //
  7. // This package defines four "families" of functions:
  8. // - AppendXxxx() appends an object to a []byte in MessagePack encoding.
  9. // - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes.
  10. // - (*Writer).WriteXxxx() writes an object to the buffered *Writer type.
  11. // - (*Reader).ReadXxxx() reads an object from a buffered *Reader type.
  12. //
  13. // Once a type has satisfied the `Encodable` and `Decodable` interfaces,
  14. // it can be written and read from arbitrary `io.Writer`s and `io.Reader`s using
  15. //
  16. // msgp.Encode(io.Writer, msgp.Encodable)
  17. //
  18. // and
  19. //
  20. // msgp.Decode(io.Reader, msgp.Decodable)
  21. //
  22. // There are also methods for converting MessagePack to JSON without
  23. // an explicit de-serialization step.
  24. //
  25. // For additional tips, tricks, and gotchas, please visit
  26. // the wiki at http://github.com/tinylib/msgp
  27. package msgp
  28. // RT is the runtime interface for all types that can be encoded and decoded.
  29. type RT interface {
  30. Decodable
  31. Encodable
  32. Sizer
  33. Unmarshaler
  34. Marshaler
  35. }
  36. // PtrTo is the runtime interface for all types that can be encoded and decoded.
  37. type PtrTo[T any] interface {
  38. ~*T
  39. }
  40. // RTFor is the runtime interface for all types that can be encoded and decoded.
  41. // Use for generic types.
  42. type RTFor[T any] interface {
  43. PtrTo[T]
  44. RT
  45. }
  46. const (
  47. last4 = 0x0f
  48. first4 = 0xf0
  49. last5 = 0x1f
  50. first3 = 0xe0
  51. last7 = 0x7f
  52. // recursionLimit is the limit of recursive calls.
  53. // This limits the call depth of dynamic code, like Skip and interface conversions.
  54. recursionLimit = 100000
  55. )
  56. func isfixint(b byte) bool {
  57. return b>>7 == 0
  58. }
  59. func isnfixint(b byte) bool {
  60. return b&first3 == mnfixint
  61. }
  62. func isfixmap(b byte) bool {
  63. return b&first4 == mfixmap
  64. }
  65. func isfixarray(b byte) bool {
  66. return b&first4 == mfixarray
  67. }
  68. func isfixstr(b byte) bool {
  69. return b&first3 == mfixstr
  70. }
  71. func wfixint(u uint8) byte {
  72. return u & last7
  73. }
  74. func rfixint(b byte) uint8 {
  75. return b
  76. }
  77. func wnfixint(i int8) byte {
  78. return byte(i) | mnfixint
  79. }
  80. func rnfixint(b byte) int8 {
  81. return int8(b)
  82. }
  83. func rfixmap(b byte) uint8 {
  84. return b & last4
  85. }
  86. func wfixmap(u uint8) byte {
  87. return mfixmap | (u & last4)
  88. }
  89. func rfixstr(b byte) uint8 {
  90. return b & last5
  91. }
  92. func wfixstr(u uint8) byte {
  93. return (u & last5) | mfixstr
  94. }
  95. func rfixarray(b byte) uint8 {
  96. return (b & last4)
  97. }
  98. func wfixarray(u uint8) byte {
  99. return (u & last4) | mfixarray
  100. }
  101. // These are all the byte
  102. // prefixes defined by the
  103. // msgpack standard
  104. const (
  105. // 0XXXXXXX
  106. mfixint uint8 = 0x00
  107. // 111XXXXX
  108. mnfixint uint8 = 0xe0
  109. // 1000XXXX
  110. mfixmap uint8 = 0x80
  111. // 1001XXXX
  112. mfixarray uint8 = 0x90
  113. // 101XXXXX
  114. mfixstr uint8 = 0xa0
  115. mnil uint8 = 0xc0
  116. mfalse uint8 = 0xc2
  117. mtrue uint8 = 0xc3
  118. mbin8 uint8 = 0xc4
  119. mbin16 uint8 = 0xc5
  120. mbin32 uint8 = 0xc6
  121. mext8 uint8 = 0xc7
  122. mext16 uint8 = 0xc8
  123. mext32 uint8 = 0xc9
  124. mfloat32 uint8 = 0xca
  125. mfloat64 uint8 = 0xcb
  126. muint8 uint8 = 0xcc
  127. muint16 uint8 = 0xcd
  128. muint32 uint8 = 0xce
  129. muint64 uint8 = 0xcf
  130. mint8 uint8 = 0xd0
  131. mint16 uint8 = 0xd1
  132. mint32 uint8 = 0xd2
  133. mint64 uint8 = 0xd3
  134. mfixext1 uint8 = 0xd4
  135. mfixext2 uint8 = 0xd5
  136. mfixext4 uint8 = 0xd6
  137. mfixext8 uint8 = 0xd7
  138. mfixext16 uint8 = 0xd8
  139. mstr8 uint8 = 0xd9
  140. mstr16 uint8 = 0xda
  141. mstr32 uint8 = 0xdb
  142. marray16 uint8 = 0xdc
  143. marray32 uint8 = 0xdd
  144. mmap16 uint8 = 0xde
  145. mmap32 uint8 = 0xdf
  146. )