Пример #1
0
void sosicon::shape::Shapefile::
writeDbf( std::ostream &os ) {
    os.write( mDbfHeader, sizeof( mDbfHeader ) );
    os.write( mDbfBuffer, mDbfBufferSize );
}
Пример #2
0
void tap::CodeHeader::write (std::ostream & out) const
{
	out.write (reinterpret_cast <const char *> (block), sizeof (block) );
}
Пример #3
0
bool CLink::Save(std::ostream& so) {
	so.write((char*)&m_nPos, 4);
	so.write((char*)&m_nSize, 4);

	return true;
};
Пример #4
0
void BlockchainSynchronizer::save(std::ostream& os) {
  os.write(reinterpret_cast<const char*>(&m_genesisBlockHash), sizeof(m_genesisBlockHash));
}
Пример #5
0
/* encoded separately for better compression. */
bool HDRWriter::writeBytesRLE(std::ostream &fout, unsigned char *data, int numbytes)
{
#define MINRUNLENGTH 4
    int           cur, beg_run, run_count, old_run_count, nonrun_count;
    unsigned char buf[2];

    cur = 0;

    while (cur < numbytes)
    {
        beg_run = cur;
        /* find next run of length at least 4 if one exists */
        run_count = old_run_count = 0;

        while ((run_count < MINRUNLENGTH) && (beg_run < numbytes))
        {
            beg_run      += run_count;
            old_run_count = run_count;
            run_count     = 1;

            while ((data[beg_run] == data[beg_run + run_count])
                   && (beg_run + run_count < numbytes)
                   && (run_count < 127))
            {
                run_count++;
            }
        }

        /* if data before next big run is a short run then write it as such */
        if ((old_run_count > 1) && (old_run_count == beg_run - cur))
        {
            buf[0] = 128 + old_run_count;   /*write short run*/
            buf[1] = data[cur];
            fout.write(reinterpret_cast<const char*>(buf), sizeof(buf[0]) * 2);
            // if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1) return false
            cur = beg_run;
        }

        /* write out bytes until we reach the start of the next run */
        while (cur < beg_run)
        {
            nonrun_count = beg_run - cur;
            if (nonrun_count > 128)
                nonrun_count = 128;

            buf[0] = nonrun_count;
            fout.write(reinterpret_cast<const char*>(buf), sizeof(buf[0]));
            // if (fwrite(buf,sizeof(buf[0]),1,fp) < 1) return false
            fout.write(reinterpret_cast<const char*>(&data[cur]), sizeof(data[0]) * nonrun_count);
            // if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1) return false;
            cur += nonrun_count;
        }

        /* write out next run if one was found */
        if (run_count >= MINRUNLENGTH)
        {
            buf[0] = 128 + run_count;
            buf[1] = data[beg_run];
            fout.write(reinterpret_cast<const char*>(buf), sizeof(buf[0]) * 2);
            // if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1) return false;
            cur += run_count;
        }
    }

    return true;
#undef MINRUNLENGTH
}
Пример #6
0
 void matrix::save(std::ostream &out) {
     out.write((char*) &m_, sizeof(uint32_t));
     out.write((char*) &n_, sizeof(uint32_t));
     out.write((char*) data_, m_ * n_ * sizeof(real));
 }
Пример #7
0
 bool request::write_header(std::ostream &os) {
     // Some validation
     if (method==http_method::INVALID_METHOD) return false;
     if (url.empty()) return false;
     if (version==http_version::INVALID_VERSION) return false;
     
     // METHOD " " URL " " HTTP_VER "\r\n"
     auto m=detail::method_name_map.find(method);
     if (m==detail::method_name_map.end()) return false;
     os.write(&(m->second[0]), m->second.size());
     os.write(" ", 1);
     os.write(&(url[0]), url.size());
     os.write(" ", 1);
     auto v=detail::http_version_name_map.find(version);
     if (v==detail::http_version_name_map.end()) return false;
     os.write(&(v->second[0]), v->second.size());
     os.write("\r\n", 2);
     
     // Write headers
     for (auto &p: headers) {
         os.write(&(p.first[0]), p.first.size());
         os.write(": ", 2);
         os.write(&(p.second[0]), p.second.size());
         os.write("\r\n", 2);
     }
     // End of header
     os.write("\r\n", 2);
     return true;
 }
