Beispiel #1
0
bool putback_test_two(std::istream& is)
{
    try {
        do {
            char buf[chunk_size];
            is.read(buf, chunk_size);
            if (is.gcount() < static_cast<std::streamsize>(chunk_size))
                break;
            is.putback('a');
            is.putback('b');
            is.putback('c');
            is.putback('d');
            if ( is.get() != 'd' || is.get() != 'c' ||
                 is.get() != 'b' || is.get() != 'a' )
            {
                return false;
            }
        } while (!is.eof());
        return true;
    } catch (std::exception&) { return false; }
}
Beispiel #2
0
int deserialize_string_raw(
	std::istream& istr,
	std::string& value,
	S32 max_bytes)
{
	int count = 0;
	const S32 BUF_LEN = 20;
	char buf[BUF_LEN];		/* Flawfinder: ignore */
	istr.get(buf, BUF_LEN - 1, ')');
	count += istr.gcount();
	char c = istr.get();
	c = istr.get();
	count += 2;
	if(((c == '"') || (c == '\'')) && (buf[0] == '('))
	{
		// We probably have a valid raw string. determine
		// the size, and read it.
		// *FIX: This is memory inefficient.
		S32 len = strtol(buf + 1, NULL, 0);
		if((max_bytes>0)&&(len>max_bytes)) return LLSDParser::PARSE_FAILURE;
		std::vector<char> buf;
		if(len)
		{
			buf.resize(len);
			count += fullread(istr, (char *)&buf[0], len);
			value.assign(buf.begin(), buf.end());
		}
		c = istr.get();
		++count;
		if(!((c == '"') || (c == '\'')))
		{
			return LLSDParser::PARSE_FAILURE;
		}
	}
	else
	{
		return LLSDParser::PARSE_FAILURE;
	}
	return count;
}
Beispiel #3
0
/*! Read the program string from the given stream
*/
bool ShaderChunk::readFragmentProgram(std::istream &stream)
{
#define BUFSIZE 200
    
    editFragmentProgram().erase();    
    char buf[BUFSIZE];

    if(!stream.good())
    {
        FWARNING(("SHLChunk::readFragmentProgram: stream is not good!\n"));
        return false;
    }
    
    do
    {
        stream.read(buf, BUFSIZE);
        editFragmentProgram().append(buf, stream.gcount());
    }
    while(!stream.eof());
    
    return true;
}
Beispiel #4
0
////////////////////////////////////////////////////////////////////////////
// MemParser constructor
//////////////////////////////////////////////////////////////////////////////
MemParser::MemParser(std::istream &in, UInt32 bytes) {
  if (bytes == 0) {
    // -----------------------------------------------------------------------------
    // Read all available bytes from the stream
    // -----------------------------------------------------------------------------
    std::string data;
    const int chunkSize = 0x10000;
    auto chunkP = new char[chunkSize];
    while (!in.eof()) {
      in.read(chunkP, chunkSize);
      NTA_CHECK(in.good() || in.eof())
          << "MemParser::MemParser() - error reading data from stream";
      data.append(chunkP, in.gcount());
    }

    bytes_ = (UInt32)data.size();
    bufP_ = new char[bytes_ + 1];
    NTA_CHECK(bufP_ != nullptr) << "MemParser::MemParser() - out of memory";
    ::memmove((void *)bufP_, data.data(), bytes_);
    ((char *)bufP_)[bytes_] = 0;

    delete[] chunkP;
  } else {
    // -----------------------------------------------------------------------------
    // Read given # of bytes from the stream
    // -----------------------------------------------------------------------------
    bytes_ = bytes;
    bufP_ = new char[bytes_ + 1];
    NTA_CHECK(bufP_ != nullptr) << "MemParser::MemParser() - out of memory";

    in.read((char *)bufP_, bytes);
    ((char *)bufP_)[bytes] = 0;
    NTA_CHECK(in.good())
        << "MemParser::MemParser() - error reading data from stream";
  }
  // Setup start and end pointers
  startP_ = bufP_;
  endP_ = startP_ + bytes_;
}
Beispiel #5
0
//------------------------------------------------------------------------------
    bool load( std::istream &stream )
    {
        clean();

        unsigned char e_ident[EI_NIDENT];

        // Read ELF file signature
        stream.seekg( 0 );
        stream.read( reinterpret_cast<char*>( &e_ident ), sizeof( e_ident ) );

        // Is it ELF file?
        if ( stream.gcount() != sizeof( e_ident ) ||
             e_ident[EI_MAG0] != ELFMAG0    ||
             e_ident[EI_MAG1] != ELFMAG1    ||
             e_ident[EI_MAG2] != ELFMAG2    ||
             e_ident[EI_MAG3] != ELFMAG3 ) {
            return false;
        }

        if ( ( e_ident[EI_CLASS] != ELFCLASS64 ) &&
             ( e_ident[EI_CLASS] != ELFCLASS32 )) {
            return false;
        }

        convertor.setup( e_ident[EI_DATA] );

        header = create_header( e_ident[EI_CLASS], e_ident[EI_DATA] );
        if ( 0 == header ) {
            return false;
        }
        if ( !header->load( stream ) ) {
            return false;
        }

        load_sections( stream );
        load_segments( stream );

        return true;
    }
