euc_jp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**********************************************************************
  2. euc_jp.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. #define eucjp_islead(c) ((UChar )((c) - 0xa1) > 0xfe - 0xa1)
  31. static const int EncLen_EUCJP[] = {
  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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
  41. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  42. 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  44. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  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, 2, 2, 2,
  47. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
  48. };
  49. static int
  50. mbc_enc_len(const UChar* p)
  51. {
  52. return EncLen_EUCJP[*p];
  53. }
  54. static OnigCodePoint
  55. mbc_to_code(const UChar* p, const UChar* end)
  56. {
  57. int c, i, len;
  58. OnigCodePoint n;
  59. len = enclen(ONIG_ENCODING_EUC_JP, p);
  60. n = (OnigCodePoint )*p++;
  61. if (len == 1) return n;
  62. for (i = 1; i < len; i++) {
  63. if (p >= end) break;
  64. c = *p++;
  65. n <<= 8; n += c;
  66. }
  67. return n;
  68. }
  69. static int
  70. code_to_mbclen(OnigCodePoint code)
  71. {
  72. if (ONIGENC_IS_CODE_ASCII(code)) return 1;
  73. else if ((code & 0xff0000) != 0) return 3;
  74. else if ((code & 0xff00) != 0) return 2;
  75. else
  76. return ONIGERR_INVALID_CODE_POINT_VALUE;
  77. }
  78. #if 0
  79. static int
  80. code_to_mbc_first(OnigCodePoint code)
  81. {
  82. int first;
  83. if ((code & 0xff0000) != 0) {
  84. first = (code >> 16) & 0xff;
  85. }
  86. else if ((code & 0xff00) != 0) {
  87. first = (code >> 8) & 0xff;
  88. }
  89. else {
  90. return (int )code;
  91. }
  92. return first;
  93. }
  94. #endif
  95. static int
  96. code_to_mbc(OnigCodePoint code, UChar *buf)
  97. {
  98. UChar *p = buf;
  99. if ((code & 0xff0000) != 0) *p++ = (UChar )(((code >> 16) & 0xff));
  100. if ((code & 0xff00) != 0) *p++ = (UChar )(((code >> 8) & 0xff));
  101. *p++ = (UChar )(code & 0xff);
  102. #if 1
  103. if (enclen(ONIG_ENCODING_EUC_JP, buf) != (p - buf))
  104. return ONIGERR_INVALID_CODE_POINT_VALUE;
  105. #endif
  106. return p - buf;
  107. }
  108. static int
  109. mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,
  110. const UChar** pp, const UChar* end ARG_UNUSED, UChar* lower)
  111. {
  112. int len;
  113. const UChar* p = *pp;
  114. if (ONIGENC_IS_MBC_ASCII(p)) {
  115. *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
  116. (*pp)++;
  117. return 1;
  118. }
  119. else {
  120. int i;
  121. len = enclen(ONIG_ENCODING_EUC_JP, p);
  122. for (i = 0; i < len; i++) {
  123. *lower++ = *p++;
  124. }
  125. (*pp) += len;
  126. return len; /* return byte length of converted char to lower */
  127. }
  128. }
  129. static UChar*
  130. left_adjust_char_head(const UChar* start, const UChar* s)
  131. {
  132. /* In this encoding
  133. mb-trail bytes doesn't mix with single bytes.
  134. */
  135. const UChar *p;
  136. int len;
  137. if (s <= start) return (UChar* )s;
  138. p = s;
  139. while (!eucjp_islead(*p) && p > start) p--;
  140. len = enclen(ONIG_ENCODING_EUC_JP, p);
  141. if (p + len > s) return (UChar* )p;
  142. p += len;
  143. return (UChar* )(p + ((s - p) & ~1));
  144. }
  145. static int
  146. is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
  147. {
  148. const UChar c = *s;
  149. if (c <= 0x7e || c == 0x8e || c == 0x8f)
  150. return TRUE;
  151. else
  152. return FALSE;
  153. }
  154. static int PropertyInited = 0;
  155. static const OnigCodePoint** PropertyList;
  156. static int PropertyListNum;
  157. static int PropertyListSize;
  158. static hash_table_type* PropertyNameTable;
  159. static const OnigCodePoint CR_Hiragana[] = {
  160. 1,
  161. 0xa4a1, 0xa4f3
  162. }; /* CR_Hiragana */
  163. static const OnigCodePoint CR_Katakana[] = {
  164. 3,
  165. 0xa5a1, 0xa5f6,
  166. 0xaaa6, 0xaaaf,
  167. 0xaab1, 0xaadd
  168. }; /* CR_Katakana */
  169. static int
  170. init_property_list(void)
  171. {
  172. int r;
  173. PROPERTY_LIST_ADD_PROP("Hiragana", CR_Hiragana);
  174. PROPERTY_LIST_ADD_PROP("Katakana", CR_Katakana);
  175. PropertyInited = 1;
  176. end:
  177. return r;
  178. }
  179. static int
  180. property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
  181. {
  182. hash_data_type ctype;
  183. PROPERTY_LIST_INIT_CHECK;
  184. if (onig_st_lookup_strend(PropertyNameTable, p, end, &ctype) == 0) {
  185. return onigenc_minimum_property_name_to_ctype(enc, p, end);
  186. }
  187. return (int )ctype;
  188. }
  189. static int
  190. is_code_ctype(OnigCodePoint code, unsigned int ctype)
  191. {
  192. if (ctype <= ONIGENC_MAX_STD_CTYPE) {
  193. if (code < 128)
  194. return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
  195. else {
  196. if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
  197. return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
  198. }
  199. }
  200. }
  201. else {
  202. PROPERTY_LIST_INIT_CHECK;
  203. ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
  204. if (ctype >= (unsigned int )PropertyListNum)
  205. return ONIGERR_TYPE_BUG;
  206. return onig_is_in_code_range((UChar* )PropertyList[ctype], code);
  207. }
  208. return FALSE;
  209. }
  210. static int
  211. get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
  212. const OnigCodePoint* ranges[])
  213. {
  214. if (ctype <= ONIGENC_MAX_STD_CTYPE) {
  215. return ONIG_NO_SUPPORT_CONFIG;
  216. }
  217. else {
  218. *sb_out = 0x80;
  219. PROPERTY_LIST_INIT_CHECK;
  220. ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
  221. if (ctype >= (OnigCtype )PropertyListNum)
  222. return ONIGERR_TYPE_BUG;
  223. *ranges = PropertyList[ctype];
  224. return 0;
  225. }
  226. }
  227. OnigEncodingType OnigEncodingEUC_JP = {
  228. mbc_enc_len,
  229. "EUC-JP", /* name */
  230. 3, /* max enc length */
  231. 1, /* min enc length */
  232. onigenc_is_mbc_newline_0x0a,
  233. mbc_to_code,
  234. code_to_mbclen,
  235. code_to_mbc,
  236. mbc_case_fold,
  237. onigenc_ascii_apply_all_case_fold,
  238. onigenc_ascii_get_case_fold_codes_by_str,
  239. property_name_to_ctype,
  240. is_code_ctype,
  241. get_ctype_code_range,
  242. left_adjust_char_head,
  243. is_allowed_reverse_match
  244. };