void LLFilterSD2XMLRPC::streamOut(std::ostream& ostr, const LLSD& sd)
{
	ostr << "<value>";
	switch(sd.type())
	{
	case LLSD::TypeMap:
	{
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(map) BEGIN" << LL_ENDL;
#endif
		ostr << "<struct>";
		if(ostr.fail())
		{
			LL_INFOS() << "STREAM FAILURE writing struct" << LL_ENDL;
		}
		LLSD::map_const_iterator it = sd.beginMap();
		LLSD::map_const_iterator end = sd.endMap();
		for(; it != end; ++it)
		{
			ostr << "<member><name>" << xml_escape_string((*it).first)
				<< "</name>";
			streamOut(ostr, (*it).second);
			if(ostr.fail())
			{
				LL_INFOS() << "STREAM FAILURE writing '" << (*it).first
						<< "' with sd type " << (*it).second.type() << LL_ENDL;
			}
			ostr << "</member>";
		}
		ostr << "</struct>";
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(map) END" << LL_ENDL;
#endif
		break;
	}
	case LLSD::TypeArray:
	{
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(array) BEGIN" << LL_ENDL;
#endif
		ostr << "<array><data>";
		LLSD::array_const_iterator it = sd.beginArray();
		LLSD::array_const_iterator end = sd.endArray();
		for(; it != end; ++it)
		{
			streamOut(ostr, *it);
			if(ostr.fail())
			{
				LL_INFOS() << "STREAM FAILURE writing array element sd type "
						<< (*it).type() << LL_ENDL;
			}
		}
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(array) END" << LL_ENDL;
#endif
		ostr << "</data></array>";
		break;
	}
	case LLSD::TypeUndefined:
		// treat undefined as a bool with a false value.
	case LLSD::TypeBoolean:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(bool)" << LL_ENDL;
#endif
		ostr << "<boolean>" << (sd.asBoolean() ? "1" : "0") << "</boolean>";
		break;
	case LLSD::TypeInteger:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(int)" << LL_ENDL;
#endif
		ostr << "<i4>" << sd.asInteger() << "</i4>";
		break;
	case LLSD::TypeReal:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(real)" << LL_ENDL;
#endif
		ostr << "<double>" << sd.asReal() << "</double>";
		break;
	case LLSD::TypeString:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(string)" << LL_ENDL;
#endif
		ostr << "<string>" << xml_escape_string(sd.asString()) << "</string>";
		break;
	case LLSD::TypeUUID:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(uuid)" << LL_ENDL;
#endif
		// serialize it as a string
		ostr << "<string>" << sd.asString() << "</string>";
		break;
	case LLSD::TypeURI:
	{
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(uri)" << LL_ENDL;
#endif
		// serialize it as a string
		ostr << "<string>" << xml_escape_string(sd.asString()) << "</string>";
		break;
	}
	case LLSD::TypeBinary:
	{
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(binary)" << LL_ENDL;
#endif
		// this is pretty inefficient, but we'll deal with that
		// problem when it becomes one.
		ostr << "<base64>";
		LLSD::Binary buffer = sd.asBinary();
		if(!buffer.empty())
		{
			// *TODO: convert to LLBase64
			int b64_buffer_length = apr_base64_encode_len(buffer.size());
			char* b64_buffer = new char[b64_buffer_length];
			b64_buffer_length = apr_base64_encode_binary(
				b64_buffer,
				&buffer[0],
				buffer.size());
			ostr.write(b64_buffer, b64_buffer_length - 1);
			delete[] b64_buffer;
		}
		ostr << "</base64>";
		break;
	}
	case LLSD::TypeDate:
#if LL_SPEW_STREAM_OUT_DEBUGGING
		LL_INFOS() << "streamOut(date)" << LL_ENDL;
#endif
		// no need to escape this since it will be alpha-numeric.
		ostr << "<dateTime.iso8601>" << sd.asString() << "</dateTime.iso8601>";
		break;
	default:
		// unhandled type
		LL_WARNS() << "Unhandled structured data type: " << sd.type()
			<< LL_ENDL;
		break;
	}
	ostr << "</value>";
}
S32 LLSDNotationFormatter::format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const
{
	S32 format_count = 1;
	std::string pre;
	std::string post;

	if (options & LLSDFormatter::OPTIONS_PRETTY)
	{
		for (U32 i = 0; i < level; i++)
		{
			pre += "    ";
		}
		post = "\n";
	}

	switch(data.type())
	{
	case LLSD::TypeMap:
	{
		if (0 != level) ostr << post << pre;
		ostr << "{";
		std::string inner_pre;
		if (options & LLSDFormatter::OPTIONS_PRETTY)
		{
			inner_pre = pre + "    ";
		}

		bool need_comma = false;
		LLSD::map_const_iterator iter = data.beginMap();
		LLSD::map_const_iterator end = data.endMap();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			ostr << post << inner_pre << '\'';
			serialize_string((*iter).first, ostr);
			ostr << "':";
			format_count += format_impl((*iter).second, ostr, options, level + 2);
		}
		ostr << post << pre << "}";
		break;
	}

	case LLSD::TypeArray:
	{
		ostr << post << pre << "[";
		bool need_comma = false;
		LLSD::array_const_iterator iter = data.beginArray();
		LLSD::array_const_iterator end = data.endArray();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			format_count += format_impl(*iter, ostr, options, level + 1);
		}
		ostr << "]";
		break;
	}

	case LLSD::TypeUndefined:
		ostr << "!";
		break;

	case LLSD::TypeBoolean:
		if(mBoolAlpha ||
#if( LL_WINDOWS || __GNUC__ > 2)
		   (ostr.flags() & std::ios::boolalpha)
#else
		   (ostr.flags() & 0x0100)
#endif
			)
		{
			ostr << (data.asBoolean()
					 ? NOTATION_TRUE_SERIAL : NOTATION_FALSE_SERIAL);
		}
		else
		{
			ostr << (data.asBoolean() ? 1 : 0);
		}
		break;

	case LLSD::TypeInteger:
		ostr << "i" << data.asInteger();
		break;

	case LLSD::TypeReal:
		ostr << "r";
		if(mRealFormat.empty())
		{
			ostr << data.asReal();
		}
		else
		{
			formatReal(data.asReal(), ostr);
		}
		break;

	case LLSD::TypeUUID:
		ostr << "u" << data.asUUID();
		break;

	case LLSD::TypeString:
		ostr << '\'';
		serialize_string(data.asStringRef(), ostr);
		ostr << '\'';
		break;

	case LLSD::TypeDate:
		ostr << "d\"" << data.asDate() << "\"";
		break;

	case LLSD::TypeURI:
		ostr << "l\"";
		serialize_string(data.asString(), ostr);
		ostr << "\"";
		break;

	case LLSD::TypeBinary:
	{
		// *FIX: memory inefficient.
		const std::vector<U8>& buffer = data.asBinary();
		ostr << "b(" << buffer.size() << ")\"";
		if(buffer.size())
		{
			if (options & LLSDFormatter::OPTIONS_PRETTY_BINARY)
			{
				std::ios_base::fmtflags old_flags = ostr.flags();
				ostr.setf( std::ios::hex, std::ios::basefield );
				ostr << "0x";
				for (size_t i = 0; i < buffer.size(); i++)
				{
					ostr << (int) buffer[i];
				}
				ostr.flags(old_flags);
			}
			else
			{
				ostr.write((const char*)&buffer[0], buffer.size());
			}
		}
		ostr << "\"";
		break;
	}

	default:
		// *NOTE: This should never happen.
		ostr << "!";
		break;
	}
	return format_count;
}
// virtual
S32 LLSDBinaryFormatter::format(const LLSD& data, std::ostream& ostr, U32 options) const
{
	S32 format_count = 1;
	switch(data.type())
	{
	case LLSD::TypeMap:
	{
		ostr.put('{');
		U32 size_nbo = htonl(data.size());
		ostr.write((const char*)(&size_nbo), sizeof(U32));
		LLSD::map_const_iterator iter = data.beginMap();
		LLSD::map_const_iterator end = data.endMap();
		for(; iter != end; ++iter)
		{
			ostr.put('k');
			formatString((*iter).first, ostr);
			format_count += format((*iter).second, ostr);
		}
		ostr.put('}');
		break;
	}

	case LLSD::TypeArray:
	{
		ostr.put('[');
		U32 size_nbo = htonl(data.size());
		ostr.write((const char*)(&size_nbo), sizeof(U32));
		LLSD::array_const_iterator iter = data.beginArray();
		LLSD::array_const_iterator end = data.endArray();
		for(; iter != end; ++iter)
		{
			format_count += format(*iter, ostr);
		}
		ostr.put(']');
		break;
	}

	case LLSD::TypeUndefined:
		ostr.put('!');
		break;

	case LLSD::TypeBoolean:
		if(data.asBoolean()) ostr.put(BINARY_TRUE_SERIAL);
		else ostr.put(BINARY_FALSE_SERIAL);
		break;

	case LLSD::TypeInteger:
	{
		ostr.put('i');
		U32 value_nbo = htonl(data.asInteger());
		ostr.write((const char*)(&value_nbo), sizeof(U32));
		break;
	}

	case LLSD::TypeReal:
	{
		ostr.put('r');
		F64 value_nbo = ll_htond(data.asReal());
		ostr.write((const char*)(&value_nbo), sizeof(F64));
		break;
	}

	case LLSD::TypeUUID:
	{
		ostr.put('u');
		LLUUID temp = data.asUUID();
		ostr.write((const char*)(&(temp.mData)), UUID_BYTES);
		break;
	}

	case LLSD::TypeString:
		ostr.put('s');
		formatString(data.asStringRef(), ostr);
		break;

	case LLSD::TypeDate:
	{
		ostr.put('d');
		F64 value = data.asReal();
		ostr.write((const char*)(&value), sizeof(F64));
		break;
	}

	case LLSD::TypeURI:
		ostr.put('l');
		formatString(data.asString(), ostr);
		break;

	case LLSD::TypeBinary:
	{
		ostr.put('b');
		const std::vector<U8>& buffer = data.asBinary();
		U32 size_nbo = htonl(buffer.size());
		ostr.write((const char*)(&size_nbo), sizeof(U32));
		if(buffer.size()) ostr.write((const char*)&buffer[0], buffer.size());
		break;
	}

	default:
		// *NOTE: This should never happen.
		ostr.put('!');
		break;
	}
	return format_count;
}
Пример #11
0
  unsigned long
  StaticRangeCoder::encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg,
                                              std::ostream& outputByteStream_arg)
  {
    DWord freq[257];
    uint8_t ch;
    int i, f;
    char out;

    // define numerical limits
    const DWord top = (DWord)1 << 24;
    const DWord bottom = (DWord)1 << 16;
    const DWord maxRange = (DWord)1 << 16;

    DWord low, range;

    unsigned int input_size;
    input_size = inputByteVector_arg.size ();

    unsigned int readPos;

    unsigned long streamByteCount;

    streamByteCount = 0;

    // init output vector
    outputCharVector_.clear();
    outputCharVector_.reserve(sizeof(char) * input_size);

    uint64_t FreqHist[257];

    // calculate frequency table
    memset (FreqHist, 0, sizeof(FreqHist));
    readPos = 0;
    while (readPos < input_size)
    {
      uint8_t symbol = (uint8_t)inputByteVector_arg[readPos++];
      FreqHist[symbol + 1]++;
    }

    // convert to cumulative frequency table
    freq[0] = 0;
    for (f = 1; f <= 256; f++)
    {
      freq[f] = freq[f - 1] + (DWord)FreqHist[f];
      if (freq[f] <= freq[f - 1])
        freq[f] = freq[f - 1] + 1;
    }

    // rescale if numerical limits are reached
    while (freq[256] >= maxRange)
    {
      for (f = 1; f <= 256; f++)
      {
        freq[f] /= 2;
        ;
        if (freq[f] <= freq[f - 1])
          freq[f] = freq[f - 1] + 1;
      }
    }

    // write cumulative  frequency table to output stream
    outputByteStream_arg.write ((const char *)&freq[0], sizeof(freq));
    streamByteCount += sizeof(freq);

    readPos = 0;

    low = 0;
    range = (DWord)-1;

    // start encoding
    while (readPos < input_size)
    {

      // read symol
      ch = inputByteVector_arg[readPos++];

      // map to range
      low += freq[ch] * (range /= freq[256]);
      range *= freq[ch + 1] - freq[ch];

      // check range limits
      while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
      {
        out = low >> 24;
        range <<= 8;
        low <<= 8;
        outputCharVector_.push_back(out);
      }

    }

    // flush remaining data
    for (i = 0; i < 4; i++)
    {
      out = low >> 24;
      outputCharVector_.push_back(out);
      low <<= 8;
    }

    // write encoded data to stream
    outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size());

    streamByteCount += outputCharVector_.size();

    return streamByteCount;

  }
