primitives.go
5.2 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ndr
import (
"bytes"
"encoding/binary"
"math"
)
// Byte sizes of primitive types
const (
SizeBool = 1
SizeChar = 1
SizeUint8 = 1
SizeUint16 = 2
SizeUint32 = 4
SizeUint64 = 8
SizeEnum = 2
SizeSingle = 4
SizeDouble = 8
SizePtr = 4
)
// Bool is an NDR Boolean which is a logical quantity that assumes one of two values: TRUE or FALSE.
// NDR represents a Boolean as one octet.
// It represents a value of FALSE as a zero octet, an octet in which every bit is reset.
// It represents a value of TRUE as a non-zero octet, an octet in which one or more bits are set.
// Char is an NDR character.
// NDR represents a character as one octet.
// Characters have two representation formats: ASCII and EBCDIC.
// USmall is an unsigned 8 bit integer
// UShort is an unsigned 16 bit integer
// ULong is an unsigned 32 bit integer
// UHyper is an unsigned 64 bit integer
// Small is an signed 8 bit integer
// Short is an signed 16 bit integer
// Long is an signed 32 bit integer
// Hyper is an signed 64 bit integer
// Enum is the NDR representation of enumerated types as signed short integers (2 octets)
// Single is an NDR defined single-precision floating-point data type
// Double is an NDR defined double-precision floating-point data type
// readBool reads a byte representing a boolean.
// NDR represents a Boolean as one octet.
// It represents a value of FALSE as a zero octet, an octet in which every bit is reset.
// It represents a value of TRUE as a non-zero octet, an octet in which one or more bits are set.
func (dec *Decoder) readBool() (bool, error) {
i, err := dec.readUint8()
if err != nil {
return false, err
}
if i != 0 {
return true, nil
}
return false, nil
}
// readChar reads bytes representing a 8bit ASCII integer cast to a rune.
func (dec *Decoder) readChar() (rune, error) {
var r rune
a, err := dec.readUint8()
if err != nil {
return r, err
}
return rune(a), nil
}
// readUint8 reads bytes representing a 8bit unsigned integer.
func (dec *Decoder) readUint8() (uint8, error) {
b, err := dec.r.ReadByte()
if err != nil {
return uint8(0), err
}
return uint8(b), nil
}
// readUint16 reads bytes representing a 16bit unsigned integer.
func (dec *Decoder) readUint16() (uint16, error) {
dec.ensureAlignment(SizeUint16)
b, err := dec.readBytes(SizeUint16)
if err != nil {
return uint16(0), err
}
return dec.ch.Endianness.Uint16(b), nil
}
// readUint32 reads bytes representing a 32bit unsigned integer.
func (dec *Decoder) readUint32() (uint32, error) {
dec.ensureAlignment(SizeUint32)
b, err := dec.readBytes(SizeUint32)
if err != nil {
return uint32(0), err
}
return dec.ch.Endianness.Uint32(b), nil
}
// readUint32 reads bytes representing a 32bit unsigned integer.
func (dec *Decoder) readUint64() (uint64, error) {
dec.ensureAlignment(SizeUint64)
b, err := dec.readBytes(SizeUint64)
if err != nil {
return uint64(0), err
}
return dec.ch.Endianness.Uint64(b), nil
}
func (dec *Decoder) readInt8() (int8, error) {
dec.ensureAlignment(SizeUint8)
b, err := dec.readBytes(SizeUint8)
if err != nil {
return 0, err
}
var i int8
buf := bytes.NewReader(b)
err = binary.Read(buf, dec.ch.Endianness, &i)
if err != nil {
return 0, err
}
return i, nil
}
func (dec *Decoder) readInt16() (int16, error) {
dec.ensureAlignment(SizeUint16)
b, err := dec.readBytes(SizeUint16)
if err != nil {
return 0, err
}
var i int16
buf := bytes.NewReader(b)
err = binary.Read(buf, dec.ch.Endianness, &i)
if err != nil {
return 0, err
}
return i, nil
}
func (dec *Decoder) readInt32() (int32, error) {
dec.ensureAlignment(SizeUint32)
b, err := dec.readBytes(SizeUint32)
if err != nil {
return 0, err
}
var i int32
buf := bytes.NewReader(b)
err = binary.Read(buf, dec.ch.Endianness, &i)
if err != nil {
return 0, err
}
return i, nil
}
func (dec *Decoder) readInt64() (int64, error) {
dec.ensureAlignment(SizeUint64)
b, err := dec.readBytes(SizeUint64)
if err != nil {
return 0, err
}
var i int64
buf := bytes.NewReader(b)
err = binary.Read(buf, dec.ch.Endianness, &i)
if err != nil {
return 0, err
}
return i, nil
}
// https://en.wikipedia.org/wiki/IEEE_754-1985
func (dec *Decoder) readFloat32() (f float32, err error) {
dec.ensureAlignment(SizeSingle)
b, err := dec.readBytes(SizeSingle)
if err != nil {
return
}
bits := dec.ch.Endianness.Uint32(b)
f = math.Float32frombits(bits)
return
}
func (dec *Decoder) readFloat64() (f float64, err error) {
dec.ensureAlignment(SizeDouble)
b, err := dec.readBytes(SizeDouble)
if err != nil {
return
}
bits := dec.ch.Endianness.Uint64(b)
f = math.Float64frombits(bits)
return
}
// NDR enforces NDR alignment of primitive data; that is, any primitive of size n octets is aligned at a octet stream
// index that is a multiple of n. (In this version of NDR, n is one of {1, 2, 4, 8}.) An octet stream index indicates
// the number of an octet in an octet stream when octets are numbered, beginning with 0, from the first octet in the
// stream. Where necessary, an alignment gap, consisting of octets of unspecified value, precedes the representation
// of a primitive. The gap is of the smallest size sufficient to align the primitive.
func (dec *Decoder) ensureAlignment(n int) {
p := dec.size - dec.r.Buffered()
if s := p % n; s != 0 {
dec.r.Discard(n - s)
}
}