Пример #1
0
/// 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);
}
Пример #2
0
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();
}
Пример #3
0
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);
}
Пример #4
0
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);
}
Пример #5
0
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;
}
Пример #6
0
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;
}
		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;
		}

	}
Пример #8
0
static ByteArray toByteArray(const OCTETSTR &v)
{
	ByteArray out;
	out.insert(out.begin(), v.begin(), v.end());
	return out;
}