mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 08:47:28 +01:00
Add base_64 codec to wuffs_base
This commit is contained in:
1 parent
c766bb72d6
commit
309a5ba231
7 files changed
+878
-1
No files matched your search
@@ -206,6 +206,7 @@ extern const char wuffs_base__error__bad_i_o_position[];
|
||||
extern const char wuffs_base__error__bad_argument_length_too_short[];
|
||||
extern const char wuffs_base__error__bad_argument[];
|
||||
extern const char wuffs_base__error__bad_call_sequence[];
|
||||
extern const char wuffs_base__error__bad_data[];
|
||||
extern const char wuffs_base__error__bad_receiver[];
|
||||
extern const char wuffs_base__error__bad_restart[];
|
||||
extern const char wuffs_base__error__bad_sizeof_receiver[];
|
||||
@@ -319,6 +320,16 @@ typedef WUFFS_BASE__RESULT(uint64_t) wuffs_base__result_u64;
|
||||
|
||||
// --------
|
||||
|
||||
// wuffs_base__transform__output is the result of transforming from a src slice
|
||||
// to a dst slice.
|
||||
typedef struct {
|
||||
wuffs_base__status status;
|
||||
size_t num_dst;
|
||||
size_t num_src;
|
||||
} wuffs_base__transform__output;
|
||||
|
||||
// --------
|
||||
|
||||
// FourCC constants.
|
||||
|
||||
// International Color Consortium Profile.
|
||||
@@ -4201,6 +4212,57 @@ WUFFS_BASE__MAYBE_STATIC size_t //
|
||||
wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src);
|
||||
|
||||
// ---------------- Base-64
|
||||
|
||||
// Options (bitwise or'ed together) for wuffs_base__base_64__xxx functions.
|
||||
|
||||
#define WUFFS_BASE__BASE_64__DEFAULT_OPTIONS ((uint32_t)0x00000000)
|
||||
|
||||
// WUFFS_BASE__BASE_64__DECODE_ALLOW_PADDING means that, when decoding base-64,
|
||||
// the input may (but does not need to) be padded with '=' bytes so that the
|
||||
// overall encoded length in bytes is a multiple of 4. A successful decoding
|
||||
// will return a num_src that includes those padding bytes.
|
||||
//
|
||||
// Excess padding (e.g. three final '='s) will be rejected as bad data.
|
||||
#define WUFFS_BASE__BASE_64__DECODE_ALLOW_PADDING ((uint32_t)0x00000001)
|
||||
|
||||
// WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING means that, when encoding base-64,
|
||||
// the output will be padded with '=' bytes so that the overall encoded length
|
||||
// in bytes is a multiple of 4.
|
||||
#define WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING ((uint32_t)0x00000002)
|
||||
|
||||
// WUFFS_BASE__BASE_64__URL_ALPHABET means that, for base-64, the URL-friendly
|
||||
// and file-name-friendly alphabet be used, as per RFC 4648 section 5. When
|
||||
// this option bit is off, the standard alphabet from section 4 is used.
|
||||
#define WUFFS_BASE__BASE_64__URL_ALPHABET ((uint32_t)0x00000100)
|
||||
|
||||
// wuffs_base__base_64__decode transforms base-64 encoded bytes from src to
|
||||
// arbitrary bytes in dst.
|
||||
//
|
||||
// It will not permit line breaks or other whitespace in src. Filtering those
|
||||
// out is the responsibility of the caller.
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__I64CONV sub-module, not
|
||||
// just WUFFS_CONFIG__MODULE__BASE__CORE.
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__transform__output //
|
||||
wuffs_base__base_64__decode(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src,
|
||||
bool src_closed,
|
||||
uint32_t options);
|
||||
|
||||
// wuffs_base__base_64__encode transforms arbitrary bytes from src to base-64
|
||||
// encoded bytes in dst.
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__I64CONV sub-module, not
|
||||
// just WUFFS_CONFIG__MODULE__BASE__CORE.
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__transform__output //
|
||||
wuffs_base__base_64__encode(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src,
|
||||
bool src_closed,
|
||||
uint32_t options);
|
||||
|
||||
// ---------------- Unicode and UTF-8
|
||||
|
||||
#define WUFFS_BASE__UNICODE_CODE_POINT__MIN_INCL 0x00000000
|
||||
@@ -8391,6 +8453,7 @@ const char wuffs_base__error__bad_i_o_position[] = "#base: bad I/O position";
|
||||
const char wuffs_base__error__bad_argument_length_too_short[] = "#base: bad argument (length too short)";
|
||||
const char wuffs_base__error__bad_argument[] = "#base: bad argument";
|
||||
const char wuffs_base__error__bad_call_sequence[] = "#base: bad call sequence";
|
||||
const char wuffs_base__error__bad_data[] = "#base: bad data";
|
||||
const char wuffs_base__error__bad_receiver[] = "#base: bad receiver";
|
||||
const char wuffs_base__error__bad_restart[] = "#base: bad restart";
|
||||
const char wuffs_base__error__bad_sizeof_receiver[] = "#base: bad sizeof receiver";
|
||||
@@ -11940,6 +12003,325 @@ wuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,
|
||||
return len;
|
||||
}
|
||||
|
||||
// ---------------- Base-64
|
||||
|
||||
// The two base-64 alphabets, std and url, differ only in the last two codes.
|
||||
// - std: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
// - url: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||
|
||||
static const uint8_t wuffs_base__base_64__decode_std[256] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x00 ..= 0x07.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x08 ..= 0x0F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x10 ..= 0x17.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x18 ..= 0x1F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x20 ..= 0x27.
|
||||
0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, 0x80, 0x3F, // 0x28 ..= 0x2F.
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, // 0x30 ..= 0x37.
|
||||
0x3C, 0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x38 ..= 0x3F.
|
||||
|
||||
0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40 ..= 0x47.
|
||||
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48 ..= 0x4F.
|
||||
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50 ..= 0x57.
|
||||
0x17, 0x18, 0x19, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x58 ..= 0x5F.
|
||||
0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, // 0x60 ..= 0x67.
|
||||
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, // 0x68 ..= 0x6F.
|
||||
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, // 0x70 ..= 0x77.
|
||||
0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x78 ..= 0x7F.
|
||||
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x80 ..= 0x87.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x88 ..= 0x8F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x90 ..= 0x97.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x98 ..= 0x9F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xA0 ..= 0xA7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xA8 ..= 0xAF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xB0 ..= 0xB7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xC0 ..= 0xC7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xC8 ..= 0xCF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xD0 ..= 0xD7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xD8 ..= 0xDF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xE0 ..= 0xE7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xE8 ..= 0xEF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xF0 ..= 0xF7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
};
|
||||
|
||||
static const uint8_t wuffs_base__base_64__decode_url[256] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x00 ..= 0x07.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x08 ..= 0x0F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x10 ..= 0x17.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x18 ..= 0x1F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x20 ..= 0x27.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, // 0x28 ..= 0x2F.
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, // 0x30 ..= 0x37.
|
||||
0x3C, 0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x38 ..= 0x3F.
|
||||
|
||||
0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40 ..= 0x47.
|
||||
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48 ..= 0x4F.
|
||||
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50 ..= 0x57.
|
||||
0x17, 0x18, 0x19, 0x80, 0x80, 0x80, 0x80, 0x3F, // 0x58 ..= 0x5F.
|
||||
0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, // 0x60 ..= 0x67.
|
||||
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, // 0x68 ..= 0x6F.
|
||||
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, // 0x70 ..= 0x77.
|
||||
0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x78 ..= 0x7F.
|
||||
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x80 ..= 0x87.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x88 ..= 0x8F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x90 ..= 0x97.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0x98 ..= 0x9F.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xA0 ..= 0xA7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xA8 ..= 0xAF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xB0 ..= 0xB7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xC0 ..= 0xC7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xC8 ..= 0xCF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xD0 ..= 0xD7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xD8 ..= 0xDF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xE0 ..= 0xE7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xE8 ..= 0xEF.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xF0 ..= 0xF7.
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
};
|
||||
|
||||
static const uint8_t wuffs_base__base_64__encode_std[64] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // 0x00 ..= 0x07.
|
||||
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, // 0x08 ..= 0x0F.
|
||||
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, // 0x10 ..= 0x17.
|
||||
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, // 0x18 ..= 0x1F.
|
||||
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, // 0x20 ..= 0x27.
|
||||
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, // 0x28 ..= 0x2F.
|
||||
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, // 0x30 ..= 0x37.
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F, // 0x38 ..= 0x3F.
|
||||
};
|
||||
|
||||
static const uint8_t wuffs_base__base_64__encode_url[64] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // 0x00 ..= 0x07.
|
||||
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, // 0x08 ..= 0x0F.
|
||||
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, // 0x10 ..= 0x17.
|
||||
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, // 0x18 ..= 0x1F.
|
||||
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, // 0x20 ..= 0x27.
|
||||
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, // 0x28 ..= 0x2F.
|
||||
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, // 0x30 ..= 0x37.
|
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2D, 0x5F, // 0x38 ..= 0x3F.
|
||||
};
|
||||
|
||||
// --------
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__transform__output //
|
||||
wuffs_base__base_64__decode(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src,
|
||||
bool src_closed,
|
||||
uint32_t options) {
|
||||
const uint8_t* alphabet = (options & WUFFS_BASE__BASE_64__URL_ALPHABET)
|
||||
? wuffs_base__base_64__decode_url
|
||||
: wuffs_base__base_64__decode_std;
|
||||
wuffs_base__transform__output o;
|
||||
uint8_t* d_ptr = dst.ptr;
|
||||
size_t d_len = dst.len;
|
||||
const uint8_t* s_ptr = src.ptr;
|
||||
size_t s_len = src.len;
|
||||
bool pad = false;
|
||||
|
||||
while (s_len >= 4) {
|
||||
uint32_t s = wuffs_base__load_u32le__no_bounds_check(s_ptr);
|
||||
uint32_t s0 = alphabet[0xFF & (s >> 0)];
|
||||
uint32_t s1 = alphabet[0xFF & (s >> 8)];
|
||||
uint32_t s2 = alphabet[0xFF & (s >> 16)];
|
||||
uint32_t s3 = alphabet[0xFF & (s >> 24)];
|
||||
|
||||
if (((s0 | s1 | s2 | s3) & 0xC0) != 0) {
|
||||
if (s_len > 4) {
|
||||
o.status.repr = wuffs_base__error__bad_data;
|
||||
goto done;
|
||||
} else if (!src_closed) {
|
||||
o.status.repr = wuffs_base__suspension__short_read;
|
||||
goto done;
|
||||
} else if ((options & WUFFS_BASE__BASE_64__DECODE_ALLOW_PADDING) &&
|
||||
(s_ptr[3] == '=')) {
|
||||
pad = true;
|
||||
if (s_ptr[2] == '=') {
|
||||
goto src2;
|
||||
}
|
||||
goto src3;
|
||||
}
|
||||
o.status.repr = wuffs_base__error__bad_data;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (d_len < 3) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
|
||||
s_ptr += 4;
|
||||
s_len -= 4;
|
||||
s = (s0 << 18) | (s1 << 12) | (s2 << 6) | (s3 << 0);
|
||||
*d_ptr++ = (uint8_t)(s >> 16);
|
||||
*d_ptr++ = (uint8_t)(s >> 8);
|
||||
*d_ptr++ = (uint8_t)(s >> 0);
|
||||
d_len -= 3;
|
||||
}
|
||||
|
||||
if (!src_closed) {
|
||||
o.status.repr = wuffs_base__suspension__short_read;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (s_len == 0) {
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
} else if (s_len == 1) {
|
||||
o.status.repr = wuffs_base__error__bad_data;
|
||||
goto done;
|
||||
} else if (s_len == 2) {
|
||||
goto src2;
|
||||
}
|
||||
|
||||
src3:
|
||||
do {
|
||||
uint32_t s = wuffs_base__load_u24le__no_bounds_check(s_ptr);
|
||||
uint32_t s0 = alphabet[0xFF & (s >> 0)];
|
||||
uint32_t s1 = alphabet[0xFF & (s >> 8)];
|
||||
uint32_t s2 = alphabet[0xFF & (s >> 16)];
|
||||
if ((s0 & 0xC0) || (s1 & 0xC0) || (s2 & 0xC3)) {
|
||||
o.status.repr = wuffs_base__error__bad_data;
|
||||
goto done;
|
||||
}
|
||||
if (d_len < 2) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
s_ptr += pad ? 4 : 3;
|
||||
s = (s0 << 18) | (s1 << 12) | (s2 << 6);
|
||||
*d_ptr++ = (uint8_t)(s >> 16);
|
||||
*d_ptr++ = (uint8_t)(s >> 8);
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
} while (0);
|
||||
|
||||
src2:
|
||||
do {
|
||||
uint32_t s = wuffs_base__load_u16le__no_bounds_check(s_ptr);
|
||||
uint32_t s0 = alphabet[0xFF & (s >> 0)];
|
||||
uint32_t s1 = alphabet[0xFF & (s >> 8)];
|
||||
if ((s0 & 0xC0) || (s1 & 0xCF)) {
|
||||
o.status.repr = wuffs_base__error__bad_data;
|
||||
goto done;
|
||||
}
|
||||
if (d_len < 1) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
s_ptr += pad ? 4 : 2;
|
||||
s = (s0 << 18) | (s1 << 12);
|
||||
*d_ptr++ = (uint8_t)(s >> 16);
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
} while (0);
|
||||
|
||||
done:
|
||||
o.num_dst = d_ptr - dst.ptr;
|
||||
o.num_src = s_ptr - src.ptr;
|
||||
return o;
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__transform__output //
|
||||
wuffs_base__base_64__encode(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 src,
|
||||
bool src_closed,
|
||||
uint32_t options) {
|
||||
const uint8_t* alphabet = (options & WUFFS_BASE__BASE_64__URL_ALPHABET)
|
||||
? wuffs_base__base_64__encode_url
|
||||
: wuffs_base__base_64__encode_std;
|
||||
wuffs_base__transform__output o;
|
||||
uint8_t* d_ptr = dst.ptr;
|
||||
size_t d_len = dst.len;
|
||||
const uint8_t* s_ptr = src.ptr;
|
||||
size_t s_len = src.len;
|
||||
|
||||
do {
|
||||
while (s_len >= 3) {
|
||||
if (d_len < 4) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
uint32_t s = wuffs_base__load_u24be__no_bounds_check(s_ptr);
|
||||
s_ptr += 3;
|
||||
s_len -= 3;
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 18)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 12)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 6)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 0)];
|
||||
d_len -= 4;
|
||||
}
|
||||
|
||||
if (!src_closed) {
|
||||
o.status.repr = wuffs_base__suspension__short_read;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (s_len == 2) {
|
||||
if (d_len <
|
||||
((options & WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING) ? 4 : 3)) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
uint32_t s = ((uint32_t)(wuffs_base__load_u16be__no_bounds_check(s_ptr)))
|
||||
<< 8;
|
||||
s_ptr += 2;
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 18)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 12)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 6)];
|
||||
if (options & WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING) {
|
||||
*d_ptr++ = '=';
|
||||
}
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
|
||||
} else if (s_len == 1) {
|
||||
if (d_len <
|
||||
((options & WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING) ? 4 : 2)) {
|
||||
o.status.repr = wuffs_base__suspension__short_write;
|
||||
goto done;
|
||||
}
|
||||
uint32_t s = ((uint32_t)(wuffs_base__load_u8__no_bounds_check(s_ptr)))
|
||||
<< 16;
|
||||
s_ptr += 1;
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 18)];
|
||||
*d_ptr++ = alphabet[0x3F & (s >> 12)];
|
||||
if (options & WUFFS_BASE__BASE_64__ENCODE_EMIT_PADDING) {
|
||||
*d_ptr++ = '=';
|
||||
*d_ptr++ = '=';
|
||||
}
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
|
||||
} else {
|
||||
o.status.repr = NULL;
|
||||
goto done;
|
||||
}
|
||||
} while (0);
|
||||
|
||||
done:
|
||||
o.num_dst = d_ptr - dst.ptr;
|
||||
o.num_src = s_ptr - src.ptr;
|
||||
return o;
|
||||
}
|
||||
|
||||
#endif // !defined(WUFFS_CONFIG__MODULES) ||
|
||||
// defined(WUFFS_CONFIG__MODULE__BASE) ||
|
||||
// defined(WUFFS_CONFIG__MODULE__BASE__I64CONV)
|
||||
|
||||
Reference in new issue
Block a user