MdePkg/BaseLib: Add Base64Encode() and Base64Decode()

Introduce public functions Base64Encode and Base64Decode.
https://bugzilla.tianocore.org/show_bug.cgi?id=1370

v2:1.Remove some white space.
   2.Add unit test with test vectors in RFC 4648.
     https://github.com/shenglei10/edk2/tree/encode_test
     https://github.com/shenglei10/edk2/tree/decode_test

v3:1.Align white space.
   2.Update comments of Base64Encode and Base64Decode.
   3.Change the use of macro RETURN_DEVICE_ERROR to
     RETURN_INVALID_PARAMETER in string.c.

v4:Change parameters' names.

v5:1.Update usage of variables.
   2.Remove debug message in Base64Decode().

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Shenglei Zhang <shenglei.zhang@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Mike Turner
2018-12-28 16:00:02 +08:00
committed by Liming Gao
parent 97c8f5b9e7
commit 1f7af69d10
2 changed files with 387 additions and 0 deletions

View File

@@ -2760,6 +2760,62 @@ AsciiCharToUpper (
IN CHAR8 Chr
);
/**
Convert binary data to a Base64 encoded ascii string based on RFC4648.
Produce a Null-terminated Ascii string in the output buffer specified by Destination and DestinationSize.
The Ascii string is produced by converting the data string specified by Source and SourceLength.
@param Source Input UINT8 data
@param SourceLength Number of UINT8 bytes of data
@param Destination Pointer to output string buffer
@param DestinationSize Size of ascii buffer. Set to 0 to get the size needed.
Caller is responsible for passing in buffer of DestinationSize
@retval RETURN_SUCCESS When ascii buffer is filled in.
@retval RETURN_INVALID_PARAMETER If Source is NULL or DestinationSize is NULL.
@retval RETURN_INVALID_PARAMETER If SourceLength or DestinationSize is bigger than (MAX_ADDRESS - (UINTN)Destination).
@retval RETURN_BUFFER_TOO_SMALL If SourceLength is 0 and DestinationSize is <1.
@retval RETURN_BUFFER_TOO_SMALL If Destination is NULL or DestinationSize is smaller than required buffersize.
**/
RETURN_STATUS
EFIAPI
Base64Encode (
IN CONST UINT8 *Source,
IN UINTN SourceLength,
OUT CHAR8 *Destination OPTIONAL,
IN OUT UINTN *DestinationSize
);
/**
Convert Base64 ascii string to binary data based on RFC4648.
Produce Null-terminated binary data in the output buffer specified by Destination and DestinationSize.
The binary data is produced by converting the Base64 ascii string specified by Source and SourceLength.
@param Source Input ASCII characters
@param SourceLength Number of ASCII characters
@param Destination Pointer to output buffer
@param DestinationSize Caller is responsible for passing in buffer of at least DestinationSize.
Set 0 to get the size needed. Set to bytes stored on return.
@retval RETURN_SUCCESS When binary buffer is filled in.
@retval RETURN_INVALID_PARAMETER If Source is NULL or DestinationSize is NULL.
@retval RETURN_INVALID_PARAMETER If SourceLength or DestinationSize is bigger than (MAX_ADDRESS -(UINTN)Destination ).
@retval RETURN_INVALID_PARAMETER If there is any invalid character in input stream.
@retval RETURN_BUFFER_TOO_SMALL If buffer length is smaller than required buffer size.
**/
RETURN_STATUS
EFIAPI
Base64Decode (
IN CONST CHAR8 *Source,
IN UINTN SourceLength,
OUT UINT8 *Destination OPTIONAL,
IN OUT UINTN *DestinationSize
);
/**
Converts an 8-bit value to an 8-bit BCD value.