forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
160 lines (132 loc) · 3.31 KB
/
encoder.go
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
package amino
import (
"encoding/binary"
"io"
"math"
"time"
)
//----------------------------------------
// Signed
func EncodeInt8(w io.Writer, i int8) (err error) {
return EncodeVarint(w, int64(i))
}
func EncodeInt16(w io.Writer, i int16) (err error) {
return EncodeVarint(w, int64(i))
}
func EncodeInt32(w io.Writer, i int32) (err error) {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(i))
_, err = w.Write(buf[:])
return
}
func EncodeInt64(w io.Writer, i int64) (err error) {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
_, err = w.Write(buf[:])
return err
}
func EncodeVarint(w io.Writer, i int64) (err error) {
var buf [10]byte
n := binary.PutVarint(buf[:], i)
_, err = w.Write(buf[0:n])
return
}
func VarintSize(i int64) int {
var buf [10]byte
n := binary.PutVarint(buf[:], i)
return n
}
//----------------------------------------
// Unsigned
func EncodeByte(w io.Writer, b byte) (err error) {
return EncodeUvarint(w, uint64(b))
}
func EncodeUint8(w io.Writer, u uint8) (err error) {
return EncodeUvarint(w, uint64(u))
}
func EncodeUint16(w io.Writer, u uint16) (err error) {
return EncodeUvarint(w, uint64(u))
}
func EncodeUint32(w io.Writer, u uint32) (err error) {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], u)
_, err = w.Write(buf[:])
return
}
func EncodeUint64(w io.Writer, u uint64) (err error) {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], u)
_, err = w.Write(buf[:])
return
}
func EncodeUvarint(w io.Writer, u uint64) (err error) {
var buf [10]byte
n := binary.PutUvarint(buf[:], u)
_, err = w.Write(buf[0:n])
return
}
func UvarintSize(u uint64) int {
var buf [10]byte
n := binary.PutUvarint(buf[:], u)
return n
}
//----------------------------------------
// Other
func EncodeBool(w io.Writer, b bool) (err error) {
if b {
err = EncodeUint8(w, 1) // same as EncodeUvarint(w, 1).
} else {
err = EncodeUint8(w, 0) // same as EncodeUvarint(w, 0).
}
return
}
// NOTE: UNSAFE
func EncodeFloat32(w io.Writer, f float32) (err error) {
return EncodeUint32(w, math.Float32bits(f))
}
// NOTE: UNSAFE
func EncodeFloat64(w io.Writer, f float64) (err error) {
return EncodeUint64(w, math.Float64bits(f))
}
// EncodeTime writes the number of seconds (int64) and nanoseconds (int32),
// with millisecond resolution since January 1, 1970 UTC to the Writer as an
// Int64.
// Milliseconds are used to ease compatibility with Javascript,
// which does not support finer resolution.
func EncodeTime(w io.Writer, t time.Time) (err error) {
var s = t.Unix()
var ns = int32(t.Nanosecond()) // this int64 -> int32 is safe.
// TODO: We are hand-encoding a struct until MarshalAmino/UnmarshalAmino is supported.
err = encodeFieldNumberAndTyp3(w, 1, Typ3_8Byte)
if err != nil {
return
}
err = EncodeInt64(w, s)
if err != nil {
return
}
err = encodeFieldNumberAndTyp3(w, 2, Typ3_4Byte)
if err != nil {
return
}
err = EncodeInt32(w, ns)
if err != nil {
return
}
err = EncodeByte(w, byte(0x04)) // StructTerm
return
}
func EncodeByteSlice(w io.Writer, bz []byte) (err error) {
err = EncodeUvarint(w, uint64(len(bz)))
if err != nil {
return
}
_, err = w.Write(bz)
return
}
func ByteSliceSize(bz []byte) int {
return UvarintSize(uint64(len(bz))) + len(bz)
}
func EncodeString(w io.Writer, s string) (err error) {
return EncodeByteSlice(w, []byte(s))
}