Esempio n. 1
0
TEST(SimpleParserTest, GetWord) {
  static const pdfium::StrFuncTestData test_data[] = {
      // Empty src string.
      STR_IN_OUT_CASE("", ""),
      // Content with whitespaces only.
      STR_IN_OUT_CASE(" \t \0 \n", ""),
      // Content with comments only.
      STR_IN_OUT_CASE("%this is a test case\r\n%2nd line", ""),
      // Mixed whitespaces and comments.
      STR_IN_OUT_CASE(" \t \0%try()%haha\n %another line \aa", ""),
      // Name.
      STR_IN_OUT_CASE(" /Tester ", "/Tester"),
      // String.
      STR_IN_OUT_CASE("\t(nice day)!\n ", "(nice day)"),
      // String with nested braces.
      STR_IN_OUT_CASE("\t(It is a (long) day)!\n ", "(It is a (long) day)"),
      // String with escaped chars.
      STR_IN_OUT_CASE("\t(It is a \\(long\\) day!)hi\n ",
                      "(It is a \\(long\\) day!)"),
      // Hex string.
      STR_IN_OUT_CASE(" \n<4545acdfedertt>abc ", "<4545acdfedertt>"),
      STR_IN_OUT_CASE(" \n<4545a<ed>ertt>abc ", "<4545a<ed>"),
      // Dictionary.
      STR_IN_OUT_CASE("<</oc 234 /color 2 3 R>>", "<<"),
      STR_IN_OUT_CASE("\t\t<< /abc>>", "<<"),
      // Handling ending delimiters.
      STR_IN_OUT_CASE("> little bear", ">"),
      STR_IN_OUT_CASE(") another bear", ")"), STR_IN_OUT_CASE(">> end ", ">>"),
      // No ending delimiters.
      STR_IN_OUT_CASE("(sdfgfgbcv", "(sdfgfgbcv"),
      // Regular cases.
      STR_IN_OUT_CASE("apple pear", "apple"),
      STR_IN_OUT_CASE(" pi=3.1415 ", "pi=3.1415"),
      STR_IN_OUT_CASE(" p t x c ", "p"), STR_IN_OUT_CASE(" pt\0xc ", "pt"),
      STR_IN_OUT_CASE(" $^&&*\t\0sdff ", "$^&&*"),
      STR_IN_OUT_CASE("\n\r+3.5656 -11.0", "+3.5656"),
  };
  for (size_t i = 0; i < FX_ArraySize(test_data); ++i) {
    const pdfium::StrFuncTestData& data = test_data[i];
    CPDF_SimpleParser parser(pdfium::make_span(data.input, data.input_size));
    ByteStringView word = parser.GetWord();
    EXPECT_EQ(data.expected_size, word.GetLength()) << " for case " << i;
    if (data.expected_size != word.GetLength())
      continue;
    EXPECT_EQ(
        0, memcmp(data.expected, word.unterminated_c_str(), data.expected_size))
        << " for case " << i;
  }
}
Esempio n. 2
0
v8::Local<v8::String> CFX_V8::NewString(ByteStringView str) {
  v8::Isolate* pIsolate = m_pIsolate ? GetIsolate() : v8::Isolate::GetCurrent();
  return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(),
                                 v8::NewStringType::kNormal, str.GetLength())
      .ToLocalChecked();
}
Esempio n. 3
0
bool CPSOutput::WriteString(ByteStringView str) {
  return WriteBlock(str.unterminated_c_str(), str.GetLength());
}
Esempio n. 4
0
FX_Number::FX_Number(ByteStringView strc)
    : m_bInteger(true), m_bSigned(false), m_UnsignedValue(0) {
  if (strc.IsEmpty())
    return;

  if (strc.Contains('.')) {
    m_bInteger = false;
    m_bSigned = true;
    m_FloatValue = StringToFloat(strc);
    return;
  }

  // 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> unsigned_val = 0;
  bool bNegative = false;
  size_t cc = 0;
  if (strc[0] == '+') {
    cc++;
    m_bSigned = true;
  } else if (strc[0] == '-') {
    bNegative = true;
    m_bSigned = true;
    cc++;
  }

  while (cc < strc.GetLength() && std::isdigit(strc[cc])) {
    unsigned_val = unsigned_val * 10 + FXSYS_DecimalCharToInt(strc.CharAt(cc));
    if (!unsigned_val.IsValid())
      break;
    cc++;
  }

  uint32_t uValue = unsigned_val.ValueOrDefault(0);
  if (!m_bSigned) {
    m_UnsignedValue = uValue;
    return;
  }

  // We have a sign, so if the value was greater then the signed integer
  // limits, then we've overflowed and must reset to the default value.
  constexpr uint32_t uLimit =
      static_cast<uint32_t>(std::numeric_limits<int>::max());

  if (uValue > (bNegative ? uLimit + 1 : uLimit))
    uValue = 0;

  // Switch back to the int space so we can flip to a negative if we need.
  int32_t value = static_cast<int32_t>(uValue);
  if (bNegative) {
    // |value| is usually positive, except in the corner case of "-2147483648",
    // where |uValue| is 2147483648. When it gets casted to an int, |value|
    // becomes -2147483648. For this case, avoid undefined behavior, because
    // an int32_t cannot represent 2147483648.
    static constexpr int kMinInt = std::numeric_limits<int>::min();
    m_SignedValue = LIKELY(value != kMinInt) ? -value : kMinInt;
  } else {
    m_SignedValue = value;
  }
}