OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fpdfsdk/javascript/util.h" | 7 #include "fpdfsdk/javascript/util.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 util::~util() {} | 115 util::~util() {} |
116 | 116 |
117 FX_BOOL util::printf(IJS_Context* cc, | 117 FX_BOOL util::printf(IJS_Context* cc, |
118 const std::vector<CJS_Value>& params, | 118 const std::vector<CJS_Value>& params, |
119 CJS_Value& vRet, | 119 CJS_Value& vRet, |
120 CFX_WideString& sError) { | 120 CFX_WideString& sError) { |
121 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 121 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
122 int iSize = params.size(); | 122 int iSize = params.size(); |
123 if (iSize < 1) | 123 if (iSize < 1) |
124 return FALSE; | 124 return FALSE; |
125 std::wstring c_ConvChar( | 125 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str()); |
126 params[0].ToCFXWideString(pRuntime->GetIsolate()).c_str()); | |
127 std::vector<std::wstring> c_strConvers; | 126 std::vector<std::wstring> c_strConvers; |
128 int iOffset = 0; | 127 int iOffset = 0; |
129 int iOffend = 0; | 128 int iOffend = 0; |
130 c_ConvChar.insert(c_ConvChar.begin(), L'S'); | 129 c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
131 while (iOffset != -1) { | 130 while (iOffset != -1) { |
132 iOffend = c_ConvChar.find(L"%", iOffset + 1); | 131 iOffend = c_ConvChar.find(L"%", iOffset + 1); |
133 std::wstring strSub; | 132 std::wstring strSub; |
134 if (iOffend == -1) | 133 if (iOffend == -1) |
135 strSub = c_ConvChar.substr(iOffset); | 134 strSub = c_ConvChar.substr(iOffset); |
136 else | 135 else |
(...skipping 12 matching lines...) Expand all Loading... |
149 } | 148 } |
150 | 149 |
151 CFX_WideString strSegment; | 150 CFX_WideString strSegment; |
152 if (iIndex >= iSize) { | 151 if (iIndex >= iSize) { |
153 c_strResult += c_strFormat; | 152 c_strResult += c_strFormat; |
154 continue; | 153 continue; |
155 } | 154 } |
156 | 155 |
157 switch (ParseDataType(&c_strFormat)) { | 156 switch (ParseDataType(&c_strFormat)) { |
158 case UTIL_INT: | 157 case UTIL_INT: |
159 strSegment.Format(c_strFormat.c_str(), | 158 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime)); |
160 params[iIndex].ToInt(pRuntime->GetIsolate())); | |
161 break; | 159 break; |
162 case UTIL_DOUBLE: | 160 case UTIL_DOUBLE: |
163 strSegment.Format(c_strFormat.c_str(), | 161 strSegment.Format(c_strFormat.c_str(), |
164 params[iIndex].ToDouble(pRuntime->GetIsolate())); | 162 params[iIndex].ToDouble(pRuntime)); |
165 break; | 163 break; |
166 case UTIL_STRING: | 164 case UTIL_STRING: |
167 strSegment.Format( | 165 strSegment.Format(c_strFormat.c_str(), |
168 c_strFormat.c_str(), | 166 params[iIndex].ToCFXWideString(pRuntime).c_str()); |
169 params[iIndex].ToCFXWideString(pRuntime->GetIsolate()).c_str()); | |
170 break; | 167 break; |
171 default: | 168 default: |
172 strSegment.Format(L"%S", c_strFormat.c_str()); | 169 strSegment.Format(L"%S", c_strFormat.c_str()); |
173 break; | 170 break; |
174 } | 171 } |
175 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); | 172 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); |
176 } | 173 } |
177 | 174 |
178 c_strResult.erase(c_strResult.begin()); | 175 c_strResult.erase(c_strResult.begin()); |
179 vRet = CJS_Value(pRuntime, c_strResult.c_str()); | 176 vRet = CJS_Value(pRuntime, c_strResult.c_str()); |
180 return TRUE; | 177 return TRUE; |
181 } | 178 } |
182 | 179 |
183 FX_BOOL util::printd(IJS_Context* cc, | 180 FX_BOOL util::printd(IJS_Context* cc, |
184 const std::vector<CJS_Value>& params, | 181 const std::vector<CJS_Value>& params, |
185 CJS_Value& vRet, | 182 CJS_Value& vRet, |
186 CFX_WideString& sError) { | 183 CFX_WideString& sError) { |
187 int iSize = params.size(); | 184 int iSize = params.size(); |
188 if (iSize < 2) | 185 if (iSize < 2) |
189 return FALSE; | 186 return FALSE; |
190 | 187 |
191 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 188 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
192 CJS_Value p1 = params[0]; | 189 CJS_Value p1 = params[0]; |
193 CJS_Value p2 = params[1]; | 190 CJS_Value p2 = params[1]; |
194 CJS_Date jsDate; | 191 CJS_Date jsDate; |
195 if (!p2.ConvertToDate(pRuntime->GetIsolate(), jsDate)) { | 192 if (!p2.ConvertToDate(pRuntime, jsDate)) { |
196 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); | 193 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); |
197 return FALSE; | 194 return FALSE; |
198 } | 195 } |
199 | 196 |
200 if (!jsDate.IsValidDate(pRuntime->GetIsolate())) { | 197 if (!jsDate.IsValidDate(pRuntime)) { |
201 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); | 198 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); |
202 return FALSE; | 199 return FALSE; |
203 } | 200 } |
204 | 201 |
205 if (p1.GetType() == CJS_Value::VT_number) { | 202 if (p1.GetType() == CJS_Value::VT_number) { |
206 CFX_WideString swResult; | 203 CFX_WideString swResult; |
207 switch (p1.ToInt(pRuntime->GetIsolate())) { | 204 switch (p1.ToInt(pRuntime)) { |
208 case 0: | 205 case 0: |
209 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", | 206 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), |
210 jsDate.GetYear(pRuntime->GetIsolate()), | 207 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), |
211 jsDate.GetMonth(pRuntime->GetIsolate()) + 1, | 208 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), |
212 jsDate.GetDay(pRuntime->GetIsolate()), | 209 jsDate.GetSeconds(pRuntime)); |
213 jsDate.GetHours(pRuntime->GetIsolate()), | |
214 jsDate.GetMinutes(pRuntime->GetIsolate()), | |
215 jsDate.GetSeconds(pRuntime->GetIsolate())); | |
216 break; | 210 break; |
217 case 1: | 211 case 1: |
218 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", | 212 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", |
219 jsDate.GetYear(pRuntime->GetIsolate()), | 213 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
220 jsDate.GetMonth(pRuntime->GetIsolate()) + 1, | 214 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
221 jsDate.GetDay(pRuntime->GetIsolate()), | 215 jsDate.GetMinutes(pRuntime), |
222 jsDate.GetHours(pRuntime->GetIsolate()), | 216 jsDate.GetSeconds(pRuntime)); |
223 jsDate.GetMinutes(pRuntime->GetIsolate()), | |
224 jsDate.GetSeconds(pRuntime->GetIsolate())); | |
225 break; | 217 break; |
226 case 2: | 218 case 2: |
227 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", | 219 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", |
228 jsDate.GetYear(pRuntime->GetIsolate()), | 220 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
229 jsDate.GetMonth(pRuntime->GetIsolate()) + 1, | 221 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
230 jsDate.GetDay(pRuntime->GetIsolate()), | 222 jsDate.GetMinutes(pRuntime), |
231 jsDate.GetHours(pRuntime->GetIsolate()), | 223 jsDate.GetSeconds(pRuntime)); |
232 jsDate.GetMinutes(pRuntime->GetIsolate()), | |
233 jsDate.GetSeconds(pRuntime->GetIsolate())); | |
234 break; | 224 break; |
235 default: | 225 default: |
236 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR); | 226 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR); |
237 return FALSE; | 227 return FALSE; |
238 } | 228 } |
239 | 229 |
240 vRet = CJS_Value(pRuntime, swResult.c_str()); | 230 vRet = CJS_Value(pRuntime, swResult.c_str()); |
241 return TRUE; | 231 return TRUE; |
242 } | 232 } |
243 | 233 |
244 if (p1.GetType() == CJS_Value::VT_string) { | 234 if (p1.GetType() == CJS_Value::VT_string) { |
245 if (iSize > 2 && params[2].ToBool(pRuntime->GetIsolate())) { | 235 if (iSize > 2 && params[2].ToBool(pRuntime)) { |
246 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT); | 236 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT); |
247 return FALSE; // currently, it doesn't support XFAPicture. | 237 return FALSE; // currently, it doesn't support XFAPicture. |
248 } | 238 } |
249 | 239 |
250 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any | 240 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any |
251 // pre-existing %-directives before inserting our own. | 241 // pre-existing %-directives before inserting our own. |
252 std::basic_string<wchar_t> cFormat = | 242 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str(); |
253 p1.ToCFXWideString(pRuntime->GetIsolate()).c_str(); | |
254 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), | 243 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), |
255 cFormat.end()); | 244 cFormat.end()); |
256 | 245 |
257 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { | 246 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { |
258 int iStart = 0; | 247 int iStart = 0; |
259 int iEnd; | 248 int iEnd; |
260 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != | 249 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != |
261 -1) { | 250 -1) { |
262 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), | 251 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), |
263 TbConvertTable[i].lpszCppMark); | 252 TbConvertTable[i].lpszCppMark); |
264 iStart = iEnd; | 253 iStart = iEnd; |
265 } | 254 } |
266 } | 255 } |
267 | 256 |
268 int iYear = jsDate.GetYear(pRuntime->GetIsolate()); | 257 int iYear = jsDate.GetYear(pRuntime); |
269 int iMonth = jsDate.GetMonth(pRuntime->GetIsolate()); | 258 int iMonth = jsDate.GetMonth(pRuntime); |
270 int iDay = jsDate.GetDay(pRuntime->GetIsolate()); | 259 int iDay = jsDate.GetDay(pRuntime); |
271 int iHour = jsDate.GetHours(pRuntime->GetIsolate()); | 260 int iHour = jsDate.GetHours(pRuntime); |
272 int iMin = jsDate.GetMinutes(pRuntime->GetIsolate()); | 261 int iMin = jsDate.GetMinutes(pRuntime); |
273 int iSec = jsDate.GetSeconds(pRuntime->GetIsolate()); | 262 int iSec = jsDate.GetSeconds(pRuntime); |
274 | 263 |
275 TbConvertAdditional cTableAd[] = { | 264 TbConvertAdditional cTableAd[] = { |
276 {L"m", iMonth + 1}, {L"d", iDay}, | 265 {L"m", iMonth + 1}, {L"d", iDay}, |
277 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, | 266 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, |
278 {L"M", iMin}, {L"s", iSec}, | 267 {L"M", iMin}, {L"s", iSec}, |
279 }; | 268 }; |
280 | 269 |
281 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { | 270 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { |
282 wchar_t tszValue[16]; | 271 wchar_t tszValue[16]; |
283 CFX_WideString sValue; | 272 CFX_WideString sValue; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 const std::vector<CJS_Value>& params, | 312 const std::vector<CJS_Value>& params, |
324 CJS_Value& vRet, | 313 CJS_Value& vRet, |
325 CFX_WideString& sError) { | 314 CFX_WideString& sError) { |
326 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 315 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
327 | 316 |
328 if (params.size() < 2) { | 317 if (params.size() < 2) { |
329 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); | 318 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); |
330 return FALSE; | 319 return FALSE; |
331 } | 320 } |
332 | 321 |
333 vRet = CJS_Value(pRuntime, | 322 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), |
334 printx(params[0].ToCFXWideString(pRuntime->GetIsolate()), | 323 params[1].ToCFXWideString(pRuntime)) |
335 params[1].ToCFXWideString(pRuntime->GetIsolate())) | 324 .c_str()); |
336 .c_str()); | |
337 | 325 |
338 return TRUE; | 326 return TRUE; |
339 } | 327 } |
340 | 328 |
341 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; | 329 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
342 | 330 |
343 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { | 331 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
344 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') | 332 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') |
345 return input | 0x20; | 333 return input | 0x20; |
346 if (eMode == kUpperCase && input >= 'a' && input <= 'z') | 334 if (eMode == kUpperCase && input >= 'a' && input <= 'z') |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 429 |
442 FX_BOOL util::scand(IJS_Context* cc, | 430 FX_BOOL util::scand(IJS_Context* cc, |
443 const std::vector<CJS_Value>& params, | 431 const std::vector<CJS_Value>& params, |
444 CJS_Value& vRet, | 432 CJS_Value& vRet, |
445 CFX_WideString& sError) { | 433 CFX_WideString& sError) { |
446 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 434 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
447 int iSize = params.size(); | 435 int iSize = params.size(); |
448 if (iSize < 2) | 436 if (iSize < 2) |
449 return FALSE; | 437 return FALSE; |
450 | 438 |
451 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime->GetIsolate()); | 439 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
452 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime->GetIsolate()); | 440 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime); |
453 double dDate = JS_GetDateTime(); | 441 double dDate = JS_GetDateTime(); |
454 if (sDate.GetLength() > 0) { | 442 if (sDate.GetLength() > 0) { |
455 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); | 443 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
456 } | 444 } |
457 | 445 |
458 if (!JS_PortIsNan(dDate)) { | 446 if (!JS_PortIsNan(dDate)) { |
459 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime->GetIsolate(), dDate)); | 447 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); |
460 } else { | 448 } else { |
461 vRet.SetNull(pRuntime); | 449 vRet.SetNull(pRuntime); |
462 } | 450 } |
463 | 451 |
464 return TRUE; | 452 return TRUE; |
465 } | 453 } |
466 | 454 |
467 FX_BOOL util::byteToChar(IJS_Context* cc, | 455 FX_BOOL util::byteToChar(IJS_Context* cc, |
468 const std::vector<CJS_Value>& params, | 456 const std::vector<CJS_Value>& params, |
469 CJS_Value& vRet, | 457 CJS_Value& vRet, |
470 CFX_WideString& sError) { | 458 CFX_WideString& sError) { |
471 CJS_Context* pContext = static_cast<CJS_Context*>(cc); | 459 CJS_Context* pContext = static_cast<CJS_Context*>(cc); |
472 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 460 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
473 | 461 |
474 if (params.size() < 1) { | 462 if (params.size() < 1) { |
475 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 463 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
476 return FALSE; | 464 return FALSE; |
477 } | 465 } |
478 | 466 |
479 int arg = params[0].ToInt(pRuntime->GetIsolate()); | 467 int arg = params[0].ToInt(pRuntime); |
480 if (arg < 0 || arg > 255) { | 468 if (arg < 0 || arg > 255) { |
481 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR); | 469 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR); |
482 return FALSE; | 470 return FALSE; |
483 } | 471 } |
484 | 472 |
485 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); | 473 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); |
486 vRet = CJS_Value(pRuntime, wStr.c_str()); | 474 vRet = CJS_Value(pRuntime, wStr.c_str()); |
487 return TRUE; | 475 return TRUE; |
488 } | 476 } |
OLD | NEW |