jpayne@68: """ Codec for the Punicode encoding, as specified in RFC 3492 jpayne@68: jpayne@68: Written by Martin v. Löwis. jpayne@68: """ jpayne@68: jpayne@68: import codecs jpayne@68: jpayne@68: ##################### Encoding ##################################### jpayne@68: jpayne@68: def segregate(str): jpayne@68: """3.1 Basic code point segregation""" jpayne@68: base = bytearray() jpayne@68: extended = set() jpayne@68: for c in str: jpayne@68: if ord(c) < 128: jpayne@68: base.append(ord(c)) jpayne@68: else: jpayne@68: extended.add(c) jpayne@68: extended = sorted(extended) jpayne@68: return bytes(base), extended jpayne@68: jpayne@68: def selective_len(str, max): jpayne@68: """Return the length of str, considering only characters below max.""" jpayne@68: res = 0 jpayne@68: for c in str: jpayne@68: if ord(c) < max: jpayne@68: res += 1 jpayne@68: return res jpayne@68: jpayne@68: def selective_find(str, char, index, pos): jpayne@68: """Return a pair (index, pos), indicating the next occurrence of jpayne@68: char in str. index is the position of the character considering jpayne@68: only ordinals up to and including char, and pos is the position in jpayne@68: the full string. index/pos is the starting position in the full jpayne@68: string.""" jpayne@68: jpayne@68: l = len(str) jpayne@68: while 1: jpayne@68: pos += 1 jpayne@68: if pos == l: jpayne@68: return (-1, -1) jpayne@68: c = str[pos] jpayne@68: if c == char: jpayne@68: return index+1, pos jpayne@68: elif c < char: jpayne@68: index += 1 jpayne@68: jpayne@68: def insertion_unsort(str, extended): jpayne@68: """3.2 Insertion unsort coding""" jpayne@68: oldchar = 0x80 jpayne@68: result = [] jpayne@68: oldindex = -1 jpayne@68: for c in extended: jpayne@68: index = pos = -1 jpayne@68: char = ord(c) jpayne@68: curlen = selective_len(str, char) jpayne@68: delta = (curlen+1) * (char - oldchar) jpayne@68: while 1: jpayne@68: index,pos = selective_find(str,c,index,pos) jpayne@68: if index == -1: jpayne@68: break jpayne@68: delta += index - oldindex jpayne@68: result.append(delta-1) jpayne@68: oldindex = index jpayne@68: delta = 0 jpayne@68: oldchar = char jpayne@68: jpayne@68: return result jpayne@68: jpayne@68: def T(j, bias): jpayne@68: # Punycode parameters: tmin = 1, tmax = 26, base = 36 jpayne@68: res = 36 * (j + 1) - bias jpayne@68: if res < 1: return 1 jpayne@68: if res > 26: return 26 jpayne@68: return res jpayne@68: jpayne@68: digits = b"abcdefghijklmnopqrstuvwxyz0123456789" jpayne@68: def generate_generalized_integer(N, bias): jpayne@68: """3.3 Generalized variable-length integers""" jpayne@68: result = bytearray() jpayne@68: j = 0 jpayne@68: while 1: jpayne@68: t = T(j, bias) jpayne@68: if N < t: jpayne@68: result.append(digits[N]) jpayne@68: return bytes(result) jpayne@68: result.append(digits[t + ((N - t) % (36 - t))]) jpayne@68: N = (N - t) // (36 - t) jpayne@68: j += 1 jpayne@68: jpayne@68: def adapt(delta, first, numchars): jpayne@68: if first: jpayne@68: delta //= 700 jpayne@68: else: jpayne@68: delta //= 2 jpayne@68: delta += delta // numchars jpayne@68: # ((base - tmin) * tmax) // 2 == 455 jpayne@68: divisions = 0 jpayne@68: while delta > 455: jpayne@68: delta = delta // 35 # base - tmin jpayne@68: divisions += 36 jpayne@68: bias = divisions + (36 * delta // (delta + 38)) jpayne@68: return bias jpayne@68: jpayne@68: jpayne@68: def generate_integers(baselen, deltas): jpayne@68: """3.4 Bias adaptation""" jpayne@68: # Punycode parameters: initial bias = 72, damp = 700, skew = 38 jpayne@68: result = bytearray() jpayne@68: bias = 72 jpayne@68: for points, delta in enumerate(deltas): jpayne@68: s = generate_generalized_integer(delta, bias) jpayne@68: result.extend(s) jpayne@68: bias = adapt(delta, points==0, baselen+points+1) jpayne@68: return bytes(result) jpayne@68: jpayne@68: def punycode_encode(text): jpayne@68: base, extended = segregate(text) jpayne@68: deltas = insertion_unsort(text, extended) jpayne@68: extended = generate_integers(len(base), deltas) jpayne@68: if base: jpayne@68: return base + b"-" + extended jpayne@68: return extended jpayne@68: jpayne@68: ##################### Decoding ##################################### jpayne@68: jpayne@68: def decode_generalized_number(extended, extpos, bias, errors): jpayne@68: """3.3 Generalized variable-length integers""" jpayne@68: result = 0 jpayne@68: w = 1 jpayne@68: j = 0 jpayne@68: while 1: jpayne@68: try: jpayne@68: char = ord(extended[extpos]) jpayne@68: except IndexError: jpayne@68: if errors == "strict": jpayne@68: raise UnicodeError("incomplete punicode string") jpayne@68: return extpos + 1, None jpayne@68: extpos += 1 jpayne@68: if 0x41 <= char <= 0x5A: # A-Z jpayne@68: digit = char - 0x41 jpayne@68: elif 0x30 <= char <= 0x39: jpayne@68: digit = char - 22 # 0x30-26 jpayne@68: elif errors == "strict": jpayne@68: raise UnicodeError("Invalid extended code point '%s'" jpayne@68: % extended[extpos]) jpayne@68: else: jpayne@68: return extpos, None jpayne@68: t = T(j, bias) jpayne@68: result += digit * w jpayne@68: if digit < t: jpayne@68: return extpos, result jpayne@68: w = w * (36 - t) jpayne@68: j += 1 jpayne@68: jpayne@68: jpayne@68: def insertion_sort(base, extended, errors): jpayne@68: """3.2 Insertion unsort coding""" jpayne@68: char = 0x80 jpayne@68: pos = -1 jpayne@68: bias = 72 jpayne@68: extpos = 0 jpayne@68: while extpos < len(extended): jpayne@68: newpos, delta = decode_generalized_number(extended, extpos, jpayne@68: bias, errors) jpayne@68: if delta is None: jpayne@68: # There was an error in decoding. We can't continue because jpayne@68: # synchronization is lost. jpayne@68: return base jpayne@68: pos += delta+1 jpayne@68: char += pos // (len(base) + 1) jpayne@68: if char > 0x10FFFF: jpayne@68: if errors == "strict": jpayne@68: raise UnicodeError("Invalid character U+%x" % char) jpayne@68: char = ord('?') jpayne@68: pos = pos % (len(base) + 1) jpayne@68: base = base[:pos] + chr(char) + base[pos:] jpayne@68: bias = adapt(delta, (extpos == 0), len(base)) jpayne@68: extpos = newpos jpayne@68: return base jpayne@68: jpayne@68: def punycode_decode(text, errors): jpayne@68: if isinstance(text, str): jpayne@68: text = text.encode("ascii") jpayne@68: if isinstance(text, memoryview): jpayne@68: text = bytes(text) jpayne@68: pos = text.rfind(b"-") jpayne@68: if pos == -1: jpayne@68: base = "" jpayne@68: extended = str(text, "ascii").upper() jpayne@68: else: jpayne@68: base = str(text[:pos], "ascii", errors) jpayne@68: extended = str(text[pos+1:], "ascii").upper() jpayne@68: return insertion_sort(base, extended, errors) jpayne@68: jpayne@68: ### Codec APIs jpayne@68: jpayne@68: class Codec(codecs.Codec): jpayne@68: jpayne@68: def encode(self, input, errors='strict'): jpayne@68: res = punycode_encode(input) jpayne@68: return res, len(input) jpayne@68: jpayne@68: def decode(self, input, errors='strict'): jpayne@68: if errors not in ('strict', 'replace', 'ignore'): jpayne@68: raise UnicodeError("Unsupported error handling "+errors) jpayne@68: res = punycode_decode(input, errors) jpayne@68: return res, len(input) jpayne@68: jpayne@68: class IncrementalEncoder(codecs.IncrementalEncoder): jpayne@68: def encode(self, input, final=False): jpayne@68: return punycode_encode(input) jpayne@68: jpayne@68: class IncrementalDecoder(codecs.IncrementalDecoder): jpayne@68: def decode(self, input, final=False): jpayne@68: if self.errors not in ('strict', 'replace', 'ignore'): jpayne@68: raise UnicodeError("Unsupported error handling "+self.errors) jpayne@68: return punycode_decode(input, self.errors) jpayne@68: jpayne@68: class StreamWriter(Codec,codecs.StreamWriter): jpayne@68: pass jpayne@68: jpayne@68: class StreamReader(Codec,codecs.StreamReader): jpayne@68: pass jpayne@68: jpayne@68: ### encodings module API jpayne@68: jpayne@68: def getregentry(): jpayne@68: return codecs.CodecInfo( jpayne@68: name='punycode', jpayne@68: encode=Codec().encode, jpayne@68: decode=Codec().decode, jpayne@68: incrementalencoder=IncrementalEncoder, jpayne@68: incrementaldecoder=IncrementalDecoder, jpayne@68: streamwriter=StreamWriter, jpayne@68: streamreader=StreamReader, jpayne@68: )