/src/libfsxfs/libuna/libuna_codepage_mac_arabic.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * MacArabic 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_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, 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_arabic_unicode_to_byte_stream_base_0x0608[ 184 ] = { |
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 | | 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
88 | | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, |
89 | | 0xb8, 0xb9, 0xa5, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, |
90 | | 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 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 | 0 | { |
112 | 0 | static char *function = "libuna_codepage_mac_arabic_copy_from_byte_stream"; |
113 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
114 | 0 | size_t safe_byte_stream_index = 0; |
115 | 0 | uint8_t byte_stream_character = 0; |
116 | |
|
117 | 0 | if( unicode_character == NULL ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
123 | 0 | "%s: invalid Unicode character.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 0 | if( byte_stream == 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.", |
135 | 0 | function ); |
136 | |
|
137 | 0 | return( -1 ); |
138 | 0 | } |
139 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
140 | 0 | { |
141 | 0 | libcerror_error_set( |
142 | 0 | error, |
143 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
144 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
145 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
146 | 0 | function ); |
147 | |
|
148 | 0 | return( -1 ); |
149 | 0 | } |
150 | 0 | if( byte_stream_index == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid byte stream index.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 0 | safe_byte_stream_index = *byte_stream_index; |
162 | |
|
163 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
164 | 0 | { |
165 | 0 | libcerror_error_set( |
166 | 0 | error, |
167 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
168 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
169 | 0 | "%s: byte stream too small.", |
170 | 0 | function ); |
171 | |
|
172 | 0 | return( -1 ); |
173 | 0 | } |
174 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
175 | |
|
176 | 0 | if( byte_stream_character < 0x80 ) |
177 | 0 | { |
178 | 0 | safe_unicode_character = byte_stream_character; |
179 | 0 | } |
180 | 0 | else |
181 | 0 | { |
182 | 0 | byte_stream_character -= 0x80; |
183 | |
|
184 | 0 | safe_unicode_character = libuna_codepage_mac_arabic_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
185 | 0 | } |
186 | 0 | *unicode_character = safe_unicode_character; |
187 | 0 | *byte_stream_index = safe_byte_stream_index; |
188 | |
|
189 | 0 | return( 1 ); |
190 | 0 | } |
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 | 0 | { |
202 | 0 | static char *function = "libuna_codepage_mac_arabic_copy_to_byte_stream"; |
203 | 0 | size_t safe_byte_stream_index = 0; |
204 | 0 | uint16_t byte_stream_value = 0x001a; |
205 | |
|
206 | 0 | if( byte_stream == 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.", |
213 | 0 | function ); |
214 | |
|
215 | 0 | return( -1 ); |
216 | 0 | } |
217 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
218 | 0 | { |
219 | 0 | libcerror_error_set( |
220 | 0 | error, |
221 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
222 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
223 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
224 | 0 | function ); |
225 | |
|
226 | 0 | return( -1 ); |
227 | 0 | } |
228 | 0 | if( byte_stream_index == NULL ) |
229 | 0 | { |
230 | 0 | libcerror_error_set( |
231 | 0 | error, |
232 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
233 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
234 | 0 | "%s: invalid byte stream index.", |
235 | 0 | function ); |
236 | |
|
237 | 0 | return( -1 ); |
238 | 0 | } |
239 | 0 | safe_byte_stream_index = *byte_stream_index; |
240 | |
|
241 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
242 | 0 | { |
243 | 0 | libcerror_error_set( |
244 | 0 | error, |
245 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
246 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
247 | 0 | "%s: byte stream too small.", |
248 | 0 | function ); |
249 | |
|
250 | 0 | return( -1 ); |
251 | 0 | } |
252 | 0 | if( unicode_character < 0x0020 ) |
253 | 0 | { |
254 | 0 | byte_stream_value = (uint16_t) unicode_character; |
255 | 0 | } |
256 | 0 | else if( ( unicode_character >= 0x0020 ) |
257 | 0 | && ( unicode_character < 0x0040 ) ) |
258 | 0 | { |
259 | 0 | unicode_character -= 0x0020; |
260 | |
|
261 | 0 | byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x0020[ unicode_character ]; |
262 | 0 | } |
263 | 0 | else if( ( unicode_character >= 0x0040 ) |
264 | 0 | && ( unicode_character < 0x005b ) ) |
265 | 0 | { |
266 | 0 | byte_stream_value = (uint16_t) unicode_character; |
267 | 0 | } |
268 | 0 | else if( ( unicode_character >= 0x0060 ) |
269 | 0 | && ( unicode_character < 0x007b ) ) |
270 | 0 | { |
271 | 0 | byte_stream_value = (uint16_t) unicode_character; |
272 | 0 | } |
273 | 0 | else if( ( unicode_character >= 0x007e ) |
274 | 0 | && ( unicode_character < 0x0080 ) ) |
275 | 0 | { |
276 | 0 | byte_stream_value = (uint16_t) unicode_character; |
277 | 0 | } |
278 | 0 | else if( ( unicode_character >= 0x00a0 ) |
279 | 0 | && ( unicode_character < 0x0100 ) ) |
280 | 0 | { |
281 | 0 | unicode_character -= 0x00a0; |
282 | |
|
283 | 0 | byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x00a0[ unicode_character ]; |
284 | 0 | } |
285 | 0 | else if( ( unicode_character >= 0x0608 ) |
286 | 0 | && ( unicode_character < 0x06c0 ) ) |
287 | 0 | { |
288 | 0 | unicode_character -= 0x0608; |
289 | |
|
290 | 0 | byte_stream_value = libuna_codepage_mac_arabic_unicode_to_byte_stream_base_0x0608[ unicode_character ]; |
291 | 0 | } |
292 | 0 | else switch( unicode_character ) |
293 | 0 | { |
294 | 0 | case 0x005b: |
295 | 0 | byte_stream_value = 0xdb; |
296 | 0 | break; |
297 | | |
298 | 0 | case 0x005c: |
299 | 0 | byte_stream_value = 0xdc; |
300 | 0 | break; |
301 | | |
302 | 0 | case 0x005d: |
303 | 0 | byte_stream_value = 0xdd; |
304 | 0 | break; |
305 | | |
306 | 0 | case 0x005e: |
307 | 0 | byte_stream_value = 0xde; |
308 | 0 | break; |
309 | | |
310 | 0 | case 0x005f: |
311 | 0 | byte_stream_value = 0xdf; |
312 | 0 | break; |
313 | | |
314 | 0 | case 0x007b: |
315 | 0 | byte_stream_value = 0xfb; |
316 | 0 | break; |
317 | | |
318 | 0 | case 0x007c: |
319 | 0 | byte_stream_value = 0xfc; |
320 | 0 | break; |
321 | | |
322 | 0 | case 0x007d: |
323 | 0 | byte_stream_value = 0xfd; |
324 | 0 | break; |
325 | | |
326 | 0 | case 0x06d2: |
327 | 0 | byte_stream_value = 0xff; |
328 | 0 | break; |
329 | | |
330 | 0 | case 0x06d5: |
331 | 0 | byte_stream_value = 0xf6; |
332 | 0 | break; |
333 | | |
334 | 0 | case 0x2026: |
335 | 0 | byte_stream_value = 0x93; |
336 | 0 | break; |
337 | | |
338 | 0 | case 0x274a: |
339 | 0 | byte_stream_value = 0xc0; |
340 | 0 | break; |
341 | | |
342 | 0 | default: |
343 | 0 | byte_stream_value = 0x1a; |
344 | 0 | break; |
345 | 0 | } |
346 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
347 | |
|
348 | 0 | *byte_stream_index = safe_byte_stream_index; |
349 | |
|
350 | 0 | return( 1 ); |
351 | 0 | } |
352 | | |