func_base64.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "asterisk/module.h"
  28. #include "asterisk/pbx.h" /* function register/unregister */
  29. #include "asterisk/utils.h"
  30. #include "asterisk/strings.h"
  31. /*** DOCUMENTATION
  32. <function name="BASE64_ENCODE" language="en_US">
  33. <synopsis>
  34. Encode a string in base64.
  35. </synopsis>
  36. <syntax>
  37. <parameter name="string" required="true">
  38. <para>Input string</para>
  39. </parameter>
  40. </syntax>
  41. <description>
  42. <para>Returns the base64 string.</para>
  43. </description>
  44. <see-also>
  45. <ref type="function">BASE64_DECODE</ref>
  46. <ref type="function">AES_DECRYPT</ref>
  47. <ref type="function">AES_ENCRYPT</ref>
  48. </see-also>
  49. </function>
  50. <function name="BASE64_DECODE" language="en_US">
  51. <synopsis>
  52. Decode a base64 string.
  53. </synopsis>
  54. <syntax>
  55. <parameter name="string" required="true">
  56. <para>Input string.</para>
  57. </parameter>
  58. </syntax>
  59. <description>
  60. <para>Returns the plain text string.</para>
  61. </description>
  62. <see-also>
  63. <ref type="function">BASE64_ENCODE</ref>
  64. <ref type="function">AES_DECRYPT</ref>
  65. <ref type="function">AES_ENCRYPT</ref>
  66. </see-also>
  67. </function>
  68. ***/
  69. static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
  70. char *buf, struct ast_str **str, ssize_t len)
  71. {
  72. if (ast_strlen_zero(data)) {
  73. ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
  74. return -1;
  75. }
  76. if (cmd[7] == 'E') {
  77. if (buf) {
  78. ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
  79. } else {
  80. if (len >= 0) {
  81. ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
  82. }
  83. ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
  84. ast_str_update(*str);
  85. }
  86. } else {
  87. int decoded_len;
  88. if (buf) {
  89. decoded_len = ast_base64decode((unsigned char *) buf, data, len);
  90. /* add a terminating null at the end of buf, or at the
  91. * end of our decoded string, which ever is less */
  92. buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
  93. } else {
  94. if (len >= 0) {
  95. ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
  96. }
  97. decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
  98. if (len)
  99. /* add a terminating null at the end of our
  100. * buffer, or at the end of our decoded string,
  101. * which ever is less */
  102. ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
  103. else
  104. /* space for the null is allocated above */
  105. ast_str_buffer(*str)[decoded_len] = '\0';
  106. ast_str_update(*str);
  107. }
  108. }
  109. return 0;
  110. }
  111. static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
  112. char *buf, size_t len)
  113. {
  114. return base64_helper(chan, cmd, data, buf, NULL, len);
  115. }
  116. static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
  117. struct ast_str **buf, ssize_t len)
  118. {
  119. return base64_helper(chan, cmd, data, NULL, buf, len);
  120. }
  121. static struct ast_custom_function base64_encode_function = {
  122. .name = "BASE64_ENCODE",
  123. .read = base64_buf_helper,
  124. .read2 = base64_str_helper,
  125. };
  126. static struct ast_custom_function base64_decode_function = {
  127. .name = "BASE64_DECODE",
  128. .read = base64_buf_helper,
  129. .read2 = base64_str_helper,
  130. };
  131. static int unload_module(void)
  132. {
  133. return ast_custom_function_unregister(&base64_encode_function) |
  134. ast_custom_function_unregister(&base64_decode_function);
  135. }
  136. static int load_module(void)
  137. {
  138. return ast_custom_function_register(&base64_encode_function) |
  139. ast_custom_function_register(&base64_decode_function);
  140. }
  141. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");