def test_decode_corners(self): self.assertEqual(decode_base85(''), '') self.assertEqual(decode_base85('00000'), '\x00\x00\x00\x00') self.assertEqual(decode_base85('|NsC0'), '\xFF\xFF\xFF\xFF') # acc will be larger than 0xFFFFFFFF. Such input is invalid. self.assertRaises(ValueError, decode_base85, '|NsC1') self.assertRaises(ValueError, decode_base85, '~~~~~')
def parse(lines): """Creates a BinaryHunk instance starting with lines[0]. Returns a tuple of the BinaryHunk instance and unconsumed lines. """ match = re.match(r'(literal|delta) (\d+)', lines[0]) if not match: raise ValueError('No "literal <size>" or "delta <size>".') bin_type = match.group(1) size = int(match.group(2)) bin_data = '' lines = lines[1:] for i, line in enumerate(lines): if len(line) == 0: return (BinaryHunk(bin_type, size, bin_data), lines[i + 1:]) line_length_letter = line[0] # Map a letter to a number. # A-Z -> 1-26 # a-z -> 27-52 line_length = 1 + ord(line_length_letter) - ord('A') if line_length_letter >= 'a': line_length = 27 + ord(line_length_letter) - ord('a') if line_length * 5 > (len(line) - 1) * 4: raise ValueError('Base85 length mismatch: length by the first ' 'letter:{}, actual:{}, line:"{}"'.format( line_length * 5, (len(line) - 1) * 4, line)) bin_data += decode_base85(line[1:])[0:line_length] raise ValueError('No blank line terminating a binary hunk.')
def test_decode(self): if six.PY2: self.assertEqual(decode_base85('cmV?d00001'), 'x\x01\x03\x00\x00\x00\x00\x01')