Debian Codegraph @

libuna/libuna_codepage_mac_romanian.c

raw symbols SCIP
1 /*
2 * MacRomanian codepage functions
3 *
4 * Copyright (C) 2008-2024, Joachim Metz <[email protected]>
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 {
90 static char *function = "libuna_codepage_mac_romanian_copy_from_byte_stream";
91 libuna_unicode_character_t safe_unicode_character = 0xfffd;
92 size_t safe_byte_stream_index = 0;
93 uint8_t byte_stream_character = 0;
94
95 if( unicode_character == NULL )
96 {
97 libcerror_error_set(
98 error,
99 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
100 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
101 "%s: invalid Unicode character.",
102 function );
103
104 return( -1 );
105 }
106 if( byte_stream == NULL )
107 {
108 libcerror_error_set(
109 error,
110 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
111 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
112 "%s: invalid byte stream.",
113 function );
114
115 return( -1 );
116 }
117 if( byte_stream_size > (size_t) SSIZE_MAX )
118 {
119 libcerror_error_set(
120 error,
121 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
123 "%s: invalid byte stream size value exceeds maximum.",
124 function );
125
126 return( -1 );
127 }
128 if( byte_stream_index == NULL )
129 {
130 libcerror_error_set(
131 error,
132 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
133 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
134 "%s: invalid byte stream index.",
135 function );
136
137 return( -1 );
138 }
139 safe_byte_stream_index = *byte_stream_index;
140
141 if( safe_byte_stream_index >= byte_stream_size )
142 {
143 libcerror_error_set(
144 error,
145 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
146 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
147 "%s: byte stream too small.",
148 function );
149
150 return( -1 );
151 }
152 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
153
154 if( byte_stream_character < 0x80 )
155 {
156 safe_unicode_character = byte_stream_character;
157 }
158 else
159 {
160 byte_stream_character -= 0x80;
161
162 safe_unicode_character = libuna_codepage_mac_romanian_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
163 }
164 *unicode_character = safe_unicode_character;
165 *byte_stream_index = safe_byte_stream_index;
166
167 return( 1 );
168 }
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 {
180 static char *function = "libuna_codepage_mac_romanian_copy_to_byte_stream";
181 size_t safe_byte_stream_index = 0;
182 uint16_t byte_stream_value = 0x001a;
183
184 if( byte_stream == NULL )
185 {
186 libcerror_error_set(
187 error,
188 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
189 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
190 "%s: invalid byte stream.",
191 function );
192
193 return( -1 );
194 }
195 if( byte_stream_size > (size_t) SSIZE_MAX )
196 {
197 libcerror_error_set(
198 error,
199 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
200 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
201 "%s: invalid byte stream size value exceeds maximum.",
202 function );
203
204 return( -1 );
205 }
206 if( byte_stream_index == NULL )
207 {
208 libcerror_error_set(
209 error,
210 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
211 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
212 "%s: invalid byte stream index.",
213 function );
214
215 return( -1 );
216 }
217 safe_byte_stream_index = *byte_stream_index;
218
219 if( safe_byte_stream_index >= byte_stream_size )
220 {
221 libcerror_error_set(
222 error,
223 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
224 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
225 "%s: byte stream too small.",
226 function );
227
228 return( -1 );
229 }
230 if( unicode_character < 0x0080 )
231 {
232 byte_stream_value = (uint16_t) unicode_character;
233 }
234 else if( ( unicode_character >= 0x00a0 )
235 && ( unicode_character < 0x0108 ) )
236 {
237 unicode_character -= 0x00a0;
238
239 byte_stream_value = libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
240 }
241 else if( ( unicode_character >= 0x2010 )
242 && ( unicode_character < 0x2048 ) )
243 {
244 unicode_character -= 0x2010;
245
246 byte_stream_value = libuna_codepage_mac_romanian_unicode_to_byte_stream_base_0x2010[ unicode_character ];
247 }
248 else switch( unicode_character )
249 {
250 case 0x0131:
251 byte_stream_value = 0xf5;
252 break;
253
254 case 0x0152:
255 byte_stream_value = 0xce;
256 break;
257
258 case 0x0153:
259 byte_stream_value = 0xcf;
260 break;
261
262 case 0x0178:
263 byte_stream_value = 0xd9;
264 break;
265
266 case 0x0192:
267 byte_stream_value = 0xc4;
268 break;
269
270 case 0x0218:
271 byte_stream_value = 0xaf;
272 break;
273
274 case 0x0219:
275 byte_stream_value = 0xbf;
276 break;
277
278 case 0x021a:
279 byte_stream_value = 0xde;
280 break;
281
282 case 0x021b:
283 byte_stream_value = 0xdf;
284 break;
285
286 case 0x02c6:
287 byte_stream_value = 0xf6;
288 break;
289
290 case 0x02c7:
291 byte_stream_value = 0xff;
292 break;
293
294 case 0x02d8:
295 byte_stream_value = 0xf9;
296 break;
297
298 case 0x02d9:
299 byte_stream_value = 0xfa;
300 break;
301
302 case 0x02da:
303 byte_stream_value = 0xfb;
304 break;
305
306 case 0x02db:
307 byte_stream_value = 0xfe;
308 break;
309
310 case 0x02dc:
311 byte_stream_value = 0xf7;
312 break;
313
314 case 0x02dd:
315 byte_stream_value = 0xfd;
316 break;
317
318 case 0x03a9:
319 byte_stream_value = 0xbd;
320 break;
321
322 case 0x03c0:
323 byte_stream_value = 0xb9;
324 break;
325
326 case 0x20ac:
327 byte_stream_value = 0xdb;
328 break;
329
330 case 0x2122:
331 byte_stream_value = 0xaa;
332 break;
333
334 case 0x2202:
335 byte_stream_value = 0xb6;
336 break;
337
338 case 0x2206:
339 byte_stream_value = 0xc6;
340 break;
341
342 case 0x220f:
343 byte_stream_value = 0xb8;
344 break;
345
346 case 0x2211:
347 byte_stream_value = 0xb7;
348 break;
349
350 case 0x221a:
351 byte_stream_value = 0xc3;
352 break;
353
354 case 0x221e:
355 byte_stream_value = 0xb0;
356 break;
357
358 case 0x222b:
359 byte_stream_value = 0xba;
360 break;
361
362 case 0x2248:
363 byte_stream_value = 0xc5;
364 break;
365
366 case 0x2260:
367 byte_stream_value = 0xad;
368 break;
369
370 case 0x2264:
371 byte_stream_value = 0xb2;
372 break;
373
374 case 0x2265:
375 byte_stream_value = 0xb3;
376 break;
377
378 case 0x25ca:
379 byte_stream_value = 0xd7;
380 break;
381
382 case 0xf8ff:
383 byte_stream_value = 0xf0;
384 break;
385
386 default:
387 byte_stream_value = 0x1a;
388 break;
389 }
390 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
391
392 *byte_stream_index = safe_byte_stream_index;
393
394 return( 1 );
395 }
396