defs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. const last4 = 0x0f
  29. const first4 = 0xf0
  30. const last5 = 0x1f
  31. const first3 = 0xe0
  32. const last7 = 0x7f
  33. func isfixint(b byte) bool {
  34. return b>>7 == 0
  35. }
  36. func isnfixint(b byte) bool {
  37. return b&first3 == mnfixint
  38. }
  39. func isfixmap(b byte) bool {
  40. return b&first4 == mfixmap
  41. }
  42. func isfixarray(b byte) bool {
  43. return b&first4 == mfixarray
  44. }
  45. func isfixstr(b byte) bool {
  46. return b&first3 == mfixstr
  47. }
  48. func wfixint(u uint8) byte {
  49. return u & last7
  50. }
  51. func rfixint(b byte) uint8 {
  52. return b
  53. }
  54. func wnfixint(i int8) byte {
  55. return byte(i) | mnfixint
  56. }
  57. func rnfixint(b byte) int8 {
  58. return int8(b)
  59. }
  60. func rfixmap(b byte) uint8 {
  61. return b & last4
  62. }
  63. func wfixmap(u uint8) byte {
  64. return mfixmap | (u & last4)
  65. }
  66. func rfixstr(b byte) uint8 {
  67. return b & last5
  68. }
  69. func wfixstr(u uint8) byte {
  70. return (u & last5) | mfixstr
  71. }
  72. func rfixarray(b byte) uint8 {
  73. return (b & last4)
  74. }
  75. func wfixarray(u uint8) byte {
  76. return (u & last4) | mfixarray
  77. }
  78. // These are all the byte
  79. // prefixes defined by the
  80. // msgpack standard
  81. const (
  82. // 0XXXXXXX
  83. mfixint uint8 = 0x00
  84. // 111XXXXX
  85. mnfixint uint8 = 0xe0
  86. // 1000XXXX
  87. mfixmap uint8 = 0x80
  88. // 1001XXXX
  89. mfixarray uint8 = 0x90
  90. // 101XXXXX
  91. mfixstr uint8 = 0xa0
  92. mnil uint8 = 0xc0
  93. mfalse uint8 = 0xc2
  94. mtrue uint8 = 0xc3
  95. mbin8 uint8 = 0xc4
  96. mbin16 uint8 = 0xc5
  97. mbin32 uint8 = 0xc6
  98. mext8 uint8 = 0xc7
  99. mext16 uint8 = 0xc8
  100. mext32 uint8 = 0xc9
  101. mfloat32 uint8 = 0xca
  102. mfloat64 uint8 = 0xcb
  103. muint8 uint8 = 0xcc
  104. muint16 uint8 = 0xcd
  105. muint32 uint8 = 0xce
  106. muint64 uint8 = 0xcf
  107. mint8 uint8 = 0xd0
  108. mint16 uint8 = 0xd1
  109. mint32 uint8 = 0xd2
  110. mint64 uint8 = 0xd3
  111. mfixext1 uint8 = 0xd4
  112. mfixext2 uint8 = 0xd5
  113. mfixext4 uint8 = 0xd6
  114. mfixext8 uint8 = 0xd7
  115. mfixext16 uint8 = 0xd8
  116. mstr8 uint8 = 0xd9
  117. mstr16 uint8 = 0xda
  118. mstr32 uint8 = 0xdb
  119. marray16 uint8 = 0xdc
  120. marray32 uint8 = 0xdd
  121. mmap16 uint8 = 0xde
  122. mmap32 uint8 = 0xdf
  123. )