Browse Source

[core] buffer_append_base64_encode_opt()

with option to include or omit padding

single func to handle both:
- buffer_append_base64_encode_no_padding
- buffer_append_base64_encode
master
Glenn Strauss 7 months ago
parent
commit
6de4c809c9
  1. 23
      src/base64.c
  2. 16
      src/base64.h

23
src/base64.c

@ -205,25 +205,18 @@ size_t li_to_base64(char* out, size_t out_length, const unsigned char* in, size_
}
char* buffer_append_base64_encode_no_padding(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset) {
size_t reserve = 4*(in_length/3) + 4;
char* result = buffer_string_prepare_append(out, reserve);
size_t out_pos = li_to_base64_no_padding(result, reserve, in, in_length, charset);
char* buffer_append_base64_encode_opt(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset, int pad) {
const size_t reserve = 4*(in_length/3) + 4;
char * const result = buffer_string_prepare_append(out, reserve);
const size_t out_pos = (pad)
? li_to_base64(result, reserve, in, in_length, charset)
: li_to_base64_no_padding(result, reserve, in, in_length, charset);
buffer_commit(out, out_pos);
buffer_commit(out, out_pos);
return result;
return result;
}
char* buffer_append_base64_encode(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset) {
size_t reserve = 4*(in_length/3) + 4;
char* result = buffer_string_prepare_append(out, reserve);
size_t out_pos = li_to_base64(result, reserve, in, in_length, charset);
buffer_commit(out, out_pos);
return result;
}
unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t in_length, base64_charset charset) {
const size_t reserve = 3*(in_length/4) + 3;

16
src/base64.h

@ -14,10 +14,22 @@ unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t i
size_t li_to_base64_no_padding(char* out, size_t out_length, const unsigned char* in, size_t in_length, base64_charset charset);
size_t li_to_base64(char* out, size_t out_length, const unsigned char* in, size_t in_length, base64_charset charset);
__attribute_nonnull__
__attribute_returns_nonnull__
char* buffer_append_base64_encode_no_padding(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset);
char* buffer_append_base64_encode_opt(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset, int pad);
__attribute_nonnull__
__attribute_returns_nonnull__
char* buffer_append_base64_encode(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset);
static inline char* buffer_append_base64_encode_no_padding(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset);
static inline char* buffer_append_base64_encode_no_padding(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset) {
return buffer_append_base64_encode_opt(out, in, in_length, charset, 0);
}
__attribute_nonnull__
__attribute_returns_nonnull__
static inline char* buffer_append_base64_encode(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset);
static inline char* buffer_append_base64_encode(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset) {
return buffer_append_base64_encode_opt(out, in, in_length, charset, 1);
}
#endif

Loading…
Cancel
Save