John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/util.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Lei Zhang | 6b37a5f | 2015-12-25 00:20:59 -0800 | [diff] [blame] | 9 | #include <time.h> |
| 10 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 15 | #include "core/fxcrt/fx_ext.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 16 | #include "fpdfsdk/javascript/JS_Define.h" |
| 17 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 18 | #include "fpdfsdk/javascript/JS_Object.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 19 | #include "fpdfsdk/javascript/JS_Value.h" |
| 20 | #include "fpdfsdk/javascript/PublicMethods.h" |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 21 | #include "fpdfsdk/javascript/cjs_event_context.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 22 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 23 | #include "fpdfsdk/javascript/resource.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 25 | #if _FX_OS_ == _FX_ANDROID_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #endif |
| 28 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 29 | JSConstSpec CJS_Util::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 30 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 31 | JSPropertySpec CJS_Util::PropertySpecs[] = {{0, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 33 | JSMethodSpec CJS_Util::MethodSpecs[] = { |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame^] | 34 | {"printd", printd_static}, {"printf", printf_static}, |
| 35 | {"printx", printx_static}, {"scand", scand_static}, |
| 36 | {"byteToChar", byteToChar_static}, {0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 37 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | IMPLEMENT_JS_CLASS(CJS_Util, util) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 39 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 40 | #define UTIL_INT 0 |
| 41 | #define UTIL_DOUBLE 1 |
| 42 | #define UTIL_STRING 2 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 43 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | // Map PDF-style directives to equivalent wcsftime directives. Not |
| 47 | // all have direct equivalents, though. |
| 48 | struct TbConvert { |
| 49 | const FX_WCHAR* lpszJSMark; |
| 50 | const FX_WCHAR* lpszCppMark; |
| 51 | }; |
| 52 | |
| 53 | // Map PDF-style directives lacking direct wcsftime directives to |
| 54 | // the value with which they will be replaced. |
| 55 | struct TbConvertAdditional { |
| 56 | const FX_WCHAR* lpszJSMark; |
| 57 | int iValue; |
| 58 | }; |
| 59 | |
| 60 | const TbConvert TbConvertTable[] = { |
| 61 | {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"}, |
| 62 | {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"}, |
| 63 | {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"}, |
| 64 | {L"TT", L"%p"}, |
| 65 | #if defined(_WIN32) |
| 66 | {L"tt", L"%p"}, {L"h", L"%#I"}, |
| 67 | #else |
| 68 | {L"tt", L"%P"}, {L"h", L"%l"}, |
| 69 | #endif |
| 70 | }; |
| 71 | |
| 72 | int ParseDataType(std::wstring* sFormat) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 73 | bool bPercent = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | for (size_t i = 0; i < sFormat->length(); ++i) { |
| 75 | wchar_t c = (*sFormat)[i]; |
| 76 | if (c == L'%') { |
| 77 | bPercent = true; |
| 78 | continue; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 79 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | if (bPercent) { |
| 82 | if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || |
| 83 | c == L'u' || c == L'x' || c == L'X') { |
| 84 | return UTIL_INT; |
| 85 | } |
| 86 | if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') { |
| 87 | return UTIL_DOUBLE; |
| 88 | } |
| 89 | if (c == L's' || c == L'S') { |
| 90 | // Map s to S since we always deal internally |
| 91 | // with wchar_t strings. |
| 92 | (*sFormat)[i] = L'S'; |
| 93 | return UTIL_STRING; |
| 94 | } |
| 95 | if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 96 | FXSYS_iswdigit(c)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | continue; |
| 98 | } |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 106 | } // namespace |
| 107 | |
| 108 | util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
| 109 | |
| 110 | util::~util() {} |
| 111 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 112 | bool util::printf(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 113 | const std::vector<CJS_Value>& params, |
| 114 | CJS_Value& vRet, |
| 115 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | int iSize = params.size(); |
| 117 | if (iSize < 1) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 118 | return false; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 119 | std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | std::vector<std::wstring> c_strConvers; |
| 121 | int iOffset = 0; |
| 122 | int iOffend = 0; |
| 123 | c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
| 124 | while (iOffset != -1) { |
| 125 | iOffend = c_ConvChar.find(L"%", iOffset + 1); |
| 126 | std::wstring strSub; |
| 127 | if (iOffend == -1) |
| 128 | strSub = c_ConvChar.substr(iOffset); |
| 129 | else |
| 130 | strSub = c_ConvChar.substr(iOffset, iOffend - iOffset); |
| 131 | c_strConvers.push_back(strSub); |
| 132 | iOffset = iOffend; |
| 133 | } |
| 134 | |
| 135 | std::wstring c_strResult; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | std::wstring c_strFormat; |
| 137 | for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) { |
| 138 | c_strFormat = c_strConvers[iIndex]; |
| 139 | if (iIndex == 0) { |
| 140 | c_strResult = c_strFormat; |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | CFX_WideString strSegment; |
| 145 | if (iIndex >= iSize) { |
| 146 | c_strResult += c_strFormat; |
| 147 | continue; |
| 148 | } |
| 149 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 150 | switch (ParseDataType(&c_strFormat)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 151 | case UTIL_INT: |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 152 | strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | break; |
| 154 | case UTIL_DOUBLE: |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 155 | strSegment.Format(c_strFormat.c_str(), |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 156 | params[iIndex].ToDouble(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | break; |
| 158 | case UTIL_STRING: |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 159 | strSegment.Format(c_strFormat.c_str(), |
| 160 | params[iIndex].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | strSegment.Format(L"%S", c_strFormat.c_str()); |
| 164 | break; |
| 165 | } |
| 166 | c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); |
| 167 | } |
| 168 | |
| 169 | c_strResult.erase(c_strResult.begin()); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 170 | vRet = CJS_Value(pRuntime, c_strResult.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 171 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 174 | bool util::printd(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 175 | const std::vector<CJS_Value>& params, |
| 176 | CJS_Value& vRet, |
| 177 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | int iSize = params.size(); |
| 179 | if (iSize < 2) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 180 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 182 | CJS_Value p1 = params[0]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | CJS_Value p2 = params[1]; |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 184 | CJS_Date jsDate; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 185 | if (!p2.ConvertToDate(pRuntime, jsDate)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 186 | sError = JSGetStringFromID(IDS_STRING_JSPRINT1); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 187 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 190 | if (!jsDate.IsValidDate(pRuntime)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 191 | sError = JSGetStringFromID(IDS_STRING_JSPRINT2); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 192 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 195 | if (p1.GetType() == CJS_Value::VT_number) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | CFX_WideString swResult; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 197 | switch (p1.ToInt(pRuntime)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | case 0: |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 199 | swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), |
| 200 | jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), |
| 201 | jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), |
| 202 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | break; |
| 204 | case 1: |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 205 | swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 206 | jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 207 | jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 208 | jsDate.GetMinutes(pRuntime), |
| 209 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | break; |
| 211 | case 2: |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 212 | swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 213 | jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 214 | jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 215 | jsDate.GetMinutes(pRuntime), |
| 216 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | break; |
| 218 | default: |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 219 | sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 220 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | } |
| 222 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 223 | vRet = CJS_Value(pRuntime, swResult.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 224 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | } |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 226 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 227 | if (p1.GetType() == CJS_Value::VT_string) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 228 | if (iSize > 2 && params[2].ToBool(pRuntime)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 229 | sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 230 | return false; // currently, it doesn't support XFAPicture. |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 231 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 232 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 233 | // Convert PDF-style format specifiers to wcsftime specifiers. Remove any |
| 234 | // pre-existing %-directives before inserting our own. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 235 | std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str(); |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 236 | cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), |
| 237 | cFormat.end()); |
| 238 | |
| 239 | for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | int iStart = 0; |
| 241 | int iEnd; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 242 | while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != |
| 243 | -1) { |
| 244 | cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), |
| 245 | TbConvertTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | iStart = iEnd; |
| 247 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 248 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 249 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 250 | int iYear = jsDate.GetYear(pRuntime); |
| 251 | int iMonth = jsDate.GetMonth(pRuntime); |
| 252 | int iDay = jsDate.GetDay(pRuntime); |
| 253 | int iHour = jsDate.GetHours(pRuntime); |
| 254 | int iMin = jsDate.GetMinutes(pRuntime); |
| 255 | int iSec = jsDate.GetSeconds(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 256 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 257 | TbConvertAdditional cTableAd[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | {L"m", iMonth + 1}, {L"d", iDay}, |
| 259 | {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, |
| 260 | {L"M", iMin}, {L"s", iSec}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 261 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 263 | for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { |
| 264 | wchar_t tszValue[16]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 266 | sValue.Format(L"%d", cTableAd[i].iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), |
| 268 | (sValue.GetLength() + 1) * sizeof(wchar_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 269 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | int iStart = 0; |
| 271 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 272 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | if (iEnd > 0) { |
| 274 | if (cFormat[iEnd - 1] == L'%') { |
| 275 | iStart = iEnd + 1; |
| 276 | continue; |
| 277 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 278 | } |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 279 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 280 | iStart = iEnd; |
| 281 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 282 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 283 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 284 | struct tm time = {}; |
| 285 | time.tm_year = iYear - 1900; |
| 286 | time.tm_mon = iMonth; |
| 287 | time.tm_mday = iDay; |
| 288 | time.tm_hour = iHour; |
| 289 | time.tm_min = iMin; |
| 290 | time.tm_sec = iSec; |
| 291 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 292 | wchar_t buf[64] = {}; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 293 | wcsftime(buf, 64, cFormat.c_str(), &time); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 294 | cFormat = buf; |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 295 | vRet = CJS_Value(pRuntime, cFormat.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 296 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 297 | } |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 298 | |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 299 | sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 300 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 303 | bool util::printx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 304 | const std::vector<CJS_Value>& params, |
| 305 | CJS_Value& vRet, |
| 306 | CFX_WideString& sError) { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 307 | if (params.size() < 2) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 308 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 309 | return false; |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 310 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 311 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 312 | vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), |
| 313 | params[1].ToCFXWideString(pRuntime)) |
| 314 | .c_str()); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 315 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 316 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | } |
| 318 | |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 319 | enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
| 320 | |
| 321 | static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
| 322 | if (eMode == kLowerCase && input >= 'A' && input <= 'Z') |
| 323 | return input | 0x20; |
| 324 | if (eMode == kUpperCase && input >= 'a' && input <= 'z') |
| 325 | return input & ~0x20; |
| 326 | return input; |
| 327 | } |
| 328 | |
| 329 | CFX_WideString util::printx(const CFX_WideString& wsFormat, |
| 330 | const CFX_WideString& wsSource) { |
| 331 | CFX_WideString wsResult; |
| 332 | FX_STRSIZE iSourceIdx = 0; |
| 333 | FX_STRSIZE iFormatIdx = 0; |
| 334 | CaseMode eCaseMode = kPreserveCase; |
| 335 | bool bEscaped = false; |
| 336 | while (iFormatIdx < wsFormat.GetLength()) { |
| 337 | if (bEscaped) { |
| 338 | bEscaped = false; |
| 339 | wsResult += wsFormat[iFormatIdx]; |
| 340 | ++iFormatIdx; |
| 341 | continue; |
| 342 | } |
| 343 | switch (wsFormat[iFormatIdx]) { |
| 344 | case '\\': { |
| 345 | bEscaped = true; |
| 346 | ++iFormatIdx; |
| 347 | } break; |
| 348 | case '<': { |
| 349 | eCaseMode = kLowerCase; |
| 350 | ++iFormatIdx; |
| 351 | } break; |
| 352 | case '>': { |
| 353 | eCaseMode = kUpperCase; |
| 354 | ++iFormatIdx; |
| 355 | } break; |
| 356 | case '=': { |
| 357 | eCaseMode = kPreserveCase; |
| 358 | ++iFormatIdx; |
| 359 | } break; |
| 360 | case '?': { |
| 361 | if (iSourceIdx < wsSource.GetLength()) { |
| 362 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 363 | ++iSourceIdx; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 364 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 365 | ++iFormatIdx; |
| 366 | } break; |
| 367 | case 'X': { |
| 368 | if (iSourceIdx < wsSource.GetLength()) { |
| 369 | if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') || |
| 370 | (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') || |
| 371 | (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) { |
| 372 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 373 | ++iFormatIdx; |
| 374 | } |
| 375 | ++iSourceIdx; |
| 376 | } else { |
| 377 | ++iFormatIdx; |
| 378 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | } break; |
| 380 | case 'A': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 381 | if (iSourceIdx < wsSource.GetLength()) { |
| 382 | if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') || |
| 383 | (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) { |
| 384 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 385 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 387 | ++iSourceIdx; |
| 388 | } else { |
| 389 | ++iFormatIdx; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 390 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | } break; |
| 392 | case '9': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 393 | if (iSourceIdx < wsSource.GetLength()) { |
| 394 | if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') { |
| 395 | wsResult += wsSource[iSourceIdx]; |
| 396 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 397 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 398 | ++iSourceIdx; |
| 399 | } else { |
| 400 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 402 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | case '*': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 404 | if (iSourceIdx < wsSource.GetLength()) { |
| 405 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 406 | ++iSourceIdx; |
| 407 | } else { |
| 408 | ++iFormatIdx; |
| 409 | } |
| 410 | } break; |
| 411 | default: { |
| 412 | wsResult += wsFormat[iFormatIdx]; |
| 413 | ++iFormatIdx; |
| 414 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 417 | return wsResult; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 420 | bool util::scand(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 421 | const std::vector<CJS_Value>& params, |
| 422 | CJS_Value& vRet, |
| 423 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | int iSize = params.size(); |
| 425 | if (iSize < 2) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 426 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 428 | CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
| 429 | CFX_WideString sDate = params[1].ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | double dDate = JS_GetDateTime(); |
| 431 | if (sDate.GetLength() > 0) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 432 | dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (!JS_PortIsNan(dDate)) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 436 | vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | } else { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 438 | vRet.SetNull(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | } |
| 440 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 441 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 444 | bool util::byteToChar(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 445 | const std::vector<CJS_Value>& params, |
| 446 | CJS_Value& vRet, |
| 447 | CFX_WideString& sError) { |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 448 | if (params.size() < 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 449 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 450 | return false; |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 451 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 452 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 453 | int arg = params[0].ToInt(pRuntime); |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 454 | if (arg < 0 || arg > 255) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 455 | sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 456 | return false; |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 457 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 458 | |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 459 | CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 460 | vRet = CJS_Value(pRuntime, wStr.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 461 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 462 | } |