* fix possible Enigma2 crash while decoding thumbnails
[enigma2-plugins.git] / mytube / src / __init__.py
1 # -*- coding: ISO-8859-1 -*-
2 from Components.Language import language
3 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_LANGUAGE
4 import os,gettext
5 import sha
6 PluginLanguageDomain = "MyTube"
7 PluginLanguagePath = "Extensions/MyTube/po"
8
9 def localeInit():
10         lang = language.getLanguage()[:2] # getLanguage returns e.g. "fi_FI" for "language_country"
11         os.environ["LANGUAGE"] = lang # Enigma doesn't set this (or LC_ALL, LC_MESSAGES, LANG). gettext needs it!
12         print "[MyTube] set language to ", lang
13         gettext.bindtextdomain(PluginLanguageDomain, resolveFilename(SCOPE_PLUGINS, PluginLanguagePath))
14
15 def _(txt):
16         t = gettext.dgettext(PluginLanguageDomain, txt)
17         if t == txt:
18                 #print "[MyTube] fallback to default translation for", txt
19                 t = gettext.gettext(txt)
20         return t
21
22 def bin2long(s):
23         return reduce( lambda x,y:(x<<8L)+y, map(ord, s))
24
25 def long2bin(l):
26         res = ""
27         for byte in range(128):
28                 res += chr((l >> (1024 - (byte + 1) * 8)) & 0xff)
29         return res
30
31 def rsa_pub1024(src, mod):
32         return long2bin(pow(bin2long(src), 65537, bin2long(mod)))
33         
34 def decrypt_block(src, mod):
35         if len(src) != 128 and len(src) != 202:
36                 return None
37         dest = rsa_pub1024(src[:128], mod)
38         hash = sha.new(dest[1:107])
39         if len(src) == 202:
40                 hash.update(src[131:192])       
41         result = hash.digest()
42         if result == dest[107:127]:
43                 return dest
44         return None
45
46 #localeInit()
47 #language.addCallback(localeInit)