Function: base64-decode-string

base64-decode-string is a function defined in fns.c.

Signature

(base64-decode-string STRING &optional BASE64URL IGNORE-INVALID)

Documentation

Base64-decode STRING and return the result as a string.

Optional argument BASE64URL determines whether to use the URL variant of the base 64 encoding, as defined in RFC 4648. If optional third argument IGNORE-INVALID is non-nil invalid characters are ignored instead of signaling an error.

View in manual

Probably introduced at or before Emacs version 20.4.

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  char *decoded;
  ptrdiff_t length, decoded_length;
  Lisp_Object decoded_string;
  USE_SAFE_ALLOCA;

  CHECK_STRING (string);

  length = SBYTES (string);
  /* We need to allocate enough room for decoding the text. */
  decoded = SAFE_ALLOCA (length);

  /* The decoded result should be unibyte. */
  ptrdiff_t decoded_chars;
  decoded_length = base64_decode_1 (SSDATA (string), decoded, length,
				    !NILP (base64url), false,
				    !NILP (ignore_invalid), &decoded_chars);
  if (decoded_length > length)
    emacs_abort ();
  else if (decoded_length >= 0)
    decoded_string = make_unibyte_string (decoded, decoded_length);
  else
    decoded_string = Qnil;

  SAFE_FREE ();
  if (!STRINGP (decoded_string))
    error ("Invalid base64 data");

  return decoded_string;
}