Пример #12
0
  unsigned long
  StaticRangeCoder::encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg,
                                             std::ostream& outputByteStream_arg)
  {

    unsigned int inputsymbol;
    unsigned int i, f;
    char out;

    uint64_t frequencyTableSize;
    uint8_t frequencyTableByteSize;

    // define numerical limits
    const uint64_t top = (uint64_t)1 << 56;
    const uint64_t bottom = (uint64_t)1 << 48;
    const uint64_t maxRange = (uint64_t)1 << 48;

    unsigned long input_size = (unsigned) inputIntVector_arg.size ();
    uint64_t low, range;

    unsigned int inputSymbol;

    unsigned int readPos;

    unsigned long streamByteCount;

    streamByteCount = 0;

    // init output vector
    outputCharVector_.clear();
    outputCharVector_.reserve(sizeof(char) * input_size * 2);

    frequencyTableSize = 1;

    readPos = 0;

    // calculate frequency table
    cFreqTable_[0] = cFreqTable_[1] = 0;
    while (readPos < input_size)
    {
      inputSymbol = inputIntVector_arg[readPos++];

      if (inputSymbol + 1 >= frequencyTableSize)
      {
        // frequency table is to small -> adaptively extend it
        uint64_t oldfrequencyTableSize;
        oldfrequencyTableSize = frequencyTableSize;

        do
        {
          // increase frequency table size by factor 2
          frequencyTableSize <<= 1;
        } while (inputSymbol + 1 > frequencyTableSize);

        if (cFreqTable_.size () < frequencyTableSize + 1)
        {
          // resize frequency vector
          cFreqTable_.resize (frequencyTableSize + 1);
        }

        // init new frequency range with zero
        memset (&cFreqTable_[oldfrequencyTableSize + 1], 0,
                sizeof(uint64_t) * (frequencyTableSize - oldfrequencyTableSize));
      }
      cFreqTable_[inputSymbol + 1]++;
    }
    frequencyTableSize++;

    // convert to cumulative frequency table
    for (f = 1; f < frequencyTableSize; f++)
    {
      cFreqTable_[f] = cFreqTable_[f - 1] + cFreqTable_[f];
      if (cFreqTable_[f] <= cFreqTable_[f - 1])
        cFreqTable_[f] = cFreqTable_[f - 1] + 1;
    }

    // rescale if numerical limits are reached
    while (cFreqTable_[frequencyTableSize - 1] >= maxRange)
    {
      for (f = 1; f < cFreqTable_.size (); f++)
      {
        cFreqTable_[f] /= 2;
        ;
        if (cFreqTable_[f] <= cFreqTable_[f - 1])
          cFreqTable_[f] = cFreqTable_[f - 1] + 1;
      }
    }

    // calculate amount of bytes per frequency table entry
    frequencyTableByteSize = (uint8_t)ceil (Log2 ((double) cFreqTable_[frequencyTableSize - 1]) / 8.0);

    // write size of frequency table to output stream
    outputByteStream_arg.write ((const char *)&frequencyTableSize, sizeof(frequencyTableSize));
    outputByteStream_arg.write ((const char *)&frequencyTableByteSize, sizeof(frequencyTableByteSize));

    streamByteCount += sizeof(frequencyTableSize)+sizeof(frequencyTableByteSize);

    // write cumulative  frequency table to output stream
    for (f = 1; f < frequencyTableSize; f++)
    {
      outputByteStream_arg.write ((const char *)&cFreqTable_[f], frequencyTableByteSize);
      streamByteCount += frequencyTableByteSize;
    }

    readPos = 0;
    low = 0;
    range = (uint64_t)-1;

    // start encoding
    while (readPos < input_size)
    {

      // read symol
      inputsymbol = inputIntVector_arg[readPos++];

      // map to range
      low += cFreqTable_[inputsymbol] * (range /= cFreqTable_[frequencyTableSize - 1]);
      range *= cFreqTable_[inputsymbol + 1] - cFreqTable_[inputsymbol];

      // check range limits
      while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
      {
        out = low >> 56;
        range <<= 8;
        low <<= 8;
        outputCharVector_.push_back(out);
      }

    }

    // flush remaining data
    for (i = 0; i < 8; i++)
    {
      out = low >> 56;
      outputCharVector_.push_back(out);
      low <<= 8;
    }

    // write encoded data to stream
    outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size());

    streamByteCount += outputCharVector_.size();

    return streamByteCount;

  }
