summaryrefslogtreecommitdiff
path: root/src/raptor_unicode.c
blob: c50d25383b4a7e89847f999ac3b3d99e3aa63daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * raptor_unicode.c - Raptor Unicode and UTF-8 support
 *
 * Copyright (C) 2002-2010, David Beckett http://www.dajobe.org/
 * Copyright (C) 2002-2004, University of Bristol, UK http://www.bristol.ac.uk/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

/* Raptor includes */
#include "raptor2.h"
#include "raptor_internal.h"


/* Unicode defines only the range U+0000 to U+10FFFF */
const raptor_unichar raptor_unicode_max_codepoint = 0x10FFFF;


/**
 * raptor_unicode_utf8_string_put_char:
 * @c: Unicode character
 * @output: UTF-8 string buffer or NULL
 * @length: length of output buffer
 *
 * Encode a Unicode character to a UTF-8 string
 * 
 * If @output is NULL, then will calculate the length rather than
 * perform the encoding.  This can be used by the called to allocate
 * space and then re-call this function with the new buffer.
 * 
 * Return value: number of bytes encoded to output buffer or <0 on failure
 **/
int
raptor_unicode_utf8_string_put_char(raptor_unichar c,
                                    unsigned char *output, size_t length)
{
  size_t size = 0;
  
  /* check for illegal code positions:
   * [ U+D800 to U+DFFF (UTF-16 surrogates) - now allowed ]
   * U+FFFE and U+FFFF
   */
  if(c == 0xFFFE || c == 0xFFFF)
    return -1;

  if      (c < 0x00000080)
    size = 1;
  else if(c < 0x00000800)
    size = 2;
  else if(c < 0x00010000)
    size = 3;
  else if(c < 0x00200000)
    size = 4;
  else if(c < 0x04000000)
    size = 5;
  else if(c < 0x80000000)
    size = 6;
  else
    return -1;

  /* when no buffer given, return size */
  if(!output)
    return RAPTOR_GOOD_CAST(int, size); /* ok since size is in range 1..6 */

  if(size > length)
    return -1;
  
  switch(size) {
    case 6:
      output[5] = RAPTOR_GOOD_CAST(unsigned char, 0x80 | (unsigned char)(c & 0x3F));
      c= c >> 6;
       /* set bit 2 (bits 7,6,5,4,3,2 less 7,6,5,4,3 set below) on last byte */
      c |= 0x4000000; /* 0x10000 = 0x04 << 24 */
      /* FALLTHROUGH */
    case 5:
      output[4] = RAPTOR_GOOD_CAST(unsigned char, 0x80 | (unsigned char)(c & 0x3F));
      c= c >> 6;
       /* set bit 3 (bits 7,6,5,4,3 less 7,6,5,4 set below) on last byte */
      c |= 0x200000; /* 0x10000 = 0x08 << 18 */
      /* FALLTHROUGH */
    case 4:
      output[3] = RAPTOR_GOOD_CAST(unsigned char, 0x80 | (unsigned char)(c & 0x3F));
      c= c >> 6;
       /* set bit 4 (bits 7,6,5,4 less 7,6,5 set below) on last byte */
      c |= 0x10000; /* 0x10000 = 0x10 << 12 */
      /* FALLTHROUGH */
    case 3:
      output[2] = RAPTOR_GOOD_CAST(unsigned char, 0x80 | (unsigned char)(c & 0x3F));
      c= c >> 6;
      /* set bit 5 (bits 7,6,5 less 7,6 set below) on last byte */
      c |= 0x800; /* 0x800 = 0x20 << 6 */
      /* FALLTHROUGH */
    case 2:
      output[1] = RAPTOR_GOOD_CAST(unsigned char, 0x80 | (unsigned char)(c & 0x3F));
      c= c >> 6;
      /* set bits 7,6 on last byte */
      c |= 0xc0; 
      /* FALLTHROUGH */
    case 1:
      output[0] = (unsigned char)c;
  }

  return RAPTOR_GOOD_CAST(int, size); /* ok since size is in range 1..6 */
}


/**
 * raptor_unicode_utf8_string_get_char:
 * @input: UTF-8 string buffer
 * @length: buffer size
 * @output: Pointer to the Unicode character or NULL
 *
 * Decode a UTF-8 encoded string to get a Unicode character.
 * 
 * If output is NULL, then will calculate the number of bytes that
 * will be used from the input buffer and not perform the conversion.
 * 
 * Return value: bytes used from input buffer or <0 on failure: -1 input buffer too short or length error, -2 overlong UTF-8 sequence, -3 illegal code positions, -4 code out of range U+0000 to U+10FFFF.  In cases -2, -3 and -4 the coded character is stored in the output.
 */
