/src/libvhdi/libuna/libuna_codepage_windows_874.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Windows 874 codepage (Thai) 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_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 | 0 | { |
83 | 0 | static char *function = "libuna_codepage_windows_874_copy_from_byte_stream"; |
84 | 0 | libuna_unicode_character_t safe_unicode_character = 0xfffd; |
85 | 0 | size_t safe_byte_stream_index = 0; |
86 | 0 | uint8_t byte_stream_character = 0; |
87 | |
|
88 | 0 | if( unicode_character == NULL ) |
89 | 0 | { |
90 | 0 | libcerror_error_set( |
91 | 0 | error, |
92 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
93 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
94 | 0 | "%s: invalid Unicode character.", |
95 | 0 | function ); |
96 | |
|
97 | 0 | return( -1 ); |
98 | 0 | } |
99 | 0 | if( byte_stream == NULL ) |
100 | 0 | { |
101 | 0 | libcerror_error_set( |
102 | 0 | error, |
103 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
104 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
105 | 0 | "%s: invalid byte stream.", |
106 | 0 | function ); |
107 | |
|
108 | 0 | return( -1 ); |
109 | 0 | } |
110 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
111 | 0 | { |
112 | 0 | libcerror_error_set( |
113 | 0 | error, |
114 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
115 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
116 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
117 | 0 | function ); |
118 | |
|
119 | 0 | return( -1 ); |
120 | 0 | } |
121 | 0 | if( byte_stream_index == NULL ) |
122 | 0 | { |
123 | 0 | libcerror_error_set( |
124 | 0 | error, |
125 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
126 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
127 | 0 | "%s: invalid byte stream index.", |
128 | 0 | function ); |
129 | |
|
130 | 0 | return( -1 ); |
131 | 0 | } |
132 | 0 | safe_byte_stream_index = *byte_stream_index; |
133 | |
|
134 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
135 | 0 | { |
136 | 0 | libcerror_error_set( |
137 | 0 | error, |
138 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
139 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
140 | 0 | "%s: byte stream too small.", |
141 | 0 | function ); |
142 | |
|
143 | 0 | return( -1 ); |
144 | 0 | } |
145 | 0 | byte_stream_character = byte_stream[ safe_byte_stream_index++ ]; |
146 | |
|
147 | 0 | if( byte_stream_character < 0x80 ) |
148 | 0 | { |
149 | 0 | safe_unicode_character = byte_stream_character; |
150 | 0 | } |
151 | 0 | else |
152 | 0 | { |
153 | 0 | byte_stream_character -= 0x80; |
154 | |
|
155 | 0 | safe_unicode_character = libuna_codepage_windows_874_byte_stream_to_unicode_base_0x80[ byte_stream_character ]; |
156 | 0 | } |
157 | 0 | *unicode_character = safe_unicode_character; |
158 | 0 | *byte_stream_index = safe_byte_stream_index; |
159 | |
|
160 | 0 | return( 1 ); |
161 | 0 | } |
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 | 0 | { |
173 | 0 | static char *function = "libuna_codepage_windows_874_copy_to_byte_stream"; |
174 | 0 | size_t safe_byte_stream_index = 0; |
175 | 0 | uint16_t byte_stream_value = 0x001a; |
176 | |
|
177 | 0 | if( byte_stream == NULL ) |
178 | 0 | { |
179 | 0 | libcerror_error_set( |
180 | 0 | error, |
181 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
182 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
183 | 0 | "%s: invalid byte stream.", |
184 | 0 | function ); |
185 | |
|
186 | 0 | return( -1 ); |
187 | 0 | } |
188 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
189 | 0 | { |
190 | 0 | libcerror_error_set( |
191 | 0 | error, |
192 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
193 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
194 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
195 | 0 | function ); |
196 | |
|
197 | 0 | return( -1 ); |
198 | 0 | } |
199 | 0 | if( byte_stream_index == NULL ) |
200 | 0 | { |
201 | 0 | libcerror_error_set( |
202 | 0 | error, |
203 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
204 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
205 | 0 | "%s: invalid byte stream index.", |
206 | 0 | function ); |
207 | |
|
208 | 0 | return( -1 ); |
209 | 0 | } |
210 | 0 | safe_byte_stream_index = *byte_stream_index; |
211 | |
|
212 | 0 | if( safe_byte_stream_index >= byte_stream_size ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
217 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
218 | 0 | "%s: byte stream too small.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 0 | if( ( unicode_character < 0x0080 ) |
224 | 0 | || ( unicode_character == 0x00a0 ) ) |
225 | 0 | { |
226 | 0 | byte_stream_value = (uint16_t) unicode_character; |
227 | 0 | } |
228 | 0 | else if( ( unicode_character >= 0x0e00 ) |
229 | 0 | && ( unicode_character < 0x0e60 ) ) |
230 | 0 | { |
231 | 0 | unicode_character -= 0x0e00; |
232 | |
|
233 | 0 | byte_stream_value = libuna_codepage_windows_874_unicode_to_byte_stream_base_0x0e00[ unicode_character ]; |
234 | 0 | } |
235 | 0 | else if( ( unicode_character >= 0x2018 ) |
236 | 0 | && ( unicode_character < 0x2020 ) ) |
237 | 0 | { |
238 | 0 | unicode_character -= 0x2018; |
239 | |
|
240 | 0 | byte_stream_value = libuna_codepage_windows_874_unicode_to_byte_stream_base_0x2018[ unicode_character ]; |
241 | 0 | } |
242 | 0 | else switch( unicode_character ) |
243 | 0 | { |
244 | 0 | case 0x2013: |
245 | 0 | byte_stream_value = 0x96; |
246 | 0 | break; |
247 | | |
248 | 0 | case 0x2014: |
249 | 0 | byte_stream_value = 0x97; |
250 | 0 | break; |
251 | | |
252 | 0 | case 0x2022: |
253 | 0 | byte_stream_value = 0x95; |
254 | 0 | break; |
255 | | |
256 | 0 | case 0x2026: |
257 | 0 | byte_stream_value = 0x85; |
258 | 0 | break; |
259 | | |
260 | 0 | case 0x20ac: |
261 | 0 | byte_stream_value = 0x80; |
262 | 0 | break; |
263 | | |
264 | 0 | default: |
265 | 0 | byte_stream_value = 0x1a; |
266 | 0 | break; |
267 | 0 | } |
268 | 0 | byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( byte_stream_value & 0x00ff ); |
269 | |
|
270 | 0 | *byte_stream_index = safe_byte_stream_index; |
271 | |
|
272 | 0 | return( 1 ); |
273 | 0 | } |
274 | | |