]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
some crappy cbor fiddling with ip addresses
authorMaria Matejka <mq@ucw.cz>
Sun, 10 Dec 2023 13:35:35 +0000 (14:35 +0100)
committerMaria Matejka <mq@ucw.cz>
Sun, 10 Dec 2023 13:35:35 +0000 (14:35 +0100)
mq-scraps/test-encap.cbor [new file with mode: 0644]
mq-scraps/test-encap.py [new file with mode: 0644]
mq-scraps/test-ip.cbor [new file with mode: 0644]
mq-scraps/test-ip.py [new file with mode: 0644]

diff --git a/mq-scraps/test-encap.cbor b/mq-scraps/test-encap.cbor
new file mode 100644 (file)
index 0000000..32ffd30
--- /dev/null
@@ -0,0 +1 @@
\18\84\ 1\ 2\ 3Ø6P \ 1\r¸\124Þ­¾ïÊþúÎþí
\ No newline at end of file
diff --git a/mq-scraps/test-encap.py b/mq-scraps/test-encap.py
new file mode 100644 (file)
index 0000000..2778f36
--- /dev/null
@@ -0,0 +1,20 @@
+import cbor2
+import json
+import ipaddress
+
+print(cbor2.decoder.semantic_decoders)
+print(cbor2.decoder.special_decoders)
+cbor2.decoder.semantic_decoders[54] = cbor2.decoder.semantic_decoders[260]
+
+def testhook(decoder, value):
+    print(value)
+    print(value.tag, value.value)
+    return {
+            24: lambda: value,
+            54: lambda: ipaddress.IPv6Address(value.value),
+            }[value.tag]()
+
+with open("test-encap.cbor", "rb") as sf:
+    dec = cbor2.CBORDecoder(sf, testhook)
+    print(data := dec.decode())
+    print(data.value[3])
diff --git a/mq-scraps/test-ip.cbor b/mq-scraps/test-ip.cbor
new file mode 100644 (file)
index 0000000..981a90b
--- /dev/null
@@ -0,0 +1 @@
+\84\ 1\ 2\ 3Ø6P \ 1\r¸\124Þ­¾ïÊþúÎþí
\ No newline at end of file
diff --git a/mq-scraps/test-ip.py b/mq-scraps/test-ip.py
new file mode 100644 (file)
index 0000000..4868dd1
--- /dev/null
@@ -0,0 +1,8 @@
+import cbor2
+import json
+
+with open("test-ip.cbor", "rb") as sf:
+    loaded = cbor2.load(sf)
+
+print(loaded)
+print(json.dumps(loaded))