int
raptor_unicode_utf8_string_get_char(const unsigned char *input, size_t length,
                                    raptor_unichar *output)
{
  unsigned char in;
  size_t size;
  raptor_unichar c = 0;
  
  if(length < 1)
    return -1;

  in=*input++;
  if((in & 0x80) == 0) {
    size = 1;
    c= in & 0x7f;
  } else if((in & 0xe0) == 0xc0) {
    size = 2;
    c= in & 0x1f;
  } else if((in & 0xf0) == 0xe0) {
    size = 3;
    c= in & 0x0f;
  } else if((in & 0xf8) == 0xf0) {
    size = 4;
    c = in & 0x07;
  } else if((in & 0xfc) == 0xf8) {
    size = 5;
    c = in & 0x03;
  } else if((in & 0xfe) == 0xfc) {
    size = 6;
    c = in & 0x01;
  } else
    return -1;


  if(!output)
    return RAPTOR_GOOD_CAST(int, size); /* ok since size is in range 1..6 */

  if(length < size)
    return -1;

  switch(size) {
    case 6:
      in=*input++ & 0x3f;
      c= c << 6;
      c |= in;
      /* FALLTHROUGH */
    case 5:
      in=*input++ & 0x3f;
      c= c << 6;
      c |= in;
      /* FALLTHROUGH */
    case 4:
      in=*input++ & 0x3f;
      c= c << 6;
      c |= in;
      /* FALLTHROUGH */
    case 3:
      in=*input++ & 0x3f;
      c= c << 6;
      c |= in;
      /* FALLTHROUGH */
    case 2:
      /* '*input' used here since we never need to use new value of input [CLANG] */
      in = *input & 0x3f;
      c= c << 6;
      c |= in;
      /* FALLTHROUGH */
    default:
      break;
  }
  
  *output=c;

  /* check for overlong UTF-8 sequences */
  switch(size) {
    case 2:
      if(c < 0x00000080)
        return -2;
      break;
    case 3:
      if(c < 0x00000800)
        return -2;
      break;
    case 4:
      if(c < 0x00010000)
        return -2;
      break;

    default: /* 1 */
      break;
  }


  /* check for illegal code positions:
   * [ U+D800 to U+DFFF (UTF-16 surrogates) - now allowed ]
   * U+FFFE and U+FFFF
   */
  if(c == 0xFFFE || c == 0xFFFF)
    return -3;

  if(c > raptor_unicode_max_codepoint)
    return -4;

  return RAPTOR_GOOD_CAST(int, size); /* ok since size is in range 1..6 */
}


static int raptor_unicode_is_letter(long c);
static int raptor_unicode_is_basechar(long c);
static int raptor_unicode_is_ideographic(long c);
static int raptor_unicode_is_combiningchar(long c);
static int raptor_unicode_is_digit(long c);
static int raptor_unicode_is_extender(long c);


/**
 * raptor_unicode_is_xml11_namestartchar:
 * @c: Unicode character to check
 * 
 * Check if Unicode character is legal to start an XML 1.1 Name
 * 
 * See <ulink url="http://www.w3.org/TR/2004/REC-xml11-20040204/#NT-NameStartChar">Namespaces in XML 1.1 REC 2004-02-04 NameStartChar</ulink>
 * updating
 * <ulink url="http://www.w3.org/TR/2004/REC-xml11-20040204/">Extensible Markup Language (XML) 1.1 REC 2004-02-04</ulink> sec 2.3, [4a]
 * excluding the ':'
 *
 * Return value: non-0 if legal
 **/
int
raptor_unicode_is_xml11_namestartchar(raptor_unichar c)
{
  return (((c >= 0x0041)  && (c <= 0x005A)) || /* [A-Z] */
          (c == 0x005F) ||                     /* '_' */
          ((c >= 0x0061)  && (c <= 0x007A)) || /* [a-z] */
          ((c >= 0x00C0)  && (c <= 0x00D6)) ||
          ((c >= 0x00D8)  && (c <= 0x00F6)) ||
          ((c >= 0x00F8)  && (c <= 0x02FF)) ||
          ((c >= 0x0370)  && (c <= 0x037D)) ||
          ((c >= 0x037F)  && (c <= 0x1FFF)) ||
          ((c >= 0x200C)  && (c <= 0x200D)) ||
          ((c >= 0x2070)  && (c <= 0x218F)) ||
          ((c >= 0x2C00)  && (c <= 0x2FEF)) ||
          ((c >= 0x3001)  && (c <= 0xD7FF)) ||
          ((c >= 0xF900)  && (c <= 0xFDCF)) ||
          ((c >= 0xFDF0)  && (c <= 0xFFFD)) ||
          ((c >= 0x10000) && (c <= 0xEFFFF)));
}


