/// Constructs the secret as the PLAIN mechanism wants it. static String plainSecret (const String & username, const String & password) { // construction of the Base64 secret = \0username\0password ByteArray secret; secret.push_back ('\0'); secret.insert (secret.end(), username.begin(), username.end()); secret.push_back ('\0'); secret.insert (secret.end(), password.begin(), password.end()); assert (secret.size() == username.size() + password.size() + 2); return Base64::encodeFromArray (secret); }
ByteArray::size_type Aes256::encrypt_start(const ByteArray::size_type plain_length, ByteArray& encrypted) { m_remainingLength = plain_length; // Generate salt ByteArray::iterator it = m_salt.begin(), itEnd = m_salt.end(); while (it != itEnd) *(it++) = (rand() & 0xFF); // Calculate padding ByteArray::size_type padding = 0; if (m_remainingLength % BLOCK_SIZE != 0) padding = (BLOCK_SIZE - (m_remainingLength % BLOCK_SIZE)); m_remainingLength += padding; // Add salt encrypted.insert(encrypted.end(), m_salt.begin(), m_salt.end()); m_remainingLength += m_salt.size(); // Add 1 bytes for padding size encrypted.push_back(padding & 0xFF); ++m_remainingLength; // Reset buffer m_buffer_pos = 0; return encrypted.size(); }
void IClient::handleBinary(ByteArray &piece, string &str, shared_ptr<Invoke> &invoke, size_t size) { if (piece.getPosition() >= size) return; ByteArray *byteArray = nullptr; size_t position = piece.getPosition(); size_t param_index = 0; //FIND WHICH PARAMETER IS BINARY for (size_t i = 0; i < invoke->size(); i++) if (invoke->at(i)->getType() == "ByteArray") { const ByteArray &ba = invoke->at(i)->referValue<ByteArray>(); if (ba.size() < ba.capacity()) { param_index = i; byteArray = (ByteArray*)&ba; break; } } //IF THERE'S NOT BINARY TYPE if(byteArray == nullptr) return; //CALCULATE SIZE size_t totalSize = byteArray->capacity(); size_t leftSize = totalSize - byteArray->size(); size_t writeSize = std::min(size - position, leftSize); //AND WRITES byteArray->insert ( byteArray->end(), piece.begin() + position, piece.begin() + (position + writeSize) ); piece.setPosition( position + writeSize ); // IF NOT FULFILLED, RETURNS if (byteArray->size() < byteArray->capacity()) return; //IF LAST BINARY for(size_t i = param_index + 1; i < invoke->size(); i++) if(invoke->at(i)->getType() == "ByteArray") return; // SHIFTS _replyData(invoke); //IS LAST BINARY, THEN CLEAR invoke.reset(); //IF BYTES ARE LEFT, CALL HANDLE_STRING handleString(piece, str, invoke, size); }
void IWebClientBase::listenBinary(size_t size, ByteArray &piece, shared_ptr<Invoke> invoke, boost::system::error_code &error) { ByteArray *data = nullptr; size_t paramIndex; for (paramIndex = 0; paramIndex < invoke->size(); paramIndex++) if (invoke->at(paramIndex)->getType() == "ByteArray") { const ByteArray &byteArray = invoke->at(paramIndex)->referValue<ByteArray>(); if (byteArray.size() == 0 && byteArray.capacity() == size) { data = (ByteArray*)&byteArray; break; } } if (data == nullptr) return; data->insert ( data->end(), piece.begin(), piece.begin() + std::min(piece.size(), size) ); while (data->size() < size) { piece.assign(BUFFER_SIZE(), 1000); socket->read_some(boost::asio::buffer(piece), error); data->insert ( data->end(), piece.begin(), piece.begin() + std::min(piece.size(), size - data->size()) ); } for(size_t i = paramIndex + 1; i < invoke->size(); i++) if(invoke->at(i)->getType() == "ByteArray") return; _replyData(invoke); }
void readAll(ByteArray& ba) { BOOST_STATIC_ASSERT(sizeof(ba[0]) == 1); ba.resize(m_len); if (m_len) { // must be a continuous memory block assert(&*ba.begin() + m_len == &*(ba.end()-1) + 1); Stream::ensureRead(&*ba.begin(), ba.size()); m_len = 0; } }
void put(const ByteArray& ary, std::ostream& os) { os << std::hex; for (ByteArray::const_iterator i = ary.begin(); i != ary.end(); ++i) { os << std::setw(2) << std::setfill('0') << static_cast<int>(static_cast<unsigned char>(*i)); } os << std::dec << "\n"; }
void AbstractWriter::write( const ByteArray & data ) { this->sampleQueue_.insert( this->sampleQueue_.end(), data.begin(), data.end() ); while( this->sampleQueue_.size() >= this->sampleBuffer_.size() ) { ByteArray::iterator copyEnd = this->sampleQueue_.begin() + this->sampleBuffer_.size(); std::copy( this->sampleQueue_.begin(), copyEnd, this->sampleBuffer_.begin() ); this->sampleQueue_.erase( this->sampleQueue_.begin(), copyEnd ); this->writeFrame( &this->sampleBuffer_[0], this->sampleBuffer_.size() ); } }
/**@brief Constructs String as string representation of byte sequence in hex form. * e.g 234F11A1...*/ String String::fromByteArray(const ByteArray& bytes) { if(bytes.empty()) return String(); String result; for(ByteArray::const_iterator i = bytes.begin(); i != bytes.end(); ++i) { result += hex2char(((*i)/0x10)%0x10); result += hex2char((*i)%0x10); } return result; }
ByteArray::size_type Aes256::decrypt_continue(const ByteArray& encrypted, ByteArray& plain) { ByteArray::const_iterator it = encrypted.begin(), itEnd = encrypted.end(); while (it != itEnd) { m_buffer[m_buffer_pos++] = *(it++); check_and_decrypt_buffer(plain); } return plain.size(); }
String CDatabaseConnectionMySql::DoWriteBinary( const ByteArray & array ) const { StringStream stream; stream.setf( std::ios::hex, std::ios::basefield ); for ( auto && it = array.begin(); it != array.end(); ++it ) { stream.width( 2 ); stream.fill( STR( '0' ) ); stream << int( *it ); } return STR( "X'" ) + stream.str() + STR( "'" ); }
size_t Buffer::writeByteArray(ByteArray _src, size_t _offset) { if(_offset != npos) _pf_writeOffset = _offset; else _pf_writeOffset = _pf_buffer.size(); if(_pf_writeOffset + _src.size() > _pf_buffer.size()) _pf_buffer.resize(_pf_writeOffset + _src.size()); std::copy(_src.begin(), _src.end(), _pf_buffer.begin() + _pf_writeOffset); return _pf_writeOffset += _src.size(); }
ByteArray SMSGGameList::serialize() const { ByteArray b; std::string msg; b << SMSG_GAME_LIST; if (!data_.SerializeToString(&msg)) { WARN("Cannot serialize: " << data_.DebugString()); return ByteArray(0); } b << msg.length(); b.insert(b.end(), msg.begin(), msg.end()); return b; }
string PfileHeaderReader::ReadExtension(rmscrypto::api::SharedStream stream, uint32_t offset, uint32_t length) { ByteArray ext; if (offset >= stream->Size()) { throw exceptions::RMSPFileException("Bad extension", exceptions::RMSPFileException::BadArguments); } ReadAtOffset(ext, stream, offset, length); string extStr(ext.begin(), ext.end()); Logger::Hidden("PfileHeaderReader: Extension: %s", extStr.c_str()); return extStr; }
string PfileHeaderReader::ReadCleartextRedirectionHeader( rmscrypto::api::SharedStream stream) { ByteArray redirectHeader; uint32_t redirectHeaderLength; stream->Read(reinterpret_cast<uint8_t *>(&redirectHeaderLength), sizeof(uint32_t)); ReadBytes(redirectHeader, stream, redirectHeaderLength); if (redirectHeader.size() != redirectHeaderLength) { throw exceptions::RMSPFileException("Bad redirect header", exceptions::RMSPFileException::BadArguments); } string redirectHeaderStr(redirectHeader.begin(), redirectHeader.end()); Logger::Hidden("PfileHeaderReader: Cleartext redirect header: %s", redirectHeaderStr.c_str()); return redirectHeaderStr; }
ByteArray DefaultReader::readFrame( double & duration, bool & stop ) { stop = false; // static just for speed static uint8_t audio_buf[AVCODEC_MAX_AUDIO_FRAME_SIZE*3/2]; int ret = 0; int dp_len, data_size; ByteArray data; ret = av_read_frame( this->pFormatContext_.get(), this->pPacket_.get() ); if( ret < 0 ) { stop = true; return data; } int64_t curPts = -1; int64_t decoded = 0; if( this->pPacket_->pts != static_cast< int64_t >( AV_NOPTS_VALUE ) ) { curPts = av_rescale( this->pPacket_->pts, AV_TIME_BASE * static_cast< int64_t >( this->pStream_->time_base.num ), this->pStream_->time_base.den ); } #if LIBAVCODEC_VERSION_MAJOR < 53 uint8_t * audio_pkt_data = this->pPacket_->data; int audio_pkt_size = this->pPacket_->size; while( audio_pkt_size > 0 ) { #else uint8_t * pktDataBackup = this->pPacket_->data; while( this->pPacket_->size > 0 ) { #endif if( this->afterEnd( toGeneral( curPts ) ) ) { stop = true; break; } data_size = sizeof( audio_buf ); #if LIBAVCODEC_VERSION_MAJOR < 53 dp_len = avcodec_decode_audio2( this->pCodecContext_.get(), static_cast< int16_t * >( static_cast< void * >( audio_buf ) ), &data_size, audio_pkt_data, audio_pkt_size ); #else dp_len = avcodec_decode_audio3( this->pCodecContext_.get(), static_cast< int16_t * >( static_cast< void * >( audio_buf ) ), &data_size, this->pPacket_.get() ); #endif if( dp_len < 0 ) { #if LIBAVCODEC_VERSION_MAJOR < 53 audio_pkt_size = 0; #endif break; } #if LIBAVCODEC_VERSION_MAJOR < 53 audio_pkt_data += dp_len; audio_pkt_size -= dp_len; #else this->pPacket_->data += dp_len; this->pPacket_->size -= dp_len; #endif if( data_size <= 0 ) { continue; } int64_t ptsDiff = (static_cast< int64_t >( AV_TIME_BASE )/2 * data_size) / (this->pCodecContext_->sample_rate * this->pCodecContext_->channels); if( this->afterBegin( toGeneral( curPts ) ) ) { data.insert( data.end(), audio_buf, audio_buf + data_size ); decoded += ptsDiff; } curPts += ptsDiff; } #if LIBAVCODEC_VERSION_MAJOR < 53 if( this->pPacket_->data ) { #else if( pktDataBackup ) { this->pPacket_->data = pktDataBackup; #endif av_free_packet( this->pPacket_.get() ); } duration = static_cast< double >( decoded ) / AV_TIME_BASE; return data; } bool DefaultReader::seekFrame( double timestamp ) { timestamp = av_rescale( timestamp, pStream_->time_base.den, pStream_->time_base.num ); int succeed = av_seek_frame( this->pFormatContext_.get(), pStream_->index, timestamp, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD ); if( succeed >= 0 ) { avcodec_flush_buffers( this->pCodecContext_.get() ); } return succeed >= 0; } }
SharedBuffer(const ByteArray& data) : data_(new std::vector<char>(data.begin(), data.end())), buffer_(boost::asio::buffer(*data_)) { }
void ByteArrayWriter::writeByteArray(const ByteArray& bytes) { _bytes.insert(_bytes.end(), bytes.begin(), bytes.end()); }
bool Digestor::HashFile(const std::string &path, int required, ByteArray &res) { const int kBufferSize = 1024 * 16; //FileInfo::FileStatus st_before = FileInfo::StatFile(path); unsigned char md5hash[16] = {0}; unsigned char sha1hash[20] = {0}; unsigned char sha512hash[64] = {0}; MD5_CTX md5ctx; SHA_CTX sha1ctx; SHA512_CTX sha512ctx; MD5_Init(&md5ctx); SHA1_Init(&sha1ctx); SHA512_Init(&sha512ctx); File inf; char buffer[kBufferSize]; if (!inf.OpenForRead(path)) { return false; } unsigned long read_bytes; while (1) { if (!inf.Read(buffer, kBufferSize, &read_bytes)) { inf.Close(); return false; } if (read_bytes == 0) break; if (required & HASH_MD5) MD5_Update(&md5ctx, buffer, read_bytes); if (required & HASH_SHA1) SHA1_Update(&sha1ctx, buffer, read_bytes); if (required & HASH_SHA512) SHA512_Update(&sha512ctx, buffer, read_bytes); } inf.Close(); if (required & HASH_MD5) { MD5_Final(md5hash, &md5ctx); res.insert(res.end(), md5hash, md5hash + 16); } if (required & HASH_SHA1) { SHA1_Final(sha1hash, &sha1ctx); res.insert(res.end(), sha1hash, sha1hash + 20); } if (required & HASH_SHA512) { SHA512_Final(sha512hash, &sha512ctx); res.insert(res.end(), sha512hash, sha512hash + 64); } // FileInfo::FileStatus st_after = FileInfo::StatFile(path); // if (st_before.size != st_after.size || st_before.modifytime != st_after.modifytime) { // res.clear(); // return false; // } return true; }
BOSHBodyExtractor::BOSHBodyExtractor(XMLParserFactory* parserFactory, const ByteArray& data) { // Look for the opening body element ByteArray::const_iterator i = data.begin(); while (i < data.end() && isWhitespace(*i)) { ++i; } if (std::distance(i, data.end()) < 6 || *i != '<' || *(i+1) != 'b' || *(i+2) != 'o' || *(i+3) != 'd' || *(i+4) != 'y' || !(isWhitespace(*(i+5)) || *(i+5) == '>' || *(i+5) == '/')) { return; } i += 5; // Parse until end of element bool inSingleQuote = false; bool inDoubleQuote = false; bool endStartTagSeen = false; bool endElementSeen = false; for (; i != data.end(); ++i) { char c = static_cast<char>(*i); if (inSingleQuote) { if (c == '\'') { inSingleQuote = false; } } else if (inDoubleQuote) { if (c == '"') { inDoubleQuote = false; } } else if (c == '\'') { inSingleQuote = true; } else if (c == '"') { inDoubleQuote = true; } else if (c == '/') { if (i + 1 == data.end() || *(i+1) != '>') { return; } else { endElementSeen = true; endStartTagSeen = true; i += 2; break; } } else if (c == '>') { endStartTagSeen = true; i += 1; break; } } if (!endStartTagSeen) { return; } // Look for the end of the element ByteArray::const_reverse_iterator j = data.rbegin(); if (!endElementSeen) { while (isWhitespace(*j) && j < data.rend()) { ++j; } if (j == data.rend() || *j != '>') { return; } ++j; while (j < data.rend() && isWhitespace(*j)) { ++j; } if (std::distance(j, data.rend()) < 6 || *(j+5) != '<' || *(j+4) != '/' || *(j+3) != 'b' || *(j+2) != 'o' || *(j+1) != 'd' || *j != 'y') { return; } j += 6; } body = BOSHBody(); if (!endElementSeen) { body->content = std::string( reinterpret_cast<const char*>(vecptr(data) + std::distance(data.begin(), i)), boost::numeric_cast<size_t>(std::distance(i, j.base()))); } // Parse the body element BOSHBodyParserClient parserClient(this); std::shared_ptr<XMLParser> parser(parserFactory->createXMLParser(&parserClient)); if (!parser->parse(std::string( reinterpret_cast<const char*>(vecptr(data)), boost::numeric_cast<size_t>(std::distance(data.begin(), i))))) { /* TODO: This needs to be only validating the BOSH <body> element, so that XMPP parsing errors are caught at the correct higher layer */ body = boost::optional<BOSHBody>(); return; } }