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 "core/fxcrt/include/fx_basic.h" | 7 #include "core/fxcrt/include/fx_basic.h" |
8 #include "core/fxcrt/include/fx_ext.h" | 8 #include "core/fxcrt/include/fx_ext.h" |
9 | 9 |
10 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 10 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
11 #include <dirent.h> | 11 #include <dirent.h> |
12 #include <sys/types.h> | 12 #include <sys/types.h> |
13 #else | 13 #else |
14 #include <direct.h> | 14 #include <direct.h> |
15 #endif | 15 #endif |
16 | 16 |
17 #include <algorithm> | 17 #include <algorithm> |
18 #include <cctype> | 18 #include <cctype> |
| 19 #include <limits> |
19 #include <memory> | 20 #include <memory> |
20 | 21 |
| 22 namespace { |
| 23 |
| 24 const int kDefaultIntValue = 0; |
| 25 |
| 26 } // namespace |
| 27 |
21 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) { | 28 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) { |
22 if (strc.Find('.') != -1) { | 29 if (strc.Find('.') != -1) { |
23 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData); | 30 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData); |
24 *pFloat = FX_atof(strc); | 31 *pFloat = FX_atof(strc); |
25 return false; | 32 return false; |
26 } | 33 } |
27 | 34 |
| 35 // Note, numbers in PDF are typically of the form 123, -123, etc. But, |
| 36 // for things like the Permissions on the encryption hash the number is |
| 37 // actually an unsigned value. We use a uint32_t so we can deal with the |
| 38 // unsigned and then check for overflow if the user actually signed the value. |
| 39 // The Permissions flag is listed in Table 3.20 PDF 1.7 spec. |
| 40 pdfium::base::CheckedNumeric<uint32_t> integer = 0; |
| 41 bool bNegative = false; |
| 42 bool bSigned = false; |
28 int cc = 0; | 43 int cc = 0; |
29 pdfium::base::CheckedNumeric<int> integer = 0; | |
30 bool bNegative = false; | |
31 if (strc[0] == '+') { | 44 if (strc[0] == '+') { |
32 cc++; | 45 cc++; |
| 46 bSigned = true; |
33 } else if (strc[0] == '-') { | 47 } else if (strc[0] == '-') { |
34 bNegative = true; | 48 bNegative = true; |
| 49 bSigned = true; |
35 cc++; | 50 cc++; |
36 } | 51 } |
| 52 |
37 while (cc < strc.GetLength() && std::isdigit(strc[cc])) { | 53 while (cc < strc.GetLength() && std::isdigit(strc[cc])) { |
38 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); | 54 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); |
39 if (!integer.IsValid()) | 55 if (!integer.IsValid()) |
40 break; | 56 break; |
41 cc++; | 57 cc++; |
42 } | 58 } |
| 59 |
| 60 // We have a sign, and the value was greater then a regular integer |
| 61 // we've overflowed, reset to the default value. |
| 62 if (bSigned) { |
| 63 if (bNegative) { |
| 64 if (integer.ValueOrDefault(kDefaultIntValue) > |
| 65 static_cast<uint32_t>(std::numeric_limits<int>::max()) + 1) { |
| 66 integer = kDefaultIntValue; |
| 67 } |
| 68 } else if (integer.ValueOrDefault(kDefaultIntValue) > |
| 69 static_cast<uint32_t>(std::numeric_limits<int>::max())) { |
| 70 integer = kDefaultIntValue; |
| 71 } |
| 72 } |
| 73 |
| 74 // Switch back to the int space so we can flip to a negative if we need. |
| 75 int value = static_cast<int>(integer.ValueOrDefault(kDefaultIntValue)); |
43 if (bNegative) | 76 if (bNegative) |
44 integer = -integer; | 77 value = -value; |
45 | 78 |
46 int* pInt = static_cast<int*>(pData); | 79 int* pInt = static_cast<int*>(pData); |
47 *pInt = integer.ValueOrDefault(0); | 80 *pInt = value; |
48 return true; | 81 return true; |
49 } | 82 } |
50 | 83 |
51 static const FX_FLOAT fraction_scales[] = { | 84 static const FX_FLOAT fraction_scales[] = { |
52 0.1f, 0.01f, 0.001f, 0.0001f, | 85 0.1f, 0.01f, 0.001f, 0.0001f, |
53 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, | 86 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, |
54 0.000000001f, 0.0000000001f, 0.00000000001f}; | 87 0.000000001f, 0.0000000001f, 0.00000000001f}; |
55 int FXSYS_FractionalScaleCount() { | 88 int FXSYS_FractionalScaleCount() { |
56 return FX_ArraySize(fraction_scales); | 89 return FX_ArraySize(fraction_scales); |
57 } | 90 } |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 dstShift -= 8; | 335 dstShift -= 8; |
303 result |= *dataPtr++ << dstShift; | 336 result |= *dataPtr++ << dstShift; |
304 } | 337 } |
305 if (dstShift > 0) { | 338 if (dstShift > 0) { |
306 bitShift = 8 - dstShift; | 339 bitShift = 8 - dstShift; |
307 bitMask = (1 << dstShift) - 1; | 340 bitMask = (1 << dstShift) - 1; |
308 result |= *dataPtr++ >> bitShift & bitMask; | 341 result |= *dataPtr++ >> bitShift & bitMask; |
309 } | 342 } |
310 return result; | 343 return result; |
311 } | 344 } |
OLD | NEW |