defs.go 3.1 KB

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