cp1255.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 1999-2001, 2004, 2016 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either version 2.1
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * CP1255
  21. */
  22. #include "flushwc.h"
  23. /* Combining characters used in Hebrew encoding CP1255. */
  24. /* Relevant combining characters:
  25. 0x05b4, 0x05b7, 0x05b8, 0x05b9, 0x05bc, 0x05bf, 0x05c1, 0x05c2. */
  26. /* Composition tables for each of the relevant combining characters. */
  27. static const struct { unsigned short base; unsigned short composed; } cp1255_comp_table_data[] = {
  28. #define cp1255_comp_table05b4_idx 0
  29. #define cp1255_comp_table05b4_len 1
  30. { 0x05D9, 0xFB1D },
  31. #define cp1255_comp_table05b7_idx (cp1255_comp_table05b4_idx+cp1255_comp_table05b4_len)
  32. #define cp1255_comp_table05b7_len 2
  33. { 0x05D0, 0xFB2E },
  34. { 0x05F2, 0xFB1F },
  35. #define cp1255_comp_table05b8_idx (cp1255_comp_table05b7_idx+cp1255_comp_table05b7_len)
  36. #define cp1255_comp_table05b8_len 1
  37. { 0x05D0, 0xFB2F },
  38. #define cp1255_comp_table05b9_idx (cp1255_comp_table05b8_idx+cp1255_comp_table05b8_len)
  39. #define cp1255_comp_table05b9_len 1
  40. { 0x05D5, 0xFB4B },
  41. #define cp1255_comp_table05bc_idx (cp1255_comp_table05b9_idx+cp1255_comp_table05b9_len)
  42. #define cp1255_comp_table05bc_len 24
  43. { 0x05D0, 0xFB30 },
  44. { 0x05D1, 0xFB31 },
  45. { 0x05D2, 0xFB32 },
  46. { 0x05D3, 0xFB33 },
  47. { 0x05D4, 0xFB34 },
  48. { 0x05D5, 0xFB35 },
  49. { 0x05D6, 0xFB36 },
  50. { 0x05D8, 0xFB38 },
  51. { 0x05D9, 0xFB39 },
  52. { 0x05DA, 0xFB3A },
  53. { 0x05DB, 0xFB3B },
  54. { 0x05DC, 0xFB3C },
  55. { 0x05DE, 0xFB3E },
  56. { 0x05E0, 0xFB40 },
  57. { 0x05E1, 0xFB41 },
  58. { 0x05E3, 0xFB43 },
  59. { 0x05E4, 0xFB44 },
  60. { 0x05E6, 0xFB46 },
  61. { 0x05E7, 0xFB47 },
  62. { 0x05E8, 0xFB48 },
  63. { 0x05E9, 0xFB49 },
  64. { 0x05EA, 0xFB4A },
  65. { 0xFB2A, 0xFB2C },
  66. { 0xFB2B, 0xFB2D },
  67. #define cp1255_comp_table05bf_idx (cp1255_comp_table05bc_idx+cp1255_comp_table05bc_len)
  68. #define cp1255_comp_table05bf_len 3
  69. { 0x05D1, 0xFB4C },
  70. { 0x05DB, 0xFB4D },
  71. { 0x05E4, 0xFB4E },
  72. #define cp1255_comp_table05c1_idx (cp1255_comp_table05bf_idx+cp1255_comp_table05bf_len)
  73. #define cp1255_comp_table05c1_len 2
  74. { 0x05E9, 0xFB2A },
  75. { 0xFB49, 0xFB2C },
  76. #define cp1255_comp_table05c2_idx (cp1255_comp_table05c1_idx+cp1255_comp_table05c1_len)
  77. #define cp1255_comp_table05c2_len 2
  78. { 0x05E9, 0xFB2B },
  79. { 0xFB49, 0xFB2D },
  80. };
  81. static const struct { unsigned int len; unsigned int idx; } cp1255_comp_table[] = {
  82. { cp1255_comp_table05b4_len, cp1255_comp_table05b4_idx },
  83. { cp1255_comp_table05b7_len, cp1255_comp_table05b7_idx },
  84. { cp1255_comp_table05b8_len, cp1255_comp_table05b8_idx },
  85. { cp1255_comp_table05b9_len, cp1255_comp_table05b9_idx },
  86. { cp1255_comp_table05bc_len, cp1255_comp_table05bc_idx },
  87. { cp1255_comp_table05bf_len, cp1255_comp_table05bf_idx },
  88. { cp1255_comp_table05c1_len, cp1255_comp_table05c1_idx },
  89. { cp1255_comp_table05c2_len, cp1255_comp_table05c2_idx },
  90. };
  91. /* Decomposition table for the relevant Unicode characters. */
  92. struct cp1255_decomp { unsigned short composed; unsigned short base; int comb1 : 8; signed int comb2 : 8; };
  93. static const struct cp1255_decomp cp1255_decomp_table[] = {
  94. { 0xFB1D, 0x05D9, 0, -1 },
  95. { 0xFB1F, 0x05F2, 1, -1 },
  96. { 0xFB2A, 0x05E9, 6, -1 },
  97. { 0xFB2B, 0x05E9, 7, -1 },
  98. { 0xFB2C, 0x05E9, 4, 6 },
  99. { 0xFB2D, 0x05E9, 4, 7 },
  100. { 0xFB2E, 0x05D0, 1, -1 },
  101. { 0xFB2F, 0x05D0, 2, -1 },
  102. { 0xFB30, 0x05D0, 4, -1 },
  103. { 0xFB31, 0x05D1, 4, -1 },
  104. { 0xFB32, 0x05D2, 4, -1 },
  105. { 0xFB33, 0x05D3, 4, -1 },
  106. { 0xFB34, 0x05D4, 4, -1 },
  107. { 0xFB35, 0x05D5, 4, -1 },
  108. { 0xFB36, 0x05D6, 4, -1 },
  109. { 0xFB38, 0x05D8, 4, -1 },
  110. { 0xFB39, 0x05D9, 4, -1 },
  111. { 0xFB3A, 0x05DA, 4, -1 },
  112. { 0xFB3B, 0x05DB, 4, -1 },
  113. { 0xFB3C, 0x05DC, 4, -1 },
  114. { 0xFB3E, 0x05DE, 4, -1 },
  115. { 0xFB40, 0x05E0, 4, -1 },
  116. { 0xFB41, 0x05E1, 4, -1 },
  117. { 0xFB43, 0x05E3, 4, -1 },
  118. { 0xFB44, 0x05E4, 4, -1 },
  119. { 0xFB46, 0x05E6, 4, -1 },
  120. { 0xFB47, 0x05E7, 4, -1 },
  121. { 0xFB48, 0x05E8, 4, -1 },
  122. { 0xFB49, 0x05E9, 4, -1 },
  123. { 0xFB4A, 0x05EA, 4, -1 },
  124. { 0xFB4B, 0x05D5, 3, -1 },
  125. { 0xFB4C, 0x05D1, 5, -1 },
  126. { 0xFB4D, 0x05DB, 5, -1 },
  127. { 0xFB4E, 0x05E4, 5, -1 },
  128. };
  129. static const unsigned char cp1255_comb_table[] = {
  130. 0xc4, 0xc7, 0xc8, 0xc9, 0xcc, 0xcf, 0xd1, 0xd2,
  131. };
  132. static const unsigned short cp1255_2uni[128] = {
  133. /* 0x80 */
  134. 0x20ac, 0xfffd, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  135. 0x02c6, 0x2030, 0xfffd, 0x2039, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
  136. /* 0x90 */
  137. 0xfffd, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  138. 0x02dc, 0x2122, 0xfffd, 0x203a, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
  139. /* 0xa0 */
  140. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x20aa, 0x00a5, 0x00a6, 0x00a7,
  141. 0x00a8, 0x00a9, 0x00d7, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  142. /* 0xb0 */
  143. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  144. 0x00b8, 0x00b9, 0x00f7, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
  145. /* 0xc0 */
  146. 0x05b0, 0x05b1, 0x05b2, 0x05b3, 0x05b4, 0x05b5, 0x05b6, 0x05b7,
  147. 0x05b8, 0x05b9, 0x05ba, 0x05bb, 0x05bc, 0x05bd, 0x05be, 0x05bf,
  148. /* 0xd0 */
  149. 0x05c0, 0x05c1, 0x05c2, 0x05c3, 0x05f0, 0x05f1, 0x05f2, 0x05f3,
  150. 0x05f4, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
  151. /* 0xe0 */
  152. 0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7,
  153. 0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df,
  154. /* 0xf0 */
  155. 0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7,
  156. 0x05e8, 0x05e9, 0x05ea, 0xfffd, 0xfffd, 0x200e, 0x200f, 0xfffd,
  157. };
  158. /* In the CP1255 to Unicode direction, the state contains a buffered
  159. character, or 0 if none. */
  160. static int
  161. cp1255_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  162. {
  163. unsigned char c = *s;
  164. unsigned short wc;
  165. unsigned short last_wc;
  166. if (c < 0x80) {
  167. wc = c;
  168. } else {
  169. wc = cp1255_2uni[c-0x80];
  170. if (wc == 0xfffd)
  171. return RET_ILSEQ;
  172. }
  173. last_wc = conv->istate;
  174. if (last_wc) {
  175. if (wc >= 0x05b0 && wc < 0x05c5) {
  176. /* See whether last_wc and wc can be combined. */
  177. unsigned int k;
  178. unsigned int i1, i2;
  179. switch (wc) {
  180. case 0x05b4: k = 0; break;
  181. case 0x05b7: k = 1; break;
  182. case 0x05b8: k = 2; break;
  183. case 0x05b9: k = 3; break;
  184. case 0x05bc: k = 4; break;
  185. case 0x05bf: k = 5; break;
  186. case 0x05c1: k = 6; break;
  187. case 0x05c2: k = 7; break;
  188. default: goto not_combining;
  189. }
  190. i1 = cp1255_comp_table[k].idx;
  191. i2 = i1 + cp1255_comp_table[k].len-1;
  192. if (last_wc >= cp1255_comp_table_data[i1].base
  193. && last_wc <= cp1255_comp_table_data[i2].base) {
  194. unsigned int i;
  195. for (;;) {
  196. i = (i1+i2)>>1;
  197. if (last_wc == cp1255_comp_table_data[i].base)
  198. break;
  199. if (last_wc < cp1255_comp_table_data[i].base) {
  200. if (i1 == i)
  201. goto not_combining;
  202. i2 = i;
  203. } else {
  204. if (i1 != i)
  205. i1 = i;
  206. else {
  207. i = i2;
  208. if (last_wc == cp1255_comp_table_data[i].base)
  209. break;
  210. goto not_combining;
  211. }
  212. }
  213. }
  214. last_wc = cp1255_comp_table_data[i].composed;
  215. if (last_wc == 0xfb2a || last_wc == 0xfb2b || last_wc == 0xfb49) {
  216. /* Buffer the combined character. */
  217. conv->istate = last_wc;
  218. return RET_TOOFEW(1);
  219. } else {
  220. /* Output the combined character. */
  221. conv->istate = 0;
  222. *pwc = (ucs4_t) last_wc;
  223. return 1;
  224. }
  225. }
  226. }
  227. not_combining:
  228. /* Output the buffered character. */
  229. conv->istate = 0;
  230. *pwc = (ucs4_t) last_wc;
  231. return 0; /* Don't advance the input pointer. */
  232. }
  233. if ((wc >= 0x05d0 && wc <= 0x05ea && ((0x07db5f7f >> (wc - 0x05d0)) & 1))
  234. || wc == 0x05f2) {
  235. /* wc is a possible match in cp1255_comp_table_data. Buffer it. */
  236. conv->istate = wc;
  237. return RET_TOOFEW(1);
  238. } else {
  239. /* Output wc immediately. */
  240. *pwc = (ucs4_t) wc;
  241. return 1;
  242. }
  243. }
  244. #define cp1255_flushwc normal_flushwc
  245. static const unsigned char cp1255_page00[88] = {
  246. 0xa0, 0xa1, 0xa2, 0xa3, 0x00, 0xa5, 0xa6, 0xa7, /* 0xa0-0xa7 */
  247. 0xa8, 0xa9, 0x00, 0xab, 0xac, 0xad, 0xae, 0xaf, /* 0xa8-0xaf */
  248. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* 0xb0-0xb7 */
  249. 0xb8, 0xb9, 0x00, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* 0xb8-0xbf */
  250. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0-0xc7 */
  251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
  252. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, /* 0xd0-0xd7 */
  253. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd8-0xdf */
  254. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0-0xe7 */
  255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe8-0xef */
  256. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, /* 0xf0-0xf7 */
  257. };
  258. static const unsigned char cp1255_page02[32] = {
  259. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, /* 0xc0-0xc7 */
  260. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
  261. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0-0xd7 */
  262. 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, /* 0xd8-0xdf */
  263. };
  264. static const unsigned char cp1255_page05[72] = {
  265. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0xb0-0xb7 */
  266. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0xb8-0xbf */
  267. 0xd0, 0xd1, 0xd2, 0xd3, 0x00, 0x00, 0x00, 0x00, /* 0xc0-0xc7 */
  268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
  269. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 0xd0-0xd7 */
  270. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* 0xd8-0xdf */
  271. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 0xe0-0xe7 */
  272. 0xf8, 0xf9, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe8-0xef */
  273. 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0x00, 0x00, 0x00, /* 0xf0-0xf7 */
  274. };
  275. static const unsigned char cp1255_page20[56] = {
  276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xfe, /* 0x08-0x0f */
  277. 0x00, 0x00, 0x00, 0x96, 0x97, 0x00, 0x00, 0x00, /* 0x10-0x17 */
  278. 0x91, 0x92, 0x82, 0x00, 0x93, 0x94, 0x84, 0x00, /* 0x18-0x1f */
  279. 0x86, 0x87, 0x95, 0x00, 0x00, 0x00, 0x85, 0x00, /* 0x20-0x27 */
  280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x28-0x2f */
  281. 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30-0x37 */
  282. 0x00, 0x8b, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x38-0x3f */
  283. };
  284. static int
  285. cp1255_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  286. {
  287. unsigned char c = 0;
  288. if (wc < 0x0080) {
  289. *r = wc;
  290. return 1;
  291. }
  292. else if (wc >= 0x00a0 && wc < 0x00f8)
  293. c = cp1255_page00[wc-0x00a0];
  294. else if (wc == 0x0192)
  295. c = 0x83;
  296. else if (wc >= 0x02c0 && wc < 0x02e0)
  297. c = cp1255_page02[wc-0x02c0];
  298. else if (wc >= 0x05b0 && wc < 0x05f8)
  299. c = cp1255_page05[wc-0x05b0];
  300. else if (wc >= 0x2008 && wc < 0x2040)
  301. c = cp1255_page20[wc-0x2008];
  302. else if (wc == 0x20aa)
  303. c = 0xa4;
  304. else if (wc == 0x20ac)
  305. c = 0x80;
  306. else if (wc == 0x2122)
  307. c = 0x99;
  308. if (c != 0) {
  309. *r = c;
  310. return 1;
  311. }
  312. /* Try canonical decomposition. */
  313. {
  314. /* Binary search through cp1255_decomp_table. */
  315. unsigned int i1 = 0;
  316. unsigned int i2 = sizeof(cp1255_decomp_table)/sizeof(cp1255_decomp_table[0])-1;
  317. if (wc >= cp1255_decomp_table[i1].composed
  318. && wc <= cp1255_decomp_table[i2].composed) {
  319. unsigned int i;
  320. for (;;) {
  321. /* Here i2 - i1 > 0. */
  322. i = (i1+i2)>>1;
  323. if (wc == cp1255_decomp_table[i].composed)
  324. break;
  325. if (wc < cp1255_decomp_table[i].composed) {
  326. if (i1 == i)
  327. return RET_ILUNI;
  328. /* Here i1 < i < i2. */
  329. i2 = i;
  330. } else {
  331. /* Here i1 <= i < i2. */
  332. if (i1 != i)
  333. i1 = i;
  334. else {
  335. /* Here i2 - i1 = 1. */
  336. i = i2;
  337. if (wc == cp1255_decomp_table[i].composed)
  338. break;
  339. else
  340. return RET_ILUNI;
  341. }
  342. }
  343. }
  344. /* Found a canonical decomposition. */
  345. wc = cp1255_decomp_table[i].base;
  346. /* wc is one of 0x05d0..0x05d6, 0x05d8..0x05dc, 0x05de, 0x05e0..0x05e1,
  347. 0x05e3..0x05e4, 0x05e6..0x05ea, 0x05f2. */
  348. c = cp1255_page05[wc-0x05b0];
  349. if (cp1255_decomp_table[i].comb2 < 0) {
  350. if (n < 2)
  351. return RET_TOOSMALL;
  352. r[0] = c;
  353. r[1] = cp1255_comb_table[cp1255_decomp_table[i].comb1];
  354. return 2;
  355. } else {
  356. if (n < 3)
  357. return RET_TOOSMALL;
  358. r[0] = c;
  359. r[1] = cp1255_comb_table[cp1255_decomp_table[i].comb1];
  360. r[2] = cp1255_comb_table[cp1255_decomp_table[i].comb2];
  361. return 3;
  362. }
  363. }
  364. }
  365. return RET_ILUNI;
  366. }