aboutsummaryrefslogtreecommitdiff
path: root/src/nimpb/nimpb.nim
blob: 4363c7dd68b164a004ad4dda8f36274ce1cbc6c9 (plain)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import endians
import intsets
import macros
import streams
import strutils

export streams

const
    MaximumVarintBytes = 10
    WireTypeBits = 3
    WireTypeMask = (1 shl WireTypeBits) - 1

type
    ProtobufStreamObj* = object of StreamObj
        stream: Stream

    ProtobufStream* = ref ProtobufStreamObj

    Tag* = distinct uint32

    ParseError* = object of Exception

    InvalidFieldNumberError* = object of ParseError

    UnexpectedWireTypeError* = object of ParseError

    bytes* = distinct string

    WireType* {.pure.} = enum
        Varint = 0
        Fixed64 = 1
        LengthDelimited = 2
        StartGroup = 3
        EndGroup = 4
        Fixed32 = 5

    FieldType* {.pure.} = enum
        Double = 1
        Float
        Int64
        UInt64
        Int32
        Fixed64
        Fixed32
        Bool
        String
        Group
        Message
        Bytes
        UInt32
        Enum
        SFixed32
        SFixed64
        SInt32
        SInt64

    UnknownField* = ref UnknownFieldObj
    UnknownFieldObj* = object
        fieldNumber: int
        case wiretype: WireType
        of WireType.Varint:
            vint: uint64
        of WireType.Fixed64:
            fixed64: uint64
        of WireType.LengthDelimited:
            data: string
        of WireType.Fixed32:
            fixed32: uint32
        else:
            discard

proc wiretype*(ft: FieldType): WireType =
    case ft
    of FieldType.Double: result = WireType.Fixed64
    of FieldType.Float: result = WireType.Fixed32
    of FieldType.Int64: result = WireType.Varint
    of FieldType.UInt64: result = WireType.Varint
    of FieldType.Int32: result = WireType.Varint
    of FieldType.Fixed64: result = WireType.Fixed64
    of FieldType.Fixed32: result = WireType.Fixed32
    of FieldType.Bool: result = WireType.Varint
    of FieldType.String: result = WireType.LengthDelimited
    of FieldType.Group: result = WireType.LengthDelimited # ???
    of FieldType.Message: result = WireType.LengthDelimited
    of FieldType.Bytes: result = WireType.LengthDelimited
    of FieldType.UInt32: result = WireType.Varint
    of FieldType.Enum: result = WireType.Varint
    of FieldType.SFixed32: result = WireType.Fixed32
    of FieldType.SFixed64: result = WireType.Fixed64
    of FieldType.SInt32: result = WireType.Varint
    of FieldType.SInt64: result = WireType.Varint

proc isNumeric*(wiretype: WireType): bool =
    case wiretype
    of WireType.Varint: result = true
    of WireType.Fixed64: result = true
    of WireType.Fixed32: result = true
    else: result = false

proc isNumeric*(ft: FieldType): bool =
    result = isNumeric(wiretype(ft))

proc pbClose(s: Stream) =
    close(ProtobufStream(s).stream)
    ProtobufStream(s).stream = nil

proc pbAtEnd(s: Stream): bool =
    result = atEnd(ProtobufStream(s).stream)

proc pbSetPosition(s: Stream, pos: int) =
    setPosition(ProtobufStream(s).stream, pos)

proc pbGetPosition(s: Stream): int =
    result = getPosition(ProtobufStream(s).stream)

proc pbReadData(s: Stream, buffer: pointer, bufLen: int): int =
    result = readData(ProtobufStream(s).stream, buffer, bufLen)

proc pbPeekData(s: Stream, buffer: pointer, bufLen: int): int =
    result = peekData(ProtobufStream(s).stream, buffer, bufLen)

proc pbWriteData(s: Stream, buffer: pointer, bufLen: int) =
    writeData(ProtobufStream(s).stream, buffer, bufLen)

