Index: EcnlProtoTool/trunk/onigmo-6.1.3/src/regenc.c
===================================================================
--- EcnlProtoTool/trunk/onigmo-5.15.0/src/regenc.c	(revision 321)
+++ EcnlProtoTool/trunk/onigmo-6.1.3/src/regenc.c	(revision 331)
@@ -4,5 +4,5 @@
 /*-
  * Copyright (c) 2002-2007  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
- * Copyright (c) 2011       K.Takata  <kentkt AT csc DOT jp>
+ * Copyright (c) 2011-2016  K.Takata  <kentkt AT csc DOT jp>
  * All rights reserved.
  *
@@ -52,10 +52,21 @@
 }
 
+extern int
+onigenc_mbclen_approximate(const OnigUChar* p,const OnigUChar* e, OnigEncoding enc)
+{
+  int ret = ONIGENC_PRECISE_MBC_ENC_LEN(enc, p, e);
+  if (ONIGENC_MBCLEN_CHARFOUND_P(ret))
+    return ONIGENC_MBCLEN_CHARFOUND_LEN(ret);
+  else if (ONIGENC_MBCLEN_NEEDMORE_P(ret))
+    return (int )(e - p) + ONIGENC_MBCLEN_NEEDMORE_LEN(ret);
+  return 1;
+}
+
 extern UChar*
-onigenc_get_right_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s)
-{
-  UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s);
+onigenc_get_right_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s, const UChar* end)
+{
+  UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end);
   if (p < s) {
-    p += enclen(enc, p);
+    p += enclen(enc, p, end);
   }
   return p;
@@ -64,11 +75,11 @@
 extern UChar*
 onigenc_get_right_adjust_char_head_with_prev(OnigEncoding enc,
-				   const UChar* start, const UChar* s, const UChar** prev)
-{
-  UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s);
+				   const UChar* start, const UChar* s, const UChar* end, const UChar** prev)
+{
+  UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end);
 
   if (p < s) {
     if (prev) *prev = (const UChar* )p;
-    p += enclen(enc, p);
+    p += enclen(enc, p, end);
   }
   else {
@@ -79,14 +90,14 @@
 
 extern UChar*
-onigenc_get_prev_char_head(OnigEncoding enc, const UChar* start, const UChar* s)
+onigenc_get_prev_char_head(OnigEncoding enc, const UChar* start, const UChar* s, const UChar* end)
 {
   if (s <= start)
     return (UChar* )NULL;
 
-  return ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s - 1);
+  return ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s - 1, end);
 }
 
 extern UChar*
-onigenc_step_back(OnigEncoding enc, const UChar* start, const UChar* s, int n)
+onigenc_step_back(OnigEncoding enc, const UChar* start, const UChar* s, const UChar* end, int n)
 {
   while (ONIG_IS_NOT_NULL(s) && n-- > 0) {
@@ -94,5 +105,5 @@
       return (UChar* )NULL;
 
-    s = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s - 1);
+    s = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s - 1, end);
   }
   return (UChar* )s;
@@ -104,5 +115,5 @@
   UChar* q = (UChar* )p;
   while (n-- > 0) {
-    q += ONIGENC_MBC_ENC_LEN(enc, q);
+    q += ONIGENC_MBC_ENC_LEN(enc, q, end);
   }
   return (q <= end ? q : NULL);
@@ -116,5 +127,5 @@
 
   while (q < end) {
-    q += ONIGENC_MBC_ENC_LEN(enc, q);
+    q += ONIGENC_MBC_ENC_LEN(enc, q, end);
     n++;
   }
@@ -127,4 +138,5 @@
   int n = 0;
   UChar* p = (UChar* )s;
+  UChar* e;
 
   while (1) {
@@ -142,5 +154,6 @@
       if (len == 1) return n;
     }
-    p += ONIGENC_MBC_ENC_LEN(enc, p);
+    e = p + ONIGENC_MBC_MAXLEN(enc);
+    p += ONIGENC_MBC_ENC_LEN(enc, p, e);
     n++;
   }
@@ -152,4 +165,5 @@
   UChar* start = (UChar* )s;
   UChar* p = (UChar* )s;
+  UChar* e;
 
   while (1) {
@@ -167,5 +181,6 @@
       if (len == 1) return (int )(p - start);
     }
-    p += ONIGENC_MBC_ENC_LEN(enc, p);
+    e = p + ONIGENC_MBC_MAXLEN(enc);
+    p += ONIGENC_MBC_ENC_LEN(enc, p, e);
   }
 }
@@ -350,4 +365,5 @@
 #endif
 
+#if 0
 extern void
 onigenc_set_default_caseconv_table(const UChar* table ARG_UNUSED)
@@ -356,9 +372,10 @@
   /* obsoleted. */
 }
+#endif
 
 extern UChar*
-onigenc_get_left_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s)
-{
-  return ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s);
+onigenc_get_left_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s, const UChar* end)
+{
+  return ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end);
 }
 
