cyr_convert.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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: Kirill Maximov <kir@rus.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdlib.h>
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "php.h"
  25. #include "cyr_convert.h"
  26. #include <stdio.h>
  27. /*****************************************************************************
  28. * This is codetables for different Cyrillic charsets (relative to koi8-r).
  29. * Each table contains data for 128-255 symbols from ASCII table.
  30. * First 256 symbols are for conversion from koi8-r to corresponding charset,
  31. * second 256 symbols are for reverse conversion, from charset to koi8-r.
  32. *
  33. * Here we have the following tables:
  34. * _cyr_win1251 - for windows-1251 charset
  35. * _cyr_iso88595 - for iso8859-5 charset
  36. * _cyr_cp866 - for x-cp866 charset
  37. * _cyr_mac - for x-mac-cyrillic charset
  38. *
  39. *****************************************************************************/
  40. typedef unsigned char _cyr_charset_table[512];
  41. /* {{{ static const _cyr_charset_table _cyr_win1251
  42. */
  43. static const _cyr_charset_table _cyr_win1251 = {
  44. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  45. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  46. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  47. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  48. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  49. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  50. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  51. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  52. 46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,
  53. 46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,
  54. 154,174,190,46,159,189,46,46,179,191,180,157,46,46,156,183,
  55. 46,46,182,166,173,46,46,158,163,152,164,155,46,46,46,167,
  56. 225,226,247,231,228,229,246,250,233,234,235,236,237,238,239,240,
  57. 242,243,244,245,230,232,227,254,251,253,255,249,248,252,224,241,
  58. 193,194,215,199,196,197,214,218,201,202,203,204,205,206,207,208,
  59. 210,211,212,213,198,200,195,222,219,221,223,217,216,220,192,209,
  60. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  61. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  62. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  63. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  64. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  65. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  66. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  67. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  68. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  69. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  70. 32,32,32,184,186,32,179,191,32,32,32,32,32,180,162,32,
  71. 32,32,32,168,170,32,178,175,32,32,32,32,32,165,161,169,
  72. 254,224,225,246,228,229,244,227,245,232,233,234,235,236,237,238,
  73. 239,255,240,241,242,243,230,226,252,251,231,248,253,249,247,250,
  74. 222,192,193,214,196,197,212,195,213,200,201,202,203,204,205,206,
  75. 207,223,208,209,210,211,198,194,220,219,199,216,221,217,215,218,
  76. },
  77. _cyr_cp866 = {
  78. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  79. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  80. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  81. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  82. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  83. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  84. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  85. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  86. 225,226,247,231,228,229,246,250,233,234,235,236,237,238,239,240,
  87. 242,243,244,245,230,232,227,254,251,253,255,249,248,252,224,241,
  88. 193,194,215,199,196,197,214,218,201,202,203,204,205,206,207,208,
  89. 35,35,35,124,124,124,124,43,43,124,124,43,43,43,43,43,
  90. 43,45,45,124,45,43,124,124,43,43,45,45,124,45,43,45,
  91. 45,45,45,43,43,43,43,43,43,43,43,35,35,124,124,35,
  92. 210,211,212,213,198,200,195,222,219,221,223,217,216,220,192,209,
  93. 179,163,180,164,183,167,190,174,32,149,158,32,152,159,148,154,
  94. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  95. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  96. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  97. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  98. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  99. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  100. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  101. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  102. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  103. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  104. 205,186,213,241,243,201,32,245,187,212,211,200,190,32,247,198,
  105. 199,204,181,240,242,185,32,244,203,207,208,202,216,32,246,32,
  106. 238,160,161,230,164,165,228,163,229,168,169,170,171,172,173,174,
  107. 175,239,224,225,226,227,166,162,236,235,167,232,237,233,231,234,
  108. 158,128,129,150,132,133,148,131,149,136,137,138,139,140,141,142,
  109. 143,159,144,145,146,147,134,130,156,155,135,152,157,153,151,154,
  110. },
  111. _cyr_iso88595 = {
  112. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  113. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  114. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  115. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  116. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  117. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  118. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  119. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  120. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  121. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  122. 32,179,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  123. 225,226,247,231,228,229,246,250,233,234,235,236,237,238,239,240,
  124. 242,243,244,245,230,232,227,254,251,253,255,249,248,252,224,241,
  125. 193,194,215,199,196,197,214,218,201,202,203,204,205,206,207,208,
  126. 210,211,212,213,198,200,195,222,219,221,223,217,216,220,192,209,
  127. 32,163,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  128. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  129. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  130. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  131. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  132. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  133. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  134. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  135. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  136. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  137. 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  138. 32,32,32,241,32,32,32,32,32,32,32,32,32,32,32,32,
  139. 32,32,32,161,32,32,32,32,32,32,32,32,32,32,32,32,
  140. 238,208,209,230,212,213,228,211,229,216,217,218,219,220,221,222,
  141. 223,239,224,225,226,227,214,210,236,235,215,232,237,233,231,234,
  142. 206,176,177,198,180,181,196,179,197,184,185,186,187,188,189,190,
  143. 191,207,192,193,194,195,182,178,204,203,183,200,205,201,199,202,
  144. },
  145. _cyr_mac = {
  146. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  147. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  148. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  149. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  150. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  151. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  152. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  153. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  154. 225,226,247,231,228,229,246,250,233,234,235,236,237,238,239,240,
  155. 242,243,244,245,230,232,227,254,251,253,255,249,248,252,224,241,
  156. 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
  157. 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
  158. 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  159. 144,145,146,147,148,149,150,151,152,153,154,155,156,179,163,209,
  160. 193,194,215,199,196,197,214,218,201,202,203,204,205,206,207,208,
  161. 210,211,212,213,198,200,195,222,219,221,223,217,216,220,192,255,
  162. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  163. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
  164. 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
  165. 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  166. 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
  167. 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
  168. 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
  169. 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  170. 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  171. 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
  172. 160,161,162,222,164,165,166,167,168,169,170,171,172,173,174,175,
  173. 176,177,178,221,180,181,182,183,184,185,186,187,188,189,190,191,
  174. 254,224,225,246,228,229,244,227,245,232,233,234,235,236,237,238,
  175. 239,223,240,241,242,243,230,226,252,251,231,248,253,249,247,250,
  176. 158,128,129,150,132,133,148,131,149,136,137,138,139,140,141,142,
  177. 143,159,144,145,146,147,134,130,156,155,135,152,157,153,151,154,
  178. };
  179. /* }}} */
  180. /* {{{ static char * php_convert_cyr_string(unsigned char *str, int length, char from, char to)
  181. * This is the function that performs real in-place conversion of the string
  182. * between charsets.
  183. * Parameters:
  184. * str - string to be converted
  185. * from,to - one-symbol label of source and destination charset
  186. * The following symbols are used as labels:
  187. * k - koi8-r
  188. * w - windows-1251
  189. * i - iso8859-5
  190. * a - x-cp866
  191. * d - x-cp866
  192. * m - x-mac-cyrillic
  193. *****************************************************************************/
  194. static char * php_convert_cyr_string(unsigned char *str, size_t length, char from, char to)
  195. {
  196. const unsigned char *from_table, *to_table;
  197. unsigned char tmp;
  198. size_t i;
  199. from_table = NULL;
  200. to_table = NULL;
  201. switch (toupper((int)(unsigned char)from))
  202. {
  203. case 'W':
  204. from_table = _cyr_win1251;
  205. break;
  206. case 'A':
  207. case 'D':
  208. from_table = _cyr_cp866;
  209. break;
  210. case 'I':
  211. from_table = _cyr_iso88595;
  212. break;
  213. case 'M':
  214. from_table = _cyr_mac;
  215. break;
  216. case 'K':
  217. break;
  218. default:
  219. php_error_docref(NULL, E_WARNING, "Unknown source charset: %c", from);
  220. break;
  221. }
  222. switch (toupper((int)(unsigned char)to))
  223. {
  224. case 'W':
  225. to_table = _cyr_win1251;
  226. break;
  227. case 'A':
  228. case 'D':
  229. to_table = _cyr_cp866;
  230. break;
  231. case 'I':
  232. to_table = _cyr_iso88595;
  233. break;
  234. case 'M':
  235. to_table = _cyr_mac;
  236. break;
  237. case 'K':
  238. break;
  239. default:
  240. php_error_docref(NULL, E_WARNING, "Unknown destination charset: %c", to);
  241. break;
  242. }
  243. if (!str)
  244. return (char *)str;
  245. for (i = 0; i < length; i++) {
  246. tmp = (from_table == NULL)? str[i] : from_table[ str[i] ];
  247. str[i] = (to_table == NULL) ? tmp : to_table[tmp + 256];
  248. }
  249. return (char *)str;
  250. }
  251. /* }}} */
  252. /* {{{ proto string convert_cyr_string(string str, string from, string to)
  253. Convert from one Cyrillic character set to another */
  254. PHP_FUNCTION(convert_cyr_string)
  255. {
  256. char *input, *fr_cs, *to_cs;
  257. size_t input_len, fr_cs_len, to_cs_len;
  258. zend_string *str;
  259. ZEND_PARSE_PARAMETERS_START(3, 3)
  260. Z_PARAM_STRING(input, input_len)
  261. Z_PARAM_STRING(fr_cs, fr_cs_len)
  262. Z_PARAM_STRING(to_cs, to_cs_len)
  263. ZEND_PARSE_PARAMETERS_END();
  264. str = zend_string_init(input, input_len, 0);
  265. php_convert_cyr_string((unsigned char *) ZSTR_VAL(str), ZSTR_LEN(str), fr_cs[0], to_cs[0]);
  266. RETVAL_NEW_STR(str);
  267. }
  268. /* }}} */
  269. /*
  270. * Local variables:
  271. * tab-width: 4
  272. * c-basic-offset: 4
  273. * End:
  274. * vim600: sw=4 ts=4 fdm=marker
  275. * vim<600: sw=4 ts=4
  276. */