| 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 CFX_WideString& sError) { | 183 CFX_WideString& sError) { |
| 184 int iSize = params.size(); | 184 int iSize = params.size(); |
| 185 if (iSize < 2) | 185 if (iSize < 2) |
| 186 return FALSE; | 186 return FALSE; |
| 187 | 187 |
| 188 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 188 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 189 CJS_Value p1 = params[0]; | 189 CJS_Value p1 = params[0]; |
| 190 CJS_Value p2 = params[1]; | 190 CJS_Value p2 = params[1]; |
| 191 CJS_Date jsDate; | 191 CJS_Date jsDate; |
| 192 if (!p2.ConvertToDate(pRuntime, jsDate)) { | 192 if (!p2.ConvertToDate(pRuntime, jsDate)) { |
| 193 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); | 193 sError = JSGetStringFromID(IDS_STRING_JSPRINT1); |
| 194 return FALSE; | 194 return FALSE; |
| 195 } | 195 } |
| 196 | 196 |
| 197 if (!jsDate.IsValidDate(pRuntime)) { | 197 if (!jsDate.IsValidDate(pRuntime)) { |
| 198 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); | 198 sError = JSGetStringFromID(IDS_STRING_JSPRINT2); |
| 199 return FALSE; | 199 return FALSE; |
| 200 } | 200 } |
| 201 | 201 |
| 202 if (p1.GetType() == CJS_Value::VT_number) { | 202 if (p1.GetType() == CJS_Value::VT_number) { |
| 203 CFX_WideString swResult; | 203 CFX_WideString swResult; |
| 204 switch (p1.ToInt(pRuntime)) { | 204 switch (p1.ToInt(pRuntime)) { |
| 205 case 0: | 205 case 0: |
| 206 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), | 206 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), |
| 207 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), | 207 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), |
| 208 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), | 208 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), |
| 209 jsDate.GetSeconds(pRuntime)); | 209 jsDate.GetSeconds(pRuntime)); |
| 210 break; | 210 break; |
| 211 case 1: | 211 case 1: |
| 212 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", | 212 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", |
| 213 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, | 213 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 214 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), | 214 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 215 jsDate.GetMinutes(pRuntime), | 215 jsDate.GetMinutes(pRuntime), |
| 216 jsDate.GetSeconds(pRuntime)); | 216 jsDate.GetSeconds(pRuntime)); |
| 217 break; | 217 break; |
| 218 case 2: | 218 case 2: |
| 219 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", | 219 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", |
| 220 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, | 220 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 221 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), | 221 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 222 jsDate.GetMinutes(pRuntime), | 222 jsDate.GetMinutes(pRuntime), |
| 223 jsDate.GetSeconds(pRuntime)); | 223 jsDate.GetSeconds(pRuntime)); |
| 224 break; | 224 break; |
| 225 default: | 225 default: |
| 226 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSVALUEERROR); | 226 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
| 227 return FALSE; | 227 return FALSE; |
| 228 } | 228 } |
| 229 | 229 |
| 230 vRet = CJS_Value(pRuntime, swResult.c_str()); | 230 vRet = CJS_Value(pRuntime, swResult.c_str()); |
| 231 return TRUE; | 231 return TRUE; |
| 232 } | 232 } |
| 233 | 233 |
| 234 if (p1.GetType() == CJS_Value::VT_string) { | 234 if (p1.GetType() == CJS_Value::VT_string) { |
| 235 if (iSize > 2 && params[2].ToBool(pRuntime)) { | 235 if (iSize > 2 && params[2].ToBool(pRuntime)) { |
| 236 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_NOTSUPPORT); | 236 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT); |
| 237 return FALSE; // currently, it doesn't support XFAPicture. | 237 return FALSE; // currently, it doesn't support XFAPicture. |
| 238 } | 238 } |
| 239 | 239 |
| 240 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any | 240 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any |
| 241 // pre-existing %-directives before inserting our own. | 241 // pre-existing %-directives before inserting our own. |
| 242 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str(); | 242 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str(); |
| 243 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), | 243 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), |
| 244 cFormat.end()); | 244 cFormat.end()); |
| 245 | 245 |
| 246 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { | 246 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 time.tm_min = iMin; | 296 time.tm_min = iMin; |
| 297 time.tm_sec = iSec; | 297 time.tm_sec = iSec; |
| 298 | 298 |
| 299 wchar_t buf[64] = {}; | 299 wchar_t buf[64] = {}; |
| 300 wcsftime(buf, 64, cFormat.c_str(), &time); | 300 wcsftime(buf, 64, cFormat.c_str(), &time); |
| 301 cFormat = buf; | 301 cFormat = buf; |
| 302 vRet = CJS_Value(pRuntime, cFormat.c_str()); | 302 vRet = CJS_Value(pRuntime, cFormat.c_str()); |
| 303 return TRUE; | 303 return TRUE; |
| 304 } | 304 } |
| 305 | 305 |
| 306 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR); | 306 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR); |
| 307 return FALSE; | 307 return FALSE; |
| 308 } | 308 } |
| 309 | 309 |
| 310 | 310 |
| 311 FX_BOOL util::printx(IJS_Context* cc, | 311 FX_BOOL util::printx(IJS_Context* cc, |
| 312 const std::vector<CJS_Value>& params, | 312 const std::vector<CJS_Value>& params, |
| 313 CJS_Value& vRet, | 313 CJS_Value& vRet, |
| 314 CFX_WideString& sError) { | 314 CFX_WideString& sError) { |
| 315 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | |
| 316 | |
| 317 if (params.size() < 2) { | 315 if (params.size() < 2) { |
| 318 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); | 316 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
| 319 return FALSE; | 317 return FALSE; |
| 320 } | 318 } |
| 321 | 319 |
| 320 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 322 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), | 321 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), |
| 323 params[1].ToCFXWideString(pRuntime)) | 322 params[1].ToCFXWideString(pRuntime)) |
| 324 .c_str()); | 323 .c_str()); |
| 325 | 324 |
| 326 return TRUE; | 325 return TRUE; |
| 327 } | 326 } |
| 328 | 327 |
| 329 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; | 328 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
| 330 | 329 |
| 331 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { | 330 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 vRet.SetNull(pRuntime); | 448 vRet.SetNull(pRuntime); |
| 450 } | 449 } |
| 451 | 450 |
| 452 return TRUE; | 451 return TRUE; |
| 453 } | 452 } |
| 454 | 453 |
| 455 FX_BOOL util::byteToChar(IJS_Context* cc, | 454 FX_BOOL util::byteToChar(IJS_Context* cc, |
| 456 const std::vector<CJS_Value>& params, | 455 const std::vector<CJS_Value>& params, |
| 457 CJS_Value& vRet, | 456 CJS_Value& vRet, |
| 458 CFX_WideString& sError) { | 457 CFX_WideString& sError) { |
| 459 CJS_Context* pContext = static_cast<CJS_Context*>(cc); | |
| 460 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | |
| 461 | |
| 462 if (params.size() < 1) { | 458 if (params.size() < 1) { |
| 463 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 459 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
| 464 return FALSE; | 460 return FALSE; |
| 465 } | 461 } |
| 466 | 462 |
| 463 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 467 int arg = params[0].ToInt(pRuntime); | 464 int arg = params[0].ToInt(pRuntime); |
| 468 if (arg < 0 || arg > 255) { | 465 if (arg < 0 || arg > 255) { |
| 469 sError = JSGetStringFromID(pContext, IDS_STRING_JSVALUEERROR); | 466 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
| 470 return FALSE; | 467 return FALSE; |
| 471 } | 468 } |
| 472 | 469 |
| 473 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); | 470 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); |
| 474 vRet = CJS_Value(pRuntime, wStr.c_str()); | 471 vRet = CJS_Value(pRuntime, wStr.c_str()); |
| 475 return TRUE; | 472 return TRUE; |
| 476 } | 473 } |
| OLD | NEW |