/**
 * raptor_unicode_is_xml10_namestartchar:
 * @c: Unicode character to check
 *
 * Check if Unicode character is legal to start an XML 1.0 Name
 * 
 * See <ulink url="http://www.w3.org/TR/1999/REC-xml-names-19990114/#NT-NCName">Namespaces in XML REC 1999-01-14</ulink>
 * updating
 * <ulink url="http://www.w3.org/TR/2004/REC-xml-20040204/">Extensible Markup Language (XML) 1.0 (Third Edition) REC 2004-02-04</ulink>
 * excluding the ':'
 *
 * Return value: non-0 if legal
 **/
int
raptor_unicode_is_xml10_namestartchar(raptor_unichar c)
{
  return (raptor_unicode_is_letter(c) ||
          (c == '_'));
}


/**
 * raptor_unicode_is_namestartchar:
 * @c: Unicode character to check
 *
 * Check if Unicode character is legal to start an XML Name
 * 
 * Return value: non-0 if the character is legal
 **/
int
raptor_unicode_is_namestartchar(raptor_unichar c) {
#ifdef RAPTOR_XML_1_1
   return raptor_unicode_is_xml11_namestartchar(c);
#else
   return raptor_unicode_is_xml10_namestartchar(c);
#endif
}


/**
 * raptor_unicode_is_xml11_namechar:
 * @c: Unicode character
 * 
 * Check if a Unicode codepoint is a legal to continue an XML 1.1 Name
 *
 * See <ulink url="http://www.w3.org/TR/2004/REC-xml11-20040204/">Namespaces in XML 1.1 REC 2004-02-04</ulink>
 * updating
 * <ulink url="http://www.w3.org/TR/2004/REC-xml-20040204/">Extensible Markup Language (XML) 1.0 (Third Edition) REC 2004-02-04</ulink> sec 2.3, [4a]
 * excluding the ':'
 * 
 * Return value: non-0 if legal
 **/
int
raptor_unicode_is_xml11_namechar(raptor_unichar c)
{
  return (raptor_unicode_is_xml11_namestartchar(c) ||
          (c == 0x002D) || /* '-' */
          (c == 0x002E) || /* '.' */
          (c >= 0x0030 && c <= 0x0039) || /* 0-9 */
          (c == 0x00B7) ||
          (c >= 0x0300 && c <=0x036F) ||
          (c >= 0x203F && c <=0x2040));
}


/**
 * raptor_unicode_is_xml10_namechar:
 * @c: Unicode character
 * 
 * Check if a Unicode codepoint is a legal to continue an XML 1.0 Name
 * 
 * See <ulink url="http://www.w3.org/TR/1999/REC-xml-names-19990114/#NT-NCNameChar">Namespaces in XML REC 1999-01-14 NCNameChar</ulink>
 * updating
 * <ulink url="http://www.w3.org/TR/2004/REC-xml-20040204/">Extensible Markup Language (XML) 1.0 (Third Edition) REC 2004-02-04</ulink>
 * excluding the ':'
 *
 * Return value: non-0 if legal
 **/
int
raptor_unicode_is_xml10_namechar(raptor_unichar c)
{
  return (raptor_unicode_is_letter(c) ||
          raptor_unicode_is_digit(c) ||
          (c == 0x002E) || /* '.' */
          (c == 0x002D) || /* '-' */
          (c == 0x005F) || /* '_' */
          raptor_unicode_is_combiningchar(c) ||
          raptor_unicode_is_extender(c));
}
 

/**
 * raptor_unicode_is_namechar:
 * @c: Unicode character to check
 *
 * Check if Unicode character is legal to continue an XML Name .
 * 
 * Return value: non-0 if the character is legal
 **/
int
raptor_unicode_is_namechar(raptor_unichar c) 
{
#ifdef RAPTOR_XML_1_1
   return raptor_unicode_is_xml11_namechar(c);
#else
   return raptor_unicode_is_xml10_namechar(c);
#endif
}


/*
 * All this below was derived by machine-transforming the classes in Appendix B
 * of http://www.w3.org/TR/2000/REC-xml-20001006
 */

static int
raptor_unicode_is_letter(long c)
{
  return(raptor_unicode_is_basechar(c) ||
         raptor_unicode_is_ideographic(c));
}


