Debian Codegraph @

libuna/libuna_codepage_mac_thai.c

raw symbols SCIP
1 /*
2 * MacThai 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_thai.h"
26 #include "libuna_libcerror.h"
27 #include "libuna_types.h"
28
29 /* Extended ASCII to Unicode character lookup table for the MacThai codepage
30 * Unknown are filled with the Unicode replacement character 0xfffd
31 */
32 const uint16_t libuna_codepage_mac_thai_byte_stream_to_unicode_base_0x80[ 128 ] = {
33 0x00ab, 0x00bb, 0x2026, 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c,
34 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x201c, 0x201d, 0x0e4d,
35 0xfffd, 0x2022, 0x0e31, 0x0e47, 0x0e34, 0x0e35, 0x0e36, 0x0e37,
36 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x2018, 0x2019, 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, 0x2060, 0x200b, 0x2013, 0x2014, 0x0e3f,
45 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47,
46 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x2122, 0x0e4f,
47 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57,
48 0x0e58, 0x0e59, 0x00ae, 0x00a9, 0xfffd, 0xfffd, 0xfffd, 0xfffd
49 };
50
51 /* Unicode to ASCII character lookup tables for the MacThai codepage
52 * Unknown are filled with the ASCII replacement character 0x1a
53 */
54 const uint8_t libuna_codepage_mac_thai_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, 0x1a, 0xef,
65 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
66 0xf8, 0xf9, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a
67 };
68
69 const uint8_t libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x2008[ 32 ] = {
70 0x1a, 0x1a, 0x1a, 0xdc, 0x1a, 0x1a, 0x1a, 0x1a,
71 0x1a, 0x1a, 0x1a, 0xdd, 0xde, 0x1a, 0x1a, 0x1a,
72 0x9d, 0x9e, 0x1a, 0x1a, 0x8d, 0x8e, 0x1a, 0x1a,
73 0x1a, 0x1a, 0x91, 0x1a, 0x1a, 0x1a, 0x82, 0x1a
74 };
75
76 /* Determines the size of a MacThai encoded byte stream from an Unicode character
77 * Adds the size to the byte stream character size value
78 * Returns 1 if successful, 0 if the byte stream character is valid but not supported since it requires special handling or -1 on error
79 */
80 int libuna_codepage_mac_thai_unicode_character_size_to_byte_stream(
81 libuna_unicode_character_t unicode_character,
82 size_t *byte_stream_character_size,
83 libcerror_error_t **error )
84 {
85 static char *function = "libuna_codepage_mac_thai_unicode_character_size_to_byte_stream";
86 int result = 0;
87
88 if( byte_stream_character_size == NULL )
89 {
90 libcerror_error_set(
91 error,
92 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
93 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
94 "%s: invalid byte stream character size.",
95 function );
96
97 return( -1 );
98 }
99 switch( unicode_character )
100 {
101 case 0x00000e31UL:
102 case 0x00000e34UL:
103 case 0x00000e35UL:
104 case 0x00000e36UL:
105 case 0x00000e37UL:
106 case 0x00000e47UL:
107 case 0x00000e48UL:
108 case 0x00000e49UL:
109 case 0x00000e4aUL:
110 case 0x00000e4bUL:
111 case 0x00000e4cUL:
112 case 0x00000e4dUL:
113 result = 0;
114 break;
115
116 default:
117 *byte_stream_character_size += 1;
118
119 result = 1;
120 break;
121 }
122 return( result );
123 }
124
125 /* Copies an Unicode character from a MacThai encoded byte stream
126 * Returns 1 if successful or -1 on error
127 */
128 int libuna_codepage_mac_thai_copy_from_byte_stream(
129 libuna_unicode_character_t *unicode_character,
130 const uint8_t *byte_stream,
131 size_t byte_stream_size,
132 size_t *byte_stream_index,
133 libcerror_error_t **error )
134 {
135 static char *function = "libuna_codepage_mac_thai_copy_from_byte_stream";
136 libuna_unicode_character_t safe_unicode_character = 0xfffd;
137 size_t safe_byte_stream_index = 0;
138 uint8_t byte_stream_character = 0;
139
140 if( unicode_character == NULL )
141 {
142 libcerror_error_set(
143 error,
144 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
145 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
146 "%s: invalid Unicode character.",
147 function );
148
149 return( -1 );
150 }
151 if( byte_stream == NULL )
152 {
153 libcerror_error_set(
154 error,
155 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157 "%s: invalid byte stream.",
158 function );
159
160 return( -1 );
161 }
162 if( byte_stream_size > (size_t) SSIZE_MAX )
163 {
164 libcerror_error_set(
165 error,
166 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
168 "%s: invalid byte stream size value exceeds maximum.",
169 function );
170
171 return( -1 );
172 }
173 if( byte_stream_index == NULL )
174 {
175 libcerror_error_set(
176 error,
177 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
178 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
179 "%s: invalid byte stream index.",
180 function );
181
182 return( -1 );
183 }
184 safe_byte_stream_index = *byte_stream_index;
185
186 if( safe_byte_stream_index >= byte_stream_size )
187 {
188 libcerror_error_set(
189 error,
190 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
191 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
192 "%s: byte stream too small.",
193 function );
194
195 return( -1 );
196 }
197 byte_stream_character = byte_stream[ safe_byte_stream_index++ ];
198
199 if( byte_stream_character < 0x80 )
200 {
201 safe_unicode_character = byte_stream_character;
202 }
203 else
204 {
205 byte_stream_character -= 0x80;
206
207 safe_unicode_character = libuna_codepage_mac_thai_byte_stream_to_unicode_base_0x80[ byte_stream_character ];
208 }
209 *unicode_character = safe_unicode_character;
210 *byte_stream_index = safe_byte_stream_index;
211
212 return( 1 );
213 }
214
215 /* Copies an Unicode character to a MacThai encoded byte stream
216 * Returns 1 if successful or -1 on error
217 */
218 int libuna_codepage_mac_thai_copy_to_byte_stream(
219 libuna_unicode_character_t unicode_character,
220 uint8_t *byte_stream,
221 size_t byte_stream_size,
222 size_t *byte_stream_index,
223 libcerror_error_t **error )
224 {
225 static char *function = "libuna_codepage_mac_thai_copy_to_byte_stream";
226 size_t safe_byte_stream_index = 0;
227 uint16_t byte_stream_value = 0x001a;
228
229 if( byte_stream == NULL )
230 {
231 libcerror_error_set(
232 error,
233 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
235 "%s: invalid byte stream.",
236 function );
237
238 return( -1 );
239 }
240 if( byte_stream_size > (size_t) SSIZE_MAX )
241 {
242 libcerror_error_set(
243 error,
244 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
245 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
246 "%s: invalid byte stream size value exceeds maximum.",
247 function );
248
249 return( -1 );
250 }
251 if( byte_stream_index == NULL )
252 {
253 libcerror_error_set(
254 error,
255 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
256 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
257 "%s: invalid byte stream index.",
258 function );
259
260 return( -1 );
261 }
262 safe_byte_stream_index = *byte_stream_index;
263
264 if( safe_byte_stream_index >= byte_stream_size )
265 {
266 libcerror_error_set(
267 error,
268 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
269 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
270 "%s: byte stream too small.",
271 function );
272
273 return( -1 );
274 }
275 if( ( unicode_character < 0x0080 )
276 || ( unicode_character == 0x00a0 ) )
277 {
278 byte_stream_value = (uint16_t) unicode_character;
279 }
280 else if( ( unicode_character >= 0x0e00 )
281 && ( unicode_character < 0x0e60 ) )
282 {
283 unicode_character -= 0x0e00;
284
285 byte_stream_value = libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x0e00[ unicode_character ];
286 }
287 else if( ( unicode_character >= 0x2008 )
288 && ( unicode_character < 0x2028 ) )
289 {
290 unicode_character -= 0x2008;
291
292 byte_stream_value = libuna_codepage_mac_thai_unicode_to_byte_stream_base_0x2008[ unicode_character ];
293 }
294 else switch( unicode_character )
295 {
296 case 0x00a9:
297 byte_stream_value = 0xfb;
298 break;
299
300 case 0x00ab:
301 byte_stream_value = 0x80;
302 break;
303
304 case 0x00ae:
305 byte_stream_value = 0xfa;
306 break;
307
308 case 0x00bb:
309 byte_stream_value = 0x81;
310 break;
311
312 case 0x2060:
313 byte_stream_value = 0xdb;
314 break;
315
316 case 0x2122:
317 byte_stream_value = 0xee;
318 break;
319
320 default:
321 byte_stream_value = 0x1a;
322 break;
323 }
324 byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff );
325
326 *byte_stream_index = safe_byte_stream_index;
327
328 return( 1 );
329 }
330