void dc_buffered_stream_send_expqueue2::send_data(procid_t target, 
                unsigned char packet_type_mask,
                std::istream &istrm,
                size_t len) {
  if (len != size_t(-1)) {
    char cbuffer[len];
    while(len > 0 && istrm.good()) {
      size_t l = istrm.readsome(cbuffer, len);
      len -= l;
    }
    send_data(target, packet_type_mask, cbuffer, len);
  }
  else {
    // annoying. we have to compute the length of the stream
    // allocate a 128byte block first.
    // \todo: This can be optimized. Though, I don't think this
    //        code path is even used.
    size_t len = 0;
    size_t cursize = 128;
    char* data = (char*)malloc(128);
    // while the stream is good. read stuff
    // len is the current length of the contents
    // cursize is the max length of the array
    // when we run out of space in the array, we double the size.
    while (istrm.good()) {
      len += istrm.readsome(data+len, cursize-len);
      if (cursize - len == 0) {
        cursize *= 2;
        data = (char*)realloc(data, cursize);
      }
    }
    send_data(target, packet_type_mask, data, len);
    free(data);
  }
}
Exemplo n.º 2
0
bool Configurator::readString (std::istream &stream, std::string &value)
{
	std::uint8_t size;
	char buffer[255];
	if (stream.readsome(reinterpret_cast<char *>(&size), sizeof(size)) != sizeof(size) || !stream.good())
		return false;

	if (stream.readsome(buffer, size) != size || !stream.good())
		return false;

	value.assign(buffer, size);
	return true;
}
Exemplo n.º 3
0
bool Configurator::readBoolean (std::istream &stream, bool &value)
{
	std::uint8_t buffer;
	if (stream.readsome(reinterpret_cast<char *>(&buffer), sizeof(buffer)) != sizeof(buffer) || !stream.good())
		return false;
	value = (buffer != 0);
	return true;
}
Exemplo n.º 4
0
bool Configurator::readInt (std::istream &stream, int &value)
{
	std::uint32_t buffer;
	if (stream.readsome(reinterpret_cast<char *>(&buffer), sizeof(buffer)) != sizeof(buffer) || !stream.good())
		return false;
	value = (int)ntohl(buffer);
	return true;
}
Exemplo n.º 5
0
void StreamBuffer::load(std::istream &is){
	clear();
	for(;;){
		AUTO_REF(back, pushBackPooled(m_chunks));
		back.writePos = is.readsome(back.data, sizeof(back.data));
		if(back.writePos == 0){
			break;
		}
	}
}
	bool
	TraceLog::load(std::istream& i_stream)
	{
		// *** Header ***
		
		int version_high = Serializer::deserialize<int>(i_stream);
		int version_low = Serializer::deserialize<int>(i_stream);
		
		
		
		int tile_number = Serializer::deserialize<int>(i_stream);
		mlog(MLog::debug, "TraceLog::load") << "Reading "
			<< tile_number << " tiles.\n";
			
		for (int i = 0; i<tile_number; ++i)
		{
			Tile* tile = new Tile(Serializer::deserialize<Tile>(i_stream));
			_tile_cache.insert(tile->get_id(), tile);
		}		
		
		// *** Commands	***
		// Read data
		char buffer[4096];
		std::streamsize read_bytes;
		_command_string = "";
		
		while(read_bytes = i_stream.readsome(buffer, 4096))
			_command_string.append(buffer, read_bytes);
			
		_command_stream.str(_command_string);
		_command_stream.seekg(0);
		
		// Parse for command positions
		while(_command_stream.peek() != EOF)
		{
			int command_length = Serializer::deserialize<int>(_command_stream);
			_command_positions.push_back((int)_command_stream.tellg());
			_command_stream.ignore(command_length);
		}
		
		_command_stream.flush();
		
		mlog(MLog::debug, "TraceLog::load") << "Read "
			<< _command_positions.size() << " commands.\n";
		
		if (_command_positions.size())		
			_current_position = 0;
		else
			_current_position = -1;
	}
