|
|
|
@ -1,4 +1,9 @@
|
|
|
|
|
#!/usr/bin/python2.7
|
|
|
|
|
"""
|
|
|
|
|
Message encoding end decoding functions
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import string
|
|
|
|
|
import zlib
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import msgpack
|
|
|
|
@ -7,14 +12,11 @@ except ImportError:
|
|
|
|
|
import umsgpack as msgpack
|
|
|
|
|
except ImportError:
|
|
|
|
|
import fallback.umsgpack.umsgpack as msgpack
|
|
|
|
|
import string
|
|
|
|
|
import zlib
|
|
|
|
|
|
|
|
|
|
import messagetypes
|
|
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
|
from debug import logger
|
|
|
|
|
import messagetypes
|
|
|
|
|
from tr import _translate
|
|
|
|
|
import helper_random
|
|
|
|
|
|
|
|
|
|
BITMESSAGE_ENCODING_IGNORE = 0
|
|
|
|
|
BITMESSAGE_ENCODING_TRIVIAL = 1
|
|
|
|
@ -62,7 +64,7 @@ class MsgEncode(object):
|
|
|
|
|
self.length = len(self.data)
|
|
|
|
|
|
|
|
|
|
def encodeSimple(self, message):
|
|
|
|
|
self.data = 'Subject:' + message['subject'] + '\n' + 'Body:' + message['body']
|
|
|
|
|
self.data = 'Subject:%(subject)s\nBody:%(body)s' % message
|
|
|
|
|
self.length = len(self.data)
|
|
|
|
|
|
|
|
|
|
def encodeTrivial(self, message):
|
|
|
|
@ -75,10 +77,14 @@ class MsgDecode(object):
|
|
|
|
|
self.encoding = encoding
|
|
|
|
|
if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
|
|
|
|
|
self.decodeExtended(data)
|
|
|
|
|
elif self.encoding in [BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL]:
|
|
|
|
|
elif self.encoding in (
|
|
|
|
|
BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL):
|
|
|
|
|
self.decodeSimple(data)
|
|
|
|
|
else:
|
|
|
|
|
self.body = _translate("MsgDecode", "The message has an unknown encoding.\nPerhaps you should upgrade Bitmessage.")
|
|
|
|
|
self.body = _translate(
|
|
|
|
|
"MsgDecode",
|
|
|
|
|
"The message has an unknown encoding.\n"
|
|
|
|
|
"Perhaps you should upgrade Bitmessage.")
|
|
|
|
|
self.subject = _translate("MsgDecode", "Unknown encoding")
|
|
|
|
|
|
|
|
|
|
def decodeExtended(self, data):
|
|
|
|
@ -86,7 +92,9 @@ class MsgDecode(object):
|
|
|
|
|
tmp = ""
|
|
|
|
|
while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"):
|
|
|
|
|
try:
|
|
|
|
|
got = dc.decompress(data, BMConfigParser().safeGetInt("zlib", "maxsize") + 1 - len(tmp))
|
|
|
|
|
got = dc.decompress(
|
|
|
|
|
data, BMConfigParser().safeGetInt("zlib", "maxsize") +
|
|
|
|
|
1 - len(tmp))
|
|
|
|
|
# EOF
|
|
|
|
|
if got == "":
|
|
|
|
|
break
|
|
|
|
@ -138,23 +146,3 @@ class MsgDecode(object):
|
|
|
|
|
subject = subject.splitlines()[0]
|
|
|
|
|
self.subject = subject
|
|
|
|
|
self.body = body
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
messageData = {
|
|
|
|
|
"subject": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(40)),
|
|
|
|
|
"body": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(10000))
|
|
|
|
|
}
|
|
|
|
|
obj1 = MsgEncode(messageData, 1)
|
|
|
|
|
obj2 = MsgEncode(messageData, 2)
|
|
|
|
|
obj3 = MsgEncode(messageData, 3)
|
|
|
|
|
print "1:%i 2:%i 3:%i" %(len(obj1.data), len(obj2.data), len(obj3.data))
|
|
|
|
|
|
|
|
|
|
obj1e = MsgDecode(1, obj1.data)
|
|
|
|
|
# no subject in trivial encoding
|
|
|
|
|
assert messageData["body"] == obj1e.body
|
|
|
|
|
obj2e = MsgDecode(2, obj2.data)
|
|
|
|
|
assert messageData["subject"] == obj2e.subject
|
|
|
|
|
assert messageData["body"] == obj2e.body
|
|
|
|
|
obj3e = MsgDecode(3, obj3.data)
|
|
|
|
|
assert messageData["subject"] == obj3e.subject
|
|
|
|
|
assert messageData["body"] == obj3e.body
|
|
|
|
|