tiny_base64.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * \file base64.h
  3. *
  4. * Based on TropicSSL: Copyright (C) 2017 Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
  7. *
  8. * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the names of PolarSSL or XySSL nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef TINY_CRYPT_BASE64_H__
  38. #define TINY_CRYPT_BASE64_H__
  39. #define ERR_BASE64_BUFFER_TOO_SMALL -0x0010
  40. #define ERR_BASE64_INVALID_CHARACTER -0x0012
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * \brief Encode a buffer into base64 format
  46. *
  47. * \param dst destination buffer
  48. * \param dlen size of the buffer
  49. * \param src source buffer
  50. * \param slen amount of data to be encoded
  51. *
  52. * \return 0 if successful, or TROPICSSL_ERR_BASE64_BUFFER_TOO_SMALL.
  53. * *dlen is always updated to reflect the amount
  54. * of data that has (or would have) been written.
  55. *
  56. * \note Call this function with *dlen = 0 to obtain the
  57. * required buffer size in *dlen
  58. */
  59. int tiny_base64_encode(unsigned char *dst, int *dlen,
  60. unsigned char *src, int slen);
  61. /**
  62. * \brief Decode a base64-formatted buffer
  63. *
  64. * \param dst destination buffer
  65. * \param dlen size of the buffer
  66. * \param src source buffer
  67. * \param slen amount of data to be decoded
  68. *
  69. * \return 0 if successful, TROPICSSL_ERR_BASE64_BUFFER_TOO_SMALL, or
  70. * TROPICSSL_ERR_BASE64_INVALID_DATA if the input data is not
  71. * correct. *dlen is always updated to reflect the amount
  72. * of data that has (or would have) been written.
  73. *
  74. * \note Call this function with *dlen = 0 to obtain the
  75. * required buffer size in *dlen
  76. */
  77. int tiny_base64_decode(unsigned char *dst, int *dlen,
  78. unsigned char *src, int slen);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif /* base64.h */