| 98 | } |
| 99 | |
| 100 | func ReadVarInt(r io.Reader) (int32, error) { |
| 101 | var ( |
| 102 | position int32 |
| 103 | currentByte byte |
| 104 | continueBit byte = 128 |
| 105 | segmentBits byte = 127 |
| 106 | |
| 107 | value int32 |
| 108 | ) |
| 109 | |
| 110 | for { |
| 111 | if _, err := r.Read(unsafe.Slice(¤tByte, 1)); err != nil { |
| 112 | return value, err |
| 113 | } |
| 114 | |
| 115 | value |= int32(currentByte&segmentBits) << position |
| 116 | |
| 117 | if (currentByte & continueBit) == 0 { |
| 118 | break |
| 119 | } |
| 120 | |
| 121 | position += 7 |
| 122 | |
| 123 | if position >= 32 { |
| 124 | return value, fmt.Errorf("VarInt is too big") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return value, nil |
| 129 | } |
| 130 | |
| 131 | func AppendVarLong(data []byte, value int64) []byte { |
| 132 | var ( |