int feistelDecrypt( const std::string &strDesKey, std::string &strPlain, const std::string &strCipher ) { const int LENGTH = 8; int iLength = strCipher.length(); if( iLength % LENGTH != 0 ) return -1; char szDesKey[ LENGTH ]; memset( szDesKey, 0, LENGTH ); strDesKey.copy( szDesKey, LENGTH ); char subKey[16][4]; genSubKey( szDesKey, subKey ); char plain[ LENGTH ]; char cipher[ LENGTH ]; strPlain.clear(); int iLoop = iLength / LENGTH; for( int i = 0; i < iLoop; i++ ) { strCipher.copy( cipher, LENGTH, i * LENGTH ); feistelDecryptUnit( 16, subKey, plain, cipher ); strPlain.append( plain, LENGTH ); } bool bPadEight = true; for( int i = 0; i < LENGTH; i++ ) { if( plain[ i ] != LENGTH ) { bPadEight = false; break; } } if( bPadEight ) // 填充了 8 个字节 { strPlain.erase( iLength - LENGTH ); } else { int iValue = 7; for( int i = 1; i < LENGTH; i++ ) { if( plain[ i ] == iValue ) continue; iValue = 7 - i; } if( iValue > 0 ) { strPlain.erase( iLength - iValue ); } } return 0; }
/*static*/ void rrdb_client_impl::generate_key(dsn::blob& key, const std::string& hash_key, const std::string& sort_key) { int len = 4 + hash_key.size() + sort_key.size(); char* buf = new char[len]; *(int*)buf = hash_key.size(); hash_key.copy(buf + 4, hash_key.size(), 0); sort_key.copy(buf + 4 + hash_key.size(), sort_key.size(), 0); std::shared_ptr<char> buffer(buf); key.assign(buffer, 0, len); }
void TripleDESCryptor::InitKeys(const std::string& key24) { BOOST_ASSERT(key24.size() == KEY_LEN*3); DES_cblock k; key24.copy((char*)&k, KEY_LEN); DES_set_key_unchecked(&k, &ks1_); key24.copy((char*)&k, KEY_LEN, KEY_LEN); DES_set_key_unchecked(&k, &ks2_); key24.copy((char*)&k, KEY_LEN, KEY_LEN*2); DES_set_key_unchecked(&k, &ks3_); }
void Element::unserialize(std::string &buffer) { buffer.copy((char*)&key,sizeof(Key)); buffer.erase(0,sizeof(Key)); buffer.copy((char*)&dataSize,sizeof(DataSize)); buffer.erase(0,sizeof(DataSize)); data = new char[dataSize]; buffer.copy((char*)data,dataSize); buffer.erase(0,dataSize); }
void get_options(crack_opt_t *opts) { memset(opts, 0, sizeof(crack_opt_t)); opts->start_cbn = start_cbn; opts->end_cbn = end_cbn; opts->total_cbn = total_cbn; opts->thread_cbn = (total_cbn/thread_cnt); opts->thread_cnt = thread_cnt; alphabet.copy(opts->alphabet, 128, 0); start_pwd.copy(opts->start_pwd, MAX_PWD, 0); end_pwd.copy(opts->end_pwd, MAX_PWD, 0); }
// We'll allow our audioQueue to be significantly larger than our video queue // This is safe because A) audio is a lot smaller, so it isn't a big deal to hold on to a lot of it and // B) our audio sample rate is typically 20 msec packets; video is anywhere from 33 to 100 msec (30 to 10 fps) // Allowing the audio queue to hold more will help prevent loss of data when the video framerate is low. ExternalOutput::ExternalOutput(const std::string& outputUrl) : fec_receiver_(this), audioQueue_(600, 60), videoQueue_(120, 60), inited_(false), video_stream_(NULL), audio_stream_(NULL), firstVideoTimestamp_(-1), firstAudioTimestamp_(-1), firstDataReceived_(-1), videoOffsetMsec_(-1), audioOffsetMsec_(-1), vp8SearchState_(lookingForStart) { ELOG_DEBUG("Creating output to %s", outputUrl.c_str()); // TODO these should really only be called once per application run av_register_all(); avcodec_register_all(); context_ = avformat_alloc_context(); if (context_==NULL){ ELOG_ERROR("Error allocating memory for IO context"); } else { outputUrl.copy(context_->filename, sizeof(context_->filename),0); context_->oformat = av_guess_format(NULL, context_->filename, NULL); if (!context_->oformat){ ELOG_ERROR("Error guessing format %s", context_->filename); } else { context_->oformat->video_codec = AV_CODEC_ID_VP8; context_->oformat->audio_codec = AV_CODEC_ID_NONE; // We'll figure this out once we start receiving data; it's either PCM or OPUS } } unpackagedBufferpart_ = unpackagedBuffer_; lastFullIntraFrameRequest_ = 0; sinkfbSource_ = this; fbSink_ = NULL; unpackagedSize_ = 0; }
RecordType getRecordType(const std::string& record) { // the record type is the first two characters of the record if(record.size() < 2) { std::cerr << "Error: record does not have a valid record-type tag" << std::endl; std::cerr << "Record: " << record << std::endl; exit(EXIT_FAILURE); } char recordTag[RECORD_TAG_SIZE]; record.copy(recordTag, RECORD_TAG_SIZE); if(strncmp(recordTag, HEADER_TAG, RECORD_TAG_SIZE) == 0) { return RT_HEADER; } if(strncmp(recordTag, VERTEX_TAG, RECORD_TAG_SIZE) == 0) { return RT_VERTEX; } if(strncmp(recordTag, EDGE_TAG, RECORD_TAG_SIZE) == 0) { return RT_EDGE; } // Unknown tag std::cerr << "Error: Unknown ASQG file record tag: " << recordTag << std::endl; exit(EXIT_FAILURE); }
static void CheckAddressIsInSet(const std::string& if_name, bool unicast, const std::set<in_addr_t>& addrs) { ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_addr.sa_family = AF_INET; if_name.copy(ifr.ifr_name, IFNAMSIZ - 1); int fd = socket(AF_INET, SOCK_DGRAM, 0); ASSERT_TRUE(fd != -1); int request = SIOCGIFADDR; if (!unicast) { // For non-unicast, the specific ioctl to use depends on whether the IFF_BROADCAST flag is set. ASSERT_EQ(0, ioctl(fd, SIOCGIFFLAGS, &ifr)) << if_name << ' ' << strerror(errno); request = ((ifr.ifr_flags & IFF_BROADCAST) != 0) ? SIOCGIFBRDADDR : SIOCGIFDSTADDR; } ASSERT_EQ(0, ioctl(fd, request, &ifr)) << if_name << ' ' << strerror(errno); close(fd); sockaddr_in* sock = reinterpret_cast<sockaddr_in*>(&ifr.ifr_addr); in_addr_t addr = sock->sin_addr.s_addr; EXPECT_TRUE(addrs.find(addr) != addrs.end()) << if_name << ' ' << std::hex << ntohl(addr); }
void matrix::set_sequences(std::string sequence1, std::string sequence2) { // dont want to maintain a different version for vc++ and g++ #pragma warning( disable : 4996 ) if(this->sequence1) delete[] this->sequence1; this->sequence1 = new char [sequence1.size()+1]; sequence1.copy(this->sequence1, sequence1.size()+1, 0); if(this->sequence2) delete[] this->sequence2; this->sequence2 = new char [sequence2.size()+1]; sequence2.copy(this->sequence2, sequence2.size()+1, 0); rows = sequence1.size() + 1; cols = sequence2.size() + 1; }
void network::writeMessage(std::string s) { char input[1024]; bzero(input,1024); s.copy(input,1024,0); sendto(sockfd, input, sizeof(input), 0, (struct sockaddr*)&name, sizeof(name)); }
TimeSec ID_SOYAL_DL_Week::GetTimeSect(std::string time_str) { char time_hh[3]="",time_mm[3]=""; TimeSec timesec; memset(×ec,0,sizeof timesec); if(time_str.length()==8) { time_str.copy(time_hh,2); time_str.copy(time_mm,2,2); timesec.begin_time = atoi(time_hh)*60 + atoi(time_mm); // 开始时间(分钟) time_str.copy(time_hh,2,4); time_str.copy(time_mm,2,6); timesec.end_time = atoi(time_hh)*60 + atoi(time_mm); // 结束时间(分钟) } return timesec; }
void textadventure::bcrypt::performHash( uvpp::Loop& loop, std::string plaintext, std::string salt, std::function<void (std::string)> callback ) { loop.doWork( [plaintext, salt]() { auto hash = new char[BCRYPT_HASHSIZE]; char csalt[BCRYPT_HASHSIZE]; auto csaltLength = salt.copy(csalt, salt.length(), 0); csalt[csaltLength] = '\0'; auto cplaintext = plaintext.c_str(); bcrypt_hashpw(cplaintext, csalt, hash); auto str = new std::string(hash); delete hash; return str; }, [callback](void* result) { auto str = std::unique_ptr<std::string>( reinterpret_cast<std::string*>(result) ); callback(*str); } ); }
void setData(int aWidth, int aHeight, std::string const & newData) { if ( data ) delete data; width = aWidth; height = aHeight; data = new unsigned char[ dataSize() ]; newData.copy( reinterpret_cast<char *>(data), dataSize() ); }
void BNode::unserialize(std::string &buffer) { buffer.copy((char*)&level,sizeof(Level)); buffer.erase(0,sizeof(Level)); //buffer.copy((char*)®isterCounter,sizeof(RegisterCounter)); //buffer.erase(0,sizeof(RegisterCounter)); }
bool FileSystem::deleteFromAddress(std::string address, std::string remoteFile) { int connectionToServer; //TCP connect to introducer/other nodes // printf("Local file: %s\n", localFile.c_str()); // printf("Remote file: %s\n", remoteFile.c_str()); logFile << "Deleting - Connecting to "<< address << "..." << std::endl; int ret = connect_to_server(address.c_str(), port, &connectionToServer); if(ret!=0) { logFile <<"ERROR putToAddress: Cannot connect to "<<address<< std::endl; std::cout <<"ERROR putToAddress: Cannot connect to "<<address<< std::endl; return false; } else{ Message_fs msg; msg.type = MSG_DELETE; memset(msg.filename, '\0', 200); remoteFile.copy(msg.filename, remoteFile.length()); msg.filename[remoteFile.length()+1] = '\0'; msg.size = 0; write(connectionToServer, &msg, sizeof(Message_fs) ); close(connectionToServer); return true; } }
void scottgs::verseCounter() { // ========================= // worker function // ========================= int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); std::cout << "scottgs::verseCounter["<< rank << "], waiting for work" << std::endl; char msg[scottgs::LINE_MESSAGE_SIZE]; MPI_Status status; while (1) { // Receive a message from the master MPI_Recv(msg, /* message buffer */ scottgs::LINE_MESSAGE_SIZE, /* buffer size */ MPI_CHAR, /* data item is an integer */ 0, /* Receive from master */ MPI_ANY_TAG, MPI_COMM_WORLD, /* default communicator */ &status); // Check if we have been terminated by the master // exit from the worker loop if (status.MPI_TAG == scottgs::TERMINATE) { std::cout << "scottgs::verseCounter["<< rank << "], recieved terminate signal" << std::endl; return; } const int index = status.MPI_TAG; // Convert the message into a string for parse work std::string line(msg); #if GJS_DEBUG_PRINT std::cout << "scottgs::verseCounter[" << rank << "], recieved (" << line << "), status = " << index << std::endl; #endif // We have now built up a term=count comma separated list // We can send the result back const std::string resultLine = scottgs::workerCountWords(line, rank); const size_t length = resultLine.size(); char resultMsg[scottgs::RESULT_MESSAGE_SIZE]; resultLine.copy(resultMsg,length); resultMsg[length] = '\0'; #if GJS_DEBUG_PRINT std::cout << "scottgs::verseCounter[" << rank << "], result list (" << resultLine << ")" << std::endl; #endif MPI_Send(resultMsg, /* message buffer */ scottgs::RESULT_MESSAGE_SIZE, /* buffer size */ MPI_CHAR, /* data item is an integer */ 0, /* destination process rank, the master */ index, /* user chosen message tag */ MPI_COMM_WORLD); /* default communicator */ } // end of an infinite loop return; }
void Magick::Options::fileName(const std::string &fileName_) { fileName_.copy(_imageInfo->filename,MagickPathExtent-1); if (fileName_.length() > MagickPathExtent-1) _imageInfo->filename[MagickPathExtent-1]=0; else _imageInfo->filename[fileName_.length()]=0; }
void SocketConnection::beginWrite(const std::string& str, boost::any tag /* = NULL */) { size_t len = str.size(); boost::shared_array<char> data(new char[len]); str.copy(data.get(), len); Proactor::instance()->beginWrite(this->shared_from_this(), boost::bind(&Connection::onWriteComplete,this,_1,_2), Buffer<char>(data, len, len), tag); }
bool FileSystem::getFromAddress(std::string address, std::string localFile, std::string remoteFile) { int connectionToServer; //TCP connect to introducer/other nodes logFile << "getFromAddress: Connecting to "<< address << "..." << std::endl; int ret = connect_to_server(address.c_str(), port, &connectionToServer); if(ret!=0) { logFile <<"ERROR getFromAddress: Cannot connect to "<<address<< std::endl; std::cout <<"ERROR getFromAddress: Cannot connect to "<<address<< std::endl; return false; } else{ Message_fs msg; msg.type = MSG_GET; memset(msg.filename, '\0', 200); remoteFile.copy(msg.filename, remoteFile.length()); write(connectionToServer, &msg, sizeof(Message_fs)); // Send filename read (connectionToServer, &msg, sizeof(Message_fs)); // Receive size if (msg.size == 0) { std::cout << "File does not exist: " << remoteFile << std::endl; close(connectionToServer); return false; } else { char * buffer = new char [msg.size]; //size_t ret = read(connectionToServer, buffer, msg.size); size_t ret =splitRead( connectionToServer, buffer, msg.size ); // std::cout << "getFromAddress: Received: " << ret <<" of "<<msg.size << std::endl; std::ofstream file(localFile, std::ofstream::binary); if (!file.good()) { std::cout << "Could not open file: " << localFile << std::endl; logFile << "Could not open file: " << localFile << std::endl; return false; } file.write(buffer, msg.size); file.close(); delete buffer; close(connectionToServer); return true; } } }
bool FileSystem::putToAddress(std::string address, std::string localFile, std::string remoteFile) { int connectionToServer; //TCP connect to introducer/other nodes // printf("Local file: %s\n", localFile.c_str()); // printf("Remote file: %s\n", remoteFile.c_str()); std::ifstream file(localFile, std::ifstream::binary); if (!file.good()) { std::cout << "putToAddress: Could not open file: " << localFile << std::endl; logFile << "putToAddress: Could not open file: " << localFile << std::endl; return false; } logFile << "putToAddress: Connecting to "<< address << "..." << std::endl; int ret = connect_to_server(address.c_str(), port, &connectionToServer); if(ret!=0) { logFile <<"ERROR putToAddress: Cannot connect to "<<address<< std::endl; std::cout <<"ERROR putToAddress: Cannot connect to "<<address<< std::endl; file.close(); return false; } else{ // Read the file file.seekg (0, file.end); int length = file.tellg(); file.seekg (0, file.beg); char * buffer = new char [length]; file.read (buffer,length); Message_fs msg; msg.type = MSG_PUT; memset(msg.filename, '\0', 200); remoteFile.copy(msg.filename, remoteFile.length()); msg.filename[remoteFile.length()+1] = '\0'; msg.size = length; //std::string test = msg.filename; //std::cout << "test: :" << test << std::endl; write(connectionToServer, &msg, sizeof(Message_fs) ); write(connectionToServer, buffer, length); close(connectionToServer); delete buffer; file.close(); return true; } }
void LeafNode::unserialize(std::string &buffer) { BNode::unserialize(buffer); RegisterCounter registerCounter; buffer.copy((char*)®isterCounter,sizeof(RegisterCounter)); buffer.erase(0,sizeof(RegisterCounter)); buffer.copy((char*)&prevNode,sizeof(Offset)); buffer.erase(0,sizeof(Offset)); buffer.copy((char*)&nextNode,sizeof(Offset)); buffer.erase(0,sizeof(Offset)); for(RegisterCounter i = 0 ; i < registerCounter; i++){ Element* el = new Element(); el->unserialize(buffer); elements.push_back(el); } }
/************************ * Purpose / what it does * Converts the given string and sets it to the object's 'line' character array * * Parameters * string a_charArray The line to be set * bool a_getWords Default true. Whether or not to process the words in the line ************************/ void Line::setLine(std::string a_newLine, bool a_getWords) { if(a_newLine[0] == 0 && a_newLine.size() == 1) numChars = 0; else numChars = a_newLine.size(); a_newLine.copy(line, numChars); if(a_getWords) getWords(); }
void setString(MYSQL_BIND& bind, unsigned long& length, const std::string& data) { reserve(bind, data.size()); data.copy(static_cast<char*>(bind.buffer), data.size()); bind.buffer_type = MYSQL_TYPE_VAR_STRING; bind.is_null = 0; length = data.size(); bind.length = &length; }
PatMat::PatElmt_::PatElmt_(const std::string& str) : pCode_(PC_Null), index_(1), pNext_(EOP) { switch (str.length()) { case 0: pCode_ = PC_Null; break; case 1: pCode_ = PC_Char; val.Char = str[0]; break; case 2: pCode_ = PC_String_2; str.copy(val.Str2, 2); break; case 3: pCode_ = PC_String_3; str.copy(val.Str3, 3); break; case 4: pCode_ = PC_String_4; str.copy(val.Str4, 4); break; case 5: pCode_ = PC_String_5; str.copy(val.Str5, 5); break; case 6: pCode_ = PC_String_6; str.copy(val.Str6, 6); break; default: pCode_ = PC_String; val.Str = new std::string(str); break; } }
bool astyle(std::string &content, const char *options) { // allocate internal buffer size_t len = content.size(); char *textIn = new(std::nothrow) char[len + 1]; if (!textIn) return false; // copy to internal buffer #ifdef WIN32 #pragma warning(push) // FIX #pragma warning(disable: 4996) size_t copied = content.copy(textIn, len); #pragma warning(pop) #else size_t copied = content.copy(textIn, len); #endif if (copied != len) { delete[] textIn; return false; } textIn[len] = '\0'; // call astyle error = false; char *textOut = AStyleMain(textIn, options, handleError, allocMemory); delete[] textIn; // check error if (error) { if (textOut) delete[] textOut; return false; } if (!textOut) return false; // return result std::string result(textOut, strlen(textOut)); delete[] textOut; content.swap(result); return true; }
void Magick::Options::fileName(const std::string &fileName_) { ssize_t max_length; max_length=sizeof(_imageInfo->filename)-1; fileName_.copy(_imageInfo->filename,max_length); if ((ssize_t) fileName_.length() > max_length) _imageInfo->filename[max_length]=0; else _imageInfo->filename[fileName_.length()]=0; }
// distribute jobs to each CPU void start(crack_routine_t func) { uint64_t cbn = start_cbn; uint64_t thd_cbn = (total_cbn / thread_cnt); threads.clear(); if (c!=NULL) { #ifdef _MSC_VER _aligned_free(c); #else free(c); #endif c=NULL; } thread_run = 0; found = false; c = (crack_opt_t*)aligned_alloc(32, thread_cnt*sizeof(crack_opt_t)); // for each available cpu for (size_t i=0; i<thread_cnt; i++) { memset(&c[i], 0, sizeof(crack_opt_t)); c[i].id = i; // set cracking routine c[i].crack = func; // set alphabet c[i].alpha_len = alphabet.length(); alphabet.copy(c[i].alphabet, 128, 0); // set hash memcpy(c[i].hash.b, hash.b, HASH_BIN_LEN); // set the first combination c[i].start_cbn = cbn; c[i].pwd_len = cbn2idx(c[i].pwd_idx, cbn); // set the last combination if ((i+1)==thread_cnt) { c[i].end_cbn = cbn + (total_cbn - (thd_cbn*i)); c[i].total_cbn = (total_cbn - (thd_cbn*i)); } else { c[i].end_cbn = (cbn + thd_cbn); c[i].total_cbn = (c[i].end_cbn - c[i].start_cbn); } cbn = c[i].end_cbn; } for(size_t i=0; i<thread_cnt; i++) { threads.push_back(std::move(std::thread(&cracker::worker, this, &c[i]))); thread_run++; } clock_gettime(CLOCK_MONOTONIC, &ts_start); }
std::string StringToLowerCase(const std::string& str_string) { char* buf = new char[str_string.length()]; str_string.copy(buf, str_string.length()); for(unsigned int i = 0; i < str_string.length(); ++i) buf[i] = tolower(buf[i]); std::string r(buf, str_string.length()); delete[] buf; return r; }
void assign(const std::string &str) { const size_t s = str.size(); if (buffer_.size() != s) { delete[] buffer_.begin; buffer_.begin = new T[s]; buffer_.end = buffer_.begin+s; data_.begin = buffer_.begin; data_.end = buffer_.end; } str.copy(data_.begin, s); }
void feistelEncrypt( const std::string &strDesKey, const std::string &strPlain, std::string &strCipher ) { const int LENGTH = 8; char szDesKey[ LENGTH ]; memset( szDesKey, 0, LENGTH ); strDesKey.copy( szDesKey, LENGTH ); char subKey[16][4]; genSubKey( szDesKey, subKey ); char plain[ LENGTH ]; char cipher[ LENGTH ]; strCipher.clear(); int iLength = strPlain.length(); int iLoop = iLength / LENGTH; for( int i = 0; i < iLoop; i++ ) { strPlain.copy( plain, LENGTH, i * LENGTH ); feistelEncryptUnit( 16, subKey, plain, cipher ); strCipher.append( cipher, LENGTH ); } iLength = iLength % LENGTH; if( iLength == 0 ) { memset( plain, LENGTH, LENGTH ); } else { int iPadding = LENGTH - iLength; memset( plain, iPadding, LENGTH ); strPlain.copy( plain, iLength, iLoop * LENGTH ); } feistelEncryptUnit( 16, subKey, plain, cipher ); strCipher.append( cipher, LENGTH ); }