Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Base64.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/UnrealString.h"
7#include "CoreTypes.h"
8#include "Misc/Timespan.h"
9
10/** Mode to use for base64 encoding and decoding */
11enum class EBase64Mode : uint8
12{
13 /** Use the standard set of character mappings. (Table 1 from RFC 4648, known as "base64") */
15 /** Use the "URL and Filename safe" set of character mappings. (Table 2 from RFC 4648, known as "base64url") */
17};
18
19/**
20 * Class for encoding/decoding Base64 data (RFC 4648)
21 */
22struct FBase64
23{
24 /**
25 * Encodes a FString into a Base64 string
26 *
27 * @param Source The string data to convert
28 * @param Mode The mode to use for encoding. Default is EBase64Mode::Standard
29 *
30 * @return A string that encodes the binary data in a way that can be safely transmitted via various Internet protocols
31 */
33
34 /**
35 * Encodes a binary uint8 array into a Base64 string
36 *
37 * @param Source The binary data to convert
38 * @param Mode The mode to use for encoding. Default is EBase64Mode::Standard
39 *
40 * @return A string that encodes the binary data in a way that can be safely transmitted via various Internet protocols
41 */
42 static FString Encode(const TArray<uint8>& Source, EBase64Mode Mode = EBase64Mode::Standard);
43
44 /**
45 * Encodes the source into a Base64 string
46 *
47 * @param Source The binary data to encode
48 * @param Length Length of the binary data to be encoded
49 * @param Mode The mode to use for encoding. Default is EBase64Mode::Standard
50 *
51 * @return Base64 encoded string containing the binary data.
52 */
53 static FString Encode(const uint8* Source, uint32 Length, EBase64Mode Mode = EBase64Mode::Standard);
54
55 /**
56 * Encodes the source into a Base64 string, storing it in a preallocated buffer.
57 *
58 * @param Source The binary data to encode
59 * @param Length Length of the binary data to be encoded
60 * @param Dest Buffer to receive the encoded data. Must be large enough to contain the entire output data (see GetEncodedDataSize()).
61 * @param Mode The mode to use for encoding. Default is EBase64Mode::Standard
62 *
63 * @return The length of the encoded data
64 */
65 template<typename CharType> static uint32 Encode(const uint8* Source, uint32 Length, CharType* Dest, EBase64Mode Mode = EBase64Mode::Standard);
66
67 /**
68 * Get the encoded data size for the given number of bytes.
69 *
70 * @param NumBytes The number of bytes of input
71 *
72 * @return The number of characters in the encoded data.
73 */
74 static inline constexpr uint32 GetEncodedDataSize(uint32 NumBytes)
75 {
76 return ((NumBytes + 2) / 3) * 4;
77 }
78
79 /**
80 * Decodes a Base64 string into a FString
81 *
82 * @param Source The Base64 encoded string
83 * @param OutDest Receives the decoded string data
84 * @param Mode The mode to use for decoding. Default is EBase64Mode::Standard
85 *
86 * @return true if the buffer was decoded, false if it was invalid.
87 */
88 static bool Decode(const FString& Source, FString& OutDest, EBase64Mode Mode = EBase64Mode::Standard);
89
90 /**
91 * Decodes a Base64 string into an array of bytes
92 *
93 * @param Source The Base64 encoded string
94 * @param Dest Array to receive the decoded data
95 * @param Mode The mode to use for decoding. Default is EBase64Mode::Standard
96 *
97 * @return true if the buffer was decoded, false if it was invalid.
98 */
99 static bool Decode(const FString& Source, TArray<uint8>& Dest, EBase64Mode Mode = EBase64Mode::Standard);
100
101 /**
102 * Decodes a Base64 string into a preallocated buffer
103 *
104 * @param Source The Base64 encoded string
105 * @param Length Length of the Base64 encoded string
106 * @param Dest Buffer to receive the decoded data
107 * @param Mode The mode to use for decoding. Default is EBase64Mode::Standard
108 *
109 * @return true if the buffer was decoded, false if it was invalid.
110 */
111 template<typename CharType> static bool Decode(const CharType* Source, uint32 Length, uint8* Dest, EBase64Mode Mode = EBase64Mode::Standard);
112
113 /**
114 * Determine the decoded data size for the incoming base64 encoded string
115 *
116 * @param Source The Base64 encoded string
117 *
118 * @return The size in bytes of the decoded data
119 */
120 static uint32 GetDecodedDataSize(const FString& Source);
121
122 /**
123 * Determine the decoded data size for the incoming base64 encoded string
124 *
125 * @param Source The Base64 encoded string
126 * @param Length Length of the Base64 encoded string
127 *
128 * @return The size in bytes of the decoded data
129 */
130 template<typename CharType> static uint32 GetDecodedDataSize(const CharType* Source, uint32 Length);
131
132 /**
133 * Get the maximum decoded data size for the given number of input characters.
134 *
135 * @param Length The number of input characters.
136 *
137 * @return The maximum number of bytes that can be decoded from this input stream. The actual number of characters decoded may be less if the data contains padding characters. Call GetDecodedDataSize() with the input data to find the exact length.
138 */
139 static inline constexpr uint32 GetMaxDecodedDataSize(uint32 NumChars)
140 {
141 return ((NumChars * 3) + 3) / 4;
142 }
143};
EBase64Mode
Definition Enums.h:11881
static bool Decode(const CharType *Source, uint32 Length, uint8 *Dest, EBase64Mode Mode=EBase64Mode::Standard)
static constexpr uint32 GetMaxDecodedDataSize(uint32 NumChars)
Definition Base64.h:139
static constexpr uint32 GetEncodedDataSize(uint32 NumBytes)
Definition Base64.h:74
static uint32 GetDecodedDataSize(const CharType *Source, uint32 Length)
static bool Decode(const FString &Source, TArray< uint8 > &Dest, EBase64Mode Mode=EBase64Mode::Standard)
static FString Encode(const TArray< uint8 > &Source, EBase64Mode Mode=EBase64Mode::Standard)
static FString Encode(const FString &Source, EBase64Mode Mode=EBase64Mode::Standard)
static FString Encode(const uint8 *Source, uint32 Length, EBase64Mode Mode=EBase64Mode::Standard)
static uint32 GetDecodedDataSize(const FString &Source)
static bool Decode(const FString &Source, FString &OutDest, EBase64Mode Mode=EBase64Mode::Standard)
static uint32 Encode(const uint8 *Source, uint32 Length, CharType *Dest, EBase64Mode Mode=EBase64Mode::Standard)