Debian Codegraph @

libuna/libuna_codepage_windows_874.c

raw symbols SCIP
1 /*
2 * Windows 874 codepage (Thai) 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_874.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the Windows 874 codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_windows_874_byte_stream_to_unicode_base_0x80[ 128 ] = {
33 0x20ac, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0x2026, 0xfffd, 0xfffd,
34 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
35 0xfffd, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
36 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
37 0x00a0, 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07,
38 0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f,
39 0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17,
40 0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f,
41 0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27,
42 0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f,
43 0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37,
44 0x0e38, 0x0e39, 0x0e3a, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0x0e3f,
45 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47,
46 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0e4e, 0x0e4f,
47 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57,
48 0x0e58, 0x0e59, 0x0e5a, 0x0e5b, 0xfffd, 0xfffd, 0xfffd, 0xfffd
49 };
50
51 /* Unicode to ASCII character lookup tables for the Windows 874 codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_windows_874_unicode_to_byte_stream_base_0x0e00[ 96 ] = {
55 0x1a, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
56 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
57 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
58 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
59 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
60 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
61 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
62 0xd8, 0xd9, 0xda, 0x1a, 0x1a, 0x1a, 0x1a, 0xdf,
63 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
64 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
65 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
66 0xf8, 0xf9, 0xfa, 0xfb, 0x1a, 0x1a, 0x1a, 0x1a
67 };
68
69 const uint8_t libuna_codepage_windows_874_unicode_to_byte_stream_base_0x2018[ 8 ] = {
70 0x91, 0x92, 0x1a, 0x1a, 0x93, 0x94, 0x1a, 0x1a
71 };
72
73 /* Copies an Unicode character from a Windows 874 encoded byte stream
74 * Returns 1 if successful or -1 on error
75 */
76 int libuna_codepage_windows_874_copy_from_byte_stream(
77 libuna_unicode_character_t *unicode_character,
78 const uint8_t *byte_stream,
79 size_t byte_stream_size,
80 size_t *byte_stream_index,
81 libcerror_error_t **error )
82 {
83 static char *function = "libuna_codepage_windows_874_copy_from_byte_stream";
84 libuna_unicode_character_t safe_unicode_character = 0xfffd;
85 size_t safe_byte_stream_index = 0;
86 uint8_t byte_stream_character = 0;
87
88 if( unicode_character == NULL )
89 {
90 libcerror_error_set(
91 error,
92 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
93 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
94 "%s: invalid Unicode character.",
95 function );
96
97 return( -1 );
98 }
99 if( byte_stream == NULL )
100 {
101 libcerror_error_set(
102 error,
103 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
104 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
105 "%s: invalid byte stream.",
106 function );
107
108 return( -1 );
109 }
110 if( byte_stream_size > (size_t) SSIZE_MAX )
111 {
112 libcerror_error_set(
113 error,
114 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
115 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
116 "%s: invalid byte stream size value exceeds maximum.",
117 function );
118
119 return( -1 );
120 }
121 if( byte_stream_index == NULL )
122 {
123 libcerror_error_set(
124 error,
125 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
126 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
127 "%s: invalid byte stream index.",
128 function );
129
130 return( -1 );
131 }
132 safe_byte_stream_index = *byte_stream_index;
133
134 if( safe_byte_stream_index >= byte_stream_size )
135 {
136 libcerror_error_set(
137 error,
138 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
139 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
140 "%s: byte stream too small.",
141 function );
142
143 return( -1 );
144 }
145 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
146
147 if( byte_stream_character < 0x80 )
148 {
149 safe_unicode_character = byte_stream_character;
150 }
151 else
152 {
153 byte_stream_character -= 0x80;
154
155 safe_unicode_character = libuna_codepage_windows_874_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
156 }
157 *unicode_character = safe_unicode_character;
158 *byte_stream_index = safe_byte_stream_index;
159
160 return( 1 );
161 }
162
163 /* Copies an Unicode character to a Windows 874 encoded byte stream
164 * Returns 1 if successful or -1 on error
165 */
166 int libuna_codepage_windows_874_copy_to_byte_stream(
167 libuna_unicode_character_t unicode_character,
168 uint8_t *byte_stream,
169 size_t byte_stream_size,
170 size_t *byte_stream_index,
171 libcerror_error_t **error )
172 {
173 static char *function = "libuna_codepage_windows_874_copy_to_byte_stream";
174 size_t safe_byte_stream_index = 0;
175 uint16_t byte_stream_value = 0x001a;
176
177 if( byte_stream == NULL )
178 {
179 libcerror_error_set(
180 error,
181 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
182 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
183 "%s: invalid byte stream.",
184 function );
185
186 return( -1 );
187 }
188 if( byte_stream_size > (size_t) SSIZE_MAX )
189 {
190 libcerror_error_set(
191 error,
192 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
194 "%s: invalid byte stream size value exceeds maximum.",
195 function );
196
197 return( -1 );
198 }
199 if( byte_stream_index == NULL )
200 {
201 libcerror_error_set(
202 error,
203 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
204 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
205 "%s: invalid byte stream index.",
206 function );
207
208 return( -1 );
209 }
210 safe_byte_stream_index = *byte_stream_index;
211
212 if( safe_byte_stream_index >= byte_stream_size )
213 {
214 libcerror_error_set(
215 error,
216 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
217 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
218 "%s: byte stream too small.",
219 function );
220
221 return( -1 );
222 }
223 if( ( unicode_character < 0x0080 )
224 || ( unicode_character == 0x00a0 ) )
225 {
226 byte_stream_value = (uint16_t) unicode_character;
227 }
228 else if( ( unicode_character >= 0x0e00 )
229 && ( unicode_character < 0x0e60 ) )
230 {
231 unicode_character -= 0x0e00;
232
233 byte_stream_value = libuna_codepage_windows_874_unicode_to_byte_stream_base_0x0e00[ unicode_character ];
234 }
235 else if( ( unicode_character >= 0x2018 )
236 && ( unicode_character < 0x2020 ) )
237 {
238 unicode_character -= 0x2018;
239
240 byte_stream_value = libuna_codepage_windows_874_unicode_to_byte_stream_base_0x2018[ unicode_character ];
241 }
242 else switch( unicode_character )
243 {
244 case 0x2013:
245 byte_stream_value = 0x96;
246 break;
247
248 case 0x2014:
249 byte_stream_value = 0x97;
250 break;
251
252 case 0x2022:
253 byte_stream_value = 0x95;
254 break;
255
256 case 0x2026:
257 byte_stream_value = 0x85;
258 break;
259
260 case 0x20ac:
261 byte_stream_value = 0x80;
262 break;
263
264 default:
265 byte_stream_value = 0x1a;
266 break;
267 }
268 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
269
270 *byte_stream_index = safe_byte_stream_index;
271
272 return( 1 );
273 }
274