-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataItemHeader.swift
395 lines (357 loc) · 13.9 KB
/
DataItemHeader.swift
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright © 2019 David Waite
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import NIO
/// Represents a CBOR ['Data Item Head'](https://tools.ietf.org/html/draft-ietf-cbor-7049bis-09#section-2.2).
///
/// The enumeration is structured roughly in line with the major types, with specialty data types to preserve the
/// different encodings, without necessarily normalizing them. In the case of `SimpleValue` and `ImmediateValue`,
/// the types also enforce legal values to prevent inconsistencies in tooling
//swiftlint:disable:next type_body_length
public enum DataItemHeader: Hashable {
case unsignedInteger(SizedValue)
case negativeInteger(data: SizedValue)
case byteString(length: SizedValue?)
case utf8TextString(length: SizedValue?)
case array(count: SizedValue?)
case object(pairs: SizedValue?)
// swiftlint:disable:next identifier_name
case tag(id: SizedValue)
case simple(SimpleValue)
case floatingPoint(FloatValue)
case `break`
public struct ImmediateValue: RawRepresentable, Hashable {
public let rawValue: UInt8
public init?(rawValue: UInt8) {
guard rawValue < 24 else {
return nil
}
self.rawValue = rawValue
}
init?(_ info: DataItemHeader.AdditionalInfo) {
guard let immediate = info.immediateValue else {
return nil
}
self.rawValue = immediate
}
}
public struct SimpleValue: RawRepresentable, Hashable {
public let rawValue: UInt8
public init?(rawValue: UInt8) {
guard !(24..<32).contains(rawValue) else {
return nil // values from 24 up to 32 are invalid
}
self.rawValue = rawValue
}
public init(_ immediate: ImmediateValue) {
self.rawValue = immediate.rawValue
}
func write(into buffer: inout ByteBuffer) {
if rawValue < 24 {
buffer.writeInteger(MajorType.etc.rawValue | rawValue)
} else {
buffer.writeInteger(MajorType.etc.rawValue | AdditionalInfo.uint8Following.rawValue)
buffer.writeInteger(rawValue)
}
}
}
public enum FloatValue: Hashable {
case half(UInt16)
case float(Float)
case double(Double)
func write(into buffer: inout ByteBuffer) {
switch self {
case .half(let value):
buffer.writeInteger(MajorType.etc.rawValue | AdditionalInfo.uint16Following.rawValue)
buffer.writeInteger(value)
case .float(let float):
buffer.writeInteger(MajorType.etc.rawValue | AdditionalInfo.uint32Following.rawValue)
buffer.writeInteger(float.bitPattern)
case .double(let double):
buffer.writeInteger(MajorType.etc.rawValue | AdditionalInfo.uint64Following.rawValue)
buffer.writeInteger(double.bitPattern)
}
}
}
public enum SizedValue: Hashable {
case immediate(ImmediateValue)
case uint8(UInt8)
case uint16(UInt16)
case uint32(UInt32)
case uint64(UInt64)
init(info: DataItemHeader.AdditionalInfo, from buffer: inout ByteBuffer) throws {
switch info {
case .uint8Following:
guard let uint8: UInt8 = buffer.readInteger() else {
throw DeserializationError.endOfStream(offset: buffer.readerIndex)
}
self = .uint8(uint8)
return
case .uint16Following:
guard let u16: UInt16 = buffer.readInteger() else {
throw DeserializationError.endOfStream(offset: buffer.readerIndex)
}
self = .uint16(u16)
return
case .uint32Following:
guard let u32: UInt32 = buffer.readInteger() else {
throw DeserializationError.endOfStream(offset: buffer.readerIndex)
}
self = .uint32(u32)
return
case .uint64Following:
guard let u64: UInt64 = buffer.readInteger() else {
throw DeserializationError.endOfStream(offset: buffer.readerIndex)
}
self = .uint64(u64)
return
default:
let immediate = ImmediateValue(info)!
self = .immediate(immediate)
}
}
public var value: UInt64 {
switch self {
case .immediate(let value):
return UInt64(value.rawValue)
case .uint8(let value):
return UInt64(value)
case .uint16(let value):
return UInt64(value)
case .uint32(let value):
return UInt64(value)
case .uint64(let value):
return UInt64(value)
}
}
var additionalInfo: AdditionalInfo {
switch self {
case .immediate(let value):
return AdditionalInfo(value)
case .uint8:
return .uint8Following
case .uint16:
return .uint16Following
case .uint32:
return .uint32Following
case .uint64:
return .uint64Following
}
}
public func normalized() -> Self {
let u64 = value
if let value = UInt8(exactly: u64) {
if let immediate = ImmediateValue(rawValue: value) {
return .immediate(immediate)
}
return .uint8(value)
}
if let value = UInt16(exactly: u64) {
return .uint16(value)
}
if let value = UInt32(exactly: u64) {
return .uint32(value)
}
return .uint64(u64)
}
func write(_ majorType: MajorType, into buffer: inout ByteBuffer) {
let info = additionalInfo
let initialByte = majorType.rawValue | info.rawValue
buffer.writeInteger(initialByte)
switch self {
case .immediate:
break // already done above
case .uint8(let value):
buffer.writeInteger(value)
case .uint16(let value):
buffer.writeInteger(value)
case .uint32(let value):
buffer.writeInteger(value)
case .uint64(let value):
buffer.writeInteger(value)
}
}
}
enum MajorType: UInt8, RawRepresentable, Hashable {
case unsignedInteger = 0x00
case negativeInteger = 0x20
case byteString = 0x40
case utf8TextString = 0x60
case array = 0x80
case object = 0xa0
case tag = 0xc0
case etc = 0xe0
var allowsIndefiniteOrBreak: Bool {
switch self {
case .unsignedInteger, .negativeInteger, .tag:
return false
default:
return true
}
}
init(initialByte: UInt8) {
self.init(rawValue: initialByte & 0xe0)!
}
}
struct AdditionalInfo: RawRepresentable, Hashable {
let rawValue: UInt8
static let uint8Following = Self(rawValue: 0x18)!
static let uint16Following = Self(rawValue: 0x19)!
static let uint32Following = Self(rawValue: 0x1a)!
static let uint64Following = Self(rawValue: 0x1b)!
public var immediateValue: UInt8? {
guard (0..<24).contains(rawValue) else {
return nil
}
return rawValue
}
public init?(rawValue: UInt8) {
guard rawValue & 0xe0 == 0,
rawValue <= 0x1b else {
return nil
}
self.rawValue = rawValue
}
public init?(initialByte: UInt8) {
self.init(rawValue: initialByte & 0x1f)
}
public init(_ value: ImmediateValue) {
self.rawValue = value.rawValue
}
public init(immediate value: UInt8) {
guard value < 24 else {
fatalError("immediate values must be in the range 0...24")
}
self.rawValue = value
}
}
public init(from buffer: inout ByteBuffer) throws {
guard let initialByte: UInt8 = buffer.readInteger() else {
throw DeserializationError.endOfStream(offset: buffer.readerIndex)
}
let majorType = MajorType(initialByte: initialByte)
let additionalInfo = AdditionalInfo(initialByte: initialByte)
switch (majorType, additionalInfo) {
case (.unsignedInteger, let info?):
self = .unsignedInteger(try SizedValue(info: info, from: &buffer))
case (.negativeInteger, let info?):
self = .negativeInteger(data: try SizedValue(info: info, from: &buffer))
case (.byteString, let info):
self = .byteString(length: try info.map { try SizedValue(info: $0, from: &buffer) })
case (.utf8TextString, let info):
self = .utf8TextString(length: try info.map { try SizedValue(info: $0, from: &buffer) })
case (.array, let info):
self = .array(count: try info.map { try SizedValue(info: $0, from: &buffer) })
case (.object, let info):
self = .object(pairs: try info.map { try SizedValue(info: $0, from: &buffer) })
case (.tag, let info?):
self = .tag(id: try SizedValue(info: info, from: &buffer))
case (.etc, let info):
self = try .cborFromEtc(info, from: &buffer)
default:
throw WellFormednessError.unknownInitialByte(initialByte: initialByte, offset: buffer.readerIndex)
}
}
private static func cborFromEtc(_ additionalInfo: AdditionalInfo?, from buffer: inout ByteBuffer) throws ->
DataItemHeader {
guard let additionalInfo = additionalInfo else {
return .break
}
let sizedValue = try SizedValue(info: additionalInfo, from: &buffer)
switch sizedValue {
case .immediate(let value):
return .simple(SimpleValue(value))
case .uint8(let value):
guard let simpleValue = SimpleValue(rawValue: value) else {
throw WellFormednessError.invalidSimpleValue(value: value, offset: buffer.readerIndex)
}
return .simple(simpleValue)
case .uint16(let value):
return .floatingPoint(.half(value))
case .uint32(let value):
return .floatingPoint(.float(Float(bitPattern: value)))
case .uint64(let value):
return .floatingPoint(.double(Double(bitPattern: value)))
}
}
public var isCompleteDataItem: Bool {
switch self {
case .negativeInteger, .unsignedInteger, .simple, .floatingPoint:
return true
case .array(let sizedValue?),
.byteString(let sizedValue?),
.object(let sizedValue?),
.utf8TextString(let sizedValue?):
return sizedValue.value == 0
case .break, .tag:
return false
default:
return false
}
}
public static let `false` = Self.simple(SimpleValue(rawValue: 0x14)!)
public static let `true` = Self.simple(SimpleValue(rawValue: 0x15)!)
public static let null = Self.simple(SimpleValue(rawValue: 0x16)!)
public static let undefined = Self.simple(SimpleValue(rawValue: 0x17)!)
func writeSV(_ sizedValue: SizedValue?, _ majorType: MajorType, into buffer: inout ByteBuffer) {
guard let sizedValue = sizedValue else {
buffer.writeInteger(majorType.rawValue | 0x1f)
return
}
sizedValue.write(majorType, into: &buffer)
}
public func write(into buffer: inout ByteBuffer) {
switch self {
case .unsignedInteger(let sizedValue):
sizedValue.write(.unsignedInteger, into: &buffer)
case .negativeInteger(let sizedValue):
sizedValue.write(.negativeInteger, into: &buffer)
case .byteString(let sizedValue):
writeSV(sizedValue, .byteString, into: &buffer)
case .utf8TextString(let sizedValue):
writeSV(sizedValue, .utf8TextString, into: &buffer)
case .array(let sizedValue):
writeSV(sizedValue, .array, into: &buffer)
case .object(let sizedValue):
writeSV(sizedValue, .object, into: &buffer)
case .tag(let tag):
tag.write(.tag, into: &buffer)
case .simple(let simple):
simple.write(into: &buffer)
case .floatingPoint(let floatValue):
floatValue.write(into: &buffer)
case .break:
buffer.writeInteger(0xff as UInt8)
}
}
}
extension DataItemHeader.SizedValue: ExpressibleByIntegerLiteral {
public init(integerLiteral value: UInt64) {
self = Self.uint64(value).normalized()
}
public init(_ source: UInt64) {
self.init(integerLiteral: source)
}
}
extension DataItemHeader: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
if value >= 0 {
self = .unsignedInteger(SizedValue(UInt64(value)))
} else {
self = .negativeInteger(data: SizedValue(UInt64(~value)))
}
}
public init(_ source: Int) {
self.init(integerLiteral: source)
}
}