DEFINITION MODULE Base64;

        (********************************************************)
        (*                                                      *)
        (*         Encoding and decoding Base64 strings         *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            31 January 2003                 *)
        (*  Last edited:        12 January 2025                 *)
        (*  Status:             OK                              *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT (* type *) LOC;


PROCEDURE Encode (VAR (*IN*) data: ARRAY OF LOC;  N: CARDINAL;
                           VAR (*OUT*) result: ARRAY OF CHAR);

    (* Translates N bytes of data to Base64. *)

PROCEDURE Decode (VAR (*IN*) data: ARRAY OF CHAR;
                      VAR (*OUT*) result: ARRAY OF LOC): CARDINAL;

    (* Translates Base64 to a string of bytes.  The function result is the  *)
    (* number of bytes in the decoded result.                               *)

PROCEDURE DecodePart (data: ARRAY OF CHAR;  VAR (*INOUT*) excess: ARRAY OF CHAR;
                          VAR (*OUT*) result: ARRAY OF LOC;
                               VAR (*INOUT*) pos: CARDINAL): CARDINAL;

    (* Like Decode, but instead of requiring all of the input to be supplied    *)
    (* at once, we append the result to decoded data from previous calls.  The  *)
    (* answer is stored starting at result[pos], and pos is updated.  Parameter *)
    (* excess, which should be an empty string on the first call, and can be    *)
    (* up to three characters long after that, holds characters that have had   *)
    (* to be held over from one call to the next.                               *)
    (* Returns the number of output bytes produced.                             *)

END Base64.

