// // Validate a slot against the contents of an open file. At most 'length' bytes // will be read from the file. // bool CodeDirectory::validateSlot(FileDesc fd, size_t length, Slot slot) const { MakeHash<CodeDirectory> hasher(this); Hashing::Byte digest[hasher->digestLength()]; generateHash(hasher, fd, digest, length); return memcmp(digest, (*this)[slot], hasher->digestLength()) == 0; }
void subshell_translate(buffer_t* buf, char* str, size_t len) { static char echo_start[] = "echo -n \""; static char echo_end[] = "\"\n"; short hash; lstr* i; char* text = NULL; if(len == 0) { return; } str = trim(str); if(!*str) { return; } if(language != NULL && translations > 0) { hash = generateHash(str); i = ltable[hash]; while(text == NULL && i != NULL) { if(strcmp(str, i->msgid) == 0) { text = i->msgstr; } else { i = i->next; } } } if(text == NULL) { text = str; } buffer_add(buf, echo_start, strlen(echo_start)); buffer_add(buf, text, strlen(text)); buffer_add(buf, echo_end, strlen(echo_end)); }
// // Validate a slot against data in memory. // bool CodeDirectory::validateSlot(const void *data, size_t length, Slot slot) const { secdebug("codedir", "%p validating slot %d", this, int(slot)); MakeHash<CodeDirectory> hasher(this); Hashing::Byte digest[hasher->digestLength()]; generateHash(hasher, data, length, digest); return memcmp(digest, (*this)[slot], hasher->digestLength()) == 0; }
int main(int argc, char **argv) { uint32_t uid; uint16_t hash; if (argc == 1) { for (uid = 0; uid < 0x100000000UL; uid++) { hash = generateHash(uid); if (hash == 0x3b6a) printf("uid 0x%08x -> hash 0x%04x\n", uid, hash); } return 0; } if (argc != 2) { printf("error: usage %s hex_uid\n", argv[0]); return -1; } sscanf(argv[1], "%x", &uid); printf("uid 0x%08x -> hash 0x%04x\n", uid, generateHash(uid)); }
bool CacheManager::hasCachedFile(const std::string& baseName, const std::string& information) const { size_t pos = baseName.find_first_of("/\\?%*:|\"<>"); if (pos != std::string::npos) { LERROR("Base name '" << baseName << "' consists of illegal character"); return false; } unsigned int hash = generateHash(baseName, information); return _files.find(hash) != _files.end(); }
bool CacheManager::getCachedFile(const std::string& baseName, const std::string& information, std::string& cachedFileName, bool isPersistent) { size_t pos = baseName.find_first_of("/\\?%*:|\"<>"); if (pos != std::string::npos) { LERROR("Base name '" << baseName << "' consists of illegal character"); return false; } unsigned int hash = generateHash(baseName, information); // If we couldn't find the file, we have to generate a directory with the name of the // hash and return the full path containing of the cache path + requested filename + // hash value std::string destinationBase = FileSys.pathByAppendingComponent(_directory, baseName); // If this is the first time anyone requests a cache for the file name, we have to // create the base directory under the cache directory if (!FileSys.directoryExists(destinationBase)) FileSys.createDirectory(destinationBase); std::string destination = FileSys.pathByAppendingComponent(destinationBase, std::to_string(hash)); // The new destination should always not exist, since we checked before if we have the // value in the map and only get here if it isn't; persistent cache entries are always // in the map and non-persistent entries should have been deleted on application close if (!FileSys.directoryExists(destination)) FileSys.createDirectory(destination); auto it = _files.find(hash); if (it != _files.end()) { // If we find the hash, it has been created before and we can just return the // file name to the caller cachedFileName = it->second.file; return true; } // Generate and output the newly generated cache name cachedFileName = FileSys.pathByAppendingComponent(destination, baseName); // Store the cache information in the map CacheInformation info = { cachedFileName, isPersistent }; _files.emplace(hash, info); return true; }
void JourneyInfo::init( const QString &operatorName, const QSet< VehicleType > &vehicleTypes, const QDateTime& departure, const QDateTime& arrival, const QString& pricing, const QString& startStopName, const QString& targetStopName, int duration, int changes, const QString &journeyNews, const QString &journeyNewsUrl, const QStringList &routeStops, const QStringList &routeStopsShortened, const QStringList &routeNews, const QStringList &routeTransportLines, const QStringList &routePlatformsDeparture, const QStringList &routePlatformsArrival, const QList< VehicleType > &routeVehicleTypes, const QList<QDateTime> &routeTimesDeparture, const QList<QDateTime> &routeTimesArrival, const QList<int> &routeTimesDepartureDelay, const QList<int> &routeTimesArrivalDelay, const QList<RouteSubJourney> &routeSubJourneys, int routeExactStops ) { m_operator = operatorName; m_vehicleTypes = vehicleTypes; m_departure = departure; m_arrival = arrival; m_pricing = pricing; m_startStopName = startStopName; m_targetStopName = targetStopName; m_duration = duration; m_changes = changes; m_journeyNews = journeyNews; m_journeyNewsUrl = journeyNewsUrl; m_routeStops = routeStops; m_routeStopsShortened = routeStopsShortened.isEmpty() ? routeStops : routeStopsShortened; m_routeNews = routeNews; m_routeTransportLines = routeTransportLines; m_routePlatformsDeparture = routePlatformsDeparture; m_routePlatformsArrival = routePlatformsArrival; m_routeVehicleTypes = routeVehicleTypes; m_routeTimesDeparture = routeTimesDeparture; m_routeTimesArrival = routeTimesArrival; m_routeTimesDepartureDelay = routeTimesDepartureDelay; m_routeTimesArrivalDelay = routeTimesArrivalDelay; m_routeSubJourneys = routeSubJourneys; m_routeExactStops = routeExactStops; generateHash(); }
void CacheManager::removeCacheFile(const std::string& baseName, const std::string& information) { size_t pos = baseName.find_first_of("/\\?%*:|\"<>"); if (pos != std::string::npos) { LERROR("Base name '" << baseName << "' consists of illegal character"); return; } unsigned int hash = generateHash(baseName, information); auto it = _files.find(hash); if (it != _files.end()) { // If we find the hash, it has been created before and we can just return the // file name to the caller const std::string& cachedFileName = it->second.file; FileSys.deleteFile(cachedFileName); _files.erase(it); } }
//----------------------------------------------------------------------------- GpuProgramPtr ProgramManager::createGpuProgram(Program* shaderProgram, ProgramWriter* programWriter, const String& language, const String& profiles, const StringVector& profilesList, const String& cachePath) { stringstream sourceCodeStringStream; // Generate source code. programWriter->writeSourceCode(sourceCodeStringStream, shaderProgram); String source = sourceCodeStringStream.str(); // Generate program name. String programName = generateHash(source); if (shaderProgram->getType() == GPT_VERTEX_PROGRAM) { programName += "_VS"; } else if (shaderProgram->getType() == GPT_FRAGMENT_PROGRAM) { programName += "_FS"; } // Try to get program by name. HighLevelGpuProgramPtr pGpuProgram = HighLevelGpuProgramManager::getSingleton().getByName( programName, ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); if(pGpuProgram) { return static_pointer_cast<GpuProgram>(pGpuProgram); } // Case the program doesn't exist yet. // Create new GPU program. pGpuProgram = HighLevelGpuProgramManager::getSingleton().createProgram(programName, ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME, language, shaderProgram->getType()); // Case cache directory specified -> create program from file. if (!cachePath.empty()) { const String programFullName = programName + "." + language; const String programFileName = cachePath + programFullName; std::ifstream programFile; // Check if program file already exist. programFile.open(programFileName.c_str()); // Case we have to write the program to a file. if (!programFile) { std::ofstream outFile(programFileName.c_str()); if (!outFile) return GpuProgramPtr(); outFile << source; outFile.close(); } else { // use program file version StringStream buffer; programFile >> buffer.rdbuf(); source = buffer.str(); } } pGpuProgram->setSource(source); pGpuProgram->setParameter("entry_point", shaderProgram->getEntryPointFunction()->getName()); if (language == "hlsl") { // HLSL program requires specific target profile settings - we have to split the profile string. StringVector::const_iterator it = profilesList.begin(); StringVector::const_iterator itEnd = profilesList.end(); for (; it != itEnd; ++it) { if (GpuProgramManager::getSingleton().isSyntaxSupported(*it)) { pGpuProgram->setParameter("target", *it); break; } } pGpuProgram->setParameter("enable_backwards_compatibility", "true"); pGpuProgram->setParameter("column_major_matrices", StringConverter::toString(shaderProgram->getUseColumnMajorMatrices())); } pGpuProgram->setParameter("profiles", profiles); pGpuProgram->load(); // Case an error occurred. if (pGpuProgram->hasCompileError()) { //! [debug_break] pGpuProgram.reset(); //! [debug_break] return GpuProgramPtr(pGpuProgram); } // Add the created GPU program to local cache. if (pGpuProgram->getType() == GPT_VERTEX_PROGRAM) { mVertexShaderMap[programName] = pGpuProgram; } else if (pGpuProgram->getType() == GPT_FRAGMENT_PROGRAM) { mFragmentShaderMap[programName] = pGpuProgram; } return static_pointer_cast<GpuProgram>(pGpuProgram); }
hash StringUtils::stringToHash(const std::string& str) { return generateHash((void*)str.c_str(), str.length()); }
void IRKE::compute_sequence_assemblies(KmerCounter &kcounter, float min_connectivity, unsigned int MIN_ASSEMBLY_LENGTH, unsigned int MIN_ASSEMBLY_COVERAGE, bool PARALLEL_IWORM, bool WRITE_COVERAGE, string COVERAGE_OUTPUT_FILENAME) { if (!got_sorted_kmers_flag) { stringstream error; error << stacktrace() << " Error, must populate_sorted_kmers_list() before computing sequence assemblies" << endl; throw (error.str()); } //vector<Kmer_counter_map_iterator>& kmers = sorted_kmers; vector<Kmer_Occurence_Pair> &kmers = sorted_kmers; // note, these are not actually sorted if PARALLEL_IWORM mode. unsigned long init_size = kcounter.size(); cerr << "Total kcounter hash size: " << init_size << " vs. sorted list size: " << kmers.size() << endl; unsigned int kmer_length = kcounter.get_kmer_length(); ofstream coverage_writer; if (WRITE_COVERAGE) { coverage_writer.open(COVERAGE_OUTPUT_FILENAME.c_str()); } // string s = "before.kmers"; // kcounter.dump_kmers_to_file(s); /* ----------------------------------------------------------- Two reconstruction modes. (bhaas, Jan-4-2014) 1. Original (not setting PARALLEL_IWORM): kmer list is sorted descendingly by abundance. Seed kmer is chosen as most abundant, and contig construction extends from this seed. 2. PARALLEL_IWORM: the kmer list is not sorted. A random kmer (just ordered by hash iterator, nothing special, probably not so random) is used as a seed for draft contig reconstruction. The most abundant kmer in the draft contig is chosen as a proper seed. An inchworm contig is then constructed using this new seed, and reported. So, in this case, the draft contig in the first phase is just used to find a better seed, from which the inchworm contig is then properly reconstructed. --------------------------------------------------------------- */ // try building an inchworm contig from each seed kmer int myTid; if (PARALLEL_IWORM) { omp_set_num_threads(IRKE_COMMON::NUM_THREADS); } else { omp_set_num_threads(1); // turn off multithreading for the contig building. } //----------------------------------------------------------------- // Prep writing to separate inchworm output files for each thread //----------------------------------------------------------------- vector<iworm_tmp_file> tmpfiles; int num_threads = omp_get_max_threads(); cerr << "num threads set to: " << num_threads << endl; for (int i = 0; i < num_threads; i++) { iworm_tmp_file tmpfile_struct; tmpfiles.push_back(tmpfile_struct); iworm_tmp_file &itmp = tmpfiles[i]; stringstream filename_constructor; filename_constructor << "tmp.iworm.fa.pid_" << getpid() << ".thread_" << i; itmp.tmp_filename = new char[100]; strcpy(itmp.tmp_filename, filename_constructor.str().c_str()); itmp.tmp_filename[filename_constructor.str().length()] = '\0'; itmp.fh = new ofstream(); itmp.fh->open(itmp.tmp_filename); cerr << "Done opening file. " << itmp.tmp_filename << endl; } //------------------- // Build contigs. //------------------- #pragma omp parallel for private (myTid) schedule (dynamic, 1000) for (unsigned int i = 0; i < kmers.size(); i++) { // cerr << "round: " << i << endl; myTid = omp_get_thread_num(); unsigned long kmer_counter_size = kcounter.size(); if (kmer_counter_size > init_size) { // string s = "after.kmers"; // kcounter.dump_kmers_to_file(s); stringstream error; error << stacktrace() << "Error, Kcounter size has grown from " << init_size << " to " << kmer_counter_size << endl; throw (error.str()); } //kmer_int_type_t kmer = kmers[i]->first; //unsigned int kmer_count = kmers[i]->second; kmer_int_type_t kmer = kmers[i].first; // unsigned int kmer_count = kmers[i].second; // NO!!! Use for sorting, but likely zeroed out in the hashtable after contig construction unsigned int kmer_count = kcounter.get_kmer_count(kmer); if (!is_good_seed_kmer(kmer, kmer_count, kmer_length, min_connectivity)) { continue; } // cout << "SEED kmer: " << kcounter.get_kmer_string(kmer) << ", count: " << kmer_count << endl; if (IRKE_COMMON::MONITOR >= 2) { cerr << "SEED kmer: " << kcounter.get_kmer_string(kmer) << ", count: " << kmer_count << endl; } if (IRKE_COMMON::MONITOR >= 2) { #pragma omp critical cerr << "Seed for thread: " << myTid << " is " << kcounter.get_kmer_string(kmer) << " with count: " << kmer_count << endl; } unsigned int total_counts; vector<kmer_int_type_t> joined_path = build_inchworm_contig_from_seed(kmer, kcounter, min_connectivity, total_counts, PARALLEL_IWORM); if (PARALLEL_IWORM && TWO_PHASE) { // get a new seed based on the draft contig // choose the 'good' kmer with highest abundance kmer_int_type_t new_seed = extract_best_seed(joined_path, kcounter, min_connectivity); if (kcounter.get_kmer_count(new_seed) == 0) { continue; // must have been zapped by another thread } joined_path = build_inchworm_contig_from_seed(new_seed, kcounter, min_connectivity, total_counts, PARALLEL_IWORM); } // report sequence reconstructed from path. vector<unsigned int> assembly_base_coverage; string sequence = reconstruct_path_sequence(kcounter, joined_path, assembly_base_coverage); unsigned int avg_cov = static_cast<unsigned int> ((float) total_counts / (sequence.length() - kcounter.get_kmer_length() + 1) + 0.5); /* cout << "Inchworm-reconstructed sequence, length: " << sequence.length() << ", avgCov: " << avg_cov << " " << sequence << endl; */ size_t contig_length = sequence.length(); if (contig_length >= MIN_ASSEMBLY_LENGTH && avg_cov >= MIN_ASSEMBLY_COVERAGE) { *(tmpfiles[myTid].fh) << total_counts << endl << avg_cov << endl << kmer_count << endl << sequence << endl; } // remove path if (IRKE_COMMON::__DEVEL_zero_kmer_on_use) { // dont forget the seed. The forward/reverse path kmers already cleared. kcounter.clear_kmer(kmer); } else { for (unsigned int i = 0; i < joined_path.size(); i++) { kmer_int_type_t kmer = joined_path[i]; kcounter.clear_kmer(kmer); } } } if (IRKE_COMMON::MONITOR) { cerr << endl; } if (WRITE_COVERAGE) { coverage_writer.close(); } // drop sorted kmer list as part of cleanup clear_sorted_kmers_list(); //------------------------------------------------------------------------------ // examine the contigs generated by the individual threads, remove redundancies. //------------------------------------------------------------------------------ map<unsigned long long, bool> seen_contig_already; for (unsigned int i = 0; i < tmpfiles.size(); i++) { tmpfiles[i].fh->close(); ifstream tmpreader(tmpfiles[i].tmp_filename); while (!tmpreader.eof()) { unsigned int total_counts; unsigned int avg_cov; unsigned int kmer_count; string sequence; tmpreader >> total_counts >> avg_cov >> kmer_count >> sequence; if (tmpreader.eof()) // apparently only happens on the read after the last line is read. break; //cerr << "Read sequence: " << sequence << endl; unsigned int contig_hash = generateHash(sequence); if (!seen_contig_already[contig_hash] ) { seen_contig_already[contig_hash] = true; INCHWORM_ASSEMBLY_COUNTER++; stringstream headerstream; headerstream << ">a" << INCHWORM_ASSEMBLY_COUNTER << ";" << avg_cov << " total_counts: " << total_counts //<< " Fpath: " << selected_path_n_pair_forward.second << " Rpath: " << selected_path_n_pair_reverse.second << " Seed: " << kmer_count << " K: " << kmer_length << " length: " << sequence.length(); string header = headerstream.str(); sequence = add_fasta_seq_line_breaks(sequence, 60); cout << header << endl << sequence << endl; } } // cleanup from earlier dynamic allocation delete (tmpfiles[i].fh); if (!IRKE_COMMON::KEEP_TMP_FILES) { remove(tmpfiles[i].tmp_filename); } delete (tmpfiles[i].tmp_filename); } /* if (WRITE_COVERAGE) { coverage_writer << header << endl; for (unsigned int i = 0; i < assembly_base_coverage.size(); i++) { coverage_writer << assembly_base_coverage[i]; if ( (i+1) % 30 == 0) { coverage_writer << endl; } else { coverage_writer << " "; } } coverage_writer << endl; } } */ return; // end of runIRKE }
//Parses the fen string, sets up the position. WARNING: no checks are currently //done to validate the fen string. Make sure it is good! void Position::fenParse(string fen){ //Clears the board, sets up NOBOARD zones. for (int i = 0; i < 120; i++) { if (i/10 < 2 || i/10 > 9 || i%10 == 0 || i%10 == 9) board[i] = NOBOARD; else board[i] = EMPTY; } //Holds the cut up fen string string FenPieces[13]; //This block cuts the fen string into segments int count = 0; string temp = ""; for (unsigned int i = 0; i < fen.size(); i++){ if (fen[i] == ' ' || fen[i] == '/'){ FenPieces[count] = temp; temp = ""; count++; } else temp += fen[i]; } FenPieces[12] = temp; ///This block places the pieces into the board[120] array for (unsigned int i = 0; i < 8; i++) { int pos = 0; for (unsigned int j = 0; j < FenPieces[i].size(); j++) { int space = 21+i*10+pos; switch(FenPieces[i][j]){ case 'r': board[space] = -ROOK; blackPiecelist.push_back(space); pos++; break; case 'n': board[space] = -KNIGHT; blackPiecelist.push_back(space); pos++; break; case 'b': board[space] = -BISHOP; blackPiecelist.push_back(space); pos++; break; case 'q': board[space] = -QUEEN; blackPiecelist.push_back(space); pos++; break; case 'k': board[space] = -KING; blackPiecelist.push_back(space); blackKing = space; pos++; break; case 'p': board[space] = -PAWN; blackPiecelist.push_back(space); pos++; break; case 'R': board[space] = ROOK; whitePiecelist.push_back(space); pos++; break; case 'N': board[space] = KNIGHT; whitePiecelist.push_back(space); pos++; break; case 'B': board[space] = BISHOP; whitePiecelist.push_back(space); pos++; break; case 'Q': board[space] = QUEEN; whitePiecelist.push_back(space); pos++; break; case 'K': board[space] = KING; whitePiecelist.push_back(space); whiteKing = space; pos++; break; case 'P': board[space] = PAWN; whitePiecelist.push_back(space); pos++; break; case '1': pos += 1; break; case '2': pos += 2; break; case '3': pos += 3; break; case '4': pos += 4; break; case '5': pos += 5; break; case '6': pos += 6; break; case '7': pos += 7; break; case '8': pos += 8; break; } } } //Assesses whos turn it is. if (FenPieces[8][0] == 'w') toMove = 1; else toMove = -1; //Castling castleWK = false; castleWQ = false; castleBK = false; castleBQ = false; for (unsigned int i = 0; i < FenPieces[9].size(); i++){ switch(FenPieces[9][i]){ case 'K': castleWK = true; break; case 'Q': castleWQ = true; break; case 'k': castleBK = true; break; case 'q': castleBQ = true; break; } } //Enpassant space if (FenPieces[10][0] != '-') enPassant = fromAlgebraic(FenPieces[10]); else enPassant = 0; fiftyMove = atoi(FenPieces[11].c_str()); totalMoves = atoi(FenPieces[12].c_str()); halfMoves = 0; keySeed = 80; blackKey = genRandomKey(); for (unsigned int i = 0; i < 64; i++) { whitePawnKeys[i] = genRandomKey(); whiteKnightKeys[i] = genRandomKey(); whiteBishopKeys[i] = genRandomKey(); whiteRookKeys[i] = genRandomKey(); whiteQueenKeys[i] = genRandomKey(); whiteKingKeys[i] = genRandomKey(); blackPawnKeys[i] = genRandomKey(); blackKnightKeys[i] = genRandomKey(); blackBishopKeys[i] = genRandomKey(); blackRookKeys[i] = genRandomKey(); blackQueenKeys[i] = genRandomKey(); blackKingKeys[i] = genRandomKey(); if (i < 8) enpassantKeys[i] = genRandomKey(); if (i < 4) castleKeys[i] = genRandomKey(); } generateHash(); }
bool SHA256Algorithm::verifyHash(const QByteArray& message, const QByteArray& hash) { return generateHash(message) == hash; }
void Conformation::setAtomPositions(const AtomContainer* parent, const HashMap < Size, Vector3 > & pos) { atom_positions_ = pos; number_of_atoms_ = parent->countAtoms(); generateHash(parent, pos, id_); }
Conformation::Conformation(AtomContainer* parent, AtomContainer* conformation_molecule) { generateHash(conformation_molecule, id_); takeConformation(*parent, *conformation_molecule); }