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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"}, | 68 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"}, |
69 {L"TT", L"%p"}, | 69 {L"TT", L"%p"}, |
70 #if defined(_WIN32) | 70 #if defined(_WIN32) |
71 {L"tt", L"%p"}, {L"h", L"%#I"}, | 71 {L"tt", L"%p"}, {L"h", L"%#I"}, |
72 #else | 72 #else |
73 {L"tt", L"%P"}, {L"h", L"%l"}, | 73 {L"tt", L"%P"}, {L"h", L"%l"}, |
74 #endif | 74 #endif |
75 }; | 75 }; |
76 | 76 |
77 int ParseDataType(std::wstring* sFormat) { | 77 int ParseDataType(std::wstring* sFormat) { |
78 bool bPercent = FALSE; | 78 bool bPercent = false; |
79 for (size_t i = 0; i < sFormat->length(); ++i) { | 79 for (size_t i = 0; i < sFormat->length(); ++i) { |
80 wchar_t c = (*sFormat)[i]; | 80 wchar_t c = (*sFormat)[i]; |
81 if (c == L'%') { | 81 if (c == L'%') { |
82 bPercent = true; | 82 bPercent = true; |
83 continue; | 83 continue; |
84 } | 84 } |
85 | 85 |
86 if (bPercent) { | 86 if (bPercent) { |
87 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || | 87 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || |
88 c == L'u' || c == L'x' || c == L'X') { | 88 c == L'u' || c == L'x' || c == L'X') { |
(...skipping 18 matching lines...) Expand all Loading... |
107 | 107 |
108 return -1; | 108 return -1; |
109 } | 109 } |
110 | 110 |
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 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(params[0].ToCFXWideString(pRuntime).c_str()); | 125 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str()); |
126 std::vector<std::wstring> c_strConvers; | 126 std::vector<std::wstring> c_strConvers; |
127 int iOffset = 0; | 127 int iOffset = 0; |
128 int iOffend = 0; | 128 int iOffend = 0; |
129 c_ConvChar.insert(c_ConvChar.begin(), L'S'); | 129 c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
130 while (iOffset != -1) { | 130 while (iOffset != -1) { |
131 iOffend = c_ConvChar.find(L"%", iOffset + 1); | 131 iOffend = c_ConvChar.find(L"%", iOffset + 1); |
132 std::wstring strSub; | 132 std::wstring strSub; |
133 if (iOffend == -1) | 133 if (iOffend == -1) |
134 strSub = c_ConvChar.substr(iOffset); | 134 strSub = c_ConvChar.substr(iOffset); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 break; | 167 break; |
168 default: | 168 default: |
169 strSegment.Format(L"%S", c_strFormat.c_str()); | 169 strSegment.Format(L"%S", c_strFormat.c_str()); |
170 break; | 170 break; |
171 } | 171 } |
172 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); | 172 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); |
173 } | 173 } |
174 | 174 |
175 c_strResult.erase(c_strResult.begin()); | 175 c_strResult.erase(c_strResult.begin()); |
176 vRet = CJS_Value(pRuntime, c_strResult.c_str()); | 176 vRet = CJS_Value(pRuntime, c_strResult.c_str()); |
177 return TRUE; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 FX_BOOL util::printd(IJS_Context* cc, | 180 bool util::printd(IJS_Context* cc, |
181 const std::vector<CJS_Value>& params, | 181 const std::vector<CJS_Value>& params, |
182 CJS_Value& vRet, | 182 CJS_Value& vRet, |
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(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(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(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(IDS_STRING_JSNOTSUPPORT); | 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) { |
247 int iStart = 0; | 247 int iStart = 0; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 time.tm_mon = iMonth; | 293 time.tm_mon = iMonth; |
294 time.tm_mday = iDay; | 294 time.tm_mday = iDay; |
295 time.tm_hour = iHour; | 295 time.tm_hour = iHour; |
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(IDS_STRING_JSTYPEERROR); | 306 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR); |
307 return FALSE; | 307 return false; |
308 } | 308 } |
309 | 309 |
310 | 310 bool util::printx(IJS_Context* cc, |
311 FX_BOOL util::printx(IJS_Context* cc, | 311 const std::vector<CJS_Value>& params, |
312 const std::vector<CJS_Value>& params, | 312 CJS_Value& vRet, |
313 CJS_Value& vRet, | 313 CFX_WideString& sError) { |
314 CFX_WideString& sError) { | |
315 if (params.size() < 2) { | 314 if (params.size() < 2) { |
316 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); | 315 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
317 return FALSE; | 316 return false; |
318 } | 317 } |
319 | 318 |
320 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 319 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
321 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), | 320 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), |
322 params[1].ToCFXWideString(pRuntime)) | 321 params[1].ToCFXWideString(pRuntime)) |
323 .c_str()); | 322 .c_str()); |
324 | 323 |
325 return TRUE; | 324 return true; |
326 } | 325 } |
327 | 326 |
328 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; | 327 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
329 | 328 |
330 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { | 329 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
331 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') | 330 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') |
332 return input | 0x20; | 331 return input | 0x20; |
333 if (eMode == kUpperCase && input >= 'a' && input <= 'z') | 332 if (eMode == kUpperCase && input >= 'a' && input <= 'z') |
334 return input & ~0x20; | 333 return input & ~0x20; |
335 return input; | 334 return input; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } break; | 418 } break; |
420 default: { | 419 default: { |
421 wsResult += wsFormat[iFormatIdx]; | 420 wsResult += wsFormat[iFormatIdx]; |
422 ++iFormatIdx; | 421 ++iFormatIdx; |
423 } break; | 422 } break; |
424 } | 423 } |
425 } | 424 } |
426 return wsResult; | 425 return wsResult; |
427 } | 426 } |
428 | 427 |
429 FX_BOOL util::scand(IJS_Context* cc, | 428 bool util::scand(IJS_Context* cc, |
430 const std::vector<CJS_Value>& params, | 429 const std::vector<CJS_Value>& params, |
431 CJS_Value& vRet, | 430 CJS_Value& vRet, |
432 CFX_WideString& sError) { | 431 CFX_WideString& sError) { |
433 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 432 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
434 int iSize = params.size(); | 433 int iSize = params.size(); |
435 if (iSize < 2) | 434 if (iSize < 2) |
436 return FALSE; | 435 return false; |
437 | 436 |
438 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); | 437 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
439 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime); | 438 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime); |
440 double dDate = JS_GetDateTime(); | 439 double dDate = JS_GetDateTime(); |
441 if (sDate.GetLength() > 0) { | 440 if (sDate.GetLength() > 0) { |
442 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); | 441 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
443 } | 442 } |
444 | 443 |
445 if (!JS_PortIsNan(dDate)) { | 444 if (!JS_PortIsNan(dDate)) { |
446 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); | 445 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); |
447 } else { | 446 } else { |
448 vRet.SetNull(pRuntime); | 447 vRet.SetNull(pRuntime); |
449 } | 448 } |
450 | 449 |
451 return TRUE; | 450 return true; |
452 } | 451 } |
453 | 452 |
454 FX_BOOL util::byteToChar(IJS_Context* cc, | 453 bool util::byteToChar(IJS_Context* cc, |
455 const std::vector<CJS_Value>& params, | 454 const std::vector<CJS_Value>& params, |
456 CJS_Value& vRet, | 455 CJS_Value& vRet, |
457 CFX_WideString& sError) { | 456 CFX_WideString& sError) { |
458 if (params.size() < 1) { | 457 if (params.size() < 1) { |
459 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); | 458 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
460 return FALSE; | 459 return false; |
461 } | 460 } |
462 | 461 |
463 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 462 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
464 int arg = params[0].ToInt(pRuntime); | 463 int arg = params[0].ToInt(pRuntime); |
465 if (arg < 0 || arg > 255) { | 464 if (arg < 0 || arg > 255) { |
466 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); | 465 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
467 return FALSE; | 466 return false; |
468 } | 467 } |
469 | 468 |
470 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); | 469 CFX_WideString wStr(static_cast<FX_WCHAR>(arg)); |
471 vRet = CJS_Value(pRuntime, wStr.c_str()); | 470 vRet = CJS_Value(pRuntime, wStr.c_str()); |
472 return TRUE; | 471 return true; |
473 } | 472 } |
OLD | NEW |