proc pbFlush(s: Stream) =
    flush(ProtobufStream(s).stream)

proc newProtobufStream*(stream: Stream): ProtobufStream =
    new(result)

    result.closeImpl = pbClose
    result.atEndImpl = pbAtEnd
    result.setPositionImpl = pbSetPosition
    result.getPositionImpl = pbGetPosition
    result.readDataImpl = pbReadData
    result.peekDataImpl = pbPeekData
    result.writeDataImpl = pbWriteData
    result.flushImpl = pbFlush

    result.stream = stream

proc readByte(stream: ProtobufStream): byte =
    result = readInt8(stream).byte

proc writeByte(stream: ProtobufStream, b: byte) =
    var x: byte
    shallowCopy(x, b)
    writeData(stream, addr(x), sizeof(x))

proc readVarint*(stream: ProtobufStream): uint64 =
    var
        count = 0

    result = 0

    while true:
        if count == MaximumVarintBytes:
            raise newException(Exception, "invalid varint (<= 10 bytes)")

        let b = readByte(stream)

        result = result or ((b.uint64 and 0x7f) shl (7 * count))

        inc(count)

        if (b and 0x80) == 0:
            break

proc writeVarint*(stream: ProtobufStream, n: uint64) =
    var value = n
    while value >= 0x80'u64:
        writeByte(stream, (value or 0x80).byte)
        value = value shr 7
    writeByte(stream, value.byte)

proc zigzagEncode*(n: int32): uint32 =
    let x = cast[uint32](n)
    let a = cast[int32](x shl 1)
    let b = -cast[int32](x shr 31)
    result = uint32(a xor b)

proc zigzagDecode*(n: uint32): int32 =
    let a = int32(n shr 1)
    let b = -int32(n and 1)
    result = a xor b

proc zigzagEncode*(n: int64): uint64 =
    let x = cast[uint64](n)
    let a = cast[int64](x shl 1)
    let b = -cast[int64](x shr 63)
    result = uint64(a xor b)

proc zigzagDecode*(n: uint64): int64 =
    let a = int64(n shr 1)
    let b = -int64(n and 1)
    result = a xor b

template makeTag*(fieldNumber: int, wireType: WireType): Tag =
    ((fieldNumber shl 3).uint32 or wireType.uint32).Tag

template wireType*(tag: Tag): WireType =
    (tag.uint32 and WireTypeMask).WireType

template fieldNumber*(tag: Tag): int =
    (tag.uint32 shr 3).int

proc writeTag*(stream: ProtobufStream, tag: Tag) =
    writeVarint(stream, tag.uint32)

proc writeTag*(stream: ProtobufStream, fieldNumber: int, wireType: WireType) =
    writeTag(stream, makeTag(fieldNumber, wireType))

proc readTag*(stream: ProtobufStream): Tag =
    result = readVarint(stream).Tag

proc writeInt32*(stream: ProtobufStream, n: int32) =
    writeVarint(stream, n.uint64)

