| 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 pypy.module.binascii.interp_binascii import AsciiBufferUnwrapper |
| 6 |
from rpython.rlib.rarithmetic import ovfcheck |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
PAD = '=' |
| 11 |
|
| 12 |
table_a2b_base64 = [ |
| 13 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 14 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 15 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, |
| 16 |
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, |
| 17 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, |
| 18 |
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, |
| 19 |
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, |
| 20 |
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1, |
| 21 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 22 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 23 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 24 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 25 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 26 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 27 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 28 |
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 29 |
] |
| 30 |
def _transform(n): |
| 31 |
if n == -1: |
| 32 |
return '\xff' |
| 33 |
else: |
| 34 |
return chr(n) |
| 35 |
table_a2b_base64 = ''.join(map(_transform, table_a2b_base64)) |
| 36 |
assert len(table_a2b_base64) == 256 |
| 37 |
|
| 38 |
@unwrap_spec(ascii=AsciiBufferUnwrapper, strict_mode=int) |
| 39 |
def a2b_base64(space, ascii, strict_mode=0): |
| 40 |
"Decode a line of base64 data." |
| 41 |
|
| 42 |
res = StringBuilder((len(ascii) // 4) * 3) |
| 43 |
quad_pos = 0 |
| 44 |
leftchar = 0 |
| 45 |
leftbits = 0 |
| 46 |
last_char_was_a_pad = False |
| 47 |
bin_used = 0 |
| 48 |
|
| 49 |
if strict_mode and len(ascii) > 0 and ascii[0] == '=': |
| 50 |
raise_Error(space, "Leading padding not allowed") |
| 51 |
i = 0 |
| 52 |
for c in ascii: |
| 53 |
if c == PAD: |
| 54 |
if quad_pos > 2 or (quad_pos == 2 and last_char_was_a_pad): |
| 55 |
if strict_mode and i + 1 < len(ascii): |
| 56 |
raise_Error(space, "Excess data after padding") |
| 57 |
break |
| 58 |
last_char_was_a_pad = True |
| 59 |
else: |
| 60 |
n = ord(table_a2b_base64[ord(c)]) |
| 61 |
if n == 0xff: |
| 62 |
if strict_mode: |
| 63 |
raise_Error(space, "Only base64 data is allowed") |
| 64 |
continue |
| 65 |
if strict_mode and last_char_was_a_pad: |
| 66 |
raise_Error(space, "Discontinuous padding not allowed") |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
quad_pos = (quad_pos + 1) & 3 |
| 71 |
leftchar = (leftchar << 6) | n |
| 72 |
leftbits += 6 |
| 73 |
|
| 74 |
if leftbits >= 8: |
| 75 |
leftbits -= 8 |
| 76 |
res.append(chr(leftchar >> leftbits)) |
| 77 |
leftchar &= ((1 << leftbits) - 1) |
| 78 |
bin_used += 1 |
| 79 |
|
| 80 |
last_char_was_a_pad = False |
| 81 |
i += 1 |
| 82 |
else: |
| 83 |
if leftbits != 0: |
| 84 |
if leftbits == 6: |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
msg = ("Invalid base64-encoded string: number of data " |
| 89 |
"characters (%d) cannot be 1 more than a multiple of 4" % |
| 90 |
((bin_used // 3) * 4 + 1)) |
| 91 |
raise_Error(space, msg) |
| 92 |
raise_Error(space, "Incorrect padding") |
| 93 |
|
| 94 |
return space.newbytes(res.build()) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
table_b2a_base64 = ( |
| 99 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") |
| 100 |
|
| 101 |
@unwrap_spec(bin='bufferstr', newline=bool) |
| 102 |
def b2a_base64(space, bin, __kwonly__, newline=True): |
| 103 |
"Base64-code line of data." |
| 104 |
|
| 105 |
newlength = (len(bin) + 2) // 3 |
| 106 |
try: |
| 107 |
newlength = ovfcheck(newlength * 4) |
| 108 |
except OverflowError: |
| 109 |
raise OperationError(space.w_MemoryError, space.w_None) |
| 110 |
newlength += 1 |
| 111 |
res = StringBuilder(newlength) |
| 112 |
|
| 113 |
leftchar = 0 |
| 114 |
leftbits = 0 |
| 115 |
for c in bin: |
| 116 |
|
| 117 |
leftchar = (leftchar << 8) | ord(c) |
| 118 |
leftbits += 8 |
| 119 |
res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f]) |
| 120 |
leftbits -= 6 |
| 121 |
if leftbits >= 6: |
| 122 |
res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f]) |
| 123 |
leftbits -= 6 |
| 124 |
|
| 125 |
if leftbits == 2: |
| 126 |
res.append(table_b2a_base64[(leftchar & 3) << 4]) |
| 127 |
res.append(PAD) |
| 128 |
res.append(PAD) |
| 129 |
elif leftbits == 4: |
| 130 |
res.append(table_b2a_base64[(leftchar & 0xf) << 2]) |
| 131 |
res.append(PAD) |
| 132 |
if newline: |
| 133 |
res.append('\n') |
| 134 |
return space.newbytes(res.build()) |