base64.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* base64.h -- Encode binary data using printable characters.
  2. Copyright (C) 2004, 2005, 2006, 2009, 2010 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, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef BASE64_H
  16. # define BASE64_H
  17. /* Get size_t. */
  18. # include <stddef.h>
  19. /* Get bool. */
  20. # include <stdbool.h>
  21. /* This uses that the expression (n+(k-1))/k means the smallest
  22. integer >= n/k, i.e., the ceiling of n/k. */
  23. # define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
  24. struct base64_decode_context
  25. {
  26. unsigned int i;
  27. char buf[4];
  28. };
  29. extern bool isbase64 (char ch);
  30. extern void base64_encode (const char *restrict in, size_t inlen,
  31. char *restrict out, size_t outlen);
  32. extern size_t base64_encode_alloc (const char *in, size_t inlen, char **out);
  33. extern void base64_decode_ctx_init (struct base64_decode_context *ctx);
  34. extern bool base64_decode_ctx (struct base64_decode_context *ctx,
  35. const char *restrict in, size_t inlen,
  36. char *restrict out, size_t *outlen);
  37. extern bool base64_decode_alloc_ctx (struct base64_decode_context *ctx,
  38. const char *in, size_t inlen,
  39. char **out, size_t *outlen);
  40. #define base64_decode(in, inlen, out, outlen) \
  41. base64_decode_ctx (NULL, in, inlen, out, outlen)
  42. #define base64_decode_alloc(in, inlen, out, outlen) \
  43. base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)
  44. #endif /* BASE64_H */