Blame src/utf16_be.c

Packit Service bd74e6
/**********************************************************************
Packit Service bd74e6
  utf16_be.c -  Oniguruma (regular expression library)
Packit Service bd74e6
**********************************************************************/
Packit Service bd74e6
/*-
Packit Service bd74e6
 * Copyright (c) 2002-2018  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
Packit Service bd74e6
 * All rights reserved.
Packit Service bd74e6
 *
Packit Service bd74e6
 * Redistribution and use in source and binary forms, with or without
Packit Service bd74e6
 * modification, are permitted provided that the following conditions
Packit Service bd74e6
 * are met:
Packit Service bd74e6
 * 1. Redistributions of source code must retain the above copyright
Packit Service bd74e6
 *    notice, this list of conditions and the following disclaimer.
Packit Service bd74e6
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service bd74e6
 *    notice, this list of conditions and the following disclaimer in the
Packit Service bd74e6
 *    documentation and/or other materials provided with the distribution.
Packit Service bd74e6
 *
Packit Service bd74e6
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit Service bd74e6
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service bd74e6
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service bd74e6
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit Service bd74e6
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service bd74e6
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service bd74e6
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service bd74e6
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service bd74e6
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service bd74e6
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service bd74e6
 * SUCH DAMAGE.
Packit Service bd74e6
 */
