| OLD | NEW |
| (Empty) |
| 1 // 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. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "fpdfsdk/src/javascript/util.h" | |
| 8 | |
| 9 #include <time.h> | |
| 10 | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "core/include/fxcrt/fx_ext.h" | |
| 15 #include "fpdfsdk/include/javascript/IJavaScript.h" | |
| 16 #include "fpdfsdk/src/javascript/JS_Context.h" | |
| 17 #include "fpdfsdk/src/javascript/JS_Define.h" | |
| 18 #include "fpdfsdk/src/javascript/JS_EventHandler.h" | |
| 19 #include "fpdfsdk/src/javascript/JS_Object.h" | |
| 20 #include "fpdfsdk/src/javascript/JS_Runtime.h" | |
| 21 #include "fpdfsdk/src/javascript/JS_Value.h" | |
| 22 #include "fpdfsdk/src/javascript/PublicMethods.h" | |
| 23 #include "fpdfsdk/src/javascript/resource.h" | |
| 24 | |
| 25 #if _FX_OS_ == _FX_ANDROID_ | |
| 26 #include <ctype.h> | |
| 27 #endif | |
| 28 | |
| 29 BEGIN_JS_STATIC_CONST(CJS_Util) | |
| 30 END_JS_STATIC_CONST() | |
| 31 | |
| 32 BEGIN_JS_STATIC_PROP(CJS_Util) | |
| 33 END_JS_STATIC_PROP() | |
| 34 | |
| 35 BEGIN_JS_STATIC_METHOD(CJS_Util) | |
| 36 JS_STATIC_METHOD_ENTRY(printd) | |
| 37 JS_STATIC_METHOD_ENTRY(printf) | |
| 38 JS_STATIC_METHOD_ENTRY(printx) | |
| 39 JS_STATIC_METHOD_ENTRY(scand) | |
| 40 JS_STATIC_METHOD_ENTRY(byteToChar) | |
| 41 END_JS_STATIC_METHOD() | |
| 42 | |
| 43 IMPLEMENT_JS_CLASS(CJS_Util, util) | |
| 44 | |
| 45 util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} | |
| 46 | |
| 47 util::~util() { | |
| 48 } | |
| 49 | |
| 50 struct stru_TbConvert { | |
| 51 const FX_WCHAR* lpszJSMark; | |
| 52 const FX_WCHAR* lpszCppMark; | |
| 53 }; | |
| 54 | |
| 55 const stru_TbConvert fcTable[] = { | |
| 56 {L"mmmm", L"%B"}, | |
| 57 {L"mmm", L"%b"}, | |
| 58 {L"mm", L"%m"}, | |
| 59 // "m" | |
| 60 {L"dddd", L"%A"}, | |
| 61 {L"ddd", L"%a"}, | |
| 62 {L"dd", L"%d"}, | |
| 63 // "d", "%w", | |
| 64 {L"yyyy", L"%Y"}, | |
| 65 {L"yy", L"%y"}, | |
| 66 {L"HH", L"%H"}, | |
| 67 // "H" | |
| 68 {L"hh", L"%I"}, | |
| 69 // "h" | |
| 70 {L"MM", L"%M"}, | |
| 71 // "M" | |
| 72 {L"ss", L"%S"}, | |
| 73 // "s | |
| 74 {L"TT", L"%p"}, | |
| 75 // "t" | |
| 76 #if defined(_WIN32) | |
| 77 {L"tt", L"%p"}, | |
| 78 {L"h", L"%#I"}, | |
| 79 #else | |
| 80 {L"tt", L"%P"}, | |
| 81 {L"h", L"%l"}, | |
| 82 #endif | |
| 83 }; | |
| 84 | |
| 85 #define UTIL_INT 0 | |
| 86 #define UTIL_DOUBLE 1 | |
| 87 #define UTIL_STRING 2 | |
| 88 | |
| 89 int util::ParstDataType(std::wstring* sFormat) { | |
| 90 bool bPercent = FALSE; | |
| 91 for (size_t i = 0; i < sFormat->length(); ++i) { | |
| 92 wchar_t c = (*sFormat)[i]; | |
| 93 if (c == L'%') { | |
| 94 bPercent = true; | |
| 95 continue; | |
| 96 } | |
| 97 | |
| 98 if (bPercent) { | |
| 99 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || | |
| 100 c == L'u' || c == L'x' || c == L'X') { | |
| 101 return UTIL_INT; | |
| 102 } | |
| 103 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') { | |
| 104 return UTIL_DOUBLE; | |
| 105 } | |
| 106 if (c == L's' || c == L'S') { | |
| 107 // Map s to S since we always deal internally | |
| 108 // with wchar_t strings. | |
| 109 (*sFormat)[i] = L'S'; | |
| 110 return UTIL_STRING; | |
| 111 } | |
| 112 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || | |
| 113 FXSYS_iswdigit(c)) { | |
| 114 continue; | |
| 115 } | |
| 116 break; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 return -1; | |
| 121 } | |
| 122 | |
| 123 FX_BOOL util::printf(IJS_Context* cc, | |
| 124 const std::vector<CJS_Value>& params, | |
| 125 CJS_Value& vRet, | |
| 126 CFX_WideString& sError) { | |
| 127 int iSize = params.size(); | |
| 128 if (iSize < 1) | |
| 129 return FALSE; | |
| 130 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str()); | |
| 131 std::vector<std::wstring> c_strConvers; | |
| 132 int iOffset = 0; | |
| 133 int iOffend = 0; | |
| 134 c_ConvChar.insert(c_ConvChar.begin(), L'S'); | |
| 135 while (iOffset != -1) { | |
| 136 iOffend = c_ConvChar.find(L"%", iOffset + 1); | |
| 137 std::wstring strSub; | |
| 138 if (iOffend == -1) | |
| 139 strSub = c_ConvChar.substr(iOffset); | |
| 140 else | |
| 141 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset); | |
| 142 c_strConvers.push_back(strSub); | |
| 143 iOffset = iOffend; | |
| 144 } | |
| 145 | |
| 146 std::wstring c_strResult; | |
| 147 | |
| 148 // for(int iIndex = 1;iIndex < params.size();iIndex++) | |
| 149 std::wstring c_strFormat; | |
| 150 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) { | |
| 151 c_strFormat = c_strConvers[iIndex]; | |
| 152 if (iIndex == 0) { | |
| 153 c_strResult = c_strFormat; | |
| 154 continue; | |
| 155 } | |
| 156 | |
| 157 CFX_WideString strSegment; | |
| 158 if (iIndex >= iSize) { | |
| 159 c_strResult += c_strFormat; | |
| 160 continue; | |
| 161 } | |
| 162 | |
| 163 switch (ParstDataType(&c_strFormat)) { | |
| 164 case UTIL_INT: | |
| 165 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt()); | |
| 166 break; | |
| 167 case UTIL_DOUBLE: | |
| 168 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble()); | |
| 169 break; | |
| 170 case UTIL_STRING: | |
| 171 strSegment.Format(c_strFormat.c_str(), | |
| 172 params[iIndex].ToCFXWideString().c_str()); | |
| 173 break; | |
| 174 default: | |
| 175 strSegment.Format(L"%S", c_strFormat.c_str()); | |
| 176 break; | |
| 177 } | |
| 178 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); | |
| 179 } | |
| 180 | |
| 181 c_strResult.erase(c_strResult.begin()); | |
| 182 vRet = c_strResult.c_str(); | |
| 183 return TRUE; | |
| 184 } | |
| 185 | |
| 186 FX_BOOL util::printd(IJS_Context* cc, | |
| 187 const std::vector<CJS_Value>& params, | |
| 188 CJS_Value& vRet, | |
| 189 CFX_WideString& sError) { | |
| 190 int iSize = params.size(); | |
| 191 if (iSize < 2) | |
| 192 return FALSE; | |
| 193 | |
| 194 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | |
| 195 CJS_Value p1(pRuntime); | |
| 196 p1 = params[0]; | |
| 197 | |
| 198 CJS_Value p2 = params[1]; | |
| 199 CJS_Date jsDate(pRuntime); | |
| 200 if (!p2.ConvertToDate(jsDate)) { | |
| 201 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); | |
| 202 return FALSE; | |
| 203 } | |
| 204 | |
| 205 if (!jsDate.IsValidDate()) { | |
| 206 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); | |
| 207 return FALSE; | |
| 208 } | |
| 209 | |
| 210 if (p1.GetType() == CJS_Value::VT_number) { | |
| 211 int nFormat = p1.ToInt(); | |
| 212 CFX_WideString swResult; | |
| 213 | |
| 214 switch (nFormat) { | |
| 215 case 0: | |
| 216 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(), | |
| 217 jsDate.GetMonth() + 1, jsDate.GetDay(), | |
| 218 jsDate.GetHours(), jsDate.GetMinutes(), | |
| 219 jsDate.GetSeconds()); | |
| 220 break; | |
| 221 case 1: | |
| 222 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(), | |
| 223 jsDate.GetMonth() + 1, jsDate.GetDay(), | |
| 224 jsDate.GetHours(), jsDate.GetMinutes(), | |
| 225 jsDate.GetSeconds()); | |
| 226 break; | |
| 227 case 2: | |
| 228 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(), | |
| 229 jsDate.GetMonth() + 1, jsDate.GetDay(), | |
| 230 jsDate.GetHours(), jsDate.GetMinutes(), | |
| 231 jsDate.GetSeconds()); | |
| 232 break; | |
| 233 default: | |
| 234 return FALSE; | |
| 235 } | |
| 236 | |
| 237 vRet = swResult.c_str(); | |
| 238 return TRUE; | |
| 239 } | |
| 240 if (p1.GetType() == CJS_Value::VT_string) { | |
| 241 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str(); | |
| 242 | |
| 243 bool bXFAPicture = false; | |
| 244 if (iSize > 2) { | |
| 245 bXFAPicture = params[2].ToBool(); | |
| 246 } | |
| 247 | |
| 248 if (bXFAPicture) { | |
| 249 return FALSE; // currently, it doesn't support XFAPicture. | |
| 250 } | |
| 251 | |
| 252 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { | |
| 253 int iStart = 0; | |
| 254 int iEnd; | |
| 255 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { | |
| 256 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), | |
| 257 fcTable[i].lpszCppMark); | |
| 258 iStart = iEnd; | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 int iYear, iMonth, iDay, iHour, iMin, iSec; | |
| 263 iYear = jsDate.GetYear(); | |
| 264 iMonth = jsDate.GetMonth(); | |
| 265 iDay = jsDate.GetDay(); | |
| 266 iHour = jsDate.GetHours(); | |
| 267 iMin = jsDate.GetMinutes(); | |
| 268 iSec = jsDate.GetSeconds(); | |
| 269 | |
| 270 struct tm time = {}; | |
| 271 time.tm_year = iYear - 1900; | |
| 272 time.tm_mon = iMonth; | |
| 273 time.tm_mday = iDay; | |
| 274 time.tm_hour = iHour; | |
| 275 time.tm_min = iMin; | |
| 276 time.tm_sec = iSec; | |
| 277 | |
| 278 struct stru_TbConvertAd { | |
| 279 const FX_WCHAR* lpszJSMark; | |
| 280 int iValue; | |
| 281 }; | |
| 282 | |
| 283 stru_TbConvertAd cTableAd[] = { | |
| 284 {L"m", iMonth + 1}, {L"d", iDay}, | |
| 285 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, | |
| 286 {L"M", iMin}, {L"s", iSec}, | |
| 287 }; | |
| 288 | |
| 289 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { | |
| 290 wchar_t tszValue[10]; | |
| 291 CFX_WideString sValue; | |
| 292 sValue.Format(L"%d", cTableAd[i].iValue); | |
| 293 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), | |
| 294 (sValue.GetLength() + 1) * sizeof(wchar_t)); | |
| 295 | |
| 296 int iStart = 0; | |
| 297 int iEnd; | |
| 298 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { | |
| 299 if (iEnd > 0) { | |
| 300 if (cFormat[iEnd - 1] == L'%') { | |
| 301 iStart = iEnd + 1; | |
| 302 continue; | |
| 303 } | |
| 304 } | |
| 305 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); | |
| 306 iStart = iEnd; | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 CFX_WideString strFormat; | |
| 311 wchar_t buf[64] = {}; | |
| 312 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time); | |
| 313 cFormat = buf; | |
| 314 vRet = cFormat.c_str(); | |
| 315 return TRUE; | |
| 316 } | |
| 317 return FALSE; | |
| 318 } | |
| 319 | |
| 320 void util::printd(const std::wstring& cFormat2, | |
| 321 CJS_Date jsDate, | |
| 322 bool bXFAPicture, | |
| 323 std::wstring& cPurpose) { | |
| 324 std::wstring cFormat = cFormat2; | |
| 325 | |
| 326 if (bXFAPicture) { | |
| 327 return; // currently, it doesn't support XFAPicture. | |
| 328 } | |
| 329 | |
| 330 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { | |
| 331 int iStart = 0; | |
| 332 int iEnd; | |
| 333 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { | |
| 334 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), | |
| 335 fcTable[i].lpszCppMark); | |
| 336 iStart = iEnd; | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 int iYear, iMonth, iDay, iHour, iMin, iSec; | |
| 341 iYear = jsDate.GetYear(); | |
| 342 iMonth = jsDate.GetMonth(); | |
| 343 iDay = jsDate.GetDay(); | |
| 344 iHour = jsDate.GetHours(); | |
| 345 iMin = jsDate.GetMinutes(); | |
| 346 iSec = jsDate.GetSeconds(); | |
| 347 | |
| 348 struct tm time = {}; | |
| 349 time.tm_year = iYear - 1900; | |
| 350 time.tm_mon = iMonth; | |
| 351 time.tm_mday = iDay; | |
| 352 time.tm_hour = iHour; | |
| 353 time.tm_min = iMin; | |
| 354 time.tm_sec = iSec; | |
| 355 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec); | |
| 356 // CString strFormat = cppTm.Format(cFormat.c_str()); | |
| 357 | |
| 358 struct stru_TbConvertAd { | |
| 359 const FX_WCHAR* lpszJSMark; | |
| 360 int iValue; | |
| 361 }; | |
| 362 | |
| 363 stru_TbConvertAd cTableAd[] = { | |
| 364 {L"m", iMonth + 1}, {L"d", iDay}, | |
| 365 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, | |
| 366 {L"M", iMin}, {L"s", iSec}, | |
| 367 }; | |
| 368 | |
| 369 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1); | |
| 370 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { | |
| 371 wchar_t tszValue[10]; | |
| 372 CFX_WideString sValue; | |
| 373 sValue.Format(L"%d", cTableAd[i].iValue); | |
| 374 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), | |
| 375 sValue.GetLength() * sizeof(wchar_t)); | |
| 376 | |
| 377 int iStart = 0; | |
| 378 int iEnd; | |
| 379 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { | |
| 380 if (iEnd > 0) { | |
| 381 if (cFormat[iEnd - 1] == L'%') { | |
| 382 iStart = iEnd + 1; | |
| 383 continue; | |
| 384 } | |
| 385 } | |
| 386 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); | |
| 387 iStart = iEnd; | |
| 388 } | |
| 389 } | |
| 390 | |
| 391 CFX_WideString strFormat; | |
| 392 wchar_t buf[64] = {}; | |
| 393 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time); | |
| 394 cFormat = buf; | |
| 395 cPurpose = cFormat; | |
| 396 } | |
| 397 | |
| 398 FX_BOOL util::printx(IJS_Context* cc, | |
| 399 const std::vector<CJS_Value>& params, | |
| 400 CJS_Value& vRet, | |
| 401 CFX_WideString& sError) { | |
| 402 int iSize = params.size(); | |
| 403 if (iSize < 2) | |
| 404 return FALSE; | |
| 405 CFX_WideString sFormat = params[0].ToCFXWideString(); | |
| 406 CFX_WideString sSource = params[1].ToCFXWideString(); | |
| 407 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str(); | |
| 408 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str(); | |
| 409 std::string cDest; | |
| 410 printx(cFormat, cSource, cDest); | |
| 411 vRet = cDest.c_str(); | |
| 412 return TRUE; | |
| 413 } | |
| 414 | |
| 415 void util::printx(const std::string& cFormat, | |
| 416 const std::string& cSource2, | |
| 417 std::string& cPurpose) { | |
| 418 std::string cSource(cSource2); | |
| 419 if (!cPurpose.empty()) | |
| 420 // cPurpose.clear(); | |
| 421 cPurpose.erase(); | |
| 422 int itSource = 0; | |
| 423 int iSize = cSource.size(); | |
| 424 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize; | |
| 425 iIndex++) { | |
| 426 char letter = cFormat[iIndex]; | |
| 427 switch (letter) { | |
| 428 case '?': | |
| 429 cPurpose += cSource[itSource]; | |
| 430 itSource++; | |
| 431 break; | |
| 432 case 'X': { | |
| 433 while (itSource < iSize) { | |
| 434 if (std::isdigit(cSource[itSource]) || | |
| 435 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || | |
| 436 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { | |
| 437 cPurpose += cSource[itSource]; | |
| 438 itSource++; | |
| 439 break; | |
| 440 } | |
| 441 itSource++; | |
| 442 } | |
| 443 break; | |
| 444 } break; | |
| 445 case 'A': { | |
| 446 while (itSource < iSize) { | |
| 447 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || | |
| 448 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { | |
| 449 cPurpose += cSource[itSource]; | |
| 450 itSource++; | |
| 451 break; | |
| 452 } | |
| 453 itSource++; | |
| 454 } | |
| 455 break; | |
| 456 } break; | |
| 457 case '9': { | |
| 458 while (itSource < iSize) { | |
| 459 if (std::isdigit(cSource[itSource])) { | |
| 460 cPurpose += cSource[itSource]; | |
| 461 itSource++; | |
| 462 break; | |
| 463 } | |
| 464 itSource++; | |
| 465 } | |
| 466 break; | |
| 467 } | |
| 468 case '*': { | |
| 469 cPurpose.append(cSource, itSource, iSize - itSource); | |
| 470 itSource = iSize - 1; | |
| 471 break; | |
| 472 } | |
| 473 case '\\': | |
| 474 break; | |
| 475 case '>': { | |
| 476 for (char& c : cSource) | |
| 477 c = toupper(c); | |
| 478 break; | |
| 479 } | |
| 480 case '<': { | |
| 481 for (char& c : cSource) | |
| 482 c = tolower(c); | |
| 483 break; | |
| 484 } | |
| 485 case '=': | |
| 486 break; | |
| 487 default: | |
| 488 cPurpose += letter; | |
| 489 break; | |
| 490 } | |
| 491 } | |
| 492 } | |
| 493 | |
| 494 FX_BOOL util::scand(IJS_Context* cc, | |
| 495 const std::vector<CJS_Value>& params, | |
| 496 CJS_Value& vRet, | |
| 497 CFX_WideString& sError) { | |
| 498 int iSize = params.size(); | |
| 499 if (iSize < 2) | |
| 500 return FALSE; | |
| 501 | |
| 502 CFX_WideString sFormat = params[0].ToCFXWideString(); | |
| 503 CFX_WideString sDate = params[1].ToCFXWideString(); | |
| 504 double dDate = JS_GetDateTime(); | |
| 505 if (sDate.GetLength() > 0) { | |
| 506 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); | |
| 507 } | |
| 508 | |
| 509 if (!JS_PortIsNan(dDate)) { | |
| 510 vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate); | |
| 511 } else { | |
| 512 vRet.SetNull(); | |
| 513 } | |
| 514 | |
| 515 return TRUE; | |
| 516 } | |
| 517 | |
| 518 FX_BOOL util::byteToChar(IJS_Context* cc, | |
| 519 const std::vector<CJS_Value>& params, | |
| 520 CJS_Value& vRet, | |
| 521 CFX_WideString& sError) { | |
| 522 int iSize = params.size(); | |
| 523 if (iSize == 0) | |
| 524 return FALSE; | |
| 525 int nByte = params[0].ToInt(); | |
| 526 unsigned char cByte = (unsigned char)nByte; | |
| 527 CFX_WideString csValue; | |
| 528 csValue.Format(L"%c", cByte); | |
| 529 vRet = csValue.c_str(); | |
| 530 return TRUE; | |
| 531 } | |
| OLD | NEW |