1 // Copyright 2018 The PDFium Authors
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 "core/fxcrt/fx_number.h"
8
9 #include <ctype.h>
10
11 #include <limits>
12
13 #include "core/fxcrt/fx_extension.h"
14 #include "core/fxcrt/fx_safe_types.h"
15 #include "core/fxcrt/fx_string.h"
16
FX_Number()17 FX_Number::FX_Number()
18 : m_bIsInteger(true), m_bIsSigned(false), m_UnsignedValue(0) {}
19
FX_Number(int32_t value)20 FX_Number::FX_Number(int32_t value)
21 : m_bIsInteger(true), m_bIsSigned(true), m_SignedValue(value) {}
22
FX_Number(float value)23 FX_Number::FX_Number(float value)
24 : m_bIsInteger(false), m_bIsSigned(true), m_FloatValue(value) {}
25
FX_Number(ByteStringView strc)26 FX_Number::FX_Number(ByteStringView strc)
27 : m_bIsInteger(true), m_bIsSigned(false), m_UnsignedValue(0) {
28 if (strc.IsEmpty())
29 return;
30
31 if (strc.Contains('.')) {
32 m_bIsInteger = false;
33 m_bIsSigned = true;
34 m_FloatValue = StringToFloat(strc);
35 return;
36 }
37
38 // Note, numbers in PDF are typically of the form 123, -123, etc. But,
39 // for things like the Permissions on the encryption hash the number is
40 // actually an unsigned value. We use a uint32_t so we can deal with the
41 // unsigned and then check for overflow if the user actually signed the value.
42 // The Permissions flag is listed in Table 3.20 PDF 1.7 spec.
43 FX_SAFE_UINT32 unsigned_val = 0;
44 bool bNegative = false;
45 size_t cc = 0;
46 if (strc[0] == '+') {
47 cc++;
48 m_bIsSigned = true;
49 } else if (strc[0] == '-') {
50 bNegative = true;
51 m_bIsSigned = true;
52 cc++;
53 }
54
55 for (; cc < strc.GetLength() && isdigit(strc[cc]); ++cc) {
56 // Deliberately not using FXSYS_DecimalCharToInt() in a tight loop to avoid
57 // a duplicate isdigit() call. Note that the order of operation is
58 // important to avoid unintentional overflows.
59 unsigned_val = unsigned_val * 10 + (strc[cc] - '0');
60 }
61
62 uint32_t uValue = unsigned_val.ValueOrDefault(0);
63 if (!m_bIsSigned) {
64 m_UnsignedValue = uValue;
65 return;
66 }
67
68 // We have a sign, so if the value was greater then the signed integer
69 // limits, then we've overflowed and must reset to the default value.
70 constexpr uint32_t uLimit =
71 static_cast<uint32_t>(std::numeric_limits<int>::max());
72
73 if (uValue > (bNegative ? uLimit + 1 : uLimit))
74 uValue = 0;
75
76 // Switch back to the int space so we can flip to a negative if we need.
77 int32_t value = static_cast<int32_t>(uValue);
78 if (bNegative) {
79 // |value| is usually positive, except in the corner case of "-2147483648",
80 // where |uValue| is 2147483648. When it gets casted to an int, |value|
81 // becomes -2147483648. For this case, avoid undefined behavior, because
82 // an int32_t cannot represent 2147483648.
83 static constexpr int kMinInt = std::numeric_limits<int>::min();
84 m_SignedValue = LIKELY(value != kMinInt) ? -value : kMinInt;
85 } else {
86 m_SignedValue = value;
87 }
88 }
89
GetSigned() const90 int32_t FX_Number::GetSigned() const {
91 return m_bIsInteger ? m_SignedValue : static_cast<int32_t>(m_FloatValue);
92 }
93
GetFloat() const94 float FX_Number::GetFloat() const {
95 if (!m_bIsInteger)
96 return m_FloatValue;
97
98 return m_bIsSigned ? static_cast<float>(m_SignedValue)
99 : static_cast<float>(m_UnsignedValue);
100 }
101