1 |
/* |
2 |
encodings.c - test handling different character encodings |
3 |
Copyright (C) 2008, 2009, 2010 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <config.h> |
21 |
|
22 |
#include <assert.h> |
23 |
#include <string.h> |
24 |
|
25 |
#include "encoding.h" |
26 |
#include "stream.h" |
27 |
|
28 |
int main(void) { |
29 |
jschar * characters; |
30 |
size_t num_characters; |
31 |
int result; |
32 |
|
33 |
/* e, e grave, e acute, e circumflex */ |
34 |
uint8_t utf8[] = { |
35 |
'e', |
36 |
0xc3, |
37 |
0xa8, |
38 |
0xc3, |
39 |
0xa9, |
40 |
0xc3, |
41 |
0xaa, |
42 |
}; |
43 |
|
44 |
result = jscoverage_bytes_to_characters("UTF-8", utf8, 7, &characters, &num_characters); |
45 |
|
46 |
#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR |
47 |
assert(result == 0); |
48 |
assert(num_characters == 4); |
49 |
assert(characters[0] == 'e'); |
50 |
assert(characters[1] == 0xe8); |
51 |
assert(characters[2] == 0xe9); |
52 |
assert(characters[3] == 0xea); |
53 |
|
54 |
free(characters); |
55 |
#else |
56 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
57 |
#endif |
58 |
|
59 |
/* |
60 |
BOM is 0xfeff |
61 |
= 1111 1110 1111 1111 |
62 |
UTF: 1110---- 10------ 10------ |
63 |
= 11101111 10111011 10111111 |
64 |
= EF BB BF |
65 |
*/ |
66 |
uint8_t utf8_with_bom[] = { |
67 |
0xef, |
68 |
0xbb, |
69 |
0xbf, |
70 |
'e', |
71 |
0xc3, |
72 |
0xa8, |
73 |
0xc3, |
74 |
0xa9, |
75 |
0xc3, |
76 |
0xaa, |
77 |
}; |
78 |
|
79 |
result = jscoverage_bytes_to_characters("UTF-8", utf8_with_bom, 10, &characters, &num_characters); |
80 |
|
81 |
#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR |
82 |
assert(result == 0); |
83 |
assert(num_characters == 4); |
84 |
assert(characters[0] == 'e'); |
85 |
assert(characters[1] == 0xe8); |
86 |
assert(characters[2] == 0xe9); |
87 |
assert(characters[3] == 0xea); |
88 |
|
89 |
free(characters); |
90 |
#else |
91 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
92 |
#endif |
93 |
|
94 |
uint8_t utf16be[] = { |
95 |
0, 'e', |
96 |
0, 0xe8, |
97 |
0, 0xe9, |
98 |
0, 0xea, |
99 |
}; |
100 |
|
101 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters); |
102 |
|
103 |
#ifdef HAVE_ICONV |
104 |
assert(result == 0); |
105 |
assert(num_characters == 4); |
106 |
assert(characters[0] == 'e'); |
107 |
assert(characters[1] == 0xe8); |
108 |
assert(characters[2] == 0xe9); |
109 |
assert(characters[3] == 0xea); |
110 |
|
111 |
free(characters); |
112 |
#else |
113 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
114 |
#endif |
115 |
|
116 |
uint8_t utf16be_with_bom[] = { |
117 |
0xfe, 0xff, |
118 |
0, 'e', |
119 |
0, 0xe8, |
120 |
0, 0xe9, |
121 |
0, 0xea, |
122 |
}; |
123 |
|
124 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters); |
125 |
|
126 |
#ifdef HAVE_ICONV |
127 |
assert(result == 0); |
128 |
assert(num_characters == 4); |
129 |
assert(characters[0] == 'e'); |
130 |
assert(characters[1] == 0xe8); |
131 |
assert(characters[2] == 0xe9); |
132 |
assert(characters[3] == 0xea); |
133 |
|
134 |
free(characters); |
135 |
#else |
136 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
137 |
#endif |
138 |
|
139 |
uint8_t utf16le[] = { |
140 |
'e', 0, |
141 |
0xe8, 0, |
142 |
0xe9, 0, |
143 |
0xea, 0, |
144 |
}; |
145 |
|
146 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters); |
147 |
|
148 |
#ifdef HAVE_ICONV |
149 |
assert(result == 0); |
150 |
assert(num_characters == 4); |
151 |
assert(characters[0] == 'e'); |
152 |
assert(characters[1] == 0xe8); |
153 |
assert(characters[2] == 0xe9); |
154 |
assert(characters[3] == 0xea); |
155 |
|
156 |
free(characters); |
157 |
#else |
158 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
159 |
#endif |
160 |
|
161 |
uint8_t utf16le_with_bom[] = { |
162 |
0xff, 0xfe, |
163 |
'e', 0, |
164 |
0xe8, 0, |
165 |
0xe9, 0, |
166 |
0xea, 0, |
167 |
}; |
168 |
|
169 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters); |
170 |
|
171 |
#ifdef HAVE_ICONV |
172 |
assert(result == 0); |
173 |
assert(num_characters == 4); |
174 |
assert(characters[0] == 'e'); |
175 |
assert(characters[1] == 0xe8); |
176 |
assert(characters[2] == 0xe9); |
177 |
assert(characters[3] == 0xea); |
178 |
|
179 |
free(characters); |
180 |
#else |
181 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
182 |
#endif |
183 |
|
184 |
/* bogus encoding */ |
185 |
uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'}; |
186 |
|
187 |
result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters); |
188 |
|
189 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
190 |
|
191 |
#ifdef HAVE_ICONV |
192 |
/* malformed US-ASCII */ |
193 |
/* NOTE: Windows simply discards the high bit */ |
194 |
uint8_t malformed_ascii[] = { |
195 |
'e', |
196 |
0xe8, |
197 |
0xe9, |
198 |
0xea, |
199 |
}; |
200 |
|
201 |
result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters); |
202 |
|
203 |
assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE); |
204 |
#endif |
205 |
|
206 |
/* malformed UTF-8 */ |
207 |
uint8_t malformed_utf8[] = { |
208 |
'e', |
209 |
0xe8, |
210 |
0xe9, |
211 |
0xea, |
212 |
}; |
213 |
|
214 |
result = jscoverage_bytes_to_characters("UTF-8", malformed_utf8, 4, &characters, &num_characters); |
215 |
|
216 |
#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR |
217 |
assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE); |
218 |
#else |
219 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
220 |
#endif |
221 |
|
222 |
return 0; |
223 |
} |