blob: fbb86fea4562bfbed6004007a8b669a59ceeb044 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhang95e854f2015-06-13 00:58:06 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclaircfb19442017-04-20 13:13:04 -04007#ifndef CORE_FXCRT_FX_EXTENSION_H_
8#define CORE_FXCRT_FX_EXTENSION_H_
Tom Sepez0570b8a2015-04-06 13:49:25 -07009
Dan Sinclair10cfea12015-11-16 13:09:00 -050010#include <cctype>
11#include <cwctype>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -050012#include <memory>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
Dan Sinclairbcd1e702017-08-31 13:19:18 -040014#include "core/fxcrt/fx_string.h"
Tom Sepez7386e292015-11-20 09:35:06 -080015
Wei Li89409932016-03-28 10:33:33 -070016#define FX_INVALID_OFFSET static_cast<uint32_t>(-1)
17
Dan Sinclairbcd1e702017-08-31 13:19:18 -040018#ifdef PDF_ENABLE_XFA
19#define FX_IsOdd(a) ((a)&1)
20#endif // PDF_ENABLE_XFA
21
Dan Sinclair05df0752017-03-14 14:43:42 -040022float FXSYS_wcstof(const wchar_t* pwsStr,
23 int32_t iLength = -1,
24 int32_t* pUsedLen = nullptr);
Dan Sinclair812e96c2017-03-13 16:43:37 -040025wchar_t* FXSYS_wcsncpy(wchar_t* dstStr, const wchar_t* srcStr, size_t count);
26int32_t FXSYS_wcsnicmp(const wchar_t* s1, const wchar_t* s2, size_t count);
Tom Sepez0570b8a2015-04-06 13:49:25 -070027
Wei Li614d20a2016-03-15 13:55:12 -070028inline bool FXSYS_islower(int32_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 return ch >= 'a' && ch <= 'z';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030}
Dan Sinclair72c6b182017-04-03 13:39:26 -040031
Wei Li614d20a2016-03-15 13:55:12 -070032inline bool FXSYS_isupper(int32_t ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 return ch >= 'A' && ch <= 'Z';
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034}
Dan Sinclair72c6b182017-04-03 13:39:26 -040035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036inline int32_t FXSYS_tolower(int32_t ch) {
37 return ch < 'A' || ch > 'Z' ? ch : (ch + 0x20);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038}
Dan Sinclair72c6b182017-04-03 13:39:26 -040039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040inline int32_t FXSYS_toupper(int32_t ch) {
41 return ch < 'a' || ch > 'z' ? ch : (ch - 0x20);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042}
Dan Sinclair72c6b182017-04-03 13:39:26 -040043
Wei Li614d20a2016-03-15 13:55:12 -070044inline bool FXSYS_iswalpha(wchar_t wch) {
Lei Zhangef002c82017-04-24 16:28:07 -070045 return FXSYS_isupper(wch) || FXSYS_islower(wch);
Wei Li60eac0f2015-12-17 18:16:23 -080046}
Dan Sinclair72c6b182017-04-03 13:39:26 -040047
Wei Li614d20a2016-03-15 13:55:12 -070048inline bool FXSYS_iswalnum(wchar_t wch) {
Lei Zhange247ec42017-04-20 21:41:36 -070049 return FXSYS_iswalpha(wch) || std::iswdigit(wch);
Wei Li60eac0f2015-12-17 18:16:23 -080050}
Dan Sinclair72c6b182017-04-03 13:39:26 -040051
Dan Sinclair812e96c2017-03-13 16:43:37 -040052inline bool FXSYS_iswspace(wchar_t c) {
dsinclairce565572016-06-23 14:00:32 -070053 return (c == 0x20) || (c == 0x0d) || (c == 0x0a) || (c == 0x09);
54}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nicolas Penaecd36462017-04-18 13:12:41 -040056inline bool FXSYS_isHexDigit(const char c) {
57 return !((c & 0x80) || !std::isxdigit(c));
58}
59
Lei Zhange8c1d412017-05-04 12:13:55 -070060inline int FXSYS_HexCharToInt(const char c) {
Nicolas Penaecd36462017-04-18 13:12:41 -040061 if (!FXSYS_isHexDigit(c))
Dan Sinclair10cfea12015-11-16 13:09:00 -050062 return 0;
63 char upchar = std::toupper(c);
64 return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
65}
66
Dan Sinclair812e96c2017-03-13 16:43:37 -040067inline bool FXSYS_isDecimalDigit(const char c) {
Nicolas Penaecd36462017-04-18 13:12:41 -040068 return !((c & 0x80) || !std::isdigit(c));
Dan Sinclair1c915372016-03-03 17:12:58 -050069}
70
Dan Sinclair812e96c2017-03-13 16:43:37 -040071inline bool FXSYS_isDecimalDigit(const wchar_t c) {
Wei Li97da9762016-03-11 17:00:48 -080072 return !!std::iswdigit(c);
Dan Sinclair1c915372016-03-03 17:12:58 -050073}
74
Lei Zhange8c1d412017-05-04 12:13:55 -070075inline int FXSYS_DecimalCharToInt(const char c) {
Nicolas Penaecd36462017-04-18 13:12:41 -040076 return FXSYS_isDecimalDigit(c) ? c - '0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -050077}
78
Lei Zhange8c1d412017-05-04 12:13:55 -070079inline int FXSYS_DecimalCharToInt(const wchar_t c) {
Wei Li97da9762016-03-11 17:00:48 -080080 return std::iswdigit(c) ? c - L'0' : 0;
Dan Sinclair10cfea12015-11-16 13:09:00 -050081}
82
Nicolas Pena54b91662017-05-05 16:49:30 -040083void FXSYS_IntToTwoHexChars(uint8_t c, char* buf);
84
85void FXSYS_IntToFourHexChars(uint16_t c, char* buf);
86
87size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf);
88
Dan Sinclair05df0752017-03-14 14:43:42 -040089float FXSYS_FractionalScale(size_t scale_factor, int value);
dsinclairce565572016-06-23 14:00:32 -070090int FXSYS_FractionalScaleCount();
91
tsepezb5e8f142016-03-25 15:18:35 -070092void* FX_Random_MT_Start(uint32_t dwSeed);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void FX_Random_MT_Close(void* pContext);
tsepezd9871432016-09-15 14:01:31 -070094uint32_t FX_Random_MT_Generate(void* pContext);
tsepezb5e8f142016-03-25 15:18:35 -070095void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount);
tsepezb5e8f142016-03-25 15:18:35 -070096void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount);
Bo Xufdc00a72014-10-28 23:03:33 -070097
Tom Sepez5c4c1932015-11-25 12:15:38 -080098#ifdef PDF_ENABLE_XFA
tsepez3128d1c2017-01-10 06:03:26 -080099struct FX_GUID {
tsepezb5e8f142016-03-25 15:18:35 -0700100 uint32_t data1;
Tom Sepez62a70f92016-03-21 15:00:20 -0700101 uint16_t data2;
102 uint16_t data3;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 uint8_t data4[8];
tsepez3128d1c2017-01-10 06:03:26 -0800104};
105void FX_GUID_CreateV4(FX_GUID* pGUID);
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700106CFX_ByteString FX_GUID_ToString(const FX_GUID* pGUID, bool bSeparator = true);
Tom Sepeza2c42ce2015-11-25 15:52:28 -0800107#endif // PDF_ENABLE_XFA
Dan Sinclair10cfea12015-11-16 13:09:00 -0500108
Dan Sinclairbcd1e702017-08-31 13:19:18 -0400109uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits);
110
Dan Sinclaircfb19442017-04-20 13:13:04 -0400111#endif // CORE_FXCRT_FX_EXTENSION_H_