/usr/share/gocode/src/github.com/tendermint/go-wire/codec.go is in golang-github-tendermint-go-wire-dev 0~20161027~0git287d8ca-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | package wire
import (
"bytes"
"errors"
"fmt"
. "github.com/tendermint/go-common"
"io"
"reflect"
"time"
)
type Encoder func(o interface{}, w io.Writer, n *int, err *error)
type Decoder func(r io.Reader, n *int, err *error) interface{}
type Comparator func(o1 interface{}, o2 interface{}) int
type Codec struct {
Encode Encoder
Decode Decoder
Compare Comparator
}
const (
typeByte = byte(0x01)
typeInt8 = byte(0x02)
// typeUint8 = byte(0x03)
typeInt16 = byte(0x04)
typeUint16 = byte(0x05)
typeInt32 = byte(0x06)
typeUint32 = byte(0x07)
typeInt64 = byte(0x08)
typeUint64 = byte(0x09)
typeVarint = byte(0x0A)
typeUvarint = byte(0x0B)
typeString = byte(0x10)
typeByteSlice = byte(0x11)
typeTime = byte(0x20)
)
func BasicCodecEncoder(o interface{}, w io.Writer, n *int, err *error) {
switch o := o.(type) {
case nil:
PanicSanity("nil type unsupported")
case byte:
WriteByte(typeByte, w, n, err)
WriteByte(o, w, n, err)
case int8:
WriteByte(typeInt8, w, n, err)
WriteInt8(o, w, n, err)
//case uint8:
// WriteByte( typeUint8, w, n, err)
// WriteUint8( o, w, n, err)
case int16:
WriteByte(typeInt16, w, n, err)
WriteInt16(o, w, n, err)
case uint16:
WriteByte(typeUint16, w, n, err)
WriteUint16(o, w, n, err)
case int32:
WriteByte(typeInt32, w, n, err)
WriteInt32(o, w, n, err)
case uint32:
WriteByte(typeUint32, w, n, err)
WriteUint32(o, w, n, err)
case int64:
WriteByte(typeInt64, w, n, err)
WriteInt64(o, w, n, err)
case uint64:
WriteByte(typeUint64, w, n, err)
WriteUint64(o, w, n, err)
case int:
WriteByte(typeVarint, w, n, err)
WriteVarint(o, w, n, err)
case uint:
WriteByte(typeUvarint, w, n, err)
WriteUvarint(o, w, n, err)
case string:
WriteByte(typeString, w, n, err)
WriteString(o, w, n, err)
case []byte:
WriteByte(typeByteSlice, w, n, err)
WriteByteSlice(o, w, n, err)
case time.Time:
WriteByte(typeTime, w, n, err)
WriteTime(o, w, n, err)
default:
PanicSanity(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
}
}
func BasicCodecDecoder(r io.Reader, n *int, err *error) (o interface{}) {
type_ := ReadByte(r, n, err)
if *err != nil {
return
}
switch type_ {
case typeByte:
o = ReadByte(r, n, err)
case typeInt8:
o = ReadInt8(r, n, err)
//case typeUint8:
// o = ReadUint8(r, n, err)
case typeInt16:
o = ReadInt16(r, n, err)
case typeUint16:
o = ReadUint16(r, n, err)
case typeInt32:
o = ReadInt32(r, n, err)
case typeUint32:
o = ReadUint32(r, n, err)
case typeInt64:
o = ReadInt64(r, n, err)
case typeUint64:
o = ReadUint64(r, n, err)
case typeVarint:
o = ReadVarint(r, n, err)
case typeUvarint:
o = ReadUvarint(r, n, err)
case typeString:
o = ReadString(r, 0, n, err)
case typeByteSlice:
o = ReadByteSlice(r, 0, n, err)
case typeTime:
o = ReadTime(r, n, err)
default:
*err = errors.New(Fmt("Unsupported type byte: %X", type_))
}
return
}
// Contract: Caller must ensure that types match.
func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
switch o1.(type) {
case byte:
return int(o1.(byte) - o2.(byte))
case int8:
return int(o1.(int8) - o2.(int8))
//case uint8:
case int16:
return int(o1.(int16) - o2.(int16))
case uint16:
return int(o1.(uint16) - o2.(uint16))
case int32:
return int(o1.(int32) - o2.(int32))
case uint32:
return int(o1.(uint32) - o2.(uint32))
case int64:
return int(o1.(int64) - o2.(int64))
case uint64:
return int(o1.(uint64) - o2.(uint64))
case int:
return o1.(int) - o2.(int)
case uint:
return int(o1.(uint)) - int(o2.(uint))
case string:
return bytes.Compare([]byte(o1.(string)), []byte(o2.(string)))
case []byte:
return bytes.Compare(o1.([]byte), o2.([]byte))
case time.Time:
return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
default:
PanicSanity(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
}
return 0
}
var BasicCodec = Codec{
Encode: BasicCodecEncoder,
Decode: BasicCodecDecoder,
Compare: BasicCodecComparator,
}
//----------------------------------------
func BytesCodecEncoder(o interface{}, w io.Writer, n *int, err *error) {
WriteByteSlice(o.([]byte), w, n, err)
}
func BytesCodecDecoder(r io.Reader, n *int, err *error) (o interface{}) {
o = ReadByteSlice(r, 0, n, err)
return
}
func BytesCodecComparator(o1 interface{}, o2 interface{}) int {
return bytes.Compare(o1.([]byte), o2.([]byte))
}
var BytesCodec = Codec{
Encode: BytesCodecEncoder,
Decode: BytesCodecDecoder,
Compare: BytesCodecComparator,
}
|