• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
28 
29 #include <stddef.h>
30 #include <stdbool.h>
31 #include <inttypes.h>
32 
33 #include "utf8.h"
34 
35 int
utf32_to_utf8(uint32_t unichar,char * buffer)36 utf32_to_utf8(uint32_t unichar, char *buffer)
37 {
38     int count, shift, length;
39     uint8_t head;
40 
41     if (unichar <= 0x007f) {
42         buffer[0] = unichar;
43         buffer[1] = '\0';
44         return 2;
45     }
46     else if (unichar <= 0x07FF) {
47         length = 2;
48         head = 0xc0;
49     }
50     else if (unichar <= 0xffff) {
51         length = 3;
52         head = 0xe0;
53     }
54     else if (unichar <= 0x10ffff) {
55         length = 4;
56         head = 0xf0;
57     }
58     else {
59         buffer[0] = '\0';
60         return 0;
61     }
62 
63     for (count = length - 1, shift = 0; count > 0; count--, shift += 6)
64         buffer[count] = 0x80 | ((unichar >> shift) & 0x3f);
65 
66     buffer[0] = head | ((unichar >> shift) & 0x3f);
67     buffer[length] = '\0';
68 
69     return length + 1;
70 }
71 
72 bool
is_valid_utf8(const char * ss,size_t len)73 is_valid_utf8(const char *ss, size_t len)
74 {
75     size_t i = 0;
76     size_t tail_bytes = 0;
77     const uint8_t *s = (const uint8_t *) ss;
78 
79     /* This beauty is from:
80      *  The Unicode Standard Version 6.2 - Core Specification, Table 3.7
81      *  https://www.unicode.org/versions/Unicode6.2.0/ch03.pdf#G7404
82      * We can optimize if needed. */
83     while (i < len)
84     {
85         if (s[i] <= 0x7F) {
86             tail_bytes = 0;
87         }
88         else if (s[i] >= 0xC2 && s[i] <= 0xDF) {
89             tail_bytes = 1;
90         }
91         else if (s[i] == 0xE0) {
92             i++;
93             if (i >= len || !(s[i] >= 0xA0 && s[i] <= 0xBF))
94                 return false;
95             tail_bytes = 1;
96         }
97         else if (s[i] >= 0xE1 && s[i] <= 0xEC) {
98             tail_bytes = 2;
99         }
100         else if (s[i] == 0xED) {
101             i++;
102             if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x9F))
103                 return false;
104             tail_bytes = 1;
105         }
106         else if (s[i] >= 0xEE && s[i] <= 0xEF) {
107             tail_bytes = 2;
108         }
109         else if (s[i] == 0xF0) {
110             i++;
111             if (i >= len || !(s[i] >= 0x90 && s[i] <= 0xBF))
112                 return false;
113             tail_bytes = 2;
114         }
115         else if (s[i] >= 0xF1 && s[i] <= 0xF3) {
116             tail_bytes = 3;
117         }
118         else if (s[i] == 0xF4) {
119             i++;
120             if (i >= len || !(s[i] >= 0x80 && s[i] <= 0x8F))
121                 return false;
122             tail_bytes = 2;
123         }
124         else {
125             return false;
126         }
127 
128         i++;
129 
130         while (i < len && tail_bytes > 0 && s[i] >= 0x80 && s[i] <= 0xBF) {
131             i++;
132             tail_bytes--;
133         }
134 
135         if (tail_bytes != 0)
136             return false;
137     }
138 
139     return true;
140 }
141