summaryrefslogtreecommitdiffstats
path: root/gl/base64.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/base64.h')
-rw-r--r--gl/base64.h54
1 files changed, 44 insertions, 10 deletions
diff --git a/gl/base64.h b/gl/base64.h
index 9913765..7691f6c 100644
--- a/gl/base64.h
+++ b/gl/base64.h
@@ -1,5 +1,5 @@
1/* base64.h -- Encode binary data using printable characters. 1/* base64.h -- Encode binary data using printable characters.
2 Copyright (C) 2004-2006, 2009-2023 Free Software Foundation, Inc. 2 Copyright (C) 2004-2006, 2009-2024 Free Software Foundation, Inc.
3 Written by Simon Josefsson. 3 Written by Simon Josefsson.
4 4
5 This file is free software: you can redistribute it and/or modify 5 This file is free software: you can redistribute it and/or modify
@@ -16,18 +16,33 @@
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 17
18#ifndef BASE64_H 18#ifndef BASE64_H
19# define BASE64_H 19#define BASE64_H
20
21/* This file uses _GL_INLINE_HEADER_BEGIN. */
22#if !_GL_CONFIG_H_INCLUDED
23 #error "Please include config.h first."
24#endif
20 25
21/* Get idx_t. */ 26/* Get idx_t. */
22# include <idx.h> 27#include <idx.h>
28
29/* Pacify GCC in isubase64. */
30#if defined __GNUC__ && 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
31# pragma GCC diagnostic ignored "-Wtype-limits"
32#endif
33
34_GL_INLINE_HEADER_BEGIN
35#ifndef BASE64_INLINE
36# define BASE64_INLINE _GL_INLINE
37#endif
23 38
24# ifdef __cplusplus 39#ifdef __cplusplus
25extern "C" { 40extern "C" {
26# endif 41#endif
27 42
28/* This uses that the expression (n+(k-1))/k means the smallest 43/* This uses that the expression (n+(k-1))/k means the smallest
29 integer >= n/k, i.e., the ceiling of n/k. */ 44 integer >= n/k, i.e., the ceiling of n/k. */
30# define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4) 45#define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
31 46
32struct base64_decode_context 47struct base64_decode_context
33{ 48{
@@ -35,14 +50,31 @@ struct base64_decode_context
35 char buf[4]; 50 char buf[4];
36}; 51};
37 52
38extern bool isbase64 (char ch) _GL_ATTRIBUTE_CONST; 53extern signed char const base64_to_int[256];
54
55BASE64_INLINE bool
56isubase64 (unsigned char ch)
57{
58 return ch < sizeof base64_to_int && 0 <= base64_to_int[ch];
59}
60
61BASE64_INLINE bool
62isbase64 (char ch)
63{
64 return isubase64 (ch);
65}
39 66
40extern void base64_encode (const char *restrict in, idx_t inlen, 67extern void base64_encode (const char *restrict in, idx_t inlen,
41 char *restrict out, idx_t outlen); 68 char *restrict out, idx_t outlen);
42 69
43extern idx_t base64_encode_alloc (const char *in, idx_t inlen, char **out); 70extern idx_t base64_encode_alloc (const char *in, idx_t inlen, char **out);
44 71
45extern void base64_decode_ctx_init (struct base64_decode_context *ctx); 72/* Initialize decode-context buffer, CTX. */
73BASE64_INLINE void
74base64_decode_ctx_init (struct base64_decode_context *ctx)
75{
76 ctx->i = 0;
77}
46 78
47extern bool base64_decode_ctx (struct base64_decode_context *ctx, 79extern bool base64_decode_ctx (struct base64_decode_context *ctx,
48 const char *restrict in, idx_t inlen, 80 const char *restrict in, idx_t inlen,
@@ -58,8 +90,10 @@ extern bool base64_decode_alloc_ctx (struct base64_decode_context *ctx,
58#define base64_decode_alloc(in, inlen, out, outlen) \ 90#define base64_decode_alloc(in, inlen, out, outlen) \
59 base64_decode_alloc_ctx (NULL, in, inlen, out, outlen) 91 base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)
60 92
61# ifdef __cplusplus 93#ifdef __cplusplus
62} 94}
63# endif 95#endif
96
97_GL_INLINE_HEADER_END
64 98
65#endif /* BASE64_H */ 99#endif /* BASE64_H */