Blame src/utf8.c

Packit Service bd74e6
/**********************************************************************
Packit Service bd74e6
  utf8.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 "regenc.h"
Packit Service bd74e6
Packit Service bd74e6
/* #define USE_INVALID_CODE_SCHEME */
Packit Service bd74e6
Packit Service bd74e6
#ifdef USE_INVALID_CODE_SCHEME
Packit Service bd74e6
/* virtual codepoint values for invalid encoding byte 0xfe and 0xff */
Packit Service bd74e6
#define INVALID_CODE_FE   0xfffffffe
Packit Service bd74e6
#define INVALID_CODE_FF   0xffffffff
Packit Service bd74e6
#define VALID_CODE_LIMIT  0x7fffffff
Packit Service bd74e6
#endif
Packit Service bd74e6
Packit Service bd74e6
#define utf8_islead(c)     ((UChar )((c) & 0xc0) != 0x80)
Packit Service bd74e6
#define utf8_istail(c)     ((UChar )((c) & 0xc0) == 0x80)
Packit Service bd74e6
Packit Service bd74e6
static const int EncLen_UTF8[] = {
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Packit Service bd74e6
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Packit Service bd74e6
  4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
Packit Service bd74e6
};
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
mbc_enc_len(const UChar* p)
Packit Service bd74e6
{
Packit Service bd74e6
  return EncLen_UTF8[*p];
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
is_valid_mbc_string(const UChar* p, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  int i, len;
Packit Service bd74e6
Packit Service bd74e6
  while (p < end) {
Packit Service bd74e6
    if (! utf8_islead(*p))
Packit Service bd74e6
      return FALSE;
Packit Service bd74e6
Packit Service bd74e6
    len = mbc_enc_len(p++);
Packit Service bd74e6
    if (len > 1) {
Packit Service bd74e6
      for (i = 1; i < len; i++) {
Packit Service bd74e6
	if (p == end)
Packit Service bd74e6
	  return FALSE;
Packit Service bd74e6
Packit Service bd74e6
	if (! utf8_istail(*p++))
Packit Service bd74e6
	  return FALSE;
Packit Service bd74e6
      }
Packit Service bd74e6
    }
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  return TRUE;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
#if 0
Packit Service bd74e6
static int
Packit Service bd74e6
is_mbc_newline(const UChar* p, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  if (p < end) {
Packit Service bd74e6
    if (*p == 0x0a) return 1;
Packit Service bd74e6
Packit Service bd74e6
#ifdef USE_UNICODE_ALL_LINE_TERMINATORS
Packit Service bd74e6
#ifndef USE_CRNL_AS_LINE_TERMINATOR
Packit Service bd74e6
    if (*p == 0x0d) return 1;
Packit Service bd74e6
#endif
Packit Service bd74e6
    if (p + 1 < end) {
Packit Service bd74e6
      if (*(p+1) == 0x85 && *p == 0xc2) /* U+0085 */
Packit Service bd74e6
        return 1;
Packit Service bd74e6
      if (p + 2 < end) {
Packit Service bd74e6
        if ((*(p+2) == 0xa8 || *(p+2) == 0xa9)
Packit Service bd74e6
            && *(p+1) == 0x80 && *p == 0xe2)  /* U+2028, U+2029 */
Packit Service bd74e6
          return 1;
Packit Service bd74e6
      }
Packit Service bd74e6
    }
Packit Service bd74e6
#endif
Packit Service bd74e6
  }
Packit Service bd74e6
Packit Service bd74e6
  return 0;
Packit Service bd74e6
}
Packit Service bd74e6
#endif
Packit Service bd74e6
Packit Service bd74e6
static OnigCodePoint
Packit Service bd74e6
mbc_to_code(const UChar* p, const UChar* end)
Packit Service bd74e6
{
Packit Service bd74e6
  int c, len;
Packit Service bd74e6
  OnigCodePoint n;
Packit Service bd74e6
Packit Service bd74e6
  len = mbc_enc_len(p);
Packit Service bd74e6
  if (len > (int )(end - p)) len = (int )(end - p);
Packit Service bd74e6
Packit Service bd74e6
  c = *p++;
Packit Service bd74e6
  if (len > 1) {
Packit Service bd74e6
    len--;
Packit Service bd74e6
    n = c & ((1 << (6 - len)) - 1);
Packit Service bd74e6
    while (len--) {
Packit Service bd74e6
      c = *p++;
Packit Service bd74e6
      n = (n << 6) | (c & ((1 << 6) - 1));
Packit Service bd74e6
    }
Packit Service bd74e6
    return n;
Packit Service bd74e6
  }
Packit Service bd74e6
  else {
Packit Service bd74e6
#ifdef USE_INVALID_CODE_SCHEME
Packit Service bd74e6
    if (c > 0xfd) {
Packit Service bd74e6
      return ((c == 0xfe) ? INVALID_CODE_FE : INVALID_CODE_FF);
Packit Service bd74e6
    }
Packit Service bd74e6
#endif
Packit Service bd74e6
    return (OnigCodePoint )c;
Packit Service bd74e6
  }
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
code_to_mbclen(OnigCodePoint code)
Packit Service bd74e6
{
Packit Service bd74e6
  if      ((code & 0xffffff80) == 0) return 1;
Packit Service bd74e6
  else if ((code & 0xfffff800) == 0) return 2;
Packit Service bd74e6
  else if ((code & 0xffff0000) == 0) return 3;
Packit Service bd74e6
  else if ((code & 0xffe00000) == 0) return 4;
Packit Service bd74e6
  else if ((code & 0xfc000000) == 0) return 5;
Packit Service bd74e6
  else if ((code & 0x80000000) == 0) return 6;
Packit Service bd74e6
#ifdef USE_INVALID_CODE_SCHEME
Packit Service bd74e6
  else if (code == INVALID_CODE_FE) return 1;
Packit Service bd74e6
  else if (code == INVALID_CODE_FF) return 1;
Packit Service bd74e6
#endif
Packit Service bd74e6
  else
Packit Service bd74e6
    return ONIGERR_INVALID_CODE_POINT_VALUE;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
code_to_mbc(OnigCodePoint code, UChar *buf)
Packit Service bd74e6
{
Packit Service bd74e6
#define UTF8_TRAILS(code, shift) (UChar )((((code) >> (shift)) & 0x3f) | 0x80)
Packit Service bd74e6
#define UTF8_TRAIL0(code)        (UChar )(((code) & 0x3f) | 0x80)
Packit Service bd74e6
Packit Service bd74e6
  if ((code & 0xffffff80) == 0) {
Packit Service bd74e6
    *buf = (UChar )code;
Packit Service bd74e6
    return 1;
Packit Service bd74e6
  }
Packit Service bd74e6
  else {
Packit Service bd74e6
    UChar *p = buf;
Packit Service bd74e6
Packit Service bd74e6
    if ((code & 0xfffff800) == 0) {
Packit Service bd74e6
      *p++ = (UChar )(((code>>6)& 0x1f) | 0xc0);
Packit Service bd74e6
    }
Packit Service bd74e6
    else if ((code & 0xffff0000) == 0) {
Packit Service bd74e6
      *p++ = (UChar )(((code>>12) & 0x0f) | 0xe0);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 6);
Packit Service bd74e6
    }
Packit Service bd74e6
    else if ((code & 0xffe00000) == 0) {
Packit Service bd74e6
      *p++ = (UChar )(((code>>18) & 0x07) | 0xf0);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 12);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code,  6);
Packit Service bd74e6
    }
Packit Service bd74e6
    else if ((code & 0xfc000000) == 0) {
Packit Service bd74e6
      *p++ = (UChar )(((code>>24) & 0x03) | 0xf8);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 18);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 12);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code,  6);
Packit Service bd74e6
    }
Packit Service bd74e6
    else if ((code & 0x80000000) == 0) {
Packit Service bd74e6
      *p++ = (UChar )(((code>>30) & 0x01) | 0xfc);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 24);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 18);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code, 12);
