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) 2010
23 LANG_UNSPECIFIED = 0xf
26 GSM_DEFAULT_ALPHABET = [
167 Decodes the given string using the given cell broadcast data coding scheme.
169 @param s: string to decode
170 @param n: GSM cell broadcast data coding scheme
171 @return: UTF-8 string
174 # separate into nibbles
175 hbits = (n & 0xf0) >> 4
180 return _decode_language(s, lbits)
182 elif (0x1 <= hbits <= 0x3):
186 elif (0x4 <= hbits <= 0x7):
187 # general data coding indication
188 return _decode_general_data_coding(s, hbits, lbits)
190 elif (0x8 <= hbits <= 0xe):
191 # reserved coding group
195 # data coding / message handling
199 def _decode_language(s, lang):
201 return _decode_default_alphabet(s)
204 def _decode_default_alphabet(s):
206 # TODO: we really might have to do 7 bit character unpacking here
208 # ought to be all in the 7 bit GSM character map
209 chars = [ GSM_DEFAULT_ALPHABET[ord(c)] for c in s ]
210 u_str = "".join(chars)
211 return u_str.encode("utf-8")
216 return s.decode("hex")
221 return s.decode("hex").decode("utf-16-be").encode("utf-8")
224 def _decode_general_data_coding(s, h, l):
226 is_compressed = (h & 0x2)
228 alphabet = (l & 0xc) >> 2
230 if (alphabet == 0x0):
232 return _decode_defaul_alphabet(s)
234 elif (alphabet == 0x1):
236 # actually, encoding is user-defined, but let's assume hex'd ASCII
238 return _decode_hex(s)
240 elif (alphabet == 0x2):
242 return _decode_usc2(s)
244 elif (alphabet == 0x3):