Beispiel #6
0
ArchiveKeys::ArchiveKeys(std::istream &stream, RsaKeyPair &keypair){
	this->init(CryptoPP::Twofish::MAX_KEYLENGTH, CryptoPP::Twofish::BLOCKSIZE, false);

	auto &private_key = keypair.get_private_key();

	CryptoPP::RSA::PrivateKey priv;
	priv.Load(CryptoPP::ArraySource((const byte *)&private_key[0], private_key.size(), true));

	CryptoPP::SecByteBlock buffer(this->size);
	{
		auto n = 4096 / 8;

		typedef CryptoPP::RSAES<CryptoPP::OAEP<CryptoPP::SHA>>::Decryptor decryptor_t;
		typedef CryptoPP::PK_DecryptorFilter filter_t;
		decryptor_t dec(priv);
		CryptoPP::SecByteBlock temp(n);
		stream.read((char *)temp.data(), temp.size());
		auto read = stream.gcount();
		if (read != temp.size())
			throw RsaBlockDecryptionException("Not enough bytes read from RSA block.");
		try{
			auto sink = new CryptoPP::ArraySink(buffer.data(), buffer.size());
			auto filter = new filter_t(*random_number_generator, dec, sink);
			CryptoPP::ArraySource(temp.data(), temp.size(), true, filter);
		}catch (...){
			throw RsaBlockDecryptionException("Invalid data in RSA block.");
		}
	}

	size_t offset = 0;
	for (size_t i = 0; i < this->key_count; i++){
		auto &key = this->keys[i];
		auto &iv = this->ivs[i];
		memcpy(key.data(), buffer.data() + offset, key.size());
		offset += key.size();
		memcpy(iv.data(), buffer.data() + offset, iv.size());
		offset += iv.size();
	}
}
std::string cgicc::readString(std::istream& in)
{
  std::string::size_type dataSize = 0;
  
  in >> dataSize;
  in.get(); // skip ' '
  
  // Avoid allocation of a zero-length vector
  if(0 == dataSize) {
    return std::string();
  }

  // Don't use auto_ptr, but vector instead
  // Bug reported by [email protected] / fix by [email protected]
  std::vector<char> temp(dataSize);

  in.read(&temp[0], dataSize);
  if(static_cast<std::string::size_type>(in.gcount()) != dataSize) {
    throw std::runtime_error("I/O error");
  }
  return std::string(&temp[0], dataSize);
}
Beispiel #8
0
void	ModPhpHandler::inParent(int *p, std::ostream& os, std::istream& is)
{
  std::cout << "ModPhpHandler::inParent" << std::endl;
  if (dup2(p[0], 0) == -1)
    std::cerr << "dup2 failed in parent" << std::endl;

  //   close(p[1]);
  // TODO: envoyer le $_POST dans p[1]
  char buf2[4096];
  int read_size = 0;
    while (!is.eof())
      {
	is.read(buf2, 4096);
	if (is.eof())
	  read_size = is.gcount();
	else
	  read_size = 4096;
	if (read_size == 0)
	  continue ;
	if (write(p[1], buf2, read_size) == -1)
	  {
	    std::cerr << "Write fail in ModPhpHandler" << std::endl;
	    break ;
	  }
	// la t'ecris buf dans le pipe
      }



  
  char	buf[512];
  memset(buf, 0, 512);
  while (read(0, buf, 512) > 0)
    {
      os << buf;
      std::cout << buf << std::endl;
      memset(buf, 0, 512);
    }
}
Beispiel #9
0
bool Soy::ReadStreamChunk(ArrayBridge<char>& Data,std::istream& Stream)
{
	//	dunno how much to read
	if ( !Soy::Assert( !Data.IsEmpty(), "Soy::ReadStreamChunk no data length specified, resorting to 1byte" ) )
		Data.SetSize(1);
	if ( Data.IsEmpty() )
		return false;
	
	auto Peek = Stream.peek();
	if ( Peek == std::char_traits<char>::eof() )
		return false;
	
	Stream.read( Data.GetArray(), Data.GetDataSize() );
	
	if ( Stream.fail() && !Stream.eof() )
		return false;
	
	auto BytesRead = Stream.gcount();
	Data.SetSize( BytesRead );
	
	return true;
}
Beispiel #10
0
void compress(std::istream& in, std::ostream& out)
{
    const size_t BUFFER_SIZE = 512;

    std::vector<byte_t> r_buffer(BUFFER_SIZE);
    std::vector<bit_t>  w_buffer;

    ahc::Tree tree(ahc::Algorithm::FGK);
    w_buffer.reserve(8 * BUFFER_SIZE + BUFFER_SIZE);

    while (in)
    {
        in.read(reinterpret_cast<char*>(r_buffer.data()), BUFFER_SIZE);
        size_t read_bytes = in.gcount();

        // encode bytes read 
        for (size_t i = 0; i < read_bytes; ++i)
        {
            std::vector<bit_t> code = tree.encode(r_buffer[i]);
            std::copy(std::begin(code), std::end(code), std::back_inserter(w_buffer));
        }

        // flush buffer if it is almost filled
        if (w_buffer.size() >= 8 * BUFFER_SIZE)
            flush_buffer(w_buffer, out);
    }

    // fill buffer with zeroes if byte is not complete
    byte_t extra_bits = 0;
    if (w_buffer.size() % 8 != 0)
    {
        extra_bits = 8 - w_buffer.size() % 8;
        std::fill_n(std::back_inserter(w_buffer), extra_bits, 0);
    }

    flush_buffer(w_buffer, out);
    out.write(reinterpret_cast<char*>(&extra_bits), 1);
}
Beispiel #11
0
			static void copyIterator(
				std::istream & in, 
				iterator & out,
				uint64_t n,
				uint64_t const multiplier = 1)
			{
				n *= multiplier;
				::libmaus2::autoarray::AutoArray < char > buf(16*1024,false);
				
				while ( n )
				{
					uint64_t const tocopy = std::min(n,buf.getN());
					
					in.read(buf.get(), tocopy);
					assert ( in.gcount() == static_cast<int64_t>(tocopy) );
					
					std::copy(buf.get(),buf.get()+tocopy,out);
					//out.write ( buf.get(), tocopy );
					// assert ( out );
					
					n -= tocopy;
				}
			}
Beispiel #12
0
static void read_scanline_old(std::istream& in,
                              std::vector<packed_colour>& scanline,
                              size_t i = 0) {
  int rshift = 0;
  while (i < scanline.size()) {
    in.read((char*)&scanline[i].val, 4);
    if (in.gcount() != 4)
      throw std::runtime_error("premature EOF");
    if (scanline[i].r == 1 && scanline[i].g == 1 && scanline[i].b == 1) {
      size_t j = i + (scanline[i].e << rshift);
      if (j > scanline.size())
        throw std::runtime_error("RLE overflow");
      uint32_t copy = scanline[i - 1].val;
      for (; i < j; i++) {
        scanline[i].val = copy;
      }
      rshift += 8;
    } else {
      i++;
      rshift = 0;
    }
  }
}
OSG_BEGIN_NAMESPACE


/*! \class TGAImageFileType

  Image File Type to read/write and store/restore Image objects as
  TGA data.
  
  All the type specific code is included in the class. Does
  not depend on external libs.
  
 */

