author | Gregory Szorc <gregory.szorc@gmail.com> |
Sat, 14 Apr 2018 16:36:15 -0700 | |
changeset 37711 | 65a23cc8e75b |
child 37898 | 2ae6a3134362 |
permissions | -rw-r--r-- |
37711
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 |
# cborutil.py - CBOR extensions |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 |
# |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com> |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 |
# |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 |
from __future__ import absolute_import |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 |
import struct |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 |
from ..thirdparty.cbor.cbor2 import ( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 |
decoder as decodermod, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 |
# Very short very of RFC 7049... |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
# |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 |
# Each item begins with a byte. The 3 high bits of that byte denote the |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 |
# "major type." The lower 5 bits denote the "subtype." Each major type |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 |
# has its own encoding mechanism. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 |
# |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 |
# Most types have lengths. However, bytestring, string, array, and map |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 |
# can be indefinite length. These are denotes by a subtype with value 31. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 |
# Sub-components of those types then come afterwards and are terminated |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 |
# by a "break" byte. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 |
MAJOR_TYPE_UINT = 0 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 |
MAJOR_TYPE_NEGINT = 1 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 |
MAJOR_TYPE_BYTESTRING = 2 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 |
MAJOR_TYPE_STRING = 3 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 |
MAJOR_TYPE_ARRAY = 4 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 |
MAJOR_TYPE_MAP = 5 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 |
MAJOR_TYPE_SEMANTIC = 6 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 |
MAJOR_TYPE_SPECIAL = 7 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 |
SUBTYPE_MASK = 0b00011111 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 |
SUBTYPE_HALF_FLOAT = 25 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 |
SUBTYPE_SINGLE_FLOAT = 26 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 |
SUBTYPE_DOUBLE_FLOAT = 27 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 |
SUBTYPE_INDEFINITE = 31 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 |
# Indefinite types begin with their major type ORd with information value 31. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 |
BEGIN_INDEFINITE_BYTESTRING = struct.pack( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 |
r'>B', MAJOR_TYPE_BYTESTRING << 5 | SUBTYPE_INDEFINITE) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 |
BEGIN_INDEFINITE_ARRAY = struct.pack( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 |
r'>B', MAJOR_TYPE_ARRAY << 5 | SUBTYPE_INDEFINITE) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 |
BEGIN_INDEFINITE_MAP = struct.pack( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 |
r'>B', MAJOR_TYPE_MAP << 5 | SUBTYPE_INDEFINITE) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 |
ENCODED_LENGTH_1 = struct.Struct(r'>B') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 |
ENCODED_LENGTH_2 = struct.Struct(r'>BB') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
53 |
ENCODED_LENGTH_3 = struct.Struct(r'>BH') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
54 |
ENCODED_LENGTH_4 = struct.Struct(r'>BL') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
55 |
ENCODED_LENGTH_5 = struct.Struct(r'>BQ') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 |
# The break ends an indefinite length item. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 |
BREAK = b'\xff' |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 |
BREAK_INT = 255 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 |
def encodelength(majortype, length): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 |
"""Obtain a value encoding the major type and its length.""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 |
if length < 24: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 |
return ENCODED_LENGTH_1.pack(majortype << 5 | length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 |
elif length < 256: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 |
return ENCODED_LENGTH_2.pack(majortype << 5 | 24, length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 |
elif length < 65536: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 |
return ENCODED_LENGTH_3.pack(majortype << 5 | 25, length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 |
elif length < 4294967296: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 |
return ENCODED_LENGTH_4.pack(majortype << 5 | 26, length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
else: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 |
return ENCODED_LENGTH_5.pack(majortype << 5 | 27, length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
73 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 |
def streamencodebytestring(v): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 |
yield encodelength(MAJOR_TYPE_BYTESTRING, len(v)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 |
yield v |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
77 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
78 |
def streamencodebytestringfromiter(it): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 |
"""Convert an iterator of chunks to an indefinite bytestring. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 |
Given an input that is iterable and each element in the iterator is |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
representable as bytes, emit an indefinite length bytestring. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 |
""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
84 |
yield BEGIN_INDEFINITE_BYTESTRING |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
85 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
86 |
for chunk in it: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 |
yield encodelength(MAJOR_TYPE_BYTESTRING, len(chunk)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 |
yield BREAK |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 |
def streamencodeindefinitebytestring(source, chunksize=65536): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 |
"""Given a large source buffer, emit as an indefinite length bytestring. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 |
This is a generator of chunks constituting the encoded CBOR data. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 |
""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
97 |
yield BEGIN_INDEFINITE_BYTESTRING |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 |
i = 0 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 |
l = len(source) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
101 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
102 |
while True: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
103 |
chunk = source[i:i + chunksize] |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
104 |
i += len(chunk) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
105 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
106 |
yield encodelength(MAJOR_TYPE_BYTESTRING, len(chunk)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
107 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
108 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
109 |
if i >= l: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
110 |
break |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
111 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
112 |
yield BREAK |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
113 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
114 |
def streamencodeint(v): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
115 |
if v >= 18446744073709551616 or v < -18446744073709551616: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
116 |
raise ValueError('big integers not supported') |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
117 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
118 |
if v >= 0: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
119 |
yield encodelength(MAJOR_TYPE_UINT, v) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
120 |
else: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
121 |
yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
122 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
123 |
def streamencodearray(l): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
124 |
"""Encode a known size iterable to an array.""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
125 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
126 |
yield encodelength(MAJOR_TYPE_ARRAY, len(l)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
127 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
128 |
for i in l: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
129 |
for chunk in streamencode(i): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
130 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
131 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
132 |
def streamencodearrayfromiter(it): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
133 |
"""Encode an iterator of items to an indefinite length array.""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
134 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
135 |
yield BEGIN_INDEFINITE_ARRAY |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
136 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
137 |
for i in it: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
138 |
for chunk in streamencode(i): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
139 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
140 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
141 |
yield BREAK |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
142 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
143 |
def streamencodeset(s): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
144 |
# https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
145 |
# semantic tag 258 for finite sets. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
146 |
yield encodelength(MAJOR_TYPE_SEMANTIC, 258) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
147 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
148 |
for chunk in streamencodearray(sorted(s)): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
149 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
150 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
151 |
def streamencodemap(d): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
152 |
"""Encode dictionary to a generator. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
153 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
154 |
Does not supporting indefinite length dictionaries. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
155 |
""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
156 |
yield encodelength(MAJOR_TYPE_MAP, len(d)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
157 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
158 |
for key, value in sorted(d.iteritems()): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
159 |
for chunk in streamencode(key): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
160 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
161 |
for chunk in streamencode(value): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
162 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
163 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
164 |
def streamencodemapfromiter(it): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
165 |
"""Given an iterable of (key, value), encode to an indefinite length map.""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
166 |
yield BEGIN_INDEFINITE_MAP |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
167 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
168 |
for key, value in it: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
169 |
for chunk in streamencode(key): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
170 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
171 |
for chunk in streamencode(value): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
172 |
yield chunk |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
173 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
174 |
yield BREAK |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
175 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
176 |
def streamencodebool(b): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
177 |
# major type 7, simple value 20 and 21. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
178 |
yield b'\xf5' if b else b'\xf4' |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
179 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
180 |
def streamencodenone(v): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
181 |
# major type 7, simple value 22. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
182 |
yield b'\xf6' |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
183 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
184 |
STREAM_ENCODERS = { |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
185 |
bytes: streamencodebytestring, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
186 |
int: streamencodeint, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
187 |
list: streamencodearray, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
188 |
tuple: streamencodearray, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
189 |
dict: streamencodemap, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
190 |
set: streamencodeset, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
191 |
bool: streamencodebool, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
192 |
type(None): streamencodenone, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
193 |
} |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
194 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
195 |
def streamencode(v): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
196 |
"""Encode a value in a streaming manner. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
197 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
198 |
Given an input object, encode it to CBOR recursively. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
199 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
200 |
Returns a generator of CBOR encoded bytes. There is no guarantee |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
201 |
that each emitted chunk fully decodes to a value or sub-value. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
202 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
203 |
Encoding is deterministic - unordered collections are sorted. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
204 |
""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
205 |
fn = STREAM_ENCODERS.get(v.__class__) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
206 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
207 |
if not fn: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
208 |
raise ValueError('do not know how to encode %s' % type(v)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
209 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
210 |
return fn(v) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
211 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
212 |
def readindefinitebytestringtoiter(fh, expectheader=True): |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
213 |
"""Read an indefinite bytestring to a generator. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
214 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
215 |
Receives an object with a ``read(X)`` method to read N bytes. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
216 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
217 |
If ``expectheader`` is True, it is expected that the first byte read |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
218 |
will represent an indefinite length bytestring. Otherwise, we |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
219 |
expect the first byte to be part of the first bytestring chunk. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
220 |
""" |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
221 |
read = fh.read |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
222 |
decodeuint = decodermod.decode_uint |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
223 |
byteasinteger = decodermod.byte_as_integer |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
224 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
225 |
if expectheader: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
226 |
initial = decodermod.byte_as_integer(read(1)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
227 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
228 |
majortype = initial >> 5 |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
229 |
subtype = initial & SUBTYPE_MASK |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
230 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
231 |
if majortype != MAJOR_TYPE_BYTESTRING: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
232 |
raise decodermod.CBORDecodeError( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
233 |
'expected major type %d; got %d' % (MAJOR_TYPE_BYTESTRING, |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
234 |
majortype)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
235 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
236 |
if subtype != SUBTYPE_INDEFINITE: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
237 |
raise decodermod.CBORDecodeError( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
238 |
'expected indefinite subtype; got %d' % subtype) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
239 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
240 |
# The indefinite bytestring is composed of chunks of normal bytestrings. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
241 |
# Read chunks until we hit a BREAK byte. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
242 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
243 |
while True: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
244 |
# We need to sniff for the BREAK byte. |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
245 |
initial = byteasinteger(read(1)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
246 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
247 |
if initial == BREAK_INT: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
248 |
break |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
249 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
250 |
length = decodeuint(fh, initial & SUBTYPE_MASK) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
251 |
chunk = read(length) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
252 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
253 |
if len(chunk) != length: |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
254 |
raise decodermod.CBORDecodeError( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
255 |
'failed to read bytestring chunk: got %d bytes; expected %d' % ( |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
256 |
len(chunk), length)) |
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
257 |
|
65a23cc8e75b
cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
258 |
yield chunk |