Back to index

php5  5.3.10
utf32_be.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   utf32_be.c -  Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2006  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 int
00033 utf32be_mbc_enc_len(const UChar* p)
00034 {
00035   return 4;
00036 }
00037 
00038 static int
00039 utf32be_is_mbc_newline(const UChar* p, const UChar* end)
00040 {
00041   if (p + 3 < end) {
00042     if (*(p+3) == 0x0a && *(p+2) == 0 && *(p+1) == 0 && *p == 0)
00043       return 1;
00044 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
00045     if ((*(p+3) == 0x0d || *(p+3) == 0x85)
00046        && *(p+2) == 0 && *(p+1) == 0 && *p == 0x00)
00047       return 1;
00048     if (*(p+2) == 0x20 && (*(p+3) == 0x29 || *(p+3) == 0x28)
00049        && *(p+1) == 0 && *p == 0)
00050       return 1;
00051 #endif
00052   }
00053   return 0;
00054 }
00055 
00056 static OnigCodePoint
00057 utf32be_mbc_to_code(const UChar* p, const UChar* end)
00058 {
00059   return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
00060 }
00061 
00062 static int
00063 utf32be_code_to_mbclen(OnigCodePoint code)
00064 {
00065   return 4;
00066 }
00067 
00068 static int
00069 utf32be_code_to_mbc(OnigCodePoint code, UChar *buf)
00070 {
00071   UChar* p = buf;
00072 
00073   *p++ = (UChar )((code & 0xff000000) >>24);
00074   *p++ = (UChar )((code & 0xff0000)   >>16);
00075   *p++ = (UChar )((code & 0xff00)     >> 8);
00076   *p++ = (UChar ) (code & 0xff);
00077   return 4;
00078 }
00079 
00080 static int
00081 utf32be_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
00082                          UChar* lower)
00083 {
00084   const UChar* p = *pp;
00085 
00086   if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
00087     p += 3;
00088     *lower++ = '\0';
00089     *lower++ = '\0';
00090     *lower++ = '\0';
00091     if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
00092         ONIGENC_IS_MBC_ASCII(p)) ||
00093        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
00094         !ONIGENC_IS_MBC_ASCII(p))) {
00095       *lower = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
00096     }
00097     else {
00098       *lower = *p;
00099     }
00100 
00101     (*pp) += 4;
00102     return 4;  /* return byte length of converted char to lower */
00103   }
00104   else {
00105     int len = 4;
00106     if (lower != p) {
00107       int i;
00108       for (i = 0; i < len; i++) {
00109        *lower++ = *p++;
00110       }
00111     }
00112     (*pp) += len;
00113     return len; /* return byte length of converted char to lower */
00114   }
00115 }
00116 
00117 static int
00118 utf32be_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
00119 {
00120   const UChar* p = *pp;
00121 
00122   (*pp) += 4;
00123 
00124   if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
00125     int c, v;
00126 
00127     p += 3;
00128     if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
00129         ONIGENC_IS_MBC_ASCII(p)) ||
00130        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
00131         !ONIGENC_IS_MBC_ASCII(p))) {
00132       c = *p;
00133       v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
00134                        (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
00135       if ((v | ONIGENC_CTYPE_LOWER) != 0) {
00136         /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
00137         if (c >= 0xaa && c <= 0xba)
00138           return FALSE;
00139         else
00140           return TRUE;
00141       }
00142       return (v != 0 ? TRUE : FALSE);
00143     }
00144   }
00145 
00146   return FALSE;
00147 }
00148 
00149 static UChar*
00150 utf32be_left_adjust_char_head(const UChar* start, const UChar* s)
00151 {
00152   int rem;
00153 
00154   if (s <= start) return (UChar* )s;
00155 
00156   rem = (s - start) % 4;
00157   return (UChar* )(s - rem);
00158 }
00159 
00160 OnigEncodingType OnigEncodingUTF32_BE = {
00161   utf32be_mbc_enc_len,
00162   "UTF-32BE",   /* name */
00163   4,            /* max byte length */
00164   4,            /* min byte length */
00165   (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
00166    ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE ),
00167   {
00168       (OnigCodePoint )'\\'                       /* esc */
00169     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00170     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00171     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00172     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00173     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00174   },
00175   utf32be_is_mbc_newline,
00176   utf32be_mbc_to_code,
00177   utf32be_code_to_mbclen,
00178   utf32be_code_to_mbc,
00179   utf32be_mbc_to_normalize,
00180   utf32be_is_mbc_ambiguous,
00181   onigenc_iso_8859_1_get_all_pair_ambig_codes,
00182   onigenc_ess_tsett_get_all_comp_ambig_codes,
00183   onigenc_unicode_is_code_ctype,
00184   onigenc_unicode_get_ctype_code_range,
00185   utf32be_left_adjust_char_head,
00186   onigenc_always_false_is_allowed_reverse_match
00187 };