~ubuntu-branches/ubuntu/karmic/pyca/karmic

« back to all changes in this revision

Viewing changes to pylib/charset.py

  • Committer: Bazaar Package Importer
  • Author(s): Lars Bahner
  • Date: 2003-12-02 19:39:35 UTC
  • Revision ID: james.westby@ubuntu.com-20031202193935-fzzt289mntvy6a8q
Tags: upstream-20031118
ImportĀ upstreamĀ versionĀ 20031118

Show diffs side-by-side

added added

removed removed

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