forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binstruct.py
504 lines (431 loc) · 14.5 KB
/
binstruct.py
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
# binstruct - binary structure serialization
# ------------------------------------------
# https://github.com/albertz/binstruct/,
# Copyright (c) 2012, Albert Zeyer, www.az2000.de
# All rights reserved.
# file created 2012-06-10
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# I wanted sth as simple as Python repr or JSON, but:
# - binary data should only add constant overhead
# - very simple format
# - very very big data should be possible
# - searching through the file should be fast
# Where the first 2 points were so important for me that
# I implemented this format.
# Some related formats and the reasons they weren't good
# enough for me.
# BSON:
# - keys in structs are only C-strings. I want
# any possible data here.
# - already too complicated
# Bencode:
# - too restricted, too less formats
# OGDL:
# - too simple
# ...
# More, without details:
# * CBOR ([RFC](http://tools.ietf.org/html/rfc7049), [HN discussion](https://news.ycombinator.com/item?id=6632576))
# * msgpack
# * Google's Protocol Buffers
# * [Apache (Facebook) Thrift](http://thrift.apache.org/)
### This format.
FILESIGNATURE = "BINSTRUCT.1\x00"
FILESIGNATURE_CRYPTED = "BINSTRUCT.CRYPTED.1\x00"
class FormatError(Exception): pass
from array import array
from StringIO import StringIO
# Bool. Byte \x00 or \x01.
def boolEncode(b): return array("B", (b,))
def boolDecode(stream): return bool(ord(stream.read(1)))
# Integers. Use EliasGamma to decode the byte size
# of the signed integer. I.e. we start with EliasGamma,
# then align that to the next byte and the signed integer
# in big endian follows.
def bitsOf(n):
assert n >= 0
if n == 0: return 0
return len(bin(n)) - 2
def bitListToInt(l):
i = 0
bitM = 1
for bit in reversed(l):
i += bitM * int(bit)
bitM <<= 1
return i
def bitListToBin(l):
bin = array("B", (0,)) * (len(l) / 8)
for i in range(0, len(l), 8):
byte = bitListToInt(l[i:i+8])
bin[i/8] = byte
return bin
def eliasGammaEncode(n):
assert n > 0
bitLen = bitsOf(n)
binData = [False] * (bitLen - 1) # prefix
bit = 1 << (bitLen - 1)
while bit > 0:
binData += [bool(n & bit)]
bit >>= 1
binData += [False] * (-len(binData) % 8) # align by 8
return bitListToBin(binData)
def eliasGammaDecode(stream):
def readBits():
while True:
byte = ord(stream.read(1))
bitM = 1 << 7
while bitM > 0:
yield bool(byte & bitM)
bitM >>= 1
num = 0
state = 0
bitM = 1
for b in readBits():
if state == 0:
if not b:
bitM <<= 1
continue
state = 1
num += bitM * int(b)
bitM >>= 1
if bitM == 0: break
return num
def intToBin(x):
bitLen = bitsOf(x) if (x >= 0) else bitsOf(abs(x+1)) # two-complement
bitLen += 1 # for the sign
byteLen = (bitLen+7) / 8
bin = array("B", (0,)) * byteLen
if x < 0:
x += 1 << (byteLen * 8)
assert x > 0
for i in range(byteLen):
bin[byteLen-i-1] = (x >> (i * 8)) & 255
return bin
def binToInt(bin):
if isinstance(bin, str): bin = array("B", bin)
n = 0
byteLen = len(bin)
for i in range(byteLen):
n += bin[byteLen-i-1] << (i * 8)
if n >= 1 << (byteLen*8 - 1):
n -= 1 << (byteLen * 8)
return n
def intEncode(x):
bin = intToBin(x)
assert len(bin) > 0
gammaBin = eliasGammaEncode(len(bin))
return gammaBin + bin
def intDecode(stream):
if isinstance(stream, array): stream = stream.tostring()
if isinstance(stream, str): stream = StringIO(stream)
binLen = eliasGammaDecode(stream)
return binToInt(stream.read(binLen))
# Float numbers. Let's keep things simple but let's
# also cover a lot of cases.
# I use x = (numerator/denominator) * 2^exponent,
# where num/denom/exp are all integers.
# The binary representation just uses the Integer repr.
# If denom=0, with num>0 we get +inf, num=0 we get NaN,
# with num<0 we get -inf.
def floatEncode(x):
import math
from fractions import Fraction
from decimal import Decimal
if math.isnan(x): return intEncode(0) * 3
if math.isinf(x): return intEncode(math.copysign(1, x)) + intEncode(0) * 2
if isinstance(x, Decimal):
sign,digits,base10e = x.as_tuple()
e = 0
num = digits
denom = 10 ** -base10e
elif isinstance(x, Fraction):
e,num,denom = 0, x.numerator, x.denominator
else:
m,e = math.frexp(x)
num,denom = m.as_integer_ratio()
return intEncode(num) + intEncode(denom) + intEncode(e)
def floatDecode(stream):
if isinstance(stream, array): stream = stream.tostring()
if isinstance(stream, str): stream = StringIO(stream)
num,denom,e = intDecode(stream),intDecode(stream),intDecode(stream)
return (float(num)/denom) * (2 ** e)
# Strings. Just size + string.
# If this is a text, please let's all just stick to UTF8.
def strEncode(s):
if isinstance(s, str): s = array("B", s)
if isinstance(s, unicode): s = array("B", s.encode("utf-8"))
return intEncode(len(s)) + s
def strDecode(stream):
if isinstance(stream, array): stream = stream.tostring()
if isinstance(stream, str): stream = StringIO(stream)
strLen = intDecode(stream)
return stream.read(strLen)
# Lists. Amount of items, each item as variant.
def listEncode(l):
bin = intEncode(len(l))
for item in l:
bin += varEncode(item)
return bin
def listDecode(stream):
listLen = intDecode(stream)
l = [None]*listLen
for i in range(listLen):
l[i] = varDecode(stream)
return l
# Dicts. Amount of items, each item as 2 variants (key+value).
def dictEncode(d):
bin = intEncode(len(d))
for key,value in sorted(d.items()):
bin += varEncode(key)
bin += varEncode(value)
return bin
class Dict(dict):
def __getattr__(self, key):
try: return dict.__getitem__(self, key)
except KeyError: raise AttributeError
def __setattr__(self, key, value):
return dict.__setitem__(self, key, value)
def dictDecode(stream):
dictLen = intDecode(stream)
d = Dict()
for i in range(dictLen):
key = varDecode(stream)
value = varDecode(stream)
d[key] = value
return d
# Variants. Bytesize + type-ID-byte + data.
# Type-IDs:
# * 1: list
# * 2: dict
# * 3: bool
# * 4: int
# * 5: float
# * 6: str
# None has no type-ID. It is just bytesize=0.
def prefixWithSize(data):
return intEncode(len(data)) + data
def varEncode(v):
from numbers import Integral, Real
from collections import Mapping, Sequence
if v is None: return intEncode(0)
if isinstance(v, bool):
return prefixWithSize(array("B", (3,)) + boolEncode(v))
if isinstance(v, Integral):
return prefixWithSize(array("B", (4,)) + intEncode(v))
if isinstance(v, Real):
return prefixWithSize(array("B", (5,)) + floatEncode(v))
if isinstance(v, (str,unicode,array)):
return prefixWithSize(array("B", (6,)) + strEncode(v))
if isinstance(v, Mapping):
data = dictEncode(v)
typeEncoded = array("B", (2,))
lenEncoded = intEncode(len(data) + 1)
return lenEncoded + typeEncoded + data
if isinstance(v, Sequence):
data = listEncode(v)
typeEncoded = array("B", (1,))
lenEncoded = intEncode(len(data) + 1)
return lenEncoded + typeEncoded + data
assert False, "type of " + repr(v) + " cannot be encoded"
def varDecode(stream):
if isinstance(stream, array): stream = stream.tostring()
if isinstance(stream, str): stream = StringIO(stream)
varLen = intDecode(stream)
if varLen < 0: raise FormatError("varLen < 0")
if varLen == 0: return None
type = ord(stream.read(1))
if type == 1: return listDecode(stream)
if type == 2: return dictDecode(stream)
if type == 3: return boolDecode(stream)
if type == 4: return intDecode(stream)
if type == 5: return floatDecode(stream)
if type == 6: return strDecode(stream)
raise FormatError("type %i unknown" % type)
### Additional functions
# File IO
def write(file, v):
if isinstance(file, (str,unicode)): file = open(file, "wb")
file.write(FILESIGNATURE)
file.write(varEncode(v).tostring())
return file
def read(file):
if isinstance(file, (str,unicode)): file = open(file, "rb")
sig = file.read(len(FILESIGNATURE))
if sig != FILESIGNATURE: raise FormatError("file signature wrong")
return varDecode(file)
# Encryption / decryption. Authorization
def randomString(l):
import random
return ''.join(chr(random.randint(0, 0xFF)) for i in range(l))
def genkeypair():
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
pubkey = key.publickey().exportKey("DER")
privkey = key.exportKey("DER")
return (pubkey,privkey)
def encrypt(v, encrypt_rsapubkey=None, sign_rsaprivkey=None):
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import AES
from Crypto.Signature import PKCS1_PSS
from Crypto.Hash import SHA512
out = {}
if encrypt_rsapubkey:
encrypt_rsapubkey = RSA.importKey(encrypt_rsapubkey)
rsa = PKCS1_OAEP.new(encrypt_rsapubkey)
aeskey = randomString(32)
iv = randomString(16)
aes = AES.new(aeskey, AES.MODE_CBC, iv)
data = varEncode(v).tostring()
data += "\x00" * (-len(data) % 16)
out["aesInfo"] = rsa.encrypt(aeskey + iv)
out["data"] = aes.encrypt(data)
out["encrypted"] = True
else:
out["data"] = varEncode(v).tostring()
out["encrypted"] = False
if sign_rsaprivkey:
sign_rsaprivkey = RSA.importKey(sign_rsaprivkey)
pss = PKCS1_PSS.new(sign_rsaprivkey)
h = SHA512.new()
h.update(out["data"])
sign = pss.sign(h)
out["signature"] = sign
else:
out["signature"] = None
return out
def verifyData(data, sign, verifysign_rsapubkey):
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_PSS
from Crypto.Hash import SHA512
h = SHA512.new()
h.update(data)
verifysign_rsapubkey = RSA.importKey(verifysign_rsapubkey)
pss = PKCS1_PSS.new(verifysign_rsapubkey)
if not pss.verify(h, sign):
raise FormatError("signature is not authentic")
def decrypt(data, decrypt_rsaprivkey=None, verifysign_rsapubkey=None):
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import AES
if data["encrypted"]:
if not decrypt_rsaprivkey: raise FormatError("data is encrypted, key missing")
decrypt_rsaprivkey = RSA.importKey(decrypt_rsaprivkey)
rsa = PKCS1_OAEP.new(decrypt_rsaprivkey)
aesdata = rsa.decrypt(data["aesInfo"])
aeskey = aesdata[0:32]
iv = aesdata[32:]
aes = AES.new(aeskey, AES.MODE_CBC, iv)
outdata = aes.decrypt(data["data"])
else:
outdata = data["data"]
if verifysign_rsapubkey:
sign = data["signature"]
if not sign: raise FormatError("signature missing")
verifyData(data["data"], sign, verifysign_rsapubkey)
return varDecode(outdata)
def writeEncrypt(file, v, encrypt_rsapubkey=None, sign_rsaprivkey=None):
if isinstance(file, (str,unicode)): file = open(file, "wb")
file.write(FILESIGNATURE_CRYPTED)
file.write(varEncode(encrypt(v, encrypt_rsapubkey, sign_rsaprivkey)).tostring())
return file
def readDecrypt(file, decrypt_rsaprivkey=None, verifysign_rsapubkey=None):
if isinstance(file, (str,unicode)): file = open(file, "rb")
sig = file.read(len(FILESIGNATURE_CRYPTED))
if sig != FILESIGNATURE_CRYPTED: raise FormatError("file signature wrong")
return decrypt(varDecode(file), decrypt_rsaprivkey, verifysign_rsapubkey)
def verifyFile(file, verifysign_rsapubkey):
if isinstance(file, (str,unicode)): file = open(file, "rb")
sig = file.read(len(FILESIGNATURE_CRYPTED))
if sig != FILESIGNATURE_CRYPTED: raise FormatError("file signature wrong")
data = varDecode(file)
sign = data["signature"]
if not sign: raise FormatError("signature missing")
verifyData(data["data"], sign, verifysign_rsapubkey)
# Some tests.
def test_crypto():
v = {"hello":"world", 1:False, 42:-2**1024, "foo":None, "bar":[0.5,1,None,1.345,[]]}
pub1,priv1 = genkeypair()
pub2,priv2 = genkeypair()
pub3,priv3 = genkeypair()
encrypted_signed = encrypt(v, pub1, priv2)
decrypted1 = decrypt(encrypted_signed, priv1)
decrypted2 = decrypt(encrypted_signed, priv1, pub2)
assert v == decrypted1, repr(v) + " != " + repr(decrypted1)
assert v == decrypted2, repr(v) + " != " + repr(decrypted2)
try:
decrypt(encrypted_signed, priv1, pub3)
assert False, "signature wrongly assumed authentic (1)"
except FormatError: pass
just_signed = encrypt(v, sign_rsaprivkey=priv1)
decrypted1 = decrypt(just_signed, priv2)
decrypted2 = decrypt(just_signed, priv2, pub1)
assert v == decrypted1, repr(v) + " != " + repr(decrypted1)
assert v == decrypted2, repr(v) + " != " + repr(decrypted2)
try:
decrypt(encrypted_signed, priv1, pub3)
assert False, "signature wrongly assumed authentic (2)"
except FormatError: pass
def test():
# bitsOf
for (arg, res) in [(0,0), (255,8), (256,9), (1<<1000-1,1000)]:
assert bitsOf(arg) == res
# bitListToInt
for (arg, res) in [([1,0,0,0],8)]:
assert bitListToInt(arg) == res
# eliasGammaEncode + decode
for (value, raw) in [
(1, '\x80'),
(255, '\x01\xfe'),
(127, '\x03\xf8'),
(16, '\x08\x00'),
(8, '\x10'),
(15, '\x1e')
]:
assert eliasGammaEncode(value).tostring() == raw
assert eliasGammaDecode(StringIO(raw)) == value
# varEncode + decode
for (value, raw) in [
(42, '\x80\x03\x04\x80*'),
("hello", '\x80\x08\x06\x80\x05hello'),
([1,False,-1,0.5,"hi",{2:3}],
'\x800\x01\x80\x06\x80\x03\x04\x80\x01\x80\x02' +
'\x03\x00\x80\x03\x04\x80\xff\x80\x07\x05\x80' +
'\x01\x80\x02\x80\x00\x80\x05\x06\x80\x02hi\x80' +
'\r\x02\x80\x01\x80\x03\x04\x80\x02\x80\x03\x04\x80\x03'),
]:
assert varEncode(value).tostring() == raw
assert varDecode(StringIO(raw)) == value
# Some RPython tests.
# For RPython lang def, see: http://doc.pypy.org/en/latest/coding-guide.html#rpython-definition
def main(argv):
print "Hello!"
print "args:", argv
print "%r" % varEncode(argv)
test()
print "Bye!"
def target(driver, args):
"""
Target function for RPython.
"""
return main, None
if __name__ == '__main__':
test()
print "tests passed"