Debian Codegraph @

libuna/libuna_codepage_windows_1253.c

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