Decode a base64 payload while tolerating omitted padding. Args: payload: Base64 payload without a data URI header. error_message: Message to use when decoding fails. validate: Whether to ask ``base64.b64decode`` to reject non-base64 characters. Returns:
(
payload: str,
*,
error_message: str,
validate: bool = False,
)
| 270 | |
| 271 | |
| 272 | def _decode_base64_payload( |
| 273 | payload: str, |
| 274 | *, |
| 275 | error_message: str, |
| 276 | validate: bool = False, |
| 277 | ) -> bytes: |
| 278 | """Decode a base64 payload while tolerating omitted padding. |
| 279 | |
| 280 | Args: |
| 281 | payload: Base64 payload without a data URI header. |
| 282 | error_message: Message to use when decoding fails. |
| 283 | validate: Whether to ask ``base64.b64decode`` to reject non-base64 |
| 284 | characters. |
| 285 | |
| 286 | Returns: |
| 287 | Decoded bytes. |
| 288 | |
| 289 | Raises: |
| 290 | ValueError: Raised when the payload cannot be decoded. |
| 291 | """ |
| 292 | payload = "".join(payload.split()) |
| 293 | missing_padding = len(payload) % 4 |
| 294 | if missing_padding: |
| 295 | payload += "=" * (4 - missing_padding) |
| 296 | |
| 297 | try: |
| 298 | return base64.b64decode(payload, validate=validate) |
| 299 | except (binascii.Error, ValueError) as exc: |
| 300 | raise ValueError(error_message) from exc |
| 301 | |
| 302 | |
| 303 | def describe_media_ref(media_ref: object | None) -> str: |
no outgoing calls
no test coverage detected