Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(739)

Side by Side Diff: core/fxcrt/fx_basic_util.cpp

Issue 2640143003: Update safe numerics package to get bitwise ops (Closed)
Patch Set: 0 is a perfectly fine value of zero Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/fx_basic.h" 7 #include "core/fxcrt/fx_basic.h"
8 #include "core/fxcrt/fx_ext.h" 8 #include "core/fxcrt/fx_ext.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <cctype> 11 #include <cctype>
12 #include <limits> 12 #include <limits>
13 #include <memory> 13 #include <memory>
14 14
15 namespace {
16
17 const int kDefaultIntValue = 0;
18
19 } // namespace
20
21 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) { 15 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) {
22 if (strc.Find('.') != -1) { 16 if (strc.Find('.') != -1) {
23 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData); 17 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData);
24 *pFloat = FX_atof(strc); 18 *pFloat = FX_atof(strc);
25 return false; 19 return false;
26 } 20 }
27 21
28 // Note, numbers in PDF are typically of the form 123, -123, etc. But, 22 // Note, numbers in PDF are typically of the form 123, -123, etc. But,
29 // for things like the Permissions on the encryption hash the number is 23 // for things like the Permissions on the encryption hash the number is
30 // actually an unsigned value. We use a uint32_t so we can deal with the 24 // actually an unsigned value. We use a uint32_t so we can deal with the
(...skipping 16 matching lines...) Expand all
47 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); 41 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc));
48 if (!integer.IsValid()) 42 if (!integer.IsValid())
49 break; 43 break;
50 cc++; 44 cc++;
51 } 45 }
52 46
53 // We have a sign, and the value was greater then a regular integer 47 // We have a sign, and the value was greater then a regular integer
54 // we've overflowed, reset to the default value. 48 // we've overflowed, reset to the default value.
55 if (bSigned) { 49 if (bSigned) {
56 if (bNegative) { 50 if (bNegative) {
57 if (integer.ValueOrDefault(kDefaultIntValue) > 51 if (integer.ValueOrDefault(0) >
58 static_cast<uint32_t>(std::numeric_limits<int>::max()) + 1) { 52 static_cast<uint32_t>(std::numeric_limits<int>::max()) + 1) {
59 integer = kDefaultIntValue; 53 integer = 0;
60 } 54 }
61 } else if (integer.ValueOrDefault(kDefaultIntValue) > 55 } else if (integer.ValueOrDefault(0) >
62 static_cast<uint32_t>(std::numeric_limits<int>::max())) { 56 static_cast<uint32_t>(std::numeric_limits<int>::max())) {
63 integer = kDefaultIntValue; 57 integer = 0;
64 } 58 }
65 } 59 }
66 60
67 // Switch back to the int space so we can flip to a negative if we need. 61 // Switch back to the int space so we can flip to a negative if we need.
68 int value = static_cast<int>(integer.ValueOrDefault(kDefaultIntValue)); 62 uint32_t uValue = integer.ValueOrDefault(0);
63 int32_t value = static_cast<int>(uValue);
69 if (bNegative) 64 if (bNegative)
70 value = -value; 65 value = -value;
71 66
72 int* pInt = static_cast<int*>(pData); 67 int* pInt = static_cast<int*>(pData);
73 *pInt = value; 68 *pInt = value;
74 return true; 69 return true;
75 } 70 }
76 71
77 static const FX_FLOAT fraction_scales[] = { 72 static const FX_FLOAT fraction_scales[] = {
78 0.1f, 0.01f, 0.001f, 0.0001f, 73 0.1f, 0.01f, 0.001f, 0.0001f,
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 dstShift -= 8; 255 dstShift -= 8;
261 result |= *dataPtr++ << dstShift; 256 result |= *dataPtr++ << dstShift;
262 } 257 }
263 if (dstShift > 0) { 258 if (dstShift > 0) {
264 bitShift = 8 - dstShift; 259 bitShift = 8 - dstShift;
265 bitMask = (1 << dstShift) - 1; 260 bitMask = (1 << dstShift) - 1;
266 result |= *dataPtr++ >> bitShift & bitMask; 261 result |= *dataPtr++ >> bitShift & bitMask;
267 } 262 }
268 return result; 263 return result;
269 } 264 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698