1
- from pypy .interpreter .error import OperationError
2
- from pypy .interpreter .gateway import unwrap_spec
3
- from rpython .rlib .rstring import StringBuilder
4
- from pypy .module .binascii .interp_binascii import raise_Error
5
- from rpython .rlib .rarithmetic import ovfcheck
1
+ from ubinascii import hexlify
2
+
3
+ def unhexlify (data ):
4
+ if len (data ) % 2 != 0 :
5
+ raise Exception ("Odd-length string" )
6
+
7
+ return b'' .join ([ int (data [i :i + 2 ], 16 ).to_bytes (1 ) for i in range (0 , len (data ), 2 ) ])
8
+
9
+ b2a_hex = hexlify
10
+ a2b_hex = unhexlify
6
11
7
12
# ____________________________________________________________
8
13
@@ -34,18 +39,17 @@ def _transform(n):
34
39
table_a2b_base64 = '' .join (map (_transform , table_a2b_base64 ))
35
40
assert len (table_a2b_base64 ) == 256
36
41
37
-
38
- @unwrap_spec (ascii = 'bufferstr' )
39
- def a2b_base64 (space , ascii ):
42
+ def a2b_base64 (ascii ):
40
43
"Decode a line of base64 data."
41
44
42
- res = StringBuilder (( len ( ascii ) // 4 ) * 3 ) # maximum estimate
45
+ res = []
43
46
quad_pos = 0
44
47
leftchar = 0
45
48
leftbits = 0
46
49
last_char_was_a_pad = False
47
50
48
51
for c in ascii :
52
+ c = chr (c )
49
53
if c == PAD :
50
54
if quad_pos > 2 or (quad_pos == 2 and last_char_was_a_pad ):
51
55
break # stop on 'xxx=' or on 'xx=='
@@ -63,38 +67,33 @@ def a2b_base64(space, ascii):
63
67
#
64
68
if leftbits >= 8 :
65
69
leftbits -= 8
66
- res .append (chr (leftchar >> leftbits ))
70
+ res .append ((leftchar >> leftbits ). to_bytes ( 1 ))
67
71
leftchar &= ((1 << leftbits ) - 1 )
68
72
#
69
73
last_char_was_a_pad = False
70
74
else :
71
75
if leftbits != 0 :
72
- raise_Error ( space , "Incorrect padding" )
76
+ raise Exception ( "Incorrect padding" )
73
77
74
- return space . wrapbytes (res . build () )
78
+ return b'' . join (res )
75
79
76
80
# ____________________________________________________________
77
81
78
82
table_b2a_base64 = (
79
83
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" )
80
84
81
- @unwrap_spec (bin = 'bufferstr' )
82
- def b2a_base64 (space , bin ):
85
+ def b2a_base64 (bin ):
83
86
"Base64-code line of data."
84
87
85
88
newlength = (len (bin ) + 2 ) // 3
86
- try :
87
- newlength = ovfcheck (newlength * 4 )
88
- except OverflowError :
89
- raise OperationError (space .w_MemoryError , space .w_None )
90
- newlength += 1
91
- res = StringBuilder (newlength )
89
+ newlength = newlength * 4 + 1
90
+ res = []
92
91
93
92
leftchar = 0
94
93
leftbits = 0
95
94
for c in bin :
96
95
# Shift into our buffer, and output any 6bits ready
97
- leftchar = (leftchar << 8 ) | ord ( c )
96
+ leftchar = (leftchar << 8 ) | c
98
97
leftbits += 8
99
98
res .append (table_b2a_base64 [(leftchar >> (leftbits - 6 )) & 0x3f ])
100
99
leftbits -= 6
@@ -110,4 +109,4 @@ def b2a_base64(space, bin):
110
109
res .append (table_b2a_base64 [(leftchar & 0xf ) << 2 ])
111
110
res .append (PAD )
112
111
res .append ('\n ' )
113
- return space . wrapbytes (res . build () )
112
+ return '' . join (res ). encode ( 'ascii' )
0 commit comments