Пример #1
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);
}
Пример #2
0
void ConnectionHandlerBase::SetBuffer(std::size_t size, ByteArray& buffer)
{
    if (RequestMaxSize < (size + buffer_.size()))
    {
        throw std::exception("too long request");
    }

    Append(buffer.begin(), buffer.begin() + size, buffer_);
}
Пример #3
0
		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;
			}
		}
Пример #4
0
	Endpoint::Endpoint(ByteArray const &addr, uint16_t port) :
		m_port(port)
	{
		if(addr.size() == 4) {
			boost::asio::ip::address_v4::bytes_type b;
			copy(addr.begin(), addr.begin() + 4, b.begin());
			m_addr = boost::asio::ip::address_v4(b);
		}

		if(addr.size() == 16) {
			boost::asio::ip::address_v6::bytes_type b;
			copy(addr.begin(), addr.begin() + 16, b.begin());
			m_addr = boost::asio::ip::address_v6(b);
		}
	}
Пример #5
0
bool BTProtocal::DecodePiecePacket(CommBuffer* pBuffer,uint_32& dataLength,uint_32& index,uint_32& begin,ByteArray& block)
{
	if(NULL != pBuffer)
	{
		int_32 len = 0;
		int_8	id = 0;
		pBuffer->ReadInt32(len);
		len = ntohl(len);
		if(len > 9)
		{
			dataLength = len - 9;
			pBuffer->ReadInt8(id);
			if(id == 7)
			{
				pBuffer->ReadUInt32(index);
				index = ntohl(index);
                pBuffer->ReadUInt32(begin);
                begin = ntohl(begin);
				block.resize(dataLength);
                TRACE("[BTProtocal::DecodePiecePacket] Len:%d Index:%d blockLen:%d\n",len,index,dataLength);
				pBuffer->Read(&(*block.begin()),block.size(),block.size());
				return true;
			}

		}
	}
	return false;
}
Пример #6
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);
}
Пример #7
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() );
			}
		}
