Ruby 1.9.3p327(2012-11-10revision37606)
enc/euc_kr.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   euc_kr.c -  Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2007  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include "regenc.h"
00031 
00032 static const int EncLen_EUCKR[] = {
00033   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00034   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00035   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00036   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00037   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00038   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00039   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00040   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00041   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00042   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00043   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00044   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00045   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00046   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00047   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00048   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
00049 };
00050 
00051 typedef enum { FAILURE = -2, ACCEPT = -1, S0 = 0, S1 } state_t;
00052 #define A ACCEPT
00053 #define F FAILURE
00054 static const signed char trans[][0x100] = {
00055   { /* S0   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
00056     /* 0 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00057     /* 1 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00058     /* 2 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00059     /* 3 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00060     /* 4 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00061     /* 5 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00062     /* 6 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00063     /* 7 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00064     /* 8 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00065     /* 9 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00066     /* a */ F, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00067     /* b */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00068     /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00069     /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00070     /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00071     /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, F
00072   },
00073   { /* S1   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
00074     /* 0 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00075     /* 1 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00076     /* 2 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00077     /* 3 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00078     /* 4 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00079     /* 5 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00080     /* 6 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00081     /* 7 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00082     /* 8 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00083     /* 9 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
00084     /* a */ F, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00085     /* b */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00086     /* c */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00087     /* d */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00088     /* e */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
00089     /* f */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, F
00090   }
00091 };
00092 #undef A
00093 #undef F
00094 
00095 static int
00096 euckr_mbc_enc_len(const UChar* p, const UChar* e, OnigEncoding enc ARG_UNUSED)
00097 {
00098   int firstbyte = *p++;
00099   state_t s = trans[0][firstbyte];
00100 #define RETURN(n) \
00101     return s == ACCEPT ? ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(n) : \
00102                          ONIGENC_CONSTRUCT_MBCLEN_INVALID()
00103   if (s < 0) RETURN(1);
00104   if (p == e) return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(EncLen_EUCKR[firstbyte]-1);
00105   s = trans[s][*p++];
00106   RETURN(2);
00107 #undef RETURN
00108 }
00109 
00110 static OnigCodePoint
00111 euckr_mbc_to_code(const UChar* p, const UChar* end, OnigEncoding enc)
00112 {
00113   return onigenc_mbn_mbc_to_code(enc, p, end);
00114 }
00115 
00116 static int
00117 euckr_code_to_mbc(OnigCodePoint code, UChar *buf, OnigEncoding enc)
00118 {
00119   return onigenc_mb2_code_to_mbc(enc, code, buf);
00120 }
00121 
00122 static int
00123 euckr_mbc_case_fold(OnigCaseFoldType flag, const UChar** pp, const UChar* end,
00124                     UChar* lower, OnigEncoding enc)
00125 {
00126   return onigenc_mbn_mbc_case_fold(enc, flag,
00127                                    pp, end, lower);
00128 }
00129 
00130 #if 0
00131 static int
00132 euckr_is_mbc_ambiguous(OnigCaseFoldType flag,
00133                        const UChar** pp, const UChar* end, OnigEncoding enc)
00134 {
00135   return onigenc_mbn_is_mbc_ambiguous(enc, flag, pp, end);
00136 }
00137 #endif
00138 
00139 static int
00140 euckr_is_code_ctype(OnigCodePoint code, unsigned int ctype, OnigEncoding enc)
00141 {
00142   return onigenc_mb2_is_code_ctype(enc, code, ctype);
00143 }
00144 
00145 #define euckr_islead(c)    ((c) < 0xa1 || (c) == 0xff)
00146 
00147 static UChar*
00148 euckr_left_adjust_char_head(const UChar* start, const UChar* s, const UChar* end, OnigEncoding enc)
00149 {
00150   /* Assumed in this encoding,
00151      mb-trail bytes don't mix with single bytes.
00152   */
00153   const UChar *p;
00154   int len;
00155 
00156   if (s <= start) return (UChar* )s;
00157   p = s;
00158 
00159   while (!euckr_islead(*p) && p > start) p--;
00160   len = enclen(enc, p, end);
00161   if (p + len > s) return (UChar* )p;
00162   p += len;
00163   return (UChar* )(p + ((s - p) & ~1));
00164 }
00165 
00166 static int
00167 euckr_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED, OnigEncoding enc ARG_UNUSED)
00168 {
00169   const UChar c = *s;
00170   if (c <= 0x7e) return TRUE;
00171   else           return FALSE;
00172 }
00173 
00174 OnigEncodingDefine(euc_kr, EUC_KR) = {
00175   euckr_mbc_enc_len,
00176   "EUC-KR",   /* name */
00177   2,          /* max enc length */
00178   1,          /* min enc length */
00179   onigenc_is_mbc_newline_0x0a,
00180   euckr_mbc_to_code,
00181   onigenc_mb2_code_to_mbclen,
00182   euckr_code_to_mbc,
00183   euckr_mbc_case_fold,
00184   onigenc_ascii_apply_all_case_fold,
00185   onigenc_ascii_get_case_fold_codes_by_str,
00186   onigenc_minimum_property_name_to_ctype,
00187   euckr_is_code_ctype,
00188   onigenc_not_support_get_ctype_code_range,
00189   euckr_left_adjust_char_head,
00190   euckr_is_allowed_reverse_match
00191 };
00192 ENC_ALIAS("eucKR", "EUC-KR")
00193