PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/mbstring/php_unicode.c

http://github.com/infusion/PHP
C | 347 lines | 208 code | 55 blank | 84 comment | 60 complexity | 6ca983249cc6eeb568f41823bc822b73 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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: Wez Furlong (wez@thebrainroom.com) |
  16. +----------------------------------------------------------------------+
  17. Based on code from ucdata-2.5, which has the following Copyright:
  18. Copyright 2001 Computing Research Labs, New Mexico State University
  19. Permission is hereby granted, free of charge, to any person obtaining a
  20. copy of this software and associated documentation files (the "Software"),
  21. to deal in the Software without restriction, including without limitation
  22. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. and/or sell copies of the Software, and to permit persons to whom the
  24. Software is furnished to do so, subject to the following conditions:
  25. The above copyright notice and this permission notice shall be included in
  26. all copies or substantial portions of the Software.
  27. */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "php.h"
  32. #include "php_ini.h"
  33. #if HAVE_MBSTRING
  34. /* include case folding data generated from the official UnicodeData.txt file */
  35. #include "mbstring.h"
  36. #include "php_unicode.h"
  37. #include "unicode_data.h"
  38. ZEND_EXTERN_MODULE_GLOBALS(mbstring)
  39. /*
  40. * A simple array of 32-bit masks for lookup.
  41. */
  42. static unsigned long masks32[32] = {
  43. 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020,
  44. 0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  45. 0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000,
  46. 0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  47. 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
  48. 0x40000000, 0x80000000
  49. };
  50. static int prop_lookup(unsigned long code, unsigned long n)
  51. {
  52. long l, r, m;
  53. /*
  54. * There is an extra node on the end of the offsets to allow this routine
  55. * to work right. If the index is 0xffff, then there are no nodes for the
  56. * property.
  57. */
  58. if ((l = _ucprop_offsets[n]) == 0xffff)
  59. return 0;
  60. /*
  61. * Locate the next offset that is not 0xffff. The sentinel at the end of
  62. * the array is the max index value.
  63. */
  64. for (m = 1; n + m < _ucprop_size && _ucprop_offsets[n + m] == 0xffff; m++)
  65. ;
  66. r = _ucprop_offsets[n + m] - 1;
  67. while (l <= r) {
  68. /*
  69. * Determine a "mid" point and adjust to make sure the mid point is at
  70. * the beginning of a range pair.
  71. */
  72. m = (l + r) >> 1;
  73. m -= (m & 1);
  74. if (code > _ucprop_ranges[m + 1])
  75. l = m + 2;
  76. else if (code < _ucprop_ranges[m])
  77. r = m - 2;
  78. else if (code >= _ucprop_ranges[m] && code <= _ucprop_ranges[m + 1])
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. MBSTRING_API int php_unicode_is_prop(unsigned long code, unsigned long mask1,
  84. unsigned long mask2)
  85. {
  86. unsigned long i;
  87. if (mask1 == 0 && mask2 == 0)
  88. return 0;
  89. for (i = 0; mask1 && i < 32; i++) {
  90. if ((mask1 & masks32[i]) && prop_lookup(code, i))
  91. return 1;
  92. }
  93. for (i = 32; mask2 && i < _ucprop_size; i++) {
  94. if ((mask2 & masks32[i & 31]) && prop_lookup(code, i))
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static unsigned long case_lookup(unsigned long code, long l, long r, int field)
  100. {
  101. long m;
  102. /*
  103. * Do the binary search.
  104. */
  105. while (l <= r) {
  106. /*
  107. * Determine a "mid" point and adjust to make sure the mid point is at
  108. * the beginning of a case mapping triple.
  109. */
  110. m = (l + r) >> 1;
  111. m -= (m % 3);
  112. if (code > _uccase_map[m])
  113. l = m + 3;
  114. else if (code < _uccase_map[m])
  115. r = m - 3;
  116. else if (code == _uccase_map[m])
  117. return _uccase_map[m + field];
  118. }
  119. return code;
  120. }
  121. MBSTRING_API unsigned long php_turkish_toupper(unsigned long code, long l, long r, int field)
  122. {
  123. if (code == 0x0069L) {
  124. return 0x0130L;
  125. }
  126. return case_lookup(code, l, r, field);
  127. }
  128. MBSTRING_API unsigned long php_turkish_tolower(unsigned long code, long l, long r, int field)
  129. {
  130. if (code == 0x0049L) {
  131. return 0x0131L;
  132. }
  133. return case_lookup(code, l, r, field);
  134. }
  135. MBSTRING_API unsigned long php_unicode_toupper(unsigned long code, enum mbfl_no_encoding enc TSRMLS_DC)
  136. {
  137. int field;
  138. long l, r;
  139. if (php_unicode_is_upper(code))
  140. return code;
  141. if (php_unicode_is_lower(code)) {
  142. /*
  143. * The character is lower case.
  144. */
  145. field = 2;
  146. l = _uccase_len[0];
  147. r = (l + _uccase_len[1]) - 3;
  148. if (enc == mbfl_no_encoding_8859_9) {
  149. return php_turkish_toupper(code, l, r, field);
  150. }
  151. } else {
  152. /*
  153. * The character is title case.
  154. */
  155. field = 1;
  156. l = _uccase_len[0] + _uccase_len[1];
  157. r = _uccase_size - 3;
  158. }
  159. return case_lookup(code, l, r, field);
  160. }
  161. MBSTRING_API unsigned long php_unicode_tolower(unsigned long code, enum mbfl_no_encoding enc TSRMLS_DC)
  162. {
  163. int field;
  164. long l, r;
  165. if (php_unicode_is_lower(code))
  166. return code;
  167. if (php_unicode_is_upper(code)) {
  168. /*
  169. * The character is upper case.
  170. */
  171. field = 1;
  172. l = 0;
  173. r = _uccase_len[0] - 3;
  174. if (enc == mbfl_no_encoding_8859_9) {
  175. return php_turkish_tolower(code, l, r, field);
  176. }
  177. } else {
  178. /*
  179. * The character is title case.
  180. */
  181. field = 2;
  182. l = _uccase_len[0] + _uccase_len[1];
  183. r = _uccase_size - 3;
  184. }
  185. return case_lookup(code, l, r, field);
  186. }
  187. MBSTRING_API unsigned long php_unicode_totitle(unsigned long code, enum mbfl_no_encoding enc TSRMLS_DC)
  188. {
  189. int field;
  190. long l, r;
  191. if (php_unicode_is_title(code))
  192. return code;
  193. /*
  194. * The offset will always be the same for converting to title case.
  195. */
  196. field = 2;
  197. if (php_unicode_is_upper(code)) {
  198. /*
  199. * The character is upper case.
  200. */
  201. l = 0;
  202. r = _uccase_len[0] - 3;
  203. } else {
  204. /*
  205. * The character is lower case.
  206. */
  207. l = _uccase_len[0];
  208. r = (l + _uccase_len[1]) - 3;
  209. }
  210. return case_lookup(code, l, r, field);
  211. }
  212. #define BE_ARY_TO_UINT32(ptr) (\
  213. ((unsigned char*)(ptr))[0]<<24 |\
  214. ((unsigned char*)(ptr))[1]<<16 |\
  215. ((unsigned char*)(ptr))[2]<< 8 |\
  216. ((unsigned char*)(ptr))[3] )
  217. #define UINT32_TO_BE_ARY(ptr,val) { \
  218. unsigned int v = val; \
  219. ((unsigned char*)(ptr))[0] = (v>>24) & 0xff,\
  220. ((unsigned char*)(ptr))[1] = (v>>16) & 0xff,\
  221. ((unsigned char*)(ptr))[2] = (v>> 8) & 0xff,\
  222. ((unsigned char*)(ptr))[3] = (v ) & 0xff;\
  223. }
  224. MBSTRING_API char *php_unicode_convert_case(int case_mode, const char *srcstr, size_t srclen, size_t *ret_len,
  225. const char *src_encoding TSRMLS_DC)
  226. {
  227. char *unicode, *newstr;
  228. size_t unicode_len;
  229. unsigned char *unicode_ptr;
  230. size_t i;
  231. enum mbfl_no_encoding _src_encoding = mbfl_name2no_encoding(src_encoding);
  232. if (_src_encoding == mbfl_no_encoding_invalid) {
  233. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", src_encoding);
  234. return NULL;
  235. }
  236. unicode = php_mb_convert_encoding(srcstr, srclen, "UCS-4BE", src_encoding, &unicode_len TSRMLS_CC);
  237. if (unicode == NULL)
  238. return NULL;
  239. unicode_ptr = (unsigned char *)unicode;
  240. switch(case_mode) {
  241. case PHP_UNICODE_CASE_UPPER:
  242. for (i = 0; i < unicode_len; i+=4) {
  243. UINT32_TO_BE_ARY(&unicode_ptr[i],
  244. php_unicode_toupper(BE_ARY_TO_UINT32(&unicode_ptr[i]), _src_encoding TSRMLS_CC));
  245. }
  246. break;
  247. case PHP_UNICODE_CASE_LOWER:
  248. for (i = 0; i < unicode_len; i+=4) {
  249. UINT32_TO_BE_ARY(&unicode_ptr[i],
  250. php_unicode_tolower(BE_ARY_TO_UINT32(&unicode_ptr[i]), _src_encoding TSRMLS_CC));
  251. }
  252. break;
  253. case PHP_UNICODE_CASE_TITLE: {
  254. int mode = 0;
  255. for (i = 0; i < unicode_len; i+=4) {
  256. int res = php_unicode_is_prop(
  257. BE_ARY_TO_UINT32(&unicode_ptr[i]),
  258. UC_MN|UC_ME|UC_CF|UC_LM|UC_SK|UC_LU|UC_LL|UC_LT|UC_PO|UC_OS, 0);
  259. if (mode) {
  260. if (res) {
  261. UINT32_TO_BE_ARY(&unicode_ptr[i],
  262. php_unicode_tolower(BE_ARY_TO_UINT32(&unicode_ptr[i]), _src_encoding TSRMLS_CC));
  263. } else {
  264. mode = 0;
  265. }
  266. } else {
  267. if (res) {
  268. mode = 1;
  269. UINT32_TO_BE_ARY(&unicode_ptr[i],
  270. php_unicode_totitle(BE_ARY_TO_UINT32(&unicode_ptr[i]), _src_encoding TSRMLS_CC));
  271. }
  272. }
  273. }
  274. } break;
  275. }
  276. newstr = php_mb_convert_encoding(unicode, unicode_len, src_encoding, "UCS-4BE", ret_len TSRMLS_CC);
  277. efree(unicode);
  278. return newstr;
  279. }
  280. #endif /* HAVE_MBSTRING */
  281. /*
  282. * Local variables:
  283. * tab-width: 4
  284. * c-basic-offset: 4
  285. * End:
  286. * vim600: sw=4 ts=4 fdm=marker
  287. * vim<600: sw=4 ts=4
  288. */