static int
raptor_unicode_is_basechar(long c)
{
  /* http://www.w3.org/TR/2000/REC-xml-20001006#NT-BaseChar */
  return((c >= 0x0041 && c <= 0x005A ) ||
         (c >= 0x0061 && c <= 0x007A ) ||
         (c >= 0x00C0 && c <= 0x00D6 ) ||
         (c >= 0x00D8 && c <= 0x00F6 ) ||
         (c >= 0x00F8 && c <= 0x00FF ) ||
         (c >= 0x0100 && c <= 0x0131 ) ||
         (c >= 0x0134 && c <= 0x013E ) ||
         (c >= 0x0141 && c <= 0x0148 ) ||
         (c >= 0x014A && c <= 0x017E ) ||
         (c >= 0x0180 && c <= 0x01C3 ) ||
         (c >= 0x01CD && c <= 0x01F0 ) ||
         (c >= 0x01F4 && c <= 0x01F5 ) || 
         (c >= 0x01FA && c <= 0x0217 ) ||
         (c >= 0x0250 && c <= 0x02A8 ) ||
         (c >= 0x02BB && c <= 0x02C1 ) ||
         (c == 0x0386) || 
         (c >= 0x0388 && c <= 0x038A ) ||
         (c == 0x038C) ||
         (c >= 0x038E && c <= 0x03A1 ) ||
         (c >= 0x03A3 && c <= 0x03CE ) ||
         (c >= 0x03D0 && c <= 0x03D6 ) ||
         (c == 0x03DA) ||
         (c == 0x03DC) ||
         (c == 0x03DE) ||
         (c == 0x03E0) ||
         (c >= 0x03E2 && c <= 0x03F3 ) ||
         (c >= 0x0401 && c <= 0x040C ) ||
         (c >= 0x040E && c <= 0x044F ) ||
         (c >= 0x0451 && c <= 0x045C ) ||
         (c >= 0x045E && c <= 0x0481 ) ||
         (c >= 0x0490 && c <= 0x04C4 ) ||
         (c >= 0x04C7 && c <= 0x04C8 ) ||
         (c >= 0x04CB && c <= 0x04CC ) ||
         (c >= 0x04D0 && c <= 0x04EB ) ||
         (c >= 0x04EE && c <= 0x04F5 ) ||
         (c >= 0x04F8 && c <= 0x04F9 ) ||
         (c >= 0x0531 && c <= 0x0556 ) ||
         (c == 0x0559) ||
         (c >= 0x0561 && c <= 0x0586 ) ||
         (c >= 0x05D0 && c <= 0x05EA ) ||
         (c >= 0x05F0 && c <= 0x05F2 ) ||
         (c >= 0x0621 && c <= 0x063A ) ||
         (c >= 0x0641 && c <= 0x064A ) ||
         (c >= 0x0671 && c <= 0x06B7 ) ||
         (c >= 0x06BA && c <= 0x06BE ) ||
         (c >= 0x06C0 && c <= 0x06CE ) ||
         (c >= 0x06D0 && c <= 0x06D3 ) ||
         (c == 0x06D5) ||
         (c >= 0x06E5 && c <= 0x06E6 ) ||
         (c >= 0x0905 && c <= 0x0939 ) ||
         (c == 0x093D) ||
         (c >= 0x0958 && c <= 0x0961 ) ||
         (c >= 0x0985 && c <= 0x098C ) ||
         (c >= 0x098F && c <= 0x0990 ) ||
         (c >= 0x0993 && c <= 0x09A8 ) ||
         (c >= 0x09AA && c <= 0x09B0 ) ||
         (c == 0x09B2) ||
         (c >= 0x09B6 && c <= 0x09B9 ) ||
         (c >= 0x09DC && c <= 0x09DD ) ||
         (c >= 0x09DF && c <= 0x09E1 ) ||
         (c >= 0x09F0 && c <= 0x09F1 ) ||
         (c >= 0x0A05 && c <= 0x0A0A ) ||
         (c >= 0x0A0F && c <= 0x0A10 ) ||
         (c >= 0x0A13 && c <= 0x0A28 ) ||
         (c >= 0x0A2A && c <= 0x0A30 ) ||
         (c >= 0x0A32 && c <= 0x0A33 ) ||
         (c >= 0x0A35 && c <= 0x0A36 ) ||
         (c >= 0x0A38 && c <= 0x0A39 ) ||
         (c >= 0x0A59 && c <= 0x0A5C ) || 
         (c == 0x0A5E) ||
         (c >= 0x0A72 && c <= 0x0A74 ) ||
         (c >= 0x0A85 && c <= 0x0A8B ) ||
         (c == 0x0A8D) ||
	 (c >= 0x0A8F && c <= 0x0A91 ) ||
         (c >= 0x0A93 && c <= 0x0AA8 ) ||
         (c >= 0x0AAA && c <= 0x0AB0 ) ||
         (c >= 0x0AB2 && c <= 0x0AB3 ) ||
	 (c >= 0x0AB5 && c <= 0x0AB9 ) ||
         (c == 0x0ABD) ||
         (c == 0x0AE0) ||
         (c >= 0x0B05 && c <= 0x0B0C ) ||
	 (c >= 0x0B0F && c <= 0x0B10 ) ||
         (c >= 0x0B13 && c <= 0x0B28 ) ||
         (c >= 0x0B2A && c <= 0x0B30 ) ||
         (c >= 0x0B32 && c <= 0x0B33 ) ||
	 (c >= 0x0B36 && c <= 0x0B39 ) ||
         (c == 0x0B3D) ||
         (c >= 0x0B5C && c <= 0x0B5D ) ||
         (c >= 0x0B5F && c <= 0x0B61 ) ||
	 (c >= 0x0B85 && c <= 0x0B8A ) ||
         (c >= 0x0B8E && c <= 0x0B90 ) ||
         (c >= 0x0B92 && c <= 0x0B95 ) ||
         (c >= 0x0B99 && c <= 0x0B9A ) ||
	 (c == 0x0B9C) ||
         (c >= 0x0B9E && c <= 0x0B9F ) ||
         (c >= 0x0BA3 && c <= 0x0BA4 ) ||
         (c >= 0x0BA8 && c <= 0x0BAA ) ||
	 (c >= 0x0BAE && c <= 0x0BB5 ) ||
         (c >= 0x0BB7 && c <= 0x0BB9 ) ||
         (c >= 0x0C05 && c <= 0x0C0C ) ||
         (c >= 0x0C0E && c <= 0x0C10 ) ||
	 (c >= 0x0C12 && c <= 0x0C28 ) ||
         (c >= 0x0C2A && c <= 0x0C33 ) ||
         (c >= 0x0C35 && c <= 0x0C39 ) ||
         (c >= 0x0C60 && c <= 0x0C61 ) ||
	 (c >= 0x0C85 && c <= 0x0C8C ) ||
         (c >= 0x0C8E && c <= 0x0C90 ) ||
         (c >= 0x0C92 && c <= 0x0CA8 ) ||
         (c >= 0x0CAA && c <= 0x0CB3 ) ||
	 (c >= 0x0CB5 && c <= 0x0CB9 ) ||
         (c == 0x0CDE) ||
         (c >= 0x0CE0 && c <= 0x0CE1 ) ||
         (c >= 0x0D05 && c <= 0x0D0C ) ||
	 (c >= 0x0D0E && c <= 0x0D10 ) ||
         (c >= 0x0D12 && c <= 0x0D28 ) ||
         (c >= 0x0D2A && c <= 0x0D39 ) ||
         (c >= 0x0D60 && c <= 0x0D61 ) ||
	 (c >= 0x0E01 && c <= 0x0E2E ) ||
         (c == 0x0E30) ||
         (c >= 0x0E32 && c <= 0x0E33 ) ||
         (c >= 0x0E40 && c <= 0x0E45 ) ||
	 (c >= 0x0E81 && c <= 0x0E82 ) ||
         (c == 0x0E84) ||
         (c >= 0x0E87 && c <= 0x0E88 ) ||
         (c == 0x0E8A) ||
	 (c == 0x0E8D) ||
         (c >= 0x0E94 && c <= 0x0E97 ) ||
         (c >= 0x0E99 && c <= 0x0E9F ) ||
         (c >= 0x0EA1 && c <= 0x0EA3 ) ||
	 (c == 0x0EA5) ||
         (c == 0x0EA7) ||
         (c >= 0x0EAA && c <= 0x0EAB ) ||
         (c >= 0x0EAD && c <= 0x0EAE ) ||
	 (c == 0x0EB0) ||
         (c >= 0x0EB2 && c <= 0x0EB3 ) ||
         (c == 0x0EBD) ||
         (c >= 0x0EC0 && c <= 0x0EC4 ) ||
	 (c >= 0x0F40 && c <= 0x0F47 ) ||
         (c >= 0x0F49 && c <= 0x0F69 ) ||
         (c >= 0x10A0 && c <= 0x10C5 ) ||
         (c >= 0x10D0 && c <= 0x10F6 ) ||
	 (c == 0x1100) ||
         (c >= 0x1102 && c <= 0x1103 ) ||
         (c >= 0x1105 && c <= 0x1107 ) ||
         (c == 0x1109) ||
         (c >= 0x110B && c <= 0x110C ) ||
         (c >= 0x110E && c <= 0x1112 ) ||
         (c == 0x113C) ||
         (c == 0x113E) ||
         (c == 0x1140) ||
         (c == 0x114C) ||
         (c == 0x114E) ||
         (c == 0x1150) ||
         (c >= 0x1154 && c <= 0x1155 ) ||
         (c == 0x1159) ||
         (c >= 0x115F && c <= 0x1161 ) ||
         (c == 0x1163) ||
         (c == 0x1165) ||
         (c == 0x1167) ||
	 (c == 0x1169) ||
         (c >= 0x116D && c <= 0x116E ) ||
         (c >= 0x1172 && c <= 0x1173 ) ||
         (c == 0x1175) ||
	 (c == 0x119E) ||
         (c == 0x11A8) ||
         (c == 0x11AB) ||
         (c >= 0x11AE && c <= 0x11AF ) ||
         (c >= 0x11B7 && c <= 0x11B8 ) ||
	 (c == 0x11BA) ||
         (c >= 0x11BC && c <= 0x11C2 ) ||
         (c == 0x11EB) ||
         (c == 0x11F0) ||
         (c == 0x11F9) ||
	 (c >= 0x1E00 && c <= 0x1E9B ) ||
         (c >= 0x1EA0 && c <= 0x1EF9 ) ||
         (c >= 0x1F00 && c <= 0x1F15 ) ||
         (c >= 0x1F18 && c <= 0x1F1D ) ||
	 (c >= 0x1F20 && c <= 0x1F45 ) ||
         (c >= 0x1F48 && c <= 0x1F4D ) ||
         (c >= 0x1F50 && c <= 0x1F57 ) ||
         (c == 0x1F59) ||
	 (c == 0x1F5B) ||
         (c == 0x1F5D) ||
         (c >= 0x1F5F && c <= 0x1F7D ) ||
         (c >= 0x1F80 && c <= 0x1FB4 ) ||
	 (c >= 0x1FB6 && c <= 0x1FBC ) ||
         (c == 0x1FBE) ||
         (c >= 0x1FC2 && c <= 0x1FC4 ) ||
         (c >= 0x1FC6 && c <= 0x1FCC ) ||
	 (c >= 0x1FD0 && c <= 0x1FD3 ) ||
         (c >= 0x1FD6 && c <= 0x1FDB ) ||
         (c >= 0x1FE0 && c <= 0x1FEC ) ||
         (c >= 0x1FF2 && c <= 0x1FF4 ) ||
	 (c >= 0x1FF6 && c <= 0x1FFC ) ||
         (c == 0x2126) ||
         (c >= 0x212A && c <= 0x212B ) ||
         (c == 0x212E) ||
	 (c >= 0x2180 && c <= 0x2182 ) ||
         (c >= 0x3041 && c <= 0x3094 ) ||
         (c >= 0x30A1 && c <= 0x30FA ) ||
         (c >= 0x3105 && c <= 0x312C ) ||
         (c >= 0xAC00 && c <= 0xD7A3 ) 
         );
}

   
static int
raptor_unicode_is_ideographic(long c)
{
  /* http://www.w3.org/TR/2000/REC-xml-20001006#NT-Ideographic */
  return((c >= 0x4E00 && c <= 0x9FA5 ) ||
         (c == 0x3007) ||
         (c >= 0x3021 && c <= 0x3029 ));
}


