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
22 LANG_UNSPECIFIED = 0xf
25 GSM_DEFAULT_ALPHABET = [
166 Decodes the given string using the given cell broadcast data coding scheme.
168 @param s: string to decode
169 @param n: GSM cell broadcast data coding scheme
170 @return: UTF-8 string
173 # separate into nibbles
174 hbits = (n & 0xf0) >> 4
179 return _decode_language(s, lbits)
181 elif (0x1 <= hbits <= 0x3):
185 elif (0x4 <= hbits <= 0x7):
186 # general data coding indication
187 return _decode_general_data_coding(s, hbits, lbits)
189 elif (0x8 <= hbits <= 0xe):
190 # reserved coding group
194 # data coding / message handling
198 def _decode_language(s, lang):
200 return _decode_default_alphabet(s)
203 def _decode_default_alphabet(s):
205 # TODO: we really might have to do 7 bit character unpacking here
207 # ought to be all in the 7 bit GSM character map
208 chars = [ GSM_DEFAULT_ALPHABET[ord(c)] for c in s ]
209 u_str = "".join(chars)
210 return u_str.encode("utf-8")
215 return s.decode("hex")
220 return s.decode("hex").decode("utf-16-be").encode("utf-8")
223 def _decode_general_data_coding(s, h, l):
225 is_compressed = (h & 0x2)
227 alphabet = (l & 0xc) >> 2
229 if (alphabet == 0x0):
231 return _decode_defaul_alphabet(s)
233 elif (alphabet == 0x1):
235 # actually, encoding is user-defined, but let's assume hex'd ASCII
237 return _decode_hex(s)
239 elif (alphabet == 0x2):
241 return _decode_usc2(s)
242 elif (alphabet == 0x3):