zend_multibyte.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Masaki Fujimoto <fujimoto@php.net> |
  16. | Rui Hirokawa <hirokawa@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include "zend.h"
  21. #include "zend_compile.h"
  22. #include "zend_operators.h"
  23. #include "zend_multibyte.h"
  24. #include "zend_ini.h"
  25. static const zend_encoding *dummy_encoding_fetcher(const char *encoding_name TSRMLS_DC)
  26. {
  27. return NULL;
  28. }
  29. static const char *dummy_encoding_name_getter(const zend_encoding *encoding)
  30. {
  31. return (const char*)encoding;
  32. }
  33. static int dummy_encoding_lexer_compatibility_checker(const zend_encoding *encoding)
  34. {
  35. return 0;
  36. }
  37. static const zend_encoding *dummy_encoding_detector(const unsigned char *string, size_t length, const zend_encoding **list, size_t list_size TSRMLS_DC)
  38. {
  39. return NULL;
  40. }
  41. static size_t dummy_encoding_converter(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length, const zend_encoding *encoding_to, const zend_encoding *encoding_from TSRMLS_DC)
  42. {
  43. return (size_t)-1;
  44. }
  45. static int dummy_encoding_list_parser(const char *encoding_list, size_t encoding_list_len, const zend_encoding ***return_list, size_t *return_size, int persistent TSRMLS_DC)
  46. {
  47. *return_list = pemalloc(0, persistent);
  48. *return_size = 0;
  49. return SUCCESS;
  50. }
  51. static const zend_encoding *dummy_internal_encoding_getter(TSRMLS_D)
  52. {
  53. return NULL;
  54. }
  55. static int dummy_internal_encoding_setter(const zend_encoding *encoding TSRMLS_DC)
  56. {
  57. return FAILURE;
  58. }
  59. static zend_multibyte_functions multibyte_functions = {
  60. NULL,
  61. dummy_encoding_fetcher,
  62. dummy_encoding_name_getter,
  63. dummy_encoding_lexer_compatibility_checker,
  64. dummy_encoding_detector,
  65. dummy_encoding_converter,
  66. dummy_encoding_list_parser,
  67. dummy_internal_encoding_getter,
  68. dummy_internal_encoding_setter
  69. };
  70. ZEND_API const zend_encoding *zend_multibyte_encoding_utf32be = (const zend_encoding*)"UTF-32BE";
  71. ZEND_API const zend_encoding *zend_multibyte_encoding_utf32le = (const zend_encoding*)"UTF-32LE";
  72. ZEND_API const zend_encoding *zend_multibyte_encoding_utf16be = (const zend_encoding*)"UTF-16BE";
  73. ZEND_API const zend_encoding *zend_multibyte_encoding_utf16le = (const zend_encoding*)"UTF-32LE";
  74. ZEND_API const zend_encoding *zend_multibyte_encoding_utf8 = (const zend_encoding*)"UTF-8";
  75. ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functions TSRMLS_DC)
  76. {
  77. zend_multibyte_encoding_utf32be = functions->encoding_fetcher("UTF-32BE" TSRMLS_CC);
  78. if (!zend_multibyte_encoding_utf32be) {
  79. return FAILURE;
  80. }
  81. zend_multibyte_encoding_utf32le = functions->encoding_fetcher("UTF-32LE" TSRMLS_CC);
  82. if (!zend_multibyte_encoding_utf32le) {
  83. return FAILURE;
  84. }
  85. zend_multibyte_encoding_utf16be = functions->encoding_fetcher("UTF-16BE" TSRMLS_CC);
  86. if (!zend_multibyte_encoding_utf16be) {
  87. return FAILURE;
  88. }
  89. zend_multibyte_encoding_utf16le = functions->encoding_fetcher("UTF-16LE" TSRMLS_CC);
  90. if (!zend_multibyte_encoding_utf16le) {
  91. return FAILURE;
  92. }
  93. zend_multibyte_encoding_utf8 = functions->encoding_fetcher("UTF-8" TSRMLS_CC);
  94. if (!zend_multibyte_encoding_utf8) {
  95. return FAILURE;
  96. }
  97. multibyte_functions = *functions;
  98. /* As zend_multibyte_set_functions() gets called after ini settings were
  99. * populated, we need to reinitialize script_encoding here.
  100. */
  101. {
  102. const char *value = zend_ini_string("zend.script_encoding", sizeof("zend.script_encoding"), 0);
  103. zend_multibyte_set_script_encoding_by_string(value, strlen(value) TSRMLS_CC);
  104. }
  105. return SUCCESS;
  106. }
  107. ZEND_API const zend_multibyte_functions *zend_multibyte_get_functions(TSRMLS_D)
  108. {
  109. return multibyte_functions.provider_name ? &multibyte_functions: NULL;
  110. }
  111. ZEND_API const zend_encoding *zend_multibyte_fetch_encoding(const char *name TSRMLS_DC)
  112. {
  113. return multibyte_functions.encoding_fetcher(name TSRMLS_CC);
  114. }
  115. ZEND_API const char *zend_multibyte_get_encoding_name(const zend_encoding *encoding)
  116. {
  117. return multibyte_functions.encoding_name_getter(encoding);
  118. }
  119. ZEND_API int zend_multibyte_check_lexer_compatibility(const zend_encoding *encoding)
  120. {
  121. return multibyte_functions.lexer_compatibility_checker(encoding);
  122. }
  123. ZEND_API const zend_encoding *zend_multibyte_encoding_detector(const unsigned char *string, size_t length, const zend_encoding **list, size_t list_size TSRMLS_DC)
  124. {
  125. return multibyte_functions.encoding_detector(string, length, list, list_size TSRMLS_CC);
  126. }
  127. ZEND_API size_t zend_multibyte_encoding_converter(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length, const zend_encoding *encoding_to, const zend_encoding *encoding_from TSRMLS_DC)
  128. {
  129. return multibyte_functions.encoding_converter(to, to_length, from, from_length, encoding_to, encoding_from TSRMLS_CC);
  130. }
  131. ZEND_API int zend_multibyte_parse_encoding_list(const char *encoding_list, size_t encoding_list_len, const zend_encoding ***return_list, size_t *return_size, int persistent TSRMLS_DC)
  132. {
  133. return multibyte_functions.encoding_list_parser(encoding_list, encoding_list_len, return_list, return_size, persistent TSRMLS_CC);
  134. }
  135. ZEND_API const zend_encoding *zend_multibyte_get_internal_encoding(TSRMLS_D)
  136. {
  137. return multibyte_functions.internal_encoding_getter(TSRMLS_C);
  138. }
  139. ZEND_API const zend_encoding *zend_multibyte_get_script_encoding(TSRMLS_D)
  140. {
  141. return LANG_SCNG(script_encoding);
  142. }
  143. ZEND_API int zend_multibyte_set_script_encoding(const zend_encoding **encoding_list, size_t encoding_list_size TSRMLS_DC)
  144. {
  145. if (CG(script_encoding_list)) {
  146. free(CG(script_encoding_list));
  147. }
  148. CG(script_encoding_list) = encoding_list;
  149. CG(script_encoding_list_size) = encoding_list_size;
  150. return SUCCESS;
  151. }
  152. ZEND_API int zend_multibyte_set_internal_encoding(const zend_encoding *encoding TSRMLS_DC)
  153. {
  154. return multibyte_functions.internal_encoding_setter(encoding TSRMLS_CC);
  155. }
  156. ZEND_API int zend_multibyte_set_script_encoding_by_string(const char *new_value, size_t new_value_length TSRMLS_DC)
  157. {
  158. const zend_encoding **list = 0;
  159. size_t size = 0;
  160. if (!new_value) {
  161. zend_multibyte_set_script_encoding(NULL, 0 TSRMLS_CC);
  162. return SUCCESS;
  163. }
  164. if (FAILURE == zend_multibyte_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
  165. return FAILURE;
  166. }
  167. if (size == 0) {
  168. pefree(list, 1);
  169. return FAILURE;
  170. }
  171. if (FAILURE == zend_multibyte_set_script_encoding(list, size TSRMLS_CC)) {
  172. return FAILURE;
  173. }
  174. return SUCCESS;
  175. }
  176. /*
  177. * Local variables:
  178. * tab-width: 4
  179. * c-basic-offset: 4
  180. * End:
  181. * vim600: sw=4 ts=4 tw=78
  182. * vim<600: sw=4 ts=4 tw=78
  183. */