Пример #13
0
/* Decompress from file source to file dest until stream ends or EOF.
   inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
   allocated for processing, Z_DATA_ERROR if the deflate data is
   invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
   the version of the library linked do not match, or Z_ERRNO if there
   is an error reading or writing the files. */
bool Misc::inflate(const uint8_t* in, size_t inlen, std::ostream& dest, string& err, int CHUNK)
{
  int ret;
  unsigned have;
  z_stream strm;
  if ( CHUNK == -1 ) CHUNK = CL_Z_DEFAULT_CHUNK;
  uint8_t* out = (uint8_t*)malloc(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 = inflateInit(&strm);
  if (ret != Z_OK){
    free(out);
    zerr(ret, err);
    return false;
  }

  /* decompress until deflate stream ends or end of file */
  do {
    strm.avail_in = inlen;
    if (strm.avail_in == 0)
      break;
    strm.next_in = (uint8_t*)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);
      assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
      switch (ret) {
      case Z_NEED_DICT:
        ret = Z_DATA_ERROR;   /* and fall through */
      case Z_DATA_ERROR:
      case Z_MEM_ERROR:
        (void)inflateEnd(&strm);
        free(out);
        zerr(ret, err);
        return false;
      }
      have = CHUNK - strm.avail_out;
      dest.write( (char*)out,have);
      if ( dest.fail() ) {
        (void)inflateEnd(&strm);
        free(out);
        zerr(Z_ERRNO, err);
        return false;
      }
    } while (strm.avail_out == 0);

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

  /* clean up and return */
  (void)inflateEnd(&strm);
  free(out);
  if ( ret == Z_STREAM_END )
    return true;
  zerr(Z_DATA_ERROR, err);
  return false;
}
Пример #14
0
void tap::CodeBlock::write (std::ostream & out) const
{
	out.write (reinterpret_cast <const char *> (head), sizeof (head) );
	out.write (reinterpret_cast <const char *> (data), datasize);
	out.write (reinterpret_cast <const char *> (& check), 1);
}
Пример #15
0
    bool response::write_header(std::ostream &os) {
        // Some validation
        if (status_code==http_status_code::INVALID_STATUS) return false;
        //if (status_message.empty()) return false;
        if (version==http_version::INVALID_VERSION) return false;
        
        // HTTP_VER " " STATUS_CODE URL " "  "\r\n"
        auto v=detail::http_version_name_map.find(version);
        if (v==detail::http_version_name_map.end()) return false;
        os.write(&(v->second[0]), v->second.size());
        os.write(" ", 1);
        
        std::string sc=boost::lexical_cast<std::string>(static_cast<unsigned short>(status_code));
        os.write(&(sc[0]), sc.size());
        os.write(" ", 1);

        auto s=detail::status_msg_map.find(status_code);
        if (s==detail::status_msg_map.end()) return false;
        os.write(&(s->second[0]), s->second.size());
        os.write("\r\n", 2);
        
        // Write headers
        for (auto &p: headers) {
            os.write(&(p.first[0]), p.first.size());
            os.write(": ", 2);
            os.write(&(p.second[0]), p.second.size());
            os.write("\r\n", 2);
        }
        // End of header
        os.write("\r\n", 2);
        return true;
    }
