-
Notifications
You must be signed in to change notification settings - Fork 78
/
encoder.go
193 lines (164 loc) · 4.43 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
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
193
package amino
import (
"encoding/binary"
"fmt"
"io"
"math"
"math/bits"
"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.LittleEndian.PutUint32(buf[:], uint32(i))
_, err = w.Write(buf[:])
return
}
func EncodeInt64(w io.Writer, i int64) (err error) {
var buf [8]byte
binary.LittleEndian.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 {
return UvarintSize((uint64(i) << 1) ^ uint64(i>>63))
}
//----------------------------------------
// 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.LittleEndian.PutUint32(buf[:], u)
_, err = w.Write(buf[:])
return
}
func EncodeUint64(w io.Writer, u uint64) (err error) {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], u)
_, err = w.Write(buf[:])
return
}
// EncodeUvarint is used to encode golang's int, int32, int64 by default. unless specified differently by the
// `binary:"fixed32"`, `binary:"fixed64"`, or `binary:"zigzag32"` `binary:"zigzag64"` tags.
// It matches protobufs varint encoding.
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 {
if u == 0 {
return 1
}
return (bits.Len64(u) + 6) / 7
}
//----------------------------------------
// 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))
}
const (
// seconds of 01-01-0001
minSeconds int64 = -62135596800
// seconds of 10000-01-01
maxSeconds int64 = 253402300800
// nanos have to be in interval: [0, 999999999]
maxNanos = 999999999
)
type InvalidTimeErr string
func (e InvalidTimeErr) Error() string {
return "invalid time: " + string(e)
}
// EncodeTime writes the number of seconds (int64) and nanoseconds (int32),
// with millisecond resolution since January 1, 1970 UTC to the Writer as an
// UInt64.
// Milliseconds are used to ease compatibility with Javascript,
// which does not support finer resolution.
func EncodeTime(w io.Writer, t time.Time) (err error) {
s := t.Unix()
// TODO: We are hand-encoding a struct until MarshalAmino/UnmarshalAmino is supported.
// skip if default/zero value:
if s != 0 {
if s < minSeconds || s >= maxSeconds {
return InvalidTimeErr(fmt.Sprintf("seconds have to be >= %d and < %d, got: %d",
minSeconds, maxSeconds, s))
}
err = encodeFieldNumberAndTyp3(w, 1, Typ3Varint)
if err != nil {
return
}
err = EncodeUvarint(w, uint64(s))
if err != nil {
return
}
}
ns := int32(t.Nanosecond()) // this int64 -> int32 cast is safe (nanos are in [0, 999999999])
// skip if default/zero value:
if ns != 0 {
// do not encode if nanos exceed allowed interval
if ns < 0 || ns > maxNanos {
// we could as well panic here:
// time.Time.Nanosecond() guarantees nanos to be in [0, 999,999,999]
return InvalidTimeErr(fmt.Sprintf("nanoseconds have to be >= 0 and <= %v, got: %d",
maxNanos, s))
}
err = encodeFieldNumberAndTyp3(w, 2, Typ3Varint)
if err != nil {
return
}
err = EncodeUvarint(w, uint64(ns))
if err != nil {
return
}
}
return err
}
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))
}