uint32 getFileVersion(const NLMISC::CSString& fileName) { // start at the back of the file name and scan forwards until we find a '/' or '\\' or ':' or a digit uint32 i= fileName.size(); while (i--) { char c= fileName[i]; // if we've hit a directory name separator then we haven't found a version number so drop out if (c=='/' || c=='\\' || c==':') return ~0u; // if we've found a digit then construct the rest of the version number and return if (isdigit(c)) { uint32 firstDigit= i; while (firstDigit!=0 && isdigit(fileName[firstDigit-1])) { --firstDigit; } return fileName.leftCrop(firstDigit).left(i-firstDigit+1).atoui(); } } // default to our 'invalid' value return ~0u; }
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; }