root/ext/mbstring/oniguruma/regext.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. conv_ext0be32
  2. conv_ext0le32
  3. conv_ext0be
  4. conv_ext0le
  5. conv_swap4bytes
  6. conv_swap2bytes
  7. conv_encoding
  8. onig_new_deluxe
  9. onig_recompile_deluxe

   1 /**********************************************************************
   2   regext.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 
  30 #include "regint.h"
  31 
  32 static void
  33 conv_ext0be32(const UChar* s, const UChar* end, UChar* conv)
  34 {
  35   while (s < end) {
  36     *conv++ = '\0';
  37     *conv++ = '\0';
  38     *conv++ = '\0';
  39     *conv++ = *s++;
  40   }
  41 }
  42 
  43 static void
  44 conv_ext0le32(const UChar* s, const UChar* end, UChar* conv)
  45 {
  46   while (s < end) {
  47     *conv++ = *s++;
  48     *conv++ = '\0';
  49     *conv++ = '\0';
  50     *conv++ = '\0';
  51   }
  52 }
  53 
  54 static void
  55 conv_ext0be(const UChar* s, const UChar* end, UChar* conv)
  56 {
  57   while (s < end) {
  58     *conv++ = '\0';
  59     *conv++ = *s++;
  60   }
  61 }
  62 
  63 static void
  64 conv_ext0le(const UChar* s, const UChar* end, UChar* conv)
  65 {
  66   while (s < end) {
  67     *conv++ = *s++;
  68     *conv++ = '\0';
  69   }
  70 }
  71 
  72 static void
  73 conv_swap4bytes(const UChar* s, const UChar* end, UChar* conv)
  74 {
  75   while (s < end) {
  76     *conv++ = s[3];
  77     *conv++ = s[2];
  78     *conv++ = s[1];
  79     *conv++ = s[0];
  80     s += 4;
  81   }
  82 }
  83 
  84 static void
  85 conv_swap2bytes(const UChar* s, const UChar* end, UChar* conv)
  86 {
  87   while (s < end) {
  88     *conv++ = s[1];
  89     *conv++ = s[0];
  90     s += 2;
  91   }
  92 }
  93 
  94 static int
  95 conv_encoding(OnigEncoding from, OnigEncoding to, const UChar* s, const UChar* end,
  96               UChar** conv, UChar** conv_end)
  97 {
  98   int len = end - s;
  99 
 100   if (to == ONIG_ENCODING_UTF16_BE) {
 101     if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
 102       *conv = (UChar* )xmalloc(len * 2);
 103       CHECK_NULL_RETURN_MEMERR(*conv);
 104       *conv_end = *conv + (len * 2);
 105       conv_ext0be(s, end, *conv);
 106       return 0;
 107     }
 108     else if (from == ONIG_ENCODING_UTF16_LE) {
 109     swap16:
 110       *conv = (UChar* )xmalloc(len);
 111       CHECK_NULL_RETURN_MEMERR(*conv);
 112       *conv_end = *conv + len;
 113       conv_swap2bytes(s, end, *conv);
 114       return 0;
 115     }
 116   }
 117   else if (to == ONIG_ENCODING_UTF16_LE) {
 118     if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
 119       *conv = (UChar* )xmalloc(len * 2);
 120       CHECK_NULL_RETURN_MEMERR(*conv);
 121       *conv_end = *conv + (len * 2);
 122       conv_ext0le(s, end, *conv);
 123       return 0;
 124     }
 125     else if (from == ONIG_ENCODING_UTF16_BE) {
 126       goto swap16;
 127     }
 128   }
 129   if (to == ONIG_ENCODING_UTF32_BE) {
 130     if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
 131       *conv = (UChar* )xmalloc(len * 4);
 132       CHECK_NULL_RETURN_MEMERR(*conv);
 133       *conv_end = *conv + (len * 4);
 134       conv_ext0be32(s, end, *conv);
 135       return 0;
 136     }
 137     else if (from == ONIG_ENCODING_UTF32_LE) {
 138     swap32:
 139       *conv = (UChar* )xmalloc(len);
 140       CHECK_NULL_RETURN_MEMERR(*conv);
 141       *conv_end = *conv + len;
 142       conv_swap4bytes(s, end, *conv);
 143       return 0;
 144     }
 145   }
 146   else if (to == ONIG_ENCODING_UTF32_LE) {
 147     if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
 148       *conv = (UChar* )xmalloc(len * 4);
 149       CHECK_NULL_RETURN_MEMERR(*conv);
 150       *conv_end = *conv + (len * 4);
 151       conv_ext0le32(s, end, *conv);
 152       return 0;
 153     }
 154     else if (from == ONIG_ENCODING_UTF32_BE) {
 155       goto swap32;
 156     }
 157   }
 158 
 159   return ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION;
 160 }
 161 
 162 extern int
 163 onig_new_deluxe(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
 164                 OnigCompileInfo* ci, OnigErrorInfo* einfo)
 165 {
 166   int r;
 167   UChar *cpat, *cpat_end;
 168 
 169   if (IS_NOT_NULL(einfo)) einfo->par = (UChar* )NULL;
 170 
 171   if (ci->pattern_enc != ci->target_enc) {
 172     r = conv_encoding(ci->pattern_enc, ci->target_enc, pattern, pattern_end,
 173                       &cpat, &cpat_end);
 174     if (r) return r;
 175   }
 176   else {
 177     cpat     = (UChar* )pattern;
 178     cpat_end = (UChar* )pattern_end;
 179   }
 180 
 181   *reg = (regex_t* )xmalloc(sizeof(regex_t));
 182   if (IS_NULL(*reg)) {
 183     r = ONIGERR_MEMORY;
 184     goto err2;
 185   }
 186 
 187   r = onig_reg_init(*reg, ci->option, ci->case_fold_flag, ci->target_enc,
 188                     ci->syntax);
 189   if (r) goto err;
 190 
 191   r = onig_compile(*reg, cpat, cpat_end, einfo);
 192   if (r) {
 193   err:
 194     onig_free(*reg);
 195     *reg = NULL;
 196   }
 197 
 198  err2:
 199   if (cpat != pattern) xfree(cpat);
 200 
 201   return r;
 202 }
 203 
 204 #ifdef USE_RECOMPILE_API
 205 extern int
 206 onig_recompile_deluxe(regex_t* reg, const UChar* pattern, const UChar* pattern_end,
 207                       OnigCompileInfo* ci, OnigErrorInfo* einfo)
 208 {
 209   int r;
 210   regex_t *new_reg;
 211 
 212   r = onig_new_deluxe(&new_reg, pattern, pattern_end, ci, einfo);
 213   if (r) return r;
 214   if (ONIG_STATE(reg) == ONIG_STATE_NORMAL) {
 215     onig_transfer(reg, new_reg);
 216   }
 217   else {
 218     onig_chain_link_add(reg, new_reg);
 219   }
 220   return 0;
 221 }
 222 #endif

/* [<][>][^][v][top][bottom][index][help] */