1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 1997-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * FILE NAME : putilimp.h
12 *
13 * Date Name Description
14 * 10/17/04 grhoten Move internal functions from putil.h to this file.
15 ******************************************************************************
16 */
17
18 #ifndef PUTILIMP_H
19 #define PUTILIMP_H
20
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
23
24 /**
25 * \def U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
26 * Nearly all CPUs and compilers implement a right-shift of a signed integer
27 * as an Arithmetic Shift Right which copies the sign bit (the Most Significant Bit (MSB))
28 * into the vacated bits (sign extension).
29 * For example, (int32_t)0xfff5fff3>>4 becomes 0xffff5fff and -1>>1=-1.
30 *
31 * This can be useful for storing a signed value in the upper bits
32 * and another bit field in the lower bits.
33 * The signed value can be retrieved by simple right-shifting.
34 *
35 * This is consistent with the Java language.
36 *
37 * However, the C standard allows compilers to implement a right-shift of a signed integer
38 * as a Logical Shift Right which copies a 0 into the vacated bits.
39 * For example, (int32_t)0xfff5fff3>>4 becomes 0x0fff5fff and -1>>1=0x7fffffff.
40 *
41 * Code that depends on the natural behavior should be guarded with this macro,
42 * with an alternate path for unusual platforms.
43 * @internal
44 */
45 #ifdef U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
46 /* Use the predefined value. */
47 #else
48 /*
49 * Nearly all CPUs & compilers implement a right-shift of a signed integer
50 * as an Arithmetic Shift Right (with sign extension).
51 */
52 # define U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC 1
53 #endif
54
55 /** Define this to 1 if your platform supports IEEE 754 floating point,
56 to 0 if it does not. */
57 #ifndef IEEE_754
58 # define IEEE_754 1
59 #endif
60
61 /**
62 * uintptr_t is an optional part of the standard definitions in stdint.h.
63 * The opengroup.org documentation for stdint.h says
64 * "On XSI-conformant systems, the intptr_t and uintptr_t types are required;
65 * otherwise, they are optional."
66 * We assume that when uintptr_t is defined, UINTPTR_MAX is defined as well.
67 *
68 * Do not use ptrdiff_t since it is signed. size_t is unsigned.
69 */
70 /* TODO: This check fails on some z environments. Filed a ticket #9357 for this. */
71 #if !defined(__intptr_t_defined) && !defined(UINTPTR_MAX) && (U_PLATFORM != U_PF_OS390)
72 typedef size_t uintptr_t;
73 #endif
74
75 /*===========================================================================*/
76 /** @{ Information about POSIX support */
77 /*===========================================================================*/
78
79 #ifdef U_HAVE_NL_LANGINFO_CODESET
80 /* Use the predefined value. */
81 #elif U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_ANDROID || U_PLATFORM == U_PF_QNX
82 # define U_HAVE_NL_LANGINFO_CODESET 0
83 #else
84 # define U_HAVE_NL_LANGINFO_CODESET 1
85 #endif
86
87 #ifdef U_NL_LANGINFO_CODESET
88 /* Use the predefined value. */
89 #elif !U_HAVE_NL_LANGINFO_CODESET
90 # define U_NL_LANGINFO_CODESET -1
91 #elif U_PLATFORM == U_PF_OS400
92 /* not defined */
93 #else
94 # define U_NL_LANGINFO_CODESET CODESET
95 #endif
96
97 #if defined(U_TZSET) || defined(U_HAVE_TZSET)
98 /* Use the predefined value. */
99 #elif U_PLATFORM_USES_ONLY_WIN32_API
100 // UWP doesn't support tzset or environment variables for tz
101 #if U_PLATFORM_HAS_WINUWP_API == 0
102 # define U_TZSET _tzset
103 #endif
104 #elif U_PLATFORM == U_PF_OS400
105 /* not defined */
106 #elif defined(__wasi__)
107 /* not defined */
108 #else
109 # define U_TZSET tzset
110 #endif
111
112 #if defined(U_TIMEZONE) || defined(U_HAVE_TIMEZONE)
113 /* Use the predefined value. */
114 #elif U_PLATFORM == U_PF_ANDROID
115 # define U_TIMEZONE timezone
116 #elif defined(__UCLIBC__)
117 // uClibc does not have __timezone or _timezone.
118 #elif defined(_NEWLIB_VERSION)
119 # define U_TIMEZONE _timezone
120 #elif defined(__GLIBC__)
121 // glibc
122 # define U_TIMEZONE __timezone
123 #elif U_PLATFORM_IS_LINUX_BASED
124 // not defined
125 #elif U_PLATFORM_USES_ONLY_WIN32_API
126 # define U_TIMEZONE _timezone
127 #elif U_PLATFORM == U_PF_BSD && !defined(__NetBSD__)
128 /* not defined */
129 #elif U_PLATFORM == U_PF_OS400
130 /* not defined */
131 #elif U_PLATFORM == U_PF_IPHONE
132 /* not defined */
133 #elif defined(__wasi__)
134 /* not defined */
135 #else
136 # define U_TIMEZONE timezone
137 #endif
138
139 #if defined(U_TZNAME) || defined(U_HAVE_TZNAME)
140 /* Use the predefined value. */
141 #elif U_PLATFORM_USES_ONLY_WIN32_API
142 /* not usable on all windows platforms */
143 #if U_PLATFORM_HAS_WINUWP_API == 0
144 # define U_TZNAME _tzname
145 #endif
146 #elif U_PLATFORM == U_PF_OS400
147 /* not defined */
148 #elif defined(__wasi__)
149 /* not defined */
150 #else
151 # define U_TZNAME tzname
152 #endif
153
154 #ifdef U_HAVE_MMAP
155 /* Use the predefined value. */
156 #elif U_PLATFORM_USES_ONLY_WIN32_API
157 # define U_HAVE_MMAP 0
158 #else
159 # define U_HAVE_MMAP 1
160 #endif
161
162 #ifdef U_HAVE_POPEN
163 /* Use the predefined value. */
164 #elif U_PLATFORM_USES_ONLY_WIN32_API
165 # define U_HAVE_POPEN 0
166 #elif U_PLATFORM == U_PF_OS400
167 # define U_HAVE_POPEN 0
168 #else
169 # define U_HAVE_POPEN 1
170 #endif
171
172 /**
173 * \def U_HAVE_DIRENT_H
174 * Defines whether dirent.h is available.
175 * @internal
176 */
177 #ifdef U_HAVE_DIRENT_H
178 /* Use the predefined value. */
179 #elif U_PLATFORM_USES_ONLY_WIN32_API
180 # define U_HAVE_DIRENT_H 0
181 #else
182 # define U_HAVE_DIRENT_H 1
183 #endif
184
185 /** @} */
186
187 /*===========================================================================*/
188 /** @{ Programs used by ICU code */
189 /*===========================================================================*/
190
191 /**
192 * \def U_MAKE_IS_NMAKE
193 * Defines whether the "make" program is Windows nmake.
194 */
195 #ifdef U_MAKE_IS_NMAKE
196 /* Use the predefined value. */
197 #elif U_PLATFORM == U_PF_WINDOWS
198 # define U_MAKE_IS_NMAKE 1
199 #else
200 # define U_MAKE_IS_NMAKE 0
201 #endif
202
203 /** @} */
204
205 /*==========================================================================*/
206 /* Platform utilities */
207 /*==========================================================================*/
208
209 /**
210 * Platform utilities isolates the platform dependencies of the
211 * library. For each platform which this code is ported to, these
212 * functions may have to be re-implemented.
213 */
214
215 /**
216 * Floating point utility to determine if a double is Not a Number (NaN).
217 * @internal
218 */
219 U_CAPI UBool U_EXPORT2 uprv_isNaN(double d);
220 /**
221 * Floating point utility to determine if a double has an infinite value.
222 * @internal
223 */
224 U_CAPI UBool U_EXPORT2 uprv_isInfinite(double d);
225 /**
226 * Floating point utility to determine if a double has a positive infinite value.
227 * @internal
228 */
229 U_CAPI UBool U_EXPORT2 uprv_isPositiveInfinity(double d);
230 /**
231 * Floating point utility to determine if a double has a negative infinite value.
232 * @internal
233 */
234 U_CAPI UBool U_EXPORT2 uprv_isNegativeInfinity(double d);
235 /**
236 * Floating point utility that returns a Not a Number (NaN) value.
237 * @internal
238 */
239 U_CAPI double U_EXPORT2 uprv_getNaN(void);
240 /**
241 * Floating point utility that returns an infinite value.
242 * @internal
243 */
244 U_CAPI double U_EXPORT2 uprv_getInfinity(void);
245
246 /**
247 * Floating point utility to truncate a double.
248 * @internal
249 */
250 U_CAPI double U_EXPORT2 uprv_trunc(double d);
251 /**
252 * Floating point utility to calculate the floor of a double.
253 * @internal
254 */
255 U_CAPI double U_EXPORT2 uprv_floor(double d);
256 /**
257 * Floating point utility to calculate the ceiling of a double.
258 * @internal
259 */
260 U_CAPI double U_EXPORT2 uprv_ceil(double d);
261 /**
262 * Floating point utility to calculate the absolute value of a double.
263 * @internal
264 */
265 U_CAPI double U_EXPORT2 uprv_fabs(double d);
266 /**
267 * Floating point utility to calculate the fractional and integer parts of a double.
268 * @internal
269 */
270 U_CAPI double U_EXPORT2 uprv_modf(double d, double* pinteger);
271 /**
272 * Floating point utility to calculate the remainder of a double divided by another double.
273 * @internal
274 */
275 U_CAPI double U_EXPORT2 uprv_fmod(double d, double y);
276 /**
277 * Floating point utility to calculate d to the power of exponent (d^exponent).
278 * @internal
279 */
280 U_CAPI double U_EXPORT2 uprv_pow(double d, double exponent);
281 /**
282 * Floating point utility to calculate 10 to the power of exponent (10^exponent).
283 * @internal
284 */
285 U_CAPI double U_EXPORT2 uprv_pow10(int32_t exponent);
286 /**
287 * Floating point utility to calculate the maximum value of two doubles.
288 * @internal
289 */
290 U_CAPI double U_EXPORT2 uprv_fmax(double d, double y);
291 /**
292 * Floating point utility to calculate the minimum value of two doubles.
293 * @internal
294 */
295 U_CAPI double U_EXPORT2 uprv_fmin(double d, double y);
296 /**
297 * Private utility to calculate the maximum value of two integers.
298 * @internal
299 */
300 U_CAPI int32_t U_EXPORT2 uprv_max(int32_t d, int32_t y);
301 /**
302 * Private utility to calculate the minimum value of two integers.
303 * @internal
304 */
305 U_CAPI int32_t U_EXPORT2 uprv_min(int32_t d, int32_t y);
306
307 #if U_IS_BIG_ENDIAN
308 # define uprv_isNegative(number) (*((signed char *)&(number))<0)
309 #else
310 # define uprv_isNegative(number) (*((signed char *)&(number)+sizeof(number)-1)<0)
311 #endif
312
313 /**
314 * Return the largest positive number that can be represented by an integer
315 * type of arbitrary bit length.
316 * @internal
317 */
318 U_CAPI double U_EXPORT2 uprv_maxMantissa(void);
319
320 /**
321 * Floating point utility to calculate the logarithm of a double.
322 * @internal
323 */
324 U_CAPI double U_EXPORT2 uprv_log(double d);
325
326 /**
327 * Does common notion of rounding e.g. uprv_floor(x + 0.5);
328 * @param x the double number
329 * @return the rounded double
330 * @internal
331 */
332 U_CAPI double U_EXPORT2 uprv_round(double x);
333
334 /**
335 * Adds the signed integers a and b, storing the result in res.
336 * Checks for signed integer overflow.
337 * Similar to the GCC/Clang extension __builtin_add_overflow
338 *
339 * @param a The first operand.
340 * @param b The second operand.
341 * @param res a + b
342 * @return true if overflow occurred; false if no overflow occurred.
343 * @internal
344 */
345 U_CAPI UBool U_EXPORT2 uprv_add32_overflow(int32_t a, int32_t b, int32_t* res);
346
347 /**
348 * Multiplies the signed integers a and b, storing the result in res.
349 * Checks for signed integer overflow.
350 * Similar to the GCC/Clang extension __builtin_mul_overflow
351 *
352 * @param a The first multiplicand.
353 * @param b The second multiplicand.
354 * @param res a * b
355 * @return true if overflow occurred; false if no overflow occurred.
356 * @internal
357 */
358 U_CAPI UBool U_EXPORT2 uprv_mul32_overflow(int32_t a, int32_t b, int32_t* res);
359
360 #if 0
361 /**
362 * Returns the number of digits after the decimal point in a double number x.
363 *
364 * @param x the double number
365 * @return the number of digits after the decimal point in a double number x.
366 * @internal
367 */
368 /*U_CAPI int32_t U_EXPORT2 uprv_digitsAfterDecimal(double x);*/
369 #endif
370
371 #if !U_CHARSET_IS_UTF8
372 /**
373 * Please use ucnv_getDefaultName() instead.
374 * Return the default codepage for this platform and locale.
375 * This function can call setlocale() on Unix platforms. Please read the
376 * platform documentation on setlocale() before calling this function.
377 * @return the default codepage for this platform
378 * @internal
379 */
380 U_CAPI const char* U_EXPORT2 uprv_getDefaultCodepage(void);
381 #endif
382
383 /**
384 * Please use uloc_getDefault() instead.
385 * Return the default locale ID string by querying the system, or
386 * zero if one cannot be found.
387 * This function can call setlocale() on Unix platforms. Please read the
388 * platform documentation on setlocale() before calling this function.
389 * @return the default locale ID string
390 * @internal
391 */
392 U_CAPI const char* U_EXPORT2 uprv_getDefaultLocaleID(void);
393
394 /**
395 * Time zone utilities
396 *
397 * Wrappers for C runtime library functions relating to timezones.
398 * The t_tzset() function (similar to tzset) uses the current setting
399 * of the environment variable TZ to assign values to three global
400 * variables: daylight, timezone, and tzname. These variables have the
401 * following meanings, and are declared in <time.h>.
402 *
403 * daylight Nonzero if daylight-saving-time zone (DST) is specified
404 * in TZ; otherwise, 0. Default value is 1.
405 * timezone Difference in seconds between coordinated universal
406 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
407 * tzname(0) Three-letter time-zone name derived from TZ environment
408 * variable. E.g., "PST".
409 * tzname(1) Three-letter DST zone name derived from TZ environment
410 * variable. E.g., "PDT". If DST zone is omitted from TZ,
411 * tzname(1) is an empty string.
412 *
413 * Notes: For example, to set the TZ environment variable to correspond
414 * to the current time zone in Germany, you can use one of the
415 * following statements:
416 *
417 * set TZ=GST1GDT
418 * set TZ=GST+1GDT
419 *
420 * If the TZ value is not set, t_tzset() attempts to use the time zone
421 * information specified by the operating system. Under Windows NT
422 * and Windows 95, this information is specified in the Control Panel's
423 * Date/Time application.
424 * @internal
425 */
426 U_CAPI void U_EXPORT2 uprv_tzset(void);
427
428 /**
429 * Difference in seconds between coordinated universal
430 * time and local time. E.g., -28,800 for PST (GMT-8hrs)
431 * @return the difference in seconds between coordinated universal time and local time.
432 * @internal
433 */
434 U_CAPI int32_t U_EXPORT2 uprv_timezone(void);
435
436 /**
437 * tzname(0) Three-letter time-zone name derived from TZ environment
438 * variable. E.g., "PST".
439 * tzname(1) Three-letter DST zone name derived from TZ environment
440 * variable. E.g., "PDT". If DST zone is omitted from TZ,
441 * tzname(1) is an empty string.
442 * @internal
443 */
444 U_CAPI const char* U_EXPORT2 uprv_tzname(int n);
445
446 /**
447 * Reset the global tzname cache.
448 * @internal
449 */
450 U_CAPI void uprv_tzname_clear_cache(void);
451
452 /**
453 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
454 * This function is affected by 'faketime' and should be the bottleneck for all user-visible ICU time functions.
455 * @return the UTC time measured in milliseconds
456 * @internal
457 */
458 U_CAPI UDate U_EXPORT2 uprv_getUTCtime(void);
459
460 /**
461 * Get UTC (GMT) time measured in milliseconds since 0:00 on 1/1/1970.
462 * This function is not affected by 'faketime', so it should only be used by low level test functions- not by anything that
463 * exposes time to the end user.
464 * @return the UTC time measured in milliseconds
465 * @internal
466 */
467 U_CAPI UDate U_EXPORT2 uprv_getRawUTCtime(void);
468
469 /**
470 * Determine whether a pathname is absolute or not, as defined by the platform.
471 * @param path Pathname to test
472 * @return true if the path is absolute
473 * @internal (ICU 3.0)
474 */
475 U_CAPI UBool U_EXPORT2 uprv_pathIsAbsolute(const char *path);
476
477 /**
478 * Use U_MAX_PTR instead of this function.
479 * @param void pointer to test
480 * @return the largest possible pointer greater than the base
481 * @internal (ICU 3.8)
482 */
483 U_CAPI void * U_EXPORT2 uprv_maximumPtr(void *base);
484
485 /**
486 * Maximum value of a (void*) - use to indicate the limit of an 'infinite' buffer.
487 * In fact, buffer sizes must not exceed 2GB so that the difference between
488 * the buffer limit and the buffer start can be expressed in an int32_t.
489 *
490 * The definition of U_MAX_PTR must fulfill the following conditions:
491 * - return the largest possible pointer greater than base
492 * - return a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
493 * - avoid wrapping around at high addresses
494 * - make sure that the returned pointer is not farther from base than 0x7fffffff bytes
495 *
496 * @param base The beginning of a buffer to find the maximum offset from
497 * @internal
498 */
499 #ifndef U_MAX_PTR
500 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
501 /* We have 31-bit pointers. */
502 # define U_MAX_PTR(base) ((void *)0x7fffffff)
503 # elif U_PLATFORM == U_PF_OS400
504 # define U_MAX_PTR(base) uprv_maximumPtr((void *)base)
505 # elif 0
506 /*
507 * For platforms where pointers are scalar values (which is normal, but unlike i5/OS)
508 * but that do not define uintptr_t.
509 *
510 * However, this does not work on modern compilers:
511 * The C++ standard does not define pointer overflow, and allows compilers to
512 * assume that p+u>p for any pointer p and any integer u>0.
513 * Thus, modern compilers optimize away the ">" comparison.
514 * (See ICU tickets #7187 and #8096.)
515 */
516 # define U_MAX_PTR(base) \
517 ((void *)(((char *)(base)+0x7fffffffu) > (char *)(base) \
518 ? ((char *)(base)+0x7fffffffu) \
519 : (char *)-1))
520 # else
521 /* Default version. C++ standard compliant for scalar pointers. */
522 # define U_MAX_PTR(base) \
523 ((void *)(((uintptr_t)(base)+0x7fffffffu) > (uintptr_t)(base) \
524 ? ((uintptr_t)(base)+0x7fffffffu) \
525 : (uintptr_t)-1))
526 # endif
527 #endif
528
529
530 #ifdef __cplusplus
531 /**
532 * Pin a buffer capacity such that doing pointer arithmetic
533 * on the destination pointer and capacity cannot overflow.
534 *
535 * The pinned capacity must fulfill the following conditions (for positive capacities):
536 * - dest + capacity is a valid pointer according to the machine architecture (AS/400, 64-bit, etc.)
537 * - (dest + capacity) >= dest
538 * - The size (in bytes) of T[capacity] does not exceed 0x7fffffff
539 *
540 * @param dest the destination buffer pointer.
541 * @param capacity the requested buffer capacity, in units of type T.
542 * @return the pinned capacity.
543 * @internal
544 */
545 template <typename T>
pinCapacity(T * dest,int32_t capacity)546 inline int32_t pinCapacity(T *dest, int32_t capacity) {
547 if (capacity <= 0) { return capacity; }
548
549 uintptr_t destInt = (uintptr_t)dest;
550 uintptr_t maxInt;
551
552 # if U_PLATFORM == U_PF_OS390 && !defined(_LP64)
553 // We have 31-bit pointers.
554 maxInt = 0x7fffffff;
555 # elif U_PLATFORM == U_PF_OS400
556 maxInt = (uintptr_t)uprv_maximumPtr((void *)dest);
557 # else
558 maxInt = destInt + 0x7fffffffu;
559 if (maxInt < destInt) {
560 // Less than 2GB to the end of the address space.
561 // Pin to that to prevent address overflow.
562 maxInt = (uintptr_t)-1;
563 }
564 # endif
565
566 uintptr_t maxBytes = maxInt - destInt; // max. 2GB
567 int32_t maxCapacity = (int32_t)(maxBytes / sizeof(T));
568 return capacity <= maxCapacity ? capacity : maxCapacity;
569 }
570 #endif // __cplusplus
571
572 /* Dynamic Library Functions */
573
574 typedef void (UVoidFunction)(void);
575
576 #if U_ENABLE_DYLOAD
577 /**
578 * Load a library
579 * @internal (ICU 4.4)
580 */
581 U_CAPI void * U_EXPORT2 uprv_dl_open(const char *libName, UErrorCode *status);
582
583 /**
584 * Close a library
585 * @internal (ICU 4.4)
586 */
587 U_CAPI void U_EXPORT2 uprv_dl_close( void *lib, UErrorCode *status);
588
589 /**
590 * Extract a symbol from a library (function)
591 * @internal (ICU 4.8)
592 */
593 U_CAPI UVoidFunction* U_EXPORT2 uprv_dlsym_func( void *lib, const char *symbolName, UErrorCode *status);
594
595 /**
596 * Extract a symbol from a library (function)
597 * Not implemented, no clients.
598 * @internal
599 */
600 /* U_CAPI void * U_EXPORT2 uprv_dlsym_data( void *lib, const char *symbolName, UErrorCode *status); */
601
602 #endif
603
604 /**
605 * Define malloc and related functions
606 * @internal
607 */
608 #if U_PLATFORM == U_PF_OS400
609 # define uprv_default_malloc(x) _C_TS_malloc(x)
610 # define uprv_default_realloc(x,y) _C_TS_realloc(x,y)
611 # define uprv_default_free(x) _C_TS_free(x)
612 /* also _C_TS_calloc(x) */
613 #else
614 /* C defaults */
615 # define uprv_default_malloc(x) malloc(x)
616 # define uprv_default_realloc(x,y) realloc(x,y)
617 # define uprv_default_free(x) free(x)
618 #endif
619
620
621 #endif
622