/src/libfsfat/libuna/libuna_codepage_mac_romanian.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * MacRomanian 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_romanian.h" |
26 | | #include "libuna_libcerror.h" |
27 | | #include "libuna_types.h" |
28 | | |
29 | | /* Extended ASCII to Unicode character lookup table for the MacRomanian codepage |
30 | | * Unknown are filled with the Unicode replacement character 0xfffd |
31 | | */ |
32 | | const uint16_t libuna_codepage_mac_romanian_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, 0x0102, 0x0218, |
39 | | 0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211, |
40 | | 0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x03a9, 0x0103, 0x0219, |
41 | | 0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab, |
42 | | 0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153, |
43 | | 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca, |
44 | | 0x00ff, 0x0178, 0x2044, 0x20ac, 0x2039, 0x203a, 0x021a, 0x021b, |
45 | | 0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1, |
46 | | 0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4, |
47 | | 0xf8ff, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc, |
48 | | 0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7 |
49 | | }; |
50 | | |
51 | | /* Unicode to ASCII character lookup tables for the MacRomanian codepage |
52 | | * Unknown are filled with the ASCII replacement character 0x1a |
53 | | */ |
54 | | const uint8_t libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x00a0[ 104 ] = { |
55 | | 0xca, 0xc1, 0xa2, 0xa3, 0x1a, 0xb4, 0x1a, 0xa4, |
56 | | 0xac, 0xa9, 0xbb, 0xc7, 0xc2, 0x1a, 0xa8, 0xf8, |
57 | | 0xa1, 0xb1, 0x1a, 0x1a, 0xab, 0xb5, 0xa6, 0xe1, |
58 | | 0xfc, 0x1a, 0xbc, 0xc8, 0x1a, 0x1a, 0x1a, 0xc0, |
59 | | 0xcb, 0xe7, 0xe5, 0xcc, 0x80, 0x81, 0x1a, 0x82, |
60 | | 0xe9, 0x83, 0xe6, 0xe8, 0xed, 0xea, 0xeb, 0xec, |
61 | | 0x1a, 0x84, 0xf1, 0xee, 0xef, 0xcd, 0x85, 0x1a, |
62 | | 0x1a, 0xf4, 0xf2, 0xf3, 0x86, 0x1a, 0x1a, 0xa7, |
63 | | 0x88, 0x87, 0x89, 0x8b, 0x8a, 0x8c, 0x1a, 0x8d, |
64 | | 0x8f, 0x8e, 0x90, 0x91, 0x93, 0x92, 0x94, 0x95, |
65 | | 0x1a, 0x96, 0x98, 0x97, 0x99, 0x9b, 0x9a, 0xd6, |
66 | | 0x1a, 0x9d, 0x9c, 0x9e, 0x9f, 0x1a, 0x1a, 0xd8, |
67 | | 0x1a, 0x1a, 0xae, 0xbe, 0x1a, 0x1a, 0x1a, 0x1a |
68 | | }; |
69 | | |
70 | | const uint8_t libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x2010[ 56 ] = { |
71 | | 0x1a, 0x1a, 0x1a, 0xd0, 0xd1, 0x1a, 0x1a, 0x1a, |
72 | | 0xd4, 0xd5, 0xe2, 0x1a, 0xd2, 0xd3, 0xe3, 0x1a, |
73 | | 0xa0, 0xe0, 0xa5, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a, |
74 | | 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
75 | | 0xe4, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
76 | | 0x1a, 0xdc, 0xdd, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
77 | | 0x1a, 0x1a, 0x1a, 0x1a, 0xda, 0x1a, 0x1a, 0x1a |
78 | | }; |
79 | | |
80 | | /* Copies an Unicode character from a MacRomanian encoded byte stream |
81 | | * Returns 1 if successful or -1 on error |
82 | | */ |
83 | | int libuna_codepage_mac_romanian_copy_from_byte_stream( |
84 | | libuna_unicode_character_t *unicode_character, |
85 | | const uint8_t *byte_stream, |
86 | | size_t byte_stream_size, |
87 | | size_t *byte_stream_index, |
88 | | libcerror_error_t **error ) |
89 | 0 | { |
90 | 0 | static char *function = "libuna_codepage_mac_romanian_copy_from_byte_stream"; |
91 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
92 | 0 | size_t safe_byte_stream_index = 0; |
93 | 0 | uint8_t byte_stream_character = 0; |
94 | |
|
95 | 0 | if( unicode_character == NULL ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
100 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
101 | 0 | "%s: invalid Unicode character.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | return( -1 ); |
105 | 0 | } |
106 | 0 | if( byte_stream == NULL ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
111 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
112 | 0 | "%s: invalid byte stream.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | return( -1 ); |
116 | 0 | } |
117 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
123 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 0 | if( byte_stream_index == NULL ) |
129 | 0 | { |
130 | 0 | libcerror_error_set( |
131 | 0 | error, |
132 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
133 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
134 | 0 | "%s: invalid byte stream index.", |
135 | 0 | function ); |
136 | |
|
137 | 0 | return( -1 ); |
138 | 0 | } |
139 | 0 | safe_byte_stream_index = *byte_stream_index; |
140 | |
|
141 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
142 | 0 | { |
143 | 0 | libcerror_error_set( |
144 | 0 | error, |
145 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
146 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
147 | 0 | "%s: byte stream too small.", |
148 | 0 | function ); |
149 | |
|
150 | 0 | return( -1 ); |
151 | 0 | } |
152 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
153 | |
|
154 | 0 | if( byte_stream_character < 0x80 ) |
155 | 0 | { |
156 | 0 | safe_unicode_character = byte_stream_character; |
157 | 0 | } |
158 | 0 | else |
159 | 0 | { |
160 | 0 | byte_stream_character -= 0x80; |
161 | |
|
162 | 0 | safe_unicode_character = libuna_codepage_mac_romanian_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
163 | 0 | } |
164 | 0 | *unicode_character = safe_unicode_character; |
165 | 0 | *byte_stream_index = safe_byte_stream_index; |
166 | |
|
167 | 0 | return( 1 ); |
168 | 0 | } |
169 | | |
170 | | /* Copies an Unicode character to a MacRomanian encoded byte stream |
171 | | * Returns 1 if successful or -1 on error |
172 | | */ |
173 | | int libuna_codepage_mac_romanian_copy_to_byte_stream( |
174 | | libuna_unicode_character_t unicode_character, |
175 | | uint8_t *byte_stream, |
176 | | size_t byte_stream_size, |
177 | | size_t *byte_stream_index, |
178 | | libcerror_error_t **error ) |
179 | 0 | { |
180 | 0 | static char *function = "libuna_codepage_mac_romanian_copy_to_byte_stream"; |
181 | 0 | size_t safe_byte_stream_index = 0; |
182 | 0 | uint16_t byte_stream_value = 0x001a; |
183 | |
|
184 | 0 | if( byte_stream == NULL ) |
185 | 0 | { |
186 | 0 | libcerror_error_set( |
187 | 0 | error, |
188 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
189 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
190 | 0 | "%s: invalid byte stream.", |
191 | 0 | function ); |
192 | |
|
193 | 0 | return( -1 ); |
194 | 0 | } |
195 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
196 | 0 | { |
197 | 0 | libcerror_error_set( |
198 | 0 | error, |
199 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
200 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
201 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
202 | 0 | function ); |
203 | |
|
204 | 0 | return( -1 ); |
205 | 0 | } |
206 | 0 | if( byte_stream_index == NULL ) |
207 | 0 | { |
208 | 0 | libcerror_error_set( |
209 | 0 | error, |
210 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
211 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
212 | 0 | "%s: invalid byte stream index.", |
213 | 0 | function ); |
214 | |
|
215 | 0 | return( -1 ); |
216 | 0 | } |
217 | 0 | safe_byte_stream_index = *byte_stream_index; |
218 | |
|
219 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
220 | 0 | { |
221 | 0 | libcerror_error_set( |
222 | 0 | error, |
223 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
224 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
225 | 0 | "%s: byte stream too small.", |
226 | 0 | function ); |
227 | |
|
228 | 0 | return( -1 ); |
229 | 0 | } |
230 | 0 | if( unicode_character < 0x0080 ) |
231 | 0 | { |
232 | 0 | byte_stream_value = (uint16_t) unicode_character; |
233 | 0 | } |
234 | 0 | else if( ( unicode_character >= 0x00a0 ) |
235 | 0 | && ( unicode_character < 0x0108 ) ) |
236 | 0 | { |
237 | 0 | unicode_character -= 0x00a0; |
238 | |
|
239 | 0 | byte_stream_value = libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x00a0[ unicode_character ]; |
240 | 0 | } |
241 | 0 | else if( ( unicode_character >= 0x2010 ) |
242 | 0 | && ( unicode_character < 0x2048 ) ) |
243 | 0 | { |
244 | 0 | unicode_character -= 0x2010; |
245 | |
|
246 | 0 | byte_stream_value = libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x2010[ unicode_character ]; |
247 | 0 | } |
248 | 0 | else switch( unicode_character ) |
249 | 0 | { |
250 | 0 | case 0x0131: |
251 | 0 | byte_stream_value = 0xf5; |
252 | 0 | break; |
253 | | |
254 | 0 | case 0x0152: |
255 | 0 | byte_stream_value = 0xce; |
256 | 0 | break; |
257 | | |
258 | 0 | case 0x0153: |
259 | 0 | byte_stream_value = 0xcf; |
260 | 0 | break; |
261 | | |
262 | 0 | case 0x0178: |
263 | 0 | byte_stream_value = 0xd9; |
264 | 0 | break; |
265 | | |
266 | 0 | case 0x0192: |
267 | 0 | byte_stream_value = 0xc4; |
268 | 0 | break; |
269 | | |
270 | 0 | case 0x0218: |
271 | 0 | byte_stream_value = 0xaf; |
272 | 0 | break; |
273 | | |
274 | 0 | case 0x0219: |
275 | 0 | byte_stream_value = 0xbf; |
276 | 0 | break; |
277 | | |
278 | 0 | case 0x021a: |
279 | 0 | byte_stream_value = 0xde; |
280 | 0 | break; |
281 | | |
282 | 0 | case 0x021b: |
283 | 0 | byte_stream_value = 0xdf; |
284 | 0 | break; |
285 | | |
286 | 0 | case 0x02c6: |
287 | 0 | byte_stream_value = 0xf6; |
288 | 0 | break; |
289 | | |
290 | 0 | case 0x02c7: |
291 | 0 | byte_stream_value = 0xff; |
292 | 0 | break; |
293 | | |
294 | 0 | case 0x02d8: |
295 | 0 | byte_stream_value = 0xf9; |
296 | 0 | break; |
297 | | |
298 | 0 | case 0x02d9: |
299 | 0 | byte_stream_value = 0xfa; |
300 | 0 | break; |
301 | | |
302 | 0 | case 0x02da: |
303 | 0 | byte_stream_value = 0xfb; |
304 | 0 | break; |
305 | | |
306 | 0 | case 0x02db: |
307 | 0 | byte_stream_value = 0xfe; |
308 | 0 | break; |
309 | | |
310 | 0 | case 0x02dc: |
311 | 0 | byte_stream_value = 0xf7; |
312 | 0 | break; |
313 | | |
314 | 0 | case 0x02dd: |
315 | 0 | byte_stream_value = 0xfd; |
316 | 0 | break; |
317 | | |
318 | 0 | case 0x03a9: |
319 | 0 | byte_stream_value = 0xbd; |
320 | 0 | break; |
321 | | |
322 | 0 | case 0x03c0: |
323 | 0 | byte_stream_value = 0xb9; |
324 | 0 | break; |
325 | | |
326 | 0 | case 0x20ac: |
327 | 0 | byte_stream_value = 0xdb; |
328 | 0 | break; |
329 | | |
330 | 0 | case 0x2122: |
331 | 0 | byte_stream_value = 0xaa; |
332 | 0 | break; |
333 | | |
334 | 0 | case 0x2202: |
335 | 0 | byte_stream_value = 0xb6; |
336 | 0 | break; |
337 | | |
338 | 0 | case 0x2206: |
339 | 0 | byte_stream_value = 0xc6; |
340 | 0 | break; |
341 | | |
342 | 0 | case 0x220f: |
343 | 0 | byte_stream_value = 0xb8; |
344 | 0 | break; |
345 | | |
346 | 0 | case 0x2211: |
347 | 0 | byte_stream_value = 0xb7; |
348 | 0 | break; |
349 | | |
350 | 0 | case 0x221a: |
351 | 0 | byte_stream_value = 0xc3; |
352 | 0 | break; |
353 | | |
354 | 0 | case 0x221e: |
355 | 0 | byte_stream_value = 0xb0; |
356 | 0 | break; |
357 | | |
358 | 0 | case 0x222b: |
359 | 0 | byte_stream_value = 0xba; |
360 | 0 | break; |
361 | | |
362 | 0 | case 0x2248: |
363 | 0 | byte_stream_value = 0xc5; |
364 | 0 | break; |
365 | | |
366 | 0 | case 0x2260: |
367 | 0 | byte_stream_value = 0xad; |
368 | 0 | break; |
369 | | |
370 | 0 | case 0x2264: |
371 | 0 | byte_stream_value = 0xb2; |
372 | 0 | break; |
373 | | |
374 | 0 | case 0x2265: |
375 | 0 | byte_stream_value = 0xb3; |
376 | 0 | break; |
377 | | |
378 | 0 | case 0x25ca: |
379 | 0 | byte_stream_value = 0xd7; |
380 | 0 | break; |
381 | | |
382 | 0 | case 0xf8ff: |
383 | 0 | byte_stream_value = 0xf0; |
384 | 0 | break; |
385 | | |
386 | 0 | default: |
387 | 0 | byte_stream_value = 0x1a; |
388 | 0 | break; |
389 | 0 | } |
390 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
391 | |
|
392 | 0 | *byte_stream_index = safe_byte_stream_index; |
393 | |
|
394 | 0 | return( 1 ); |
395 | 0 | } |
396 | | |