bool TGAImageFileType::readHeader(std::istream &is, TGAHeader &header)
{
    UInt8 dum[18];

    is.read(reinterpret_cast<char *>(dum), 18);

    if(is.gcount() != 18)
        return false;

    header.idLength      = dum[ 0];
    header.colorMapType  = dum[ 1];
    header.imageType     = dum[ 2];
    header.cmapFirst     = dum[ 3] | (dum[ 4] << 8);
    header.cmapLength    = dum[ 5] | (dum[ 6] << 8);
    header.cmapEntrySize = dum[ 7];
    header.xOrigin       = dum[ 8] | (dum[ 9] << 8);
    header.yOrigin       = dum[10] | (dum[11] << 8);
    header.width         = dum[12] | (dum[13] << 8);
    header.height        = dum[14] | (dum[15] << 8);
    header.depth         = dum[16];
    header.descriptor    = dum[17];

    return true;
}
void
Producer::populateStore(std::istream& is)
{
  BOOST_ASSERT(m_store.size() == 0);

  if (m_isVerbose)
    std::cerr << "Loading input ..." << std::endl;

  std::vector<uint8_t> buffer(m_maxSegmentSize);
  while (is.good()) {
    is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
    const auto nCharsRead = is.gcount();
    if (nCharsRead > 0) {
      auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
      data->setFreshnessPeriod(m_freshnessPeriod);
      data->setContent(&buffer[0], nCharsRead);

      m_store.push_back(data);
    }
  }

  if (m_store.empty()) {
    auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
    data->setFreshnessPeriod(m_freshnessPeriod);
    m_store.push_back(data);
  }

  auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
  for (const auto& data : m_store) {
    data->setFinalBlockId(finalBlockId);
    m_keyChain.sign(*data, m_signingInfo);
  }

  if (m_isVerbose)
    std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl;
}
Beispiel #15
0
static bool read_buffer(std::istream& is, z_stream& z, char* in_buf, void* p, size_t size) {
    z.next_out=(Bytef*)p;
    z.avail_out=(uInt)size;

    while (z.avail_out) {
        if ( z.avail_in == 0 ) {
            if (!is.eof()) {
                z.next_in = (Bytef*)in_buf;
                is.read((char*)z.next_in, OUT_BUFSIZE);
                if (is.bad()) {
                    std::cerr<<"read error "<<std::endl;;
                    return false;
                }
                z.avail_in = (uInt)is.gcount();
            }
        }
        int ret = inflate( &z, Z_BLOCK  );
        if ( ret != Z_OK && ret != Z_STREAM_END ) {
            std::cerr<<"Zlib error "<<z.msg<<std::endl;;
            return false;
        }
    }
    return true;
}
Beispiel #16
0
		void decode(std::istream& istream_in, std::ostream& ostream_in)
		{
			base64_init_decodestate(&_state);
			//
			const int N = _buffersize;
			char* code = new char[N];
			char* plaintext = new char[N];
			int codelength;
			int plainlength;

			do
			{
				istream_in.read((char*)code, N);
				codelength = istream_in.gcount();
				plainlength = decode(code, codelength, plaintext);
				ostream_in.write((const char*)plaintext, plainlength);
			}
			while (istream_in.good() && codelength > 0);
			//
			base64_init_decodestate(&_state);

			delete [] code;
			delete [] plaintext;
		}
