bool FX_atonum(const CFX_ByteStringC& strc, void* pData) { if (strc.Find('.') != -1) { FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData); *pFloat = FX_atof(strc); return false; } // Note, numbers in PDF are typically of the form 123, -123, etc. But, // for things like the Permissions on the encryption hash the number is // actually an unsigned value. We use a uint32_t so we can deal with the // unsigned and then check for overflow if the user actually signed the value. // The Permissions flag is listed in Table 3.20 PDF 1.7 spec. pdfium::base::CheckedNumeric<uint32_t> integer = 0; bool bNegative = false; bool bSigned = false; int cc = 0; if (strc[0] == '+') { cc++; bSigned = true; } else if (strc[0] == '-') { bNegative = true; bSigned = true; cc++; } while (cc < strc.GetLength() && std::isdigit(strc[cc])) { integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); if (!integer.IsValid()) break; cc++; } // We have a sign, and the value was greater then a regular integer // we've overflowed, reset to the default value. if (bSigned) { if (bNegative) { if (integer.ValueOrDefault(kDefaultIntValue) > static_cast<uint32_t>(std::numeric_limits<int>::max()) + 1) { integer = kDefaultIntValue; } } else if (integer.ValueOrDefault(kDefaultIntValue) > static_cast<uint32_t>(std::numeric_limits<int>::max())) { integer = kDefaultIntValue; } } // Switch back to the int space so we can flip to a negative if we need. int value = static_cast<int>(integer.ValueOrDefault(kDefaultIntValue)); if (bNegative) value = -value; int* pInt = static_cast<int*>(pData); *pInt = value; return true; }
FX_FLOAT FX_atof(const CFX_ByteStringC& strc) { if (strc.IsEmpty()) return 0.0; int cc = 0; bool bNegative = false; int len = strc.GetLength(); if (strc[0] == '+') { cc++; } else if (strc[0] == '-') { bNegative = TRUE; cc++; } while (cc < len) { if (strc[cc] != '+' && strc[cc] != '-') { break; } cc++; } FX_FLOAT value = 0; while (cc < len) { if (strc[cc] == '.') { break; } value = value * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); cc++; } int scale = 0; if (cc < len && strc[cc] == '.') { cc++; while (cc < len) { value += FXSYS_FractionalScale(scale, FXSYS_toDecimalDigit(strc.CharAt(cc))); scale++; if (scale == FXSYS_FractionalScaleCount()) break; cc++; } } return bNegative ? -value : value; }