atom: describe how this odd data structure works
[platform/upstream/libxkbcommon.git] / src / utf8.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2014 Ran Benita <ran234@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Author: Rob Bradford <rob@linux.intel.com>
25  */
26
27 #include <stddef.h>
28 #include <stdbool.h>
29 #include <inttypes.h>
30
31 #include "utf8.h"
32
33 int
34 utf32_to_utf8(uint32_t unichar, char *buffer)
35 {
36     int count, shift, length;
37     uint8_t head;
38
39     if (unichar <= 0x007f) {
40         buffer[0] = unichar;
41         buffer[1] = '\0';
42         return 2;
43     }
44     else if (unichar <= 0x07FF) {
45         length = 2;
46         head = 0xc0;
47     }
48     else if (unichar <= 0xffff) {
49         length = 3;
50         head = 0xe0;
51     }
52     else if (unichar <= 0x10ffff) {
53         length = 4;
54         head = 0xf0;
55     }
56     else {
57         buffer[0] = '\0';
58         return 0;
59     }
60
61     for (count = length - 1, shift = 0; count > 0; count--, shift += 6)
62         buffer[count] = 0x80 | ((unichar >> shift) & 0x3f);
63
64     buffer[0] = head | ((unichar >> shift) & 0x3f);
65     buffer[length] = '\0';
66
67     return length + 1;
68 }
69
70 bool
71 is_valid_utf8(const char *ss, size_t len)
72 {
73     size_t i = 0;
74     size_t tail_bytes = 0;
75     const uint8_t *s = (const uint8_t *) ss;
76
77     /* This beauty is from:
78      *  The Unicode Standard Version 6.2 - Core Specification, Table 3.7
79      *  https://www.unicode.org/versions/Unicode6.2.0/ch03.pdf#G7404
80      * We can optimize if needed. */
81     while (i < len)
82     {
83         if (s[i] <= 0x7F) {
84             tail_bytes = 0;
85         }
86         else if (s[i] >= 0xC2 && s[i] <= 0xDF) {
87             tail_bytes = 1;
88         }
89         else if (s[i] == 0xE0) {
90             i++;
91             if (i >= len || !(s[i] >= 0xA0 && s[i] <= 0xBF))
92                 return false;
93             tail_bytes = 1;
94         }
95         else if (s[i] >= 0xE1 && s[i] <= 0xEC) {
96             tail_bytes = 2;
97         }
98         else if (s[i] == 0xED) {
99             i++;
100             if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x9F))
101                 return false;
102             tail_bytes = 1;
103         }
104         else if (s[i] >= 0xEE && s[i] <= 0xEF) {
105             tail_bytes = 2;
106         }
107         else if (s[i] == 0xF0) {
108             i++;
109             if (i >= len || !(s[i] >= 0x90 && s[i] <= 0xBF))
110                 return false;
111             tail_bytes = 2;
112         }
113         else if (s[i] >= 0xF1 && s[i] <= 0xF3) {
114             tail_bytes = 3;
115         }
116         else if (s[i] == 0xF4) {
117             i++;
118             if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x8F))
119                 return false;
120             tail_bytes = 2;
121         }
122         else {
123             return false;
124         }
125
126         i++;
127
128         while (i < len && tail_bytes > 0 && s[i] >= 0x80 && s[i] <= 0xBF) {
129             i++;
130             tail_bytes--;
131         }
132
133         if (tail_bytes != 0)
134             return false;
135     }
136
137     return true;
138 }