sjis.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**********************************************************************
  2. sjis.c - Oniguruma (regular expression library)
  3. **********************************************************************/
  4. /*-
  5. * Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "regint.h"
  30. static const int EncLen_SJIS[] = {
  31. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  32. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  40. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  41. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1
  47. };
  48. static const char SJIS_CAN_BE_TRAIL_TABLE[256] = {
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  57. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  58. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  59. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  61. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  62. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  63. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  64. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0
  65. };
  66. #define SJIS_ISMB_FIRST(byte) (EncLen_SJIS[byte] > 1)
  67. #define SJIS_ISMB_TRAIL(byte) SJIS_CAN_BE_TRAIL_TABLE[(byte)]
  68. static int
  69. mbc_enc_len(const UChar* p)
  70. {
  71. return EncLen_SJIS[*p];
  72. }
  73. static int
  74. code_to_mbclen(OnigCodePoint code)
  75. {
  76. if (code < 256) {
  77. if (EncLen_SJIS[(int )code] == 1)
  78. return 1;
  79. else
  80. return 0;
  81. }
  82. else if (code <= 0xffff) {
  83. return 2;
  84. }
  85. else
  86. return ONIGERR_INVALID_CODE_POINT_VALUE;
  87. }
  88. static OnigCodePoint
  89. mbc_to_code(const UChar* p, const UChar* end)
  90. {
  91. int c, i, len;
  92. OnigCodePoint n;
  93. len = enclen(ONIG_ENCODING_SJIS, p);
  94. c = *p++;
  95. n = c;
  96. if (len == 1) return n;
  97. for (i = 1; i < len; i++) {
  98. if (p >= end) break;
  99. c = *p++;
  100. n <<= 8; n += c;
  101. }
  102. return n;
  103. }
  104. static int
  105. code_to_mbc(OnigCodePoint code, UChar *buf)
  106. {
  107. UChar *p = buf;
  108. if ((code & 0xff00) != 0) *p++ = (UChar )(((code >> 8) & 0xff));
  109. *p++ = (UChar )(code & 0xff);
  110. #if 0
  111. if (enclen(ONIG_ENCODING_SJIS, buf) != (p - buf))
  112. return REGERR_INVALID_CODE_POINT_VALUE;
  113. #endif
  114. return p - buf;
  115. }
  116. static int
  117. mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,
  118. const UChar** pp, const UChar* end ARG_UNUSED, UChar* lower)
  119. {
  120. const UChar* p = *pp;
  121. if (ONIGENC_IS_MBC_ASCII(p)) {
  122. *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
  123. (*pp)++;
  124. return 1;
  125. }
  126. else {
  127. int i;
  128. int len = enclen(ONIG_ENCODING_SJIS, p);
  129. for (i = 0; i < len; i++) {
  130. *lower++ = *p++;
  131. }
  132. (*pp) += len;
  133. return len; /* return byte length of converted char to lower */
  134. }
  135. }
  136. #if 0
  137. static int
  138. is_mbc_ambiguous(OnigCaseFoldType flag,
  139. const UChar** pp, const UChar* end)
  140. {
  141. return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_SJIS, flag, pp, end);
  142. }
  143. #endif
  144. #if 0
  145. static int
  146. is_code_ctype(OnigCodePoint code, unsigned int ctype)
  147. {
  148. if (code < 128)
  149. return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
  150. else {
  151. if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
  152. return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
  153. }
  154. }
  155. return FALSE;
  156. }
  157. #endif
  158. static UChar*
  159. left_adjust_char_head(const UChar* start, const UChar* s)
  160. {
  161. const UChar *p;
  162. int len;
  163. if (s <= start) return (UChar* )s;
  164. p = s;
  165. if (SJIS_ISMB_TRAIL(*p)) {
  166. while (p > start) {
  167. if (! SJIS_ISMB_FIRST(*--p)) {
  168. p++;
  169. break;
  170. }
  171. }
  172. }
  173. len = enclen(ONIG_ENCODING_SJIS, p);
  174. if (p + len > s) return (UChar* )p;
  175. p += len;
  176. return (UChar* )(p + ((s - p) & ~1));
  177. }
  178. static int
  179. is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
  180. {
  181. const UChar c = *s;
  182. return (SJIS_ISMB_TRAIL(c) ? FALSE : TRUE);
  183. }
  184. static int PropertyInited = 0;
  185. static const OnigCodePoint** PropertyList;
  186. static int PropertyListNum;
  187. static int PropertyListSize;
  188. static hash_table_type* PropertyNameTable;
  189. static const OnigCodePoint CR_Hiragana[] = {
  190. 1,
  191. 0x829f, 0x82f1
  192. }; /* CR_Hiragana */
  193. static const OnigCodePoint CR_Katakana[] = {
  194. 4,
  195. 0x00a6, 0x00af,
  196. 0x00b1, 0x00dd,
  197. 0x8340, 0x837e,
  198. 0x8380, 0x8396,
  199. }; /* CR_Katakana */
  200. static int
  201. init_property_list(void)
  202. {
  203. int r;
  204. PROPERTY_LIST_ADD_PROP("Hiragana", CR_Hiragana);
  205. PROPERTY_LIST_ADD_PROP("Katakana", CR_Katakana);
  206. PropertyInited = 1;
  207. end:
  208. return r;
  209. }
  210. static int
  211. property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
  212. {
  213. hash_data_type ctype;
  214. PROPERTY_LIST_INIT_CHECK;
  215. if (onig_st_lookup_strend(PropertyNameTable, p, end, &ctype) == 0) {
  216. return onigenc_minimum_property_name_to_ctype(enc, p, end);
  217. }
  218. return (int )ctype;
  219. }
  220. static int
  221. is_code_ctype(OnigCodePoint code, unsigned int ctype)
  222. {
  223. if (ctype <= ONIGENC_MAX_STD_CTYPE) {
  224. if (code < 128)
  225. return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
  226. else {
  227. if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
  228. return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
  229. }
  230. }
  231. }
  232. else {
  233. PROPERTY_LIST_INIT_CHECK;
  234. ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
  235. if (ctype >= (unsigned int )PropertyListNum)
  236. return ONIGERR_TYPE_BUG;
  237. return onig_is_in_code_range((UChar* )PropertyList[ctype], code);
  238. }
  239. return FALSE;
  240. }
  241. static int
  242. get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
  243. const OnigCodePoint* ranges[])
  244. {
  245. if (ctype <= ONIGENC_MAX_STD_CTYPE) {
  246. return ONIG_NO_SUPPORT_CONFIG;
  247. }
  248. else {
  249. *sb_out = 0x80;
  250. PROPERTY_LIST_INIT_CHECK;
  251. ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
  252. if (ctype >= (OnigCtype )PropertyListNum)
  253. return ONIGERR_TYPE_BUG;
  254. *ranges = PropertyList[ctype];
  255. return 0;
  256. }
  257. }
  258. OnigEncodingType OnigEncodingSJIS = {
  259. mbc_enc_len,
  260. "Shift_JIS", /* name */
  261. 2, /* max byte length */
  262. 1, /* min byte length */
  263. onigenc_is_mbc_newline_0x0a,
  264. mbc_to_code,
  265. code_to_mbclen,
  266. code_to_mbc,
  267. mbc_case_fold,
  268. onigenc_ascii_apply_all_case_fold,
  269. onigenc_ascii_get_case_fold_codes_by_str,
  270. property_name_to_ctype,
  271. is_code_ctype,
  272. get_ctype_code_range,
  273. left_adjust_char_head,
  274. is_allowed_reverse_match
  275. };