Beispiel #1
0
void DBServer::handle(const byte_vec& input, byte_vec* output) {
    // unpack incoming data
    msgpack::unpacked result;
    std::string str_input(input.begin(), input.end());
    msgpack::unpack(result, str_input.data(), input.size());
    msgpack::object request(result.get());
    // prepare reply object
    msgpack::sbuffer header_buf;
    msgpack::sbuffer data_buf;
    Packer reply_header(&header_buf);
    Packer reply_data(&data_buf);
    // identify endpoint function based on request data
    endpoint_fn endpoint;
    try {
        endpoint = identify_endpoint(request);
    } catch (invalid_request& e) {
        set_status(status_codes::INVALID_REQUEST, e.what(), "", &reply_header);
    }
    // get reply from endpoint function
    (this->*endpoint)(request, &reply_header, &reply_data);
    // write serialized reply data into output buffer
    output->insert(output->end(),
                   header_buf.data(),
                   header_buf.data() + header_buf.size());
    output->insert(output->end(),
                   data_buf.data(),
                   data_buf.data() + data_buf.size());
}
Beispiel #2
0
vector<double> set_para_val(int argc_, char *argv_[], int& sites, int& cutoff, int& sweep,
                            int& sector, vector<string>& para_name, string& filename)
{
  vector<double> ret(para_name.size(),0);
  if(ret.size()) ret[0] = 1;
  string str_bef, str_aft;
  for(int i=0;i<argc_;i++)
  {
    string str_input(argv_[i]);
    split(str_input, "=", str_bef, str_aft);
    if(str_bef.compare("l") == 0)
      sites = (int)atof(str_aft.c_str());
    if(str_bef.compare("m") == 0)
      cutoff = (int)atof(str_aft.c_str());
    if(str_bef.compare("n") == 0)
      sweep = (int)atof(str_aft.c_str());
    if(str_bef.compare("s") == 0)
      sector = (int)atof(str_aft.c_str());
    for(int j=0;j<para_name.size();j++) if(str_bef.compare(para_name[j]) == 0)
      ret[j] = atof(str_aft.c_str());
  }
  
  cout << "\n ***** The parameters for this running ***** \n\n"
          "  sites(l) = " << sites << "\t cutoff(m) = " << cutoff
       << "\t sweep(n) = " << sweep << "\t symmetry_section(s) = " << sector << endl;
  for(int i=0;i<ret.size();i++) cout << "\t " << para_name[i] << " = " << ret[i] << endl;
  cout << endl;
  
  std::stringstream ss;
  ss << "_L_" << sites << "_m_" << cutoff << "_n_" << sweep << "_s_" << sector;
  for(int i=0;i<ret.size();i++) ss << "_" << para_name[i] << "_" << ret[i];
  filename = ss.str()+"_.txt";
  
  return ret;
}
Beispiel #3
0
int main(void) {
  int n1, n2, n0;
  char str1[LEN], str2[LEN], str0[LEN];
  int b1[LEN], b2[LEN], b0[LEN];
  int a[LEN];
  int i;

  for ( i = 0; i < LEN; i++ ) {
    a[i] = i;
  }

  scanf("%d %d %d ", &n1, &n2, &n0);
  str_input(str1, n1, str2, n2, str0, n0);

  set_strs_id(str1, str2, str0, b1, b2, b0);

  printf("%s %s %s\n", str1, str2, str0);
  number_output(a, b1, n1);
  number_output(a, b2, n2);
  number_output(a, b0, n0);

  return 0;
}
void DlgDecrypt::on_pushButtonDecrypt_clicked()
{
    bool bSuccessDecrypting = false;
    bool bSuccessVerifying  = false;

    QString qstrNymWhoDecrypted;
    QString qstrNymWhoVerified;

    QString qstrText = ui->plainTextEdit->toPlainText().trimmed();
    // --------------------------------
    if (qstrText.isEmpty())
    {
        // pop up a message box warning that the input text is empty.
        //
        QMessageBox::warning(this, tr("Ciphertext is Empty"),
                             tr("Please paste something to be decrypted/verified."));
        return;
    }
    // --------------------------------
    else //qstrText not empty.
    {
        std::string str_input(qstrText.toStdString());
        opentxs::String    strInput (str_input.c_str());

        if (strInput.Exists())
        {
            if (strInput.Contains("-----BEGIN OT ARMORED ENVELOPE-----"))
            {
                opentxs::OTEnvelope theEnvelope;

                if (theEnvelope.SetFromBookendedString(strInput))
                {
                    opentxs::String strOutput;
                    // -------------------------
                    // First we'll try the default nym, if one is available.
                    //
                    QString qstrTempID = Moneychanger::It()->get_default_nym_id();

                    if (!qstrTempID.isEmpty()) // Default Nym IS available.
                    {
                        std::string  str_nym    (qstrTempID.toStdString());
                        opentxs::String     strNym     (str_nym.c_str());
                        opentxs::Identifier nym_id     (strNym);

                        if (!nym_id.IsEmpty())
                        {
                            opentxs::OTPasswordData thePWData("Recipient passphrase");

                            opentxs::Nym * pNym = opentxs::OTAPI_Wrap::OTAPI()->GetOrLoadPrivateNym(nym_id,
                                                                                   false, //bChecking=false
                                                                                   __FUNCTION__,
                                                                                   &thePWData);
                            if (NULL != pNym)
                            {
                                if (theEnvelope.Open(*pNym, strOutput) && strOutput.Exists())
                                {
                                    bSuccessDecrypting = true;
                                    qstrNymWhoDecrypted = qstrTempID;

                                    strInput  = strOutput;
                                    str_input = strInput.Get();
                                    qstrText  = QString::fromStdString(str_input);
                                }
                            }
                        }
                    }
                    // ------------
                    if (!bSuccessDecrypting) // Default nym is NOT available. Okay let's loop through all the Nyms in the wallet then, and try then all...
                    {
                        const int32_t nym_count = opentxs::OTAPI_Wrap::It()->GetNymCount();
                        // -----------------------------------------------
                        for (int32_t ii = 0; ii < nym_count; ++ii)
                        {
                            //Get OT Nym ID
                            QString OT_nym_id = QString::fromStdString(opentxs::OTAPI_Wrap::It()->GetNym_ID(ii));

                            if (!OT_nym_id.isEmpty())
                            {
                                std::string         str_nym    (OT_nym_id.toStdString());
                                opentxs::String     strNym     (str_nym.c_str());
                                opentxs::Identifier nym_id     (strNym);

                                if (!nym_id.IsEmpty())
                                {
                                    opentxs::OTPasswordData thePWData("Recipient passphrase");

                                    opentxs::Nym * pNym = opentxs::OTAPI_Wrap::OTAPI()->GetOrLoadPrivateNym(nym_id,
                                                                                           false, //bChecking=false
                                                                                           "DlgEncrypt::on_pushButtonDecrypt_clicked",
                                                                                           &thePWData);
                                    if (NULL != pNym)
                                    {
                                        // Okay there is a private key available for this Nym, so let's
                                        // try to open the envelope using it.
                                        //
                                        if (theEnvelope.Open(*pNym, strOutput) && strOutput.Exists())
                                        {
                                            bSuccessDecrypting = true;
                                            qstrNymWhoDecrypted = OT_nym_id;

                                            strInput  = strOutput;
                                            str_input = strInput.Get();
                                            qstrText  = QString::fromStdString(str_input);

                                            break;
                                        }
                                    }
                                }
                                // ------------
                            }
                        } // for
                    } // else default nym not available.
                    // -----------------------
                    if (!bSuccessDecrypting)
                    {
                        QMessageBox::warning(this, tr("Failed Decrypting"),
                                             tr("None of the identities in your wallet (including your default identity, "
                                                "if applicable) were able to open this message."));
                        return;

                    }
                    // -----------------------
                } // if (theEnvelope.SetFromBookendedString(strInput))

            } // if (strInput.Contains("-----BEGIN OT ARMORED ENVELOPE-----"))
            // --------------------------------------------
            // This call to DecodeIfArmored is what handles the: "-----BEGIN OT ARMORED ... -----"

            if (strInput.DecodeIfArmored(false)) // bEscapedIsAllowed=true by default.
            {
                std::string str_decoded(strInput.Get());
                QString qstrDecoded(QString::fromStdString(str_decoded));

                if (!qstrDecoded.isEmpty())
                    qstrText = qstrDecoded;
                // -----------------------------------
                // At this point, we know it's been decrypted, if applicable, and it's been
                // de-armored, if applicable. So now we check to see if it's a signed file.
                //
                if (strInput.Contains("-----BEGIN SIGNED FILE-----"))
                {
                    opentxs::OTSignedFile theSignedFile;

                    if (theSignedFile.LoadContractFromString(strInput))
                    {
                        opentxs::String strSignerNymID = theSignedFile.GetSignerNymID();
                        std::string str_signer_nym(strSignerNymID.Get());
                        QString qstrSignerNym(QString::fromStdString(str_signer_nym));

                        if (!str_signer_nym.empty())
                        {
                            opentxs::OTPasswordData thePWData("Sometimes need to load private part of nym in order to use its public key. (Fix that!)");
                            opentxs::Identifier id_signer_nym(strSignerNymID);
                            const opentxs::Nym * pNym = opentxs::OTAPI_Wrap::OTAPI()->GetOrLoadNym(id_signer_nym,
                                                                                   false, //bChecking=false
                                                                                   __FUNCTION__,
                                                                                   &thePWData);
                            if (NULL != pNym)
                            {
                                if (theSignedFile.VerifySignature(*pNym, &thePWData))
                                {
                                    bSuccessVerifying = true;
                                    qstrNymWhoVerified = qstrSignerNym;

                                    opentxs::String strContents = theSignedFile.GetFilePayload();

                                    if (strContents.Exists())
                                    {
                                        strInput  = strContents;
                                        str_input = strInput.Get();
                                        qstrText  = QString::fromStdString(str_input);
                                    }
                                } // signature verified
                            } // pNym exists
                        } // if str_signer_nym exists
                    } // signed file: load contract from string.
                } // "BEGIN SIGNED FILE"
            } // Decode If Armored.
            // -----------------------------------------------
            // if qstrText still contains something, pop up a dialog to display the result to the user.
            //
            if (!qstrText.isEmpty())
            {
                MTNameLookupQT theLookup;
                QString qstrNymWhoDecryptedName(""), qstrNymWhoVerifiedName("");

                if (!qstrNymWhoDecrypted.isEmpty())
                    qstrNymWhoDecryptedName = QString::fromStdString(theLookup.GetNymName(qstrNymWhoDecrypted.toStdString(), ""));
                if (!qstrNymWhoVerified.isEmpty())
                    qstrNymWhoVerifiedName = QString::fromStdString(theLookup.GetNymName(qstrNymWhoVerified.toStdString(), ""));
                // -------------------------------
                if (qstrNymWhoDecryptedName.isEmpty())
                    qstrNymWhoDecryptedName = qstrNymWhoDecrypted;
                else if (qstrNymWhoDecryptedName != qstrNymWhoDecrypted)
                    qstrNymWhoDecryptedName += QString(" (%1)").arg(qstrNymWhoDecrypted);
                // -------------------------------
                if (qstrNymWhoVerifiedName.isEmpty())
                    qstrNymWhoVerifiedName = qstrNymWhoVerified;
                else if (qstrNymWhoVerifiedName != qstrNymWhoVerified)
                    qstrNymWhoVerifiedName += QString(" (%1)").arg(qstrNymWhoVerified);
                // -------------------------------
                QString qstrType("Output:");

                if (bSuccessVerifying)
                {
                    qstrType = QString("%1: %2").arg(tr("Verified signature by Nym")).arg(qstrNymWhoVerifiedName);
                }
                // -----------
                if (bSuccessDecrypting)
                {
                    if (bSuccessVerifying)
                        qstrType = QString("%1: %2\n%3: %4").arg(tr("Decrypted using Nym")).arg(qstrNymWhoDecryptedName).arg(tr("Verified signature by Nym")).arg(qstrNymWhoVerifiedName);
                    else
                        qstrType = QString("%1: %2").arg(tr("Decrypted using Nym")).arg(qstrNymWhoDecryptedName);
                }
                // -----------
                QString qstrSubTitle(tr("Be sure to copy it somewhere before closing this dialog."));
                // -----------
                // Pop up the result dialog.
                //
                DlgExportedToPass dlgExported(this, qstrText,
                                              qstrType,
                                              qstrSubTitle, false);
                dlgExported.exec();
            } // if (!qstrText.isEmpty())
        } // if strInput
    } //qstrText not empty
}
void PlasmaInterface::ParseResourceListData( const wxString& buffer )
{
	//MUTEX !!
	wxString wxbuf = buffer;

    wxString t_begin = _T("<soap:Envelope");
    wxString t_end = _T("</soap:Envelope>");
    wxString xml_section = wxbuf.Mid( wxbuf.Find( t_begin ) );//first char after t_begin to one before t_end

    wxStringInputStream str_input( xml_section );
    wxXmlDocument xml( str_input );
    assert( xml.GetRoot() );
    wxXmlNode *node = xml.GetRoot()->GetChildren();
    assert( node );

    m_resource_list.clear();

    wxString resourceType ( _T("unknown") );
    node = node->GetChildren();
    assert( node );
    while ( node ) {
        wxString node_name = node->GetName();
        if ( node_name == _T("GetResourceListResponse") ) {
            wxXmlNode* resourceListResult = node->GetChildren();
            assert( resourceListResult );
            while ( resourceListResult ) {
                wxXmlNode* resourceData = resourceListResult->GetChildren();

                while ( resourceData ) {
                    wxXmlNode* resourceDataContent = resourceData->GetChildren();
                    PlasmaResourceInfo info;
                    while ( resourceDataContent ) {
                        wxString rc_node_name = resourceDataContent->GetName();
                        if ( rc_node_name == _T("Dependencies") ){
                            //! TODO
                        }
                        else if ( rc_node_name == _T("InternalName") ){
                            info.m_name = resourceDataContent->GetNodeContent();
                        }
                        else if ( rc_node_name == _T("ResourceType") ){
                            resourceType = resourceDataContent->GetNodeContent();
                            if ( resourceType == _T("Mod") )
                                info.m_type = PlasmaResourceInfo::mod;
                            else if ( resourceType == _T("Map") )
                                info.m_type = PlasmaResourceInfo::map;
                            else
								info.m_type = PlasmaResourceInfo::unknown;
                        }
                        else if ( rc_node_name == _T("SpringHashes") ){
                            //! TODO
                        }
                        resourceDataContent = resourceDataContent->GetNext();
                    }
                    m_resource_list.push_back( info );
                    resourceData = resourceData->GetNext();
                }
                resourceListResult = resourceListResult->GetNext();
            }
        } // end section <GetResourceListResponse/>
        node = node->GetNext();
    }
	PlasmaResourceInfo::VectorToFile( m_resource_list, _T("plasmaresourcelist.sl") );
}
PlasmaResourceInfo PlasmaInterface::ParseResourceInfoData( const int buffer_index )
{
    PlasmaResourceInfo info;
    wxString wxbuf = m_buffers[buffer_index];

    wxString t_begin = _T("<soap:Envelope");
    wxString t_end = _T("</soap:Envelope>");
    wxString xml_section = wxbuf.Mid( wxbuf.Find( t_begin ) );//first char after t_begin to one before t_end

    wxStringInputStream str_input( xml_section );
    wxXmlDocument xml( str_input );
	ASSERT_EXCEPTION( xml.GetRoot(), _T("Plasma: XMLparser: no root") );
    wxXmlNode *node = xml.GetRoot()->GetChildren();
	ASSERT_EXCEPTION( node , _T("Plasma: XMLparser: no first node") );
    wxString resourceType ( _T("unknown") );
    node = node->GetChildren();
	ASSERT_EXCEPTION( node , _T("Plasma: XMLparser: no node") );
    while ( node ) {
        wxString node_name = node->GetName();
        if ( node_name == _T("DownloadFileResponse") ) {
            wxXmlNode* downloadFileResult = node->GetChildren();
			ASSERT_EXCEPTION( downloadFileResult, _T("Plasma: XMLparser: no result section") );
            wxString result = downloadFileResult->GetNodeContent();
            //check result
            wxXmlNode* links = downloadFileResult->GetNext();
			ASSERT_EXCEPTION( links, _T("Plasma: XMLparser: no webseed section") );
            wxXmlNode* url = links->GetChildren();
            while ( url ) {
				wxString seed_url = url->GetNodeContent();
				seed_url.Replace(_T(" "),_T("%20"));
				info.m_webseeds.Add( seed_url );
                url = url->GetNext();
            }
            wxXmlNode* next = links->GetNext();
            while ( next ) {
                wxString next_name = next->GetName();
                if ( next_name == _T("torrentFileName") ) {
                    info.m_torrent_filename = next->GetNodeContent();
                }
                else if ( next_name == _T("dependencies") ) {
                    wxXmlNode* deps = next->GetChildren();
                    while ( deps ) {
                        info.m_dependencies.Add( deps->GetNodeContent() );
                        deps = deps->GetNext();
                    }
                }
                else if ( next_name == _T("resourceType") ) {
                    resourceType = next->GetNodeContent();
                    if ( resourceType == _T("Mod") )
                        info.m_type = PlasmaResourceInfo::mod;
                    else if ( resourceType == _T("Map") )
                        info.m_type = PlasmaResourceInfo::map;
                    else
						info.m_type = PlasmaResourceInfo::unknown;
                }
                next = next->GetNext();
            }
            break;
        } // end section <DownloadFileResponse/>
        node = node->GetNext();
    }
    wxString seeds;
    for ( size_t i = 0; i < info.m_webseeds.Count(); ++i )
        seeds += info.m_webseeds[i] + _T("\n");

    return info;
}