Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions gsmmodem/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ def _decodeUserData(byteIter, userDataLen, dataCoding, udhPresent):
# Since we are using 7-bit data, "fill bits" may have been added to make the UDH end on a septet boundary
shift = ((udhLen + 1) * 8) % 7 # "fill bits" needed to make the UDH end on a septet boundary
# Simulate another "shift" in the unpackSeptets algorithm in order to ignore the fill bits
prevOctet = next(byteIter)
shift += 1
if shift:
prevOctet = next(byteIter)
shift += 1
else:
prevOctet = 0

if dataCoding == 0x00: # GSM-7
if udhPresent:
Expand Down Expand Up @@ -904,16 +907,14 @@ def unpackSeptets(septets, numberOfSeptets=None, prevOctet=None, shift=7):

def decodeUcs2(byteIter, numBytes):
""" Decodes UCS2-encoded text from the specified byte iterator, up to a maximum of numBytes """
userData = []
i = 0
userData = bytearray()
try:
while i < numBytes:
userData.append(unichr((next(byteIter) << 8) | next(byteIter)))
i += 2
for i in range(numBytes):
userData.append(next(byteIter))
except StopIteration:
# Not enough bytes in iterator to reach numBytes; return what we have
pass
return ''.join(userData)
return userData.decode('utf-16-be')

def encodeUcs2(text):
""" UCS2 text encoding algorithm
Expand All @@ -925,12 +926,7 @@ def encodeUcs2(text):
:return: A bytearray containing the string encoded in UCS2 encoding
:rtype: bytearray
"""
result = bytearray()

for b in map(ord, text):
result.append(b >> 8)
result.append(b & 0xFF)
return result
return text.encode('utf-16-be')

def divideTextUcs2(plainText):
""" UCS-2 message dividing algorithm
Expand Down