mobile: fix SMS 7bit codec for fragmented message

This commit is contained in:
mich 2020-01-28 11:55:54 +01:00
parent 1f632c1d09
commit b9297332c3
1 changed files with 18 additions and 4 deletions

View File

@ -546,8 +546,15 @@ _GSM7bExtLUTInv = {
}
def encode_7b(txt):
def encode_7b(txt, off=0):
"""translates the unicode string `txt' to a GSM 7 bit characters buffer
Args:
txt (utf8 str): text string to encode
off (uint): bit offset, set to 1 for fragmented text, otherwise 0
Returns:
encoded buffer and septet count (bytes, uint)
"""
arr, cnt = [], 0
for c in reversed(txt):
@ -565,7 +572,7 @@ def encode_7b(txt):
else:
cnt += 1
# check the length in bits and add padding bits
pad = (8-(7*len(arr))%8)%8
pad = ((8-(7*len(arr))%8)%8)-off
arr.insert(0, (TYPE_UINT, 0, pad))
if python_version < 3:
return ''.join(reversed(pack_val(*arr)[0])), cnt
@ -573,8 +580,15 @@ def encode_7b(txt):
return bytes(reversed(pack_val(*arr)[0])), cnt
def decode_7b(buf):
def decode_7b(buf, off=0):
"""translates the GSM 7 bit characters buffer `buf' to an unicode string
Args:
buf (bytes): buffer to decode
off (uint): bit offset, set to 1 for fragmented text, otherwise 0
Returns:
decoded text string (utf8 str)
"""
if python_version < 3:
char = Charpy(''.join(reversed(buf)))
@ -583,7 +597,7 @@ def decode_7b(buf):
# jump over the padding bits
# WNG: in case of 7 bits padding, we will have an @ at the end
chars_num = (8*len(buf)) // 7
char._cur = (8*len(buf))-(7*chars_num)
char._cur = (8*len(buf))-(7*chars_num)-off
# get all chars
arr = [char.get_uint(7) for i in range(chars_num)]
chars = []