/src/libmsiecf/libuna/libuna_codepage_mac_thai.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * MacThai codepage functions |
3 | | * |
4 | | * Copyright (C) 2008-2024, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <types.h> |
24 | | |
25 | | #include "libuna_codepage_mac_thai.h" |
26 | | #include "libuna_libcerror.h" |
27 | | #include "libuna_types.h" |
28 | | |
29 | | /* Extended ASCII to Unicode character lookup table for the MacThai codepage |
30 | | * Unknown are filled with the Unicode replacement character 0xfffd |
31 | | */ |
32 | | const uint16_t libuna_codepage_mac_thai_byte_stream_to_unicode_base_0x80[ 128 ] = { |
33 | | 0x00ab, 0x00bb, 0x2026, 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, |
34 | | 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x201c, 0x201d, 0x0e4d, |
35 | | 0xfffd, 0x2022, 0x0e31, 0x0e47, 0x0e34, 0x0e35, 0x0e36, 0x0e37, |
36 | | 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x2018, 0x2019, 0xfffd, |
37 | | 0x00a0, 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07, |
38 | | 0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f, |
39 | | 0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17, |
40 | | 0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f, |
41 | | 0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27, |
42 | | 0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f, |
43 | | 0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37, |
44 | | 0x0e38, 0x0e39, 0x0e3a, 0x2060, 0x200b, 0x2013, 0x2014, 0x0e3f, |
45 | | 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47, |
46 | | 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x2122, 0x0e4f, |
47 | | 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57, |
48 | | 0x0e58, 0x0e59, 0x00ae, 0x00a9, 0xfffd, 0xfffd, 0xfffd, 0xfffd |
49 | | }; |
50 | | |
51 | | /* Unicode to ASCII character lookup tables for the MacThai codepage |
52 | | * Unknown are filled with the ASCII replacement character 0x1a |
53 | | */ |
54 | | const uint8_t libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x0e00[ 96 ] = { |
55 | | 0x1a, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
56 | | 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
57 | | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, |
58 | | 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
59 | | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
60 | | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, |
61 | | 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, |
62 | | 0xd8, 0xd9, 0xda, 0x1a, 0x1a, 0x1a, 0x1a, 0xdf, |
63 | | 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
64 | | 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0x1a, 0xef, |
65 | | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
66 | | 0xf8, 0xf9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a |
67 | | }; |
68 | | |
69 | | const uint8_t libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x2008[ 32 ] = { |
70 | | 0x1a, 0x1a, 0x1a, 0xdc, 0x1a, 0x1a, 0x1a, 0x1a, |
71 | | 0x1a, 0x1a, 0x1a, 0xdd, 0xde, 0x1a, 0x1a, 0x1a, |
72 | | 0x9d, 0x9e, 0x1a, 0x1a, 0x8d, 0x8e, 0x1a, 0x1a, |
73 | | 0x1a, 0x1a, 0x91, 0x1a, 0x1a, 0x1a, 0x82, 0x1a |
74 | | }; |
75 | | |
76 | | /* Determines the size of a MacThai encoded byte stream from an Unicode character |
77 | | * Adds the size to the byte stream character size value |
78 | | * Returns 1 if successful, 0 if the byte stream character is valid but not supported since it requires special handling or -1 on error |
79 | | */ |
80 | | int libuna_codepage_mac_thai_unicode_character_size_to_byte_stream( |
81 | | libuna_unicode_character_t unicode_character, |
82 | | size_t *byte_stream_character_size, |
83 | | libcerror_error_t **error ) |
84 | 0 | { |
85 | 0 | static char *function = "libuna_codepage_mac_thai_unicode_character_size_to_byte_stream"; |
86 | 0 | int result = 0; |
87 | |
|
88 | 0 | if( byte_stream_character_size == NULL ) |
89 | 0 | { |
90 | 0 | libcerror_error_set( |
91 | 0 | error, |
92 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
93 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
94 | 0 | "%s: invalid byte stream character size.", |
95 | 0 | function ); |
96 | |
|
97 | 0 | return( -1 ); |
98 | 0 | } |
99 | 0 | switch( unicode_character ) |
100 | 0 | { |
101 | 0 | case 0x00000e31UL: |
102 | 0 | case 0x00000e34UL: |
103 | 0 | case 0x00000e35UL: |
104 | 0 | case 0x00000e36UL: |
105 | 0 | case 0x00000e37UL: |
106 | 0 | case 0x00000e47UL: |
107 | 0 | case 0x00000e48UL: |
108 | 0 | case 0x00000e49UL: |
109 | 0 | case 0x00000e4aUL: |
110 | 0 | case 0x00000e4bUL: |
111 | 0 | case 0x00000e4cUL: |
112 | 0 | case 0x00000e4dUL: |
113 | 0 | result = 0; |
114 | 0 | break; |
115 | | |
116 | 0 | default: |
117 | 0 | *byte_stream_character_size += 1; |
118 | |
|
119 | 0 | result = 1; |
120 | 0 | break; |
121 | 0 | } |
122 | 0 | return( result ); |
123 | 0 | } |
124 | | |
125 | | /* Copies an Unicode character from a MacThai encoded byte stream |
126 | | * Returns 1 if successful or -1 on error |
127 | | */ |
128 | | int libuna_codepage_mac_thai_copy_from_byte_stream( |
129 | | libuna_unicode_character_t *unicode_character, |
130 | | const uint8_t *byte_stream, |
131 | | size_t byte_stream_size, |
132 | | size_t *byte_stream_index, |
133 | | libcerror_error_t **error ) |
134 | 0 | { |
135 | 0 | static char *function = "libuna_codepage_mac_thai_copy_from_byte_stream"; |
136 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
137 | 0 | size_t safe_byte_stream_index = 0; |
138 | 0 | uint8_t byte_stream_character = 0; |
139 | |
|
140 | 0 | if( unicode_character == NULL ) |
141 | 0 | { |
142 | 0 | libcerror_error_set( |
143 | 0 | error, |
144 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
145 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
146 | 0 | "%s: invalid Unicode character.", |
147 | 0 | function ); |
148 | |
|
149 | 0 | return( -1 ); |
150 | 0 | } |
151 | 0 | if( byte_stream == NULL ) |
152 | 0 | { |
153 | 0 | libcerror_error_set( |
154 | 0 | error, |
155 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
156 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
157 | 0 | "%s: invalid byte stream.", |
158 | 0 | function ); |
159 | |
|
160 | 0 | return( -1 ); |
161 | 0 | } |
162 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
163 | 0 | { |
164 | 0 | libcerror_error_set( |
165 | 0 | error, |
166 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
167 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
168 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
169 | 0 | function ); |
170 | |
|
171 | 0 | return( -1 ); |
172 | 0 | } |
173 | 0 | if( byte_stream_index == NULL ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
178 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
179 | 0 | "%s: invalid byte stream index.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | 0 | safe_byte_stream_index = *byte_stream_index; |
185 | |
|
186 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
187 | 0 | { |
188 | 0 | libcerror_error_set( |
189 | 0 | error, |
190 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
191 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
192 | 0 | "%s: byte stream too small.", |
193 | 0 | function ); |
194 | |
|
195 | 0 | return( -1 ); |
196 | 0 | } |
197 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
198 | |
|
199 | 0 | if( byte_stream_character < 0x80 ) |
200 | 0 | { |
201 | 0 | safe_unicode_character = byte_stream_character; |
202 | 0 | } |
203 | 0 | else |
204 | 0 | { |
205 | 0 | byte_stream_character -= 0x80; |
206 | |
|
207 | 0 | safe_unicode_character = libuna_codepage_mac_thai_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
208 | 0 | } |
209 | 0 | *unicode_character = safe_unicode_character; |
210 | 0 | *byte_stream_index = safe_byte_stream_index; |
211 | |
|
212 | 0 | return( 1 ); |
213 | 0 | } |
214 | | |
215 | | /* Copies an Unicode character to a MacThai encoded byte stream |
216 | | * Returns 1 if successful or -1 on error |
217 | | */ |
218 | | int libuna_codepage_mac_thai_copy_to_byte_stream( |
219 | | libuna_unicode_character_t unicode_character, |
220 | | uint8_t *byte_stream, |
221 | | size_t byte_stream_size, |
222 | | size_t *byte_stream_index, |
223 | | libcerror_error_t **error ) |
224 | 0 | { |
225 | 0 | static char *function = "libuna_codepage_mac_thai_copy_to_byte_stream"; |
226 | 0 | size_t safe_byte_stream_index = 0; |
227 | 0 | uint16_t byte_stream_value = 0x001a; |
228 | |
|
229 | 0 | if( byte_stream == NULL ) |
230 | 0 | { |
231 | 0 | libcerror_error_set( |
232 | 0 | error, |
233 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
234 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
235 | 0 | "%s: invalid byte stream.", |
236 | 0 | function ); |
237 | |
|
238 | 0 | return( -1 ); |
239 | 0 | } |
240 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
241 | 0 | { |
242 | 0 | libcerror_error_set( |
243 | 0 | error, |
244 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
245 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
246 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
247 | 0 | function ); |
248 | |
|
249 | 0 | return( -1 ); |
250 | 0 | } |
251 | 0 | if( byte_stream_index == NULL ) |
252 | 0 | { |
253 | 0 | libcerror_error_set( |
254 | 0 | error, |
255 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
256 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
257 | 0 | "%s: invalid byte stream index.", |
258 | 0 | function ); |
259 | |
|
260 | 0 | return( -1 ); |
261 | 0 | } |
262 | 0 | safe_byte_stream_index = *byte_stream_index; |
263 | |
|
264 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
265 | 0 | { |
266 | 0 | libcerror_error_set( |
267 | 0 | error, |
268 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
269 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
270 | 0 | "%s: byte stream too small.", |
271 | 0 | function ); |
272 | |
|
273 | 0 | return( -1 ); |
274 | 0 | } |
275 | 0 | if( ( unicode_character < 0x0080 ) |
276 | 0 | || ( unicode_character == 0x00a0 ) ) |
277 | 0 | { |
278 | 0 | byte_stream_value = (uint16_t) unicode_character; |
279 | 0 | } |
280 | 0 | else if( ( unicode_character >= 0x0e00 ) |
281 | 0 | && ( unicode_character < 0x0e60 ) ) |
282 | 0 | { |
283 | 0 | unicode_character -= 0x0e00; |
284 | |
|
285 | 0 | byte_stream_value = libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x0e00[ unicode_character ]; |
286 | 0 | } |
287 | 0 | else if( ( unicode_character >= 0x2008 ) |
288 | 0 | && ( unicode_character < 0x2028 ) ) |
289 | 0 | { |
290 | 0 | unicode_character -= 0x2008; |
291 | |
|
292 | 0 | byte_stream_value = libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x2008[ unicode_character ]; |
293 | 0 | } |
294 | 0 | else switch( unicode_character ) |
295 | 0 | { |
296 | 0 | case 0x00a9: |
297 | 0 | byte_stream_value = 0xfb; |
298 | 0 | break; |
299 | | |
300 | 0 | case 0x00ab: |
301 | 0 | byte_stream_value = 0x80; |
302 | 0 | break; |
303 | | |
304 | 0 | case 0x00ae: |
305 | 0 | byte_stream_value = 0xfa; |
306 | 0 | break; |
307 | | |
308 | 0 | case 0x00bb: |
309 | 0 | byte_stream_value = 0x81; |
310 | 0 | break; |
311 | | |
312 | 0 | case 0x2060: |
313 | 0 | byte_stream_value = 0xdb; |
314 | 0 | break; |
315 | | |
316 | 0 | case 0x2122: |
317 | 0 | byte_stream_value = 0xee; |
318 | 0 | break; |
319 | | |
320 | 0 | default: |
321 | 0 | byte_stream_value = 0x1a; |
322 | 0 | break; |
323 | 0 | } |
324 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
325 | |
|
326 | 0 | *byte_stream_index = safe_byte_stream_index; |
327 | |
|
328 | 0 | return( 1 ); |
329 | 0 | } |
330 | | |