Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty, vendored at upstream commit 91fb38a without its git history.
71 lines
2.6 KiB
C
71 lines
2.6 KiB
C
/*
|
|
* Copyright (C) 2023 Kovid Goyal <kovid at kovidgoyal.net>
|
|
*
|
|
* Distributed under terms of the GPL3 license.
|
|
*/
|
|
|
|
#pragma once
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
#include "../3rdparty/base64/include/libbase64.h"
|
|
|
|
typedef struct base64_state base64_state;
|
|
|
|
static inline size_t
|
|
required_buffer_size_for_base64_decode(size_t src_sz) {
|
|
return (src_sz / 4 * 3 + 2);
|
|
}
|
|
static inline size_t
|
|
required_buffer_size_for_base64_encode(size_t src_sz) {
|
|
return ((src_sz + 2) / 3 * 4);
|
|
}
|
|
|
|
static inline void
|
|
base64_init_stream_decoder(base64_state *state) {
|
|
base64_stream_decode_init(state, 0);
|
|
}
|
|
|
|
static inline bool
|
|
base64_decode_stream(base64_state *state, const uint8_t *src, size_t src_sz, uint8_t *dest, size_t *dest_sz) {
|
|
// Decoding nothing always succeeds. libbase64 fails any call made after
|
|
// it has seen padding, even an empty one, which breaks protocols that
|
|
// terminate streams with an empty chunk.
|
|
if (!src_sz) {
|
|
*dest_sz = 0;
|
|
return true;
|
|
}
|
|
if (*dest_sz < required_buffer_size_for_base64_decode(src_sz)) return false;
|
|
return base64_stream_decode(state, (const char *)src, src_sz, (char *)dest, dest_sz) == 1;
|
|
}
|
|
|
|
static inline bool
|
|
base64_decode8(const uint8_t *src, size_t src_sz, uint8_t *dest, size_t *dest_sz) {
|
|
if (*dest_sz < required_buffer_size_for_base64_decode(src_sz)) return false;
|
|
// we ignore the return value of base64_decode as it returns non-zero when it is
|
|
// waiting for padding bytes
|
|
base64_decode((const char *)src, src_sz, (char *)dest, dest_sz, 0);
|
|
return true;
|
|
}
|
|
|
|
// Same as base64_decode8() but rejects, rather than ignoring, data that is not
|
|
// valid RFC-4648 base64, that is, data containing characters outside the
|
|
// base64 alphabet or data that is not correctly padded.
|
|
static inline bool
|
|
base64_decode8_strict(const uint8_t *src, size_t src_sz, uint8_t *dest, size_t *dest_sz) {
|
|
if (*dest_sz < required_buffer_size_for_base64_decode(src_sz)) return false;
|
|
// base64_decode() returns zero both for invalid characters and for input
|
|
// that ends in the middle of a four byte group, i.e. missing padding bytes
|
|
return base64_decode((const char *)src, src_sz, (char *)dest, dest_sz, 0) != 0;
|
|
}
|
|
|
|
static inline bool
|
|
base64_encode8(const unsigned char *src, size_t src_len, unsigned char *out, size_t *out_len, bool add_padding) {
|
|
if (*out_len < required_buffer_size_for_base64_encode(src_len)) return false;
|
|
base64_encode((const char *)src, src_len, (char *)out, out_len, 0);
|
|
if (!add_padding) {
|
|
while (*out_len && out[*out_len - 1] == '=') *out_len -= 1;
|
|
}
|
|
return true;
|
|
}
|