Пример #16
0
void MapBlock::serialize_pre22(std::ostream &os, u8 version, bool disk)
{
	u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;

	MapNode *tmp_data = new MapNode[nodecount];
	
	// Legacy data changes
	// This code has to change from post-22 to pre-22 format.
	INodeDefManager *nodedef = m_gamedef->ndef();
	for(u32 i=0; i<nodecount; i++)
	{
		const ContentFeatures &f = nodedef->get(tmp_data[i].getContent());
		// Mineral
		if(nodedef->getId("default:stone_with_coal") == tmp_data[i].getContent())
		{
			tmp_data[i].setContent(nodedef->getId("default:stone"));
			tmp_data[i].setParam1(1);  // MINERAL_COAL
		}
		else if(nodedef->getId("default:stone_with_iron") == tmp_data[i].getContent())
		{
			tmp_data[i].setContent(nodedef->getId("default:stone"));
			tmp_data[i].setParam1(2);  // MINERAL_IRON
		}
		// facedir_simple
		if(f.legacy_facedir_simple)
		{
			tmp_data[i].setParam1(tmp_data[i].getParam2());
			tmp_data[i].setParam2(0);
		}
		// wall_mounted
		if(f.legacy_wallmounted)
		{
			u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
			u8 dir_new_format = tmp_data[i].getParam2() & 7; // lowest 3 bits
			u8 dir_old_format = wallmounted_new_to_old[dir_new_format];
			tmp_data[i].setParam2(dir_old_format);
		}
	}

	// Serialize nodes
	u32 ser_length = MapNode::serializedLength(version);
	SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
	for(u32 i=0; i<nodecount; i++)
	{
		tmp_data[i].serialize(&databuf_nodelist[i*ser_length], version);
	}

	delete[] tmp_data;
		
	// These have no compression
	if(version <= 3 || version == 5 || version == 6)
	{
		writeU8(os, is_underground);
		os.write((char*)*databuf_nodelist, databuf_nodelist.getSize());
	}
	else if(version <= 10)
	{
		/*
			With compression.
			Compress the materials and the params separately.
		*/
		
		// First byte
		writeU8(os, is_underground);

		// Get and compress materials
		SharedBuffer<u8> materialdata(nodecount);
		for(u32 i=0; i<nodecount; i++)
		{
			materialdata[i] = databuf_nodelist[i*ser_length];
		}
		compress(materialdata, os, version);

		// Get and compress lights
		SharedBuffer<u8> lightdata(nodecount);
		for(u32 i=0; i<nodecount; i++)
		{
			lightdata[i] = databuf_nodelist[i*ser_length+1];
		}
		compress(lightdata, os, version);
		
		if(version >= 10)
		{
			// Get and compress param2
			SharedBuffer<u8> param2data(nodecount);
			for(u32 i=0; i<nodecount; i++)
			{
				param2data[i] = databuf_nodelist[i*ser_length+2];
			}
			compress(param2data, os, version);
		}
	}
	// All other versions (newest)
	else
	{
		// First byte
		u8 flags = 0;
		if(is_underground)
			flags |= 0x01;
		if(getDayNightDiff())
			flags |= 0x02;
		if(m_lighting_expired)
			flags |= 0x04;
		if(version >= 18)
		{
			if(m_generated == false)
				flags |= 0x08;
		}
		writeU8(os, flags);
		
		/*
			Get data
		*/

		// Create buffer with different parameters sorted
		SharedBuffer<u8> databuf(nodecount*3);
		for(u32 i=0; i<nodecount; i++)
		{
			databuf[i] = databuf_nodelist[i*ser_length];
			databuf[i+nodecount] = databuf_nodelist[i*ser_length+1];
			databuf[i+nodecount*2] = databuf_nodelist[i*ser_length+2];
		}

		/*
			Compress data to output stream
		*/

		compress(databuf, os, version);
		
		/*
			NodeMetadata
		*/
		if(version >= 14)
		{
			if(version <= 15)
			{
				try{
					std::ostringstream oss(std::ios_base::binary);
					m_node_metadata->serialize(oss);
					os<<serializeString(oss.str());
				}
				// This will happen if the string is longer than 65535
				catch(SerializationError &e)
				{
					// Use an empty string
					os<<serializeString("");
				}
			}
			else
			{
				std::ostringstream oss(std::ios_base::binary);
				m_node_metadata->serialize(oss);
				compressZlib(oss.str(), os);
				//os<<serializeLongString(oss.str());
			}
		}
	}


	if(disk)
	{
		// Versions up from 9 have block objects. (DEPRECATED)
		if(version >= 9)
		{
			// count=0
			writeU16(os, 0);
		}

		// Versions up from 15 have static objects.
		if(version >= 15)
		{
			m_static_objects.serialize(os);
		}

		// Timestamp
		if(version >= 17)
		{
			writeU32(os, getTimestamp());
		}

		// Scan and write node definition id mapping
		if(version >= 21)
		{
			NameIdMapping nimap;
			getBlockNodeIdMapping_pre22(&nimap, data, m_gamedef->ndef());
			nimap.serialize(os);
		}
	}
}
Пример #17
0
void PhraseTableCreator::EncodeTargetPhrasePREnc(std::vector<std::string>& s,
    std::vector<std::string>& t,
    std::set<AlignPoint>& a,
    size_t ownRank,
    std::ostream& os)
{
  std::vector<unsigned> encodedSymbols(t.size());
  std::vector<unsigned> encodedSymbolsLengths(t.size(), 0);

  ConsistentPhrases cp(s.size(), t.size(), a);
  while(!cp.Empty()) {
    ConsistentPhrases::Phrase p = cp.Pop();

    std::stringstream key1;
    key1 << s[p.i];
    for(int i = p.i+1; i < p.i+p.m; i++)
      key1 << " " << s[i];

    std::stringstream key2;
    key2 << t[p.j];
    for(int i = p.j+1; i < p.j+p.n; i++)
      key2 << " " << t[i];

    int rank = -1;
    std::string key1Str = key1.str(), key2Str = key2.str();
    size_t idx = m_rnkHash[MakeSourceTargetKey(key1Str, key2Str)];
    if(idx != m_rnkHash.GetSize())
      rank = m_ranks[idx];

    if(rank >= 0 && (m_maxRank == 0 || unsigned(rank) < m_maxRank)) {
      if(unsigned(p.m) != s.size() || unsigned(rank) < ownRank) {
        std::stringstream encodedSymbol;
        encodedSymbols[p.j] = EncodePREncSymbol2(p.i-p.j, s.size()-(p.i+p.m), rank);
        encodedSymbolsLengths[p.j] = p.n;

        std::set<AlignPoint> tAlignment;
        for(std::set<AlignPoint>::iterator it = a.begin();
            it != a.end(); it++)
          if(it->first < p.i || it->first >= p.i + p.m
              || it->second < p.j || it->second >= p.j + p.n)
            tAlignment.insert(*it);
        a = tAlignment;
        cp.RemoveOverlap(p);
      }
    }
  }

  std::stringstream encodedTargetPhrase;

  size_t j = 0;
  while(j < t.size()) {
    if(encodedSymbolsLengths[j] > 0) {
      unsigned encodedSymbol = encodedSymbols[j];
      m_symbolCounter.Increase(encodedSymbol);
      os.write((char*)&encodedSymbol, sizeof(encodedSymbol));
      j += encodedSymbolsLengths[j];
    } else {
      unsigned targetSymbolId = GetOrAddTargetSymbolId(t[j]);
      unsigned encodedSymbol = EncodePREncSymbol1(targetSymbolId);
      m_symbolCounter.Increase(encodedSymbol);
      os.write((char*)&encodedSymbol, sizeof(encodedSymbol));
      j++;
    }
  }

  unsigned stopSymbolId = GetTargetSymbolId(m_phraseStopSymbol);
  unsigned encodedSymbol = EncodePREncSymbol1(stopSymbolId);
  os.write((char*)&encodedSymbol, sizeof(encodedSymbol));
  m_symbolCounter.Increase(encodedSymbol);
}
// this is a c function here since it's really an implementation
// detail that requires a header file just get the definition of the
// parameters.
LLIOPipe::EStatus stream_out(std::ostream& ostr, XMLRPC_VALUE value)
{
	XMLRPC_VALUE_TYPE_EASY type = XMLRPC_GetValueTypeEasy(value);
	LLIOPipe::EStatus status = LLIOPipe::STATUS_OK;
	switch(type)
	{
	case xmlrpc_type_base64:
	{
		S32 len = XMLRPC_GetValueStringLen(value);
		const char* buf = XMLRPC_GetValueBase64(value);
		ostr << " b(";
		if((len > 0) && buf)
		{
			ostr << len << ")\"";
			ostr.write(buf, len);
			ostr << "\"";
		}
		else
		{
			ostr << "0)\"\"";
		}
		break;
	}
	case xmlrpc_type_boolean:
		//LL_DEBUGS() << "stream_out() bool" << LL_ENDL;
		ostr << " " << (XMLRPC_GetValueBoolean(value) ? "true" : "false");
		break;
	case xmlrpc_type_datetime:
		ostr << " d\"" << XMLRPC_GetValueDateTime_ISO8601(value) << "\"";
		break;
	case xmlrpc_type_double:
		ostr << " r" << XMLRPC_GetValueDouble(value);
		//LL_DEBUGS() << "stream_out() double" << XMLRPC_GetValueDouble(value)
		//		 << LL_ENDL;
		break;
	case xmlrpc_type_int:
		ostr << " i" << XMLRPC_GetValueInt(value);
		//LL_DEBUGS() << "stream_out() integer:" << XMLRPC_GetValueInt(value)
		//		 << LL_ENDL;
		break;
	case xmlrpc_type_string:
		//LL_DEBUGS() << "stream_out() string: " << str << LL_ENDL;
		ostr << " s(" << XMLRPC_GetValueStringLen(value) << ")'"
			<< XMLRPC_GetValueString(value) << "'";
		break;
	case xmlrpc_type_array: // vector
	case xmlrpc_type_mixed: // vector
	{
		//LL_DEBUGS() << "stream_out() array" << LL_ENDL;
		ostr << " [";
		U32 needs_comma = 0;
		XMLRPC_VALUE current = XMLRPC_VectorRewind(value);
		while(current && (LLIOPipe::STATUS_OK == status))
		{
			if(needs_comma++) ostr << ",";
			status = stream_out(ostr, current);
			current = XMLRPC_VectorNext(value);
		}
		ostr << "]";
		break;
	}
	case xmlrpc_type_struct: // still vector
	{
		//LL_DEBUGS() << "stream_out() struct" << LL_ENDL;
		ostr << " {";
		std::string name;
		U32 needs_comma = 0;
		XMLRPC_VALUE current = XMLRPC_VectorRewind(value);
		while(current && (LLIOPipe::STATUS_OK == status))
		{
			if(needs_comma++) ostr << ",";
			name.assign(XMLRPC_GetValueID(current));
			ostr << "'" << LLSDNotationFormatter::escapeString(name) << "':";
			status = stream_out(ostr, current);
			current = XMLRPC_VectorNext(value);
		}
		ostr << "}";
		break;
	}
	case xmlrpc_type_empty:
	case xmlrpc_type_none:
	default:
		status = LLIOPipe::STATUS_ERROR;
		LL_WARNS() << "Found an empty xmlrpc type.." << LL_ENDL;
		// not much we can do here...
		break;
	};
	return status;
}
	void max_subsampling_layer::write(std::ostream& binary_stream_to_write_to) const
	{
		unsigned int dimension_count = static_cast<unsigned int>(subsampling_sizes.size());
		binary_stream_to_write_to.write(reinterpret_cast<const char*>(&dimension_count), sizeof(dimension_count));
		binary_stream_to_write_to.write(reinterpret_cast<const char*>(&(*subsampling_sizes.begin())), sizeof(unsigned int) * dimension_count);
	}