Beispiel #17
0
bool symkey::encrypt (std::istream&in, std::ostream&out, prng&rng)
{
	if (!is_valid()) return false;

	/*
	 * structure of symmetrically encrypted file:
	 *
	 * - one-time key part, key.size() bytes
	 *  (repeat:
	 * - blocksize encrypted bytes
	 * - sum(hashes's size) blocksize marker+bytes of block hashes
	 *  )
	 * - possibly incomplete last block (may be empty)
	 * - hashes of last blocksize+block
	 * - eof
	 */

	std::vector<byte> otkey;
	otkey.resize (key.size());
	for (uint i = 0; i < otkey.size(); ++i) otkey[i] = rng.random (256);

	/*
	 * initialize the ciphers
	 */

	scs_t scs;
	for (std::set<std::string>::iterator
	     i = ciphers.begin(), e = ciphers.end();
	     i != e; ++i) {
		if (!streamcipher::suite().count (*i)) {
			err ("symkey: unsupported cipher: " << escape_output (*i));
			return false;
		}
		scs.push_back (streamcipher::suite() [*i]->get());
		scs.back().collect();
		scs.back()->init();
		scs.back()->load_key_vector (key);
		scs.back()->load_key_vector (otkey);
	}

	/*
	 * initialize the hashes
	 */

	uint hashes_size = 0;

	hashes_t hs;
	for (std::set<std::string>::iterator
	     i = hashes.begin(), e = hashes.end();
	     i != e; ++i) {
		if (!hash_proc::suite().count (*i)) {
			err ("symkey: unsupported hash function: " << escape_output (*i));
			return false;
		}
		hs.push_back (hash_proc::suite() [*i]->get());
		hs.back().collect();

		hashes_size += hs.back()->size();
	}

	/*
	 * output the onetime key
	 */

	out.write ( (char*) & (otkey[0]), otkey.size());

	/*
	 * process the blocks
	 */

	std::vector<byte>buf, cipbuf;
	buf.resize (blocksize + hashes_size);
	cipbuf.resize (buf.size());

	for (;;) {
		in.read ( (char*) & (buf[0]), blocksize);
		uint bytes_read = in.gcount();

		if (!in && !in.eof()) {
			err ("symkey: failed reading input");
			return false;
		}

		//hashup!
		uint hashpos = bytes_read;
		for (hashes_t::iterator i = hs.begin(), e = hs.end();
		     i != e; ++i) {
			hash_proc&hp = **i;
			hp.init();
			hp.eat (& (buf[0]), & (buf[bytes_read]));
			hp.eat (key);
			hp.eat (otkey);
			std::vector<byte> res = hp.finish();
			for (uint j = 0; j < res.size(); ++j, ++hashpos)
				buf[hashpos] = res[j];
			//hashpos gets to the end of block with hashes
		}

		//encrypt!
		for (scs_t::iterator i = scs.begin(), e = scs.end();
		     i != e; ++i) {
			streamcipher&sc = **i;
			sc.gen (hashpos, & (cipbuf[0]));
			for (uint j = 0; j < hashpos; ++j)
				buf[j] = buf[j] ^ cipbuf[j];
		}

		//output!
		out.write ( (char*) & (buf[0]), hashpos);
		if (!out) {
			err ("symkey: failed to write output");
			return false;
		}

		//this was the last one
		if (bytes_read < blocksize) break;
	}

	return true;
}
Beispiel #18
0
bool BabelFileFormat::read(std::istream &input, chemkit::MoleculeFile *file)
{
    // get input format to use
    std::string format = option("format").toString();
    if(format.empty()){
        setErrorString("No format set for Babel conversion.");
        return false;
    }

    // setup babel arguments
    QStringList arguments;
    arguments.append(QString("-i") + format.c_str());
    arguments.append("-");
    arguments.append("-ocml");
    arguments.append("-");

    // create and start the babel process
    QProcess babel;
    babel.start("babel", arguments);
    if(!babel.waitForStarted()){
        setErrorString("Failed to start Babel process.");
        return false;
    }

    // write input data to babel via stdin
    while(!input.eof()){
        char buffer[1024];
        input.read(buffer, sizeof(buffer));
        babel.write(buffer, input.gcount());
    }

    babel.closeWriteChannel();

    // wait until the babel process is finished
    if(!babel.waitForFinished()){
        setErrorString("Babel process never finished.");
        return false;
    }

    // check babel's exit status
    if(babel.exitCode() != QProcess::NormalExit){
        setErrorString("Babel process crashed.");
        return false;
    }

    // read output data to string buffer
    std::stringstream buffer;
    buffer << babel.readAll().constData();

    // parse cml output file
    chemkit::MoleculeFile outputFile;
    outputFile.setFormat("cml");
    bool ok = outputFile.read(buffer);
    if(!ok){
        setErrorString("Failed to parse Babel's CML output: " + outputFile.errorString());
        return false;
    }

    // add molecules to file
    foreach(const boost::shared_ptr<chemkit::Molecule> &molecule, outputFile.molecules()){
        file->addMolecule(molecule);
    }

    return true;
}
Beispiel #19
0
// static
bool LLSDSerialize::deserialize(LLSD& sd, std::istream& str, S32 max_bytes)
{
	LLPointer<LLSDParser> p = NULL;
	char hdr_buf[MAX_HDR_LEN + 1] = ""; /* Flawfinder: ignore */
	int i;
	int inbuf = 0;
	bool legacy_no_header = false;
	bool fail_if_not_legacy = false;
	std::string header;

	/*
	 * Get the first line before anything.
	 */
	str.get(hdr_buf, MAX_HDR_LEN, '\n');
	if (str.fail())
	{
		str.clear();
		fail_if_not_legacy = true;
	}

	if (!strncasecmp(LEGACY_NON_HEADER, hdr_buf, strlen(LEGACY_NON_HEADER))) /* Flawfinder: ignore */
	{
		legacy_no_header = true;
		inbuf = str.gcount();
	}
	else
	{
		if (fail_if_not_legacy)
			goto fail;
		/*
		* Remove the newline chars
		*/
		for (i = 0; i < MAX_HDR_LEN; i++)
		{
			if (hdr_buf[i] == 0 || hdr_buf[i] == '\r' ||
				hdr_buf[i] == '\n')
			{
				hdr_buf[i] = 0;
				break;
			}
		}
		header = hdr_buf;

		std::string::size_type start = std::string::npos;
		std::string::size_type end = std::string::npos;
		start = header.find_first_not_of("<? ");
		if (start != std::string::npos)
		{
			end = header.find_first_of(" ?", start);
		}
		if ((start == std::string::npos) || (end == std::string::npos))
			goto fail;

		header = header.substr(start, end - start);
		ws(str);
	}
	/*
	 * Create the parser as appropriate
	 */
	if (legacy_no_header)
	{	// Create a LLSD XML parser, and parse the first chunk read above
		LLSDXMLParser* x = new LLSDXMLParser();
		x->parsePart(hdr_buf, inbuf);	// Parse the first part that was already read
		x->parseLines(str, sd);			// Parse the rest of it
		delete x;
		return true;
	}

	if (header == LLSD_BINARY_HEADER)
	{
		p = new LLSDBinaryParser;
	}
	else if (header == LLSD_XML_HEADER)
	{
		p = new LLSDXMLParser;
	}
	else
	{
		llwarns << "deserialize request for unknown ELLSD_Serialize" << llendl;
	}

	if (p.notNull())
	{
		p->parse(str, sd, max_bytes);
		return true;
	}

fail:
	llwarns << "deserialize LLSD parse failure" << llendl;
	return false;
}
bool ReaderWriterGZ::read(std::istream& fin, std::string& destination) const
{
    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 // autodetected zlib or gzip header
                       );
    if (ret != Z_OK)
        return false;

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

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

        if (fin.bad())
        {
            (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;
}
Beispiel #21
0
MERLMeasuredData::MERLMeasuredData(std::istream &i_stream)
  {
  int dims[3];
  i_stream.read(reinterpret_cast<char*>(&dims[0]), sizeof(int));
  i_stream.read(reinterpret_cast<char*>(&dims[1]), sizeof(int));
  i_stream.read(reinterpret_cast<char*>(&dims[2]), sizeof(int));
  if (i_stream.eof() || i_stream.fail())
    {
    ASSERT(0 && "Error during reading input stream in MERLMeasuredData::MERLMeasuredData().");
    return;
    }

  size_t n = dims[0] * dims[1] * dims[2];
  if (n != BRDF_SAMPLING_RES_THETA_H*BRDF_SAMPLING_RES_THETA_D*BRDF_SAMPLING_RES_PHI_D) 
    {
    ASSERT(0 && "Dimensions don't match in MERLMeasuredData::MERLMeasuredData().");
    return;
    }

  double *p_regular_halfangle_data = new double[3*n];

  const size_t chunk_size = 2*BRDF_SAMPLING_RES_PHI_D;
  char *p_tmp = new char[chunk_size*sizeof(double)];
  size_t num_chunks = n / chunk_size;
  ASSERT((n % chunk_size) == 0);
  double scales[3] = {1.0/1500, 1.15/1500, 1.66/1500};

  for (unsigned char c=0;c<3;++c)
    {
    int offset = 0;
    for (size_t i=0;i<num_chunks;++i)
      {
      i_stream.read(p_tmp, chunk_size*sizeof(double));
      if (i_stream.eof() || i_stream.fail() || i_stream.gcount() != chunk_size*sizeof(double))
        {
        ASSERT(0 && "Premature end-of-file in MERLMeasuredData::MERLMeasuredData().");
        delete[] p_tmp;
        delete[] p_regular_halfangle_data;
        return;
        }

      for (size_t j=0;j<chunk_size;++j)
        {
        double val = *reinterpret_cast<double*>(p_tmp+j*sizeof(double));
        p_regular_halfangle_data[3*(offset++) + c] = std::max(0.0, val*scales[c]);
        }
      }
    }

  m_brdf_data.resize(n);
  for (size_t i=0;i<n;++i)
    {
    RGBColor_d rgb(p_regular_halfangle_data[3*i+0], p_regular_halfangle_data[3*i+1], p_regular_halfangle_data[3*i+2]);
    XYZColor_d xyz = global_sRGB_E_ColorSystem.RGB_To_XYZ(rgb);
    m_brdf_data[i] =  Convert<HalfFloat>( Convert<float>( SpectrumRoutines::XYZToSpectrumCoef(xyz) ) );
    }

  delete[] p_tmp;
  delete[] p_regular_halfangle_data;

  _InitializeSegmentations();
  }
S32 LLSDXMLParser::Impl::parseLines(std::istream& input, LLSD& data)
{
	XML_Status status = XML_STATUS_OK;

	data = LLSD();

	static const int BUFFER_SIZE = 1024;

	//static char last_buffer[ BUFFER_SIZE ];
	//std::streamsize last_num_read;

	// Must get rid of any leading \n, otherwise the stream gets into an error/eof state
	clear_eol(input);

	while( !mGracefullStop
		&& input.good() 
		&& !input.eof())
	{
		void* buffer = XML_GetBuffer(mParser, BUFFER_SIZE);
		/*
		 * If we happened to end our last buffer right at the end of the llsd, but the
		 * stream is still going we will get a null buffer here.  Check for mGracefullStop.
		 * -- I don't think this is actually true - zero 2008-05-09
		 */
		if (!buffer)
		{
			break;
		}
		
		// Get one line
		input.getline((char*)buffer, BUFFER_SIZE);
		std::streamsize num_read = input.gcount();

		//memcpy( last_buffer, buffer, num_read );
		//last_num_read = num_read;

		if ( num_read > 0 )
		{
			if (!input.good() )
			{	// Clear state that's set when we run out of buffer
				input.clear();
			}
		
			// Re-insert with the \n that was absorbed by getline()
			char * text = (char *) buffer;
			if ( text[num_read - 1] == 0)
			{
				text[num_read - 1] = '\n';
			}
		}

		status = XML_ParseBuffer(mParser, (int)num_read, false);
		if (status == XML_STATUS_ERROR)
		{
			break;
		}
	}

	if (status != XML_STATUS_ERROR
		&& !mGracefullStop)
	{	// Parse last bit
		status = XML_ParseBuffer(mParser, 0, true);
	}
	
	if (status == XML_STATUS_ERROR  
		&& !mGracefullStop)
	{
		if (mEmitErrors)
		{
			LL_INFOS() << "LLSDXMLParser::Impl::parseLines: XML_STATUS_ERROR" << LL_ENDL;
		}
		return LLSDParser::PARSE_FAILURE;
	}

	clear_eol(input);
	data = mResult;
	return mParseCount;
}
Beispiel #23
0
parsenode_t XQueryCompiler::parse(std::istream& aXQuery, const zstring& aFileName)
{
  // TODO: move these out
  if (Properties::instance().getPrintAST())
  {
    theCompilerCB->theConfig.parse_cb = print_ast_tree;
  }

  std::stringstream xquery_stream;

#ifdef ZORBA_XQUERYX
  char* converted_xquery_str = NULL;
  std::string   xquery_str;
  bool is_xqueryx = false;

  {
    char strtemp[1000];
    do
    {
      strtemp[0] = 0;
      aXQuery.read(strtemp, sizeof(strtemp)-1);
      strtemp[aXQuery.gcount()] = 0;
      xquery_str += strtemp;
    }
    while(aXQuery.gcount() == (sizeof(strtemp)-1));
  }

  XQueryXConvertor* xqxconvertor = GENV.getXQueryXConvertor();

  if(xqxconvertor->isXQueryX((char*)xquery_str.c_str()))
  {
    // identify XQueryX by content:
    // root tag =
    // "<prefix:module ... xmlns:prefix="http://www.w3.org/2005/XQueryX" ... > "

    is_xqueryx = true;
    //translate from xqueryx to xquery using XSLT
    //read all input stream into std::string

    converted_xquery_str = xqxconvertor->XQueryX2XQuery(xquery_str.c_str());

#ifndef NDEBUG
    printf ("\n\n%s", converted_xquery_str);  // debug
#endif
    xquery_stream << converted_xquery_str;
  }
  else
  {
    xquery_stream << xquery_str;
  }
#else // ZORBA_XQUERYX

  xquery_stream << aXQuery.rdbuf();
#endif

  theCompilerCB->setPhase(CompilerCB::PARSING);

  parsenode_t node;
  theCompilerCB->setPhase(CompilerCB::NONE);
  
  bool lXQueryMode = getLanguageMode(xquery_stream);

  if (lXQueryMode)
  {
    xquery_driver lDriver(&*theCompilerCB, xquery_driver::XQUERY_GRAMMAR);
    lDriver.parse_stream(xquery_stream, aFileName);
    node =  lDriver.get_expr();
  }
  else
  {
    xquery_driver lDriver(&*theCompilerCB, xquery_driver::JSONIQ_GRAMMAR);
    lDriver.parse_stream(xquery_stream, aFileName);
    node =  lDriver.get_expr();
  }

#ifdef ZORBA_XQUERYX
  if (is_xqueryx)
  {
    xqxconvertor->freeResult(converted_xquery_str);
  }
#endif



  if (typeid (*node) == typeid (ParseErrorNode))
  {
    ParseErrorNode* pen = static_cast<ParseErrorNode *>(&*node);
    throw XQUERY_EXCEPTION_VAR(pen->err, 
    ERROR_PARAMS(pen->msg), ERROR_LOC(pen->get_location()));
  }

  return node;
}
Beispiel #24
0
int symkey::decrypt (std::istream&in, std::ostream&out)
{
	if (!is_valid()) return 1;

	std::vector<byte> otkey;
	otkey.resize (key.size());

	/*
	 * read otkey
	 */

	in.read ( (char*) & (otkey[0]), otkey.size());
	if (in.gcount() != (std::streamsize) otkey.size() || !in) {
		err ("symkey: failed reading input");
		return 1;
	}

	/*
	 * initialize the ciphers
	 */

	scs_t scs;
	for (std::set<std::string>::iterator
	     i = ciphers.begin(), e = ciphers.end();
	     i != e; ++i) {
		if (!streamcipher::suite().count (*i)) {
			err ("symkey: unsupported cipher: " << escape_output (*i));
			return 1;
		}
		scs.push_back (streamcipher::suite() [*i]->get());
		scs.back().collect();
		scs.back()->init();
		scs.back()->load_key_vector (key);
		scs.back()->load_key_vector (otkey);
	}

	/*
	 * initialize the hashes
	 */

	uint hashes_size = 0;

	hashes_t hs;
	for (std::set<std::string>::iterator
	     i = hashes.begin(), e = hashes.end();
	     i != e; ++i) {
		if (!hash_proc::suite().count (*i)) {
			err ("symkey: unsupported hash function: " << escape_output (*i));
			return 1;
		}
		hs.push_back (hash_proc::suite() [*i]->get());
		hs.back().collect();

		hashes_size += hs.back()->size();
	}

	/*
	 * process the blocks
	 */

	std::vector<byte> buf, cipbuf;
	buf.resize (blocksize + hashes_size);
	cipbuf.resize (buf.size());

	for (;;) {
		in.read ( (char*) & (buf[0]), buf.size());
		uint bytes_read = in.gcount();

		if ( (!in && !in.eof()) || bytes_read < hashes_size) {
			err ("symkey: failed reading input");
			return 1;
		}

		//decrypt!
		for (scs_t::iterator i = scs.begin(), e = scs.end();
		     i != e; ++i) {
			streamcipher&sc = **i;
			sc.gen (bytes_read, & (cipbuf[0]));
			for (uint j = 0; j < bytes_read; ++j)
				buf[j] = buf[j] ^ cipbuf[j];
		}

		bytes_read -= hashes_size;

		//verify the hashes
		uint hashpos = bytes_read;
		for (hashes_t::iterator i = hs.begin(), e = hs.end();
		     i != e; ++i) {
			hash_proc&hp = **i;
			hp.init();
			hp.eat (& (buf[0]), & (buf[bytes_read]));
			hp.eat (key);
			hp.eat (otkey);
			std::vector<byte> res = hp.finish();
			for (uint j = 0; j < res.size(); ++j, ++hashpos)
				if (buf[hashpos] != res[j]) {
					err ("symkey: mangled input");
					return 3;
				}
		}

		//now that all is OK, output!
		out.write ( (char*) & (buf[0]), bytes_read);

		//last one
		if (bytes_read < blocksize) break;
	}

	//did we read whole input?
	if (!in.eof()) {
		err ("symkey: failed reading input");
		return 1;
	}
	return 0;
}
Beispiel #25
0
        ReadResult readPNGStream(std::istream& fin) const
        {
            int trans = PNG_ALPHA;
            pngInfo pInfo;
            pngInfo *pinfo = &pInfo;

            unsigned char header[8];
            png_structp png;
            png_infop   info;
            png_infop   endinfo;
            png_bytep   data;    //, data2;
            png_bytep  *row_p;
            double  fileGamma;

            png_uint_32 width, height;
            int depth, color;

            png_uint_32 i;
            png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

            // Set custom error handlers
            png_set_error_fn(png, png_get_error_ptr(png), user_error_fn, user_warning_fn);

            #ifdef OSG_CPP_EXCEPTIONS_AVAILABLE
            try
            #endif
            {
                info = png_create_info_struct(png);
                endinfo = png_create_info_struct(png);

                fin.read((char*)header,8);
                if (fin.gcount() == 8 && png_sig_cmp(header, 0, 8) == 0)
                    png_set_read_fn(png,&fin,png_read_istream); //Use custom read function that will get data from istream
                else
                {
                    png_destroy_read_struct(&png, &info, &endinfo);
                    return ReadResult::FILE_NOT_HANDLED;
                }
                png_set_sig_bytes(png, 8);

                png_read_info(png, info);
                png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);

                if (pinfo != NULL)
                {
                    pinfo->Width  = width;
                    pinfo->Height = height;
                    pinfo->Depth  = depth;
                }

                OSG_INFO<<"width="<<width<<" height="<<height<<" depth="<<depth<<std::endl;
                if ( color == PNG_COLOR_TYPE_RGB) { OSG_INFO << "color == PNG_COLOR_TYPE_RGB "<<std::endl; }
                if ( color == PNG_COLOR_TYPE_GRAY) { OSG_INFO << "color == PNG_COLOR_TYPE_GRAY "<<std::endl; }
                if ( color == PNG_COLOR_TYPE_GRAY_ALPHA) { OSG_INFO << "color ==  PNG_COLOR_TYPE_GRAY_ALPHA"<<std::endl; }

                // png default to big endian, so we'll need to swap bytes if on a little endian machine.
                if (depth>8 && getCpuByteOrder()==osg::LittleEndian)
                    png_set_swap(png);


                if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
                {
                    //png_set_gray_to_rgb(png);
                }

                if (color&PNG_COLOR_MASK_ALPHA && trans != PNG_ALPHA)
                {
                    png_set_strip_alpha(png);
                    color &= ~PNG_COLOR_MASK_ALPHA;
                }



                //    if (!(PalettedTextures && mipmap >= 0 && trans == PNG_SOLID))
                //if (color == PNG_COLOR_TYPE_PALETTE)
                //    png_set_expand(png);

                // In addition to expanding the palette, we also need to check
                // to expand greyscale and alpha images.  See libpng man page.
                if (color == PNG_COLOR_TYPE_PALETTE)
                    png_set_palette_to_rgb(png);
                if (color == PNG_COLOR_TYPE_GRAY && depth < 8)
                {
                #if PNG_LIBPNG_VER >= 10209
                    png_set_expand_gray_1_2_4_to_8(png);
                #else
                    // use older now deprecated but identical call
                    png_set_gray_1_2_4_to_8(png);
                #endif
                }
                if (png_get_valid(png, info, PNG_INFO_tRNS))
                    png_set_tRNS_to_alpha(png);

                // Make sure that files of small depth are packed properly.
                if (depth < 8)
                    png_set_packing(png);


                /*--GAMMA--*/
                //    checkForGammaEnv();
                double screenGamma = 2.2 / 1.0;
                if (png_get_gAMA(png, info, &fileGamma))
                    png_set_gamma(png, screenGamma, fileGamma);
                else
                    png_set_gamma(png, screenGamma, 1.0/2.2);

                png_read_update_info(png, info);

                data = (png_bytep) new unsigned char [png_get_rowbytes(png, info)*height];
                row_p = new png_bytep [height];

                bool StandardOrientation = true;
                for (i = 0; i < height; i++)
                {
                    if (StandardOrientation)
                        row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
                    else
                        row_p[i] = &data[png_get_rowbytes(png, info)*i];
                }

                png_read_image(png, row_p);
                delete [] row_p;
                png_read_end(png, endinfo);

                GLenum pixelFormat = 0;
                GLenum dataType = depth<=8?GL_UNSIGNED_BYTE:GL_UNSIGNED_SHORT;
                switch(color)
                {
                  case(PNG_SOLID): pixelFormat = GL_LUMINANCE; break;
                  case(PNG_ALPHA): pixelFormat = GL_ALPHA; break;
                  case(PNG_COLOR_TYPE_GRAY): pixelFormat =GL_LUMINANCE ; break;
                  case(PNG_COLOR_TYPE_GRAY_ALPHA): pixelFormat = GL_LUMINANCE_ALPHA; break;
                  case(PNG_COLOR_TYPE_RGB): pixelFormat = GL_RGB; break;
                  case(PNG_COLOR_TYPE_PALETTE): pixelFormat = GL_RGB; break;
                  case(PNG_COLOR_TYPE_RGB_ALPHA): pixelFormat = GL_RGBA; break;
                  default: break;
                }

                // Some paletted images contain alpha information.  To be
                // able to give that back to the calling program, we need to
                // check the number of channels in the image.  However, the
                // call might not return correct information unless
                // png_read_end is called first.  See libpng man page.
                if (pixelFormat == GL_RGB && png_get_channels(png, info) == 4)
                    pixelFormat = GL_RGBA;

                int internalFormat = pixelFormat;

                png_destroy_read_struct(&png, &info, &endinfo);

                //    delete [] data;

                if (pixelFormat==0)
                    return ReadResult::FILE_NOT_HANDLED;

                osg::Image* pOsgImage = new osg::Image();

                pOsgImage->setImage(width, height, 1,
                    internalFormat,
                    pixelFormat,
                    dataType,
                    data,
                    osg::Image::USE_NEW_DELETE);

                return pOsgImage;

            }
            #ifdef OSG_CPP_EXCEPTIONS_AVAILABLE
            catch (PNGError& err)
            {
                OSG_WARN << err << std::endl;
                png_destroy_read_struct(&png, &info, &endinfo);
                return ReadResult::ERROR_IN_READING_FILE;
            }
            #endif
        }
