| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } // namespace | 111 } // namespace |
| 112 | 112 |
| 113 util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} | 113 util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
| 114 | 114 |
| 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 int iSize = params.size(); | 122 int iSize = params.size(); |
| 122 if (iSize < 1) | 123 if (iSize < 1) |
| 123 return FALSE; | 124 return FALSE; |
| 124 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str()); | 125 std::wstring c_ConvChar( |
| 126 params[0].ToCFXWideString(pRuntime->GetIsolate()).c_str()); |
| 125 std::vector<std::wstring> c_strConvers; | 127 std::vector<std::wstring> c_strConvers; |
| 126 int iOffset = 0; | 128 int iOffset = 0; |
| 127 int iOffend = 0; | 129 int iOffend = 0; |
| 128 c_ConvChar.insert(c_ConvChar.begin(), L'S'); | 130 c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
| 129 while (iOffset != -1) { | 131 while (iOffset != -1) { |
| 130 iOffend = c_ConvChar.find(L"%", iOffset + 1); | 132 iOffend = c_ConvChar.find(L"%", iOffset + 1); |
| 131 std::wstring strSub; | 133 std::wstring strSub; |
| 132 if (iOffend == -1) | 134 if (iOffend == -1) |
| 133 strSub = c_ConvChar.substr(iOffset); | 135 strSub = c_ConvChar.substr(iOffset); |
| 134 else | 136 else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 147 } | 149 } |
| 148 | 150 |
| 149 CFX_WideString strSegment; | 151 CFX_WideString strSegment; |
| 150 if (iIndex >= iSize) { | 152 if (iIndex >= iSize) { |
| 151 c_strResult += c_strFormat; | 153 c_strResult += c_strFormat; |
| 152 continue; | 154 continue; |
| 153 } | 155 } |
| 154 | 156 |
| 155 switch (ParseDataType(&c_strFormat)) { | 157 switch (ParseDataType(&c_strFormat)) { |
| 156 case UTIL_INT: | 158 case UTIL_INT: |
| 157 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt()); | 159 strSegment.Format(c_strFormat.c_str(), |
| 160 params[iIndex].ToInt(pRuntime->GetIsolate())); |
| 158 break; | 161 break; |
| 159 case UTIL_DOUBLE: | 162 case UTIL_DOUBLE: |
| 160 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble()); | 163 strSegment.Format(c_strFormat.c_str(), |
| 164 params[iIndex].ToDouble(pRuntime->GetIsolate())); |
| 161 break; | 165 break; |
| 162 case UTIL_STRING: | 166 case UTIL_STRING: |
| 163 strSegment.Format(c_strFormat.c_str(), | 167 strSegment.Format( |
| 164 params[iIndex].ToCFXWideString().c_str()); | 168 c_strFormat.c_str(), |
| 169 params[iIndex].ToCFXWideString(pRuntime->GetIsolate()).c_str()); |
| 165 break; | 170 break; |
| 166 default: | 171 default: |
| 167 strSegment.Format(L"%S", c_strFormat.c_str()); | 172 strSegment.Format(L"%S", c_strFormat.c_str()); |
| 168 break; | 173 break; |
| 169 } | 174 } |
| 170 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); | 175 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); |
| 171 } | 176 } |
| 172 | 177 |
| 173 c_strResult.erase(c_strResult.begin()); | 178 c_strResult.erase(c_strResult.begin()); |
| 174 vRet = c_strResult.c_str(); | 179 vRet = CJS_Value(pRuntime, c_strResult.c_str()); |
| 175 return TRUE; | 180 return TRUE; |
| 176 } | 181 } |
| 177 | 182 |
| 178 FX_BOOL util::printd(IJS_Context* cc, | 183 FX_BOOL util::printd(IJS_Context* cc, |
| 179 const std::vector<CJS_Value>& params, | 184 const std::vector<CJS_Value>& params, |
| 180 CJS_Value& vRet, | 185 CJS_Value& vRet, |
| 181 CFX_WideString& sError) { | 186 CFX_WideString& sError) { |
| 182 int iSize = params.size(); | 187 int iSize = params.size(); |
| 183 if (iSize < 2) | 188 if (iSize < 2) |
| 184 return FALSE; | 189 return FALSE; |
| 185 | 190 |
| 186 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 191 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 187 CJS_Value p1 = params[0]; | 192 CJS_Value p1 = params[0]; |
| 188 CJS_Value p2 = params[1]; | 193 CJS_Value p2 = params[1]; |
| 189 CJS_Date jsDate; | 194 CJS_Date jsDate; |
| 190 if (!p2.ConvertToDate(jsDate)) { | 195 if (!p2.ConvertToDate(pRuntime->GetIsolate(), jsDate)) { |
| 191 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); | 196 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); |
| 192 return FALSE; | 197 return FALSE; |
| 193 } | 198 } |
| 194 | 199 |
| 195 if (!jsDate.IsValidDate(pRuntime->GetIsolate())) { | 200 if (!jsDate.IsValidDate(pRuntime->GetIsolate())) { |
| 196 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); | 201 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); |
| 197 return FALSE; | 202 return FALSE; |
| 198 } | 203 } |
| 199 | 204 |
| 200 if (p1.GetType() == CJS_Value::VT_number) { | 205 if (p1.GetType() == CJS_Value::VT_number) { |
| 201 CFX_WideString swResult; | 206 CFX_WideString swResult; |
| 202 switch (p1.ToInt()) { | 207 switch (p1.ToInt(pRuntime->GetIsolate())) { |
| 203 case 0: | 208 case 0: |
| 204 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", | 209 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", |
| 205 jsDate.GetYear(pRuntime->GetIsolate()), | 210 jsDate.GetYear(pRuntime->GetIsolate()), |
| 206 jsDate.GetMonth(pRuntime->GetIsolate()) + 1, | 211 jsDate.GetMonth(pRuntime->GetIsolate()) + 1, |
| 207 jsDate.GetDay(pRuntime->GetIsolate()), | 212 jsDate.GetDay(pRuntime->GetIsolate()), |
| 208 jsDate.GetHours(pRuntime->GetIsolate()), | 213 jsDate.GetHours(pRuntime->GetIsolate()), |
| 209 jsDate.GetMinutes(pRuntime->GetIsolate()), | 214 jsDate.GetMinutes(pRuntime->GetIsolate()), |
| 210 jsDate.GetSeconds(pRuntime->GetIsolate())); | 215 jsDate.GetSeconds(pRuntime->GetIsolate())); |
| 211 break; | 216 break; |
| 212 case 1: | 217 case 1: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 225 jsDate.GetDay(pRuntime->GetIsolate()), | 230 jsDate.GetDay(pRuntime->GetIsolate()), |
| 226 jsDate.GetHours(pRuntime->GetIsolate()), | 231 jsDate.GetHours(pRuntime->GetIsolate()), |
| 227 jsDate.GetMinutes(pRuntime->GetIsolate()), | 232 jsDate.GetMinutes(pRuntime->GetIsolate()), |
| 228 jsDate.GetSeconds(pRuntime->GetIsolate())); | 233 jsDate.GetSeconds(pRuntime->GetIsolate())); |
| 229 break; | 234 break; |
| 230 default: | 235 default: |
| 231 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR); | 236 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR); |
| 232 return FALSE; | 237 return FALSE; |
| 233 } | 238 } |
| 234 | 239 |
| 235 vRet = swResult.c_str(); | 240 vRet = CJS_Value(pRuntime, swResult.c_str()); |
| 236 return TRUE; | 241 return TRUE; |
| 237 } | 242 } |
| 238 | 243 |
| 239 if (p1.GetType() == CJS_Value::VT_string) { | 244 if (p1.GetType() == CJS_Value::VT_string) { |
| 240 if (iSize > 2 && params[2].ToBool()) { | 245 if (iSize > 2 && params[2].ToBool(pRuntime->GetIsolate())) { |
| 241 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT); | 246 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT); |
| 242 return FALSE; // currently, it doesn't support XFAPicture. | 247 return FALSE; // currently, it doesn't support XFAPicture. |
| 243 } | 248 } |
| 244 | 249 |
| 245 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any | 250 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any |
| 246 // pre-existing %-directives before inserting our own. | 251 // pre-existing %-directives before inserting our own. |
| 247 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str(); | 252 std::basic_string<wchar_t> cFormat = |
| 253 p1.ToCFXWideString(pRuntime->GetIsolate()).c_str(); |
| 248 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), | 254 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), |
| 249 cFormat.end()); | 255 cFormat.end()); |
| 250 | 256 |
| 251 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { | 257 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { |
| 252 int iStart = 0; | 258 int iStart = 0; |
| 253 int iEnd; | 259 int iEnd; |
| 254 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != | 260 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != |
| 255 -1) { | 261 -1) { |
| 256 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), | 262 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), |
| 257 TbConvertTable[i].lpszCppMark); | 263 TbConvertTable[i].lpszCppMark); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 time.tm_year = iYear - 1900; | 303 time.tm_year = iYear - 1900; |
| 298 time.tm_mon = iMonth; | 304 time.tm_mon = iMonth; |
| 299 time.tm_mday = iDay; | 305 time.tm_mday = iDay; |
| 300 time.tm_hour = iHour; | 306 time.tm_hour = iHour; |
| 301 time.tm_min = iMin; | 307 time.tm_min = iMin; |
| 302 time.tm_sec = iSec; | 308 time.tm_sec = iSec; |
| 303 | 309 |
| 304 wchar_t buf[64] = {}; | 310 wchar_t buf[64] = {}; |
| 305 wcsftime(buf, 64, cFormat.c_str(), &time); | 311 wcsftime(buf, 64, cFormat.c_str(), &time); |
| 306 cFormat = buf; | 312 cFormat = buf; |
| 307 vRet = cFormat.c_str(); | 313 vRet = CJS_Value(pRuntime, cFormat.c_str()); |
| 308 return TRUE; | 314 return TRUE; |
| 309 } | 315 } |
| 310 | 316 |
| 311 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR); | 317 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR); |
| 312 return FALSE; | 318 return FALSE; |
| 313 } | 319 } |
| 314 | 320 |
| 315 | 321 |
| 316 FX_BOOL util::printx(IJS_Context* cc, | 322 FX_BOOL util::printx(IJS_Context* cc, |
| 317 const std::vector<CJS_Value>& params, | 323 const std::vector<CJS_Value>& params, |
| 318 CJS_Value& vRet, | 324 CJS_Value& vRet, |
| 319 CFX_WideString& sError) { | 325 CFX_WideString& sError) { |
| 326 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 327 |
| 320 if (params.size() < 2) { | 328 if (params.size() < 2) { |
| 321 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); | 329 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); |
| 322 return FALSE; | 330 return FALSE; |
| 323 } | 331 } |
| 324 vRet = | 332 |
| 325 printx(params[0].ToCFXWideString(), params[1].ToCFXWideString()).c_str(); | 333 vRet = CJS_Value(pRuntime, |
| 334 printx(params[0].ToCFXWideString(pRuntime->GetIsolate()), |
| 335 params[1].ToCFXWideString(pRuntime->GetIsolate())) |
| 336 .c_str()); |
| 337 |
| 326 return TRUE; | 338 return TRUE; |
| 327 } | 339 } |
| 328 | 340 |
| 329 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; | 341 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
| 330 | 342 |
| 331 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { | 343 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
| 332 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') | 344 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') |
| 333 return input | 0x20; | 345 return input | 0x20; |
| 334 if (eMode == kUpperCase && input >= 'a' && input <= 'z') | 346 if (eMode == kUpperCase && input >= 'a' && input <= 'z') |
| 335 return input & ~0x20; | 347 return input & ~0x20; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 } break; | 436 } break; |
| 425 } | 437 } |
| 426 } | 438 } |
| 427 return wsResult; | 439 return wsResult; |
| 428 } | 440 } |
| 429 | 441 |
| 430 FX_BOOL util::scand(IJS_Context* cc, | 442 FX_BOOL util::scand(IJS_Context* cc, |
| 431 const std::vector<CJS_Value>& params, | 443 const std::vector<CJS_Value>& params, |
| 432 CJS_Value& vRet, | 444 CJS_Value& vRet, |
| 433 CFX_WideString& sError) { | 445 CFX_WideString& sError) { |
| 446 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 434 int iSize = params.size(); | 447 int iSize = params.size(); |
| 435 if (iSize < 2) | 448 if (iSize < 2) |
| 436 return FALSE; | 449 return FALSE; |
| 437 | 450 |
| 438 CFX_WideString sFormat = params[0].ToCFXWideString(); | 451 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime->GetIsolate()); |
| 439 CFX_WideString sDate = params[1].ToCFXWideString(); | 452 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime->GetIsolate()); |
| 440 double dDate = JS_GetDateTime(); | 453 double dDate = JS_GetDateTime(); |
| 441 if (sDate.GetLength() > 0) { | 454 if (sDate.GetLength() > 0) { |
| 442 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); | 455 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
| 443 } | 456 } |
| 444 | 457 |
| 445 if (!JS_PortIsNan(dDate)) { | 458 if (!JS_PortIsNan(dDate)) { |
| 446 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 459 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime->GetIsolate(), dDate)); |
| 447 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime->GetIsolate(), dDate) | |
| 448 .ToV8Date(pRuntime->GetIsolate())); | |
| 449 } else { | 460 } else { |
| 450 vRet.SetNull(); | 461 vRet.SetNull(pRuntime); |
| 451 } | 462 } |
| 452 | 463 |
| 453 return TRUE; | 464 return TRUE; |
| 454 } | 465 } |
| 455 | 466 |
| 456 FX_BOOL util::byteToChar(IJS_Context* cc, | 467 FX_BOOL util::byteToChar(IJS_Context* cc, |
| 457 const std::vector<CJS_Value>& params, | 468 const std::vector<CJS_Value>& params, |
| 458 CJS_Value& vRet, | 469 CJS_Value& vRet, |
| 459 CFX_WideString& sError) { | 470 CFX_WideString& sError) { |
| 460 CJS_Context* pContext = static_cast<CJS_Context*>(cc); | 471 CJS_Context* pContext = static_cast<CJS_Context*>(cc); |
| 472 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 473 |
| 461 if (params.size() < 1) { | 474 if (params.size() < 1) { |
| 462 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 475 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
| 463 return FALSE; | 476 return FALSE; |
| 464 } | 477 } |
| 465 int arg = params[0].ToInt(); | 478 |
| 479 int arg = params[0].ToInt(pRuntime->GetIsolate()); |
| 466 if (arg < 0 || arg > 255) { | 480 if (arg < 0 || arg > 255) { |
| 467 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR); | 481 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR); |
| 468 return FALSE; | 482 return FALSE; |
| 469 } | 483 } |
| 484 |
| 470 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); | 485 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); |
| 471 vRet = wStr.c_str(); | 486 vRet = CJS_Value(pRuntime, wStr.c_str()); |
| 472 return TRUE; | 487 return TRUE; |
| 473 } | 488 } |
| OLD | NEW |