Пример #20
0
void GfxImageWriterPNM::writeImage(
    std::ostream& os,
    const GfxImage& image
) {
    DGFX_ASSERT( os.good() );

    switch( image.getFormat() ) {
        case GfxImage::GREY8: {
            if ( _formatType == ASCII ) {
                os << "P2";
            }
            else {
                os << "P5";
            }
        } break;
        case GfxImage::RGB24: {
            if ( _formatType == ASCII ) {
                os << "P3";
            }
            else {
                os << "P6";
            }
        } break;
    }
    os << std::endl;
    os << image.getWidth() << " " << image.getHeight() << std::endl;
    switch ( image.getBitsPerComponent() ) {
        case 8: {
            os << "255 ";
        } break;
        default: {
            DGFX_ASSERT( 0 );
        }
    }

    if ( _formatType == ASCII ) {
        UInt32 x, y, c;
        switch ( image.getBitsPerComponent() ) {
            case 8: {
                for ( y = 0; y < image.getHeight(); ++y ) {
                    for ( x = 0; x < image.getWidth(); ++x ) {
                        for ( c = 0; c < image.getNbComponents(); ++c ) {
                            os << static_cast<UInt32>( image.getData( x, y, c ) );
                            os << " ";
                        }
                        os << " ";
                    }
                    os << std::endl;
                }
            } break;

            default: {
                DGFX_ASSERT( 0 );
            }
        }
    }
    else {
        // FIXME: dependent upon endianness!
        os.write(
            reinterpret_cast<const char*>( image.getRawData() ),
            image.getRawSize()
        );
    }
}
Пример #21
0
	void StreamObject::writeStream(std::ostream& fout) const
	{
		fout.write(_membuffer.c_str(), _membuffer.length());
	}
