(rfc base64) - Base 64 encode and decode library

Library (rfc base64)

This library provides Base 64 encoding and decoding procedures.

Encoding procedures

in must be either a bytevector or binary input port.

Encodes given input in to Base 64 encoded bytevector.

The keyword argument line-width specifies where the encode procedure should put linefeed. If this is less than 1 or #f, encoder does not put linefeed.

The keyword argument padding? controls if the result encoded value contains padding character #\= or not. If this is #f, then the result value won't contain padding character.

The base64url-encode encodes the given input to Base 64 URL safe encoded bytevector. Which doesn't use + and /.

Convenient procedure for string.

Encodes given string to Base 64 encoded string.

The keyword argument transcoder is used to convert given string to bytevector. The converted bytevector will be passed to the base64-encodeprocedure. The default value is a transcoder with UTF-8 codec with EOL style none.

The keyword argument padding? is the same as base64-encode.

The base64url-encode-string encodes the given input to Base 64 URL safe encoded bytevector. Which doesn't use + and /.

Creates binary Base64 encode input and output port, respectively.

source must be binary inpurt port.

The input port reads bytes from source and returns Base64 encoded result.

sink must be binary inpurt port.

The output port puts encoded bytes to sink. The port must be closed to finish the encoding process properly.

The keyword argument padding? is the same as base64-encode.

The open-base64url-encode-input-port and open-base64url-encode-output-port encode to Base64 URL safe encode.

Decoding procedures

in must be a bytevector or binary input port.

Decode Base 64 encoded input in to original bytevector.

The base64url-decode decodes Base64 URL safe encoded value.

Convenient procedure.

Decode Base 64 encoded string to original string. The procedure is using base64-decode.

The keyword argument specifies how to convert the decoded bytevector to string. If this is #f, the procedure returns raw bytevector.

The base64url-decode-string decodes Base64 URL safe encoded value.

Creates binary Base64 decode input and output port, respectively.

source must be binary inpurt port.

The input port reads Base64 encoded bytes from source and returns decoded results.

sink must be binary inpurt port.

The output port puts decoded bytes to sink. The port must be closed to finish the encoding process properly.

The open-base64url-decode-input-port and open-base64url-decode-output-port decode Base64 URL safe encoded value.

Low level APIs

Both encode and decode procedures are using encoder and decoder. Both encoder and decoder are just a procedure which takes 2 arguments get and put and not reentrant as they have own internal buffer to process the input.

Creates a Base64 encoder. An encoder is a procedure which takes 2 arguments get and put.

get must be a procedure takes 0 argument and it must return one of the followings; a fixnum of range 0 to 255, EOF object, or negative integer. The fixnum value is treated as an input of the encoding value. The negative integer value is treated as a continue marker. When the encoder receives this value, then it won't encode until the next value is available.

put must be a procedure takes 1 argument which is either a fixnum of range 0 to 255 or #f. The fixnum is the encoded value of the input. #f indicates line break point, so user can determine which line break this encoder should use.

The following shows how to make Base64 encoder with line break of CRLF.

(define (base64-encode-w/crlf bv)
  (let-values (((out e) (open-bytevector-output-port)))
    (define (put v)
      (if v
	  (put-u8 out v)
	  (begin (put-u8 out #x0d) (put-u8 out #x0a))))
    (define inp (open-bytevector-input-port bv))
    (define (in) (get-u8 inp))
    (define encoder (make-base64-encoder))
    (do () ((encoder in put)))
    (e)))

Creates a Base64 decoder. A decoder is a procedure which takes 2 arguments get and put.

get is the same as the one from encoder.

put must be a procedure takes 1 arguments which is a fixnm of range 0 to 255. The value is always a decoded byte.

Default encode or decode tables. If the name contains url, then it is suitable for "base64url".