Back to index

php5  5.3.10
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-2005  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 static int
00052 euckr_mbc_enc_len(const UChar* p)
00053 {
00054   return EncLen_EUCKR[*p];
00055 }
00056 
00057 static OnigCodePoint
00058 euckr_mbc_to_code(const UChar* p, const UChar* end)
00059 {
00060   return onigenc_mbn_mbc_to_code(ONIG_ENCODING_EUC_KR, p, end);
00061 }
00062 
00063 static int
00064 euckr_code_to_mbc(OnigCodePoint code, UChar *buf)
00065 {
00066   return onigenc_mb2_code_to_mbc(ONIG_ENCODING_EUC_KR, code, buf);
00067 }
00068 
00069 static int
00070 euckr_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
00071                        UChar* lower)
00072 {
00073   return onigenc_mbn_mbc_to_normalize(ONIG_ENCODING_EUC_KR, flag,
00074                                       pp, end, lower);
00075 }
00076 
00077 static int
00078 euckr_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
00079 {
00080   return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_EUC_KR, flag, pp, end);
00081 }
00082 
00083 static int
00084 euckr_is_code_ctype(OnigCodePoint code, unsigned int ctype)
00085 {
00086   return onigenc_mb2_is_code_ctype(ONIG_ENCODING_EUC_KR, code, ctype);
00087 }
00088 
00089 #define euckr_islead(c)    ((c) < 0xa1 || (c) == 0xff)
00090 
00091 static UChar*
00092 euckr_left_adjust_char_head(const UChar* start, const UChar* s)
00093 {
00094   /* Assumed in this encoding,
00095      mb-trail bytes don't mix with single bytes.
00096   */
00097   const UChar *p;
00098   int len;
00099 
00100   if (s <= start) return (UChar* )s;
00101   p = s;
00102 
00103   while (!euckr_islead(*p) && p > start) p--;
00104   len = enc_len(ONIG_ENCODING_EUC_KR, p);
00105   if (p + len > s) return (UChar* )p;
00106   p += len;
00107   return (UChar* )(p + ((s - p) & ~1));
00108 }
00109 
00110 static int
00111 euckr_is_allowed_reverse_match(const UChar* s, const UChar* end)
00112 {
00113   const UChar c = *s;
00114   if (c <= 0x7e) return TRUE;
00115   else           return FALSE;
00116 }
00117 
00118 OnigEncodingType OnigEncodingEUC_KR = {
00119   euckr_mbc_enc_len,
00120   "EUC-KR",   /* name */
00121   2,          /* max enc length */
00122   1,          /* min enc length */
00123   ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE,
00124   {
00125       (OnigCodePoint )'\\'                       /* esc */
00126     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00127     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00128     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00129     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00130     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00131   },
00132   onigenc_is_mbc_newline_0x0a,
00133   euckr_mbc_to_code,
00134   onigenc_mb2_code_to_mbclen,
00135   euckr_code_to_mbc,
00136   euckr_mbc_to_normalize,
00137   euckr_is_mbc_ambiguous,
00138   onigenc_ascii_get_all_pair_ambig_codes,
00139   onigenc_nothing_get_all_comp_ambig_codes,
00140   euckr_is_code_ctype,
00141   onigenc_not_support_get_ctype_code_range,
00142   euckr_left_adjust_char_head,
00143   euckr_is_allowed_reverse_match
00144 };
00145 
00146 /* Same with OnigEncodingEUC_KR except the name */
00147 OnigEncodingType OnigEncodingEUC_CN = {
00148   euckr_mbc_enc_len,
00149   "EUC-CN",   /* name */
00150   2,          /* max enc length */
00151   1,          /* min enc length */
00152   ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE,
00153   {
00154       (OnigCodePoint )'\\'                       /* esc */
00155     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00156     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00157     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00158     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00159     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00160   },
00161   onigenc_is_mbc_newline_0x0a,
00162   euckr_mbc_to_code,
00163   onigenc_mb2_code_to_mbclen,
00164   euckr_code_to_mbc,
00165   euckr_mbc_to_normalize,
00166   euckr_is_mbc_ambiguous,
00167   onigenc_ascii_get_all_pair_ambig_codes,
00168   onigenc_nothing_get_all_comp_ambig_codes,
00169   euckr_is_code_ctype,
00170   onigenc_not_support_get_ctype_code_range,
00171   euckr_left_adjust_char_head,
00172   euckr_is_allowed_reverse_match
00173 };