def encode_int24(self, value): if value < 0 or value > 16777215: raise ValueError('value out of range') raw = [value & 63, value >> 6 & 63, value >> 12 & 63, value >> 18 & 63] if self.big: raw = reversed(raw) return join_byte_elems(imap(self._encode64, raw))
def _encode_int(self, value, bits): pad = -bits % 6 bits += pad if self.big: itr = irange(bits - 6, -6, -6) value <<= pad else: itr = irange(0, bits, 6) return join_byte_elems( imap(self._encode64, (value >> off & 63 for off in itr)))
def encode_bytes(self, source): if not isinstance(source, bytes): raise TypeError('source must be bytes, not %s' % (type(source), )) chunks, tail = divmod(len(source), 3) if PY3: next_value = nextgetter(iter(source)) else: next_value = nextgetter(ord(elem) for elem in source) gen = self._encode_bytes(next_value, chunks, tail) out = join_byte_elems(imap(self._encode64, gen)) return out
def decode_bytes(self, source): if not isinstance(source, bytes): raise TypeError('source must be bytes, not %s' % (type(source), )) chunks, tail = divmod(len(source), 4) if tail == 1: raise ValueError('input string length cannot be == 1 mod 4') next_value = nextgetter(imap(self._decode64, source)) try: return join_byte_values( self._decode_bytes(next_value, chunks, tail)) except KeyError as err: raise ValueError('invalid character: %r' % (err.args[0], ))