2023-08-23 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* @file Base64.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief Base64 encoding and decoding.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-26 18:57:48 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/Buffer.h>
|
|
|
|
#include <luna/String.h>
|
|
|
|
|
|
|
|
namespace Base64
|
|
|
|
{
|
2023-08-23 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* @brief Encode a string into Base64.
|
|
|
|
*
|
|
|
|
* @param data The string to encode.
|
|
|
|
* @return Result<String> An error, or the resulting Base64 string.
|
|
|
|
*/
|
2023-04-26 18:57:48 +00:00
|
|
|
Result<String> encode(StringView data);
|
2023-08-23 11:34:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Encode a slice of data into Base64.
|
|
|
|
*
|
|
|
|
* @param data The data to encode.
|
|
|
|
* @return Result<String> An error, or the resulting Base64 string.
|
|
|
|
*/
|
2023-04-26 18:57:48 +00:00
|
|
|
Result<String> encode(Slice<const u8> data);
|
2023-08-23 11:34:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Encode a buffer into Base64.
|
|
|
|
*
|
|
|
|
* @param data The buffer to encode.
|
|
|
|
* @return Result<String> An error, or the resulting Base64 string.
|
|
|
|
*/
|
2023-04-26 18:57:48 +00:00
|
|
|
Result<String> encode(const Buffer& data);
|
|
|
|
|
2023-08-23 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* @brief Decode a Base64 string.
|
|
|
|
*
|
|
|
|
* @param data The string to decode.
|
|
|
|
* @param allow_garbage_chars Whether to skip non-Base64 characters instead of returning an error.
|
|
|
|
* @return Result<Buffer> An error, or the raw decoded data.
|
|
|
|
*/
|
2023-04-26 18:57:48 +00:00
|
|
|
Result<Buffer> decode(StringView data, bool allow_garbage_chars = false);
|
2023-08-23 11:34:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Decode a Base64 string, returning a string instead of raw data.
|
|
|
|
*
|
|
|
|
* Note that decoded Base64 may be anything; invalid UTF-8, null characters in the middle of data, etc...
|
|
|
|
* For this reason it is not recommended to use this function unless you're sure that what is decoded will be a
|
|
|
|
* valid string (so, don't use this with untrusted user input).
|
|
|
|
*
|
|
|
|
* @param data The string to decode.
|
|
|
|
* @param allow_garbage_chars Whether to skip non-Base64 characters instead of returning an error.
|
|
|
|
* @return Result<String> An error, or the decoded string.
|
|
|
|
*/
|
2023-04-26 18:57:48 +00:00
|
|
|
Result<String> decode_string(StringView data, bool allow_garbage_chars = false);
|
|
|
|
}
|