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// msgp.Encode(io.Writer, msgp.Encodable) 16// and 17// msgp.Decode(io.Reader, msgp.Decodable) 18// 19// There are also methods for converting MessagePack to JSON without 20// an explicit de-serialization step. 21// 22// For additional tips, tricks, and gotchas, please visit 23// the wiki at http://github.com/tinylib/msgp 24package msgp 25 26const last4 = 0x0f 27const first4 = 0xf0 28const last5 = 0x1f 29const first3 = 0xe0 30const last7 = 0x7f 31 32func isfixint(b byte) bool { 33 return b>>7 == 0 34} 35 36func isnfixint(b byte) bool { 37 return b&first3 == mnfixint 38} 39 40func isfixmap(b byte) bool { 41 return b&first4 == mfixmap 42} 43 44func isfixarray(b byte) bool { 45 return b&first4 == mfixarray 46} 47 48func isfixstr(b byte) bool { 49 return b&first3 == mfixstr 50} 51 52func wfixint(u uint8) byte { 53 return u & last7 54} 55 56func rfixint(b byte) uint8 { 57 return b 58} 59 60func wnfixint(i int8) byte { 61 return byte(i) | mnfixint 62} 63 64func rnfixint(b byte) int8 { 65 return int8(b) 66} 67 68func rfixmap(b byte) uint8 { 69 return b & last4 70} 71 72func wfixmap(u uint8) byte { 73 return mfixmap | (u & last4) 74} 75 76func rfixstr(b byte) uint8 { 77 return b & last5 78} 79 80func wfixstr(u uint8) byte { 81 return (u & last5) | mfixstr 82} 83 84func rfixarray(b byte) uint8 { 85 return (b & last4) 86} 87 88func wfixarray(u uint8) byte { 89 return (u & last4) | mfixarray 90} 91 92// These are all the byte 93// prefixes defined by the 94// msgpack standard 95const ( 96 // 0XXXXXXX 97 mfixint uint8 = 0x00 98 99 // 111XXXXX 100 mnfixint uint8 = 0xe0 101 102 // 1000XXXX 103 mfixmap uint8 = 0x80 104 105 // 1001XXXX 106 mfixarray uint8 = 0x90 107 108 // 101XXXXX 109 mfixstr uint8 = 0xa0 110 111 mnil uint8 = 0xc0 112 mfalse uint8 = 0xc2 113 mtrue uint8 = 0xc3 114 mbin8 uint8 = 0xc4 115 mbin16 uint8 = 0xc5 116 mbin32 uint8 = 0xc6 117 mext8 uint8 = 0xc7 118 mext16 uint8 = 0xc8 119 mext32 uint8 = 0xc9 120 mfloat32 uint8 = 0xca 121 mfloat64 uint8 = 0xcb 122 muint8 uint8 = 0xcc 123 muint16 uint8 = 0xcd 124 muint32 uint8 = 0xce 125 muint64 uint8 = 0xcf 126 mint8 uint8 = 0xd0 127 mint16 uint8 = 0xd1 128 mint32 uint8 = 0xd2 129 mint64 uint8 = 0xd3 130 mfixext1 uint8 = 0xd4 131 mfixext2 uint8 = 0xd5 132 mfixext4 uint8 = 0xd6 133 mfixext8 uint8 = 0xd7 134 mfixext16 uint8 = 0xd8 135 mstr8 uint8 = 0xd9 136 mstr16 uint8 = 0xda 137 mstr32 uint8 = 0xdb 138 marray16 uint8 = 0xdc 139 marray32 uint8 = 0xdd 140 mmap16 uint8 = 0xde 141 mmap32 uint8 = 0xdf 142) 143