// Method invariant: One of the following must always be true even in the case 
// of exceptions.
// - m_bytes_needed > 0
// - m-state = STATE_READY
void frame::consume(std::istream &s) {
	try {
		switch (m_state) {
			case STATE_BASIC_HEADER:
				s.read(&m_header[BASIC_HEADER_LENGTH-m_bytes_needed],m_bytes_needed);
		
				m_bytes_needed -= s.gcount();
				
				if (m_bytes_needed == 0) {
					process_basic_header();
					
					validate_basic_header();
					
					if (m_bytes_needed > 0) {
						m_state = STATE_EXTENDED_HEADER;
					} else {
						process_extended_header();
						
						if (m_bytes_needed == 0) {
							m_state = STATE_READY;
							process_payload();
							
						} else {
							m_state = STATE_PAYLOAD;
						}
					}
				}
				break;
			case STATE_EXTENDED_HEADER:
				s.read(&m_header[get_header_len()-m_bytes_needed],m_bytes_needed);
				
				m_bytes_needed -= s.gcount();
				
				if (m_bytes_needed == 0) {
					process_extended_header();
					if (m_bytes_needed == 0) {
						m_state = STATE_READY;
						process_payload();
					} else {
						m_state = STATE_PAYLOAD;
					}
				}
				break;
			case STATE_PAYLOAD:
				s.read(reinterpret_cast<char *>(&m_payload[m_payload.size()-m_bytes_needed]),
					   m_bytes_needed);
				
				m_bytes_needed -= s.gcount();
				
				if (m_bytes_needed == 0) {
					m_state = STATE_READY;
					process_payload();
				}
				break;
			case STATE_RECOVERY:
				// Recovery state discards all bytes that are not the first byte
				// of a close frame.
				do {
					s.read(reinterpret_cast<char *>(&m_header[0]),1);
					
					//std::cout << std::hex << int(static_cast<unsigned char>(m_header[0])) << " ";
					
					if (int(static_cast<unsigned char>(m_header[0])) == 0x88) {
						//(BPB0_FIN && CONNECTION_CLOSE)
						m_bytes_needed--;
						m_state = STATE_BASIC_HEADER;
						break;
					}
				} while (s.gcount() > 0);
				
				//std::cout << std::endl;
				
				break;
			default:
				break;
		}
		
		/*if (s.gcount() == 0) {
			throw frame_error("consume read zero bytes",FERR_FATAL_SESSION_ERROR);
		}*/
	} catch (const frame_error& e) {
		// After this point all non-close frames must be considered garbage, 
		// including the current one. Reset it and put the reading frame into
		// a recovery state.
		if (m_degraded == true) {
			throw frame_error("An error occurred while trying to gracefully recover from a less serious frame error.",FERR_FATAL_SESSION_ERROR);
		} else {
			reset();
			m_state = STATE_RECOVERY;
			m_degraded = true;
			
			throw e;
		}
	}
}
Beispiel #27
0
void Process::runParentProcess(std::istream &input, bool reportErrors)
{
	const int &childReadIn = m_inPipe[0];
	const int &parentWriteIn = m_inPipe[1];
	const int &parentReadOut = m_outPipe[0];
	const int &childWriteOut = m_outPipe[1];
	const int &parentReadErr = m_errPipe[0];
	const int &childWriteErr = m_errPipe[1];

	close(childReadIn);
	close(childWriteOut);
	close(childWriteErr);

	std::stringstream::char_type buffer[1024];

	const auto maxFD = std::max(std::max(parentReadOut, parentReadErr), parentWriteIn);

	auto handleWrite = [&]()
	{
		if (!input.good())
		{
			close(parentWriteIn);
			return false;
		}

		input.read(buffer, sizeof(buffer));
		const auto count = input.gcount();

		const auto written = write(parentWriteIn, buffer, count);

		if (written != count)
			std::cerr << "[Error] Couldn't write entire buffer" << std::endl;

		return true;
	};

	auto handleRead = [&](int fileDescriptor, std::ostream &outputStream, std::mutex &outputStreamMutex)
	{
		const auto count = read(fileDescriptor, buffer, sizeof(buffer));

		if (count == 0)
		{
			close(fileDescriptor);
			return false;
		}

		std::lock_guard<std::mutex> lock(outputStreamMutex);
		outputStream.write(buffer, count).flush();
		return true;
	};

	bool parentReadOutActive = true;
	bool parentReadErrActive = true;
	bool parentWriteInActive = true;

	auto waitForInput = [&]()
	{
		fd_set readFDSet;
		fd_set writeFDSet;

		int numberOfEvents = -1;

		while (true)
		{
			FD_ZERO(&readFDSet);
			FD_ZERO(&writeFDSet);

			if (parentReadOutActive)
				FD_SET(parentReadOut, &readFDSet);

			if (parentReadErrActive)
				FD_SET(parentReadErr, &readFDSet);

			if (parentWriteInActive)
				FD_SET(parentWriteIn, &writeFDSet);

			numberOfEvents = select(maxFD + 1, &readFDSet, &writeFDSet, nullptr, nullptr);

			if (numberOfEvents != -1 || errno != EINTR)
				break;
		}

		if (numberOfEvents < 0)
		{
			std::cerr << "[Error] select failed" << std::endl;
			exit(EXIT_FAILURE);
		}

		if (FD_ISSET(parentReadOut, &readFDSet))
			parentReadOutActive &= handleRead(parentReadOut, m_stdout, m_stdoutMutex);

		if (FD_ISSET(parentReadErr, &readFDSet))
			parentReadErrActive &= handleRead(parentReadErr, m_stderr, m_stderrMutex);

		if (FD_ISSET(parentWriteIn, &writeFDSet))
			parentWriteInActive &= handleWrite();

		return parentReadOutActive || parentReadErrActive || parentWriteInActive;
	};

	while (waitForInput());

	auto finishReading = [&](int fileDescriptor, std::ostream &outputStream, std::mutex &outputStreamMutex)
	{
		if (fcntl(fileDescriptor, F_GETFD) == -1 && errno == EBADF)
			return;

		while (handleRead(fileDescriptor, outputStream, outputStreamMutex));
	};

	finishReading(parentReadOut, m_stdout, m_stdoutMutex);
	finishReading(parentReadErr, m_stderr, m_stderrMutex);

	close(parentReadOut);
	close(parentReadErr);

	int status = 0;
	waitpid(m_childPID, &status, 0);

	m_exitCode = WEXITSTATUS(status);

	if (reportErrors && !isStderrEmpty())
	{
		std::cerr << "\033[1;31m[Error] Error occurred while executing " << m_binary << ":" << std::endl;
		std::cerr << m_stderr.rdbuf() << "\033[0m" << std::endl;
	}
}
Beispiel #28
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);
}
Beispiel #29
0
void MapBlock::deSerialize(std::istream &is, u8 version)
{
	if(!ser_ver_supported(version))
		throw VersionMismatchException("ERROR: MapBlock format not supported");

	// These have no lighting info
	if(version <= 1)
	{
		setLightingExpired(true);
	}

	// These have no "generated" field
	if(version < 18)
	{
		m_generated = true;
	}

	// These have no compression
	if(version <= 3 || version == 5 || version == 6)
	{
		u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
		char tmp;
		is.read(&tmp, 1);
		if(is.gcount() != 1)
			throw SerializationError
					("MapBlock::deSerialize: no enough input data");
		is_underground = tmp;
		for(u32 i=0; i<nodecount; i++)
		{
			s32 len = MapNode::serializedLength(version);
			SharedBuffer<u8> d(len);
			is.read((char*)*d, len);
			if(is.gcount() != len)
				throw SerializationError
						("MapBlock::deSerialize: no enough input data");
			data[i].deSerialize(*d, version);
		}
	}
	else if(version <= 10)
	{
		u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;

		u8 t8;
		is.read((char*)&t8, 1);
		is_underground = t8;

		{
			// Uncompress and set material data
			std::ostringstream os(std::ios_base::binary);
			decompress(is, os, version);
			std::string s = os.str();
			if(s.size() != nodecount)
				throw SerializationError
						("MapBlock::deSerialize: invalid format");
			for(u32 i=0; i<s.size(); i++)
			{
				data[i].param0 = s[i];
			}
		}
		{
			// Uncompress and set param data
			std::ostringstream os(std::ios_base::binary);
			decompress(is, os, version);
			std::string s = os.str();
			if(s.size() != nodecount)
				throw SerializationError
						("MapBlock::deSerialize: invalid format");
			for(u32 i=0; i<s.size(); i++)
			{
				data[i].param1 = s[i];
			}
		}
	
		if(version >= 10)
		{
			// Uncompress and set param2 data
			std::ostringstream os(std::ios_base::binary);
			decompress(is, os, version);
			std::string s = os.str();
			if(s.size() != nodecount)
				throw SerializationError
						("MapBlock::deSerialize: invalid format");
			for(u32 i=0; i<s.size(); i++)
			{
				data[i].param2 = s[i];
			}
		}
	}
	// All other versions (newest)
	else
	{
		u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;

		u8 flags;
		is.read((char*)&flags, 1);
		is_underground = (flags & 0x01) ? true : false;
		m_day_night_differs = (flags & 0x02) ? true : false;
		m_lighting_expired = (flags & 0x04) ? true : false;
		if(version >= 18)
			m_generated = (flags & 0x08) ? false : true;

		// Uncompress data
		std::ostringstream os(std::ios_base::binary);
		decompress(is, os, version);
		std::string s = os.str();
		if(s.size() != nodecount*3)
			throw SerializationError
					("MapBlock::deSerialize: decompress resulted in size"
					" other than nodecount*3");

		// deserialize nodes from buffer
		for(u32 i=0; i<nodecount; i++)
		{
			u8 buf[3];
			buf[0] = s[i];
			buf[1] = s[i+nodecount];
			buf[2] = s[i+nodecount*2];
			data[i].deSerialize(buf, version);
		}
		
		/*
			NodeMetadata
		*/
		if(version >= 14)
		{
			// Ignore errors
			try{
				if(version <= 15)
				{
					std::string data = deSerializeString(is);
					std::istringstream iss(data, std::ios_base::binary);
					m_node_metadata->deSerialize(iss, m_gamedef);
				}
				else
				{
					//std::string data = deSerializeLongString(is);
					std::ostringstream oss(std::ios_base::binary);
					decompressZlib(is, oss);
					std::istringstream iss(oss.str(), std::ios_base::binary);
					m_node_metadata->deSerialize(iss, m_gamedef);
				}
			}
			catch(SerializationError &e)
			{
				errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error"
						<<" while deserializing node metadata"<<std::endl;
			}
		}
	}
}
Beispiel #30
0
/**
 * Method parses passed input stream and returns the vector of parsed ACL with corresponding rules.
 *
 * @param inputStream reference to input stream std::istream containing input configuration.
 * @return smart pointer containing the pointer to the vector of ACL with rules.
 */