Packit Service bd74e6
      *p++ = UTF8_TRAILS(code,  6);
Packit Service bd74e6
    }
Packit Service bd74e6
#ifdef USE_INVALID_CODE_SCHEME
Packit Service bd74e6
    else if (code == INVALID_CODE_FE) {
Packit Service bd74e6
      *p = 0xfe;
Packit Service bd74e6
      return 1;
Packit Service bd74e6
    }
Packit Service bd74e6
    else if (code == INVALID_CODE_FF) {
Packit Service bd74e6
      *p = 0xff;
Packit Service bd74e6
      return 1;
Packit Service bd74e6
    }
Packit Service bd74e6
#endif
Packit Service bd74e6
    else {
Packit Service bd74e6
      return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
Packit Service bd74e6
    }
Packit Service bd74e6
Packit Service bd74e6
    *p++ = UTF8_TRAIL0(code);
Packit Service bd74e6
    return (int )(p - buf);
Packit Service bd74e6
  }
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
mbc_case_fold(OnigCaseFoldType flag, const UChar** pp,
Packit Service bd74e6
	      const UChar* end, UChar* fold)
Packit Service bd74e6
{
Packit Service bd74e6
  const UChar* p = *pp;
Packit Service bd74e6
Packit Service bd74e6
  if (ONIGENC_IS_MBC_ASCII(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++ = 0xc4;
Packit Service bd74e6
        *fold   = 0xb1;
Packit Service bd74e6
        (*pp)++;
Packit Service bd74e6
        return 2;
Packit Service bd74e6
      }
Packit Service bd74e6
    }
Packit Service bd74e6
#endif
Packit Service bd74e6
Packit Service bd74e6
    *fold = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
Packit Service bd74e6
    (*pp)++;
Packit Service bd74e6
    return 1; /* return byte length of converted char to lower */
Packit Service bd74e6
  }
