diff --git a/src/b64.ml b/src/b64.ml index 11cd192..cffdc6c 100644 --- a/src/b64.ml +++ b/src/b64.ml @@ -59,6 +59,12 @@ let decode ?alphabet input = done; Bytes.unsafe_to_string output +let decode_opt ?alphabet input = + try + Some (decode ?alphabet input) + with + Not_found -> None + let encode ?(pad=true) ?alphabet input = let length = String.length input in let words = (length + 2) / 3 in (* rounded up *) diff --git a/src/b64.mli b/src/b64.mli index 3e63fbf..6922d6e 100644 --- a/src/b64.mli +++ b/src/b64.mli @@ -39,6 +39,9 @@ val uri_safe_alphabet : string @raise Not_found if [s] is not a valid Base64 string. *) val decode : ?alphabet:string -> string -> string +(** Same as [decode], but returns [None] instead of raising. *) +val decode_opt : ?alphabet:string -> string -> string option + (** [encode s] encodes the string [s] into base64. If [pad] is false, no trailing padding is added. [pad] defaults to [true], and [alphabet] to {!default_alphabet}. *)