Debian Codegraph @

libuna/libuna_codepage_mac_greek.c

raw symbols SCIP
1 /*
2 * MacGreek 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_greek.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the MacGreek codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_mac_greek_byte_stream_to_unicode_base_0x80[ 128 ] = {
33 0x00c4, 0x00b9, 0x00b2, 0x00c9, 0x00b3, 0x00d6, 0x00dc, 0x0385,
34 0x00e0, 0x00e2, 0x00e4, 0x0384, 0x00a8, 0x00e7, 0x00e9, 0x00e8,
35 0x00ea, 0x00eb, 0x00a3, 0x2122, 0x00ee, 0x00ef, 0x2022, 0x00bd,
36 0x2030, 0x00f4, 0x00f6, 0x00a6, 0x20ac, 0x00f9, 0x00fb, 0x00fc,
37 0x2020, 0x0393, 0x0394, 0x0398, 0x039b, 0x039e, 0x03a0, 0x00df,
38 0x00ae, 0x00a9, 0x03a3, 0x03aa, 0x00a7, 0x2260, 0x00b0, 0x00b7,
39 0x0391, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x0392, 0x0395, 0x0396,
40 0x0397, 0x0399, 0x039a, 0x039c, 0x03a6, 0x03ab, 0x03a8, 0x03a9,
41 0x03ac, 0x039d, 0x00ac, 0x039f, 0x03a1, 0x2248, 0x03a4, 0x00ab,
42 0x00bb, 0x2026, 0x00a0, 0x03a5, 0x03a7, 0x0386, 0x0388, 0x0153,
43 0x2013, 0x2015, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x0389,
44 0x038a, 0x038c, 0x038e, 0x03ad, 0x03ae, 0x03af, 0x03cc, 0x038f,
45 0x03cd, 0x03b1, 0x03b2, 0x03c8, 0x03b4, 0x03b5, 0x03c6, 0x03b3,
46 0x03b7, 0x03b9, 0x03be, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf,
47 0x03c0, 0x03ce, 0x03c1, 0x03c3, 0x03c4, 0x03b8, 0x03c9, 0x03c2,
48 0x03c7, 0x03c5, 0x03b6, 0x03ca, 0x03cb, 0x0390, 0x03b0, 0x00ad
49 };
50
51 /* Unicode to ASCII character lookup tables for the MacGreek codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x00a0[ 96 ] = {
55 0xca, 0x1a, 0x1a, 0x92, 0x1a, 0xb4, 0x9b, 0xac,
56 0x8c, 0xa9, 0x1a, 0xc7, 0xc2, 0xff, 0xa8, 0x1a,
57 0xae, 0xb1, 0x82, 0x84, 0x1a, 0x1a, 0x1a, 0xaf,
58 0x1a, 0x81, 0x1a, 0xc8, 0x1a, 0x97, 0x1a, 0x1a,
59 0x1a, 0x1a, 0x1a, 0x1a, 0x80, 0x1a, 0x1a, 0x1a,
60 0x1a, 0x83, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
61 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x85, 0x1a,
62 0x1a, 0x1a, 0x1a, 0x1a, 0x86, 0x1a, 0x1a, 0xa7,
63 0x88, 0x1a, 0x89, 0x1a, 0x8a, 0x1a, 0x1a, 0x8d,
64 0x8f, 0x8e, 0x90, 0x91, 0x1a, 0x1a, 0x94, 0x95,
65 0x1a, 0x1a, 0x1a, 0x1a, 0x99, 0x1a, 0x9a, 0xd6,
66 0x1a, 0x9d, 0x1a, 0x9e, 0x9f, 0x1a, 0x1a, 0x1a
67 };
68
69 const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x0380[ 80 ] = {
70 0x1a, 0x1a, 0x1a, 0x1a, 0x8b, 0x87, 0xcd, 0x1a,
71 0xce, 0xd7, 0xd8, 0x1a, 0xd9, 0x1a, 0xda, 0xdf,
72 0xfd, 0xb0, 0xb5, 0xa1, 0xa2, 0xb6, 0xb7, 0xb8,
73 0xa3, 0xb9, 0xba, 0xa4, 0xbb, 0xc1, 0xa5, 0xc3,
74 0xa6, 0xc4, 0x1a, 0xaa, 0xc6, 0xcb, 0xbc, 0xcc,
75 0xbe, 0xbf, 0xab, 0xbd, 0xc0, 0xdb, 0xdc, 0xdd,
76 0xfe, 0xe1, 0xe2, 0xe7, 0xe4, 0xe5, 0xfa, 0xe8,
77 0xf5, 0xe9, 0xeb, 0xec, 0xed, 0xee, 0xea, 0xef,
78 0xf0, 0xf2, 0xf7, 0xf3, 0xf4, 0xf9, 0xe6, 0xf8,
79 0xe3, 0xf6, 0xfb, 0xfc, 0xde, 0xe0, 0xf1, 0x1a
80 };
81
82 const uint8_t libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x2010[ 40 ] = {
83 0x1a, 0x1a, 0x1a, 0xd0, 0x1a, 0xd1, 0x1a, 0x1a,
84 0xd4, 0xd5, 0x1a, 0x1a, 0xd2, 0xd3, 0x1a, 0x1a,
85 0xa0, 0x1a, 0x96, 0x1a, 0x1a, 0x1a, 0xc9, 0x1a,
86 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
87 0x98, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
88 };
89
90 /* Copies an Unicode character from a MacGreek encoded byte stream
91 * Returns 1 if successful or -1 on error
92 */
93 int libuna_codepage_mac_greek_copy_from_byte_stream(
94 libuna_unicode_character_t *unicode_character,
95 const uint8_t *byte_stream,
96 size_t byte_stream_size,
97 size_t *byte_stream_index,
98 libcerror_error_t **error )
99 {
100 static char *function = "libuna_codepage_mac_greek_copy_from_byte_stream";
101 libuna_unicode_character_t safe_unicode_character = 0xfffd;
102 size_t safe_byte_stream_index = 0;
103 uint8_t byte_stream_character = 0;
104
105 if( unicode_character == NULL )
106 {
107 libcerror_error_set(
108 error,
109 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
110 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
111 "%s: invalid Unicode character.",
112 function );
113
114 return( -1 );
115 }
116 if( byte_stream == NULL )
117 {
118 libcerror_error_set(
119 error,
120 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122 "%s: invalid byte stream.",
123 function );
124
125 return( -1 );
126 }
127 if( byte_stream_size > (size_t) SSIZE_MAX )
128 {
129 libcerror_error_set(
130 error,
131 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
132 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
133 "%s: invalid byte stream size value exceeds maximum.",
134 function );
135
136 return( -1 );
137 }
138 if( byte_stream_index == NULL )
139 {
140 libcerror_error_set(
141 error,
142 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
143 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
144 "%s: invalid byte stream index.",
145 function );
146
147 return( -1 );
148 }
149 safe_byte_stream_index = *byte_stream_index;
150
151 if( safe_byte_stream_index >= byte_stream_size )
152 {
153 libcerror_error_set(
154 error,
155 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
157 "%s: byte stream too small.",
158 function );
159
160 return( -1 );
161 }
162 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
163
164 if( byte_stream_character < 0x80 )
165 {
166 safe_unicode_character = byte_stream_character;
167 }
168 else
169 {
170 byte_stream_character -= 0x80;
171
172 safe_unicode_character = libuna_codepage_mac_greek_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
173 }
174 *unicode_character = safe_unicode_character;
175 *byte_stream_index = safe_byte_stream_index;
176
177 return( 1 );
178 }
179
180 /* Copies an Unicode character to a MacGreek encoded byte stream
181 * Returns 1 if successful or -1 on error
182 */
183 int libuna_codepage_mac_greek_copy_to_byte_stream(
184 libuna_unicode_character_t unicode_character,
185 uint8_t *byte_stream,
186 size_t byte_stream_size,
187 size_t *byte_stream_index,
188 libcerror_error_t **error )
189 {
190 static char *function = "libuna_codepage_mac_greek_copy_to_byte_stream";
191 size_t safe_byte_stream_index = 0;
192 uint16_t byte_stream_value = 0x001a;
193
194 if( byte_stream == NULL )
195 {
196 libcerror_error_set(
197 error,
198 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
199 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
200 "%s: invalid byte stream.",
201 function );
202
203 return( -1 );
204 }
205 if( byte_stream_size > (size_t) SSIZE_MAX )
206 {
207 libcerror_error_set(
208 error,
209 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
210 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
211 "%s: invalid byte stream size value exceeds maximum.",
212 function );
213
214 return( -1 );
215 }
216 if( byte_stream_index == NULL )
217 {
218 libcerror_error_set(
219 error,
220 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
221 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
222 "%s: invalid byte stream index.",
223 function );
224
225 return( -1 );
226 }
227 safe_byte_stream_index = *byte_stream_index;
228
229 if( safe_byte_stream_index >= byte_stream_size )
230 {
231 libcerror_error_set(
232 error,
233 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
235 "%s: byte stream too small.",
236 function );
237
238 return( -1 );
239 }
240 if( unicode_character < 0x0080 )
241 {
242 byte_stream_value = (uint16_t) unicode_character;
243 }
244 else if( ( unicode_character >= 0x00a0 )
245 && ( unicode_character < 0x0100 ) )
246 {
247 unicode_character -= 0x00a0;
248
249 byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
250 }
251 else if( ( unicode_character >= 0x0380 )
252 && ( unicode_character < 0x03d0 ) )
253 {
254 unicode_character -= 0x0380;
255
256 byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x0380[ unicode_character ];
257 }
258 else if( ( unicode_character >= 0x2010 )
259 && ( unicode_character < 0x2038 ) )
260 {
261 unicode_character -= 0x2010;
262
263 byte_stream_value = libuna_codepage_mac_greek_unicode_to_byte_stream_base_0x2010[ unicode_character ];
264 }
265 else switch( unicode_character )
266 {
267 case 0x0153:
268 byte_stream_value = 0xcf;
269 break;
270
271 case 0x20ac:
272 byte_stream_value = 0x9c;
273 break;
274
275 case 0x2122:
276 byte_stream_value = 0x93;
277 break;
278
279 case 0x2248:
280 byte_stream_value = 0xc5;
281 break;
282
283 case 0x2260:
284 byte_stream_value = 0xad;
285 break;
286
287 case 0x2264:
288 byte_stream_value = 0xb2;
289 break;
290
291 case 0x2265:
292 byte_stream_value = 0xb3;
293 break;
294
295 default:
296 byte_stream_value = 0x1a;
297 break;
298 }
299 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
300
301 *byte_stream_index = safe_byte_stream_index;
302
303 return( 1 );
304 }
305