std::auto_ptr< boost::ptr_vector< AccessControlList > > HpInputParser::parse(std::istream& inputStream)
{
    /* use smart pointer to store address and control allocated memory */
    m_aclsVector = auto_ptr< boost::ptr_vector< AccessControlList > >(new boost::ptr_vector< AccessControlList >);

    char buffer[256];

    /* read whole input till EOF */
    while ( !inputStream.eof() )
    {
        inputStream.getline(buffer, 256);
        unsigned tmp_extracted = 0;

        char* tmp_buffer = buffer;

        /* skip white characters at the beginning */
        if ( isspace(buffer[0]) )
        {
            skipWhiteChars(buffer, tmp_extracted);
            tmp_buffer += tmp_extracted;
        }

        /* find out the type of command in line */
        int cmd = parseCommand(tmp_buffer, tmp_extracted);

        /*************************/
        /* command "access-list" */
        if ( cmd == CMD_ACCESS_LIST )
        {
            tmp_buffer += tmp_extracted;        /* shift after "access-list " to first character after it */

            skipWhiteChars(tmp_buffer, tmp_extracted);
            tmp_buffer += tmp_extracted;

            int aclNum = parseAccessListNumber(tmp_buffer, tmp_extracted);
            tmp_buffer += tmp_extracted;        /* shift after the number of ACL to the first next character */

            skipWhiteChars(tmp_buffer, tmp_extracted);
            tmp_buffer += tmp_extracted;

            /* skip REMARK command */
            if ( parseAction(tmp_buffer, tmp_extracted) == CMD_ACL_RULE_REMARK )
            {
                continue;
            }

            /* get ACL to add new rule */
            AccessControlList* tmp_curentAcl = getAclByName(aclNum);

            /* parse and add rule to ACL (but ONLY IPv4) */
            if ( resolveAccessListType(aclNum) != ERROR_FLAG )
            {
                tmp_curentAcl->pushBack(handleAccessList(aclNum, tmp_curentAcl->size(), tmp_buffer).release());
            }
        }
        /****************************/
        /* command "ip access-list" */
        else if ( cmd == CMD_IP_ACCESS_LIST )
        {
            tmp_buffer += tmp_extracted;        /* shift after "ip access-list " to first next character */

            int aclType = resolveAccessListType(tmp_buffer, tmp_extracted);
            if ( aclType == ERROR_FLAG )
            {
                continue;
            }

            tmp_buffer += tmp_extracted;        /* shift after "typ acl" to first next character */
            /* get ACL to add new rule */
            AccessControlList* tmp_curentAcl = getAclByName(parseAccessListName(tmp_buffer));

            /* reading rules of named ACL */
            while ( !inputStream.eof() )
            {
                inputStream.getline(buffer, 256);
                tmp_buffer = buffer;

                /* get the type of rule in line */
                if ( isspace(buffer[0]) )
                {
                    skipWhiteChars(buffer, tmp_extracted);
                    tmp_buffer += tmp_extracted;
                }

                /* if there is sequence number before the command -> skip */
                int tmp = 0;
                while ( isdigit(tmp_buffer[tmp]) )
                {
                    tmp++;
                }
                if ( tmp != 0 )
                {
                    tmp_buffer += tmp;
                    skipWhiteChars(tmp_buffer, tmp_extracted);
                    tmp_buffer += tmp_extracted;
                }

                /* find out the type of the rule in line */
                int cmd = parseCommand(tmp_buffer, tmp_extracted);

                /* if the rule is REMARK -> continue to the next */
                if ( cmd == CMD_ACL_RULE_REMARK )
                {
                    continue;
                }
                /* if there was EXIT command */
                else if ( cmd == CMD_EXIT )
                {
                    break;
                }
                /* if it is not the rule of named ACL, return read line and break the cycle! */
                else if ( cmd != CMD_IP_ACCESS_LIST_RULE )
                {
                    rollbackStream(inputStream, inputStream.gcount());
                    break;
                }

                /* the rule of the standard ACL */
                if ( aclType == ACL_STANDARD )
                {
                    tmp_curentAcl->pushBack(handleStandardRule(tmp_curentAcl->size(), tmp_buffer).release());
                }
                /* the rule of the extended ACL */
                else
                {
                    tmp_curentAcl->pushBack(handleExtendedRule(tmp_curentAcl->size(), tmp_buffer).release());
                }
            }
        }
        else
        {
            continue;
        }
    }

    /* clear the map */
    m_aclsByName.clear();

    return m_aclsVector;
}