blob: 7f0fe1eadbd0873cf3b2dbff574f2c5dd4422e6b [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
tsepez86a61dc2016-03-25 10:00:11 -070073} // namespace
74
75util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
76
77util::~util() {}
78
Tom Sepezb1670b52017-02-16 17:01:00 -080079bool util::printf(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -070080 const std::vector<CJS_Value>& params,
81 CJS_Value& vRet,
82 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -070083 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (iSize < 1)
tsepez4cf55152016-11-02 14:37:54 -070085 return false;
Lei Zhang574b5742017-03-30 12:41:55 -070086
tsepezb4694242016-08-15 16:44:55 -070087 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 std::vector<std::wstring> c_strConvers;
89 int iOffset = 0;
90 int iOffend = 0;
91 c_ConvChar.insert(c_ConvChar.begin(), L'S');
92 while (iOffset != -1) {
93 iOffend = c_ConvChar.find(L"%", iOffset + 1);
94 std::wstring strSub;
95 if (iOffend == -1)
96 strSub = c_ConvChar.substr(iOffset);
97 else
98 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
99 c_strConvers.push_back(strSub);
100 iOffset = iOffend;
101 }
102
103 std::wstring c_strResult;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 std::wstring c_strFormat;
Lei Zhang574b5742017-03-30 12:41:55 -0700105 for (size_t iIndex = 0; iIndex < c_strConvers.size(); ++iIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 c_strFormat = c_strConvers[iIndex];
107 if (iIndex == 0) {
108 c_strResult = c_strFormat;
109 continue;
110 }
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 if (iIndex >= iSize) {
113 c_strResult += c_strFormat;
114 continue;
115 }
116
Lei Zhang574b5742017-03-30 12:41:55 -0700117 CFX_WideString strSegment;
tsepez86a61dc2016-03-25 10:00:11 -0700118 switch (ParseDataType(&c_strFormat)) {
Dan Sinclair0c998292017-07-13 09:58:52 -0400119 case UTIL_INT: {
120 int dot = c_strFormat.find(L".", 0);
121 if (dot != -1) {
122 size_t len = 0;
123 for (size_t i = dot + 1; i < c_strFormat.length(); ++i) {
124 wchar_t c = c_strFormat[i];
125 if (std::iswdigit(c)) {
126 ++len;
127 continue;
128 }
129 break;
130 }
131
132 // Windows has a max of ~261 characters in the format string of
133 // the form %0.261x. We're just going to bail out if the format
134 // would be over 3 or more characters long.
135 if (len > 2)
136 return false;
137 }
tsepezb4694242016-08-15 16:44:55 -0700138 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 break;
Dan Sinclair0c998292017-07-13 09:58:52 -0400140 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700142 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700143 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 break;
145 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700146 strSegment.Format(c_strFormat.c_str(),
147 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 break;
149 default:
150 strSegment.Format(L"%S", c_strFormat.c_str());
151 break;
152 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700153 c_strResult += strSegment.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 }
155
156 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700157 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700158 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Tom Sepezb1670b52017-02-16 17:01:00 -0800161bool util::printd(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700162 const std::vector<CJS_Value>& params,
163 CJS_Value& vRet,
164 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700165 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700167 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168
Lei Zhang574b5742017-03-30 12:41:55 -0700169 const CJS_Value& p1 = params[0];
170 const CJS_Value& p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700171 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700172 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700173 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700174 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
176
tsepezb4694242016-08-15 16:44:55 -0700177 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700178 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700179 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 }
181
Tom Sepez39bfe122015-09-17 15:25:23 -0700182 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CFX_WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700184 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 case 0:
tsepezb4694242016-08-15 16:44:55 -0700186 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
187 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
188 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
189 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 break;
191 case 1:
tsepezf3c88322016-08-09 07:30:38 -0700192 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700193 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
194 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
195 jsDate.GetMinutes(pRuntime),
196 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 break;
198 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700199 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700200 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
201 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
202 jsDate.GetMinutes(pRuntime),
203 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 break;
205 default:
tsepezcd5dc852016-09-08 11:23:24 -0700206 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700207 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 }
209
tsepezf3dc8c62016-08-10 06:29:29 -0700210 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700211 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 }
tsepez86a61dc2016-03-25 10:00:11 -0700213
Tom Sepez39bfe122015-09-17 15:25:23 -0700214 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700215 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700216 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700217 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700218 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219
tsepez86a61dc2016-03-25 10:00:11 -0700220 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
221 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700222 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700223 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
224 cFormat.end());
225
226 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 int iStart = 0;
228 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700229 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
230 -1) {
231 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
232 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 iStart = iEnd;
234 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700235 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
tsepezb4694242016-08-15 16:44:55 -0700237 int iYear = jsDate.GetYear(pRuntime);
Lei Zhang2bf942d2017-06-16 13:48:19 -0700238 if (iYear < 0) {
239 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
240 return false;
241 }
242
tsepezb4694242016-08-15 16:44:55 -0700243 int iMonth = jsDate.GetMonth(pRuntime);
244 int iDay = jsDate.GetDay(pRuntime);
245 int iHour = jsDate.GetHours(pRuntime);
246 int iMin = jsDate.GetMinutes(pRuntime);
247 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
Lei Zhang1e25e122017-06-16 02:14:40 -0700249 static const TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 {L"m", iMonth + 1}, {L"d", iDay},
251 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
252 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700253 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
tsepez86a61dc2016-03-25 10:00:11 -0700255 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700257 sValue.Format(L"%d", cTableAd[i].iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 int iStart = 0;
260 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700261 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 if (iEnd > 0) {
263 if (cFormat[iEnd - 1] == L'%') {
264 iStart = iEnd + 1;
265 continue;
266 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700267 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700268 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark),
269 sValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 iStart = iEnd;
271 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700272 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
tsepez86a61dc2016-03-25 10:00:11 -0700274 struct tm time = {};
275 time.tm_year = iYear - 1900;
276 time.tm_mon = iMonth;
277 time.tm_mday = iDay;
278 time.tm_hour = iHour;
279 time.tm_min = iMin;
280 time.tm_sec = iSec;
281
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700282 wchar_t buf[64] = {};
Lei Zhang2bf942d2017-06-16 13:48:19 -0700283 FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700284 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700285 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700286 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
tsepez86a61dc2016-03-25 10:00:11 -0700288
tsepezcd5dc852016-09-08 11:23:24 -0700289 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700290 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Tom Sepezb1670b52017-02-16 17:01:00 -0800293bool util::printx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700294 const std::vector<CJS_Value>& params,
295 CJS_Value& vRet,
296 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700297 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700298 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700299 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700300 }
tsepezf3dc8c62016-08-10 06:29:29 -0700301
tsepezb4694242016-08-15 16:44:55 -0700302 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
303 params[1].ToCFXWideString(pRuntime))
304 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700305
tsepez4cf55152016-11-02 14:37:54 -0700306 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307}
308
tsepez4f1f41f2016-03-28 14:13:16 -0700309enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
310
Dan Sinclair812e96c2017-03-13 16:43:37 -0400311static wchar_t TranslateCase(wchar_t input, CaseMode eMode) {
Lei Zhangef002c82017-04-24 16:28:07 -0700312 if (eMode == kLowerCase && FXSYS_isupper(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700313 return input | 0x20;
Lei Zhangef002c82017-04-24 16:28:07 -0700314 if (eMode == kUpperCase && FXSYS_islower(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700315 return input & ~0x20;
316 return input;
317}
318
319CFX_WideString util::printx(const CFX_WideString& wsFormat,
320 const CFX_WideString& wsSource) {
321 CFX_WideString wsResult;
322 FX_STRSIZE iSourceIdx = 0;
323 FX_STRSIZE iFormatIdx = 0;
324 CaseMode eCaseMode = kPreserveCase;
325 bool bEscaped = false;
326 while (iFormatIdx < wsFormat.GetLength()) {
327 if (bEscaped) {
328 bEscaped = false;
329 wsResult += wsFormat[iFormatIdx];
330 ++iFormatIdx;
331 continue;
332 }
333 switch (wsFormat[iFormatIdx]) {
334 case '\\': {
335 bEscaped = true;
336 ++iFormatIdx;
337 } break;
338 case '<': {
339 eCaseMode = kLowerCase;
340 ++iFormatIdx;
341 } break;
342 case '>': {
343 eCaseMode = kUpperCase;
344 ++iFormatIdx;
345 } break;
346 case '=': {
347 eCaseMode = kPreserveCase;
348 ++iFormatIdx;
349 } break;
350 case '?': {
351 if (iSourceIdx < wsSource.GetLength()) {
352 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
353 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700354 }
tsepez4f1f41f2016-03-28 14:13:16 -0700355 ++iFormatIdx;
356 } break;
357 case 'X': {
358 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700359 if (FXSYS_iswalnum(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700360 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
361 ++iFormatIdx;
362 }
363 ++iSourceIdx;
364 } else {
365 ++iFormatIdx;
366 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 } break;
368 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700369 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700370 if (FXSYS_iswalpha(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700371 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
372 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 }
tsepez4f1f41f2016-03-28 14:13:16 -0700374 ++iSourceIdx;
375 } else {
376 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 } break;
379 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700380 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700381 if (std::iswdigit(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700382 wsResult += wsSource[iSourceIdx];
383 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 }
tsepez4f1f41f2016-03-28 14:13:16 -0700385 ++iSourceIdx;
386 } else {
387 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 }
tsepez4f1f41f2016-03-28 14:13:16 -0700389 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700391 if (iSourceIdx < wsSource.GetLength()) {
392 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
393 ++iSourceIdx;
394 } else {
395 ++iFormatIdx;
396 }
397 } break;
398 default: {
399 wsResult += wsFormat[iFormatIdx];
400 ++iFormatIdx;
401 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 }
403 }
tsepez4f1f41f2016-03-28 14:13:16 -0700404 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405}
406
Tom Sepezb1670b52017-02-16 17:01:00 -0800407bool util::scand(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700408 const std::vector<CJS_Value>& params,
409 CJS_Value& vRet,
410 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700411 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700412 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413
tsepezb4694242016-08-15 16:44:55 -0700414 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
415 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 double dDate = JS_GetDateTime();
417 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800418 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 }
420
421 if (!JS_PortIsNan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700422 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700424 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 }
426
tsepez4cf55152016-11-02 14:37:54 -0700427 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428}
429
Tom Sepezb1670b52017-02-16 17:01:00 -0800430bool util::byteToChar(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700431 const std::vector<CJS_Value>& params,
432 CJS_Value& vRet,
433 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700434 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700435 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700436 return false;
tsepez90d87792016-03-29 09:21:54 -0700437 }
tsepezf3dc8c62016-08-10 06:29:29 -0700438
tsepezb4694242016-08-15 16:44:55 -0700439 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700440 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700441 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700442 return false;
tsepez90d87792016-03-29 09:21:54 -0700443 }
tsepezf3dc8c62016-08-10 06:29:29 -0700444
Dan Sinclair812e96c2017-03-13 16:43:37 -0400445 CFX_WideString wStr(static_cast<wchar_t>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700446 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700447 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448}
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400449
450int util::ParseDataType(std::wstring* sFormat) {
451 bool bPercent = false;
452 for (size_t i = 0; i < sFormat->length(); ++i) {
453 wchar_t c = (*sFormat)[i];
454 if (c == L'%') {
455 bPercent = true;
456 continue;
457 }
458
459 if (bPercent) {
460 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
461 c == L'u' || c == L'x' || c == L'X') {
462 return UTIL_INT;
463 }
464 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
465 return UTIL_DOUBLE;
466 }
467 if (c == L's' || c == L'S') {
468 // Map s to S since we always deal internally
469 // with wchar_t strings.
470 (*sFormat)[i] = L'S';
471 return UTIL_STRING;
472 }
473 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
474 std::iswdigit(c)) {
475 continue;
476 }
477 break;
478 }
479 }
480
481 return -1;
482}