blob: 5d1adc9298bc8c1bb14186f112ea4d55e81f23d5 [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
Tom Sepezffbc0d92017-07-17 09:29:05 -070087 std::wstring unsafe_fmt_string(params[0].ToCFXWideString(pRuntime).c_str());
88 std::vector<std::wstring> unsafe_conversion_specifiers;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 int iOffset = 0;
90 int iOffend = 0;
Tom Sepezffbc0d92017-07-17 09:29:05 -070091 unsafe_fmt_string.insert(unsafe_fmt_string.begin(), L'S');
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 while (iOffset != -1) {
Tom Sepezffbc0d92017-07-17 09:29:05 -070093 iOffend = unsafe_fmt_string.find(L"%", iOffset + 1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 std::wstring strSub;
95 if (iOffend == -1)
Tom Sepezffbc0d92017-07-17 09:29:05 -070096 strSub = unsafe_fmt_string.substr(iOffset);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 else
Tom Sepezffbc0d92017-07-17 09:29:05 -070098 strSub = unsafe_fmt_string.substr(iOffset, iOffend - iOffset);
99 unsafe_conversion_specifiers.push_back(strSub);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 iOffset = iOffend;
101 }
102
103 std::wstring c_strResult;
Tom Sepezffbc0d92017-07-17 09:29:05 -0700104 for (size_t iIndex = 0; iIndex < unsafe_conversion_specifiers.size();
105 ++iIndex) {
106 std::wstring c_strFormat = unsafe_conversion_specifiers[iIndex];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 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)) {
Tom Sepezffbc0d92017-07-17 09:29:05 -0700119 case UTIL_INT:
tsepezb4694242016-08-15 16:44:55 -0700120 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 break;
122 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700123 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700124 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 break;
126 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700127 strSegment.Format(c_strFormat.c_str(),
128 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 break;
130 default:
Tom Sepezffbc0d92017-07-17 09:29:05 -0700131 strSegment.Format(L"%ls", c_strFormat.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 break;
133 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700134 c_strResult += strSegment.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 }
136
137 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700138 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700139 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Tom Sepezb1670b52017-02-16 17:01:00 -0800142bool util::printd(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700143 const std::vector<CJS_Value>& params,
144 CJS_Value& vRet,
145 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700146 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700148 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149
Lei Zhang574b5742017-03-30 12:41:55 -0700150 const CJS_Value& p1 = params[0];
151 const CJS_Value& p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700152 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700153 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700154 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700155 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 }
157
tsepezb4694242016-08-15 16:44:55 -0700158 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700159 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700160 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
162
Tom Sepez39bfe122015-09-17 15:25:23 -0700163 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 CFX_WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700165 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 case 0:
tsepezb4694242016-08-15 16:44:55 -0700167 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
168 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
169 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
170 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 break;
172 case 1:
tsepezf3c88322016-08-09 07:30:38 -0700173 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700174 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
175 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
176 jsDate.GetMinutes(pRuntime),
177 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 break;
179 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700180 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700181 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
182 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
183 jsDate.GetMinutes(pRuntime),
184 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 break;
186 default:
tsepezcd5dc852016-09-08 11:23:24 -0700187 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 }
190
tsepezf3dc8c62016-08-10 06:29:29 -0700191 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700192 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 }
tsepez86a61dc2016-03-25 10:00:11 -0700194
Tom Sepez39bfe122015-09-17 15:25:23 -0700195 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700196 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700197 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700198 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700199 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200
tsepez86a61dc2016-03-25 10:00:11 -0700201 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
202 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700203 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700204 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
205 cFormat.end());
206
207 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 int iStart = 0;
209 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700210 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
211 -1) {
212 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
213 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 iStart = iEnd;
215 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700216 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
tsepezb4694242016-08-15 16:44:55 -0700218 int iYear = jsDate.GetYear(pRuntime);
Lei Zhang2bf942d2017-06-16 13:48:19 -0700219 if (iYear < 0) {
220 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
221 return false;
222 }
223
tsepezb4694242016-08-15 16:44:55 -0700224 int iMonth = jsDate.GetMonth(pRuntime);
225 int iDay = jsDate.GetDay(pRuntime);
226 int iHour = jsDate.GetHours(pRuntime);
227 int iMin = jsDate.GetMinutes(pRuntime);
228 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229
Lei Zhang1e25e122017-06-16 02:14:40 -0700230 static const TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 {L"m", iMonth + 1}, {L"d", iDay},
232 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
233 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700234 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
tsepez86a61dc2016-03-25 10:00:11 -0700236 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700238 sValue.Format(L"%d", cTableAd[i].iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 int iStart = 0;
241 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700242 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 if (iEnd > 0) {
244 if (cFormat[iEnd - 1] == L'%') {
245 iStart = iEnd + 1;
246 continue;
247 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700248 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700249 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark),
250 sValue.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 iStart = iEnd;
252 }
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 struct tm time = {};
256 time.tm_year = iYear - 1900;
257 time.tm_mon = iMonth;
258 time.tm_mday = iDay;
259 time.tm_hour = iHour;
260 time.tm_min = iMin;
261 time.tm_sec = iSec;
262
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700263 wchar_t buf[64] = {};
Lei Zhang2bf942d2017-06-16 13:48:19 -0700264 FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700265 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700266 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700267 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 }
tsepez86a61dc2016-03-25 10:00:11 -0700269
tsepezcd5dc852016-09-08 11:23:24 -0700270 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700271 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
Tom Sepezb1670b52017-02-16 17:01:00 -0800274bool util::printx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700275 const std::vector<CJS_Value>& params,
276 CJS_Value& vRet,
277 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700278 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700279 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700280 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700281 }
tsepezf3dc8c62016-08-10 06:29:29 -0700282
tsepezb4694242016-08-15 16:44:55 -0700283 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
284 params[1].ToCFXWideString(pRuntime))
285 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700286
tsepez4cf55152016-11-02 14:37:54 -0700287 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288}
289
tsepez4f1f41f2016-03-28 14:13:16 -0700290enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
291
Dan Sinclair812e96c2017-03-13 16:43:37 -0400292static wchar_t TranslateCase(wchar_t input, CaseMode eMode) {
Lei Zhangef002c82017-04-24 16:28:07 -0700293 if (eMode == kLowerCase && FXSYS_isupper(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700294 return input | 0x20;
Lei Zhangef002c82017-04-24 16:28:07 -0700295 if (eMode == kUpperCase && FXSYS_islower(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700296 return input & ~0x20;
297 return input;
298}
299
300CFX_WideString util::printx(const CFX_WideString& wsFormat,
301 const CFX_WideString& wsSource) {
302 CFX_WideString wsResult;
303 FX_STRSIZE iSourceIdx = 0;
304 FX_STRSIZE iFormatIdx = 0;
305 CaseMode eCaseMode = kPreserveCase;
306 bool bEscaped = false;
307 while (iFormatIdx < wsFormat.GetLength()) {
308 if (bEscaped) {
309 bEscaped = false;
310 wsResult += wsFormat[iFormatIdx];
311 ++iFormatIdx;
312 continue;
313 }
314 switch (wsFormat[iFormatIdx]) {
315 case '\\': {
316 bEscaped = true;
317 ++iFormatIdx;
318 } break;
319 case '<': {
320 eCaseMode = kLowerCase;
321 ++iFormatIdx;
322 } break;
323 case '>': {
324 eCaseMode = kUpperCase;
325 ++iFormatIdx;
326 } break;
327 case '=': {
328 eCaseMode = kPreserveCase;
329 ++iFormatIdx;
330 } break;
331 case '?': {
332 if (iSourceIdx < wsSource.GetLength()) {
333 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
334 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700335 }
tsepez4f1f41f2016-03-28 14:13:16 -0700336 ++iFormatIdx;
337 } break;
338 case 'X': {
339 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700340 if (FXSYS_iswalnum(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700341 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
342 ++iFormatIdx;
343 }
344 ++iSourceIdx;
345 } else {
346 ++iFormatIdx;
347 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 } break;
349 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700350 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700351 if (FXSYS_iswalpha(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700352 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
353 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 }
tsepez4f1f41f2016-03-28 14:13:16 -0700355 ++iSourceIdx;
356 } else {
357 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 } break;
360 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700361 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700362 if (std::iswdigit(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700363 wsResult += wsSource[iSourceIdx];
364 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 }
tsepez4f1f41f2016-03-28 14:13:16 -0700366 ++iSourceIdx;
367 } else {
368 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 }
tsepez4f1f41f2016-03-28 14:13:16 -0700370 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700372 if (iSourceIdx < wsSource.GetLength()) {
373 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
374 ++iSourceIdx;
375 } else {
376 ++iFormatIdx;
377 }
378 } break;
379 default: {
380 wsResult += wsFormat[iFormatIdx];
381 ++iFormatIdx;
382 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 }
384 }
tsepez4f1f41f2016-03-28 14:13:16 -0700385 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386}
387
Tom Sepezb1670b52017-02-16 17:01:00 -0800388bool util::scand(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700389 const std::vector<CJS_Value>& params,
390 CJS_Value& vRet,
391 CFX_WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700392 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700393 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394
tsepezb4694242016-08-15 16:44:55 -0700395 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
396 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 double dDate = JS_GetDateTime();
398 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800399 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 }
401
402 if (!JS_PortIsNan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700403 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700405 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 }
407
tsepez4cf55152016-11-02 14:37:54 -0700408 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409}
410
Tom Sepezb1670b52017-02-16 17:01:00 -0800411bool util::byteToChar(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700412 const std::vector<CJS_Value>& params,
413 CJS_Value& vRet,
414 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700415 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700416 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700417 return false;
tsepez90d87792016-03-29 09:21:54 -0700418 }
tsepezf3dc8c62016-08-10 06:29:29 -0700419
tsepezb4694242016-08-15 16:44:55 -0700420 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700421 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700422 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700423 return false;
tsepez90d87792016-03-29 09:21:54 -0700424 }
tsepezf3dc8c62016-08-10 06:29:29 -0700425
Dan Sinclair812e96c2017-03-13 16:43:37 -0400426 CFX_WideString wStr(static_cast<wchar_t>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700427 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700428 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429}
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400430
Tom Sepezffbc0d92017-07-17 09:29:05 -0700431// Ensure that sFormat contains at most one well-understood printf formatting
432// directive which is safe to use with a single argument, and return the type
433// of argument expected, or -1 otherwise. If -1 is returned, it is NOT safe
434// to use sFormat with printf() and it must be copied byte-by-byte.
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400435int util::ParseDataType(std::wstring* sFormat) {
Tom Sepezffbc0d92017-07-17 09:29:05 -0700436 enum State { BEFORE, FLAGS, WIDTH, PRECISION, SPECIFIER, AFTER };
437
438 int result = -1;
439 State state = BEFORE;
440 size_t precision_digits = 0;
441 size_t i = 0;
442 while (i < sFormat->length()) {
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400443 wchar_t c = (*sFormat)[i];
Tom Sepezffbc0d92017-07-17 09:29:05 -0700444 switch (state) {
445 case BEFORE:
446 if (c == L'%')
447 state = FLAGS;
448 break;
449 case FLAGS:
450 if (c == L'+' || c == L'-' || c == L'#' || c == L' ') {
451 // Stay in same state.
452 } else {
453 state = WIDTH;
454 continue; // Re-process same character.
455 }
456 break;
457 case WIDTH:
458 if (c == L'*')
459 return -1;
460 if (std::iswdigit(c)) {
461 // Stay in same state.
462 } else if (c == L'.') {
463 state = PRECISION;
464 } else {
465 state = SPECIFIER;
466 continue; // Re-process same character.
467 }
468 break;
469 case PRECISION:
470 if (c == L'*')
471 return -1;
472 if (std::iswdigit(c)) {
473 // Stay in same state.
474 ++precision_digits;
475 } else {
476 state = SPECIFIER;
477 continue; // Re-process same character.
478 }
479 break;
480 case SPECIFIER:
481 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
482 c == L'u' || c == L'x' || c == L'X') {
483 result = UTIL_INT;
484 } else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' ||
485 c == L'G') {
486 result = UTIL_DOUBLE;
487 } else if (c == L's' || c == L'S') {
488 // Map s to S since we always deal internally with wchar_t strings.
489 // TODO(tsepez): Probably 100% borked. %S is not a standard
490 // conversion.
491 (*sFormat)[i] = L'S';
492 result = UTIL_STRING;
493 } else {
494 return -1;
495 }
496 state = AFTER;
497 break;
498 case AFTER:
499 if (c == L'%')
500 return -1;
501 // Stay in same state until string exhausted.
502 break;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400503 }
Tom Sepezffbc0d92017-07-17 09:29:05 -0700504 ++i;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400505 }
Tom Sepezffbc0d92017-07-17 09:29:05 -0700506 // See https://crbug.com/740166
507 if (result == UTIL_INT && precision_digits > 2)
508 return -1;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400509
Tom Sepezffbc0d92017-07-17 09:29:05 -0700510 return result;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400511}