Packit Service bd74e6
  else {
Packit Service bd74e6
    return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF8, flag,
Packit Service bd74e6
					 pp, end, fold);
Packit Service bd74e6
  }
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
get_ctype_code_range(OnigCtype ctype, OnigCodePoint *sb_out,
Packit Service bd74e6
		     const OnigCodePoint* ranges[])
Packit Service bd74e6
{
Packit Service bd74e6
  *sb_out = 0x80;
Packit Service bd74e6
  return onigenc_unicode_ctype_code_range(ctype, ranges);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
Packit Service bd74e6
static UChar*
Packit Service bd74e6
left_adjust_char_head(const UChar* start, const UChar* s)
Packit Service bd74e6
{
Packit Service bd74e6
  const UChar *p;
Packit Service bd74e6
Packit Service bd74e6
  if (s <= start) return (UChar* )s;
Packit Service bd74e6
  p = s;
Packit Service bd74e6
Packit Service bd74e6
  while (!utf8_islead(*p) && p > start) p--;
Packit Service bd74e6
  return (UChar* )p;
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
static int
Packit Service bd74e6
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_UTF8,
Packit Service bd74e6
						    flag, p, end, items);
Packit Service bd74e6
}
Packit Service bd74e6
Packit Service bd74e6
OnigEncodingType OnigEncodingUTF8 = {
Packit Service bd74e6
  mbc_enc_len,
Packit Service bd74e6
  "UTF-8",     /* name */
Packit Service bd74e6
  6,           /* max enc length */
Packit Service bd74e6
  1,           /* min enc length */
Packit Service bd74e6
  onigenc_is_mbc_newline_0x0a,
Packit Service bd74e6
  mbc_to_code,
Packit Service bd74e6
  code_to_mbclen,
Packit Service bd74e6
  code_to_mbc,
Packit Service bd74e6
  mbc_case_fold,
Packit Service bd74e6
  onigenc_unicode_apply_all_case_fold,
Packit Service bd74e6
  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
  get_ctype_code_range,
Packit Service bd74e6
  left_adjust_char_head,
Packit Service bd74e6
  onigenc_always_true_is_allowed_reverse_match,
Packit Service bd74e6
  NULL, /* init */
Packit Service bd74e6
  NULL, /* is_initialized */
Packit Service bd74e6
  is_valid_mbc_string,
Packit Service bd74e6
  ENC_FLAG_ASCII_COMPATIBLE|ENC_FLAG_UNICODE,
Packit Service bd74e6
  0, 0
Packit Service bd74e6
};