14 LANG_UNSPECIFIED = 0xf
17 GSM_DEFAULT_ALPHABET = [
158 Decodes the given string using the given cell broadcast data coding scheme.
160 @param s: string to decode
161 @param n: GSM cell broadcast data coding scheme
162 @return: UTF-8 string
165 # separate into nibbles
166 hbits = (n & 0xf0) >> 4
171 return _decode_language(s, lbits)
173 elif (0x1 <= hbits <= 0x3):
177 elif (0x4 <= hbits <= 0x7):
178 # general data coding indication
179 return _decode_general_data_coding(s, hbits, lbits)
181 elif (0x8 <= hbits <= 0xe):
182 # reserved coding group
186 # data coding / message handling
190 def _decode_language(s, lang):
192 return _decode_default_alphabet(s)
195 def _decode_default_alphabet(s):
197 # TODO: we really might have to do 7 bit character unpacking here
199 # ought to be all in the 7 bit GSM character map
200 chars = [ GSM_DEFAULT_ALPHABET[ord(c)] for c in s ]
201 u_str = "".join(chars)
202 return u_str.encode("utf-8")
207 return s.decode("hex")
212 return s.decode("hex").decode("utf-16-be").encode("utf-8")
215 def _decode_general_data_coding(s, h, l):
217 is_compressed = (h & 0x2)
219 alphabet = (l & 0xc) >> 2
222 return _decode_defaul_alphabet(s)
226 # actually, encoding is user-defined, but let's assume hex'd ASCII
228 return _decode_hex(s)
232 return _decode_usc2(s)