base64.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* base64.h -- Encode binary data using printable characters.
  2. Copyright (C) 2004-2006, 2009-2015 Free Software Foundation, Inc.
  3. Written by Simon Josefsson.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef BASE64_H
  15. # define BASE64_H
  16. /* Get size_t. */
  17. # include <stddef.h>
  18. /* Get bool. */
  19. # include <stdbool.h>
  20. # ifdef __cplusplus
  21. extern "C" {
  22. # endif
  23. /* This uses that the expression (n+(k-1))/k means the smallest
  24. integer >= n/k, i.e., the ceiling of n/k. */
  25. # define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
  26. struct base64_decode_context
  27. {
  28. unsigned int i;
  29. char buf[4];
  30. };
  31. extern bool isbase64 (char ch) _GL_ATTRIBUTE_CONST;
  32. extern void base64_encode (const char *restrict in, size_t inlen,
  33. char *restrict out, size_t outlen);
  34. extern size_t base64_encode_alloc (const char *in, size_t inlen, char **out);
  35. extern void base64_decode_ctx_init (struct base64_decode_context *ctx);
  36. extern bool base64_decode_ctx (struct base64_decode_context *ctx,
  37. const char *restrict in, size_t inlen,
  38. char *restrict out, size_t *outlen);
  39. extern bool base64_decode_alloc_ctx (struct base64_decode_context *ctx,
  40. const char *in, size_t inlen,
  41. char **out, size_t *outlen);
  42. #define base64_decode(in, inlen, out, outlen) \
  43. base64_decode_ctx (NULL, in, inlen, out, outlen)
  44. #define base64_decode_alloc(in, inlen, out, outlen) \
  45. base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)
  46. # ifdef __cplusplus
  47. }
  48. # endif
  49. #endif /* BASE64_H */