Пример #9
0
bool Packet::deserialize(ByteArray& buffer)
{
    _header = 0;
    _id = 0;
    _lengthType = 0;
    _length = 0;
    _data.clear();

    BinaryReader reader(buffer);
    uint countReadedBytes = 0;

    if (reader.bytesAvailable() < sizeof(_header))
    {
        return false;
    }

    _header = reader.readUShort();
    _id = getMessageId(_header);
    _lengthType = getMessageLengthType(_header);
    countReadedBytes += sizeof(_header);

    if (reader.bytesAvailable() < _lengthType)
    {
        return false;
    }

    _length = getMessageLength(_lengthType, reader);
    countReadedBytes += _lengthType;

    if (reader.bytesAvailable() < _length)
    {
        return false;
    }

    _data = reader.readBytes(_length);
    countReadedBytes += _length;

    buffer.erase(buffer.begin(), buffer.begin() + countReadedBytes);

    return true;
}
Пример #10
0
/**@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;
}
Пример #11
0
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();
}
Пример #12
0
Error Datagram::decodeFrom (const ByteArray & source, long * bytes) {
	if (source.size() < 8) return sf::error::NotEnough;
	uint32_t headerLength = ntohl (*((const uint32_t*) (source.const_c_array())));
	uint32_t contentLength = ntohl (*((const uint32_t*) (source.const_c_array()+4)));
	if (headerLength > 2147483647 || contentLength > 2147483647){
		Log (LogError) << LOGID << "Header/Content length to big" << std::endl;
		return error::InvalidArgument;
	}

	if (source.size () >= headerLength + contentLength + 8){
		// we can decode it all
		mHeader = sf::createByteArrayPtr();
		mHeader->assign (source.begin() + 8, source.begin() + 8 + headerLength);
		mContent = sf::createByteArrayPtr();
		mContent->assign (source.begin() + 8 + headerLength, source.begin() + 8 + headerLength + contentLength);
	} else {
		return error::NotEnough;
	}
	if (bytes) *bytes = headerLength + contentLength + 8;
	return NoError;
}
	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( "'" );
	}
Пример #14
0
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();
}
Пример #15
0
size_t Buffer::readByteArray(ByteArray& _dst, size_t _size, size_t _offset)
{
	if(_offset != npos)
		_pf_readOffset = _offset;

	_pm_checkIfEnoughBytesToRead(_size, _pf_readOffset);

	_dst.clear();
	_dst.reserve(_size);
	std::copy(_pf_buffer.begin() + _offset, _pf_buffer.begin() + _offset + _size, _dst.begin());

	_pf_readOffset += _size;

	return _pf_readOffset;
}
Пример #16
0
ByteArray jDataSegment::getData() {
	ByteArray data;
	jbyteArray dataArr = getByteArray(this, "data");
	jsize length = env->GetArrayLength(dataArr);
	jbyte *data_ptr = env->GetByteArrayElements(dataArr, NULL);

	if (data_ptr != NULL) {
		data.resize(length);
		copy(data_ptr, data_ptr + length, data.begin());
		env->ReleaseByteArrayElements(dataArr, data_ptr, JNI_ABORT);
	} else {
		data.clear();
	}

	env->DeleteLocalRef(dataArr);

	return data;
}
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;
}
shared_ptr<PfileHeader>PfileHeaderReader::ReadHeader(
  rmscrypto::api::SharedStream stream,
  uint32_t                     majorVersion,
  uint32_t                     minorVersion,
  const string               & cleartextRedirectionHeader)
{
  uint32_t headerSize;
  uint32_t extensionOffset = 0;
  uint32_t extensionLength = 0;
  uint32_t plOffset        = 0;
  uint32_t plLength        = 0;
  uint32_t contentOffset   = 0;

  stream->Read(reinterpret_cast<uint8_t *>(&headerSize),      sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&extensionOffset), sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&extensionLength), sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&plOffset),        sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&plLength),        sizeof(uint32_t));
  stream->Read(reinterpret_cast<uint8_t *>(&contentOffset),   sizeof(uint32_t));

  uint32_t endOfHeader      = plOffset + plLength;
  uint64_t originalFileSize = 0;
  uint32_t metadataOffset   = 0;
  uint32_t metadataLength   = 0;

  ByteArray metadata;
  ByteArray publishingLicense;

  if ((majorVersion >= 2) && (minorVersion >= 1))
  {
    stream->Read(reinterpret_cast<uint8_t *>(&originalFileSize),
                 sizeof(uint64_t));
    stream->Read(reinterpret_cast<uint8_t *>(&metadataOffset),
                 sizeof(uint32_t));
    stream->Read(reinterpret_cast<uint8_t *>(&metadataLength),
                 sizeof(uint32_t));
    endOfHeader = metadataOffset + metadataLength;
  }

  if (contentOffset < endOfHeader) {
    throw exceptions::RMSPFileException("Bad content offset",
                                        exceptions::RMSPFileException::BadArguments);
  }

  string extension = ReadExtension(stream, extensionOffset, extensionLength);

  ReadAtOffset(publishingLicense, stream, plOffset, plLength);

  // remove UTF-8 BOM
  if ((publishingLicense.size() > 3) &&
      (memcmp(publishingLicense.data(), "\xEF\xBB\xBF", 3) == 0)) {
    publishingLicense.erase(publishingLicense.begin(),
                            publishingLicense.begin() + 3);
  }

  if ((majorVersion == 2) && (minorVersion == 1))
  {
    ReadAtOffset(metadata, stream, metadataOffset, metadataLength);
  }
  return make_shared<PfileHeader>(move(publishingLicense), extension,
                                  contentOffset, originalFileSize,
                                  move(metadata), majorVersion, minorVersion,
                                  cleartextRedirectionHeader);
}
Пример #20
0
static ByteArray toByteArray(const OCTETSTR &v)
{
	ByteArray out;
	out.insert(out.begin(), v.begin(), v.end());
	return out;
}
Пример #21
0
		ByteArray Recv()
		{ return ByteArray(Buffer.begin(), Buffer.begin() + KernelUrb.actual_length); }
Пример #22
0
const std::string getStringFromByteArray(const ByteArray& byteArray,
		size_t startIndex, size_t size)
{
	return std::string(byteArray.begin() + startIndex, byteArray.begin()
			+ startIndex + size);
}
Пример #23
0
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;
    }
}
Пример #24
0
void ByteArrayWriter::writeByteArray(const ByteArray& bytes)
{
	_bytes.insert(_bytes.end(), bytes.begin(), bytes.end());
}
Пример #25
0
bool tryLock(const Zstring& lockfilepath) //throw FileError
{
#ifdef ZEN_WIN
#ifdef TODO_MinFFS_ActivatePriviledge
    try { activatePrivilege(SE_BACKUP_NAME); }
    catch (const FileError&) {}
    try { activatePrivilege(SE_RESTORE_NAME); }
    catch (const FileError&) {}
#endif//TODO_MinFFS_ActivatePriviledge

    const HANDLE fileHandle = ::CreateFile(applyLongPathPrefix(lockfilepath).c_str(),              //_In_      LPCTSTR lpFileName,
                                           //use both when writing over network, see comment in file_io.cpp
                                           GENERIC_READ | GENERIC_WRITE,                           //_In_      DWORD dwDesiredAccess,
                                           FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, //_In_      DWORD dwShareMode,
                                           nullptr,               //_In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                           CREATE_NEW,            //_In_      DWORD dwCreationDisposition,
                                           FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, //_In_      DWORD dwFlagsAndAttributes,
                                           nullptr);              //_In_opt_  HANDLE hTemplateFile
    if (fileHandle == INVALID_HANDLE_VALUE)
    {
        const DWORD ec = ::GetLastError(); //copy before directly/indirectly making other system calls!
        if (ec == ERROR_FILE_EXISTS || //confirmed to be used
            ec == ERROR_ALREADY_EXISTS) //comment on msdn claims, this one is used on Windows Mobile 6
            return false;

        throw FileError(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(lockfilepath)), formatSystemError(L"CreateFile", ec));
    }
    ScopeGuard guardLockFile = zen::makeGuard([&] { removeFile(lockfilepath); });
    FileOutput fileOut(fileHandle, lockfilepath); //pass handle ownership

    //be careful to avoid CreateFile() + CREATE_ALWAYS on a hidden file -> see file_io.cpp
    //=> we don't need it that badly //::SetFileAttributes(applyLongPathPrefix(lockfilepath).c_str(), FILE_ATTRIBUTE_HIDDEN); //(try to) hide it

#elif defined ZEN_LINUX || defined ZEN_MAC
    const mode_t oldMask = ::umask(0); //important: we want the lock file to have exactly the permissions specified
    ZEN_ON_SCOPE_EXIT(::umask(oldMask));

    //O_EXCL contains a race condition on NFS file systems: http://linux.die.net/man/2/open
    const int fileHandle = ::open(lockfilepath.c_str(), O_CREAT | O_EXCL | O_WRONLY,
                                  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    if (fileHandle == -1)
    {
        if (errno == EEXIST)
            return false;
        else
            THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(lockfilepath)), L"open");
    }
    ScopeGuard guardLockFile = zen::makeGuard([&] { removeFile(lockfilepath); });
    FileOutput fileOut(fileHandle, lockfilepath); //pass handle ownership
#endif

    //write housekeeping info: user, process info, lock GUID
    ByteArray binStream = [&]
    {
        MemStreamOut streamOut;
        LockInformation(FromCurrentProcess()).toStream(streamOut);
        return streamOut.ref();
    }();

    if (!binStream.empty())
        fileOut.write(&*binStream.begin(), binStream.size()); //throw FileError

    guardLockFile.dismiss(); //lockfile created successfully
    return true;
}
Пример #26
0
		SharedBuffer(const ByteArray& data) : 
				data_(new std::vector<char>(data.begin(), data.end())),
				buffer_(boost::asio::buffer(*data_)) {
		}