/usr/share/pyca/pylib/charset.py is in pyca 20031119-0.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | """
charset.py - Module for converting characters sets
(c) by Michael Stroeder <michael@stroeder.com>
This module is distributed under the terms of the
GPL (GNU GENERAL PUBLIC LICENSE) Version 2
(see http://www.gnu.org/copyleft/gpl.html)
"""
__version__ = '0.4.1'
import sys, string
# Alphabet for encrypted passwords (see module crypt)
crypt_alphabet = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
crypt_alphabet_len = len(crypt_alphabet)
def is_ascii(s):
"""
returns 1 if s is plain ASCII
"""
if s:
pos=0 ; s_len = len(s)
while ((ord(s[pos]) & 0x80) == 0) and (pos<s_len-1):
pos=pos+1
if pos<s_len-1:
return 0
else:
return (ord(s[pos]) & 0x80) == 0
else:
return 1
def escapeHTML(s,escape_html_chars='&;<>":={}()'):
"""
Escape all characters with a special meaning in HTML
to appropriate character tags
"""
result = ''; escape_html_chars_list = list(escape_html_chars)
for c in s:
if c in escape_html_chars:
result=result+'&#%d;'%ord(c)
else:
result=result+c
return result
def iso2utf(s):
"""
Convert ISO-8859-1 to UTF-8 encoded Unicode
"""
new = ''
for ch in s:
c=ord(ch)
if (c & 0x80) == 0:
new = new+ch
else:
new = new+chr(0xC0 | (0x03 & (c >> 6)))+chr(0x80 | (0x3F & c))
return new
UTF8len= ( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6 )
UTF8mask = (0x3F,0x7F,0x1F,0x0F,0x07,0x03,0x01)
def utf2iso(s):
"""
Convert UTF-8 encoded Unicode to ISO-8859-1
"""
new = ''
ind = 0
slen = len(s)
while ind < slen:
c=ord(s[ind])
ind = ind+1
clen = UTF8len[(c >> 2) & 0x3F]
u = c & UTF8mask[clen]
if clen==0:
clen = 4
else:
clen = clen-1
while clen and ind < slen:
c=ord(s[ind])
ind = ind+1
if (c & 0xC0) == 0x80:
u = (u << 6) | (c & 0x3F)
else:
ind = ind-1
break
clen = clen-1
if (u <= 0xFF):
new = new+chr(u)
else:
new = new+'?'
return new
def utf2html4(s):
"""
Convert UTF-8 encoded Unicode to HTML-4 character representation
"""
new = ''
ind = 0
slen = len(s)
while ind < slen:
c=ord(s[ind])
ind = ind+1
clen = UTF8len[(c >> 2) & 0x3F]
u = c & UTF8mask[clen]
if clen==0:
clen = 4
else:
clen = clen-1
while clen and ind < slen:
c=ord(s[ind])
ind = ind+1
if (c & 0xC0) == 0x80:
u = (u << 6) | (c & 0x3F)
else:
ind = ind-1
break
clen = clen-1
if u<128:
new = new + chr(u)
else:
new = new + '&#%d;' % u
return new
def iso2html4(s):
"""
Convert ISO-8859-1 to HTML-4 character representation
"""
new = ''
for ch in s:
c=ord(ch)
if (c & 0x80)==0:
new = new + ch
else:
new = new + '&#%d;' % (c)
return new
def iso2t61(s):
"""
Convert ISO-8859-1 to T.61 character representation
"""
new = ''
for ch in s:
c=ord(ch)
if (c & 0x80) == 0:
new = new+ch
else:
new = '%s\\x%X' % (new,ord(ch))
return new
def t612iso(s):
"""
Convert T.61 character representation to ISO-8859-1
"""
new = ''
slashpos = string.find(s,'\\x')
while slashpos!=-1:
if (s[slashpos]==0) or (s[slashpos]>0 and s[slashpos-1]!='\\'):
new = new+s[0:slashpos]+chr(string.atoi(s[slashpos+2:slashpos+4],16))
s = s[slashpos+4:]
else:
new = new+s[0:slashpos-1]
s = s[slashpos+1:]
slashpos = string.find(s,'\\x')
return new+s
def t612html4(s):
"""
Convert T.61 character representation to HTML-4 character representation
"""
new = ''
slashpos = string.find(s,'\\x')
while slashpos!=-1:
if (s[slashpos]==0) or (s[slashpos]>0 and s[slashpos-1]!='\\'):
new = new+s[0:slashpos]+'&#%d;' % string.atoi(s[slashpos+2:slashpos+4],16)
s = s[slashpos+4:]
else:
new = new+s[0:slashpos-1]
s = s[slashpos+1:]
slashpos = string.find(s,'\\x')
return new+s
def iso2asn1(s):
"""
Convert ISO-8859-1 to BMPString
"""
new = ''
for ch in s:
c=ord(ch)
if (c & 0x80) == 0:
new = '%s\\x00%s' % (new,ch)
else:
new = '%s\\x00\\x%X%s' % (new,ord(ch),ch)
return new
def asn12iso(s):
"""
Convert BMPString to ISO-8859-1
"""
return t612iso(string.replace(s,'\\x00',''))
def asn12html4(s):
"""
Convert BMPString to HTML-4 character representation
"""
return t612html4(string.replace(s,'\\x00',''))
recode_func = {
'ISO-8859-1': {
'UTF-8' : iso2utf,
'HTML4' : iso2html4
},
'ISO-8859-15': {
'UTF-8' : iso2utf,
'HTML4' : iso2html4
},
'UTF-8': {
'HTML4' : utf2html4,
'ISO-8859-1' : utf2iso
}
}
def recode(s,source,target):
"""
Convert from/to known character set / encoding
"""
if source==target:
return s
return recode_func[string.upper(source)][string.upper(target)](s)
|