static int
raptor_unicode_is_combiningchar(long c)
{
  /* http://www.w3.org/TR/2000/REC-xml-20001006#NT-CombiningChar */
  return((c >= 0x0300 && c <= 0x0345 ) ||
         (c >= 0x0360 && c <= 0x0361 ) ||
         (c >= 0x0483 && c <= 0x0486 ) ||
         (c >= 0x0591 && c <= 0x05A1 ) ||
         (c >= 0x05A3 && c <= 0x05B9 ) ||
         (c >= 0x05BB && c <= 0x05BD ) ||
         (c == 0x05BF) ||
         (c >= 0x05C1 && c <= 0x05C2 ) ||
         (c == 0x05C4) ||
         (c >= 0x064B && c <= 0x0652 ) ||
         (c == 0x0670) ||
         (c >= 0x06D6 && c <= 0x06DC ) ||
	 (c >= 0x06DD && c <= 0x06DF ) ||
         (c >= 0x06E0 && c <= 0x06E4 ) ||
         (c >= 0x06E7 && c <= 0x06E8 ) ||
         (c >= 0x06EA && c <= 0x06ED ) ||
	 (c >= 0x0901 && c <= 0x0903 ) ||
         (c == 0x093C) ||
         (c >= 0x093E && c <= 0x094C ) ||
         (c == 0x094D) ||
	 (c >= 0x0951 && c <= 0x0954 ) ||
         (c >= 0x0962 && c <= 0x0963 ) ||
         (c >= 0x0981 && c <= 0x0983 ) ||
         (c == 0x09BC) ||
	 (c == 0x09BE) ||
         (c == 0x09BF) ||
         (c >= 0x09C0 && c <= 0x09C4 ) ||
         (c >= 0x09C7 && c <= 0x09C8 ) ||
	 (c >= 0x09CB && c <= 0x09CD ) ||
         (c == 0x09D7) ||
         (c >= 0x09E2 && c <= 0x09E3 ) ||
         (c == 0x0A02) ||
	 (c == 0x0A3C) ||
         (c == 0x0A3E) ||
         (c == 0x0A3F) ||
         (c >= 0x0A40 && c <= 0x0A42 ) ||
         (c >= 0x0A47 && c <= 0x0A48 ) ||
	 (c >= 0x0A4B && c <= 0x0A4D ) ||
         (c >= 0x0A70 && c <= 0x0A71 ) ||
         (c >= 0x0A81 && c <= 0x0A83 ) ||
         (c == 0x0ABC) ||
	 (c >= 0x0ABE && c <= 0x0AC5 ) ||
         (c >= 0x0AC7 && c <= 0x0AC9 ) ||
         (c >= 0x0ACB && c <= 0x0ACD ) ||
         (c >= 0x0B01 && c <= 0x0B03 ) ||
	 (c == 0x0B3C) ||
         (c >= 0x0B3E && c <= 0x0B43 ) ||
         (c >= 0x0B47 && c <= 0x0B48 ) ||
         (c >= 0x0B4B && c <= 0x0B4D ) ||
	 (c >= 0x0B56 && c <= 0x0B57 ) ||
         (c >= 0x0B82 && c <= 0x0B83 ) ||
         (c >= 0x0BBE && c <= 0x0BC2 ) ||
         (c >= 0x0BC6 && c <= 0x0BC8 ) ||
	 (c >= 0x0BCA && c <= 0x0BCD ) ||
         (c == 0x0BD7) ||
         (c >= 0x0C01 && c <= 0x0C03 ) ||
         (c >= 0x0C3E && c <= 0x0C44 ) ||
	 (c >= 0x0C46 && c <= 0x0C48 ) ||
         (c >= 0x0C4A && c <= 0x0C4D ) ||
         (c >= 0x0C55 && c <= 0x0C56 ) ||
         (c >= 0x0C82 && c <= 0x0C83 ) ||
	 (c >= 0x0CBE && c <= 0x0CC4 ) ||
         (c >= 0x0CC6 && c <= 0x0CC8 ) ||
         (c >= 0x0CCA && c <= 0x0CCD ) ||
         (c >= 0x0CD5 && c <= 0x0CD6 ) ||
	 (c >= 0x0D02 && c <= 0x0D03 ) ||
         (c >= 0x0D3E && c <= 0x0D43 ) ||
         (c >= 0x0D46 && c <= 0x0D48 ) ||
         (c >= 0x0D4A && c <= 0x0D4D ) ||
	 (c == 0x0D57) ||
         (c == 0x0E31) ||
         (c >= 0x0E34 && c <= 0x0E3A ) ||
         (c >= 0x0E47 && c <= 0x0E4E ) ||
	 (c == 0x0EB1) ||
         (c >= 0x0EB4 && c <= 0x0EB9 ) ||
         (c >= 0x0EBB && c <= 0x0EBC ) ||
         (c >= 0x0EC8 && c <= 0x0ECD ) ||
	 (c >= 0x0F18 && c <= 0x0F19 ) ||
         (c == 0x0F35) ||
         (c == 0x0F37) ||
         (c == 0x0F39) ||
         (c == 0x0F3E) ||
	 (c == 0x0F3F) ||
         (c >= 0x0F71 && c <= 0x0F84 ) ||
         (c >= 0x0F86 && c <= 0x0F8B ) ||
         (c >= 0x0F90 && c <= 0x0F95 ) ||
	 (c == 0x0F97) ||
         (c >= 0x0F99 && c <= 0x0FAD ) ||
         (c >= 0x0FB1 && c <= 0x0FB7 ) ||
         (c == 0x0FB9) ||
	 (c >= 0x20D0 && c <= 0x20DC ) ||
         (c == 0x20E1) ||
         (c >= 0x302A && c <= 0x302F ) ||
         (c == 0x3099) ||
	 (c == 0x309A));
}


