Debian Codegraph @

libclocale/libclocale_locale.c

raw symbols SCIP
1 /*
2 * Locale functions
3 *
4 * Copyright (C) 2010-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 <narrow_string.h>
24 #include <types.h>
25
26 #if defined( __BORLANDC__ ) && __BORLANDC__ <= 0x0520
27 #include <locale.h>
28 #elif defined( HAVE_LOCALE_H )
29 #include <locale.h>
30 #endif
31
32 #if defined( HAVE_LANGINFO_H )
33 #include <langinfo.h>
34 #endif
35
36 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
37 #include <stdlib.h>
38 #endif
39
40 #include "libclocale_definitions.h"
41 #include "libclocale_codepage.h"
42 #include "libclocale_locale.h"
43
44 #if defined( WINAPI ) && ( WINVER < 0x0500 )
45
46 /* Cross Windows safe version of GetLocaleInfoA
47 * Returns the number of bytes read if successful or 0 on error
48 */
49 int libclocale_GetLocaleInfoA(
50 LCID locale_identifier,
51 LCTYPE locale_type,
52 LPSTR buffer,
53 int size )
54 {
55 FARPROC function = NULL;
56 HMODULE library_handle = NULL;
57 int result = 0;
58
59 library_handle = LoadLibrary(
60 _SYSTEM_STRING( "kernel32.dll" ) );
61
62 if( library_handle == NULL )
63 {
64 return( 0 );
65 }
66 function = GetProcAddress(
67 library_handle,
68 (LPCSTR) "GetLocaleInfoA" );
69
70 if( function != NULL )
71 {
72 result = function(
73 locale_identifier,
74 locale_type,
75 buffer,
76 size );
77 }
78 /* This call should be after using the function
79 * in most cases kernel32.dll will still be available after free
80 */
81 if( FreeLibrary(
82 library_handle ) != TRUE )
83 {
84 result = 0;
85 }
86 return( result );
87 }
88
89 #endif /* defined( WINAPI ) && ( WINVER < 0x0500 ) */
90
91 /* Retrieves the codepage from the locale character set
92 * The codepage is set to 0 if the character set is UTF-8
93 * and will default to LIBCLOCALE_CODEPAGE_ASCII the codepage cannot be determined
94 * Returns 1 if successful or -1 on error
95 */
96 int libclocale_locale_get_codepage_from_charset(
97 int *codepage,
98 char *charset,
99 size_t charset_length,
100 libcerror_error_t **error )
101 {
102 static char *function = "libclocale_locale_get_codepage_from_charset";
103
104 if( codepage == NULL )
105 {
106 libcerror_error_set(
107 error,
108 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
109 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
110 "%s: invalid codepage.",
111 function );
112
113 return( -1 );
114 }
115 if( charset == NULL )
116 {
117 libcerror_error_set(
118 error,
119 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
121 "%s: invalid charset.",
122 function );
123
124 return( -1 );
125 }
126 if( charset_length > (size_t) SSIZE_MAX )
127 {
128 libcerror_error_set(
129 error,
130 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
131 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
132 "%s: invalid charset length value exceeds maximum.",
133 function );
134
135 return( -1 );
136 }
137 /* Determine codepage
138 */
139 *codepage = -1;
140
141 if( *codepage == -1 )
142 {
143 if( charset_length == 5 )
144 {
145 if( narrow_string_compare_no_case(
146 "UTF-8",
147 charset,
148 5 ) == 0 )
149 {
150 *codepage = 0;
151 }
152 }
153 }
154 if( *codepage == -1 )
155 {
156 if( charset_length >= 3 )
157 {
158 if( narrow_string_compare(
159 "874",
160 charset,
161 3 ) == 0 )
162 {
163 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_874;
164 }
165 else if( narrow_string_compare(
166 "932",
167 charset,
168 3 ) == 0 )
169 {
170 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_932;
171 }
172 else if( narrow_string_compare(
173 "936",
174 charset,
175 3 ) == 0 )
176 {
177 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_936;
178 }
179 else if( narrow_string_compare(
180 "949",
181 charset,
182 3 ) == 0 )
183 {
184 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_949;
185 }
186 else if( narrow_string_compare(
187 "950",
188 charset,
189 3 ) == 0 )
190 {
191 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_950;
192 }
193 }
194 }
195 if( *codepage == -1 )
196 {
197 if( charset_length >= 4 )
198 {
199 if( narrow_string_compare(
200 "1250",
201 charset,
202 4 ) == 0 )
203 {
204 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1250;
205 }
206 else if( narrow_string_compare(
207 "1251",
208 charset,
209 4 ) == 0 )
210 {
211 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1251;
212 }
213 else if( narrow_string_compare(
214 "1252",
215 charset,
216 4 ) == 0 )
217 {
218 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1252;
219 }
220 else if( narrow_string_compare(
221 "1253",
222 charset,
223 4 ) == 0 )
224 {
225 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1253;
226 }
227 else if( narrow_string_compare(
228 "1254",
229 charset,
230 4 ) == 0 )
231 {
232 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1254;
233 }
234 else if( narrow_string_compare(
235 "1255",
236 charset,
237 4 ) == 0 )
238 {
239 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1255;
240 }
241 else if( narrow_string_compare(
242 "1256",
243 charset,
244 4 ) == 0 )
245 {
246 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1256;
247 }
248 else if( narrow_string_compare(
249 "1257",
250 charset,
251 4 ) == 0 )
252 {
253 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1257;
254 }
255 else if( narrow_string_compare(
256 "1258",
257 charset,
258 4 ) == 0 )
259 {
260 *codepage = LIBCLOCALE_CODEPAGE_WINDOWS_1258;
261 }
262 else if( narrow_string_compare_no_case(
263 "utf8",
264 charset,
265 4 ) == 0 )
266 {
267 *codepage = 0;
268 }
269 }
270 }
271 if( *codepage == -1 )
272 {
273 *codepage = LIBCLOCALE_CODEPAGE_ASCII;
274 }
275 return( 1 );
276 }
277
278 /* Retrieves the codepage for the locale character set
279 * The codepage is set to 0 if the character set is UTF-8
280 * and will default to LIBCLOCALE_CODEPAGE_ASCII the codepage cannot be determined
281 * Returns 1 if successful or -1 on error
282 */
283 int libclocale_locale_get_codepage(
284 int *codepage,
285 libcerror_error_t **error )
286 {
287 char *locale = NULL;
288 char *charset = NULL;
289 static char *function = "libclocale_locale_get_codepage";
290 size_t charset_length = 0;
291 size_t locale_length = 0;
292
293 if( codepage == NULL )
294 {
295 libcerror_error_set(
296 error,
297 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
299 "%s: invalid codepage.",
300 function );
301
302 return( -1 );
303 }
304 #if defined( HAVE_LANGINFO_CODESET )
305 charset = nl_langinfo(
306 CODESET );
307 #endif
308 if( ( charset != NULL )
309 && ( charset[ 0 ] != 0 ) )
310 {
311 charset_length = narrow_string_length(
312 charset );
313 }
314 else
315 {
316 #if defined( HAVE_SETLOCALE ) || ( defined( __BORLANDC__ ) && __BORLANDC__ <= 0x0520 )
317 locale = setlocale(
318 LC_CTYPE,
319 "" );
320
321 /* Check if the locale returned is not "C"
322 */
323 if( ( locale != NULL )
324 && ( locale[ 0 ] != 0 ) )
325 {
326 if( ( locale[ 0 ] == 'C' )
327 && ( locale[ 1 ] != 0 ) )
328 {
329 locale = NULL;
330 }
331 }
332 #endif
333 #if defined( HAVE_GETENV ) || defined( WINAPI )
334 /* Check if the locale can be determined from the environment variables
335 */
336 if( ( locale == NULL )
337 || ( locale[ 0 ] == 0 ) )
338 {
339 locale = getenv(
340 "LC_ALL" );
341 }
342 if( ( locale == NULL )
343 || ( locale[ 0 ] == 0 ) )
344 {
345 locale = getenv(
346 "LC_TYPE" );
347 }
348 if( ( locale == NULL )
349 || ( locale[ 0 ] == 0 ) )
350 {
351 locale = getenv(
352 "LANG" );
353 }
354 #endif
355 if( ( locale == NULL )
356 || ( locale[ 0 ] == 0 ) )
357 {
358 *codepage = LIBCLOCALE_CODEPAGE_ASCII;
359
360 return( 1 );
361 }
362 locale_length = narrow_string_length(
363 locale );
364
365 charset = narrow_string_search_character(
366 locale,
367 '.',
368 locale_length + 1 );
369
370 if( charset == NULL )
371 {
372 *codepage = LIBCLOCALE_CODEPAGE_ASCII;
373
374 return( 1 );
375 }
376 charset++;
377
378 charset_length = locale_length - (size_t) ( charset - locale );
379 }
380 if( libclocale_locale_get_codepage_from_charset(
381 codepage,
382 charset,
383 charset_length,
384 error ) != 1 )
385 {
386 libcerror_error_set(
387 error,
388 LIBCERROR_ERROR_DOMAIN_RUNTIME,
389 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
390 "%s: unable to retrieve codepage.",
391 function );
392
393 return( -1 );
394 }
395 return( 1 );
396 }
397
398 /* Retrieves the decimal point
399 * Returns 1 if successful or -1 on error
400 */
401 int libclocale_locale_get_decimal_point(
402 int *decimal_point,
403 libcerror_error_t **error )
404 {
405 static char *function = "libclocale_locale_get_decimal_point";
406
407 #if defined( WINAPI )
408 DWORD error_code = 0;
409 DWORD locale_data = 0;
410 int read_count = 0;
411 #else
412 struct lconv *locale_data = NULL;
413 #endif
414
415 if( decimal_point == NULL )
416 {
417 libcerror_error_set(
418 error,
419 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
420 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
421 "%s: invalid decimal point.",
422 function );
423
424 return( -1 );
425 }
426 *decimal_point = -1;
427
428 #if defined( WINAPI )
429 #if ( WINVER >= 0x0600 )
430 read_count = GetLocaleInfoEx(
431 LOCALE_NAME_USER_DEFAULT,
432 LOCALE_SDECIMAL,
433 (LPWSTR) &locale_data,
434 sizeof( DWORD ) / sizeof( wchar_t ) );
435
436 #elif ( WINVER >= 0x0500 )
437 read_count = GetLocaleInfoA(
438 LOCALE_USER_DEFAULT,
439 LOCALE_SDECIMAL,
440 (LPSTR) &locale_data,
441 sizeof( DWORD ) / sizeof( char ) );
442 #else
443 read_count = libclocale_GetLocaleInfoA(
444 LOCALE_USER_DEFAULT,
445 LOCALE_SDECIMAL,
446 (LPSTR) &locale_data,
447 sizeof( DWORD ) / sizeof( char ) );
448 #endif
449 if( read_count == 0 )
450 {
451 error_code = GetLastError();
452
453 libcerror_system_set_error(
454 error,
455 LIBCERROR_ERROR_DOMAIN_RUNTIME,
456 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
457 error_code,
458 "%s: unable to retrieve locale information.",
459 function );
460
461 return( -1 );
462 }
463 *decimal_point = (int) locale_data;
464
465 #else
466 locale_data = localeconv();
467
468 if( locale_data == NULL )
469 {
470 libcerror_error_set(
471 error,
472 LIBCERROR_ERROR_DOMAIN_RUNTIME,
473 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
474 "%s: invalid locale data.",
475 function );
476
477 return( -1 );
478 }
479 if( locale_data->decimal_point == NULL )
480 {
481 libcerror_error_set(
482 error,
483 LIBCERROR_ERROR_DOMAIN_RUNTIME,
484 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
485 "%s: invalid locale data - missing decimal point.",
486 function );
487
488 return( -1 );
489 }
490 *decimal_point = ( locale_data->decimal_point )[ 0 ];
491 #endif
492 return( 1 );
493 }
494