2 # -*- coding: utf-8 -*-
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published
5 ## by the Free Software Foundation; version 2 and higer.
7 ## Martin Grimme (martin.grimme # gmail.com) 2010LANG_DE = 0x0
21 LANG_UNSPECIFIED = 0xf
24 GSM_DEFAULT_ALPHABET = [
165 Decodes the given string using the given cell broadcast data coding scheme.
167 @param s: string to decode
168 @param n: GSM cell broadcast data coding scheme
169 @return: UTF-8 string
172 # separate into nibbles
173 hbits = (n & 0xf0) >> 4
178 return _decode_language(s, lbits)
180 elif (0x1 <= hbits <= 0x3):
184 elif (0x4 <= hbits <= 0x7):
185 # general data coding indication
186 return _decode_general_data_coding(s, hbits, lbits)
188 elif (0x8 <= hbits <= 0xe):
189 # reserved coding group
193 # data coding / message handling
197 def _decode_language(s, lang):
199 return _decode_default_alphabet(s)
202 def _decode_default_alphabet(s):
204 # TODO: we really might have to do 7 bit character unpacking here
206 # ought to be all in the 7 bit GSM character map
207 chars = [ GSM_DEFAULT_ALPHABET[ord(c)] for c in s ]
208 u_str = "".join(chars)
209 return u_str.encode("utf-8")
214 return s.decode("hex")
219 return s.decode("hex").decode("utf-16-be").encode("utf-8")
222 def _decode_general_data_coding(s, h, l):
224 is_compressed = (h & 0x2)
226 alphabet = (l & 0xc) >> 2
228 if (alphabet == 0x0):
230 return _decode_defaul_alphabet(s)
232 elif (alphabet == 0x1):
234 # actually, encoding is user-defined, but let's assume hex'd ASCII
236 return _decode_hex(s)
238 elif (alphabet == 0x2):
240 return _decode_usc2(s)
241 elif (alphabet == 0x3):