static int
raptor_unicode_is_digit(long c)
{
  /* http://www.w3.org/TR/2000/REC-xml-20001006#NT-Digit */
  return((c >= 0x0030 && c <= 0x0039 ) ||
         (c >= 0x0660 && c <= 0x0669 ) ||
         (c >= 0x06F0 && c <= 0x06F9 ) ||
         (c >= 0x0966 && c <= 0x096F ) ||
         (c >= 0x09E6 && c <= 0x09EF ) ||
         (c >= 0x0A66 && c <= 0x0A6F ) ||
         (c >= 0x0AE6 && c <= 0x0AEF ) ||
         (c >= 0x0B66 && c <= 0x0B6F ) ||
         (c >= 0x0BE7 && c <= 0x0BEF ) ||
         (c >= 0x0C66 && c <= 0x0C6F ) ||
         (c >= 0x0CE6 && c <= 0x0CEF ) ||
         (c >= 0x0D66 && c <= 0x0D6F ) ||
         (c >= 0x0E50 && c <= 0x0E59 ) ||
         (c >= 0x0ED0 && c <= 0x0ED9 ) ||
         (c >= 0x0F20 && c <= 0x0F29 ));
}


static int
raptor_unicode_is_extender(long c)
{
  /* http://www.w3.org/TR/2000/REC-xml-20001006#NT-Extender */
  return((c == 0x00B7) ||
         (c == 0x02D0) ||
         (c == 0x02D1) ||
         (c == 0x0387) ||
         (c == 0x0640) ||
         (c == 0x0E46) || 
         (c == 0x0EC6) ||
         (c == 0x3005) ||
         (c >= 0x3031 && c <= 0x3035 ) ||
         (c >= 0x309D && c <= 0x309E ) ||
         (c >= 0x30FC && c <= 0x30FE ));
}


