Add LICENSE and a copyright note to all sources
[jansson.git] / src / utf.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <string.h>
9
10 int utf8_encode(int codepoint, char *buffer, int *size)
11 {
12     if(codepoint < 0)
13         return -1;
14     else if(codepoint < 0x80)
15     {
16         buffer[0] = (char)codepoint;
17         *size = 1;
18     }
19     else if(codepoint < 0x800)
20     {
21         buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
22         buffer[1] = 0x80 + ((codepoint & 0x03F));
23         *size = 2;
24     }
25     else if(codepoint < 0x10000)
26     {
27         buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
28         buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
29         buffer[2] = 0x80 + ((codepoint & 0x003F));
30         *size = 3;
31     }
32     else if(codepoint <= 0x10FFFF)
33     {
34         buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
35         buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
36         buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
37         buffer[3] = 0x80 + ((codepoint & 0x00003F));
38         *size = 4;
39     }
40     else
41         return -1;
42
43     return 0;
44 }
45
46 int utf8_check_first(char byte)
47 {
48     unsigned char u = (unsigned char)byte;
49
50     if(u < 0x80)
51         return 1;
52
53     if(0x80 <= u && u <= 0xBF) {
54         /* second, third or fourth byte of a multi-byte
55            sequence, i.e. a "continuation byte" */
56         return 0;
57     }
58     else if(u == 0xC0 || u == 0xC1) {
59         /* overlong encoding of an ASCII byte */
60         return 0;
61     }
62     else if(0xC2 <= u && u <= 0xDF) {
63         /* 2-byte sequence */
64         return 2;
65     }
66
67     else if(0xE0 <= u && u <= 0xEF) {
68         /* 3-byte sequence */
69         return 3;
70     }
71     else if(0xF0 <= u && u <= 0xF4) {
72         /* 4-byte sequence */
73         return 4;
74     }
75     else { /* u >= 0xF5 */
76         /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
77            UTF-8 */
78         return 0;
79     }
80 }
81
82 int utf8_check_full(const char *buffer, int size)
83 {
84     int i, value = 0;
85     unsigned char u = (unsigned char)buffer[0];
86
87     if(size == 2)
88     {
89         value = u & 0x1F;
90     }
91     else if(size == 3)
92     {
93         value = u & 0xF;
94     }
95     else if(size == 4)
96     {
97         value = u & 0x7;
98     }
99     else
100         return 0;
101
102     for(i = 1; i < size; i++)
103     {
104         u = (unsigned char)buffer[i];
105
106         if(u < 0x80 || u > 0xBF) {
107             /* not a continuation byte */
108             return 0;
109         }
110
111         value = (value << 6) + (u & 0x3F);
112     }
113
114     if(value > 0x10FFFF) {
115         /* not in Unicode range */
116         return 0;
117     }
118
119     else if(0xD800 <= value && value <= 0xDFFF) {
120         /* invalid code point (UTF-16 surrogate halves) */
121         return 0;
122     }
123
124     else if((size == 2 && value < 0x80) ||
125             (size == 3 && value < 0x800) ||
126             (size == 4 && value < 0x10000)) {
127         /* overlong encoding */
128         return 0;
129     }
130
131     return 1;
132 }
133
134 int utf8_check_string(const char *string, int length)
135 {
136     int i;
137
138     if(length == -1)
139         length = strlen(string);
140
141     for(i = 0; i < length; i++)
142     {
143         int count = utf8_check_first(string[i]);
144         if(count == 0)
145             return 0;
146         else if(count > 1)
147         {
148             if(i + count > length)
149                 return 0;
150
151             if(!utf8_check_full(&string[i], count))
152                 return 0;
153
154             i += count - 1;
155         }
156     }
157
158     return 1;
159 }