utf32_be.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**********************************************************************
  2. utf32_be.c - Oniguruma (regular expression library)
  3. **********************************************************************/
  4. /*-
  5. * Copyright (c) 2002-2007 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. #include "regenc.h"
  30. static int
  31. utf32be_mbc_enc_len(const UChar* p ARG_UNUSED)
  32. {
  33. return 4;
  34. }
  35. static int
  36. utf32be_is_mbc_newline(const UChar* p, const UChar* end)
  37. {
  38. if (p + 3 < end) {
  39. if (*(p+3) == 0x0a && *(p+2) == 0 && *(p+1) == 0 && *p == 0)
  40. return 1;
  41. #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
  42. if ((
  43. #ifndef USE_CRNL_AS_LINE_TERMINATOR
  44. *(p+3) == 0x0d ||
  45. #endif
  46. *(p+3) == 0x85)
  47. && *(p+2) == 0 && *(p+1) == 0 && *p == 0x00)
  48. return 1;
  49. if (*(p+2) == 0x20 && (*(p+3) == 0x29 || *(p+3) == 0x28)
  50. && *(p+1) == 0 && *p == 0)
  51. return 1;
  52. #endif
  53. }
  54. return 0;
  55. }
  56. static OnigCodePoint
  57. utf32be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
  58. {
  59. if (end - p < 4) return 0;
  60. return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
  61. }
  62. static int
  63. utf32be_code_to_mbclen(OnigCodePoint code ARG_UNUSED)
  64. {
  65. return 4;
  66. }
  67. static int
  68. utf32be_code_to_mbc(OnigCodePoint code, UChar *buf)
  69. {
  70. UChar* p = buf;
  71. *p++ = (UChar )((code & 0xff000000) >>24);
  72. *p++ = (UChar )((code & 0xff0000) >>16);
  73. *p++ = (UChar )((code & 0xff00) >> 8);
  74. *p++ = (UChar ) (code & 0xff);
  75. return 4;
  76. }
  77. static int
  78. utf32be_mbc_case_fold(OnigCaseFoldType flag,
  79. const UChar** pp, const UChar* end, UChar* fold)
  80. {
  81. const UChar* p = *pp;
  82. if (ONIGENC_IS_ASCII_CODE(*(p+3)) && *(p+2) == 0 && *(p+1) == 0 && *p == 0) {
  83. *fold++ = 0;
  84. *fold++ = 0;
  85. #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
  86. if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
  87. if (*(p+3) == 0x49) {
  88. *fold++ = 0x01;
  89. *fold = 0x31;
  90. (*pp) += 4;
  91. return 4;
  92. }
  93. }
  94. #endif
  95. *fold++ = 0;
  96. *fold = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*(p+3));
  97. *pp += 4;
  98. return 4;
  99. }
  100. else
  101. return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF32_BE, flag, pp, end,
  102. fold);
  103. }
  104. #if 0
  105. static int
  106. utf32be_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp, const UChar* end)
  107. {
  108. const UChar* p = *pp;
  109. (*pp) += 4;
  110. if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
  111. int c, v;
  112. p += 3;
  113. if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
  114. return TRUE;
  115. }
  116. c = *p;
  117. v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
  118. (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
  119. if ((v | BIT_CTYPE_LOWER) != 0) {
  120. /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
  121. if (c >= 0xaa && c <= 0xba)
  122. return FALSE;
  123. else
  124. return TRUE;
  125. }
  126. return (v != 0 ? TRUE : FALSE);
  127. }
  128. return FALSE;
  129. }
  130. #endif
  131. static UChar*
  132. utf32be_left_adjust_char_head(const UChar* start, const UChar* s)
  133. {
  134. int rem;
  135. if (s <= start) return (UChar* )s;
  136. rem = (s - start) % 4;
  137. return (UChar* )(s - rem);
  138. }
  139. static int
  140. utf32be_get_case_fold_codes_by_str(OnigCaseFoldType flag,
  141. const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
  142. {
  143. return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF32_BE,
  144. flag, p, end, items);
  145. }
  146. OnigEncodingType OnigEncodingUTF32_BE = {
  147. utf32be_mbc_enc_len,
  148. "UTF-32BE", /* name */
  149. 4, /* max byte length */
  150. 4, /* min byte length */
  151. utf32be_is_mbc_newline,
  152. utf32be_mbc_to_code,
  153. utf32be_code_to_mbclen,
  154. utf32be_code_to_mbc,
  155. utf32be_mbc_case_fold,
  156. onigenc_unicode_apply_all_case_fold,
  157. utf32be_get_case_fold_codes_by_str,
  158. onigenc_unicode_property_name_to_ctype,
  159. onigenc_unicode_is_code_ctype,
  160. onigenc_utf16_32_get_ctype_code_range,
  161. utf32be_left_adjust_char_head,
  162. onigenc_always_false_is_allowed_reverse_match
  163. };