/src/libfwsi/libuna/libuna_codepage_mac_gaelic.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * MacGaelic 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_gaelic.h" |
26 | | #include "libuna_libcerror.h" |
27 | | #include "libuna_types.h" |
28 | | |
29 | | /* Extended ASCII to Unicode character lookup table for the MacGaelic codepage |
30 | | * Unknown are filled with the Unicode replacement character 0xfffd |
31 | | */ |
32 | | const uint16_t libuna_codepage_mac_gaelic_byte_stream_to_unicode_base_0x80[ 128 ] = { |
33 | | 0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1, |
34 | | 0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8, |
35 | | 0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3, |
36 | | 0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc, |
37 | | 0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df, |
38 | | 0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8, |
39 | | 0x1e02, 0x00b1, 0x2264, 0x2265, 0x1e03, 0x010a, 0x010b, 0x1e0a, |
40 | | 0x1e0b, 0x1e1e, 0x1e1f, 0x0120, 0x0121, 0x1e40, 0x00e6, 0x00f8, |
41 | | 0x1e41, 0x1e56, 0x1e57, 0x027c, 0x0192, 0x017f, 0x1e60, 0x00ab, |
42 | | 0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153, |
43 | | 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x1e61, 0x1e9b, |
44 | | 0x00ff, 0x0178, 0x1e6a, 0x20ac, 0x2039, 0x203a, 0x0176, 0x0177, |
45 | | 0x1e6b, 0x00b7, 0x1ef2, 0x1ef3, 0x204a, 0x00c2, 0x00ca, 0x00c1, |
46 | | 0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4, |
47 | | 0x2663, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x00dd, 0x00fd, |
48 | | 0x0174, 0x0175, 0x1e84, 0x1e85, 0x1e80, 0x1e81, 0x1e82, 0x1e83 |
49 | | }; |
50 | | |
51 | | /* Unicode to ASCII character lookup tables for the MacGaelic codepage |
52 | | * Unknown are filled with the ASCII replacement character 0x1a |
53 | | */ |
54 | | const uint8_t libuna_codepage_mac_gaelic_unicode_to_byte_stream_base_0x00a0[ 112 ] = { |
55 | | 0xca, 0x1a, 0xa2, 0xa3, 0x1a, 0x1a, 0x1a, 0xa4, |
56 | | 0xac, 0xa9, 0x1a, 0xc7, 0x1a, 0x1a, 0xa8, 0x1a, |
57 | | 0xa1, 0xb1, 0x1a, 0x1a, 0xab, 0x1a, 0xa6, 0xe1, |
58 | | 0x1a, 0x1a, 0x1a, 0xc8, 0x1a, 0x1a, 0x1a, 0x1a, |
59 | | 0xcb, 0xe7, 0xe5, 0xcc, 0x80, 0x81, 0xae, 0x82, |
60 | | 0xe9, 0x83, 0xe6, 0xe8, 0xed, 0xea, 0xeb, 0xec, |
61 | | 0x1a, 0x84, 0xf1, 0xee, 0xef, 0xcd, 0x85, 0x1a, |
62 | | 0xaf, 0xf4, 0xf2, 0xf3, 0x86, 0xf6, 0x1a, 0xa7, |
63 | | 0x88, 0x87, 0x89, 0x8b, 0x8a, 0x8c, 0xbe, 0x8d, |
64 | | 0x8f, 0x8e, 0x90, 0x91, 0x93, 0x92, 0x94, 0x95, |
65 | | 0x1a, 0x96, 0x98, 0x97, 0x99, 0x9b, 0x9a, 0x1a, |
66 | | 0xbf, 0x9d, 0x9c, 0x9e, 0x9f, 0xf7, 0x1a, 0xd8, |
67 | | 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
68 | | 0x1a, 0x1a, 0xb5, 0xb6, 0x1a, 0x1a, 0x1a, 0x1a |
69 | | }; |
70 | | |
71 | | const uint8_t libuna_codepage_mac_gaelic_unicode_to_byte_stream_base_0x2010[ 24 ] = { |
72 | | 0x1a, 0x1a, 0x1a, 0xd0, 0xd1, 0x1a, 0x1a, 0x1a, |
73 | | 0xd4, 0xd5, 0x1a, 0x1a, 0xd2, 0xd3, 0x1a, 0x1a, |
74 | | 0xa0, 0x1a, 0xa5, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a |
75 | | }; |
76 | | |
77 | | /* Copies an Unicode character from a MacGaelic encoded byte stream |
78 | | * Returns 1 if successful or -1 on error |
79 | | */ |
80 | | int libuna_codepage_mac_gaelic_copy_from_byte_stream( |
81 | | libuna_unicode_character_t *unicode_character, |
82 | | const uint8_t *byte_stream, |
83 | | size_t byte_stream_size, |
84 | | size_t *byte_stream_index, |
85 | | libcerror_error_t **error ) |
86 | 0 | { |
87 | 0 | static char *function = "libuna_codepage_mac_gaelic_copy_from_byte_stream"; |
88 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
89 | 0 | size_t safe_byte_stream_index = 0; |
90 | 0 | uint8_t byte_stream_character = 0; |
91 | |
|
92 | 0 | if( unicode_character == NULL ) |
93 | 0 | { |
94 | 0 | libcerror_error_set( |
95 | 0 | error, |
96 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
97 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
98 | 0 | "%s: invalid Unicode character.", |
99 | 0 | function ); |
100 | |
|
101 | 0 | return( -1 ); |
102 | 0 | } |
103 | 0 | if( byte_stream == NULL ) |
104 | 0 | { |
105 | 0 | libcerror_error_set( |
106 | 0 | error, |
107 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
108 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
109 | 0 | "%s: invalid byte stream.", |
110 | 0 | function ); |
111 | |
|
112 | 0 | return( -1 ); |
113 | 0 | } |
114 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
115 | 0 | { |
116 | 0 | libcerror_error_set( |
117 | 0 | error, |
118 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
119 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
120 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
121 | 0 | function ); |
122 | |
|
123 | 0 | return( -1 ); |
124 | 0 | } |
125 | 0 | if( byte_stream_index == NULL ) |
126 | 0 | { |
127 | 0 | libcerror_error_set( |
128 | 0 | error, |
129 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
130 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
131 | 0 | "%s: invalid byte stream index.", |
132 | 0 | function ); |
133 | |
|
134 | 0 | return( -1 ); |
135 | 0 | } |
136 | 0 | safe_byte_stream_index = *byte_stream_index; |
137 | |
|
138 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
139 | 0 | { |
140 | 0 | libcerror_error_set( |
141 | 0 | error, |
142 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
143 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
144 | 0 | "%s: byte stream too small.", |
145 | 0 | function ); |
146 | |
|
147 | 0 | return( -1 ); |
148 | 0 | } |
149 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
150 | |
|
151 | 0 | if( byte_stream_character < 0x80 ) |
152 | 0 | { |
153 | 0 | safe_unicode_character = byte_stream_character; |
154 | 0 | } |
155 | 0 | else |
156 | 0 | { |
157 | 0 | byte_stream_character -= 0x80; |
158 | |
|
159 | 0 | safe_unicode_character = libuna_codepage_mac_gaelic_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
160 | 0 | } |
161 | 0 | *unicode_character = safe_unicode_character; |
162 | 0 | *byte_stream_index = safe_byte_stream_index; |
163 | |
|
164 | 0 | return( 1 ); |
165 | 0 | } |
166 | | |
167 | | /* Copies an Unicode character to a MacGaelic encoded byte stream |
168 | | * Returns 1 if successful or -1 on error |
169 | | */ |
170 | | int libuna_codepage_mac_gaelic_copy_to_byte_stream( |
171 | | libuna_unicode_character_t unicode_character, |
172 | | uint8_t *byte_stream, |
173 | | size_t byte_stream_size, |
174 | | size_t *byte_stream_index, |
175 | | libcerror_error_t **error ) |
176 | 0 | { |
177 | 0 | static char *function = "libuna_codepage_mac_gaelic_copy_to_byte_stream"; |
178 | 0 | size_t safe_byte_stream_index = 0; |
179 | 0 | uint16_t byte_stream_value = 0x001a; |
180 | |
|
181 | 0 | if( byte_stream == NULL ) |
182 | 0 | { |
183 | 0 | libcerror_error_set( |
184 | 0 | error, |
185 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
186 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
187 | 0 | "%s: invalid byte stream.", |
188 | 0 | function ); |
189 | |
|
190 | 0 | return( -1 ); |
191 | 0 | } |
192 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
193 | 0 | { |
194 | 0 | libcerror_error_set( |
195 | 0 | error, |
196 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
197 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
198 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
199 | 0 | function ); |
200 | |
|
201 | 0 | return( -1 ); |
202 | 0 | } |
203 | 0 | if( byte_stream_index == NULL ) |
204 | 0 | { |
205 | 0 | libcerror_error_set( |
206 | 0 | error, |
207 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
208 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
209 | 0 | "%s: invalid byte stream index.", |
210 | 0 | function ); |
211 | |
|
212 | 0 | return( -1 ); |
213 | 0 | } |
214 | 0 | safe_byte_stream_index = *byte_stream_index; |
215 | |
|
216 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
217 | 0 | { |
218 | 0 | libcerror_error_set( |
219 | 0 | error, |
220 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
221 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
222 | 0 | "%s: byte stream too small.", |
223 | 0 | function ); |
224 | |
|
225 | 0 | return( -1 ); |
226 | 0 | } |
227 | 0 | if( unicode_character < 0x0080 ) |
228 | 0 | { |
229 | 0 | byte_stream_value = (uint16_t) unicode_character; |
230 | 0 | } |
231 | 0 | else if( ( unicode_character >= 0x00a0 ) |
232 | 0 | && ( unicode_character < 0x0110 ) ) |
233 | 0 | { |
234 | 0 | unicode_character -= 0x00a0; |
235 | |
|
236 | 0 | byte_stream_value = libuna_codepage_mac_gaelic_unicode_to_byte_stream_base_0x00a0[ unicode_character ]; |
237 | 0 | } |
238 | 0 | else if( ( unicode_character >= 0x2010 ) |
239 | 0 | && ( unicode_character < 0x2028 ) ) |
240 | 0 | { |
241 | 0 | unicode_character -= 0x2010; |
242 | |
|
243 | 0 | byte_stream_value = libuna_codepage_mac_gaelic_unicode_to_byte_stream_base_0x2010[ unicode_character ]; |
244 | 0 | } |
245 | 0 | else switch( unicode_character ) |
246 | 0 | { |
247 | 0 | case 0x0120: |
248 | 0 | byte_stream_value = 0xbb; |
249 | 0 | break; |
250 | | |
251 | 0 | case 0x0121: |
252 | 0 | byte_stream_value = 0xbc; |
253 | 0 | break; |
254 | | |
255 | 0 | case 0x0131: |
256 | 0 | byte_stream_value = 0xf5; |
257 | 0 | break; |
258 | | |
259 | 0 | case 0x0152: |
260 | 0 | byte_stream_value = 0xce; |
261 | 0 | break; |
262 | | |
263 | 0 | case 0x0153: |
264 | 0 | byte_stream_value = 0xcf; |
265 | 0 | break; |
266 | | |
267 | 0 | case 0x0174: |
268 | 0 | byte_stream_value = 0xf8; |
269 | 0 | break; |
270 | | |
271 | 0 | case 0x0175: |
272 | 0 | byte_stream_value = 0xf9; |
273 | 0 | break; |
274 | | |
275 | 0 | case 0x0176: |
276 | 0 | byte_stream_value = 0xde; |
277 | 0 | break; |
278 | | |
279 | 0 | case 0x0177: |
280 | 0 | byte_stream_value = 0xdf; |
281 | 0 | break; |
282 | | |
283 | 0 | case 0x0178: |
284 | 0 | byte_stream_value = 0xd9; |
285 | 0 | break; |
286 | | |
287 | 0 | case 0x017f: |
288 | 0 | byte_stream_value = 0xc5; |
289 | 0 | break; |
290 | | |
291 | 0 | case 0x0192: |
292 | 0 | byte_stream_value = 0xc4; |
293 | 0 | break; |
294 | | |
295 | 0 | case 0x027c: |
296 | 0 | byte_stream_value = 0xc3; |
297 | 0 | break; |
298 | | |
299 | 0 | case 0x1e02: |
300 | 0 | byte_stream_value = 0xb0; |
301 | 0 | break; |
302 | | |
303 | 0 | case 0x1e03: |
304 | 0 | byte_stream_value = 0xb4; |
305 | 0 | break; |
306 | | |
307 | 0 | case 0x1e0a: |
308 | 0 | byte_stream_value = 0xb7; |
309 | 0 | break; |
310 | | |
311 | 0 | case 0x1e0b: |
312 | 0 | byte_stream_value = 0xb8; |
313 | 0 | break; |
314 | | |
315 | 0 | case 0x1e1e: |
316 | 0 | byte_stream_value = 0xb9; |
317 | 0 | break; |
318 | | |
319 | 0 | case 0x1e1f: |
320 | 0 | byte_stream_value = 0xba; |
321 | 0 | break; |
322 | | |
323 | 0 | case 0x1e40: |
324 | 0 | byte_stream_value = 0xbd; |
325 | 0 | break; |
326 | | |
327 | 0 | case 0x1e41: |
328 | 0 | byte_stream_value = 0xc0; |
329 | 0 | break; |
330 | | |
331 | 0 | case 0x1e56: |
332 | 0 | byte_stream_value = 0xc1; |
333 | 0 | break; |
334 | | |
335 | 0 | case 0x1e57: |
336 | 0 | byte_stream_value = 0xc2; |
337 | 0 | break; |
338 | | |
339 | 0 | case 0x1e60: |
340 | 0 | byte_stream_value = 0xc6; |
341 | 0 | break; |
342 | | |
343 | 0 | case 0x1e61: |
344 | 0 | byte_stream_value = 0xd6; |
345 | 0 | break; |
346 | | |
347 | 0 | case 0x1e6a: |
348 | 0 | byte_stream_value = 0xda; |
349 | 0 | break; |
350 | | |
351 | 0 | case 0x1e6b: |
352 | 0 | byte_stream_value = 0xe0; |
353 | 0 | break; |
354 | | |
355 | 0 | case 0x1e80: |
356 | 0 | byte_stream_value = 0xfc; |
357 | 0 | break; |
358 | | |
359 | 0 | case 0x1e81: |
360 | 0 | byte_stream_value = 0xfd; |
361 | 0 | break; |
362 | | |
363 | 0 | case 0x1e82: |
364 | 0 | byte_stream_value = 0xfe; |
365 | 0 | break; |
366 | | |
367 | 0 | case 0x1e83: |
368 | 0 | byte_stream_value = 0xff; |
369 | 0 | break; |
370 | | |
371 | 0 | case 0x1e84: |
372 | 0 | byte_stream_value = 0xfa; |
373 | 0 | break; |
374 | | |
375 | 0 | case 0x1e85: |
376 | 0 | byte_stream_value = 0xfb; |
377 | 0 | break; |
378 | | |
379 | 0 | case 0x1e9b: |
380 | 0 | byte_stream_value = 0xd7; |
381 | 0 | break; |
382 | | |
383 | 0 | case 0x1ef2: |
384 | 0 | byte_stream_value = 0xe2; |
385 | 0 | break; |
386 | | |
387 | 0 | case 0x1ef3: |
388 | 0 | byte_stream_value = 0xe3; |
389 | 0 | break; |
390 | | |
391 | 0 | case 0x2039: |
392 | 0 | byte_stream_value = 0xdc; |
393 | 0 | break; |
394 | | |
395 | 0 | case 0x203a: |
396 | 0 | byte_stream_value = 0xdd; |
397 | 0 | break; |
398 | | |
399 | 0 | case 0x204a: |
400 | 0 | byte_stream_value = 0xe4; |
401 | 0 | break; |
402 | | |
403 | 0 | case 0x20ac: |
404 | 0 | byte_stream_value = 0xdb; |
405 | 0 | break; |
406 | | |
407 | 0 | case 0x2122: |
408 | 0 | byte_stream_value = 0xaa; |
409 | 0 | break; |
410 | | |
411 | 0 | case 0x2260: |
412 | 0 | byte_stream_value = 0xad; |
413 | 0 | break; |
414 | | |
415 | 0 | case 0x2264: |
416 | 0 | byte_stream_value = 0xb2; |
417 | 0 | break; |
418 | | |
419 | 0 | case 0x2265: |
420 | 0 | byte_stream_value = 0xb3; |
421 | 0 | break; |
422 | | |
423 | 0 | case 0x2663: |
424 | 0 | byte_stream_value = 0xf0; |
425 | 0 | break; |
426 | | |
427 | 0 | default: |
428 | 0 | byte_stream_value = 0x1a; |
429 | 0 | break; |
430 | 0 | } |
431 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
432 | |
|
433 | 0 | *byte_stream_index = safe_byte_stream_index; |
434 | |
|
435 | 0 | return( 1 ); |
436 | 0 | } |
437 | | |