decodeBase64ZlibToString

@RequiresApi(value = 26)
fun decodeBase64ZlibToString(b64: String, urlSafePreferred: Boolean = true, tolerateMissingPadding: Boolean = true, maxOutputBytes: Int = 2 * 1024 * 1024): String?

Decode a QR payload: Base64 (URL-safe or standard) → inflate (gzip/zlib/raw-deflate) → UTF-8 string.

The function tries to auto-detect:

  1. Base64 alphabet (URL-safe first, then standard), optionally fixing missing padding.

  2. Compression container: gzip (RFC1952), zlib (RFC1950), or raw DEFLATE (no headers).

Returns null on any problem (invalid Base64, corrupt stream, unsupported format, or if the inflated output exceeds maxOutputBytes).

Example

val text = QrZlibB64.decodeBase64ZlibToString(b64Payload)
if (text != null) {
// use the UTF-8 content (e.g., JSON)
}

Return

The decoded UTF-8 string, or null if decoding fails or size cap is exceeded.

Parameters

b64

The Base64 or Base64URL string (padding optional).

urlSafePreferred

Try Base64URL decoder first (common for QR). Falls back to standard if needed.

tolerateMissingPadding

If true, auto-adds '=' padding when length % 4 != 0.

maxOutputBytes

Hard limit on inflated size to mitigate zip-bombs (default 2 MiB).