Exemplo n.º 7
0
	BlobPtr Blob::fromJson(std::istream& is) {
		int next_ch = skipWhitespace(is);
		if (next_ch != '=')
			throw runtime_error(
			"Invalid input: Expected '=', got: '" +
			to_string(static_cast<char>(next_ch)) + "'");
		readChar(is);
		unique_ptr<BlobValue> pv{ new BlobValue() };
		B64::decode([&is](){ char ch; return is.readsome(&ch, 1) ? ch : -1; },
			[&pv](uint8_t x){ pv.get()->push_back(x); });
		// Read away trailing '=':
		while (is.peek() == '=')
			readChar(is);
		return Blob::make(pv);
	}
Exemplo n.º 8
0
bool Configurator::readIp (std::istream &stream, IpAddress &address)
{
	int family;
	if (!readInt(stream, family))
		return false;

	unsigned int size = IpAddress::familySize(family);
	if (size > 0)
	{
		std::uint8_t *buffer = new std::uint8_t[size];
		if (stream.readsome(reinterpret_cast<char *>(buffer), size) != size || !stream.good())
		{
			delete[] buffer;
			return false;
		}

		address = IpAddress(buffer, family);
		delete[] buffer;
	}
	else
		address = IpAddress();

	return true;
}
Exemplo n.º 9
0
bool ReaderWriterCURL::read(std::istream& fin, std::string& destination) const
{
    #define CHUNK 16384

    int ret;
    unsigned have;
    z_stream strm;
    unsigned char in[CHUNK];
    unsigned char out[CHUNK];

    /* allocate inflate state */
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = 0;
    strm.next_in = Z_NULL;
    ret = inflateInit2(&strm,
                       15 + 32 // autodected zlib or gzip header
                       );
    if (ret != Z_OK)
        return false;

    /* decompress until deflate stream ends or end of file */
    do {

        strm.avail_in = fin.readsome((char*)in, CHUNK);

        if (fin.fail())
        {
            (void)inflateEnd(&strm);
            return false;
        }
        if (strm.avail_in == 0)
            break;
        strm.next_in = in;

        /* run inflate() on input until output buffer not full */
        do {
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = inflate(&strm, Z_NO_FLUSH);

            switch (ret) {
            case Z_NEED_DICT:
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                (void)inflateEnd(&strm);
                return false;
            }
            have = CHUNK - strm.avail_out;

            destination.append((char*)out, have);

        } while (strm.avail_out == 0);

        /* done when inflate() says it's done */
    } while (ret != Z_STREAM_END);

    /* clean up and return */
    (void)inflateEnd(&strm);
    return ret == Z_STREAM_END ? true : false;
}
Exemplo n.º 10
0
void decompressZlib(std::istream &is, std::ostream &os)
{
	z_stream z;
	const s32 bufsize = 16384;
	char input_buffer[bufsize];
	char output_buffer[bufsize];
	int status = 0;
	int ret;
	int bytes_read = 0;
	int input_buffer_len = 0;

	z.zalloc = Z_NULL;
	z.zfree = Z_NULL;
	z.opaque = Z_NULL;

	ret = inflateInit(&z);
	if(ret != Z_OK)
		throw SerializationError("dcompressZlib: inflateInit failed");
	
	z.avail_in = 0;
	
	//dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;

	for(;;)
	{
		z.next_out = (Bytef*)output_buffer;
		z.avail_out = bufsize;

		if(z.avail_in == 0)
		{
			z.next_in = (Bytef*)input_buffer;
			input_buffer_len = is.readsome(input_buffer, bufsize);
			z.avail_in = input_buffer_len;
			//dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
		}
		if(z.avail_in == 0)
		{
			//dstream<<"z.avail_in == 0"<<std::endl;
			break;
		}
			
		//dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
		status = inflate(&z, Z_NO_FLUSH);
		//dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
		bytes_read += is.gcount() - z.avail_in;
		//dstream<<"bytes_read="<<bytes_read<<std::endl;

		if(status == Z_NEED_DICT || status == Z_DATA_ERROR
				|| status == Z_MEM_ERROR)
		{
			zerr(status);
			throw SerializationError("decompressZlib: inflate failed");
		}
		int count = bufsize - z.avail_out;
		//dstream<<"count="<<count<<std::endl;
		if(count)
			os.write(output_buffer, count);
		if(status == Z_STREAM_END)
		{
			//dstream<<"Z_STREAM_END"<<std::endl;
			
			//dstream<<"z.avail_in="<<z.avail_in<<std::endl;
			//dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
			// Unget all the data that inflate didn't take
			for(u32 i=0; i < z.avail_in; i++)
			{
				is.unget();
				if(is.fail() || is.bad())
				{
					dstream<<"unget #"<<i<<" failed"<<std::endl;
					dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
					throw SerializationError("decompressZlib: unget failed");
				}
			}
			
			break;
		}
	}

	inflateEnd(&z);
}
Exemplo n.º 11
0
	void addFile(const std::string& name, std::istream& file)
	{
		util::log("TarArchive") << " Adding file '" + name +
			"' to archive '" + _path + "'\n";
		
		auto entry = TarLibrary::archive_entry_new();
		 
		if(entry == nullptr)
		{
			throw std::runtime_error("Failed to create archive entry.");
		}

		util::log("TarArchive") << "  Creating entry in archive...\n";
		
		size_t size = getSize(file);
		
		TarLibrary::archive_entry_set_uid(entry, 0);
		TarLibrary::archive_entry_set_uname(entry, "root");
		TarLibrary::archive_entry_set_gid(entry, 0);
		TarLibrary::archive_entry_set_gname(entry, "wheel");
		TarLibrary::archive_entry_set_filetype(entry, TarLibrary::RegularFile);
		TarLibrary::archive_entry_set_perm(entry, 0644);
		TarLibrary::archive_entry_set_size(entry, size);
		TarLibrary::archive_entry_set_pathname(entry, name.c_str());
		
		if(TarLibrary::archive_write_header(_archive, entry) !=
			TarLibrary::OK)
		{
			TarLibrary::archive_entry_free(entry);
			
			throw std::runtime_error("Failed to write entry to archive. "
				"Message: " + TarLibrary::archive_error_string(_archive));
		}

		util::log("TarArchive") << "  Writing data (" << size
			<< " bytes) to archive...\n";
		
		char buffer[1024];
		
		while(size > 0)
		{
			int count = file.readsome(buffer, 1024);
		
			size -= count;
		
			if(TarLibrary::archive_write_data(_archive, buffer, count) != count)
			{
				throw std::runtime_error("Failed to write data to archive."
					"Message: " + TarLibrary::archive_error_string(_archive));
			}
		}

		if(TarLibrary::archive_write_finish_entry(_archive) != TarLibrary::OK)
		{
			throw std::runtime_error("Failed to finish "
				"writing data to archive.");
		}

		TarLibrary::archive_entry_free(entry);

		util::log("TarArchive") << "  File added successfully...\n";
	}
