recode.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Kristian Koehntopp <kris@koehntopp.de> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* {{{ includes & prototypes */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_streams.h"
  24. #if HAVE_LIBRECODE
  25. /* For recode 3.5 */
  26. #if HAVE_BROKEN_RECODE
  27. extern char *program_name;
  28. char *program_name = "php";
  29. #endif
  30. #ifdef HAVE_STDBOOL_H
  31. # include <stdbool.h>
  32. #else
  33. typedef enum {false = 0, true = 1} bool;
  34. #endif
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #include <recode.h>
  39. #include "php_recode.h"
  40. #include "ext/standard/info.h"
  41. #include "ext/standard/file.h"
  42. #include "ext/standard/php_string.h"
  43. /* }}} */
  44. ZEND_BEGIN_MODULE_GLOBALS(recode)
  45. RECODE_OUTER outer;
  46. ZEND_END_MODULE_GLOBALS(recode)
  47. #ifdef ZTS
  48. # define ReSG(v) TSRMG(recode_globals_id, zend_recode_globals *, v)
  49. #else
  50. # define ReSG(v) (recode_globals.v)
  51. #endif
  52. ZEND_DECLARE_MODULE_GLOBALS(recode)
  53. static PHP_GINIT_FUNCTION(recode);
  54. /* {{{ arginfo */
  55. ZEND_BEGIN_ARG_INFO_EX(arginfo_recode_string, 0, 0, 2)
  56. ZEND_ARG_INFO(0, request)
  57. ZEND_ARG_INFO(0, str)
  58. ZEND_END_ARG_INFO()
  59. ZEND_BEGIN_ARG_INFO_EX(arginfo_recode_file, 0, 0, 3)
  60. ZEND_ARG_INFO(0, request)
  61. ZEND_ARG_INFO(0, input)
  62. ZEND_ARG_INFO(0, output)
  63. ZEND_END_ARG_INFO()
  64. /* }}} */
  65. /* {{{ module stuff */
  66. static const zend_function_entry php_recode_functions[] = {
  67. PHP_FE(recode_string, arginfo_recode_string)
  68. PHP_FE(recode_file, arginfo_recode_file)
  69. PHP_FALIAS(recode, recode_string, arginfo_recode_string)
  70. PHP_FE_END
  71. }; /* }}} */
  72. zend_module_entry recode_module_entry = {
  73. STANDARD_MODULE_HEADER,
  74. "recode",
  75. php_recode_functions,
  76. PHP_MINIT(recode),
  77. PHP_MSHUTDOWN(recode),
  78. NULL,
  79. NULL,
  80. PHP_MINFO(recode),
  81. PHP_RECODE_VERSION,
  82. PHP_MODULE_GLOBALS(recode),
  83. PHP_GINIT(recode),
  84. NULL,
  85. NULL,
  86. STANDARD_MODULE_PROPERTIES_EX
  87. };
  88. #ifdef COMPILE_DL_RECODE
  89. ZEND_GET_MODULE(recode)
  90. #endif
  91. static PHP_GINIT_FUNCTION(recode)
  92. {
  93. recode_globals->outer = NULL;
  94. }
  95. PHP_MINIT_FUNCTION(recode)
  96. {
  97. ReSG(outer) = recode_new_outer(false);
  98. if (ReSG(outer) == NULL) {
  99. return FAILURE;
  100. }
  101. return SUCCESS;
  102. }
  103. PHP_MSHUTDOWN_FUNCTION(recode)
  104. {
  105. if (ReSG(outer)) {
  106. recode_delete_outer(ReSG(outer));
  107. }
  108. return SUCCESS;
  109. }
  110. PHP_MINFO_FUNCTION(recode)
  111. {
  112. php_info_print_table_start();
  113. php_info_print_table_row(2, "Recode Support", "enabled");
  114. php_info_print_table_end();
  115. }
  116. /* {{{ proto string recode_string(string request, string str)
  117. Recode string str according to request string */
  118. PHP_FUNCTION(recode_string)
  119. {
  120. RECODE_REQUEST request = NULL;
  121. char *r = NULL;
  122. size_t r_len = 0, r_alen = 0;
  123. size_t req_len, str_len;
  124. char *req, *str;
  125. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &req, &req_len, &str, &str_len) == FAILURE) {
  126. return;
  127. }
  128. request = recode_new_request(ReSG(outer));
  129. if (request == NULL) {
  130. php_error_docref(NULL, E_WARNING, "Cannot allocate request structure");
  131. RETURN_FALSE;
  132. }
  133. if (!recode_scan_request(request, req)) {
  134. php_error_docref(NULL, E_WARNING, "Illegal recode request '%s'", req);
  135. goto error_exit;
  136. }
  137. recode_buffer_to_buffer(request, str, str_len, &r, &r_len, &r_alen);
  138. if (!r) {
  139. php_error_docref(NULL, E_WARNING, "Recoding failed.");
  140. error_exit:
  141. RETVAL_FALSE;
  142. } else {
  143. RETVAL_STRINGL(r, r_len);
  144. free(r);
  145. }
  146. recode_delete_request(request);
  147. return;
  148. }
  149. /* }}} */
  150. /* {{{ proto bool recode_file(string request, resource input, resource output)
  151. Recode file input into file output according to request */
  152. PHP_FUNCTION(recode_file)
  153. {
  154. RECODE_REQUEST request = NULL;
  155. char *req;
  156. size_t req_len;
  157. zval *input, *output;
  158. php_stream *instream, *outstream;
  159. FILE *in_fp, *out_fp;
  160. if (zend_parse_parameters(ZEND_NUM_ARGS(), "srr", &req, &req_len, &input, &output) == FAILURE) {
  161. return;
  162. }
  163. php_stream_from_zval(instream, input);
  164. php_stream_from_zval(outstream, output);
  165. if (FAILURE == php_stream_cast(instream, PHP_STREAM_AS_STDIO, (void**)&in_fp, REPORT_ERRORS)) {
  166. RETURN_FALSE;
  167. }
  168. if (FAILURE == php_stream_cast(outstream, PHP_STREAM_AS_STDIO, (void**)&out_fp, REPORT_ERRORS)) {
  169. RETURN_FALSE;
  170. }
  171. request = recode_new_request(ReSG(outer));
  172. if (request == NULL) {
  173. php_error_docref(NULL, E_WARNING, "Cannot allocate request structure");
  174. RETURN_FALSE;
  175. }
  176. if (!recode_scan_request(request, req)) {
  177. php_error_docref(NULL, E_WARNING, "Illegal recode request '%s'", req);
  178. goto error_exit;
  179. }
  180. if (!recode_file_to_file(request, in_fp, out_fp)) {
  181. php_error_docref(NULL, E_WARNING, "Recoding failed.");
  182. goto error_exit;
  183. }
  184. recode_delete_request(request);
  185. RETURN_TRUE;
  186. error_exit:
  187. recode_delete_request(request);
  188. RETURN_FALSE;
  189. }
  190. /* }}} */
  191. #endif
  192. /*
  193. * Local variables:
  194. * tab-width: 4
  195. * c-basic-offset: 4
  196. * End:
  197. */