~ehmry/nim_cbor

nim_cbor/src/cbor.nim -rw-r--r-- 24.5 KiB
a77c79bfEmery Hemingway Add `isTagged` 17 days ago
                                                                                
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
## A CBOR encoder and decoder with an emphasis on data streaming.
##
## .. include:: ../doc/cbor.txt
##

import endians, hashes, math, options, streams, strutils, tables

func isHalfPrecise(single: float32): bool =
  # TODO: check for subnormal false-positives
  let val = cast[uint32](single)
  if val == 0 or val == (1 shl 31):
    result = true
  else:
    let
      exp = int32((val and (0xff'u32 shl 23)) shr 23) - 127
      mant = val and 0x7fffff'u32
    if -25 < exp and exp < 16 and (mant and 0x1fff) == 0:
      result = true

func floatHalf(single: float32): uint16 =
  ## Convert a 32-bit float to 16-bits.
  let
    val = cast[uint32](single)
    exp = val and 0x7f800000
    mant = val and 0x7fffff
    sign = uint16(val shr 16) and (1 shl 15)
  let
    unbiasedExp = int32(exp shr 23) - 127
    halfExp = unbiasedExp + 15
  if halfExp < 1:
    if 14 - halfExp < 25:
      result = sign or uint16((mant or 0x800000) shr uint16(14 - halfExp))
  else:
    result = sign or uint16(halfExp shl 10) or uint16(mant shr 13)

func floatSingle(half: uint16): float32 =
  ## Convert a 16-bit float to 32-bits.
  func ldexp(x: float64; exponent: int): float64 {.importc: "ldexp",
        header: "<math.h>".}
  let
    exp = (half shr 10) and 0x1f
    mant = float64(half and 0x3ff)
    val = if exp == 0:
        ldexp(mant, -24)
      elif exp != 31:
        ldexp(mant + 1024, exp.int - 25)
      else:
        if mant == 0: Inf else: Nan
  if (half and 0x8000) == 0: val else: -val

const
  PositiveMajor = 0'u8
  NegativeMajor = 1'u8
  BytesMajor = 2'u8
  TextMajor = 3'u8
  ArrayMajor = 4'u8
  MapMajor = 5'u8
  TagMajor = 6'u8
  SimpleMajor = 7'u8
  Null = 0xf6'u8

type
  CborEventKind* {.pure.} = enum
    ## enumeration of events that may occur while parsing
    cborEof,
    cborPositive,
    cborNegative,
    cborBytes,
    cborText,
    cborArray,
    cborMap,
    cborTag,
    cborSimple,
    cborFloat,
    cborBreak

  CborParser* = object ## CBOR parser state.
    s*: Stream
    intVal: uint64
    minor: uint8
    kind*: CborEventKind

  CborParseError* = object of ValueError

  CborNodeKind* = enum
    cborUnsigned = 0,
    cborNegative = 1,
    cborBytes = 2,
    cborText = 3,
    cborArray = 4,
    cborMap = 5,
    cborTag = 6,
    cborSimple = 7,
    cborFloat,
    cborRaw

  CborNode* = object
    ## An abstract representation of a CBOR item. Useful for diagnostics.
    tag: Option[uint64]
    case kind*: CborNodeKind
    of cborUnsigned:
      uint*: uint64
    of cborNegative:
      int*: int64
    of cborBytes:
      bytes*: seq[byte]
    of cborText:
      text*: string
    of cborArray:
      seq*: seq[CborNode]
    of cborMap:
      map*: OrderedTable[CborNode, CborNode]
    of cborTag:
      discard
    of cborSimple:
      simple*: uint8
    of cborFloat:
      float*: float64
    of cborRaw:
      raw*: string

func `==`*(x, y: CborNode): bool

func hash*(x: CborNode): Hash

proc parseAssert(check: bool; msg = "") {.inline.} =
  if not check: raise newException(CborParseError, msg)

func isIndefinite*(c: CborParser): bool {.inline.} = c.minor == 31
  ## Return true if the parser is positioned on an item of indefinite length.

func open*(c: var CborParser; s: Stream) =
  ## Begin parsing a stream of CBOR in binary form.
  ## The parser will be initialized in an EOF state, call
  ## ``next`` to advance it before parsing.
  c.s = s
  c.kind = cborEof
  c.intVal = 0

proc next*(c: var CborParser) =
  ## Advance the parser to the initial or next event.
  if c.s.atEnd:
    c.kind = CborEventKind.cborEof
    c.intVal = 0
  else:
    let
      ib = c.s.readUint8
      mb = ib shr 5
    c.minor = ib and 0b11111
    case c.minor
    of 0..23:
      c.intVal = c.minor.uint64
    of 24:
      c.intVal = c.s.readChar.uint64
    of 25:
      c.intVal = c.s.readChar.uint64
      c.intVal = (c.intVal shl 8) or c.s.readChar.uint64
    of 26:
      c.intVal = c.s.readChar.uint64
      for _ in 1..3:
        {.unroll.}
        c.intVal = (c.intVal shl 8) or c.s.readChar.uint64
    of 27:
      c.intVal = c.s.readChar.uint64
      for _ in 1..7:
        {.unroll.}
        c.intVal = (c.intVal shl 8) or c.s.readChar.uint64
    else:
      c.intVal = 0
    case mb
    of PositiveMajor:
      c.kind = CborEventKind.cborPositive
    of NegativeMajor:
      c.kind = CborEventKind.cborNegative
    of BytesMajor:
      c.kind = CborEventKind.cborBytes
    of TextMajor:
      c.kind = CborEventKind.cborText
    of ArrayMajor:
      c.kind = CborEventKind.cborArray
    of MapMajor:
      c.kind = CborEventKind.cborMap
    of TagMajor:
      c.kind = CborEventKind.cborTag
    of SimpleMajor:
      if c.minor in {25, 26, 27}:
        c.kind = CborEventKind.cborFloat
      elif c.isIndefinite:
        c.kind = CborEventKind.cborBreak
      else:
        c.kind = CborEventKind.cborSimple
    else:
      raise newException(CborParseError, "unhandled major type " & $mb)

proc nextUInt*(c: var CborParser): BiggestUInt =
  ## Parse the integer value that the parser is positioned on.
  assert(c.kind == CborEventKind.cborPositive, $c.kind)
  result = c.intVal.BiggestUInt
  c.next()

proc nextInt*(c: var CborParser): BiggestInt =
  ## Parse the integer value that the parser is positioned on.
  case c.kind
  of CborEventKind.cborPositive:
    result = c.intVal.BiggestInt
  of CborEventKind.cborNegative:
    result = -1.BiggestInt - c.intVal.BiggestInt
  else:
    assert(false)
  c.next()

proc nextFloat*(c: var CborParser): float64 =
  ## Parse the float value that the parser is positioned on.
  parseAssert(c.kind == CborEventKind.cborFloat)
  case c.minor
  of 25:
    result = floatSingle(c.intVal.uint16).float64
  of 26:
    result = cast[float32](c.intVal).float64
  of 27:
    result = cast[float64](c.intVal)
  else:
    discard
  c.next()

func bytesLen*(c: CborParser): int =
  ## Return the length of the byte string that the parser is positioned on.
  assert(c.kind == CborEventKind.cborBytes, $c.kind)
  c.intVal.int

proc nextBytes*(c: var CborParser; buf: var openArray[byte]) =
  ## Read the bytes that the parser is positioned on and advance.
  assert(c.kind == CborEventKind.cborBytes, $c.kind)
  assert(buf.len == c.intVal.int)
  if buf.len > 0:
    let n = c.s.readData(buf[0].addr, buf.len)
    parseAssert(n == buf.len, "truncated read of CBOR data")
    c.next()

proc nextBytes*(c: var CborParser): seq[byte] =
  ## Read the bytes that the parser is positioned on into a seq and advance.
  result = newSeq[byte](c.intVal.int)
  nextBytes(c, result)

func textLen*(c: CborParser): int =
  ## Return the length of the text that the parser is positioned on.
  assert(c.kind == CborEventKind.cborText, $c.kind)
  c.intVal.int

proc nextText*(c: var CborParser; buf: var string) =
  ## Read the text that the parser is positioned on into a string and advance.
  assert(c.kind == CborEventKind.cborText, $c.kind)
  buf.setLen c.intVal.int
  if buf.len > 0:
    let n = c.s.readData(buf[0].addr, buf.len)
    assert(n == buf.len)
  c.next()

proc nextText*(c: var CborParser): string = nextText(c, result)
  ## Read the text that the parser is positioned on into a string and advance.

func arrayLen*(c: CborParser): int =
  ## Return the length of the array that the parser is positioned on.
  assert(c.kind == CborEventKind.cborArray, $c.kind)
  c.intVal.int

func mapLen*(c: CborParser): int =
  ## Return the length of the map that the parser is positioned on.
  assert(c.kind == CborEventKind.cborMap, $c.kind)
  c.intVal.int

func tag*(c: CborParser): uint64 =
  ## Return the tag value the parser is positioned on.
  assert(c.kind == CborEventKind.cborTag, $c.kind)
  c.intVal

proc nextNode*(c: var CborParser): CborNode =
  ## Parse the item the parser is positioned on into a ``CborNode``.
  ## This is cheap for numbers or simple values but expensive
  ## for nested types.
  case c.kind
  of CborEventKind.cborEof:
    raise newException(EOFError, "end of CBOR stream")
  of CborEventKind.cborPositive:
    result = CborNode(kind: cborUnsigned, uint: c.intVal)
    c.next()
  of CborEventKind.cborNegative:
    result = CborNode(kind: cborNegative, int: -1 - c.intVal.int64)
    c.next()
  of CborEventKind.cborBytes:
    if c.isIndefinite:
      result = CborNode(kind: cborBytes, bytes: newSeq[byte]())
      c.next
      while c.kind != CborEventKind.cborBreak:
        parseAssert(c.kind == CborEventKind.cborBytes)
        let
          chunkLen = c.intVal.int
          pos = result.bytes.len
        result.bytes.setLen(pos+chunkLen)
        let n = c.s.readData(result.bytes[pos].addr, chunkLen)
        parseAssert(n == chunkLen)
        c.next()
    else:
      result = CborNode(kind: cborBytes, bytes: c.nextBytes())
  of CborEventKind.cborText:
    if c.isIndefinite:
      result = CborNode(kind: cborText, text: "")
      c.next()
      while c.kind != CborEventKind.cborBreak:
        parseAssert(c.kind == CborEventKind.cborText)
        let
          chunkLen = c.intVal.int
          pos = result.text.len
        result.text.setLen(pos+chunkLen)
        let n = c.s.readData(result.text[pos].addr, chunkLen)
        parseAssert(n == chunkLen)
        c.next()
      c.next()
    else:
      result = CborNode(kind: cborText, text: c.nextText())
  of CborEventKind.cborArray:
    result = CborNode(kind: cborArray, seq: newSeq[CborNode](c.intVal))
    if c.isIndefinite:
      c.next()
      while c.kind != CborEventKind.cborBreak:
        result.seq.add(c.nextNode())
      c.next()
    else:
      c.next()
      for i in 0..result.seq.high:
        result.seq[i] = c.nextNode()
  of CborEventKind.cborMap:
    let mapLen = c.intVal.int
    result = CborNode(kind: cborMap, map: initOrderedTable[CborNode, CborNode](
        mapLen.nextPowerOfTwo))
    if c.isIndefinite:
      c.next()
      while c.kind != CborEventKind.cborBreak:
        result.map[c.nextNode()] = c.nextNode()
      c.next()
    else:
      c.next()
      for _ in 1 .. mapLen:
        result.map[c.nextNode()] = c.nextNode()
  of CborEventKind.cborTag:
    let tag = c.intval
    c.next()
    result = c.nextNode()
    result.tag = some tag
  of CborEventKind.cborSimple:
    case c.minor
    of 24:
      result = CborNode(kind: cborSimple, simple: c.intval.uint8)
    else:
      result = CborNode(kind: cborSimple, simple: c.minor)
    c.next()
  of CborEventKind.cborFloat:
    result = CborNode(kind: cborFloat, float: c.nextFloat())
  of CborEventKind.cborBreak:
    discard

proc readCbor*(s: Stream): CborNode =
  ## Parse a stream into a CBOR object.
  var parser: CborParser
  parser.open(s)
  parser.next()
  parser.nextNode()

proc parseCbor*(s: string): CborNode =
  ## Parse a string into a CBOR object.
  ## A wrapper over stream parsing.
  readCbor(newStringStream s)

func initialByte(major, minor: Natural): uint8 {.inline.} =
  uint8((major shl 5) or (minor and 0b11111))

{.push checks: off.}
proc writeInitial[T: SomeInteger](str: Stream; m: uint8; n: T) =
  ## Write the initial integer of a CBOR item.
  let m = m shl 5
  if n < 24:
    str.write(m or n.uint8)
  elif n <= (T)uint8.high:
    str.write(m or 24'u8)
    str.write(n.uint8)
  elif n <= (T)uint16.high:
    str.write(m or 25'u8)
    str.write((uint8)n shr 8)
    str.write((uint8)n)
  elif n <= (T)uint32.high:
    str.write(m or 26'u8)
    for i in countdown(24, 8, 8):
      {.unroll.}
      str.write((uint8)n shr i)
    str.write((uint8)n)
  else:
    str.write(m or 27'u8)
    for i in countdown(56, 8, 8):
      {.unroll.}
      str.write((uint8)n shr i)
    str.write((uint8)n)
{.pop.}

proc writeCbor*(str: Stream; n: SomeUnsignedInt) =
  ## Encode an unsigned integer to CBOR binary form.
  str.writeInitial(0, n)

proc writeCbor*(str: Stream; n: SomeSignedInt) =
  ## Encode a signed integer to CBOR binary form.
  if n < 0:
    # Major type 1
    str.writeInitial(1, -1 - n)
  else:
    # Major type 0
    str.writeInitial(0, n)

proc writeCbor*(str: Stream; a: openArray[char | uint8 | int8]) =
  ## Encode a byte string to CBOR binary form.
  str.writeInitial(2, a.len)
  str.writeData(unsafeAddr(a), a.len)

proc writeCbor*(str: Stream; s: string) =
  ## Encode a text string to CBOR binary form.
  str.writeInitial(TextMajor, s.len)
  str.write(s)

proc writeCbor*[T](str: Stream; a: openArray[T]) =
  ## Encode to a CBOR array in binary form.
  str.writeInitial(4, a.len)
  for x in a.items:
    str.writeCbor(x)

proc writeCbor*(str: Stream; o: object) =
  ## Encode an object to a CBOR map in binary form.
  var n: uint
  for _, _ in o.fieldPairs:
    inc n
  str.writeInitial(5, n)
  for k, v in o.fieldPairs:
    str.writeCbor(k)
    str.writeCbor(v)

proc writeCbor*(str: Stream; obj: ref object) =
  ## Encode a reference to CBOR or encode the null item.
  if obj.isNil:
    # Major type 7
    str.write(Null)
  else:
    str.writeCbor(obj)

proc writeCbor*(str: Stream; b: bool) =
  ## Encode a boolean value to CBOR binary form.
  str.write(initialByte(7, (if b: 21 else: 20)))

proc writeCbor*(str: Stream; float: SomeFloat) =
  ## Encode a floating-point number to CBOR binary form.
  case float.classify
  of fcNormal, fcSubnormal:
    let single = float.float32
    if single.float64 == float.float64:
      if single.isHalfPrecise:
        let half = floatHalf(single)
        str.write(initialByte(7, 25))
        when system.cpuEndian == bigEndian:
          str.write(half)
        else:
          var be: uint16
          swapEndian16 be.addr, half.unsafeAddr
          str.write(be)
      else:
        str.write initialByte(7, 26)
        when system.cpuEndian == bigEndian:
          str.write(single)
        else:
          var be: uint32
          swapEndian32 be.addr, single.unsafeAddr
          str.write(be)
    else:
      str.write initialByte(7, 27)
      when system.cpuEndian == bigEndian:
        str.write(float)
      else:
        var be: float64
        swapEndian64 be.addr, float.unsafeAddr
        str.write be
    return
  of fcZero:
    str.write initialByte(7, 25)
    str.write((char)0x00)
  of fcNegZero:
    str.write initialByte(7, 25)
    str.write((char)0x80)
  of fcInf:
    str.write initialByte(7, 25)
    str.write((char)0x7c)
  of fcNan:
    str.write initialByte(7, 25)
    str.write((char)0x7e)
  of fcNegInf:
    str.write initialByte(7, 25)
    str.write((char)0xfc)
  str.write((char)0x00)

proc writeCborArrayLen*(str: Stream; len: Natural) =
  ## Write a marker to the stream that initiates an array of ``len`` items.
  str.writeInitial(4, len)

proc writeCborIndefiniteArrayLen*(str: Stream) =
  ## Write a marker to the stream that initiates an array of indefinite length.
  ## Indefinite length arrays are composed of an indefinite amount of arrays
  ## of definite lengths.
  str.write(initialByte(4, 31))

proc writeCborMapLen*(str: Stream; len: Natural) =
  ## Write a marker to the stream that initiates an map of ``len`` pairs.
  str.writeInitial(5, len)

proc writeCborIndefiniteMapLen*(str: Stream) =
  ## Write a marker to the stream that initiates a map of indefinite length.
  ## Indefinite length maps are composed of an indefinite amount of maps
  ## of definite length.
  str.write(initialByte(5, 31))

proc writeCborBreak*(str: Stream) =
  ## Write a marker to the stream that ends an indefinite array or map.
  str.write(initialByte(7, 31))

proc writeCborTag*(str: Stream; tag: Natural) {.inline.} =
  ## Write a tag for the next CBOR item to a binary stream.
  str.writeInitial(6, tag)

proc isSorted(n: CborNode): bool {.gcsafe.}

proc writeCbor*(s: Stream; n: CborNode) =
  ## Encode a CBOR node to a binary stream.
  if n.tag.isSome:
    s.writeCborTag(n.tag.get)
  case n.kind:
  of cborUnsigned:
    s.writeCbor(n.uint)
  of cborNegative:
    s.writeCbor(n.int)
  of cborBytes:
    s.writeInitial(cborBytes.uint8, n.bytes.len)
    for b in n.bytes.items:
      s.write(b)
  of cborText:
    s.writeInitial(cborText.uint8, n.text.len)
    s.write(n.text)
  of cborArray:
    s.writeInitial(4, n.seq.len)
    for e in n.seq:
      s.writeCbor(e)
  of cborMap:
    assert(n.isSorted, "refusing to write unsorted map to stream")
    s.writeInitial(5, n.map.len)
    for k, v in n.map.pairs:
      s.writeCbor(k)
      s.writeCbor(v)
  of cborTag:
    discard
  of cborSimple:
    if n.simple > 31'u or n.simple == 24:
      s.write(initialByte(cborSimple.uint8, 24))
      s.write(n.simple)
    else:
      s.write(initialByte(cborSimple.uint8, n.simple))
  of cborFloat:
    s.writeCbor(n.float)
  of cborRaw:
    s.write(n.raw)

proc writeCborArray*(str: Stream; args: varargs[CborNode, `%`]) =
  ## Encode to a CBOR array in binary form. This magic doesn't
  ## always work, some arguments may need to be explicitly
  ## converted with ``%`` before passing.
  str.writeCborArrayLen(args.len)
  for x in args:
    str.writeCbor(x)

proc encode*(n: CborNode): string =
  ## Encode a CBOR node to a binary representation.
  ## A wrapper over ``writeCbor``.
  let s = newStringStream()
  s.writeCbor(n)
  s.data

proc toRaw*(n: CborNode): CborNode =
  ## Reduce a CborNode to a string of bytes.
  if n.kind == cborRaw: n
  else: CborNode(kind: cborRaw, raw: encode(n))

func `==`*(x, y: CborNode): bool =
  if x.kind == y.kind and x.tag == y.tag:
    case x.kind
    of cborUnsigned:
      x.uint == y.uint
    of cborNegative:
      x.int == y.int
    of cborBytes:
      x.bytes == y.bytes
    of cborText:
      x.text == y.text
    of cborArray:
      x.seq == y.seq
    of cborMap:
      x.map == y.map
    of cborTag:
      false
    of cborSimple:
      x.simple == y.simple
    of cborFloat:
      x.float == y.float
    of cborRaw:
      x.raw == y.raw
  else:
    false

func `==`*(x: CborNode; y: SomeInteger): bool =
  case x.kind
  of cborUnsigned:
    x.uint == y
  of cborNegative:
    x.int == y
  else:
    false

func `==`*(x: CborNode; y: string): bool =
  x.kind == cborText and x.text == y

func `==`*(x: CborNode; y: SomeFloat): bool =
  if x.kind == cborFloat: x.float == y

func hash(x: CborNode): Hash =
  var h = hash(get(x.tag, 0))
  h = h !& x.kind.int.hash
  case x.kind
  of cborUnsigned:
    h = h !& x.uint.hash
  of cborNegative:
    h = h !& x.int.hash
  of cborBytes:
    h = h !& x.bytes.hash
  of cborText:
    h = h !& x.text.hash
  of cborArray:
    for y in x.seq:
      h = h !& y.hash
  of cborMap:
    for key, val in x.map.pairs:
      h = h !& key.hash
      h = h !& val.hash
  of cborTag:
    discard
  of cborSimple:
    h = h !& x.simple.hash
  of cborFloat:
    h = h !& x.float.hash
  of cborRaw:
    assert(x.tag.isNone)
    h = x.raw.hash
  !$h

proc isSorted(n: CborNode): bool =
  ## Check if the item is sorted correctly.
  var lastRaw = ""
  for key in n.map.keys:
    let thisRaw = key.toRaw.raw
    if lastRaw != "":
      if thisRaw.len > lastRaw.len: return false
      if cmp(lastRaw, thisRaw) > 0: return false
    lastRaw = thisRaw
  true

proc sort*(n: var CborNode) =
  ## Sort a CBOR map object.
  var tmp = initOrderedTable[CborNode, CborNode](n.map.len.nextPowerOfTwo)
  for key, val in n.map.mpairs:
    tmp[key.toRaw] = move(val)
  sort(tmp) do (x, y: tuple[k: CborNode; v: CborNode]) -> int:
    result = x.k.raw.len - y.k.raw.len
    if result == 0:
      result = cmp(x.k.raw, y.k.raw)
  n.map = tmp

proc `$`*(n: CborNode): string =
  ## Get a ``CborNode`` in diagnostic notation.
  result = ""
  if n.tag.isSome:
    result.add($n.tag.get)
    result.add("(")
  case n.kind
  of cborUnsigned:
    result.add $n.uint
  of cborNegative:
    result.add $n.int
  of cborBytes:
    result.add "h'"
    for c in n.bytes:
      result.add(c.toHex)
    result.add "'"
  of cborText:
    result.add escape n.text
  of cborArray:
    result.add "["
    for i in 0..<n.seq.high:
      result.add $(n.seq[i])
      result.add ", "
    if n.seq.len > 0:
      result.add $(n.seq[n.seq.high])
    result.add "]"
  of cborMap:
    result.add "{"
    let final = n.map.len
    var i = 1
    for k, v in n.map.pairs:
      result.add $k
      result.add ": "
      result.add $v
      if i != final:
        result.add ", "
      inc i
    result.add "}"
  of cborTag:
    discard
  of cborSimple:
    case n.simple
    of 20: result.add "false"
    of 21: result.add "true"
    of 22: result.add "null"
    of 23: result.add "undefined"
    of 31: discard # break code for indefinite-length items
    else: result.add "simple(" & $n.simple & ")"
  of cborFloat:
    case n.float.classify
    of fcNan:
      result.add "NaN"
    of fcInf:
      result.add "Infinity"
    of fcNegInf:
      result.add "-Infinity"
    else:
      result.add $n.float
  of cborRaw:
    result.add $parseCbor(n.raw)
  if n.tag.isSome:
    result.add(")")

func `%`*(x: CborNode): CborNode = x

func `%`*(x: SomeUnsignedInt): CborNode =
  CborNode(kind: cborUnsigned, uint: x.uint64)

func `%`*(x: SomeSignedInt): CborNode =
  if x > 0:
    CborNode(kind: cborUnsigned, uint: x.uint64)
  else:
    CborNode(kind: cborNegative, int: x.int64)

func `%`*(x: openarray[byte]): CborNode =
  CborNode(kind: cborBytes, bytes: @x)

func `%`*(x: string): CborNode =
  CborNode(kind: cborText, text: x)

func `%`*(x: openarray[CborNode]): CborNode =
  CborNode(kind: cborArray, seq: @x)

func `%`*(pairs: openArray[(CborNode, CborNode)]): CborNode =
  CborNode(kind: cborMap, map: pairs.toOrderedTable)

func `%`*(tag: uint64; val: CborNode): CborNode =
  result = %val
  result.tag = some(tag)

func `%`*(x: bool): CborNode =
  case x
  of false:
    CborNode(kind: cborSimple, simple: 20)
  of true:
    CborNode(kind: cborSimple, simple: 21)

func `%`*(x: SomeFloat): CborNode =
  CborNode(kind: cborFloat, float: x.float64)

func `%`*(x: pointer): CborNode =
  ## A hack to produce a CBOR null item.
  assert(x.isNil)
  CborNode(kind: cborSimple, simple: 22)

func initCborBytes*(b: string): CborNode =
  ## Create a CBOR byte string from ``b``.
  result = CborNode(kind: cborBytes, bytes: newSeq[byte](b.len))
  for i in 0..<b.len:
    result.bytes[i] = (byte)b[i]

func initCborBytes*(len: int): CborNode =
  ## Create a CBOR byte string of ``len`` bytes.
  CborNode(kind: cborBytes, bytes: newSeq[byte](len))

func initCborText*(s: string): CborNode =
  ## Create a CBOR text string from ``s``.
  ## CBOR text must be unicode.
  CborNode(kind: cborText, text: s)

func initCborArray*(): CborNode =
  ## Create an empty CBOR array.
  CborNode(kind: cborArray, seq: newSeq[CborNode]())

func initCborArray*(len: Natural): CborNode =
  ## Initialize a CBOR arrary.
  CborNode(kind: cborArray, seq: newSeq[CborNode](len))

func initCborMap*(initialSize = tables.defaultInitialSize): CborNode =
  ## Initialize a CBOR arrary.
  CborNode(kind: cborMap,
      map: initOrderedTable[CborNode, CborNode](initialSize))

func initCbor*(items: varargs[CborNode, `%`]): CborNode =
  ## Initialize a CBOR arrary.
  CborNode(kind: cborArray, seq: @items)

template initCborOther*(x: untyped): CborNode =
  ## Initialize a ``CborNode`` from a type where ``%`` is not implemented.
  ## This encodes ``x`` to binary using ``writeCbor``, so
  ## ``$(initCborOther(x))`` will incur an encode and decode roundtrip.
  let s = newStringStream()
  s.writeCbor(x)
  CborNode(kind: cborRaw, raw: s.data)

func isTagged*(n: CborNode): bool =
  ## Check if a CBOR item has a tag.
  n.tag.isSome

proc `tag=`*(result: var CborNode; tag: Natural) =
  ## Tag a CBOR item.
  result.tag = some(tag.uint64)

func tag*(n: CborNode): uint64 =
  ## Get a CBOR item tag.
  n.tag.get

func isBool*(n: CborNode): bool =
  (n.kind == cborSimple) and (n.simple in {20, 21})

func getBool*(n: CborNode; default = false): bool =
  ## Get the boolean value of a ``CborNode`` or a fallback.
  if n.kind == cborSimple:
    case n.simple
    of 20: false
    of 21: true
    else: default
  else:
    default

func isNull*(n: CborNode): bool =
  ## Return true if ``n`` is a CBOR null.
  (n.kind == cborSimple) and (n.simple == 22)

proc getInt*(n: CborNode, default: int = 0): int =
  ## Get the numerical value of a ``CborNode`` or a fallback.
  case n.kind
  of cborUnsigned: n.uint.int
  of cborNegative: n.int.int
  else: default

proc getUnsigned*(n: CborNode, default: uint64 = 0): uint64 =
  ## Get the numerical value of a ``CborNode`` or a fallback.
  case n.kind
  of cborUnsigned: n.uint
  of cborNegative: n.int.uint64
  else: default

proc getSigned*(n: CborNode, default: int64 = 0): int64 =
  ## Get the numerical value of a ``CborNode`` or a fallback.
  case n.kind
  of cborUnsigned: n.uint.int64
  of cborNegative: n.int
  else: default


func getFloat*(n: CborNode; default = 0.0): float =
  ## Get the floating-poing value of a ``CborNode`` or a fallback.
  if n.kind == cborFloat:
    n.float
  else:
    default

func len*(node: CborNode): int =
  ## Return the logical length of a ``CborNode``, that is the
  ## length of a byte or text string, or the number of
  ## elements in a array or map. Otherwise it returns -1.
  case node.kind
  of cborBytes: node.bytes.len
  of cborText: node.text.len
  of cborArray: node.seq.len
  of cborMap: node.map.len
  else: -1