@@ -394,5 +411,6 @@
 extern int
 onigenc_ascii_apply_all_case_fold(OnigCaseFoldType flag ARG_UNUSED,
-				  OnigApplyAllCaseFoldFunc f, void* arg)
+				  OnigApplyAllCaseFoldFunc f, void* arg,
+				  OnigEncoding enc ARG_UNUSED)
 {
   OnigCodePoint code;
@@ -415,5 +433,5 @@
 onigenc_ascii_get_case_fold_codes_by_str(OnigCaseFoldType flag ARG_UNUSED,
 	 const OnigUChar* p, const OnigUChar* end ARG_UNUSED,
-	 OnigCaseFoldCodeItem items[])
+	 OnigCaseFoldCodeItem items[], OnigEncoding enc ARG_UNUSED)
 {
   if (0x41 <= *p && *p <= 0x5a) {
@@ -451,5 +469,5 @@
   int i, r;
 
-  r = onigenc_ascii_apply_all_case_fold(flag, f, arg);
+  r = onigenc_ascii_apply_all_case_fold(flag, f, arg, 0);
   if (r != 0) return r;
 
@@ -555,5 +573,6 @@
 onigenc_not_support_get_ctype_code_range(OnigCtype ctype ARG_UNUSED,
 	 OnigCodePoint* sb_out ARG_UNUSED,
-	 const OnigCodePoint* ranges[] ARG_UNUSED)
+	 const OnigCodePoint* ranges[] ARG_UNUSED,
+	 OnigEncoding enc)
 {
   return ONIG_NO_SUPPORT_CONFIG;
@@ -561,5 +580,5 @@
 
 extern int
-onigenc_is_mbc_newline_0x0a(const UChar* p, const UChar* end)
+onigenc_is_mbc_newline_0x0a(const UChar* p, const UChar* end, OnigEncoding enc ARG_UNUSED)
 {
   if (p < end) {
@@ -572,5 +591,5 @@
 extern int
 onigenc_ascii_mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED, const UChar** p,
-	    const UChar*end ARG_UNUSED, UChar* lower)
+			    const UChar* end, UChar* lower, OnigEncoding enc ARG_UNUSED)
 {
   *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(**p);
@@ -593,5 +612,6 @@
 
 extern int
-onigenc_single_byte_mbc_enc_len(const UChar* p ARG_UNUSED)
+onigenc_single_byte_mbc_enc_len(const UChar* p ARG_UNUSED, const UChar* e ARG_UNUSED,
+				OnigEncoding enc ARG_UNUSED)
 {
   return 1;
@@ -599,5 +619,6 @@
 
 extern OnigCodePoint
-onigenc_single_byte_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
+onigenc_single_byte_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED,
+				OnigEncoding enc ARG_UNUSED)
 {
   return (OnigCodePoint )(*p);
@@ -605,12 +626,16 @@
 
 extern int
-onigenc_single_byte_code_to_mbclen(OnigCodePoint code ARG_UNUSED)
-{
-  return (code < 0x100 ? 1 : ONIGERR_INVALID_CODE_POINT_VALUE);
-}
-
-extern int
-onigenc_single_byte_code_to_mbc(OnigCodePoint code, UChar *buf)
-{
+onigenc_single_byte_code_to_mbclen(OnigCodePoint code ARG_UNUSED, OnigEncoding enc ARG_UNUSED)
+{
+  return 1;
+}
+
+extern int
+onigenc_single_byte_code_to_mbc(OnigCodePoint code, UChar *buf, OnigEncoding enc ARG_UNUSED)
+{
+#ifdef RUBY
+  if (code > 0xff)
+    rb_raise(rb_eRangeError, "%u out of char range", code);
+#endif
   *buf = (UChar )(code & 0xff);
   return 1;
@@ -619,5 +644,7 @@
 extern UChar*
 onigenc_single_byte_left_adjust_char_head(const UChar* start ARG_UNUSED,
-					  const UChar* s)
+					  const UChar* s,
+					  const UChar* end ARG_UNUSED,
+					  OnigEncoding enc ARG_UNUSED)
 {
   return (UChar* )s;
@@ -626,5 +653,6 @@
 extern int
 onigenc_always_true_is_allowed_reverse_match(const UChar* s   ARG_UNUSED,
-					     const UChar* end ARG_UNUSED)
+					     const UChar* end ARG_UNUSED,
+					     OnigEncoding enc ARG_UNUSED)
 {
   return TRUE;
@@ -633,5 +661,6 @@
 extern int
 onigenc_always_false_is_allowed_reverse_match(const UChar* s   ARG_UNUSED,
-					      const UChar* end ARG_UNUSED)
+					      const UChar* end ARG_UNUSED,
+					      OnigEncoding enc ARG_UNUSED)
 {
   return FALSE;
@@ -639,5 +668,6 @@
 
 extern int
-onigenc_ascii_is_code_ctype(OnigCodePoint code, unsigned int ctype)
+onigenc_ascii_is_code_ctype(OnigCodePoint code, unsigned int ctype,
+                            OnigEncoding enc ARG_UNUSED)
 {
   if (code < 128)
@@ -653,5 +683,5 @@
   OnigCodePoint n;
 
-  len = enclen(enc, p);
+  len = enclen(enc, p, end);
   n = (OnigCodePoint )(*p++);
   if (len == 1) return n;
@@ -681,5 +711,5 @@
     int i;
 
-    len = enclen(enc, p);
+    len = enclen(enc, p, end);
     for (i = 0; i < len; i++) {
       *lower++ = *p++;
@@ -708,12 +738,13 @@
 
 extern int
-onigenc_mb2_code_to_mbclen(OnigCodePoint code)
-{
-  if ((code & 0xff00) != 0) return 2;
-  else return 1;
-}
-
-extern int
-onigenc_mb4_code_to_mbclen(OnigCodePoint code)
+onigenc_mb2_code_to_mbclen(OnigCodePoint code, OnigEncoding enc ARG_UNUSED)
+{
+  if (code <= 0xff) return 1;
+  if (code <= 0xffff) return 2;
+  return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
+}
+
+extern int
+onigenc_mb4_code_to_mbclen(OnigCodePoint code, OnigEncoding enc ARG_UNUSED)
 {
        if ((code & 0xff000000) != 0) return 4;
@@ -734,5 +765,5 @@
 
 #if 1
-  if (enclen(enc, buf) != (p - buf))
+  if (enclen(enc, buf, p) != (p - buf))
     return ONIGERR_INVALID_CODE_POINT_VALUE;
 #endif
@@ -757,5 +788,5 @@
 
 #if 1
-  if (enclen(enc, buf) != (p - buf))
+  if (enclen(enc, buf, p) != (p - buf))
     return ONIGERR_INVALID_CODE_POINT_VALUE;
 #endif
@@ -764,5 +795,5 @@
 
 extern int
-onigenc_minimum_property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
+onigenc_minimum_property_name_to_ctype(OnigEncoding enc, const UChar* p, const UChar* end)
 {
   static const PosixBracketEntryType PBS[] = {
@@ -840,5 +871,5 @@
 
     sascii++;
-    p += enclen(enc, p);
+    p += enclen(enc, p, end);
   }
   return 0;
@@ -861,9 +892,10 @@
 
     sascii++;
-    p += enclen(enc, p);
+    p += enclen(enc, p, end);
   }
   return 0;
 }
 
+#if 0
 /* Property management */
 static int
@@ -918,15 +950,63 @@
   return 0;
 }
-
-extern int
-onigenc_property_list_init(int (*f)(void))
-{
-  int r;
-
-  THREAD_ATOMIC_START;
-
-  r = f();
-
-  THREAD_ATOMIC_END;
-  return r;
-}
+#endif
+
+extern int
+onigenc_ascii_only_case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, const OnigUChar* end,
+			    OnigUChar* to, OnigUChar* to_end, const struct OnigEncodingTypeST* enc)
+{
+  OnigCodePoint code;
+  OnigUChar *to_start = to;
+  OnigCaseFoldType flags = *flagP;
+  int codepoint_length;
+
+  while (*pp < end && to < to_end) {
+    codepoint_length = ONIGENC_PRECISE_MBC_ENC_LEN(enc, *pp, end);
+    if (codepoint_length < 0)
+      return codepoint_length; /* encoding invalid */
+    code = ONIGENC_MBC_TO_CODE(enc, *pp, end);
+    *pp += codepoint_length;
+
+    if (code >= 'a' && code <= 'z' && (flags & ONIGENC_CASE_UPCASE)) {
+      flags |= ONIGENC_CASE_MODIFIED;
+      code += 'A' - 'a';
+    } else if (code >= 'A' && code <= 'Z' &&
+	(flags & (ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_FOLD))) {
+      flags |= ONIGENC_CASE_MODIFIED;
+      code += 'a' - 'A';
+    }
+    to += ONIGENC_CODE_TO_MBC(enc, code, to);
+    if (flags & ONIGENC_CASE_TITLECASE)  /* switch from titlecase to lowercase for capitalize */
+      flags ^= (ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_TITLECASE);
+  }
+  *flagP = flags;
+  return (int )(to - to_start);
+}
+
+extern int
+onigenc_single_byte_ascii_only_case_map(OnigCaseFoldType* flagP, const OnigUChar** pp,
+					const OnigUChar* end, OnigUChar* to, OnigUChar* to_end,
+					const struct OnigEncodingTypeST* enc)
+{
+  OnigCodePoint code;
+  OnigUChar *to_start = to;
+  OnigCaseFoldType flags = *flagP;
+
+  while (*pp < end && to < to_end) {
+    code = *(*pp)++;
+
+    if (code >= 'a' && code <= 'z' && (flags & ONIGENC_CASE_UPCASE)) {
+      flags |= ONIGENC_CASE_MODIFIED;
+      code += 'A' - 'a';
+    } else if (code >= 'A' && code <= 'Z' &&
+	(flags & (ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_FOLD))) {
+      flags |= ONIGENC_CASE_MODIFIED;
+      code += 'a' - 'A';
+    }
+    *to++ = code;
+    if (flags & ONIGENC_CASE_TITLECASE)  /* switch from titlecase to lowercase for capitalize */
+      flags ^= (ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_TITLECASE);
+  }
+  *flagP = flags;
+  return (int )(to - to_start);
+}
