-
Notifications
You must be signed in to change notification settings - Fork 2
/
byte_order_reader.nim
192 lines (147 loc) · 5.53 KB
/
byte_order_reader.nim
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
#[
Author: Dr. John Lindsay
Created: January 18, 2018
Last Modified: January 18, 2018
License: MIT
Notes: This module provides convience procs for reading multi-byte values from a byte buffer
taking byte endianness into account.
]#
import system, endians, unittest
type
ByteOrderReader* = object
data: seq[uint8]
endian: Endianness
pos: int
proc newByteOrderReader*(data: seq[uint8], endian: Endianness, pos = 0): ByteOrderReader =
result = ByteOrderReader(data: data, endian: endian, pos: pos)
proc `pos=`*(b: var ByteOrderReader, value: int) {.inline.} = b.pos = value
proc pos*(b: var ByteOrderReader): int {.inline.} = return b.pos
proc `endian=`*(b: var ByteOrderReader, value: Endianness) {.inline.} = b.endian = value
proc endian*(b: var ByteOrderReader): Endianness {.inline.} = return b.endian
proc `data=`*(b: var ByteOrderReader, value: seq[uint8]) =
b.data = value
b.pos = 0
proc readUint8*(b: var ByteOrderReader): uint8 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
result = b.data[b.pos]
b.pos += 1
proc readUint16*(b: var ByteOrderReader): uint16 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian16(addr result, addr b.data[b.pos])
else:
bigEndian16(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readUint32*(b: var ByteOrderReader): uint32 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian32(addr result, addr b.data[b.pos])
else:
bigEndian32(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readUint64*(b: var ByteOrderReader): uint64 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian64(addr result, addr b.data[b.pos])
else:
bigEndian64(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readInt8*(b: var ByteOrderReader): int8 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
result = cast[int8](b.data[b.pos])
b.pos += 1
proc readInt16*(b: var ByteOrderReader): int16 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian16(addr result, addr b.data[b.pos])
else:
bigEndian16(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readInt32*(b: var ByteOrderReader): int32 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian32(addr result, addr b.data[b.pos])
else:
bigEndian32(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readInt64*(b: var ByteOrderReader): int64 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian64(addr result, addr b.data[b.pos])
else:
bigEndian64(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readFloat32*(b: var ByteOrderReader): float32 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian32(addr result, addr b.data[b.pos])
else:
bigEndian32(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
proc readFloat64*(b: var ByteOrderReader): float64 {.inline.} =
if b.pos + sizeOf(result) > b.data.len or b.pos < 0:
raise newException(IndexError, "Read past data length")
if b.endian == littleEndian:
littleEndian64(addr result, addr b.data[b.pos])
else:
bigEndian64(addr result, addr b.data[b.pos])
b.pos += sizeOf(result)
when isMainModule:
test "indexError":
let z: seq[uint8] = @[1'u8]
var b = newByteOrderReader(z, littleEndian)
expect(IndexError):
discard b.readUint16
expect(IndexError):
discard b.readUint32
expect(IndexError):
discard b.readUint64
expect(IndexError):
discard b.readInt16
expect(IndexError):
discard b.readInt32
expect(IndexError):
discard b.readInt64
expect(IndexError):
discard b.readFloat32
expect(IndexError):
discard b.readFloat64
expect(IndexError):
b.pos = -1
discard b.readInt16
test "readUint16":
let z: seq[uint8] = @[1'u8, 0, 0, 2]
var b = newByteOrderReader(z, littleEndian)
check(b.readUint16 == 1'u16)
check(b.readUint16 == 512'u16)
b.endian = bigEndian
b.pos = 0
check(b.readUint16 == 256'u16)
check(b.readUint16 == 2'u16)
test "readUint32":
let z: seq[uint8] = @[1'u8, 0, 0, 0, 0, 2, 0, 0]
var b = newByteOrderReader(z, littleEndian)
check(b.readUint32 == 1'u32)
check(b.readUint32 == 512'u32)
b.endian = bigEndian
b.pos = 0
check(b.readUint32 == 16777216'u32)
check(b.readUint32 == 131072'u32)
test "readUint64":
let z: seq[uint8] = @[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
var b = newByteOrderReader(z, littleEndian)
check(b.readUint64 == 1'u64)
check(b.readUint64 == 512'u64)
b.endian = bigEndian
b.pos = 0
check(b.readUint64 == 72057594037927936'u64)
check(b.readUint64 == 562949953421312'u64)