Decode the Base64 encoded bytes-like object or ASCII string s. Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters. If padded is false, padding in input is not required. T
(s, altchars=None, validate=_NOT_SPECIFIED,
*, padded=True, ignorechars=_NOT_SPECIFIED)
| 68 | |
| 69 | |
| 70 | def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, |
| 71 | *, padded=True, ignorechars=_NOT_SPECIFIED): |
| 72 | """Decode the Base64 encoded bytes-like object or ASCII string s. |
| 73 | |
| 74 | Optional altchars must be a bytes-like object or ASCII string of length 2 |
| 75 | which specifies the alternative alphabet used instead of the '+' and '/' |
| 76 | characters. |
| 77 | |
| 78 | If padded is false, padding in input is not required. |
| 79 | |
| 80 | The result is returned as a bytes object. A binascii.Error is raised if |
| 81 | s is incorrectly padded. |
| 82 | |
| 83 | If ignorechars is specified, it should be a byte string containing |
| 84 | characters to ignore from the input. The default value of validate is |
| 85 | True if ignorechars is specified, False otherwise. |
| 86 | |
| 87 | If validate is false, characters that are neither in the normal base-64 |
| 88 | alphabet nor the alternative alphabet are discarded prior to the |
| 89 | padding check. If validate is true, these non-alphabet characters in |
| 90 | the input result in a binascii.Error if they are not in ignorechars. |
| 91 | For more information about the strict base64 check, see: |
| 92 | |
| 93 | https://docs.python.org/3.11/library/binascii.html#binascii.a2b_base64 |
| 94 | """ |
| 95 | s = _bytes_from_decode_data(s) |
| 96 | if validate is _NOT_SPECIFIED: |
| 97 | validate = ignorechars is not _NOT_SPECIFIED |
| 98 | badchar = None |
| 99 | if altchars is not None: |
| 100 | altchars = _bytes_from_decode_data(altchars) |
| 101 | if len(altchars) != 2: |
| 102 | raise ValueError(f'invalid altchars: {altchars!r}') |
| 103 | if ignorechars is _NOT_SPECIFIED: |
| 104 | for b in b'+/': |
| 105 | if b not in altchars and b in s: |
| 106 | badchar = b |
| 107 | break |
| 108 | s = s.translate(bytes.maketrans(altchars, b'+/')) |
| 109 | else: |
| 110 | alphabet = binascii.BASE64_ALPHABET[:-2] + altchars |
| 111 | return binascii.a2b_base64(s, strict_mode=validate, |
| 112 | alphabet=alphabet, |
| 113 | padded=padded, ignorechars=ignorechars) |
| 114 | if ignorechars is _NOT_SPECIFIED: |
| 115 | ignorechars = b'' |
| 116 | result = binascii.a2b_base64(s, strict_mode=validate, |
| 117 | padded=padded, ignorechars=ignorechars) |
| 118 | if badchar is not None: |
| 119 | import warnings |
| 120 | if validate: |
| 121 | warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data ' |
| 122 | f'with altchars={altchars!r} and validate=True ' |
| 123 | f'will be an error in future Python versions', |
| 124 | DeprecationWarning, stacklevel=2) |
| 125 | else: |
| 126 | warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data ' |
| 127 | f'with altchars={altchars!r} and validate=False ' |
searching dependent graphs…