Example #1
0
bool CUserComponentValidator::setUserComponentToSave(const std::string& filename, const CUserComponentValidator::TValues& values, std::string& headerMD5, const std::string &body)
{
    _Filename = filename;
//	std::ostringstream out2;
//	out2.str(body);

//	_UserComponentBody =out2.str();

    _UserComponentBody = body;

    {
        NLMISC::CHashKeyMD5 md5Id = NLMISC::getMD5((uint8*)_UserComponentBody.data(), (uint32)_UserComponentBody.size());
        _BodyMd5 = md5Id.toString().c_str();
    }

//	out2.str("");
//	out2 << NLMISC::toString("-- BodyMD5 = '%s'\n", _BodyMd5.c_str() );
//	TValues::const_iterator first(values.begin()), last(values.end());
//	for (; first != last; ++first)
//	{
    //>first->second.c_str()) "\n" => "\\n"
//		NLMISC::CSString tmp = first->second.c_str();
//		tmp = tmp.replace("\n", "\\n");

//		out2 << NLMISC::toString("-- %s = '%s'\n", first->first.c_str(), tmp.c_str());
//	}


//	_HeaderBody =out2.str();


    _HeaderBody = NLMISC::toString("-- BodyMD5 = '%s'\n", _BodyMd5.c_str() );

    TValues::const_iterator first(values.begin()), last(values.end());
    for (; first != last; ++first)
    {
        //>first->second.c_str()) "\n" => "\\n"
        NLMISC::CSString tmp = first->second.c_str();
        tmp = tmp.replace("\n", "\\n");

        _HeaderBody += NLMISC::toString("-- %s = '%s'\n", first->first.c_str(), tmp.c_str());
    }


    std::string headerBodyMd5;
    {
        NLMISC::CHashKeyMD5 md5Id = NLMISC::getMD5((uint8*)_HeaderBody.data(), (uint32)_HeaderBody.size());
        _HeaderMd5 = md5Id.toString().c_str();
        headerMD5 = _HeaderMd5;
    }

    return true;
}
Example #2
0
bool CScenarioValidator::setScenarioToSave(const std::string& filename, CObject* scenario, const CScenarioValidator::TValues& values, std::string& headerMD5)
{
    _Filename = filename;
    //std::ostringstream out2;
    //out2.str("");
    std::string out2;

    if (!scenario)
    {
        return false;
    }

    //out2 <<"scenario = "<< *scenario ;
    out2 += "scenario = ";
    scenario->serialize(out2);

    _ScenarioBody = out2;
    {
        NLMISC::CHashKeyMD5 md5Id = NLMISC::getMD5((uint8*)_ScenarioBody.data(),(uint32) _ScenarioBody.size());
        _BodyMd5 = md5Id.toString().c_str();
    }


    out2.clear();
    //out2.str("");
    out2 += NLMISC::toString("-- BodyMD5 = '%s'\n", _BodyMd5.c_str() );
    TValues::const_iterator first(values.begin()), last(values.end());
    for (; first != last; ++first)
    {
        //>first->second.c_str()) "\n" => "\\n"
        NLMISC::CSString tmp = first->second.c_str();
        tmp = tmp.replace("\n", "\\n");

        out2 += NLMISC::toString("-- %s = '%s'\n", first->first.c_str(), tmp.c_str());
    }

    _HeaderBody =out2;
    std::string headerBodyMd5;
    {
        NLMISC::CHashKeyMD5 md5Id = NLMISC::getMD5((uint8*)_HeaderBody.data(), (uint32)_HeaderBody.size());
        _HeaderMd5 = md5Id.toString().c_str();
        headerMD5 = _HeaderMd5;
    }
    return true;
}
Example #3
0
bool CScenarioValidator::setScenarioToLoad( const std::string& filename, CScenarioValidator::TValues& values, std::string& md5,  std::string& signature, bool checkMD5)
{

    values.clear();

    _Filename = filename;
    _Values.clear();
    // open our input file
    NLMISC::CIFile inf;
    if (!inf.open(_Filename, true) )
    {
        nlwarning("Can't load scenario %s", _Filename.c_str());
        return false;
    }

    try
    {
        static const char * header = "---- Header\n";
        static const char * slashheader = "---- /Header\n\n";
        static const char * comment = "-- ";

        static const unsigned int headerLen = (unsigned int)strlen(header);
        static const unsigned int slasheaderLen = (unsigned int)strlen(slashheader);
        static const unsigned int commentLen = (unsigned int)strlen(comment);


        NLMISC::CSString tmp;
        tmp.resize( inf.getFileSize() );
        inf.serialBuffer((uint8*)&tmp[0], (uint)tmp.size());
        _ScenarioBody = tmp.replace("\r\n", "\n");





        // Scenario without header
        if (_ScenarioBody.size() < headerLen ||_ScenarioBody.substr(0, headerLen) != header )
        {
            md5 = "";
            signature = "";
            inf.close();
            return true;
        }

        std::string::size_type endHeader = _ScenarioBody.find(slashheader, headerLen);
        if (endHeader == std::string::npos ) {
            inf.close();
            return false;
        }
        std::string::size_type startHeader = headerLen;

        std::vector<std::string> lines;
        NLMISC::splitString( _ScenarioBody.substr(startHeader, endHeader - startHeader), "'\n", lines);
        std::vector<std::string>::const_iterator firstLine(lines.begin()), lastLine(lines.end());
        std::vector<std::string> result;

        for (; firstLine != lastLine ; ++firstLine)
        {
            result.clear();
            NLMISC::splitString(*firstLine, " = '", result);
            if (result.size() == 1)
            {
                result.push_back("");
            }
            if (result.size() == 2)
            {
                if (result[0].find(comment) != std::string::npos)
                {
                    //>result[1]"\\n" => "\n"
                    NLMISC::CSString tmp = result[1];
                    tmp = tmp.replace("\\n", "\n");

                    values.push_back( std::make_pair( result[0].substr(commentLen), tmp));
                }
            }
        }

        if (values.size() >=2
                && values[0].first == "Version"
                && values[1].first == "Signature"
                && values[2].first == "HeaderMD5" && values[3].first =="BodyMD5")
        {
            std::string headerBodyMd5;
            std::string::size_type subHeader = _ScenarioBody.find("-- BodyMD5", startHeader);
            if (checkMD5)
            {
                std::string md5Id1 = NLMISC::getMD5((uint8*)(_ScenarioBody.data() + subHeader), (uint32)(endHeader - subHeader)).toString();
                if (values[2].second != md5Id1 )
                {
                    return false;
                }
                std::string md5Id2 = NLMISC::getMD5((uint8*)(_ScenarioBody.data() + endHeader + slasheaderLen), (uint32)(_ScenarioBody.size() - (endHeader + slasheaderLen))).toString();
                if (values[3].second != md5Id2)
                {
                    return false;
                }

            }
            md5 = values[2].second;
            signature = values[1].second;

        }


    }
    catch(...)
    {
        _Values = values;
        nlwarning("Can't load scenario %s", _Filename.c_str());
        return false;
    }

    _Values = values;
    // close the file
    inf.close();
    return true;

}