1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FXCRT_FX_EXTENSION_H_
8 #define CORE_FXCRT_FX_EXTENSION_H_
9 
10 #include <time.h>
11 
12 #include <cctype>
13 #include <cmath>
14 #include <cwctype>
15 #include <memory>
16 
17 #include "core/fxcrt/fx_string.h"
18 
19 #if defined(USE_SYSTEM_ICUUC)
20 #include <unicode/uchar.h>
21 #else
22 #include "third_party/icu/source/common/unicode/uchar.h"
23 #endif
24 
25 #define FX_INVALID_OFFSET static_cast<uint32_t>(-1)
26 
27 #ifdef PDF_ENABLE_XFA
28 #define FX_IsOdd(a) ((a)&1)
29 #endif  // PDF_ENABLE_XFA
30 
31 float FXSYS_wcstof(const wchar_t* pwsStr, int32_t iLength, int32_t* pUsedLen);
32 wchar_t* FXSYS_wcsncpy(wchar_t* dstStr, const wchar_t* srcStr, size_t count);
33 int32_t FXSYS_wcsnicmp(const wchar_t* s1, const wchar_t* s2, size_t count);
34 
FXSYS_iswlower(int32_t c)35 inline bool FXSYS_iswlower(int32_t c) {
36   return u_islower(c);
37 }
38 
FXSYS_iswupper(int32_t c)39 inline bool FXSYS_iswupper(int32_t c) {
40   return u_isupper(c);
41 }
42 
FXSYS_towlower(wchar_t c)43 inline int32_t FXSYS_towlower(wchar_t c) {
44   return u_tolower(c);
45 }
46 
FXSYS_towupper(wchar_t c)47 inline int32_t FXSYS_towupper(wchar_t c) {
48   return u_toupper(c);
49 }
50 
FXSYS_ToUpperASCII(char c)51 inline char FXSYS_ToUpperASCII(char c) {
52   return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
53 }
54 
FXSYS_iswalpha(wchar_t c)55 inline bool FXSYS_iswalpha(wchar_t c) {
56   return u_isalpha(c);
57 }
58 
FXSYS_iswalnum(wchar_t c)59 inline bool FXSYS_iswalnum(wchar_t c) {
60   return u_isalnum(c);
61 }
62 
FXSYS_iswspace(wchar_t c)63 inline bool FXSYS_iswspace(wchar_t c) {
64   return u_isspace(c);
65 }
66 
FXSYS_IsOctalDigit(char c)67 inline bool FXSYS_IsOctalDigit(char c) {
68   return c >= '0' && c <= '7';
69 }
70 
FXSYS_IsHexDigit(char c)71 inline bool FXSYS_IsHexDigit(char c) {
72   return !((c & 0x80) || !std::isxdigit(c));
73 }
74 
FXSYS_IsWideHexDigit(wchar_t c)75 inline bool FXSYS_IsWideHexDigit(wchar_t c) {
76   return !((c & 0xFFFFFF80) || !std::isxdigit(c));
77 }
78 
FXSYS_HexCharToInt(char c)79 inline int FXSYS_HexCharToInt(char c) {
80   if (!FXSYS_IsHexDigit(c))
81     return 0;
82   char upchar = FXSYS_ToUpperASCII(c);
83   return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
84 }
85 
FXSYS_WideHexCharToInt(wchar_t c)86 inline int FXSYS_WideHexCharToInt(wchar_t c) {
87   if (!FXSYS_IsWideHexDigit(c))
88     return 0;
89   char upchar = std::toupper(static_cast<char>(c));
90   return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
91 }
92 
FXSYS_IsDecimalDigit(char c)93 inline bool FXSYS_IsDecimalDigit(char c) {
94   return !((c & 0x80) || !std::isdigit(c));
95 }
96 
FXSYS_IsDecimalDigit(wchar_t c)97 inline bool FXSYS_IsDecimalDigit(wchar_t c) {
98   return !((c & 0xFFFFFF80) || !std::iswdigit(c));
99 }
100 
FXSYS_DecimalCharToInt(char c)101 inline int FXSYS_DecimalCharToInt(char c) {
102   return FXSYS_IsDecimalDigit(c) ? c - '0' : 0;
103 }
104 
FXSYS_DecimalCharToInt(wchar_t c)105 inline int FXSYS_DecimalCharToInt(wchar_t c) {
106   return FXSYS_IsDecimalDigit(c) ? c - L'0' : 0;
107 }
108 
109 void FXSYS_IntToTwoHexChars(uint8_t n, char* buf);
110 void FXSYS_IntToFourHexChars(uint16_t n, char* buf);
111 
112 size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf);
113 
114 // Strict order over floating types where NaNs may be present.
115 template <typename T>
FXSYS_SafeEQ(const T & lhs,const T & rhs)116 bool FXSYS_SafeEQ(const T& lhs, const T& rhs) {
117   return (std::isnan(lhs) && std::isnan(rhs)) ||
118          (!std::isnan(lhs) && !std::isnan(rhs) && lhs == rhs);
119 }
120 
121 template <typename T>
FXSYS_SafeLT(const T & lhs,const T & rhs)122 bool FXSYS_SafeLT(const T& lhs, const T& rhs) {
123   if (std::isnan(lhs) && std::isnan(rhs))
124     return false;
125   if (std::isnan(lhs) || std::isnan(rhs))
126     return std::isnan(lhs) < std::isnan(rhs);
127   return lhs < rhs;
128 }
129 
130 // Override time/localtime functions for test consistency.
131 void FXSYS_SetTimeFunction(time_t (*func)());
132 void FXSYS_SetLocaltimeFunction(struct tm* (*func)(const time_t*));
133 
134 // Replacements for time/localtime that respect overrides.
135 time_t FXSYS_time(time_t* tloc);
136 struct tm* FXSYS_localtime(const time_t* tp);
137 
138 #endif  // CORE_FXCRT_FX_EXTENSION_H_
139