Debian Codegraph @

libuna/libuna_codepage_mac_inuit.c

raw symbols SCIP
1 /*
2 * MacInuit 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_inuit.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the MacInuit codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_mac_inuit_byte_stream_to_unicode_base_0x80[ 128 ] = {
33 0x1403, 0x1404, 0x1405, 0x1406, 0x140a, 0x140b, 0x1431, 0x1432,
34 0x1433, 0x1434, 0x1438, 0x1439, 0x1449, 0x144e, 0x144f, 0x1450,
35 0x1451, 0x1455, 0x1456, 0x1466, 0x146d, 0x146e, 0x146f, 0x1470,
36 0x1472, 0x1473, 0x1483, 0x148b, 0x148c, 0x148d, 0x148e, 0x1490,
37 0x1491, 0x00b0, 0x14a1, 0x14a5, 0x14a6, 0x2022, 0x00b6, 0x14a7,
38 0x00ae, 0x00a9, 0x2122, 0x14a8, 0x14aa, 0x14ab, 0x14bb, 0x14c2,
39 0x14c3, 0x14c4, 0x14c5, 0x14c7, 0x14c8, 0x14d0, 0x14ef, 0x14f0,
40 0x14f1, 0x14f2, 0x14f4, 0x14f5, 0x1505, 0x14d5, 0x14d6, 0x14d7,
41 0x14d8, 0x14da, 0x14db, 0x14ea, 0x1528, 0x1529, 0x152a, 0x152b,
42 0x152d, 0x2026, 0x00a0, 0x152e, 0x153e, 0x1555, 0x1556, 0x1557,
43 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x1558, 0x1559,
44 0x155a, 0x155d, 0x1546, 0x1547, 0x1548, 0x1549, 0x154b, 0x154c,
45 0x1550, 0x157f, 0x1580, 0x1581, 0x1582, 0x1583, 0x1584, 0x1585,
46 0x158f, 0x1590, 0x1591, 0x1592, 0x1593, 0x1594, 0x1595, 0x1671,
47 0x1672, 0x1673, 0x1674, 0x1675, 0x1676, 0x1596, 0x15a0, 0x15a1,
48 0x15a2, 0x15a3, 0x15a4, 0x15a5, 0x15a6, 0x157c, 0x0141, 0x0142
49 };
50
51 /* Unicode to ASCII character lookup tables for the MacInuit codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1430[ 216 ] = {
55 0x1a, 0x86, 0x87, 0x88, 0x89, 0x1a, 0x1a, 0x1a,
56 0x8a, 0x8b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
57 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
58 0x1a, 0x8c, 0x1a, 0x1a, 0x1a, 0x1a, 0x8d, 0x8e,
59 0x8f, 0x90, 0x1a, 0x1a, 0x1a, 0x91, 0x92, 0x1a,
60 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
61 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x93, 0x1a,
62 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x94, 0x95, 0x96,
63 0x97, 0x1a, 0x98, 0x99, 0x1a, 0x1a, 0x1a, 0x1a,
64 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
65 0x1a, 0x1a, 0x1a, 0x9a, 0x1a, 0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, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xda, 0xdb,
89 0xdc, 0xdd, 0x1a, 0xde, 0xdf, 0x1a, 0x1a, 0x1a,
90 0xe0, 0x1a, 0x1a, 0x1a, 0x1a, 0xcd, 0xce, 0xcf,
91 0xd6, 0xd7, 0xd8, 0x1a, 0x1a, 0xd9, 0x1a, 0x1a
92 };
93
94 const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1578[ 48 ] = {
95 0x1a, 0x1a, 0x1a, 0x1a, 0xfd, 0x1a, 0x1a, 0xe1,
96 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0x1a, 0x1a,
97 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xe8,
98 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xf5, 0x1a,
99 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
100 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0x1a
101 };
102
103 const uint8_t libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x2010[ 24 ] = {
104 0x1a, 0x1a, 0x1a, 0xd0, 0xd1, 0x1a, 0x1a, 0x1a,
105 0xd4, 0xd5, 0x1a, 0x1a, 0xd2, 0xd3, 0x1a, 0x1a,
106 0x1a, 0x1a, 0xa5, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a
107 };
108
109 /* Copies an Unicode character from a MacInuit encoded byte stream
110 * Returns 1 if successful or -1 on error
111 */
112 int libuna_codepage_mac_inuit_copy_from_byte_stream(
113 libuna_unicode_character_t *unicode_character,
114 const uint8_t *byte_stream,
115 size_t byte_stream_size,
116 size_t *byte_stream_index,
117 libcerror_error_t **error )
118 {
119 static char *function = "libuna_codepage_mac_inuit_copy_from_byte_stream";
120 libuna_unicode_character_t safe_unicode_character = 0xfffd;
121 size_t safe_byte_stream_index = 0;
122 uint8_t byte_stream_character = 0;
123
124 if( unicode_character == NULL )
125 {
126 libcerror_error_set(
127 error,
128 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
129 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
130 "%s: invalid Unicode character.",
131 function );
132
133 return( -1 );
134 }
135 if( byte_stream == NULL )
136 {
137 libcerror_error_set(
138 error,
139 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
140 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
141 "%s: invalid byte stream.",
142 function );
143
144 return( -1 );
145 }
146 if( byte_stream_size > (size_t) SSIZE_MAX )
147 {
148 libcerror_error_set(
149 error,
150 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
151 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
152 "%s: invalid byte stream size value exceeds maximum.",
153 function );
154
155 return( -1 );
156 }
157 if( byte_stream_index == NULL )
158 {
159 libcerror_error_set(
160 error,
161 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
162 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
163 "%s: invalid byte stream index.",
164 function );
165
166 return( -1 );
167 }
168 safe_byte_stream_index = *byte_stream_index;
169
170 if( safe_byte_stream_index >= byte_stream_size )
171 {
172 libcerror_error_set(
173 error,
174 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
175 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
176 "%s: byte stream too small.",
177 function );
178
179 return( -1 );
180 }
181 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
182
183 if( byte_stream_character < 0x80 )
184 {
185 safe_unicode_character = byte_stream_character;
186 }
187 else
188 {
189 byte_stream_character -= 0x80;
190
191 safe_unicode_character = libuna_codepage_mac_inuit_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
192 }
193 *unicode_character = safe_unicode_character;
194 *byte_stream_index = safe_byte_stream_index;
195
196 return( 1 );
197 }
198
199 /* Copies an Unicode character to a MacInuit encoded byte stream
200 * Returns 1 if successful or -1 on error
201 */
202 int libuna_codepage_mac_inuit_copy_to_byte_stream(
203 libuna_unicode_character_t unicode_character,
204 uint8_t *byte_stream,
205 size_t byte_stream_size,
206 size_t *byte_stream_index,
207 libcerror_error_t **error )
208 {
209 static char *function = "libuna_codepage_mac_inuit_copy_to_byte_stream";
210 size_t safe_byte_stream_index = 0;
211 uint16_t byte_stream_value = 0x001a;
212
213 if( byte_stream == NULL )
214 {
215 libcerror_error_set(
216 error,
217 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
218 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
219 "%s: invalid byte stream.",
220 function );
221
222 return( -1 );
223 }
224 if( byte_stream_size > (size_t) SSIZE_MAX )
225 {
226 libcerror_error_set(
227 error,
228 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
230 "%s: invalid byte stream size value exceeds maximum.",
231 function );
232
233 return( -1 );
234 }
235 if( byte_stream_index == NULL )
236 {
237 libcerror_error_set(
238 error,
239 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241 "%s: invalid byte stream index.",
242 function );
243
244 return( -1 );
245 }
246 safe_byte_stream_index = *byte_stream_index;
247
248 if( safe_byte_stream_index >= byte_stream_size )
249 {
250 libcerror_error_set(
251 error,
252 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
253 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
254 "%s: byte stream too small.",
255 function );
256
257 return( -1 );
258 }
259 if( ( unicode_character < 0x0080 )
260 || ( unicode_character == 0x00a9 ) )
261 {
262 byte_stream_value = (uint16_t) unicode_character;
263 }
264 else if( ( unicode_character >= 0x1430 )
265 && ( unicode_character < 0x1508 ) )
266 {
267 unicode_character -= 0x1430;
268
269 byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1430[ unicode_character ];
270 }
271 else if( ( unicode_character >= 0x1528 )
272 && ( unicode_character < 0x1560 ) )
273 {
274 unicode_character -= 0x1528;
275
276 byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1528[ unicode_character ];
277 }
278 else if( ( unicode_character >= 0x1578 )
279 && ( unicode_character < 0x15a8 ) )
280 {
281 unicode_character -= 0x1578;
282
283 byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x1578[ unicode_character ];
284 }
285 else if( ( unicode_character >= 0x2010 )
286 && ( unicode_character < 0x2028 ) )
287 {
288 unicode_character -= 0x2010;
289
290 byte_stream_value = libuna_codepage_mac_inuit_unicode_to_byte_stream_base_0x2010[ unicode_character ];
291 }
292 else switch( unicode_character )
293 {
294 case 0x00a0:
295 byte_stream_value = 0xca;
296 break;
297
298 case 0x00ae:
299 byte_stream_value = 0xa8;
300 break;
301
302 case 0x00b0:
303 byte_stream_value = 0xa1;
304 break;
305
306 case 0x00b6:
307 byte_stream_value = 0xa6;
308 break;
309
310 case 0x0141:
311 byte_stream_value = 0xfe;
312 break;
313
314 case 0x0142:
315 byte_stream_value = 0xff;
316 break;
317
318 case 0x1403:
319 byte_stream_value = 0x80;
320 break;
321
322 case 0x1404:
323 byte_stream_value = 0x81;
324 break;
325
326 case 0x1405:
327 byte_stream_value = 0x82;
328 break;
329
330 case 0x1406:
331 byte_stream_value = 0x83;
332 break;
333
334 case 0x140a:
335 byte_stream_value = 0x84;
336 break;
337
338 case 0x140b:
339 byte_stream_value = 0x85;
340 break;
341
342 case 0x1671:
343 byte_stream_value = 0xef;
344 break;
345
346 case 0x1672:
347 byte_stream_value = 0xf0;
348 break;
349
350 case 0x1673:
351 byte_stream_value = 0xf1;
352 break;
353
354 case 0x1674:
355 byte_stream_value = 0xf2;
356 break;
357
358 case 0x1675:
359 byte_stream_value = 0xf3;
360 break;
361
362 case 0x1676:
363 byte_stream_value = 0xf4;
364 break;
365
366 case 0x2122:
367 byte_stream_value = 0xaa;
368 break;
369
370 default:
371 byte_stream_value = 0x1a;
372 break;
373 }
374 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
375
376 *byte_stream_index = safe_byte_stream_index;
377
378 return( 1 );
379 }
380