/src/libfmapi/libuna/libuna_codepage_windows_1253.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Windows 1253 codepage (Greek) 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_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 | 0 | { |
90 | 0 | static char *function = "libuna_codepage_windows_1253_copy_from_byte_stream"; |
91 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
92 | 0 | size_t safe_byte_stream_index = 0; |
93 | 0 | uint8_t byte_stream_character = 0; |
94 | |
|
95 | 0 | if( unicode_character == NULL ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
100 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
101 | 0 | "%s: invalid Unicode character.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | return( -1 ); |
105 | 0 | } |
106 | 0 | if( byte_stream == NULL ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
111 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
112 | 0 | "%s: invalid byte stream.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | return( -1 ); |
116 | 0 | } |
117 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
123 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 0 | if( byte_stream_index == 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 index.", |
135 | 0 | function ); |
136 | |
|
137 | 0 | return( -1 ); |
138 | 0 | } |
139 | 0 | safe_byte_stream_index = *byte_stream_index; |
140 | |
|
141 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
142 | 0 | { |
143 | 0 | libcerror_error_set( |
144 | 0 | error, |
145 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
146 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
147 | 0 | "%s: byte stream too small.", |
148 | 0 | function ); |
149 | |
|
150 | 0 | return( -1 ); |
151 | 0 | } |
152 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
153 | |
|
154 | 0 | if( byte_stream_character < 0x80 ) |
155 | 0 | { |
156 | 0 | safe_unicode_character = byte_stream_character; |
157 | 0 | } |
158 | 0 | else |
159 | 0 | { |
160 | 0 | byte_stream_character -= 0x80; |
161 | |
|
162 | 0 | safe_unicode_character = libuna_codepage_windows_1253_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
163 | 0 | } |
164 | 0 | *unicode_character = safe_unicode_character; |
165 | 0 | *byte_stream_index = safe_byte_stream_index; |
166 | |
|
167 | 0 | return( 1 ); |
168 | 0 | } |
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 | 0 | { |
180 | 0 | static char *function = "libuna_codepage_windows_1253_copy_to_byte_stream"; |
181 | 0 | size_t safe_byte_stream_index = 0; |
182 | 0 | uint16_t byte_stream_value = 0x001a; |
183 | |
|
184 | 0 | if( byte_stream == NULL ) |
185 | 0 | { |
186 | 0 | libcerror_error_set( |
187 | 0 | error, |
188 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
189 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
190 | 0 | "%s: invalid byte stream.", |
191 | 0 | function ); |
192 | |
|
193 | 0 | return( -1 ); |
194 | 0 | } |
195 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
196 | 0 | { |
197 | 0 | libcerror_error_set( |
198 | 0 | error, |
199 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
200 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
201 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
202 | 0 | function ); |
203 | |
|
204 | 0 | return( -1 ); |
205 | 0 | } |
206 | 0 | if( byte_stream_index == 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 index.", |
213 | 0 | function ); |
214 | |
|
215 | 0 | return( -1 ); |
216 | 0 | } |
217 | 0 | safe_byte_stream_index = *byte_stream_index; |
218 | |
|
219 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
220 | 0 | { |
221 | 0 | libcerror_error_set( |
222 | 0 | error, |
223 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
224 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
225 | 0 | "%s: byte stream too small.", |
226 | 0 | function ); |
227 | |
|
228 | 0 | return( -1 ); |
229 | 0 | } |
230 | 0 | if( unicode_character < 0x0080 ) |
231 | 0 | { |
232 | 0 | byte_stream_value = (uint16_t) unicode_character; |
233 | 0 | } |
234 | 0 | else if( ( unicode_character >= 0x00a0 ) |
235 | 0 | && ( unicode_character < 0x00c0 ) ) |
236 | 0 | { |
237 | 0 | unicode_character -= 0x00a0; |
238 | |
|
239 | 0 | byte_stream_value = libuna_codepage_windows_1253_unicode_to_byte_stream_base_0x00a0[ unicode_character ]; |
240 | 0 | } |
241 | 0 | else if( ( unicode_character >= 0x0380 ) |
242 | 0 | && ( unicode_character < 0x03d0 ) ) |
243 | 0 | { |
244 | 0 | unicode_character -= 0x0380; |
245 | |
|
246 | 0 | byte_stream_value = libuna_codepage_windows_1253_unicode_to_byte_stream_base_0x0380[ unicode_character ]; |
247 | 0 | } |
248 | 0 | else if( ( unicode_character >= 0x2010 ) |
249 | 0 | && ( unicode_character < 0x2028 ) ) |
250 | 0 | { |
251 | 0 | unicode_character -= 0x2010; |
252 | |
|
253 | 0 | byte_stream_value = libuna_codepage_windows_1253_unicode_to_byte_stream_base_0x2010[ unicode_character ]; |
254 | 0 | } |
255 | 0 | else switch( unicode_character ) |
256 | 0 | { |
257 | 0 | case 0x0192: |
258 | 0 | byte_stream_value = 0x83; |
259 | 0 | break; |
260 | | |
261 | 0 | case 0x2030: |
262 | 0 | byte_stream_value = 0x89; |
263 | 0 | break; |
264 | | |
265 | 0 | case 0x2039: |
266 | 0 | byte_stream_value = 0x8b; |
267 | 0 | break; |
268 | | |
269 | 0 | case 0x203a: |
270 | 0 | byte_stream_value = 0x9b; |
271 | 0 | break; |
272 | | |
273 | 0 | case 0x20ac: |
274 | 0 | byte_stream_value = 0x80; |
275 | 0 | break; |
276 | | |
277 | 0 | case 0x2122: |
278 | 0 | byte_stream_value = 0x99; |
279 | 0 | break; |
280 | | |
281 | 0 | default: |
282 | 0 | byte_stream_value = 0x1a; |
283 | 0 | break; |
284 | 0 | } |
285 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
286 | |
|
287 | 0 | *byte_stream_index = safe_byte_stream_index; |
288 | |
|
289 | 0 | return( 1 ); |
290 | 0 | } |
291 | | |