blob: dc34119c38f91752577682ce143c315d1cb6d487 [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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/util.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang6b37a5f2015-12-25 00:20:59 -08009#include <time.h>
10
tsepez86a61dc2016-03-25 10:00:11 -070011#include <algorithm>
Lei Zhange247ec42017-04-20 21:41:36 -070012#include <cwctype>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050013#include <string>
14#include <vector>
15
Dan Sinclaircfb19442017-04-20 13:13:04 -040016#include "core/fxcrt/fx_extension.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040017#include "fpdfsdk/javascript/JS_Define.h"
18#include "fpdfsdk/javascript/JS_EventHandler.h"
19#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040020#include "fpdfsdk/javascript/JS_Value.h"
21#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080022#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070023#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040024#include "fpdfsdk/javascript/resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027#include <ctype.h>
28#endif
29
Tom Sepez04557b82017-02-16 09:43:10 -080030JSConstSpec CJS_Util::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Tom Sepez04557b82017-02-16 09:43:10 -080032JSPropertySpec CJS_Util::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Tom Sepez04557b82017-02-16 09:43:10 -080034JSMethodSpec CJS_Util::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080035 {"printd", printd_static}, {"printf", printf_static},
36 {"printx", printx_static}, {"scand", scand_static},
37 {"byteToChar", byteToChar_static}, {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041#define UTIL_INT 0
42#define UTIL_DOUBLE 1
43#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
tsepez86a61dc2016-03-25 10:00:11 -070045namespace {
46
47// Map PDF-style directives to equivalent wcsftime directives. Not
48// all have direct equivalents, though.
49struct TbConvert {
Dan Sinclair812e96c2017-03-13 16:43:37 -040050 const wchar_t* lpszJSMark;
51 const wchar_t* lpszCppMark;
tsepez86a61dc2016-03-25 10:00:11 -070052};
53
54// Map PDF-style directives lacking direct wcsftime directives to
55// the value with which they will be replaced.
56struct TbConvertAdditional {
Dan Sinclair812e96c2017-03-13 16:43:37 -040057 const wchar_t* lpszJSMark;
tsepez86a61dc2016-03-25 10:00:11 -070058 int iValue;
59};
60
61const TbConvert TbConvertTable[] = {
62 {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"},
63 {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"},
64 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"},
65 {L"TT", L"%p"},
66#if defined(_WIN32)
67 {L"tt", L"%p"}, {L"h", L"%#I"},
68#else
69 {L"tt", L"%P"}, {L"h", L"%l"},
70#endif
71};
72
73int ParseDataType(std::wstring* sFormat) {
tsepez4cf55152016-11-02 14:37:54 -070074 bool bPercent = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 for (size_t i = 0; i < sFormat->length(); ++i) {
76 wchar_t c = (*sFormat)[i];
77 if (c == L'%') {
78 bPercent = true;
79 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070080 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 if (bPercent) {
83 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
84 c == L'u' || c == L'x' || c == L'X') {
85 return UTIL_INT;
86 }
87 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
88 return UTIL_DOUBLE;
89 }
90 if (c == L's' || c == L'S') {
91 // Map s to S since we always deal internally
92 // with wchar_t strings.
93 (*sFormat)[i] = L'S';
94 return UTIL_STRING;
95 }
96 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
Lei Zhange247ec42017-04-20 21:41:36 -070097 std::iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 continue;
99 }
100 break;
101 }
102 }
103
104 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
tsepez86a61dc2016-03-25 10:00:11 -0700107} // namespace
108
109util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
110
111util::~util() {}
112
Tom Sepezb1670b52017-02-16 17:01:00 -0800113bool util::printf(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700114 const std::vector<CJS_Value>& params,
115 CJS_Value& vRet,
116 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700117 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 if (iSize < 1)
tsepez4cf55152016-11-02 14:37:54 -0700119 return false;
Lei Zhang574b5742017-03-30 12:41:55 -0700120
tsepezb4694242016-08-15 16:44:55 -0700121 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 std::vector<std::wstring> c_strConvers;
123 int iOffset = 0;
124 int iOffend = 0;
125 c_ConvChar.insert(c_ConvChar.begin(), L'S');
126 while (iOffset != -1) {
127 iOffend = c_ConvChar.find(L"%", iOffset + 1);
128 std::wstring strSub;
129 if (iOffend == -1)
130 strSub = c_ConvChar.substr(iOffset);
131 else
132 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
133 c_strConvers.push_back(strSub);
134 iOffset = iOffend;
135 }
136
137 std::wstring c_strResult;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 std::wstring c_strFormat;
Lei Zhang574b5742017-03-30 12:41:55 -0700139 for (size_t iIndex = 0; iIndex < c_strConvers.size(); ++iIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 c_strFormat = c_strConvers[iIndex];
141 if (iIndex == 0) {
142 c_strResult = c_strFormat;
143 continue;
144 }
145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 if (iIndex >= iSize) {
147 c_strResult += c_strFormat;
148 continue;
149 }
150
Lei Zhang574b5742017-03-30 12:41:55 -0700151 CFX_WideString strSegment;
tsepez86a61dc2016-03-25 10:00:11 -0700152 switch (ParseDataType(&c_strFormat)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 case UTIL_INT:
tsepezb4694242016-08-15 16:44:55 -0700154 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 break;
156 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700157 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700158 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 break;
160 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700161 strSegment.Format(c_strFormat.c_str(),
162 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 break;
164 default:
165 strSegment.Format(L"%S", c_strFormat.c_str());
166 break;
167 }
168 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
169 }
170
171 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700172 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700173 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
175
Tom Sepezb1670b52017-02-16 17:01:00 -0800176bool util::printd(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700177 const std::vector<CJS_Value>& params,
178 CJS_Value& vRet,
179 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700180 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700182 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183
Lei Zhang574b5742017-03-30 12:41:55 -0700184 const CJS_Value& p1 = params[0];
185 const CJS_Value& p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700186 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700187 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700188 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700189 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
191
tsepezb4694242016-08-15 16:44:55 -0700192 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700193 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700194 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 }
196
Tom Sepez39bfe122015-09-17 15:25:23 -0700197 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 CFX_WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700199 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 case 0:
tsepezb4694242016-08-15 16:44:55 -0700201 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
202 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
203 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
204 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 break;
206 case 1:
tsepezf3c88322016-08-09 07:30:38 -0700207 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700208 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
209 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
210 jsDate.GetMinutes(pRuntime),
211 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 break;
213 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700214 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700215 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
216 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
217 jsDate.GetMinutes(pRuntime),
218 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 break;
220 default:
tsepezcd5dc852016-09-08 11:23:24 -0700221 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700222 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 }
224
tsepezf3dc8c62016-08-10 06:29:29 -0700225 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700226 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 }
tsepez86a61dc2016-03-25 10:00:11 -0700228
Tom Sepez39bfe122015-09-17 15:25:23 -0700229 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700230 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700231 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700232 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700233 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
tsepez86a61dc2016-03-25 10:00:11 -0700235 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
236 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700237 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700238 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
239 cFormat.end());
240
241 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 int iStart = 0;
243 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700244 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
245 -1) {
246 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
247 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 iStart = iEnd;
249 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700250 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251
tsepezb4694242016-08-15 16:44:55 -0700252 int iYear = jsDate.GetYear(pRuntime);
253 int iMonth = jsDate.GetMonth(pRuntime);
254 int iDay = jsDate.GetDay(pRuntime);
255 int iHour = jsDate.GetHours(pRuntime);
256 int iMin = jsDate.GetMinutes(pRuntime);
257 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
tsepez86a61dc2016-03-25 10:00:11 -0700259 TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 {L"m", iMonth + 1}, {L"d", iDay},
261 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
262 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700263 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264
tsepez86a61dc2016-03-25 10:00:11 -0700265 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
266 wchar_t tszValue[16];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700268 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
270 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 int iStart = 0;
273 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700274 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 if (iEnd > 0) {
276 if (cFormat[iEnd - 1] == L'%') {
277 iStart = iEnd + 1;
278 continue;
279 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700280 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700281 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 iStart = iEnd;
283 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700284 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
tsepez86a61dc2016-03-25 10:00:11 -0700286 struct tm time = {};
287 time.tm_year = iYear - 1900;
288 time.tm_mon = iMonth;
289 time.tm_mday = iDay;
290 time.tm_hour = iHour;
291 time.tm_min = iMin;
292 time.tm_sec = iSec;
293
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700294 wchar_t buf[64] = {};
tsepez86a61dc2016-03-25 10:00:11 -0700295 wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700296 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700297 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700298 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 }
tsepez86a61dc2016-03-25 10:00:11 -0700300
tsepezcd5dc852016-09-08 11:23:24 -0700301 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700302 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Tom Sepezb1670b52017-02-16 17:01:00 -0800305bool util::printx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700306 const std::vector<CJS_Value>& params,
307 CJS_Value& vRet,
308 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700309 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700310 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700311 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700312 }
tsepezf3dc8c62016-08-10 06:29:29 -0700313
tsepezb4694242016-08-15 16:44:55 -0700314 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
315 params[1].ToCFXWideString(pRuntime))
316 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700317
tsepez4cf55152016-11-02 14:37:54 -0700318 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319}
320
tsepez4f1f41f2016-03-28 14:13:16 -0700321enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
322
Dan Sinclair812e96c2017-03-13 16:43:37 -0400323static wchar_t TranslateCase(wchar_t input, CaseMode eMode) {
tsepez4f1f41f2016-03-28 14:13:16 -0700324 if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
325 return input | 0x20;
326 if (eMode == kUpperCase && input >= 'a' && input <= 'z')
327 return input & ~0x20;
328 return input;
329}
330
331CFX_WideString util::printx(const CFX_WideString& wsFormat,
332 const CFX_WideString& wsSource) {
333 CFX_WideString wsResult;
334 FX_STRSIZE iSourceIdx = 0;
335 FX_STRSIZE iFormatIdx = 0;
336 CaseMode eCaseMode = kPreserveCase;
337 bool bEscaped = false;
338 while (iFormatIdx < wsFormat.GetLength()) {
339 if (bEscaped) {
340 bEscaped = false;
341 wsResult += wsFormat[iFormatIdx];
342 ++iFormatIdx;
343 continue;
344 }
345 switch (wsFormat[iFormatIdx]) {
346 case '\\': {
347 bEscaped = true;
348 ++iFormatIdx;
349 } break;
350 case '<': {
351 eCaseMode = kLowerCase;
352 ++iFormatIdx;
353 } break;
354 case '>': {
355 eCaseMode = kUpperCase;
356 ++iFormatIdx;
357 } break;
358 case '=': {
359 eCaseMode = kPreserveCase;
360 ++iFormatIdx;
361 } break;
362 case '?': {
363 if (iSourceIdx < wsSource.GetLength()) {
364 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
365 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700366 }
tsepez4f1f41f2016-03-28 14:13:16 -0700367 ++iFormatIdx;
368 } break;
369 case 'X': {
370 if (iSourceIdx < wsSource.GetLength()) {
371 if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
372 (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
373 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
374 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
375 ++iFormatIdx;
376 }
377 ++iSourceIdx;
378 } else {
379 ++iFormatIdx;
380 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 } break;
382 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700383 if (iSourceIdx < wsSource.GetLength()) {
384 if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
385 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
386 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
387 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 }
tsepez4f1f41f2016-03-28 14:13:16 -0700389 ++iSourceIdx;
390 } else {
391 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 } break;
394 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700395 if (iSourceIdx < wsSource.GetLength()) {
396 if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
397 wsResult += wsSource[iSourceIdx];
398 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 }
tsepez4f1f41f2016-03-28 14:13:16 -0700400 ++iSourceIdx;
401 } else {
402 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 }
tsepez4f1f41f2016-03-28 14:13:16 -0700404 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700406 if (iSourceIdx < wsSource.GetLength()) {
407 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
408 ++iSourceIdx;
409 } else {
410 ++iFormatIdx;
411 }
412 } break;
413 default: {
414 wsResult += wsFormat[iFormatIdx];
415 ++iFormatIdx;
416 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 }
418 }
tsepez4f1f41f2016-03-28 14:13:16 -0700419 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420}
421
Tom Sepezb1670b52017-02-16 17:01:00 -0800422bool util::scand(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700423 const std::vector<CJS_Value>& params,
424 CJS_Value& vRet,
425 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700426 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700427 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428
tsepezb4694242016-08-15 16:44:55 -0700429 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
430 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 double dDate = JS_GetDateTime();
432 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800433 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 }
435
436 if (!JS_PortIsNan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700437 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700439 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 }
441
tsepez4cf55152016-11-02 14:37:54 -0700442 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443}
444
Tom Sepezb1670b52017-02-16 17:01:00 -0800445bool util::byteToChar(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700446 const std::vector<CJS_Value>& params,
447 CJS_Value& vRet,
448 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700449 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700450 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700451 return false;
tsepez90d87792016-03-29 09:21:54 -0700452 }
tsepezf3dc8c62016-08-10 06:29:29 -0700453
tsepezb4694242016-08-15 16:44:55 -0700454 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700455 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700456 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700457 return false;
tsepez90d87792016-03-29 09:21:54 -0700458 }
tsepezf3dc8c62016-08-10 06:29:29 -0700459
Dan Sinclair812e96c2017-03-13 16:43:37 -0400460 CFX_WideString wStr(static_cast<wchar_t>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700461 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700462 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463}