-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
85 lines (79 loc) · 1.77 KB
/
helpers.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
package degob
import (
"io"
"math"
)
type typeId int32
// reads a uint from the reader
func readUint(r io.Reader, into []byte, read *uint64) (uint64, int, *Error) {
var n int
width := 1
n, err := r.Read(into[:1])
if err != nil {
if err == io.EOF {
return 0, width, genericError(io.ErrUnexpectedEOF, *read, nil)
}
var gobBytes []byte
if v, ok := r.(*gobBuf); ok {
gobBytes = v.Data()
}
return 0, width, genericError(err, *read, gobBytes)
}
*read += uint64(n)
b := into[0]
// anything less than 0x7f is encoded as a single byte with
// that value so we're done
if b <= 0x7f {
return uint64(b), width, nil
}
// FROM DOCS:
// Otherwise it is sent as a minimal-length big-endian (high byte first)
// byte stream holding the value, preceded by one byte holding the byte
// count, negated.
n = -int(int8(b))
width += n
if n > uintByteSize {
var gobBytes []byte
if v, ok := r.(*gobBuf); ok {
gobBytes = v.Data()
}
return 0, width, errUintTooBig(*read, gobBytes)
}
// now we read n bytes and that is our uint
n, err = io.ReadFull(r, into[0:n])
if err != nil {
var gobBytes []byte
if v, ok := r.(*gobBuf); ok {
gobBytes = v.Data()
}
if err == io.EOF {
return 0, width, genericError(io.ErrUnexpectedEOF, *read, gobBytes)
}
return 0, width, genericError(err, *read, gobBytes)
}
*read += uint64(n)
var val uint64
for _, b := range into[0:n] {
val = val<<8 | uint64(b)
}
return val, width, nil
}
func uintToInt(x uint64) int64 {
i := int64(x >> 1)
if x&1 != 0 {
i = ^i
}
return i
}
func uintToFloat(x uint64) float64 {
var v uint64
for i := 0; i < 8; i++ {
v <<= 8
v |= x & 0xFF
x >>= 8
}
return math.Float64frombits(v)
}
func uintToComplex(r uint64, i uint64) complex128 {
return complex(uintToFloat(r), uintToFloat(i))
}