Пример #22
0
inline bool writeFingerprint(std::ostream &stream)
{
    const auto fingerprint = FingerPrint::GetValid();
    stream.write(reinterpret_cast<const char *>(&fingerprint), sizeof(fingerprint));
    return static_cast<bool>(stream);
}
Пример #23
0
static void WriteZStr(std::ostream& f, const string& s)
{
	int c;
	c = s.length ();
	f.write (s.c_str(),c+1);
}
bool SGSHAPE::WriteCache( std::ostream& aFile, SGNODE* parentNode )
{
    if( NULL == parentNode )
    {
        if( NULL == m_Parent )
        {
            #ifdef DEBUG
            std::ostringstream ostr;
            ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
            ostr << " * [BUG] corrupt data; m_aParent is NULL";
            wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
            #endif

            return false;
        }

        SGNODE* np = m_Parent;

        while( NULL != np->GetParent() )
            np = np->GetParent();

        if( np->WriteCache( aFile, NULL ) )
        {
            m_written = true;
            return true;
        }

        return false;
    }

    if( parentNode != m_Parent )
    {
        #ifdef DEBUG
        std::ostringstream ostr;
        ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
        ostr << " * [BUG] corrupt data; parentNode != m_aParent";
        wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
        #endif

        return false;
    }

    if( !aFile.good() )
    {
        #ifdef DEBUG
        std::ostringstream ostr;
        ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
        ostr << " * [INFO] bad stream";
        wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
        #endif

        return false;
    }

    // check if any references are unwritten and swap parents if so
    if( NULL != m_RAppearance && !m_RAppearance->isWritten() )
        m_RAppearance->SwapParent(this);

    if( NULL != m_RFaceSet && !m_RFaceSet->isWritten() )
        m_RFaceSet->SwapParent( this );

    aFile << "[" << GetName() << "]";
    #define NITEMS 4
    bool items[NITEMS];
    int i;

    for( i = 0; i < NITEMS; ++i )
        items[i] = 0;

    i = 0;
    if( NULL != m_Appearance )
        items[i] = true;

    ++i;
    if( NULL != m_RAppearance )
        items[i] = true;

    ++i;
    if( NULL != m_FaceSet )
        items[i] = true;

    ++i;
    if( NULL != m_RFaceSet )
        items[i] = true;

    for( int jj = 0; jj < NITEMS; ++jj )
        aFile.write( (char*)&items[jj], sizeof(bool) );

    if( items[0] )
        m_Appearance->WriteCache( aFile, this );

    if( items[1] )
        aFile << "[" << m_RAppearance->GetName() << "]";

    if( items[2] )
        m_FaceSet->WriteCache( aFile, this );

    if( items[3] )
        aFile << "[" << m_RFaceSet->GetName() << "]";

    if( aFile.fail() )
        return false;

    m_written = true;
    return true;
}
Пример #25
0
void tap::BasicBlock::write (std::ostream & out) const
{
	out.write (reinterpret_cast <const char *> (block), sizeof (block) );
	out.write (basic.data (), basicsize);
	out.write (reinterpret_cast <const char *> (& check), 1);
}
Пример #26
0
void serialize_trait<std::string>::writeToFile(const std::string& value, std::ostream& file)
{
  size_t count = value.size();
  file.write((char*)&count, sizeof(count));
  file.write((char*)value.c_str(), sizeof(std::string::value_type) * count);
}
Пример #27
0
void sosicon::shape::Shapefile::
writeShx( std::ostream &os ) {
    os.write( mShxHeader, sizeof( mShxHeader ) );
    os.write( mShxBuffer, mShxBufferSize );
}