Debian Codegraph @

libuna/libuna_codepage_mac_arabic.c

raw symbols SCIP
1 /*
2 * MacArabic 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_arabic.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the MacArabic codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_mac_arabic_byte_stream_to_unicode_base_0x80[ 128 ] = {
33 0x00c4, 0x00a0, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1,
34 0x00e0, 0x00e2, 0x00e4, 0x06ba, 0x00ab, 0x00e7, 0x00e9, 0x00e8,
35 0x00ea, 0x00eb, 0x00ed, 0x2026, 0x00ee, 0x00ef, 0x00f1, 0x00f3,
36 0x00bb, 0x00f4, 0x00f6, 0x00f7, 0x00fa, 0x00f9, 0x00fb, 0x00fc,
37 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x066a, 0x0026, 0x0027,
38 0x0028, 0x0029, 0x002a, 0x002b, 0x060c, 0x002d, 0x002e, 0x002f,
39 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667,
40 0x0668, 0x0669, 0x003a, 0x061b, 0x003c, 0x003d, 0x003e, 0x061f,
41 0x274a, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
42 0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f,
43 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637,
44 0x0638, 0x0639, 0x063a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
45 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647,
46 0x0648, 0x0649, 0x064a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f,
47 0x0650, 0x0651, 0x0652, 0x067e, 0x0679, 0x0686, 0x06d5, 0x06a4,
48 0x06af, 0x0688, 0x0691, 0x007b, 0x007c, 0x007d, 0x0698, 0x06d2
49 };
50
51 /* Unicode to ASCII character lookup tables for the MacArabic codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x0020[ 32 ] = {
55 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0x25, 0xa6, 0xa7,
56 0xa8, 0xa9, 0xaa, 0xab, 0x2c, 0xad, 0xae, 0xaf,
57 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
58 0x38, 0x39, 0xba, 0x3b, 0xbc, 0xbd, 0xbe, 0x3f
59 };
60
61 const uint8_t libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x00a0[ 96 ] = {
62 0x81, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
63 0x1a, 0x1a, 0x1a, 0x8c, 0x1a, 0x1a, 0x1a, 0x1a,
64 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
65 0x1a, 0x1a, 0x1a, 0x98, 0x1a, 0x1a, 0x1a, 0x1a,
66 0x1a, 0x1a, 0x1a, 0x1a, 0x80, 0x1a, 0x1a, 0x82,
67 0x1a, 0x83, 0x1a, 0x1a, 0x1a, 0x1a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x1a, 0x1a, 0x1a, 0x1a,
91 0x1a, 0xf4, 0x1a, 0x1a, 0x1a, 0x1a, 0xf3, 0x1a,
92 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf5, 0x1a,
93 0xf9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
94 0x1a, 0xfa, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
95 0xfe, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
96 0x1a, 0x1a, 0x1a, 0x1a, 0xf7, 0x1a, 0x1a, 0x1a,
97 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf8,
98 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
99 0x1a, 0x1a, 0x8b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
100 };
101
102 /* Copies an Unicode character from a MacArabic encoded byte stream
103 * Returns 1 if successful or -1 on error
104 */
105 int libuna_codepage_mac_arabic_copy_from_byte_stream(
106 libuna_unicode_character_t *unicode_character,
107 const uint8_t *byte_stream,
108 size_t byte_stream_size,
109 size_t *byte_stream_index,
110 libcerror_error_t **error )
111 {
112 static char *function = "libuna_codepage_mac_arabic_copy_from_byte_stream";
113 libuna_unicode_character_t safe_unicode_character = 0xfffd;
114 size_t safe_byte_stream_index = 0;
115 uint8_t byte_stream_character = 0;
116
117 if( unicode_character == NULL )
118 {
119 libcerror_error_set(
120 error,
121 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123 "%s: invalid Unicode character.",
124 function );
125
126 return( -1 );
127 }
128 if( byte_stream == NULL )
129 {
130 libcerror_error_set(
131 error,
132 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
133 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
134 "%s: invalid byte stream.",
135 function );
136
137 return( -1 );
138 }
139 if( byte_stream_size > (size_t) SSIZE_MAX )
140 {
141 libcerror_error_set(
142 error,
143 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
144 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
145 "%s: invalid byte stream size value exceeds maximum.",
146 function );
147
148 return( -1 );
149 }
150 if( byte_stream_index == NULL )
151 {
152 libcerror_error_set(
153 error,
154 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
155 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
156 "%s: invalid byte stream index.",
157 function );
158
159 return( -1 );
160 }
161 safe_byte_stream_index = *byte_stream_index;
162
163 if( safe_byte_stream_index >= byte_stream_size )
164 {
165 libcerror_error_set(
166 error,
167 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
168 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
169 "%s: byte stream too small.",
170 function );
171
172 return( -1 );
173 }
174 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
175
176 if( byte_stream_character < 0x80 )
177 {
178 safe_unicode_character = byte_stream_character;
179 }
180 else
181 {
182 byte_stream_character -= 0x80;
183
184 safe_unicode_character = libuna_codepage_mac_arabic_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
185 }
186 *unicode_character = safe_unicode_character;
187 *byte_stream_index = safe_byte_stream_index;
188
189 return( 1 );
190 }
191
192 /* Copies an Unicode character to a MacArabic encoded byte stream
193 * Returns 1 if successful or -1 on error
194 */
195 int libuna_codepage_mac_arabic_copy_to_byte_stream(
196 libuna_unicode_character_t unicode_character,
197 uint8_t *byte_stream,
198 size_t byte_stream_size,
199 size_t *byte_stream_index,
200 libcerror_error_t **error )
201 {
202 static char *function = "libuna_codepage_mac_arabic_copy_to_byte_stream";
203 size_t safe_byte_stream_index = 0;
204 uint16_t byte_stream_value = 0x001a;
205
206 if( byte_stream == NULL )
207 {
208 libcerror_error_set(
209 error,
210 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
211 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
212 "%s: invalid byte stream.",
213 function );
214
215 return( -1 );
216 }
217 if( byte_stream_size > (size_t) SSIZE_MAX )
218 {
219 libcerror_error_set(
220 error,
221 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
222 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
223 "%s: invalid byte stream size value exceeds maximum.",
224 function );
225
226 return( -1 );
227 }
228 if( byte_stream_index == NULL )
229 {
230 libcerror_error_set(
231 error,
232 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
233 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
234 "%s: invalid byte stream index.",
235 function );
236
237 return( -1 );
238 }
239 safe_byte_stream_index = *byte_stream_index;
240
241 if( safe_byte_stream_index >= byte_stream_size )
242 {
243 libcerror_error_set(
244 error,
245 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
246 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
247 "%s: byte stream too small.",
248 function );
249
250 return( -1 );
251 }
252 if( unicode_character < 0x0020 )
253 {
254 byte_stream_value = (uint16_t) unicode_character;
255 }
256 else if( ( unicode_character >= 0x0020 )
257 && ( unicode_character < 0x0040 ) )
258 {
259 unicode_character -= 0x0020;
260
261 byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x0020[ unicode_character ];
262 }
263 else if( ( unicode_character >= 0x0040 )
264 && ( unicode_character < 0x005b ) )
265 {
266 byte_stream_value = (uint16_t) unicode_character;
267 }
268 else if( ( unicode_character >= 0x0060 )
269 && ( unicode_character < 0x007b ) )
270 {
271 byte_stream_value = (uint16_t) unicode_character;
272 }
273 else if( ( unicode_character >= 0x007e )
274 && ( unicode_character < 0x0080 ) )
275 {
276 byte_stream_value = (uint16_t) unicode_character;
277 }
278 else if( ( unicode_character >= 0x00a0 )
279 && ( unicode_character < 0x0100 ) )
280 {
281 unicode_character -= 0x00a0;
282
283 byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
284 }
285 else if( ( unicode_character >= 0x0608 )
286 && ( unicode_character < 0x06c0 ) )
287 {
288 unicode_character -= 0x0608;
289
290 byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x0608[ unicode_character ];
291 }
292 else switch( unicode_character )
293 {
294 case 0x005b:
295 byte_stream_value = 0xdb;
296 break;
297
298 case 0x005c:
299 byte_stream_value = 0xdc;
300 break;
301
302 case 0x005d:
303 byte_stream_value = 0xdd;
304 break;
305
306 case 0x005e:
307 byte_stream_value = 0xde;
308 break;
309
310 case 0x005f:
311 byte_stream_value = 0xdf;
312 break;
313
314 case 0x007b:
315 byte_stream_value = 0xfb;
316 break;
317
318 case 0x007c:
319 byte_stream_value = 0xfc;
320 break;
321
322 case 0x007d:
323 byte_stream_value = 0xfd;
324 break;
325
326 case 0x06d2:
327 byte_stream_value = 0xff;
328 break;
329
330 case 0x06d5:
331 byte_stream_value = 0xf6;
332 break;
333
334 case 0x2026:
335 byte_stream_value = 0x93;
336 break;
337
338 case 0x274a:
339 byte_stream_value = 0xc0;
340 break;
341
342 default:
343 byte_stream_value = 0x1a;
344 break;
345 }
346 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
347
348 *byte_stream_index = safe_byte_stream_index;
349
350 return( 1 );
351 }
352