/*
 * raptor_unicode_check_utf8_nfc_string:
 * @input: UTF-8 string
 * @length: length of string
 * @error: pointer to error flag (or NULL)
 *
 * INTERNAL - Check if a Unicode UTF-8 encoded string is in Unicode Normal Form C.
 *
 * Return value: <0 on error, 0 if not NFC, >0 if is NFC
 **/
int
raptor_unicode_check_utf8_nfc_string(const unsigned char *input, size_t length)
{
  unsigned int i;
  int plain = 1;
  int rc;
  
  for(i = 0; i < length; i++)
    if(input[i] > 0x7f) {
      plain = 0;
      break;
    }
    
  if(plain)
    return 1;

#ifdef RAPTOR_NFC_ICU
  rc = raptor_nfc_icu_check(input, length);
#else
  rc = 1;
#endif
  return rc;
}


/**
 * raptor_unicode_check_utf8_string:
 * @string: UTF-8 string
 * @length: length of string
 *
 * Check a string is valid Unicode UTF-8.
 * 
 * Return value: Non 0 if the string is UTF-8
 **/
int
raptor_unicode_check_utf8_string(const unsigned char *string, size_t length)
{
  while(length > 0) {
    raptor_unichar unichar = 0;

    int unichar_len;
    unichar_len = raptor_unicode_utf8_string_get_char(string, length, &unichar);
    if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > length)
      return 0;

    if(unichar > raptor_unicode_max_codepoint)
      return 0;
  
    string += unichar_len;
    length -= unichar_len;
  }
  return 1;
}


