decodeWithInfo static method

B64DecodingInfo decodeWithInfo(
  1. String data, {
  2. bool validatePadding = true,
  3. bool urlSafe = true,
})

Implementation

static B64DecodingInfo decodeWithInfo(
  String data, {
  bool validatePadding = true,
  bool urlSafe = true,
}) {
  // Strip embedded newlines *before* measuring length, so line-wrapped
  // input doesn't throw off the padding/length checks below (the
  // original checked length first, so a line-wrapped-but-otherwise-valid
  // string could fail padding validation purely because of newlines).
  if (data.contains('\n') || data.contains('\r')) {
    data = data.replaceAll('\n', '').replaceAll('\r', '');
  }

  final isPadded = data.length % 4 == 0;
  final isUrlSafeInput = data.contains('-') || data.contains('_');
  if (isUrlSafeInput && !urlSafe) {
    throw const BuError(DefaultErrorType.badBase64, "Invalid Base64 string.");
  }

  if (validatePadding) {
    if (!isPadded) {
      throw const BuError(
        DefaultErrorType.badBase64,
        "Invalid Base64 string.",
      );
    }
  } else if (!isPadded) {
    final missing = (4 - data.length % 4) % 4;
    data = data.padRight(data.length + missing, '=');
  }

  return B64DecodingInfo(
    isUrlSafe: isUrlSafeInput,
    isPadded: isPadded,
    data: _decode(data),
  );
}