base64.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJLIB_UTIL_BASE64_H__
  20. #define __PJLIB_UTIL_BASE64_H__
  21. /**
  22. * @file base64.h
  23. * @brief Base64 encoding and decoding
  24. */
  25. #include <pjlib-util/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJLIB_UTIL_BASE64 Base64 Encoding/Decoding
  29. * @ingroup PJLIB_UTIL_ENCRYPTION
  30. * @{
  31. * This module implements base64 encoding and decoding.
  32. */
  33. /**
  34. * Helper macro to calculate the approximate length required for base256 to
  35. * base64 conversion.
  36. */
  37. #define PJ_BASE256_TO_BASE64_LEN(len) (len * 4 / 3 + 3)
  38. /**
  39. * Helper macro to calculate the approximage length required for base64 to
  40. * base256 conversion.
  41. */
  42. #define PJ_BASE64_TO_BASE256_LEN(len) (len * 3 / 4)
  43. /**
  44. * Encode a buffer into base64 encoding.
  45. *
  46. * @param input The input buffer.
  47. * @param in_len Size of the input buffer.
  48. * @param output Output buffer. Caller must allocate this buffer with
  49. * the appropriate size.
  50. * @param out_len On entry, it specifies the length of the output buffer.
  51. * Upon return, this will be filled with the actual
  52. * length of the output buffer.
  53. *
  54. * @return PJ_SUCCESS on success.
  55. */
  56. PJ_DECL(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
  57. char *output, int *out_len);
  58. /**
  59. * Encode a buffer into base64 (URL and Filename Safe) encoding.
  60. *
  61. * @param input The input buffer.
  62. * @param in_len Size of the input buffer.
  63. * @param output Output buffer. Caller must allocate this buffer with
  64. * the appropriate size.
  65. * @param out_len On entry, it specifies the length of the output buffer.
  66. * Upon return, this will be filled with the actual
  67. * length of the output buffer.
  68. *
  69. * @return PJ_SUCCESS on success.
  70. */
  71. PJ_DECL(pj_status_t) pj_base64url_encode(const pj_uint8_t *input, int in_len,
  72. char *output, int *out_len);
  73. /**
  74. * Decode base64 string.
  75. *
  76. * @param input Input string.
  77. * @param out Buffer to store the output. Caller must allocate
  78. * this buffer with the appropriate size.
  79. * @param out_len On entry, it specifies the length of the output buffer.
  80. * Upon return, this will be filled with the actual
  81. * length of the output.
  82. */
  83. PJ_DECL(pj_status_t) pj_base64_decode(const pj_str_t *input,
  84. pj_uint8_t *out, int *out_len);
  85. /**
  86. * Decode base64 (URL and Filename Safe) string.
  87. *
  88. * @param input Input string.
  89. * @param out Buffer to store the output. Caller must allocate
  90. * this buffer with the appropriate size.
  91. * @param out_len On entry, it specifies the length of the output buffer.
  92. * Upon return, this will be filled with the actual
  93. * length of the output.
  94. */
  95. PJ_DECL(pj_status_t) pj_base64url_decode(const pj_str_t *input,
  96. pj_uint8_t *out, int *out_len);
  97. /**
  98. * @}
  99. */
  100. PJ_END_DECL
  101. #endif /* __PJLIB_UTIL_BASE64_H__ */