/**
 * raptor_unicode_utf8_strlen:
 * @string: buffer
 * @length: buffer length
 *
 * Calculate the number of Unicode characters in the given UTF-8 encoded buffer
 *
 * Return value: number of characters or <0 if sequence is invalid
 */
int
raptor_unicode_utf8_strlen(const unsigned char *string, size_t length)
{
  int unicode_length = 0;
  
  while(length > 0) {
    int unichar_len;
    unichar_len = raptor_unicode_utf8_string_get_char(string, length, NULL);
    if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > length) {
      unicode_length = -1;
      break;
    }
    
    string += unichar_len;
    length -= unichar_len;

    unicode_length++;
  }

  return unicode_length;
}


/**
 * raptor_unicode_utf8_substr:
 * @dest: destination string buffer to write to (or NULL)
 * @dest_length_p: location to store actual destination length (or NULL)
 * @src: source string
 * @src_length: source length in bytes
 * @startingLoc: starting location offset 0 for first Unicode character
 * @length: number of Unicode characters to copy at offset @startingLoc (or < 0)
 *
 * Get a unicode (UTF-8) substring of an existing UTF-8 string
 *
 * If @dest is NULL, returns the number of bytes needed to write and
 * does no work.
 * 
 * Return value: number of bytes used in destination string or 0 on failure
 */
size_t
raptor_unicode_utf8_substr(unsigned char* dest, size_t* dest_length_p,
                           const unsigned char* src, size_t src_length,
                           int startingLoc, int length)
{
  size_t dest_length = 0; /* destination unicode characters count */
  size_t dest_bytes = 0;  /* destination UTF-8 bytes count */
  int dest_offset = 0; /* destination string unicode characters index */
  unsigned char* p = dest;
  
  if(!src)
    return 0;

  while(src_length > 0) {
    int unichar_len;

    unichar_len = raptor_unicode_utf8_string_get_char(src, src_length, NULL);
    if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > src_length)
      break;

    if(dest_offset >= startingLoc) {
      if(p) {
        /* copy 1 Unicode character to dest */
        memcpy(p, src, RAPTOR_GOOD_CAST(size_t, unichar_len));
        p += unichar_len;
      }
      dest_bytes += unichar_len;

      dest_length++;
      if(length >= 0 && dest_length == RAPTOR_GOOD_CAST(size_t, length))
        break;
    }

    src += unichar_len;
    src_length -= unichar_len;

    dest_offset++;
  }

  if(p)
    *p = '\0';

  if(dest_length_p)
    *dest_length_p = dest_length;

  return dest_bytes;
}