Exemplo n.º 12
0
void
read_istream(
    std::istream& is,
    basic_flat_buffer<Allocator>& buffer,
    message<isRequest, Body, fields>& msg,
    error_code& ec)
{
    // Create the message parser
    //
    // Arguments passed to the parser's constructor are
    // forwarded to the message constructor. Here, we use
    // a move construction in case the caller has constructed
    // their message in a non-default way.
    //
    parser<isRequest, Body> p{std::move(msg)};

    do
    {
        // Extract whatever characters are presently available in the istream
        if(is.rdbuf()->in_avail() > 0)
        {
            // Get a mutable buffer sequence for writing
            auto const mb = buffer.prepare(
                static_cast<std::size_t>(is.rdbuf()->in_avail()));

            // Now get everything we can from the istream
            buffer.commit(static_cast<std::size_t>(is.readsome(
                boost::asio::buffer_cast<char*>(mb),
                boost::asio::buffer_size(mb))));
        }
        else if(buffer.size() == 0)
        {
            // Our buffer is empty and we need more characters, 
            // see if we've reached the end of file on the istream
            if(! is.eof())
            {
                // Get a mutable buffer sequence for writing
                auto const mb = buffer.prepare(1024);

                // Try to get more from the istream. This might block.
                is.read(
                    boost::asio::buffer_cast<char*>(mb),
                    boost::asio::buffer_size(mb));

                // If an error occurs on the istream then return it to the caller.
                if(is.fail() && ! is.eof())
                {
                    // We'll just re-use io_error since std::istream has no error_code interface.
                    ec = make_error_code(errc::io_error);
                    return;
                }

                // Commit the characters we got to the buffer.
                buffer.commit(static_cast<std::size_t>(is.gcount()));
            }
            else
            {
                // Inform the parser that we've reached the end of the istream.
                p.put_eof(ec);
                if(ec)
                    return;
                break;
            }
        }

        // Write the data to the parser
        auto const bytes_used = p.put(buffer.data(), ec);

        // This error means that the parser needs additional octets.
        if(ec == error::need_more)
            ec = {};
        if(ec)
            return;

        // Consume the buffer octets that were actually parsed.
        buffer.consume(bytes_used);
    }
    while(! p.is_done());

    // Transfer ownership of the message container in the parser to the caller.
    msg = p.release();
}
	int VirtualMachine::readProgram(std::istream & input)
	{
		qvm_header_t qvminfo;
		int i, n;
		uint1_t x[4];
		word w;

		DEBUGTRACE("Loading file...\n");
		qvminfo.magic = readInt(input); /* magic. */
		if (qvminfo.magic != QVM_MAGIC)
		{
			DEBUGTRACE("Invalid magic");
			throw InvalidProgramException();
			//q3vm_error("Does not appear to be a QVM file.");
			/* XXX: option to force continue. */
			return 0;
		}
		DEBUGTRACE("Magic OK\n");
		/* variable-length instructions mean instruction count != code length */
		qvminfo.inscount = readInt(input);
		qvminfo.codeoff = readInt(input);
		qvminfo.codelen = readInt(input);
		qvminfo.dataoff = readInt(input);
		qvminfo.datalen = readInt(input);
		qvminfo.litlen = readInt(input);
		qvminfo.bsslen = readInt(input);

		/* Code segment should follow... */
		/* XXX: use fseek with SEEK_CUR? */
		DEBUGTRACE("Searching for .code @ %d from %d\n", qvminfo.codeoff, input.tellg());

		//  rom = (q3vm_rom_t*)(hunk);  /* ROM-in-hunk */
		rom = (Instruction*)calloc(qvminfo.inscount, sizeof(rom[0]));
		while (input.tellg() < qvminfo.codeoff)
			readByte(input);
		while (romSize < qvminfo.inscount)
		{
			n = readByte(input);
			w.int4 = 0;
			if ((i = opcodeParameterSize(n)))
			{
				x[0] = x[1] = x[2] = x[3] = 0;
				input.readsome((char*)x, i);
				w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24);
			}
			rom[romSize].Operation = n;
			rom[romSize].Parameter = w;
			romSize++;
		}
		DEBUGTRACE("After loading code: at %d, should be %d\n", input.tellg(), qvminfo.codeoff + qvminfo.codelen);

		/* Then data segment. */
		//  ram = hunk + ((romlen + 3) & ~3);  /* RAM-in-hunk */
		ram = hunk;
		DEBUGTRACE("Searching for .data @ %d from %d\n", qvminfo.dataoff, input.tellg());
		while (input.tellg() < qvminfo.dataoff)
			readByte(input);
		for (n = 0; n < (qvminfo.datalen / sizeof(uint1_t)); n++)
		{
			i = input.readsome((char*)x, sizeof(x));
			w.uint4 = (x[0]) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24);
			*((word*)(ram + ramSize)) = w;
			ramSize += sizeof(word);
		}
	
		/* lit segment follows data segment. */
		/* Assembler should have already padded properly. */
		DEBUGTRACE("Loading .lit\n");
		for (n = 0; n < (qvminfo.litlen / sizeof(uint1_t)); n++)
		{
			i = input.readsome((char*)x, sizeof(x));
			memcpy(&(w.uint1), &x, sizeof(x));  /* no byte-swapping. */
			*((word*)(ram + ramSize)) = w;
			ramSize += sizeof(word);
		}
		/* bss segment. */
		DEBUGTRACE("Allocating .bss %d (%X) bytes\n", qvminfo.bsslen, qvminfo.bsslen);
		/* huge empty chunk. */
		ramSize += qvminfo.bsslen;

		hunkFree = hunkSize - ((ramSize * sizeof(uint1_t)) + 4);

		DEBUGTRACE("VM hunk has %d of %d bytes free (RAM = %d B).\n", hunkFree, hunkSize, ramSize);
		if (ramSize > hunkSize)
		{
			throw OutOfMemoryException();
			return 0;
		}

		/* set up stack. */
		{
			int stacksize = 0x10000;
			dataStack = ramSize - (stacksize / 2);
			returnStack = ramSize;
			//returnStack = dataStack+4;
			RP = returnStack;
			DP = dataStack;
		}

		/* set up PC for return-to-termination. */
		PC = romSize + 1;

		ramMask = ramSize;

		return 1;
	}