Debian Codegraph @

libuna/libuna_codepage_windows_1252.c

raw symbols SCIP
1 /*
2 * Windows 1252 codepage (Western European/Latin 1) 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_1252.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the Windows 1252 codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_windows_1252_byte_stream_to_unicode_base_0x80[ 32 ] = {
33 0x20ac, 0xfffd, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
34 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0xfffd, 0x017d, 0xfffd,
35 0xfffd, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
36 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0xfffd, 0x017e, 0x0178
37 };
38
39 /* Unicode to ASCII character lookup tables for the Windows 1252 codepage
40 * Unknown are filled with the ASCII replacement character 0x1a
41 */
42 const uint8_t libuna_codepage_windows_1252_unicode_to_byte_stream_base_0x2010[ 24 ] = {
43 0x1a, 0x1a, 0x1a, 0x96, 0x97, 0x1a, 0x1a, 0x1a,
44 0x91, 0x92, 0x82, 0x1a, 0x93, 0x94, 0x84, 0x1a,
45 0x86, 0x87, 0x95, 0x1a, 0x1a, 0x1a, 0x85, 0x1a
46 };
47
48 /* Copies an Unicode character from a Windows 1252 encoded byte stream
49 * Returns 1 if successful or -1 on error
50 */
51 int libuna_codepage_windows_1252_copy_from_byte_stream(
52 libuna_unicode_character_t *unicode_character,
53 const uint8_t *byte_stream,
54 size_t byte_stream_size,
55 size_t *byte_stream_index,
56 libcerror_error_t **error )
57 {
58 static char *function = "libuna_codepage_windows_1252_copy_from_byte_stream";
59 libuna_unicode_character_t safe_unicode_character = 0xfffd;
60 size_t safe_byte_stream_index = 0;
61 uint8_t byte_stream_character = 0;
62
63 if( unicode_character == NULL )
64 {
65 libcerror_error_set(
66 error,
67 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
68 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
69 "%s: invalid Unicode character.",
70 function );
71
72 return( -1 );
73 }
74 if( byte_stream == NULL )
75 {
76 libcerror_error_set(
77 error,
78 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
79 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
80 "%s: invalid byte stream.",
81 function );
82
83 return( -1 );
84 }
85 if( byte_stream_size > (size_t) SSIZE_MAX )
86 {
87 libcerror_error_set(
88 error,
89 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
90 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
91 "%s: invalid byte stream size value exceeds maximum.",
92 function );
93
94 return( -1 );
95 }
96 if( byte_stream_index == NULL )
97 {
98 libcerror_error_set(
99 error,
100 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
101 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
102 "%s: invalid byte stream index.",
103 function );
104
105 return( -1 );
106 }
107 safe_byte_stream_index = *byte_stream_index;
108
109 if( safe_byte_stream_index >= byte_stream_size )
110 {
111 libcerror_error_set(
112 error,
113 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
114 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
115 "%s: byte stream too small.",
116 function );
117
118 return( -1 );
119 }
120 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
121
122 if( ( byte_stream_character < 0x80 )
123 || ( byte_stream_character >= 0xa0 ) )
124 {
125 safe_unicode_character = byte_stream_character;
126 }
127 else
128 {
129 byte_stream_character -= 0x80;
130
131 safe_unicode_character = libuna_codepage_windows_1252_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
132 }
133 *unicode_character = safe_unicode_character;
134 *byte_stream_index = safe_byte_stream_index;
135
136 return( 1 );
137 }
138
139 /* Copies an Unicode character to a Windows 1252 encoded byte stream
140 * Returns 1 if successful or -1 on error
141 */
142 int libuna_codepage_windows_1252_copy_to_byte_stream(
143 libuna_unicode_character_t unicode_character,
144 uint8_t *byte_stream,
145 size_t byte_stream_size,
146 size_t *byte_stream_index,
147 libcerror_error_t **error )
148 {
149 static char *function = "libuna_codepage_windows_1252_copy_to_byte_stream";
150 size_t safe_byte_stream_index = 0;
151 uint16_t byte_stream_value = 0x001a;
152
153 if( byte_stream == NULL )
154 {
155 libcerror_error_set(
156 error,
157 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159 "%s: invalid byte stream.",
160 function );
161
162 return( -1 );
163 }
164 if( byte_stream_size > (size_t) SSIZE_MAX )
165 {
166 libcerror_error_set(
167 error,
168 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
170 "%s: invalid byte stream size value exceeds maximum.",
171 function );
172
173 return( -1 );
174 }
175 if( byte_stream_index == NULL )
176 {
177 libcerror_error_set(
178 error,
179 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
181 "%s: invalid byte stream index.",
182 function );
183
184 return( -1 );
185 }
186 safe_byte_stream_index = *byte_stream_index;
187
188 if( safe_byte_stream_index >= byte_stream_size )
189 {
190 libcerror_error_set(
191 error,
192 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
194 "%s: byte stream too small.",
195 function );
196
197 return( -1 );
198 }
199 if( unicode_character < 0x0080 )
200 {
201 byte_stream_value = (uint16_t) unicode_character;
202 }
203 else if( ( unicode_character >= 0x00a0 )
204 && ( unicode_character < 0x0100 ) )
205 {
206 byte_stream_value = (uint16_t) unicode_character;
207 }
208 else if( ( unicode_character >= 0x2010 )
209 && ( unicode_character < 0x2028 ) )
210 {
211 unicode_character -= 0x2010;
212
213 byte_stream_value = libuna_codepage_windows_1252_unicode_to_byte_stream_base_0x2010[ unicode_character ];
214 }
215 else switch( unicode_character )
216 {
217 case 0x0152:
218 byte_stream_value = 0x8c;
219 break;
220
221 case 0x0153:
222 byte_stream_value = 0x9c;
223 break;
224
225 case 0x0160:
226 byte_stream_value = 0x8a;
227 break;
228
229 case 0x0161:
230 byte_stream_value = 0x9a;
231 break;
232
233 case 0x0178:
234 byte_stream_value = 0x9f;
235 break;
236
237 case 0x017d:
238 byte_stream_value = 0x8e;
239 break;
240
241 case 0x017e:
242 byte_stream_value = 0x9e;
243 break;
244
245 case 0x0192:
246 byte_stream_value = 0x83;
247 break;
248
249 case 0x02c6:
250 byte_stream_value = 0x88;
251 break;
252
253 case 0x02dc:
254 byte_stream_value = 0x98;
255 break;
256
257 case 0x2030:
258 byte_stream_value = 0x89;
259 break;
260
261 case 0x2039:
262 byte_stream_value = 0x8b;
263 break;
264
265 case 0x203a:
266 byte_stream_value = 0x9b;
267 break;
268
269 case 0x20ac:
270 byte_stream_value = 0x80;
271 break;
272
273 case 0x2122:
274 byte_stream_value = 0x99;
275 break;
276
277 default:
278 byte_stream_value = 0x1a;
279 break;
280 }
281 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
282
283 *byte_stream_index = safe_byte_stream_index;
284
285 return( 1 );
286 }
287