proc writeInt32*(stream: ProtobufStream, n: int32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeInt32(stream, n)

proc readInt32*(stream: ProtobufStream): int32 =
    result = readVarint(stream).int32

proc writeSInt32*(stream: ProtobufStream, n: int32) =
    writeVarint(stream, zigzagEncode(n))

proc writeSInt32*(stream: ProtobufStream, n: int32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, zigzagEncode(n))

proc readSInt32*(stream: ProtobufStream): int32 =
    result = zigzagDecode(readVarint(stream).uint32)

proc writeUInt32*(stream: ProtobufStream, n: uint32) =
    writeVarint(stream, n)

proc writeUInt32*(stream: ProtobufStream, n: uint32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, n)

proc readUInt32*(stream: ProtobufStream): uint32 =
    result = readVarint(stream).uint32

proc writeInt64*(stream: ProtobufStream, n: int64) =
    writeVarint(stream, n.uint64)

proc writeInt64*(stream: ProtobufStream, n: int64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, n.uint64)

proc readInt64*(stream: ProtobufStream): int64 =
    result = readVarint(stream).int64

proc writeSInt64*(stream: ProtobufStream, n: int64) =
    writeVarint(stream, zigzagEncode(n))

proc writeSInt64*(stream: ProtobufStream, n: int64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, zigzagEncode(n))

proc readSInt64*(stream: ProtobufStream): int64 =
    result = zigzagDecode(readVarint(stream))

proc writeUInt64*(stream: ProtobufStream, n: uint64) =
    writeVarint(stream, n)

proc writeUInt64*(stream: ProtobufStream, n: uint64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, n)

proc readUInt64*(stream: ProtobufStream): uint64 =
    result = readVarint(stream)

proc writeBool*(stream: ProtobufStream, value: bool) =
    writeVarint(stream, value.uint32)

proc writeBool*(stream: ProtobufStream, n: bool, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeVarint(stream, n.uint32)

proc readBool*(stream: ProtobufStream): bool =
    result = readVarint(stream).bool

proc writeFixed64*(stream: ProtobufStream, value: uint64) =
    var
        input = value
        output: uint64

    littleEndian64(addr(output), addr(input))

    write(stream, output)

proc writeFixed64*(stream: ProtobufStream, n: uint64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed64)
    writeFixed64(stream, n)

proc readFixed64*(stream: ProtobufStream): uint64 =
    var tmp: uint64
    if readData(stream, addr(tmp), sizeof(tmp)) != sizeof(tmp):
        raise newException(IOError, "cannot read from stream")
    littleEndian64(addr(result), addr(tmp))

proc writeSFixed64*(stream: ProtobufStream, value: int64) =
    writeFixed64(stream, cast[uint64](value))

proc writeSFixed64*(stream: ProtobufStream, value: int64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed64)
    writeSFixed64(stream, value)

proc readSFixed64*(stream: ProtobufStream): int64 =
    result = cast[int64](readFixed64(stream))

proc writeDouble*(stream: ProtobufStream, value: float64) =
    var
        input = value
        output: float64

    littleEndian64(addr(output), addr(input))

    write(stream, output)

proc writeDouble*(stream: ProtobufStream, value: float64, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed64)
    writeDouble(stream, value)

proc readDouble*(stream: ProtobufStream): float64 =
    var tmp: uint64
    if readData(stream, addr(tmp), sizeof(tmp)) != sizeof(tmp):
        raise newException(IOError, "cannot read from stream")
    littleEndian64(addr(result), addr(tmp))

proc writeFixed32*(stream: ProtobufStream, value: uint32) =
    var
        input = value
        output: uint32

    littleEndian32(addr(output), addr(input))

    write(stream, output)

proc writeFixed32*(stream: ProtobufStream, value: uint32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed32)
    writeFixed32(stream, value)

proc readFixed32*(stream: ProtobufStream): uint32 =
    var tmp: uint32
    if readData(stream, addr(tmp), sizeof(tmp)) != sizeof(tmp):
        raise newException(IOError, "cannot read from stream")
    littleEndian32(addr(result), addr(tmp))

proc writeSFixed32*(stream: ProtobufStream, value: int32) =
    writeFixed32(stream, cast[uint32](value))

proc writeSFixed32*(stream: ProtobufStream, value: int32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed32)
    writeSFixed32(stream, value)

proc readSFixed32*(stream: ProtobufStream): int32 =
    result = cast[int32](readFixed32(stream))

proc writeFloat*(stream: ProtobufStream, value: float32) =
    var
        input = value
        output: float32

    littleEndian32(addr(output), addr(input))

    write(stream, output)

proc writeFloat*(stream: ProtobufStream, value: float32, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Fixed32)
    writeFloat(stream, value)

proc readFloat*(stream: ProtobufStream): float32 =
    var tmp: float32
    if readData(stream, addr(tmp), sizeof(tmp)) != sizeof(tmp):
        raise newException(IOError, "cannot read from stream")
    littleEndian32(addr(result), addr(tmp))

proc writeString*(stream: ProtobufStream, s: string) =
    writeUInt64(stream, len(s).uint64)
    write(stream, s)

proc writeString*(stream: ProtobufStream, s: string, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.LengthDelimited)
    writeString(stream, s)

proc writeBytes*(stream: ProtobufStream, s: bytes) =
    writeString(stream, string(s))

proc writeBytes*(stream: ProtobufStream, s: bytes, fieldNumber: int) =
    writeString(stream, string(s), fieldNumber)

proc safeReadStr*(stream: Stream, size: int): string =
    result = newString(size)
    if readData(stream, addr(result[0]), size) != size:
        raise newException(IOError, "cannot read from stream")

proc readString*(stream: ProtobufStream): string =
    let size = int(readUInt64(stream))
    result = safeReadStr(stream, size)

proc readBytes*(stream: ProtobufStream): bytes =
    bytes(readString(stream))

proc readEnum*[T](stream: ProtobufStream): T =
    result = T(readUInt32(stream))

proc writeEnum*[T](stream: ProtobufStream, value: T) =
    writeUInt32(stream, uint32(value))

proc writeEnum*[T](stream: ProtobufStream, value: T, fieldNumber: int) =
    writeTag(stream, fieldNumber, WireType.Varint)
    writeUInt32(stream, uint32(value))

proc sizeOfVarint[T](value: T): uint64 =
    var tmp = uint64(value)
    while tmp >= 0x80'u64:
        tmp = tmp shr 7
        inc(result)
    inc(result)

proc packedFieldSize*[T](values: seq[T], fieldtype: FieldType): uint64 =
    case fieldtype
    of FieldType.Fixed64, FieldType.SFixed64, FieldType.Double:
        result = uint64(len(values)) * 8
    of FieldType.Fixed32, FieldType.SFixed32, FieldType.Float:
        result = uint64(len(values)) * 4
    of FieldType.Int64, FieldType.UInt64, FieldType.Int32, FieldType.Bool,
       FieldType.UInt32, FieldType.Enum:
        for value in values:
            result += sizeOfVarint(value)
    of FieldType.SInt32:
        for value in values:
            result += sizeOfVarint(zigzagEncode(int32(value)))
    of FieldType.SInt64:
        for value in values:
            result += sizeOfVarint(zigzagEncode(int64(value)))
    else:
        raise newException(Exception, "invalid fieldtype")

proc sizeOfString*(s: string): uint64 =
    result = sizeOfVarint(len(s).uint64) + len(s).uint64

proc sizeOfBytes*(s: bytes): uint64 =
    result = sizeOfString(string(s))

proc sizeOfDouble*(value: float64): uint64 =
    result = 8

proc sizeOfFloat*(value: float32): uint64 =
    result = 4

proc sizeOfInt64*(value: int64): uint64 =
    result = sizeOfVarint(value)

proc sizeOfUInt64*(value: uint64): uint64 =
    result = sizeOfVarint(value)

proc sizeOfInt32*(value: int32): uint64 =
    result = sizeOfVarint(value)

proc sizeOfFixed64*(value: uint64): uint64 =
    result = 8

proc sizeOfFixed32*(value: uint32): uint64 =
    result = 4

proc sizeOfBool*(value: bool): uint64 =
    result = sizeOfVarint(value)

proc sizeOfUInt32*(value: uint32): uint64 =
    result = sizeOfVarint(value)

proc sizeOfSFixed32*(value: int32): uint64 =
    result = 4

proc sizeOfSFixed64*(value: int64): uint64 =
    result = 8

proc sizeOfSInt32*(value: int32): uint64 =
    result = sizeOfVarint(zigzagEncode(value))

proc sizeOfSInt64*(value: int64): uint64 =
    result = sizeOfVarint(zigzagEncode(value))

proc sizeOfEnum*[T](value: T): uint64 =
    result = sizeOfUInt32(value.uint32)

proc sizeOfLengthDelimited*(size: uint64): uint64 =
    result = size + sizeOfVarint(size)

proc sizeOfTag*(fieldNumber: int, wiretype: WireType): uint64 =
    result = sizeOfUInt32(uint32(makeTag(fieldNumber, wiretype)))

proc skipField*(stream: ProtobufStream, wiretype: WireType) =
    case wiretype
    of WireType.Varint:
        discard readVarint(stream)
    of WireType.Fixed64:
        discard readFixed64(stream)
    of WireType.Fixed32:
        discard readFixed32(stream)
    of WireType.LengthDelimited:
        let size = readVarint(stream)
        discard safeReadStr(stream, int(size))
    else:
        raise newException(Exception, "unsupported wiretype: " & $wiretype)

proc expectWireType*(actual: WireType, expected: varargs[WireType]) =
    for exp in expected:
        if actual == exp:
            return
    let message = "Got wiretype " & $actual & " but expected: " &
        join(expected, ", ")
    raise newException(UnexpectedWireTypeError, message)

macro writeMessage*(stream: ProtobufStream, message: typed, fieldNumber: int): typed =
    ## Write a message to a stream with tag and length.
    let t = getTypeInst(message)
    result = newStmtList(
        newCall(
            ident("writeTag"),
            stream,
            fieldNumber,
            newDotExpr(ident("WireType"), ident("LengthDelimited"))
        ),
        newCall(
            ident("writeVarint"),
            stream,
            newCall(ident("sizeOf" & $t), message)
        ),
        newCall(
            ident("write" & $t),
            stream,
            message
        )
    )

proc excl*(s: var IntSet, values: openArray[int]) =
    ## Exclude multiple values from an IntSet.
    for value in values:
        excl(s, value)

proc readLengthDelimited*(stream: ProtobufStream): string =
    let size = int(readVarint(stream))
    result = safeReadStr(stream, size)

proc readUnknownField*(stream: ProtobufStream, tag: Tag,
                       fields: var seq[UnknownField]) =
    var field: UnknownField

    new(field)
    field.fieldNumber = fieldNumber(tag)
    field.wireType = wiretype(tag)

    case field.wiretype
    of WireType.Varint:
        field.vint = readVarint(stream)
    of WireType.Fixed64:
        field.fixed64 = readFixed64(stream)
    of WireType.Fixed32:
        field.fixed32 = readFixed32(stream)
    of WireType.LengthDelimited:
        let size = readVarint(stream)
        field.data = safeReadStr(stream, int(size))
    else:
        raise newException(Exception, "unsupported wiretype: " & $field.wiretype)

    add(fields, field)

proc writeUnknownFields*(stream: ProtobufStream, fields: seq[UnknownField]) =
    for field in fields:
        case field.wiretype
        of WireType.Varint:
            writeTag(stream, field.fieldNumber, field.wireType)
            writeVarint(stream, field.vint)
        of WireType.Fixed64:
            writeFixed64(stream, field.fixed64, field.fieldNumber)
        of WireType.Fixed32:
            writeFixed32(stream, field.fixed32, field.fieldNumber)
        of WireType.LengthDelimited:
            writeString(stream, field.data, field.fieldNumber)
        else:
            raise newException(Exception, "unsupported wiretype: " & $field.wiretype)

proc discardUnknownFields*[T](message: T) =
    message.unknownFields = @[]

proc sizeOfUnknownField*(field: UnknownField): uint64 =
    result = sizeOfTag(field.fieldNumber, field.wiretype)
    case field.wiretype
    of WireType.Varint:
        result = result + sizeOfVarint(field.vint)
    of WireType.Fixed64:
        result = result + 8
    of WireType.Fixed32:
        result = result + 4
    of WireType.LengthDelimited:
        result = result + sizeOfLengthDelimited(uint64(len(field.data)))
    else:
        raise newException(Exception, "unsupported wiretype: " & $field.wiretype)