void CXFA_FMStringExpression::ToJavaScript(CFX_WideTextBuf& javascript) {
  CFX_WideString tempStr(m_wsString);
  if (tempStr.GetLength() > 2) {
    javascript.AppendChar(L'\"');
    FX_WCHAR oneChar;
    for (int16_t i = 1; i < tempStr.GetLength() - 1; i++) {
      oneChar = tempStr[i];
      switch (oneChar) {
        case L'\"': {
          i++;
          javascript << FX_WSTRC(L"\\\"");
        } break;
        case 0x0d:
          break;
        case 0x0a: {
          javascript << FX_WSTRC(L"\\n");
        } break;
        default: { javascript.AppendChar(oneChar); } break;
      }
    }
    javascript.AppendChar(L'\"');
  } else {
    javascript << tempStr;
  }
}
TEST(FMCallExpression, more_than_32_arguments) {
  // Use sign as it has 3 object parameters at positions 0, 5, and 6.
  std::unique_ptr<CXFA_FMIdentifierExpression> exp(
      new CXFA_FMIdentifierExpression(0, CFX_WideStringC(L"sign")));

  std::unique_ptr<CFX_ArrayTemplate<CXFA_FMSimpleExpression*>> args(
      new CFX_ArrayTemplate<CXFA_FMSimpleExpression*>());
  for (size_t i = 0; i < 50; i++)
    args->Add(new CXFA_FMSimpleExpression(0, TOKnan));

  CXFA_FMCallExpression callExp(0, exp.release(), args.release(), true);
  CFX_WideTextBuf js;
  callExp.ToJavaScript(js);

  // Generate the result javascript string.
  CFX_WideString result = L"sign(";
  for (size_t i = 0; i < 50; i++) {
    if (i > 0)
      result += L", ";

    result += L"foxit_xfa_formcalc_runtime.get_fm_";
    // Object positions for sign() method.
    if (i == 0 || i == 5 || i == 6)
      result += L"jsobj()";
    else
      result += L"value()";
  }
  result += L")";

  EXPECT_EQ(result.AsStringC(), js.AsStringC());
}
FX_BOOL CXFA_ScriptContext::RunScript(XFA_SCRIPTLANGTYPE eScriptType,
                                      const CFX_WideStringC& wsScript,
                                      CFXJSE_Value* hRetValue,
                                      CXFA_Object* pThisObject) {
  CFX_ByteString btScript;
  XFA_SCRIPTLANGTYPE eSaveType = m_eScriptType;
  m_eScriptType = eScriptType;
  if (eScriptType == XFA_SCRIPTLANGTYPE_Formcalc) {
    if (!m_FM2JSContext) {
      m_FM2JSContext.reset(
          new CXFA_FM2JSContext(m_pIsolate, m_JsContext.get(), m_pDocument));
    }
    CFX_WideTextBuf wsJavaScript;
    CFX_WideString wsErrorInfo;
    int32_t iFlags =
        CXFA_FM2JSContext::Translate(wsScript, wsJavaScript, wsErrorInfo);
    if (iFlags) {
      hRetValue->SetUndefined();
      return FALSE;
    }
    btScript =
        FX_UTF8Encode(wsJavaScript.GetBuffer(), wsJavaScript.GetLength());
  } else {
    btScript = FX_UTF8Encode(wsScript.c_str(), wsScript.GetLength());
  }
  CXFA_Object* pOriginalObject = m_pThisObject;
  m_pThisObject = pThisObject;
  CFXJSE_Value* pValue = pThisObject ? GetJSValueFromMap(pThisObject) : nullptr;
  FX_BOOL bRet =
      m_JsContext->ExecuteScript(btScript.c_str(), hRetValue, pValue);
  m_pThisObject = pOriginalObject;
  m_eScriptType = eSaveType;
  return bRet;
}
Exemple #4
0
WideString CFX_XMLElement::GetTextData() const {
  CFX_WideTextBuf buffer;
  for (CFX_XMLNode* pChild = GetFirstChild(); pChild;
       pChild = pChild->GetNextSibling()) {
    CFX_XMLText* pText = ToXMLText(pChild);
    if (pText)
      buffer << pText->GetText();
  }
  return buffer.MakeString();
}
Exemple #5
0
void PDF_GetTextStream_Unicode(CFX_WideTextBuf& buffer, CPDF_Document* pDoc, CPDF_Dictionary* pPage, FX_DWORD flags)
{
    buffer.EstimateSize(0, 10240);
    CPDF_Page page;
    page.Load(pDoc, pPage);
    CPDF_ParseOptions options;
    options.m_bTextOnly = TRUE;
    options.m_bSeparateForm = FALSE;
    page.ParseContent(&options);
    _PDF_GetTextStream_Unicode(buffer, &page, TRUE, NULL);
}
CFX_WideString PDF_GetFirstTextLine_Unicode(CPDF_Document* pDoc, CPDF_Dictionary* pPage)
{
    CFX_WideTextBuf buffer;
    buffer.EstimateSize(0, 1024);
    CPDF_Page page;
    page.Load(pDoc, pPage);
    CPDF_ParseOptions options;
    options.m_bTextOnly = TRUE;
    options.m_bSeparateForm = FALSE;
    page.ParseContent(&options);
    CPDF_TextStream textstream(buffer, FALSE, NULL);
    FX_POSITION pos = page.GetFirstObjectPosition();
    while (pos) {
        CPDF_PageObject* pObject = page.GetNextObject(pos);
        if (pObject->m_Type != PDFPAGE_TEXT) {
            continue;
        }
        if (textstream.ProcessObject((CPDF_TextObject*)pObject, TRUE)) {
            break;
        }
    }
    return buffer.GetWideString();
}
Exemple #7
0
int32_t XFA_FM2JS_Translate(const CFX_WideStringC& wsFormcalc,
                            CFX_WideTextBuf& wsJavascript,
                            CFX_WideString& wsError) {
  if (wsFormcalc.IsEmpty()) {
    wsJavascript.Clear();
    wsError.Empty();
    return 0;
  }
  int32_t status = 0;
  CXFA_FMProgram program;
  status = program.Init(wsFormcalc);
  if (status) {
    wsError = program.GetError().message;
    return status;
  }
  status = program.ParseProgram();
  if (status) {
    wsError = program.GetError().message;
    return status;
  }
  program.TranslateProgram(wsJavascript);
  return 0;
}
Exemple #8
0
int32_t CXFA_FMProgram::TranslateProgram(CFX_WideTextBuf& wsJavaScript) {
  m_globalFunction->ToJavaScript(wsJavaScript);
  wsJavaScript.AppendChar(0);
  return 0;
}
void CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript) {
  CFX_WideTextBuf funcName;
  m_pExp->ToJavaScript(funcName);
  if (m_bIsSomMethod) {
    javascript << funcName;
    javascript << FX_WSTRC(L"(");
    if (m_pArguments) {
      uint32_t methodPara = IsMethodWithObjParam(funcName.AsStringC());
      if (methodPara > 0) {
        for (int i = 0; i < m_pArguments->GetSize(); ++i) {
          // Currently none of our expressions use objects for a parameter over
          // the 6th. Make sure we don't overflow the shift when doing this
          // check. If we ever need more the 32 object params we can revisit.
          if (i < 32 && (methodPara & (0x01 << i)) > 0) {
            javascript << gs_lpStrExpFuncName[GETFMJSOBJ];
          } else {
            javascript << gs_lpStrExpFuncName[GETFMVALUE];
          }
          javascript << FX_WSTRC(L"(");
          CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i);
          e->ToJavaScript(javascript);
          javascript << FX_WSTRC(L")");
          if (i + 1 < m_pArguments->GetSize()) {
            javascript << FX_WSTRC(L", ");
          }
        }
      } else {
        for (int i = 0; i < m_pArguments->GetSize(); ++i) {
          javascript << gs_lpStrExpFuncName[GETFMVALUE];
          javascript << FX_WSTRC(L"(");
          CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i);
          e->ToJavaScript(javascript);
          javascript << FX_WSTRC(L")");
          if (i + 1 < m_pArguments->GetSize()) {
            javascript << FX_WSTRC(L", ");
          }
        }
      }
    }
    javascript << FX_WSTRC(L")");
  } else {
    bool isEvalFunc = false;
    bool isExistsFunc = false;
    if (IsBuildInFunc(&funcName)) {
      if (funcName.AsStringC() == FX_WSTRC(L"Eval")) {
        isEvalFunc = true;
        javascript << FX_WSTRC(L"eval.call(this, ");
        javascript << gs_lpStrExpFuncName[CALL];
        javascript << FX_WSTRC(L"Translate");
      } else if (funcName.AsStringC() == FX_WSTRC(L"Exists")) {
        isExistsFunc = true;
        javascript << gs_lpStrExpFuncName[CALL];
        javascript << funcName;
      } else {
        javascript << gs_lpStrExpFuncName[CALL];
        javascript << funcName;
      }
    } else {
      javascript << funcName;
    }
    javascript << FX_WSTRC(L"(");
    if (isExistsFunc) {
      javascript << FX_WSTRC(L"\n(\nfunction ()\n{\ntry\n{\n");
      if (m_pArguments && m_pArguments->GetSize() > 0) {
        CXFA_FMSimpleExpression* e = m_pArguments->GetAt(0);
        javascript << FX_WSTRC(L"return ");
        e->ToJavaScript(javascript);
        javascript << FX_WSTRC(L";\n}\n");
      } else {
        javascript << FX_WSTRC(L"return 0;\n}\n");
      }
      javascript << FX_WSTRC(
          L"catch(accessExceptions)\n{\nreturn 0;\n}\n}\n).call(this)\n");
    } else if (m_pArguments) {
      for (int i = 0; i < m_pArguments->GetSize(); ++i) {
        CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i);
        e->ToJavaScript(javascript);
        if (i + 1 < m_pArguments->GetSize()) {
          javascript << FX_WSTRC(L", ");
        }
      }
    }
    javascript << FX_WSTRC(L")");
    if (isEvalFunc) {
      javascript << FX_WSTRC(L")");
    }
  }
}
CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, bool bStartTag) {
  m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
  if (IsEOF()) {
    return nullptr;
  }
  CFX_ByteString tag_name, tag_space;
  bool bEndTag;
  GetTagName(tag_space, tag_name, bEndTag, bStartTag);
  if (tag_name.IsEmpty() || bEndTag) {
    return nullptr;
  }
  CXML_Element* pElement = new CXML_Element;
  pElement->m_pParent = pParent;
  pElement->SetTag(tag_space.AsStringC(), tag_name.AsStringC());
  do {
    CFX_ByteString attr_space, attr_name;
    while (m_dwIndex < m_dwBufferSize) {
      SkipWhiteSpaces();
      if (IsEOF()) {
        break;
      }
      if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) {
        break;
      }
      GetName(attr_space, attr_name);
      SkipWhiteSpaces();
      if (IsEOF()) {
        break;
      }
      if (m_pBuffer[m_dwIndex] != '=') {
        break;
      }
      m_dwIndex++;
      SkipWhiteSpaces();
      if (IsEOF()) {
        break;
      }
      CFX_WideString attr_value;
      GetAttrValue(attr_value);
      pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value);
    }
    m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
    if (m_dwIndex < m_dwBufferSize || IsEOF()) {
      break;
    }
  } while (ReadNextBlock());
  SkipWhiteSpaces();
  if (IsEOF()) {
    return pElement;
  }
  uint8_t ch = m_pBuffer[m_dwIndex++];
  if (ch == '/') {
    m_dwIndex++;
    m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
    return pElement;
  }
  if (ch != '>') {
    m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
    delete pElement;
    return nullptr;
  }
  SkipWhiteSpaces();
  if (IsEOF()) {
    return pElement;
  }
  CFX_UTF8Decoder decoder;
  CFX_WideTextBuf content;
  bool bCDATA = false;
  int32_t iState = 0;
  do {
    while (m_dwIndex < m_dwBufferSize) {
      ch = m_pBuffer[m_dwIndex++];
      switch (iState) {
        case 0:
          if (ch == '<') {
            iState = 1;
          } else if (ch == '&') {
            decoder.ClearStatus();
            decoder.AppendChar(GetCharRef());
          } else {
            decoder.Input(ch);
          }
          break;
        case 1:
          if (ch == '!') {
            iState = 2;
          } else if (ch == '?') {
            SkipLiterals("?>");
            SkipWhiteSpaces();
            iState = 0;
          } else if (ch == '/') {
            CFX_ByteString space, name;
            GetName(space, name);
            SkipWhiteSpaces();
            m_dwIndex++;
            iState = 10;
          } else {
            content << decoder.GetResult();
            CFX_WideString dataStr = content.MakeString();
            if (!bCDATA && !m_bSaveSpaceChars) {
              dataStr.TrimRight(L" \t\r\n");
            }
            InsertContentSegment(bCDATA, dataStr.AsStringC(), pElement);
            content.Clear();
            decoder.Clear();
            bCDATA = false;
            iState = 0;
            m_dwIndex--;
            CXML_Element* pSubElement = ParseElement(pElement, true);
            if (!pSubElement) {
              break;
            }
            pSubElement->m_pParent = pElement;
            pElement->m_Children.push_back(
                {CXML_Element::Element, pSubElement});
            SkipWhiteSpaces();
          }
          break;
        case 2:
          if (ch == '[') {
            SkipLiterals("]]>");
          } else if (ch == '-') {
            m_dwIndex++;
            SkipLiterals("-->");
          } else {
            SkipLiterals(">");
          }
          decoder.Clear();
          SkipWhiteSpaces();
          iState = 0;
          break;
      }
      if (iState == 10) {
        break;
      }
    }
    m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
    if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) {
      break;
    }
  } while (ReadNextBlock());
  content << decoder.GetResult();
  CFX_WideString dataStr = content.MakeString();
  if (!m_bSaveSpaceChars) {
    dataStr.TrimRight(L" \t\r\n");
  }
  InsertContentSegment(bCDATA, dataStr.AsStringC(), pElement);
  content.Clear();
  decoder.Clear();
  bCDATA = false;
  return pElement;
}
Exemple #11
0
CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, FX_BOOL bStartTag)
{
    m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
    if (IsEOF()) {
        return NULL;
    }
    CFX_ByteString tag_name, tag_space;
    FX_BOOL bEndTag;
    GetTagName(tag_space, tag_name, bEndTag, bStartTag);
    if (tag_name.IsEmpty() || bEndTag) {
        return NULL;
    }
    CXML_Element* pElement;
    pElement = FX_NEW CXML_Element;
    if (pElement) {
        pElement->m_pParent = pParent;
        pElement->SetTag(tag_space, tag_name);
    }
    if (!pElement) {
        return NULL;
    }
    do {
        CFX_ByteString attr_space, attr_name;
        while (m_dwIndex < m_dwBufferSize) {
            SkipWhiteSpaces();
            if (IsEOF()) {
                break;
            }
            if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) {
                break;
            }
            GetName(attr_space, attr_name);
            SkipWhiteSpaces();
            if (IsEOF()) {
                break;
            }
            if (m_pBuffer[m_dwIndex] != '=') {
                break;
            }
            m_dwIndex ++;
            SkipWhiteSpaces();
            if (IsEOF()) {
                break;
            }
            CFX_WideString attr_value;
            GetAttrValue(attr_value);
            pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value);
        }
        m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
        if (m_dwIndex < m_dwBufferSize || IsEOF()) {
            break;
        }
    } while (ReadNextBlock());
    SkipWhiteSpaces();
    if (IsEOF()) {
        return pElement;
    }
    FX_BYTE ch = m_pBuffer[m_dwIndex ++];
    if (ch == '/') {
        m_dwIndex ++;
        m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
        return pElement;
    }
    if (ch != '>') {
        m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
        delete pElement;
        return NULL;
    }
    SkipWhiteSpaces();
    if (IsEOF()) {
        return pElement;
    }
    CFX_UTF8Decoder decoder;
    CFX_WideTextBuf content;
    FX_BOOL bCDATA = FALSE;
    FX_INT32 iState = 0;
    do {
        while (m_dwIndex < m_dwBufferSize) {
            ch = m_pBuffer[m_dwIndex ++];
            switch (iState) {
                case 0:
                    if (ch == '<') {
                        iState = 1;
                    } else if (ch == '&') {
                        decoder.ClearStatus();
                        decoder.AppendChar(GetCharRef());
                    } else {
                        decoder.Input(ch);
                    }
                    break;
                case 1:
                    if (ch == '!') {
                        iState = 2;
                    } else if (ch == '?') {
                        SkipLiterals(FX_BSTRC("?>"));
                        SkipWhiteSpaces();
                        iState = 0;
                    } else if (ch == '/') {
                        CFX_ByteString space, name;
                        GetName(space, name);
                        SkipWhiteSpaces();
                        m_dwIndex ++;
                        iState = 10;
                    } else {
                        content << decoder.GetResult();
                        CFX_WideString dataStr = content.GetWideString();
                        if (!bCDATA && !m_bSaveSpaceChars) {
                            dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n");
                        }
                        InsertContentSegment(bCDATA, dataStr, pElement);
                        content.Clear();
                        decoder.Clear();
                        bCDATA = FALSE;
                        iState = 0;
                        m_dwIndex --;
                        CXML_Element* pSubElement = ParseElement(pElement, TRUE);
                        if (pSubElement == NULL) {
                            break;
                        }
                        pSubElement->m_pParent = pElement;
                        pElement->m_Children.Add((FX_LPVOID)CXML_Element::Element);
                        pElement->m_Children.Add(pSubElement);
                        SkipWhiteSpaces();
                    }
                    break;
                case 2:
                    if (ch == '[') {
                        SkipLiterals(FX_BSTRC("]]>"));
                    } else if (ch == '-') {
                        m_dwIndex ++;
                        SkipLiterals(FX_BSTRC("-->"));
                    } else {
                        SkipLiterals(FX_BSTRC(">"));
                    }
                    decoder.Clear();
                    SkipWhiteSpaces();
                    iState = 0;
                    break;
            }
            if (iState == 10) {
                break;
            }
        }
        m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
        if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) {
            break;
        }
    } while (ReadNextBlock());
    content << decoder.GetResult();
    CFX_WideString dataStr = content.GetWideString();
    if (!m_bSaveSpaceChars) {
        dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n");
    }
    InsertContentSegment(bCDATA, dataStr, pElement);
    content.Clear();
    decoder.Clear();
    bCDATA = FALSE;
    return pElement;
}