Packit Service bd74e6
Packit Service bd74e6
#include "regint.h"  /* for USE_CALLOUT */
Packit Service bd74e6
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
init(void)
Packit Service bd74e6
{
Packit Service bd74e6
#ifdef USE_CALLOUT
Packit Service bd74e6
Packit Service bd74e6
    int id;
Packit Service bd74e6
    OnigEncoding enc;
Packit Service bd74e6
    char* name;
Packit Service bd74e6
    unsigned int args[4];
Packit Service bd74e6
    OnigValue opts[4];
Packit Service bd74e6
Packit Service bd74e6
    enc = ONIG_ENCODING_UTF16_BE;
Packit Service bd74e6
Packit Service bd74e6
    name = "\000F\000A\000I\000L\000\000";            BC0_P(name, fail);
Packit Service bd74e6
    name = "\000M\000I\000S\000M\000A\000T\000C\000H\000\000"; BC0_P(name, mismatch);
Packit Service bd74e6
Packit Service bd74e6
    name = "\000M\000A\000X\000\000";
Packit Service bd74e6
    args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
Packit Service bd74e6
    args[1] = ONIG_TYPE_CHAR;
Packit Service bd74e6
    opts[0].c = 'X';
Packit Service bd74e6
    BC_B_O(name, max, 2, args, 1, opts);
Packit Service bd74e6
Packit Service bd74e6
    name = "\000E\000R\000R\000O\000R\000\000";
Packit Service bd74e6
    args[0] = ONIG_TYPE_LONG; opts[0].l = ONIG_ABORT;
Packit Service bd74e6
    BC_P_O(name, error, 1, args, 1, opts);
Packit Service bd74e6
Packit Service bd74e6
    name = "\000C\000O\000U\000N\000T\000\000";
Packit Service bd74e6
    args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
Packit Service bd74e6
    BC_B_O(name, count, 1, args, 1, opts);
Packit Service bd74e6
Packit Service bd74e6
    name = "\000T\000O\000T\000A\000L\000_\000C\000O\000U\000N\000T\000\000";
Packit Service bd74e6
    args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
Packit Service bd74e6
    BC_B_O(name, total_count, 1, args, 1, opts);
Packit Service bd74e6
Packit Service bd74e6
    name = "\000C\000M\000P\000\000";
Packit Service bd74e6
    args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
Packit Service bd74e6
    args[1] = ONIG_TYPE_STRING;
Packit Service bd74e6
    args[2] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
Packit Service bd74e6
    BC_P(name, cmp, 3, args);
Packit Service bd74e6
Packit Service bd74e6
#endif /* USE_CALLOUT */
Packit Service bd74e6
Packit Service bd74e6
  return ONIG_NORMAL;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static const int EncLen_UTF16[] = {
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Packit Service bd74e6
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
Packit Service bd74e6
};
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_mbc_enc_len(const UChar* p)
Packit Service bd74e6
{
Packit Service bd74e6
  return EncLen_UTF16[*p];
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
is_valid_mbc_string(const UChar* s, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  return onigenc_length_check_is_valid_mbc_string(ONIG_ENCODING_UTF16_BE, s, end);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_is_mbc_newline(const UChar* p, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  if (p + 1 < end) {
Packit Service bd74e6
    if (*(p+1) == 0x0a && *p == 0x00)
Packit Service bd74e6
      return 1;
Packit Service bd74e6
#ifdef USE_UNICODE_ALL_LINE_TERMINATORS
Packit Service bd74e6
    if ((
Packit Service bd74e6
#ifndef USE_CRNL_AS_LINE_TERMINATOR
Packit Service bd74e6
         *(p+1) == 0x0d ||
Packit Service bd74e6
#endif
Packit Service bd74e6
         *(p+1) == 0x85) && *p == 0x00)
Packit Service bd74e6
      return 1;
Packit Service bd74e6
Packit Service bd74e6
    if (*p == 0x20 && (*(p+1) == 0x29 || *(p+1) == 0x28))
Packit Service bd74e6
      return 1;
Packit Service bd74e6
#endif
Packit Service bd74e6
  }
Packit Service bd74e6
  return 0;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static OnigCodePoint
Packit Service bd74e6
utf16be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
Packit Service bd74e6
{
Packit Service bd74e6
  OnigCodePoint code;
Packit Service bd74e6
Packit Service bd74e6
  if (UTF16_IS_SURROGATE_FIRST(*p)) {
Packit Service bd74e6
    code = ((((p[0] - 0xd8) << 2) + ((p[1] & 0xc0) >> 6) + 1) << 16)
Packit Service bd74e6
         + ((((p[1] & 0x3f) << 2) + (p[2] - 0xdc)) << 8)
Packit Service bd74e6
         + p[3];
Packit Service bd74e6
  }
Packit Service bd74e6
  else {
Packit Service bd74e6
    code = p[0] * 256 + p[1];
Packit Service bd74e6
  }
Packit Service bd74e6
  return code;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_code_to_mbclen(OnigCodePoint code)
Packit Service bd74e6
{
Packit Service bd74e6
  return (code > 0xffff ? 4 : 2);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_code_to_mbc(OnigCodePoint code, UChar *buf)
Packit Service bd74e6
{
Packit Service bd74e6
  UChar* p = buf;
Packit Service bd74e6
Packit Service bd74e6
  if (code > 0xffff) {
Packit Service bd74e6
    unsigned int plane, high;
Packit Service bd74e6
Packit Service bd74e6
    plane = (code >> 16) - 1;
Packit Service bd74e6
    *p++ = (plane >> 2) + 0xd8;
Packit Service bd74e6
    high = (code & 0xff00) >> 8;
Packit Service bd74e6
    *p++ = ((plane & 0x03) << 6) + (high >> 2);
Packit Service bd74e6
    *p++ = (high & 0x03) + 0xdc;
Packit Service bd74e6
    *p   = (UChar )(code & 0xff);
Packit Service bd74e6
    return 4;
Packit Service bd74e6
  }
Packit Service bd74e6
  else {
Packit Service bd74e6
    *p++ = (UChar )((code & 0xff00) >> 8);
Packit Service bd74e6
    *p++ = (UChar )(code & 0xff);
Packit Service bd74e6
    return 2;
Packit Service bd74e6
  }
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_mbc_case_fold(OnigCaseFoldType flag,
Packit Service bd74e6
		      const UChar** pp, const UChar* end, UChar* fold)
Packit Service bd74e6
{
Packit Service bd74e6
  const UChar* p = *pp;
Packit Service bd74e6
Packit Service bd74e6
  if (ONIGENC_IS_ASCII_CODE(*(p+1)) && *p == 0) {
Packit Service bd74e6
    p++;
Packit Service bd74e6
#ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
Packit Service bd74e6
    if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
Packit Service bd74e6
      if (*p == 0x49) {
Packit Service bd74e6
        *fold++ = 0x01;
Packit Service bd74e6
        *fold   = 0x31;
Packit Service bd74e6
        (*pp) += 2;
Packit Service bd74e6
        return 2;
Packit Service bd74e6
      }
Packit Service bd74e6
    }
Packit Service bd74e6
#endif
Packit Service bd74e6
Packit Service bd74e6
    *fold++ = 0;
Packit Service bd74e6
    *fold   = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
Packit Service bd74e6
    *pp += 2;
Packit Service bd74e6
    return 2;
Packit Service bd74e6
  }
Packit Service bd74e6
  else
Packit Service bd74e6
    return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF16_BE, flag,
Packit Service bd74e6
					 pp, end, fold);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
#if 0
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  const UChar* p = *pp;
Packit Service bd74e6
Packit Service bd74e6
  (*pp) += EncLen_UTF16[*p];
Packit Service bd74e6
Packit Service bd74e6
  if (*p == 0) {
Packit Service bd74e6
    int c, v;
Packit Service bd74e6
Packit Service bd74e6
    p++;
Packit Service bd74e6
    if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
Packit Service bd74e6
      return TRUE;
Packit Service bd74e6
    }
Packit Service bd74e6
Packit Service bd74e6
    c = *p;
Packit Service bd74e6
    v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
Packit Service bd74e6
		(BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
Packit Service bd74e6
Packit Service bd74e6
    if ((v | BIT_CTYPE_LOWER) != 0) {
Packit Service bd74e6
      /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
Packit Service bd74e6
      if (c >= 0xaa && c <= 0xba)
Packit Service bd74e6
        return FALSE;
Packit Service bd74e6
      else
Packit Service bd74e6
        return TRUE;
Packit Service bd74e6
    }
Packit Service bd74e6
    return (v != 0 ? TRUE : FALSE);
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  return FALSE;
Packit Service bd74e6
}
Packit Service bd74e6
#endif
Packit Service bd74e6
Packit Service bd74e6
static UChar*
Packit Service bd74e6
utf16be_left_adjust_char_head(const UChar* start, const UChar* s)
Packit Service bd74e6
{
Packit Service bd74e6
  if (s <= start) return (UChar* )s;
Packit Service bd74e6
Packit Service bd74e6
  if ((s - start) % 2 == 1) {
Packit Service bd74e6
    s--;
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  if (UTF16_IS_SURROGATE_SECOND(*s) && s > start + 1)
Packit Service bd74e6
    s -= 2;
Packit Service bd74e6
Packit Service bd74e6
  return (UChar* )s;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
utf16be_get_case_fold_codes_by_str(OnigCaseFoldType flag,
Packit Service bd74e6
    const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
Packit Service bd74e6
{
Packit Service bd74e6
  return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF16_BE,
Packit Service bd74e6
						    flag, p, end, items);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
OnigEncodingType OnigEncodingUTF16_BE = {
Packit Service bd74e6
  utf16be_mbc_enc_len,
Packit Service bd74e6
  "UTF-16BE",   /* name */
Packit Service bd74e6
  4,            /* max enc length */
Packit Service bd74e6
  2,            /* min enc length */
Packit Service bd74e6
  utf16be_is_mbc_newline,
Packit Service bd74e6
  utf16be_mbc_to_code,
Packit Service bd74e6
  utf16be_code_to_mbclen,
Packit Service bd74e6
  utf16be_code_to_mbc,
Packit Service bd74e6
  utf16be_mbc_case_fold,
Packit Service bd74e6
  onigenc_unicode_apply_all_case_fold,
Packit Service bd74e6
  utf16be_get_case_fold_codes_by_str,
Packit Service bd74e6
  onigenc_unicode_property_name_to_ctype,
Packit Service bd74e6
  onigenc_unicode_is_code_ctype,
Packit Service bd74e6
  onigenc_utf16_32_get_ctype_code_range,
Packit Service bd74e6
  utf16be_left_adjust_char_head,
Packit Service bd74e6
  onigenc_always_false_is_allowed_reverse_match,
Packit Service bd74e6
  init,
Packit Service bd74e6
  0, /* is_initialized */
Packit Service bd74e6
  is_valid_mbc_string,
Packit Service bd74e6
  ENC_FLAG_UNICODE,
Packit Service bd74e6
  0, 0
Packit Service bd74e6
};