Skip to content

Commit

Permalink
Handle failing to decode to utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelwastaken committed Sep 10, 2023
1 parent 8b11b4e commit 0da0ade
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,21 @@ def decode_sms_content(self, content: str) -> str:
str: ASCII decoded SMS content
"""
# Convert the content from hexadecimals to ASCII
# Also strip null bytes from the ASCII output
return bytes.fromhex(content).decode().replace('\x00', '')
try:
# Try to decode to UTF-8 first
decoded = bytes.fromhex(content).decode('utf-8').replace('\x00', '')
except UnicodeDecodeError:
try:
# Try to decode to latin-1 if UTF-8 failed
decoded = bytes.fromhex(content).decode('latin-1').replace('\x00', '')
except UnicodeDecodeError:
# Somehow failed to decode the SMS content
log.error(f'Failed to decode SMS content: {content}')
# Fallback and return the raw content
decoded = content

# Strip null bytes from the decoded output
return decoded.replace('\x00', '')

def decode_sms_timestamp(self, timestamp: str) -> datetime:
"""
Expand Down

0 comments on commit 0da0ade

Please sign in to comment.