blob: 04f1eb36cdeb0fa238a5fcd48f6c257b3f2bb80b [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>
Tom Sepezdf950b82017-08-04 11:33:49 -070012#include <cmath>
Lei Zhange247ec42017-04-20 21:41:36 -070013#include <cwctype>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014#include <string>
15#include <vector>
16
Dan Sinclaircfb19442017-04-20 13:13:04 -040017#include "core/fxcrt/fx_extension.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040018#include "fpdfsdk/javascript/JS_Define.h"
19#include "fpdfsdk/javascript/JS_EventHandler.h"
20#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040021#include "fpdfsdk/javascript/JS_Value.h"
22#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080023#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070024#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040025#include "fpdfsdk/javascript/resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028#include <ctype.h>
29#endif
30
Tom Sepez04557b82017-02-16 09:43:10 -080031JSConstSpec CJS_Util::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Tom Sepez04557b82017-02-16 09:43:10 -080033JSPropertySpec CJS_Util::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Tom Sepez04557b82017-02-16 09:43:10 -080035JSMethodSpec CJS_Util::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080036 {"printd", printd_static}, {"printf", printf_static},
37 {"printx", printx_static}, {"scand", scand_static},
38 {"byteToChar", byteToChar_static}, {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
tsepez86a61dc2016-03-25 10:00:11 -070042namespace {
43
44// Map PDF-style directives to equivalent wcsftime directives. Not
45// all have direct equivalents, though.
46struct TbConvert {
Dan Sinclair812e96c2017-03-13 16:43:37 -040047 const wchar_t* lpszJSMark;
48 const wchar_t* lpszCppMark;
tsepez86a61dc2016-03-25 10:00:11 -070049};
50
51// Map PDF-style directives lacking direct wcsftime directives to
52// the value with which they will be replaced.
53struct TbConvertAdditional {
Dan Sinclair812e96c2017-03-13 16:43:37 -040054 const wchar_t* lpszJSMark;
tsepez86a61dc2016-03-25 10:00:11 -070055 int iValue;
56};
57
58const TbConvert TbConvertTable[] = {
59 {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"},
60 {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"},
61 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"},
62 {L"TT", L"%p"},
63#if defined(_WIN32)
64 {L"tt", L"%p"}, {L"h", L"%#I"},
65#else
66 {L"tt", L"%P"}, {L"h", L"%l"},
67#endif
68};
69
tsepez86a61dc2016-03-25 10:00:11 -070070} // namespace
71
72util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
73
74util::~util() {}
75
Tom Sepezb1670b52017-02-16 17:01:00 -080076bool util::printf(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -070077 const std::vector<CJS_Value>& params,
78 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -040079 WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -070080 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (iSize < 1)
tsepez4cf55152016-11-02 14:37:54 -070082 return false;
Lei Zhang574b5742017-03-30 12:41:55 -070083
Tom Sepezffbc0d92017-07-17 09:29:05 -070084 std::wstring unsafe_fmt_string(params[0].ToCFXWideString(pRuntime).c_str());
85 std::vector<std::wstring> unsafe_conversion_specifiers;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 int iOffset = 0;
87 int iOffend = 0;
Tom Sepezffbc0d92017-07-17 09:29:05 -070088 unsafe_fmt_string.insert(unsafe_fmt_string.begin(), L'S');
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 while (iOffset != -1) {
Tom Sepezffbc0d92017-07-17 09:29:05 -070090 iOffend = unsafe_fmt_string.find(L"%", iOffset + 1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 std::wstring strSub;
92 if (iOffend == -1)
Tom Sepezffbc0d92017-07-17 09:29:05 -070093 strSub = unsafe_fmt_string.substr(iOffset);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 else
Tom Sepezffbc0d92017-07-17 09:29:05 -070095 strSub = unsafe_fmt_string.substr(iOffset, iOffend - iOffset);
96 unsafe_conversion_specifiers.push_back(strSub);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 iOffset = iOffend;
98 }
99
100 std::wstring c_strResult;
Tom Sepezffbc0d92017-07-17 09:29:05 -0700101 for (size_t iIndex = 0; iIndex < unsafe_conversion_specifiers.size();
102 ++iIndex) {
103 std::wstring c_strFormat = unsafe_conversion_specifiers[iIndex];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (iIndex == 0) {
105 c_strResult = c_strFormat;
106 continue;
107 }
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 if (iIndex >= iSize) {
110 c_strResult += c_strFormat;
111 continue;
112 }
113
Ryan Harrison275e2602017-09-18 14:23:18 -0400114 WideString strSegment;
tsepez86a61dc2016-03-25 10:00:11 -0700115 switch (ParseDataType(&c_strFormat)) {
Tom Sepezffbc0d92017-07-17 09:29:05 -0700116 case UTIL_INT:
tsepezb4694242016-08-15 16:44:55 -0700117 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 break;
119 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700120 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700121 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 break;
123 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700124 strSegment.Format(c_strFormat.c_str(),
125 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 break;
127 default:
Tom Sepezffbc0d92017-07-17 09:29:05 -0700128 strSegment.Format(L"%ls", c_strFormat.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 break;
130 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700131 c_strResult += strSegment.c_str();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 }
133
134 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700135 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700136 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Tom Sepezb1670b52017-02-16 17:01:00 -0800139bool util::printd(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700140 const std::vector<CJS_Value>& params,
141 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400142 WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700143 const size_t iSize = params.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700145 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146
Lei Zhang574b5742017-03-30 12:41:55 -0700147 const CJS_Value& p1 = params[0];
148 const CJS_Value& p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700149 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700150 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700151 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700152 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 }
154
tsepezb4694242016-08-15 16:44:55 -0700155 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700156 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700157 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 }
159
Tom Sepez39bfe122015-09-17 15:25:23 -0700160 if (p1.GetType() == CJS_Value::VT_number) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400161 WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700162 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 case 0:
tsepezb4694242016-08-15 16:44:55 -0700164 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
165 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
166 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
167 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 break;
169 case 1:
tsepezf3c88322016-08-09 07:30:38 -0700170 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700171 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
172 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
173 jsDate.GetMinutes(pRuntime),
174 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 break;
176 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700177 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700178 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
179 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
180 jsDate.GetMinutes(pRuntime),
181 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 break;
183 default:
tsepezcd5dc852016-09-08 11:23:24 -0700184 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700185 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 }
187
tsepezf3dc8c62016-08-10 06:29:29 -0700188 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700189 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
tsepez86a61dc2016-03-25 10:00:11 -0700191
Tom Sepez39bfe122015-09-17 15:25:23 -0700192 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700193 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700194 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700195 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700196 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197
tsepez86a61dc2016-03-25 10:00:11 -0700198 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
199 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700200 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700201 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
202 cFormat.end());
203
204 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 int iStart = 0;
206 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700207 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
208 -1) {
209 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
210 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 iStart = iEnd;
212 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700213 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214
tsepezb4694242016-08-15 16:44:55 -0700215 int iYear = jsDate.GetYear(pRuntime);
Lei Zhang2bf942d2017-06-16 13:48:19 -0700216 if (iYear < 0) {
217 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
218 return false;
219 }
220
tsepezb4694242016-08-15 16:44:55 -0700221 int iMonth = jsDate.GetMonth(pRuntime);
222 int iDay = jsDate.GetDay(pRuntime);
223 int iHour = jsDate.GetHours(pRuntime);
224 int iMin = jsDate.GetMinutes(pRuntime);
225 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226
Lei Zhang1e25e122017-06-16 02:14:40 -0700227 static const TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 {L"m", iMonth + 1}, {L"d", iDay},
229 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
230 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700231 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232
tsepez86a61dc2016-03-25 10:00:11 -0700233 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400234 WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700235 sValue.Format(L"%d", cTableAd[i].iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 int iStart = 0;
238 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700239 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 if (iEnd > 0) {
241 if (cFormat[iEnd - 1] == L'%') {
242 iStart = iEnd + 1;
243 continue;
244 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700245 }
Lei Zhang1e25e122017-06-16 02:14:40 -0700246 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark),
247 sValue.c_str());
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
tsepez86a61dc2016-03-25 10:00:11 -0700252 struct tm time = {};
253 time.tm_year = iYear - 1900;
254 time.tm_mon = iMonth;
255 time.tm_mday = iDay;
256 time.tm_hour = iHour;
257 time.tm_min = iMin;
258 time.tm_sec = iSec;
259
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700260 wchar_t buf[64] = {};
Lei Zhang2bf942d2017-06-16 13:48:19 -0700261 FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700262 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700263 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700264 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 }
tsepez86a61dc2016-03-25 10:00:11 -0700266
tsepezcd5dc852016-09-08 11:23:24 -0700267 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700268 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269}
270
Tom Sepezb1670b52017-02-16 17:01:00 -0800271bool util::printx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700272 const std::vector<CJS_Value>& params,
273 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400274 WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700275 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700276 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700277 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700278 }
tsepezf3dc8c62016-08-10 06:29:29 -0700279
tsepezb4694242016-08-15 16:44:55 -0700280 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
281 params[1].ToCFXWideString(pRuntime))
282 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700283
tsepez4cf55152016-11-02 14:37:54 -0700284 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285}
286
tsepez4f1f41f2016-03-28 14:13:16 -0700287enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
288
Dan Sinclair812e96c2017-03-13 16:43:37 -0400289static wchar_t TranslateCase(wchar_t input, CaseMode eMode) {
Lei Zhangef002c82017-04-24 16:28:07 -0700290 if (eMode == kLowerCase && FXSYS_isupper(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700291 return input | 0x20;
Lei Zhangef002c82017-04-24 16:28:07 -0700292 if (eMode == kUpperCase && FXSYS_islower(input))
tsepez4f1f41f2016-03-28 14:13:16 -0700293 return input & ~0x20;
294 return input;
295}
296
Ryan Harrison275e2602017-09-18 14:23:18 -0400297WideString util::printx(const WideString& wsFormat,
298 const WideString& wsSource) {
299 WideString wsResult;
tsepez4f1f41f2016-03-28 14:13:16 -0700300 FX_STRSIZE iSourceIdx = 0;
301 FX_STRSIZE iFormatIdx = 0;
302 CaseMode eCaseMode = kPreserveCase;
303 bool bEscaped = false;
304 while (iFormatIdx < wsFormat.GetLength()) {
305 if (bEscaped) {
306 bEscaped = false;
307 wsResult += wsFormat[iFormatIdx];
308 ++iFormatIdx;
309 continue;
310 }
311 switch (wsFormat[iFormatIdx]) {
312 case '\\': {
313 bEscaped = true;
314 ++iFormatIdx;
315 } break;
316 case '<': {
317 eCaseMode = kLowerCase;
318 ++iFormatIdx;
319 } break;
320 case '>': {
321 eCaseMode = kUpperCase;
322 ++iFormatIdx;
323 } break;
324 case '=': {
325 eCaseMode = kPreserveCase;
326 ++iFormatIdx;
327 } break;
328 case '?': {
329 if (iSourceIdx < wsSource.GetLength()) {
330 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
331 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700332 }
tsepez4f1f41f2016-03-28 14:13:16 -0700333 ++iFormatIdx;
334 } break;
335 case 'X': {
336 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700337 if (FXSYS_iswalnum(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700338 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
339 ++iFormatIdx;
340 }
341 ++iSourceIdx;
342 } else {
343 ++iFormatIdx;
344 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 } break;
346 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700347 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700348 if (FXSYS_iswalpha(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700349 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
350 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 }
tsepez4f1f41f2016-03-28 14:13:16 -0700352 ++iSourceIdx;
353 } else {
354 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 } break;
357 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700358 if (iSourceIdx < wsSource.GetLength()) {
Lei Zhangef002c82017-04-24 16:28:07 -0700359 if (std::iswdigit(wsSource[iSourceIdx])) {
tsepez4f1f41f2016-03-28 14:13:16 -0700360 wsResult += wsSource[iSourceIdx];
361 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 }
tsepez4f1f41f2016-03-28 14:13:16 -0700363 ++iSourceIdx;
364 } else {
365 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 }
tsepez4f1f41f2016-03-28 14:13:16 -0700367 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700369 if (iSourceIdx < wsSource.GetLength()) {
370 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
371 ++iSourceIdx;
372 } else {
373 ++iFormatIdx;
374 }
375 } break;
376 default: {
377 wsResult += wsFormat[iFormatIdx];
378 ++iFormatIdx;
379 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 }
381 }
tsepez4f1f41f2016-03-28 14:13:16 -0700382 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}
384
Tom Sepezb1670b52017-02-16 17:01:00 -0800385bool util::scand(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700386 const std::vector<CJS_Value>& params,
387 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400388 WideString& sError) {
Lei Zhang574b5742017-03-30 12:41:55 -0700389 if (params.size() < 2)
tsepez4cf55152016-11-02 14:37:54 -0700390 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391
Ryan Harrison275e2602017-09-18 14:23:18 -0400392 WideString sFormat = params[0].ToCFXWideString(pRuntime);
393 WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 double dDate = JS_GetDateTime();
395 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800396 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 }
398
Tom Sepezdf950b82017-08-04 11:33:49 -0700399 if (!std::isnan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700400 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700402 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 }
404
tsepez4cf55152016-11-02 14:37:54 -0700405 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406}
407
Tom Sepezb1670b52017-02-16 17:01:00 -0800408bool util::byteToChar(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700409 const std::vector<CJS_Value>& params,
410 CJS_Value& vRet,
Ryan Harrison275e2602017-09-18 14:23:18 -0400411 WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700412 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700413 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700414 return false;
tsepez90d87792016-03-29 09:21:54 -0700415 }
tsepezf3dc8c62016-08-10 06:29:29 -0700416
tsepezb4694242016-08-15 16:44:55 -0700417 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700418 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700419 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700420 return false;
tsepez90d87792016-03-29 09:21:54 -0700421 }
tsepezf3dc8c62016-08-10 06:29:29 -0700422
Ryan Harrison275e2602017-09-18 14:23:18 -0400423 WideString wStr(static_cast<wchar_t>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700424 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700425 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426}
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400427
Tom Sepezffbc0d92017-07-17 09:29:05 -0700428// Ensure that sFormat contains at most one well-understood printf formatting
429// directive which is safe to use with a single argument, and return the type
430// of argument expected, or -1 otherwise. If -1 is returned, it is NOT safe
431// to use sFormat with printf() and it must be copied byte-by-byte.
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400432int util::ParseDataType(std::wstring* sFormat) {
Tom Sepezffbc0d92017-07-17 09:29:05 -0700433 enum State { BEFORE, FLAGS, WIDTH, PRECISION, SPECIFIER, AFTER };
434
435 int result = -1;
436 State state = BEFORE;
437 size_t precision_digits = 0;
438 size_t i = 0;
439 while (i < sFormat->length()) {
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400440 wchar_t c = (*sFormat)[i];
Tom Sepezffbc0d92017-07-17 09:29:05 -0700441 switch (state) {
442 case BEFORE:
443 if (c == L'%')
444 state = FLAGS;
445 break;
446 case FLAGS:
447 if (c == L'+' || c == L'-' || c == L'#' || c == L' ') {
448 // Stay in same state.
449 } else {
450 state = WIDTH;
451 continue; // Re-process same character.
452 }
453 break;
454 case WIDTH:
455 if (c == L'*')
456 return -1;
457 if (std::iswdigit(c)) {
458 // Stay in same state.
459 } else if (c == L'.') {
460 state = PRECISION;
461 } else {
462 state = SPECIFIER;
463 continue; // Re-process same character.
464 }
465 break;
466 case PRECISION:
467 if (c == L'*')
468 return -1;
469 if (std::iswdigit(c)) {
470 // Stay in same state.
471 ++precision_digits;
472 } else {
473 state = SPECIFIER;
474 continue; // Re-process same character.
475 }
476 break;
477 case SPECIFIER:
478 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
479 c == L'u' || c == L'x' || c == L'X') {
480 result = UTIL_INT;
481 } else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' ||
482 c == L'G') {
483 result = UTIL_DOUBLE;
484 } else if (c == L's' || c == L'S') {
485 // Map s to S since we always deal internally with wchar_t strings.
486 // TODO(tsepez): Probably 100% borked. %S is not a standard
487 // conversion.
488 (*sFormat)[i] = L'S';
489 result = UTIL_STRING;
490 } else {
491 return -1;
492 }
493 state = AFTER;
494 break;
495 case AFTER:
496 if (c == L'%')
497 return -1;
498 // Stay in same state until string exhausted.
499 break;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400500 }
Tom Sepezffbc0d92017-07-17 09:29:05 -0700501 ++i;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400502 }
Tom Sepezffbc0d92017-07-17 09:29:05 -0700503 // See https://crbug.com/740166
504 if (result == UTIL_INT && precision_digits > 2)
505 return -1;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400506
Tom Sepezffbc0d92017-07-17 09:29:05 -0700507 return result;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400508}