Debian Codegraph @

libuna/libuna_codepage_mac_farsi.c

raw symbols SCIP
1 /*
2 * MacFarsi 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_farsi.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the MacFarsi codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_mac_farsi_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 0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4, 0x06f5, 0x06f6, 0x06f7,
40 0x06f8, 0x06f9, 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 MacFarsi codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_mac_farsi_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_farsi_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, 0x1a, 0x1a,
68 0x1a, 0x84, 0x1a, 0x1a, 0x1a, 0x1a, 0x85, 0x1a,
69 0x1a, 0x1a, 0x1a, 0x1a, 0x86, 0x1a, 0x1a, 0x1a,
70 0x88, 0x87, 0x89, 0x1a, 0x8a, 0x1a, 0x1a, 0x8d,
71 0x8f, 0x8e, 0x90, 0x91, 0x1a, 0x92, 0x94, 0x95,
72 0x1a, 0x96, 0x1a, 0x97, 0x99, 0x1a, 0x9a, 0x9b,
73 0x1a, 0x9d, 0x9c, 0x9e, 0x9f, 0x1a, 0x1a, 0x1a
74 };
75
76 const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0608[ 80 ] = {
77 0x1a, 0x1a, 0x1a, 0x1a, 0xac, 0x1a, 0x1a, 0x1a,
78 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
79 0x1a, 0x1a, 0x1a, 0xbb, 0x1a, 0x1a, 0x1a, 0xbf,
80 0x1a, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
81 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
82 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
83 0xd8, 0xd9, 0xda, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
84 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
85 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
86 0xf0, 0xf1, 0xf2, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
87 };
88
89 const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0668[ 88 ] = {
90 0x1a, 0x1a, 0xa5, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
91 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
92 0x1a, 0xf4, 0x1a, 0x1a, 0x1a, 0x1a, 0xf3, 0x1a,
93 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf5, 0x1a,
94 0xf9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
95 0x1a, 0xfa, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
96 0xfe, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
97 0x1a, 0x1a, 0x1a, 0x1a, 0xf7, 0x1a, 0x1a, 0x1a,
98 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0xf8,
99 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
100 0x1a, 0x1a, 0x8b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
101 };
102
103 const uint8_t libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x06f0[ 16 ] = {
104 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
105 0xb8, 0xb9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
106 };
107
108 /* Copies an Unicode character from a MacFarsi encoded byte stream
109 * Returns 1 if successful or -1 on error
110 */
111 int libuna_codepage_mac_farsi_copy_from_byte_stream(
112 libuna_unicode_character_t *unicode_character,
113 const uint8_t *byte_stream,
114 size_t byte_stream_size,
115 size_t *byte_stream_index,
116 libcerror_error_t **error )
117 {
118 static char *function = "libuna_codepage_mac_farsi_copy_from_byte_stream";
119 libuna_unicode_character_t safe_unicode_character = 0xfffd;
120 size_t safe_byte_stream_index = 0;
121 uint8_t byte_stream_character = 0;
122
123 if( unicode_character == NULL )
124 {
125 libcerror_error_set(
126 error,
127 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
128 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
129 "%s: invalid Unicode character.",
130 function );
131
132 return( -1 );
133 }
134 if( byte_stream == NULL )
135 {
136 libcerror_error_set(
137 error,
138 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
139 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
140 "%s: invalid byte stream.",
141 function );
142
143 return( -1 );
144 }
145 if( byte_stream_size > (size_t) SSIZE_MAX )
146 {
147 libcerror_error_set(
148 error,
149 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
151 "%s: invalid byte stream size value exceeds maximum.",
152 function );
153
154 return( -1 );
155 }
156 if( byte_stream_index == NULL )
157 {
158 libcerror_error_set(
159 error,
160 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162 "%s: invalid byte stream index.",
163 function );
164
165 return( -1 );
166 }
167 safe_byte_stream_index = *byte_stream_index;
168
169 if( safe_byte_stream_index >= byte_stream_size )
170 {
171 libcerror_error_set(
172 error,
173 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
174 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
175 "%s: byte stream too small.",
176 function );
177
178 return( -1 );
179 }
180 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
181
182 if( byte_stream_character < 0x80 )
183 {
184 safe_unicode_character = byte_stream_character;
185 }
186 else
187 {
188 byte_stream_character -= 0x80;
189
190 safe_unicode_character = libuna_codepage_mac_farsi_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
191 }
192 *unicode_character = safe_unicode_character;
193 *byte_stream_index = safe_byte_stream_index;
194
195 return( 1 );
196 }
197
198 /* Copies an Unicode character to a MacFarsi encoded byte stream
199 * Returns 1 if successful or -1 on error
200 */
201 int libuna_codepage_mac_farsi_copy_to_byte_stream(
202 libuna_unicode_character_t unicode_character,
203 uint8_t *byte_stream,
204 size_t byte_stream_size,
205 size_t *byte_stream_index,
206 libcerror_error_t **error )
207 {
208 static char *function = "libuna_codepage_mac_farsi_copy_to_byte_stream";
209 size_t safe_byte_stream_index = 0;
210 uint16_t byte_stream_value = 0x001a;
211
212 if( byte_stream == NULL )
213 {
214 libcerror_error_set(
215 error,
216 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
217 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
218 "%s: invalid byte stream.",
219 function );
220
221 return( -1 );
222 }
223 if( byte_stream_size > (size_t) SSIZE_MAX )
224 {
225 libcerror_error_set(
226 error,
227 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
228 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
229 "%s: invalid byte stream size value exceeds maximum.",
230 function );
231
232 return( -1 );
233 }
234 if( byte_stream_index == NULL )
235 {
236 libcerror_error_set(
237 error,
238 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
239 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
240 "%s: invalid byte stream index.",
241 function );
242
243 return( -1 );
244 }
245 safe_byte_stream_index = *byte_stream_index;
246
247 if( safe_byte_stream_index >= byte_stream_size )
248 {
249 libcerror_error_set(
250 error,
251 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
252 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
253 "%s: byte stream too small.",
254 function );
255
256 return( -1 );
257 }
258 if( unicode_character < 0x0020 )
259 {
260 byte_stream_value = (uint16_t) unicode_character;
261 }
262 else if( ( unicode_character >= 0x0020 )
263 && ( unicode_character < 0x0040 ) )
264 {
265 unicode_character -= 0x0020;
266
267 byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0020[ unicode_character ];
268 }
269 else if( ( unicode_character >= 0x0040 )
270 && ( unicode_character < 0x005b ) )
271 {
272 byte_stream_value = (uint16_t) unicode_character;
273 }
274 else if( ( unicode_character >= 0x0060 )
275 && ( unicode_character < 0x007b ) )
276 {
277 byte_stream_value = (uint16_t) unicode_character;
278 }
279 else if( ( unicode_character >= 0x007e )
280 && ( unicode_character < 0x0080 ) )
281 {
282 byte_stream_value = (uint16_t) unicode_character;
283 }
284 else if( ( unicode_character >= 0x00a0 )
285 && ( unicode_character < 0x0100 ) )
286 {
287 unicode_character -= 0x00a0;
288
289 byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
290 }
291 else if( ( unicode_character >= 0x0608 )
292 && ( unicode_character < 0x0658 ) )
293 {
294 unicode_character -= 0x0608;
295
296 byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0608[ unicode_character ];
297 }
298 else if( ( unicode_character >= 0x0668 )
299 && ( unicode_character < 0x06c0 ) )
300 {
301 unicode_character -= 0x0668;
302
303 byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x0668[ unicode_character ];
304 }
305 else if( ( unicode_character >= 0x06f0 )
306 && ( unicode_character < 0x0700 ) )
307 {
308 unicode_character -= 0x06f0;
309
310 byte_stream_value = libuna_codepage_mac_farsi_unicode_to_byte_stream_base_0x06f0[ unicode_character ];
311 }
312 else switch( unicode_character )
313 {
314 case 0x005b:
315 byte_stream_value = 0xdb;
316 break;
317
318 case 0x005c:
319 byte_stream_value = 0xdc;
320 break;
321
322 case 0x005d:
323 byte_stream_value = 0xdd;
324 break;
325
326 case 0x005e:
327 byte_stream_value = 0xde;
328 break;
329
330 case 0x005f:
331 byte_stream_value = 0xdf;
332 break;
333
334 case 0x007b:
335 byte_stream_value = 0xfb;
336 break;
337
338 case 0x007c:
339 byte_stream_value = 0xfc;
340 break;
341
342 case 0x007d:
343 byte_stream_value = 0xfd;
344 break;
345
346 case 0x06d2:
347 byte_stream_value = 0xff;
348 break;
349
350 case 0x06d5:
351 byte_stream_value = 0xf6;
352 break;
353
354 case 0x2026:
355 byte_stream_value = 0x93;
356 break;
357
358 case 0x274a:
359 byte_stream_value = 0xc0;
360 break;
361
362 default:
363 byte_stream_value = 0x1a;
364 break;
365 }
366 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
367
368 *byte_stream_index = safe_byte_stream_index;
369
370 return( 1 );
371 }
372