func_base64.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005 - 2006, Digium, Inc.
  5. * Copyright (C) 2005, Claude Patry
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief Use the base64 as functions
  20. *
  21. * \ingroup functions
  22. */
  23. /*** MODULEINFO
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/module.h"
  29. #include "asterisk/pbx.h" /* function register/unregister */
  30. #include "asterisk/utils.h"
  31. #include "asterisk/strings.h"
  32. /*** DOCUMENTATION
  33. <function name="BASE64_ENCODE" language="en_US">
  34. <synopsis>
  35. Encode a string in base64.
  36. </synopsis>
  37. <syntax>
  38. <parameter name="string" required="true">
  39. <para>Input string</para>
  40. </parameter>
  41. </syntax>
  42. <description>
  43. <para>Returns the base64 string.</para>
  44. </description>
  45. <see-also>
  46. <ref type="function">BASE64_DECODE</ref>
  47. <ref type="function">AES_DECRYPT</ref>
  48. <ref type="function">AES_ENCRYPT</ref>
  49. </see-also>
  50. </function>
  51. <function name="BASE64_DECODE" language="en_US">
  52. <synopsis>
  53. Decode a base64 string.
  54. </synopsis>
  55. <syntax>
  56. <parameter name="string" required="true">
  57. <para>Input string.</para>
  58. </parameter>
  59. </syntax>
  60. <description>
  61. <para>Returns the plain text string.</para>
  62. </description>
  63. <see-also>
  64. <ref type="function">BASE64_ENCODE</ref>
  65. <ref type="function">AES_DECRYPT</ref>
  66. <ref type="function">AES_ENCRYPT</ref>
  67. </see-also>
  68. </function>
  69. ***/
  70. static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
  71. char *buf, struct ast_str **str, ssize_t len)
  72. {
  73. if (ast_strlen_zero(data)) {
  74. ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
  75. return -1;
  76. }
  77. if (cmd[7] == 'E') {
  78. if (buf) {
  79. ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
  80. } else {
  81. if (len >= 0) {
  82. ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
  83. }
  84. ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
  85. ast_str_update(*str);
  86. }
  87. } else {
  88. int decoded_len;
  89. if (buf) {
  90. decoded_len = ast_base64decode((unsigned char *) buf, data, len);
  91. /* add a terminating null at the end of buf, or at the
  92. * end of our decoded string, which ever is less */
  93. buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
  94. } else {
  95. if (len >= 0) {
  96. ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
  97. }
  98. decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
  99. if (len)
  100. /* add a terminating null at the end of our
  101. * buffer, or at the end of our decoded string,
  102. * which ever is less */
  103. ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
  104. else
  105. /* space for the null is allocated above */
  106. ast_str_buffer(*str)[decoded_len] = '\0';
  107. ast_str_update(*str);
  108. }
  109. }
  110. return 0;
  111. }
  112. static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
  113. char *buf, size_t len)
  114. {
  115. return base64_helper(chan, cmd, data, buf, NULL, len);
  116. }
  117. static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
  118. struct ast_str **buf, ssize_t len)
  119. {
  120. return base64_helper(chan, cmd, data, NULL, buf, len);
  121. }
  122. static struct ast_custom_function base64_encode_function = {
  123. .name = "BASE64_ENCODE",
  124. .read = base64_buf_helper,
  125. .read2 = base64_str_helper,
  126. };
  127. static struct ast_custom_function base64_decode_function = {
  128. .name = "BASE64_DECODE",
  129. .read = base64_buf_helper,
  130. .read2 = base64_str_helper,
  131. };
  132. static int unload_module(void)
  133. {
  134. return ast_custom_function_unregister(&base64_encode_function) |
  135. ast_custom_function_unregister(&base64_decode_function);
  136. }
  137. static int load_module(void)
  138. {
  139. return ast_custom_function_register(&base64_encode_function) |
  140. ast_custom_function_register(&base64_decode_function);
  141. }
  142. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");