packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cm_utf8.c
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cm_utf8.h"
13
14 /*
15   RFC 3629
16   07-bit: 0xxxxxxx
17   11-bit: 110xxxxx 10xxxxxx
18   16-bit: 1110xxxx 10xxxxxx 10xxxxxx
19   21-bit: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
20
21   Pre-RFC Compatibility
22   26-bit: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
23   31-bit: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
24 */
25
26 /* Number of leading ones before a zero in the byte.  */
27 static unsigned char const cm_utf8_ones[256] = {
28   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
29   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
30   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
31   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
32   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
33   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
34   2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
35   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,7,8
36 };
37
38 /* Mask away control bits from bytes with n leading ones.  */
39 static unsigned char const cm_utf8_mask[7] = {
40   0xEF, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01
41 };
42
43 /* Minimum allowed value when first byte has n leading ones.  */
44 static unsigned int const cm_utf8_min[7] = {
45   0, 0, 1u<<7, 1u<<11, 1u<<16, 1u<<21, 1u<<26 /*, 1u<<31 */
46 };
47
48 /*--------------------------------------------------------------------------*/
49 const char* cm_utf8_decode_character(const char* first, const char* last,
50                                      unsigned int* pc)
51 {
52   /* Count leading ones in the first byte.  */
53   unsigned char c = (unsigned char)*first++;
54   unsigned char const ones = cm_utf8_ones[c];
55   switch(ones)
56     {
57     case 0: *pc = c; return first;    /* One-byte character.  */
58     case 1: case 7: case 8: return 0; /* Invalid leading byte.  */
59     default: break;
60     }
61
62   /* Extract bits from this multi-byte character.  */
63   {
64   unsigned int uc = c & cm_utf8_mask[ones];
65   int left;
66   for(left = ones-1; left && first != last; --left)
67     {
68     c = (unsigned char)*first++;
69     if(cm_utf8_ones[c] != 1)
70       {
71       return 0;
72       }
73     uc = (uc << 6) | (c & cm_utf8_mask[1]);
74     }
75
76   if(left > 0 || uc < cm_utf8_min[ones])
77     {
78     return 0;
79     }
80
81   *pc = uc;
82   return first;
83   }
84 }