1 /**********************************************************************
2   unicode.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2019  K.Kosako
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 struct PoolPropertyNameCtype {
33   short int name;
34   short int ctype;
35 };
36 
37 #define ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(code,ctype) \
38   ((EncUNICODE_ISO_8859_1_CtypeTable[code] & CTYPE_TO_BIT(ctype)) != 0)
39 
40 static const unsigned short EncUNICODE_ISO_8859_1_CtypeTable[256] = {
41   0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
42   0x4008, 0x428c, 0x4289, 0x4288, 0x4288, 0x4288, 0x4008, 0x4008,
43   0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
44   0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
45   0x4284, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
46   0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
47   0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0,
48   0x78b0, 0x78b0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
49   0x41a0, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x74a2,
50   0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2,
51   0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2,
52   0x74a2, 0x74a2, 0x74a2, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x51a0,
53   0x41a0, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x70e2,
54   0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2,
55   0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2,
56   0x70e2, 0x70e2, 0x70e2, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x4008,
57   0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0288, 0x0008, 0x0008,
58   0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
59   0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
60   0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
61   0x0284, 0x01a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0,
62   0x00a0, 0x00a0, 0x30e2, 0x01a0, 0x00a0, 0x00a8, 0x00a0, 0x00a0,
63   0x00a0, 0x00a0, 0x10a0, 0x10a0, 0x00a0, 0x30e2, 0x00a0, 0x01a0,
64   0x00a0, 0x10a0, 0x30e2, 0x01a0, 0x10a0, 0x10a0, 0x10a0, 0x01a0,
65   0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2,
66   0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2,
67   0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x00a0,
68   0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x30e2,
69   0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2,
70   0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2,
71   0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x00a0,
72   0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2
73 };
74 
75 #include "st.h"
76 
77 #include "unicode_fold_data.c"
78 
79 extern int
onigenc_unicode_mbc_case_fold(OnigEncoding enc,OnigCaseFoldType flag ARG_UNUSED,const UChar ** pp,const UChar * end,UChar * fold)80 onigenc_unicode_mbc_case_fold(OnigEncoding enc,
81     OnigCaseFoldType flag ARG_UNUSED, const UChar** pp, const UChar* end,
82     UChar* fold)
83 {
84   const struct ByUnfoldKey* buk;
85 
86   OnigCodePoint code;
87   int i, len, rlen;
88   const UChar *p = *pp;
89 
90   code = ONIGENC_MBC_TO_CODE(enc, p, end);
91   len = enclen(enc, p);
92   *pp += len;
93 
94 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
95   if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
96     if (code == 0x0130) {
97       return ONIGENC_CODE_TO_MBC(enc, 0x0069, fold);
98     }
99 #if 0
100     if (code == 0x0049) {
101       return ONIGENC_CODE_TO_MBC(enc, 0x0131, fold);
102     }
103 #endif
104   }
105 #endif
106 
107   buk = onigenc_unicode_unfold_key(code);
108   if (buk != 0) {
109     if (buk->fold_len == 1) {
110       return ONIGENC_CODE_TO_MBC(enc, *FOLDS1_FOLD(buk->index), fold);
111     }
112     else {
113       OnigCodePoint* addr;
114 
115       FOLDS_FOLD_ADDR_BUK(buk, addr);
116       rlen = 0;
117       for (i = 0; i < buk->fold_len; i++) {
118         OnigCodePoint c = addr[i];
119         len = ONIGENC_CODE_TO_MBC(enc, c, fold);
120         fold += len;
121         rlen += len;
122       }
123       return rlen;
124     }
125   }
126 
127   for (i = 0; i < len; i++) {
128     *fold++ = *p++;
129   }
130   return len;
131 }
132 
133 static int
apply_case_fold1(int from,int to,OnigApplyAllCaseFoldFunc f,void * arg)134 apply_case_fold1(int from, int to, OnigApplyAllCaseFoldFunc f, void* arg)
135 {
136   int i, j, k, n, r;
137 
138   for (i = from; i < to; ) {
139     OnigCodePoint fold = *FOLDS1_FOLD(i);
140     n = FOLDS1_UNFOLDS_NUM(i);
141     for (j = 0; j < n; j++) {
142       OnigCodePoint unfold = FOLDS1_UNFOLDS(i)[j];
143 
144       r = (*f)(fold, &unfold, 1, arg);
145       if (r != 0) return r;
146       r = (*f)(unfold, &fold, 1, arg);
147       if (r != 0) return r;
148 
149       for (k = 0; k < j; k++) {
150         OnigCodePoint unfold2 = FOLDS1_UNFOLDS(i)[k];
151         r = (*f)(unfold, &unfold2, 1, arg);
152         if (r != 0) return r;
153         r = (*f)(unfold2, &unfold, 1, arg);
154         if (r != 0) return r;
155       }
156     }
157 
158     i = FOLDS1_NEXT_INDEX(i);
159   }
160 
161   return 0;
162 }
163 
164 static int
apply_case_fold2(int from,int to,OnigApplyAllCaseFoldFunc f,void * arg)165 apply_case_fold2(int from, int to, OnigApplyAllCaseFoldFunc f, void* arg)
166 {
167   int i, j, k, n, r;
168 
169   for (i = from; i < to; ) {
170     OnigCodePoint* fold = FOLDS2_FOLD(i);
171     n = FOLDS2_UNFOLDS_NUM(i);
172     for (j = 0; j < n; j++) {
173       OnigCodePoint unfold = FOLDS2_UNFOLDS(i)[j];
174 
175       r = (*f)(unfold, fold, 2, arg);
176       if (r != 0) return r;
177 
178       for (k = 0; k < j; k++) {
179         OnigCodePoint unfold2 = FOLDS2_UNFOLDS(i)[k];
180         r = (*f)(unfold, &unfold2, 1, arg);
181         if (r != 0) return r;
182         r = (*f)(unfold2, &unfold, 1, arg);
183         if (r != 0) return r;
184       }
185     }
186 
187     i = FOLDS2_NEXT_INDEX(i);
188   }
189 
190   return 0;
191 }
192 
193 static int
apply_case_fold3(int from,int to,OnigApplyAllCaseFoldFunc f,void * arg)194 apply_case_fold3(int from, int to, OnigApplyAllCaseFoldFunc f, void* arg)
195 {
196   int i, j, k, n, r;
197 
198   for (i = from; i < to; ) {
199     OnigCodePoint* fold = FOLDS3_FOLD(i);
200     n = FOLDS3_UNFOLDS_NUM(i);
201     for (j = 0; j < n; j++) {
202       OnigCodePoint unfold = FOLDS3_UNFOLDS(i)[j];
203 
204       r = (*f)(unfold, fold, 3, arg);
205       if (r != 0) return r;
206 
207       for (k = 0; k < j; k++) {
208         OnigCodePoint unfold2 = FOLDS3_UNFOLDS(i)[k];
209         r = (*f)(unfold, &unfold2, 1, arg);
210         if (r != 0) return r;
211         r = (*f)(unfold2, &unfold, 1, arg);
212         if (r != 0) return r;
213       }
214     }
215 
216     i = FOLDS3_NEXT_INDEX(i);
217   }
218 
219   return 0;
220 }
221 
222 extern int
onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,OnigApplyAllCaseFoldFunc f,void * arg)223 onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
224                                     OnigApplyAllCaseFoldFunc f, void* arg)
225 {
226   int r;
227 
228   r = apply_case_fold1(0, FOLDS1_NORMAL_END_INDEX, f, arg);
229   if (r != 0) return r;
230 
231 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
232   if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
233     code = 0x0131;
234     r = (*f)(0x0049, &code, 1, arg);
235     if (r != 0) return r;
236     code = 0x0049;
237     r = (*f)(0x0131, &code, 1, arg);
238     if (r != 0) return r;
239 
240     code = 0x0130;
241     r = (*f)(0x0069, &code, 1, arg);
242     if (r != 0) return r;
243     code = 0x0069;
244     r = (*f)(0x0130, &code, 1, arg);
245     if (r != 0) return r;
246   }
247   else {
248 #endif
249     r = apply_case_fold1(FOLDS1_NORMAL_END_INDEX, FOLDS1_END_INDEX, f, arg);
250     if (r != 0) return r;
251 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
252   }
253 #endif
254 
255   if ((flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) == 0)
256     return 0;
257 
258   r = apply_case_fold2(0, FOLDS2_NORMAL_END_INDEX, f, arg);
259   if (r != 0) return r;
260 
261 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
262   if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) == 0) {
263 #endif
264     r = apply_case_fold2(FOLDS2_NORMAL_END_INDEX, FOLDS2_END_INDEX, f, arg);
265     if (r != 0) return r;
266 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
267   }
268 #endif
269 
270   r = apply_case_fold3(0, FOLDS3_NORMAL_END_INDEX, f, arg);
271   if (r != 0) return r;
272 
273   return 0;
274 }
275 
276 extern int
onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,OnigCaseFoldType flag,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])277 onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
278     OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end,
279     OnigCaseFoldCodeItem items[])
280 {
281   int n, m, i, j, k, len, lens[3];
282   int index;
283   int fn, ncs[3];
284   OnigCodePoint cs[3][4];
285   OnigCodePoint code, codes[3], orig_codes[3];
286   const struct ByUnfoldKey* buk1;
287 
288   n = 0;
289 
290   code = ONIGENC_MBC_TO_CODE(enc, p, end);
291   len = enclen(enc, p);
292 
293 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
294   if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
295     if (code == 0x0049) {
296       items[0].byte_len = len;
297       items[0].code_len = 1;
298       items[0].code[0]  = 0x0131;
299       return 1;
300     }
301     else if (code == 0x0130) {
302       items[0].byte_len = len;
303       items[0].code_len = 1;
304       items[0].code[0]  = 0x0069;
305       return 1;
306     }
307     else if (code == 0x0131) {
308       items[0].byte_len = len;
309       items[0].code_len = 1;
310       items[0].code[0]  = 0x0049;
311       return 1;
312     }
313     else if (code == 0x0069) {
314       items[0].byte_len = len;
315       items[0].code_len = 1;
316       items[0].code[0]  = 0x0130;
317       return 1;
318     }
319   }
320 #endif
321 
322   orig_codes[0] = code;
323   lens[0] = len;
324   p += len;
325 
326   buk1 = onigenc_unicode_unfold_key(orig_codes[0]);
327   if (buk1 != 0 && buk1->fold_len == 1) {
328     codes[0] = *FOLDS1_FOLD(buk1->index);
329   }
330   else
331     codes[0] = orig_codes[0];
332 
333   if ((flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) == 0)
334     goto fold1;
335 
336   if (p < end) {
337     const struct ByUnfoldKey* buk;
338 
339     code = ONIGENC_MBC_TO_CODE(enc, p, end);
340     orig_codes[1] = code;
341     len = enclen(enc, p);
342     lens[1] = lens[0] + len;
343     buk = onigenc_unicode_unfold_key(orig_codes[1]);
344     if (buk != 0 && buk->fold_len == 1) {
345       codes[1] = *FOLDS1_FOLD(buk->index);
346     }
347     else
348       codes[1] = orig_codes[1];
349 
350     p += len;
351     if (p < end) {
352       code = ONIGENC_MBC_TO_CODE(enc, p, end);
353       orig_codes[2] = code;
354       len = enclen(enc, p);
355       lens[2] = lens[1] + len;
356       buk = onigenc_unicode_unfold_key(orig_codes[2]);
357       if (buk != 0 && buk->fold_len == 1) {
358         codes[2] = *FOLDS1_FOLD(buk->index);
359       }
360       else
361         codes[2] = orig_codes[2];
362 
363       index = onigenc_unicode_fold3_key(codes);
364       if (index >= 0) {
365         m = FOLDS3_UNFOLDS_NUM(index);
366         for (i = 0; i < m; i++) {
367           items[n].byte_len = lens[2];
368           items[n].code_len = 1;
369           items[n].code[0]  = FOLDS3_UNFOLDS(index)[i];
370           n++;
371         }
372 
373         for (fn = 0; fn < 3; fn++) {
374           int sindex;
375           cs[fn][0] = FOLDS3_FOLD(index)[fn];
376           ncs[fn] = 1;
377           sindex = onigenc_unicode_fold1_key(&cs[fn][0]);
378           if (sindex >= 0) {
379             int m = FOLDS1_UNFOLDS_NUM(sindex);
380             for (i = 0; i < m; i++) {
381               cs[fn][i+1] = FOLDS1_UNFOLDS(sindex)[i];
382             }
383             ncs[fn] += m;
384           }
385         }
386 
387         for (i = 0; i < ncs[0]; i++) {
388           for (j = 0; j < ncs[1]; j++) {
389             for (k = 0; k < ncs[2]; k++) {
390               if (cs[0][i] == orig_codes[0] && cs[1][j] == orig_codes[1] &&
391                   cs[2][k] == orig_codes[2])
392                 continue;
393 
394               items[n].byte_len = lens[2];
395               items[n].code_len = 3;
396               items[n].code[0]  = cs[0][i];
397               items[n].code[1]  = cs[1][j];
398               items[n].code[2]  = cs[2][k];
399               n++;
400             }
401           }
402         }
403 
404         return n;
405       }
406     }
407 
408     index = onigenc_unicode_fold2_key(codes);
409     if (index >= 0) {
410       m = FOLDS2_UNFOLDS_NUM(index);
411       for (i = 0; i < m; i++) {
412         items[n].byte_len = lens[1];
413         items[n].code_len = 1;
414         items[n].code[0]  = FOLDS2_UNFOLDS(index)[i];
415         n++;
416       }
417 
418       for (fn = 0; fn < 2; fn++) {
419         int sindex;
420         cs[fn][0] = FOLDS2_FOLD(index)[fn];
421         ncs[fn] = 1;
422         sindex = onigenc_unicode_fold1_key(&cs[fn][0]);
423         if (sindex >= 0) {
424           int m = FOLDS1_UNFOLDS_NUM(sindex);
425           for (i = 0; i < m; i++) {
426             cs[fn][i+1] = FOLDS1_UNFOLDS(sindex)[i];
427           }
428           ncs[fn] += m;
429         }
430       }
431 
432       for (i = 0; i < ncs[0]; i++) {
433         for (j = 0; j < ncs[1]; j++) {
434           if (cs[0][i] == orig_codes[0] && cs[1][j] == orig_codes[1])
435             continue;
436           items[n].byte_len = lens[1];
437           items[n].code_len = 2;
438           items[n].code[0]  = cs[0][i];
439           items[n].code[1]  = cs[1][j];
440           n++;
441         }
442       }
443 
444       return n;
445     }
446   }
447 
448  fold1:
449   if (buk1 != 0) {
450     if (buk1->fold_len == 1) {
451       int un;
452       items[0].byte_len = lens[0];
453       items[0].code_len = 1;
454       items[0].code[0]  = *FOLDS1_FOLD(buk1->index);
455       n++;
456 
457       un = FOLDS1_UNFOLDS_NUM(buk1->index);
458       for (i = 0; i < un; i++) {
459         OnigCodePoint unfold = FOLDS1_UNFOLDS(buk1->index)[i];
460         if (unfold != orig_codes[0]) {
461           items[n].byte_len = lens[0];
462           items[n].code_len = 1;
463           items[n].code[0]  = unfold;
464           n++;
465         }
466       }
467     }
468     else if ((flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
469       if (buk1->fold_len == 2) {
470         m = FOLDS2_UNFOLDS_NUM(buk1->index);
471         for (i = 0; i < m; i++) {
472           OnigCodePoint unfold = FOLDS2_UNFOLDS(buk1->index)[i];
473           if (unfold == orig_codes[0]) continue;
474 
475           items[n].byte_len = lens[0];
476           items[n].code_len = 1;
477           items[n].code[0]  = unfold;
478           n++;
479         }
480 
481         for (fn = 0; fn < 2; fn++) {
482           int index;
483           cs[fn][0] = FOLDS2_FOLD(buk1->index)[fn];
484           ncs[fn] = 1;
485           index = onigenc_unicode_fold1_key(&cs[fn][0]);
486           if (index >= 0) {
487             int m = FOLDS1_UNFOLDS_NUM(index);
488             for (i = 0; i < m; i++) {
489               cs[fn][i+1] = FOLDS1_UNFOLDS(index)[i];
490             }
491             ncs[fn] += m;
492           }
493         }
494 
495         for (i = 0; i < ncs[0]; i++) {
496           for (j = 0; j < ncs[1]; j++) {
497             items[n].byte_len = lens[0];
498             items[n].code_len = 2;
499             items[n].code[0]  = cs[0][i];
500             items[n].code[1]  = cs[1][j];
501             n++;
502           }
503         }
504       }
505       else { /* fold_len == 3 */
506         m = FOLDS3_UNFOLDS_NUM(buk1->index);
507         for (i = 0; i < m; i++) {
508           OnigCodePoint unfold = FOLDS3_UNFOLDS(buk1->index)[i];
509           if (unfold == orig_codes[0]) continue;
510 
511           items[n].byte_len = lens[0];
512           items[n].code_len = 1;
513           items[n].code[0]  = unfold;
514           n++;
515         }
516 
517         for (fn = 0; fn < 3; fn++) {
518           int index;
519           cs[fn][0] = FOLDS3_FOLD(buk1->index)[fn];
520           ncs[fn] = 1;
521           index = onigenc_unicode_fold1_key(&cs[fn][0]);
522           if (index >= 0) {
523             int m = FOLDS1_UNFOLDS_NUM(index);
524             for (i = 0; i < m; i++) {
525               cs[fn][i+1] = FOLDS1_UNFOLDS(index)[i];
526             }
527             ncs[fn] += m;
528           }
529         }
530 
531         for (i = 0; i < ncs[0]; i++) {
532           for (j = 0; j < ncs[1]; j++) {
533             for (k = 0; k < ncs[2]; k++) {
534               items[n].byte_len = lens[0];
535               items[n].code_len = 3;
536               items[n].code[0]  = cs[0][i];
537               items[n].code[1]  = cs[1][j];
538               items[n].code[2]  = cs[2][k];
539               n++;
540             }
541           }
542         }
543       }
544     }
545   }
546   else {
547     int index = onigenc_unicode_fold1_key(orig_codes);
548     if (index >= 0) {
549       int m = FOLDS1_UNFOLDS_NUM(index);
550       for (i = 0; i < m; i++) {
551         items[n].byte_len = lens[0];
552         items[n].code_len = 1;
553         items[n].code[0]  = FOLDS1_UNFOLDS(index)[i];
554         n++;
555       }
556     }
557   }
558 
559   return n;
560 }
561 
562 #ifdef USE_UNICODE_PROPERTIES
563 #include "unicode_property_data.c"
564 #else
565 #include "unicode_property_data_posix.c"
566 #endif
567 
568 
569 #ifdef USE_UNICODE_WORD_BREAK
570 
571 enum WB_TYPE {
572   WB_Any = 0,
573   WB_ALetter,
574   WB_CR,
575   WB_Double_Quote,
576   WB_Extend,
577   WB_ExtendNumLet,
578   WB_Format,
579   WB_Hebrew_Letter,
580   WB_Katakana,
581   WB_LF,
582   WB_MidLetter,
583   WB_MidNum,
584   WB_MidNumLet,
585   WB_Newline,
586   WB_Numeric,
587   WB_Regional_Indicator,
588   WB_Single_Quote,
589   WB_WSegSpace,
590   WB_ZWJ,
591 };
592 
593 typedef struct {
594   OnigCodePoint start;
595   OnigCodePoint end;
596   enum WB_TYPE  type;
597 } WB_RANGE_TYPE;
598 
599 #include "unicode_wb_data.c"
600 
601 static enum WB_TYPE
wb_get_type(OnigCodePoint code)602 wb_get_type(OnigCodePoint code)
603 {
604   OnigCodePoint low, high, x;
605   enum WB_TYPE type;
606 
607   for (low = 0, high = (OnigCodePoint )WB_RANGE_NUM; low < high; ) {
608     x = (low + high) >> 1;
609     if (code > WB_RANGES[x].end)
610       low = x + 1;
611     else
612       high = x;
613   }
614 
615   type = (low < (OnigCodePoint )WB_RANGE_NUM &&
616           code >= WB_RANGES[low].start) ?
617     WB_RANGES[low].type : WB_Any;
618 
619   return type;
620 }
621 
622 #define IS_WB_IGNORE_TAIL(t)  ((t) == WB_Extend || (t) == WB_Format || (t) == WB_ZWJ)
623 #define IS_WB_AHLetter(t)     ((t) == WB_ALetter || (t) == WB_Hebrew_Letter)
624 #define IS_WB_MidNumLetQ(t)   ((t) == WB_MidNumLet || (t) == WB_Single_Quote)
625 
626 static int
wb_get_next_main_code(OnigEncoding enc,UChar * p,const UChar * end,OnigCodePoint * rcode,enum WB_TYPE * rtype)627 wb_get_next_main_code(OnigEncoding enc, UChar* p, const UChar* end,
628                       OnigCodePoint* rcode, enum WB_TYPE* rtype)
629 {
630   OnigCodePoint code;
631   enum WB_TYPE type;
632 
633   while (TRUE) {
634     p += enclen(enc, p);
635     if (p >= end) break;
636 
637     code = ONIGENC_MBC_TO_CODE(enc, p, end);
638     type = wb_get_type(code);
639     if (! IS_WB_IGNORE_TAIL(type)) {
640       *rcode = code;
641       *rtype = type;
642       return 1;
643     }
644   }
645 
646   return 0;
647 }
648 
649 extern int
onigenc_wb_is_break_position(OnigEncoding enc,UChar * p,UChar * prev,const UChar * start,const UChar * end)650 onigenc_wb_is_break_position(OnigEncoding enc, UChar* p, UChar* prev,
651                              const UChar* start, const UChar* end)
652 {
653   int r;
654   UChar* pp;
655   OnigCodePoint cfrom;
656   OnigCodePoint cfrom2;
657   OnigCodePoint cto;
658   OnigCodePoint cto2;
659   enum WB_TYPE from;
660   enum WB_TYPE from2;
661   enum WB_TYPE to;
662   enum WB_TYPE to2;
663 
664   /* WB1: sot / Any */
665   if (p == start) return TRUE;
666   /* WB2: Any / eot */
667   if (p == end)   return TRUE;
668 
669   if (IS_NULL(prev)) {
670     prev = onigenc_get_prev_char_head(enc, start, p);
671     if (IS_NULL(prev)) return TRUE;
672   }
673 
674   cfrom = ONIGENC_MBC_TO_CODE(enc, prev, end);
675   cto   = ONIGENC_MBC_TO_CODE(enc, p, end);
676 
677   from = wb_get_type(cfrom);
678   to   = wb_get_type(cto);
679 
680   /* short cut */
681   if (from == 0 && to == 0) goto WB999;
682 
683   /* WB3: CR + LF */
684   if (from == WB_CR && to == WB_LF) return FALSE;
685 
686   /* WB3a: (Newline|CR|LF) /  */
687   if (from == WB_Newline || from == WB_CR || from == WB_LF) return TRUE;
688   /* WB3b: / (Newline|CR|LF) */
689   if (to == WB_Newline || to == WB_CR || to == WB_LF) return TRUE;
690 
691   /* WB3c: ZWJ + {Extended_Pictographic} */
692   if (from == WB_ZWJ) {
693     if (onigenc_unicode_is_code_ctype(cto, PROP_INDEX_EXTENDEDPICTOGRAPHIC))
694       return FALSE;
695   }
696 
697   /* WB3d: WSegSpace + WSegSpace */
698   if (from == WB_WSegSpace && to == WB_WSegSpace) return FALSE;
699 
700   /* WB4:  X (Extend|Format|ZWJ)* -> X */
701   if (IS_WB_IGNORE_TAIL(to)) return FALSE;
702   if (IS_WB_IGNORE_TAIL(from)) {
703     while ((pp = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
704       prev = pp;
705       cfrom = ONIGENC_MBC_TO_CODE(enc, prev, end);
706       from = wb_get_type(cfrom);
707       if (! IS_WB_IGNORE_TAIL(from))
708         break;
709     }
710   }
711 
712   if (IS_WB_AHLetter(from)) {
713     /* WB5: AHLetter + AHLetter */
714     if (IS_WB_AHLetter(to)) return FALSE;
715 
716     /* WB6: AHLetter + (MidLetter | MidNumLetQ) AHLetter */
717     if (to == WB_MidLetter || IS_WB_MidNumLetQ(to)) {
718       r = wb_get_next_main_code(enc, p, end, &cto2, &to2);
719       if (r == 1) {
720         if (IS_WB_AHLetter(to2)) return FALSE;
721       }
722     }
723   }
724 
725   /* WB7: AHLetter (MidLetter | MidNumLetQ) + AHLetter */
726   if (from == WB_MidLetter || IS_WB_MidNumLetQ(from)) {
727     if (IS_WB_AHLetter(to)) {
728       from2 = WB_Any;
729       while ((pp = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
730         prev = pp;
731         cfrom2 = ONIGENC_MBC_TO_CODE(enc, prev, end);
732         from2 = wb_get_type(cfrom2);
733         if (! IS_WB_IGNORE_TAIL(from2))
734           break;
735       }
736 
737       if (IS_WB_AHLetter(from2)) return FALSE;
738     }
739   }
740 
741   if (from == WB_Hebrew_Letter) {
742     /* WB7a: Hebrew_Letter + Single_Quote */
743     if (to == WB_Single_Quote) return FALSE;
744 
745     /* WB7b: Hebrew_Letter + Double_Quote Hebrew_Letter */
746     if (to == WB_Double_Quote) {
747       r = wb_get_next_main_code(enc, p, end, &cto2, &to2);
748       if (r == 1) {
749         if (to2 == WB_Hebrew_Letter) return FALSE;
750       }
751     }
752   }
753 
754   /* WB7c: Hebrew_Letter Double_Quote + Hebrew_Letter */
755   if (from == WB_Double_Quote) {
756     if (to == WB_Hebrew_Letter) {
757       from2 = WB_Any;
758       while ((pp = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
759         prev = pp;
760         cfrom2 = ONIGENC_MBC_TO_CODE(enc, prev, end);
761         from2 = wb_get_type(cfrom2);
762         if (! IS_WB_IGNORE_TAIL(from2))
763           break;
764       }
765 
766       if (from2 == WB_Hebrew_Letter) return FALSE;
767     }
768   }
769 
770   if (to == WB_Numeric) {
771     /* WB8: Numeric + Numeric */
772     if (from == WB_Numeric) return FALSE;
773 
774     /* WB9: AHLetter + Numeric */
775     if (IS_WB_AHLetter(from)) return FALSE;
776 
777     /* WB11: Numeric (MidNum | MidNumLetQ) + Numeric */
778     if (from == WB_MidNum || IS_WB_MidNumLetQ(from)) {
779       from2 = WB_Any;
780       while ((pp = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
781         prev = pp;
782         cfrom2 = ONIGENC_MBC_TO_CODE(enc, prev, end);
783         from2 = wb_get_type(cfrom2);
784         if (! IS_WB_IGNORE_TAIL(from2))
785           break;
786       }
787 
788       if (from2 == WB_Numeric) return FALSE;
789     }
790   }
791 
792   if (from == WB_Numeric) {
793     /* WB10: Numeric + AHLetter */
794     if (IS_WB_AHLetter(to)) return FALSE;
795 
796     /* WB12: Numeric + (MidNum | MidNumLetQ) Numeric */
797     if (to == WB_MidNum || IS_WB_MidNumLetQ(to)) {
798       r = wb_get_next_main_code(enc, p, end, &cto2, &to2);
799       if (r == 1) {
800         if (to2 == WB_Numeric) return FALSE;
801       }
802     }
803   }
804 
805   /* WB13: Katakana + Katakana */
806   if (from == WB_Katakana && to == WB_Katakana) return FALSE;
807 
808   /* WB13a: (AHLetter | Numeric | Katakana | ExtendNumLet) + ExtendNumLet */
809   if (IS_WB_AHLetter(from) || from == WB_Numeric || from == WB_Katakana
810       || from == WB_ExtendNumLet) {
811     if (to == WB_ExtendNumLet) return FALSE;
812   }
813 
814   /* WB13b: ExtendNumLet + (AHLetter | Numeric | Katakana) */
815   if (from == WB_ExtendNumLet) {
816     if (IS_WB_AHLetter(to) || to == WB_Numeric || to == WB_Katakana)
817       return FALSE;
818   }
819 
820 
821   /* WB15:   sot (RI RI)* RI + RI */
822   /* WB16: [^RI] (RI RI)* RI + RI */
823   if (from == WB_Regional_Indicator && to == WB_Regional_Indicator) {
824     int n = 0;
825     while ((prev = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
826       cfrom2 = ONIGENC_MBC_TO_CODE(enc, prev, end);
827       from2  = wb_get_type(cfrom2);
828       if (from2 != WB_Regional_Indicator)
829         break;
830 
831       n++;
832     }
833     if ((n % 2) == 0) return FALSE;
834   }
835 
836  WB999:
837   /* WB999: Any / Any */
838   return TRUE;
839 }
840 
841 #endif /* USE_UNICODE_WORD_BREAK */
842 
843 
844 #ifdef USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER
845 
846 enum EGCB_BREAK_TYPE {
847   EGCB_NOT_BREAK = 0,
848   EGCB_BREAK     = 1,
849   EGCB_BREAK_UNDEF_GB11  = 2,
850   EGCB_BREAK_UNDEF_RI_RI = 3
851 };
852 
853 enum EGCB_TYPE {
854   EGCB_Other   = 0,
855   EGCB_CR      = 1,
856   EGCB_LF      = 2,
857   EGCB_Control = 3,
858   EGCB_Extend  = 4,
859   EGCB_Prepend = 5,
860   EGCB_Regional_Indicator = 6,
861   EGCB_SpacingMark = 7,
862   EGCB_ZWJ         = 8,
863 #if 0
864   /* obsoleted */
865   EGCB_E_Base         = 9,
866   EGCB_E_Base_GAZ     = 10,
867   EGCB_E_Modifier     = 11,
868   EGCB_Glue_After_Zwj = 12,
869 #endif
870   EGCB_L   = 13,
871   EGCB_LV  = 14,
872   EGCB_LVT = 15,
873   EGCB_T   = 16,
874   EGCB_V   = 17
875 };
876 
877 typedef struct {
878   OnigCodePoint  start;
879   OnigCodePoint  end;
880   enum EGCB_TYPE type;
881 } EGCB_RANGE_TYPE;
882 
883 #include "unicode_egcb_data.c"
884 
885 static enum EGCB_TYPE
egcb_get_type(OnigCodePoint code)886 egcb_get_type(OnigCodePoint code)
887 {
888   OnigCodePoint low, high, x;
889   enum EGCB_TYPE type;
890 
891   for (low = 0, high = (OnigCodePoint )EGCB_RANGE_NUM; low < high; ) {
892     x = (low + high) >> 1;
893     if (code > EGCB_RANGES[x].end)
894       low = x + 1;
895     else
896       high = x;
897   }
898 
899   type = (low < (OnigCodePoint )EGCB_RANGE_NUM &&
900           code >= EGCB_RANGES[low].start) ?
901     EGCB_RANGES[low].type : EGCB_Other;
902 
903   return type;
904 }
905 
906 #define IS_CONTROL_CR_LF(code)   ((code) <= EGCB_Control && (code) >= EGCB_CR)
907 #define IS_HANGUL(code)          ((code) >= EGCB_L)
908 
909 /* GB1 and GB2 are outside of this function. */
910 static enum EGCB_BREAK_TYPE
unicode_egcb_is_break_2code(OnigCodePoint from_code,OnigCodePoint to_code)911 unicode_egcb_is_break_2code(OnigCodePoint from_code, OnigCodePoint to_code)
912 {
913   enum EGCB_TYPE from;
914   enum EGCB_TYPE to;
915 
916   from = egcb_get_type(from_code);
917   to   = egcb_get_type(to_code);
918 
919   /* short cut */
920   if (from == 0 && to == 0) goto GB999;
921 
922   /* GB3 */
923   if (from == EGCB_CR && to == EGCB_LF) return EGCB_NOT_BREAK;
924   /* GB4 */
925   if (IS_CONTROL_CR_LF(from)) return EGCB_BREAK;
926   /* GB5 */
927   if (IS_CONTROL_CR_LF(to)) return EGCB_BREAK;
928 
929   if (IS_HANGUL(from) && IS_HANGUL(to)) {
930     /* GB6 */
931     if (from == EGCB_L && to != EGCB_T) return EGCB_NOT_BREAK;
932     /* GB7 */
933     if ((from == EGCB_LV || from == EGCB_V)
934         && (to == EGCB_V || to == EGCB_T)) return EGCB_NOT_BREAK;
935 
936     /* GB8 */
937     if ((to == EGCB_T) && (from == EGCB_LVT || from == EGCB_T))
938       return EGCB_NOT_BREAK;
939 
940     goto GB999;
941   }
942 
943   /* GB9 */
944   if (to == EGCB_Extend || to == EGCB_ZWJ) return EGCB_NOT_BREAK;
945 
946   /* GB9a */
947   if (to == EGCB_SpacingMark) return EGCB_NOT_BREAK;
948   /* GB9b */
949   if (from == EGCB_Prepend) return EGCB_NOT_BREAK;
950 
951   /* GB10 removed */
952 
953   /* GB11 */
954   if (from == EGCB_ZWJ) {
955     if (onigenc_unicode_is_code_ctype(to_code, PROP_INDEX_EXTENDEDPICTOGRAPHIC))
956       return EGCB_BREAK_UNDEF_GB11;
957 
958     goto GB999;
959   }
960 
961   /* GB12, GB13 */
962   if (from == EGCB_Regional_Indicator && to == EGCB_Regional_Indicator) {
963     return EGCB_BREAK_UNDEF_RI_RI;
964   }
965 
966  GB999:
967   return EGCB_BREAK;
968 }
969 
970 #endif /* USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER */
971 
972 extern int
onigenc_egcb_is_break_position(OnigEncoding enc,UChar * p,UChar * prev,const UChar * start,const UChar * end)973 onigenc_egcb_is_break_position(OnigEncoding enc, UChar* p, UChar* prev,
974                                const UChar* start, const UChar* end)
975 {
976   OnigCodePoint from;
977   OnigCodePoint to;
978 #ifdef USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER
979   enum EGCB_BREAK_TYPE btype;
980   enum EGCB_TYPE type;
981 #endif
982 
983   /* GB1 and GB2 */
984   if (p == start) return 1;
985   if (p == end)   return 1;
986 
987   if (IS_NULL(prev)) {
988     prev = onigenc_get_prev_char_head(enc, start, p);
989     if (IS_NULL(prev)) return 1;
990   }
991 
992   from = ONIGENC_MBC_TO_CODE(enc, prev, end);
993   to   = ONIGENC_MBC_TO_CODE(enc, p, end);
994 
995 #ifdef USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER
996   if (! ONIGENC_IS_UNICODE_ENCODING(enc)) {
997     return from != 0x000d || to != NEWLINE_CODE;
998   }
999 
1000   btype = unicode_egcb_is_break_2code(from, to);
1001   switch (btype) {
1002   case EGCB_NOT_BREAK:
1003     return 0;
1004     break;
1005   case EGCB_BREAK:
1006     return 1;
1007     break;
1008 
1009   case EGCB_BREAK_UNDEF_GB11:
1010     while ((prev = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
1011       from = ONIGENC_MBC_TO_CODE(enc, prev, end);
1012       if (onigenc_unicode_is_code_ctype(from, PROP_INDEX_EXTENDEDPICTOGRAPHIC))
1013         return 0;
1014 
1015       type = egcb_get_type(from);
1016       if (type != EGCB_Extend)
1017         break;
1018     }
1019     break;
1020 
1021   case EGCB_BREAK_UNDEF_RI_RI:
1022     {
1023       int n = 0;
1024       while ((prev = onigenc_get_prev_char_head(enc, start, prev)) != NULL) {
1025         from = ONIGENC_MBC_TO_CODE(enc, prev, end);
1026         type = egcb_get_type(from);
1027         if (type != EGCB_Regional_Indicator)
1028           break;
1029 
1030         n++;
1031       }
1032       if ((n % 2) == 0) return 0;
1033     }
1034     break;
1035   }
1036 
1037   return 1;
1038 
1039 #else
1040   return from != 0x000d || to != NEWLINE_CODE;
1041 #endif /* USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER */
1042 }
1043 
1044 
1045 #define USER_DEFINED_PROPERTY_MAX_NUM  20
1046 
1047 typedef struct {
1048   int ctype;
1049   OnigCodePoint* ranges;
1050 } UserDefinedPropertyValue;
1051 
1052 static int UserDefinedPropertyNum;
1053 static UserDefinedPropertyValue
1054 UserDefinedPropertyRanges[USER_DEFINED_PROPERTY_MAX_NUM];
1055 static st_table* UserDefinedPropertyTable;
1056 
1057 extern int
onig_unicode_define_user_property(const char * name,OnigCodePoint * ranges)1058 onig_unicode_define_user_property(const char* name, OnigCodePoint* ranges)
1059 {
1060   UserDefinedPropertyValue* e;
1061   int r;
1062   int i;
1063   int n;
1064   int len;
1065   int c;
1066   char* s;
1067   UChar* uname;
1068 
1069   if (UserDefinedPropertyNum >= USER_DEFINED_PROPERTY_MAX_NUM)
1070     return ONIGERR_TOO_MANY_USER_DEFINED_OBJECTS;
1071 
1072   len = (int )strlen(name);
1073   if (len >= PROPERTY_NAME_MAX_SIZE)
1074     return ONIGERR_TOO_LONG_PROPERTY_NAME;
1075 
1076   s = (char* )xmalloc(len + 1);
1077   if (s == 0)
1078     return ONIGERR_MEMORY;
1079 
1080   uname = (UChar* )name;
1081   n = 0;
1082   for (i = 0; i < len; i++) {
1083     c = uname[i];
1084     if (c < 0x20 || c >= 0x80) {
1085       xfree(s);
1086       return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
1087     }
1088 
1089     if (c != ' ' && c != '-' && c != '_') {
1090       s[n] = c;
1091       n++;
1092     }
1093   }
1094   s[n] = '\0';
1095 
1096   if (UserDefinedPropertyTable == 0) {
1097     UserDefinedPropertyTable = onig_st_init_strend_table_with_size(10);
1098     if (IS_NULL(UserDefinedPropertyTable)) {
1099       xfree(s);
1100       return ONIGERR_MEMORY;
1101     }
1102   }
1103 
1104   e = UserDefinedPropertyRanges + UserDefinedPropertyNum;
1105   e->ctype = CODE_RANGES_NUM + UserDefinedPropertyNum;
1106   e->ranges = ranges;
1107   r = onig_st_insert_strend(UserDefinedPropertyTable,
1108                             (const UChar* )s, (const UChar* )s + n,
1109                             (hash_data_type )((void* )e));
1110   if (r < 0) return r;
1111 
1112   UserDefinedPropertyNum++;
1113   return 0;
1114 }
1115 
1116 extern int
onigenc_unicode_is_code_ctype(OnigCodePoint code,unsigned int ctype)1117 onigenc_unicode_is_code_ctype(OnigCodePoint code, unsigned int ctype)
1118 {
1119   if (
1120 #ifdef USE_UNICODE_PROPERTIES
1121       ctype <= ONIGENC_MAX_STD_CTYPE &&
1122 #endif
1123       code < 256) {
1124     return ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(code, ctype);
1125   }
1126 
1127   if (ctype >= CODE_RANGES_NUM) {
1128     int index = ctype - CODE_RANGES_NUM;
1129     if (index < UserDefinedPropertyNum)
1130       return onig_is_in_code_range((UChar* )UserDefinedPropertyRanges[index].ranges, code);
1131     else
1132       return ONIGERR_TYPE_BUG;
1133   }
1134 
1135   return onig_is_in_code_range((UChar* )CodeRanges[ctype], code);
1136 }
1137 
1138 
1139 extern int
onigenc_unicode_ctype_code_range(OnigCtype ctype,const OnigCodePoint * ranges[])1140 onigenc_unicode_ctype_code_range(OnigCtype ctype, const OnigCodePoint* ranges[])
1141 {
1142   if (ctype >= CODE_RANGES_NUM) {
1143     int index = ctype - CODE_RANGES_NUM;
1144     if (index < UserDefinedPropertyNum) {
1145       *ranges = UserDefinedPropertyRanges[index].ranges;
1146       return 0;
1147     }
1148     else
1149       return ONIGERR_TYPE_BUG;
1150   }
1151 
1152   *ranges = CodeRanges[ctype];
1153   return 0;
1154 }
1155 
1156 extern int
onigenc_utf16_32_get_ctype_code_range(OnigCtype ctype,OnigCodePoint * sb_out,const OnigCodePoint * ranges[])1157 onigenc_utf16_32_get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
1158                                       const OnigCodePoint* ranges[])
1159 {
1160   *sb_out = 0x00;
1161   return onigenc_unicode_ctype_code_range(ctype, ranges);
1162 }
1163 
1164 extern int
onigenc_unicode_property_name_to_ctype(OnigEncoding enc,UChar * name,UChar * end)1165 onigenc_unicode_property_name_to_ctype(OnigEncoding enc, UChar* name, UChar* end)
1166 {
1167   int len;
1168   UChar *p;
1169   OnigCodePoint code;
1170   const struct PoolPropertyNameCtype* pc;
1171   char buf[PROPERTY_NAME_MAX_SIZE];
1172 
1173   p = name;
1174   len = 0;
1175   while (p < end) {
1176     code = ONIGENC_MBC_TO_CODE(enc, p, end);
1177     if (code >= 0x80)
1178       return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
1179 
1180     if (code != ' ' && code != '-' && code != '_') {
1181       buf[len++] = (char )code;
1182       if (len >= PROPERTY_NAME_MAX_SIZE)
1183         return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
1184     }
1185 
1186     p += enclen(enc, p);
1187   }
1188 
1189   buf[len] = 0;
1190 
1191   if (UserDefinedPropertyTable != 0) {
1192     UserDefinedPropertyValue* e;
1193     e = (UserDefinedPropertyValue* )NULL;
1194     onig_st_lookup_strend(UserDefinedPropertyTable,
1195                           (const UChar* )buf, (const UChar* )buf + len,
1196                           (hash_data_type* )((void* )(&e)));
1197     if (e != 0) {
1198       return e->ctype;
1199     }
1200   }
1201 
1202   pc = unicode_lookup_property_name(buf, len);
1203   if (pc != 0) {
1204     /* fprintf(stderr, "LOOKUP: %s: %d\n", buf, pc->ctype); */
1205 #ifndef USE_UNICODE_PROPERTIES
1206     if (pc->ctype > ONIGENC_MAX_STD_CTYPE)
1207       return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
1208 #endif
1209 
1210     return (int )pc->ctype;
1211   }
1212 
1213   return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
1214 }
1215