Example #1
0
void MolCatalogEntry::initFromStream(std::istream &ss) {
  delete dp_mol;
  dp_mol = nullptr;
  delete dp_props;
  dp_props = nullptr;

  // the molecule:
  dp_mol = new ROMol();
  MolPickler::molFromPickle(ss, *const_cast<ROMol *>(dp_mol));

  dp_props = new Dict();

  boost::int32_t tmpInt;
  // the bitId:
  streamRead(ss, tmpInt);
  setBitId(tmpInt);

  // the order:
  streamRead(ss, tmpInt);
  setOrder(tmpInt);

  // the description:
  streamRead(ss, tmpInt);
  auto *tmpText = new char[tmpInt + 1];
  ss.read(tmpText, tmpInt * sizeof(char));
  tmpText[tmpInt] = 0;
  d_descrip = tmpText;
  delete[] tmpText;
}
Example #2
0
/* Documentation from Andrew's code on the structure of the arena:
The 'AREN'a starts with a header:
<num_bytes: 4 bytes>  -- the number of bytes in a fingerprint
<storage_size: 4 bytes>  -- number of bytes in fingerprint + extra bytes
<spacer_size: 1 byte>   -- the number of spacer bytes used so the fingerprint
       chunk starts on an aligned file position.
<spacer : $spacer_size> NUL bytes> -- up to 255 NUL bytes, used for alignment.
The fingerprints are N fingerprint fields, ordered sequentially.
<fp0: $storage_size bytes> -- the first fingerprint
<fp1: $storage_size bytes> -- the second fingerprint
   ...
The last fingerprint ends at the last byte of the arena chunk.

Each fingerprint contains:
<fingerprint: $num_bytes bytes> -- the actual fingerprint data
<extra: $storage_size-$num_bytes bytes> -- the 'extra' NULL padding bytes
    used so storage_size is a multiple of the alignment.

To get the number of fingerprints in the arena:
 (len(arena content) - 4 - 4 - 1 - $spacer_size) // $storage_size
 */
void extractArenaDetails(FPBReader_impl *dp_impl, boost::uint64_t sz) {
  PRECONDITION(dp_impl, "bad pointer");
  PRECONDITION(dp_impl->df_lazy, "should only be used in lazy mode");

  boost::uint32_t numBytesPerFingerprint;
  streamRead(*dp_impl->istrm, numBytesPerFingerprint);
  dp_impl->nBits = numBytesPerFingerprint * 8;

  boost::uint32_t numBytesStoredPerFingerprint;
  streamRead(*dp_impl->istrm, numBytesStoredPerFingerprint);
  dp_impl->numBytesStoredPerFingerprint = numBytesStoredPerFingerprint;
  boost::uint8_t spacer;
  streamRead(*dp_impl->istrm, spacer);
  dp_impl->len = (sz - 9 - spacer) / numBytesStoredPerFingerprint;

  // streamRead(*dp_impl->istrm, spacer);
  // now move forward the length of the spacer
  if (spacer)
    dp_impl->istrm->seekg(static_cast<std::streamoff>(spacer),
                          std::ios_base::cur);
  dp_impl->fpDataOffset = dp_impl->istrm->tellg();
  dp_impl->istrm->seekg(
      static_cast<std::streamoff>(numBytesStoredPerFingerprint * dp_impl->len),
      std::ios_base::cur);
}
Example #3
0
// NOTE: if the reaction passed in here already has reactants and products
// be left intact.
void ReactionPickler::reactionFromPickle(std::istream &ss,
                                         ChemicalReaction *rxn) {
  PRECONDITION(rxn, "empty reaction");
  Tags tag;
  int32_t tmpInt;

  streamRead(ss, tmpInt);
  if (tmpInt != endianId) {
    throw ReactionPicklerException(
        "Bad pickle format: bad endian ID or invalid file format");
  }

  streamRead(ss, tag);
  if (tag != VERSION) {
    throw ReactionPicklerException("Bad pickle format: no version tag");
  }
  int32_t majorVersion, minorVersion, patchVersion;
  streamRead(ss, majorVersion);
  streamRead(ss, minorVersion);
  streamRead(ss, patchVersion);
  if (majorVersion > versionMajor ||
      (majorVersion == versionMajor && minorVersion > versionMinor)) {
    BOOST_LOG(rdWarningLog) << "Depickling from a version number ("
                            << majorVersion << "." << minorVersion << ")"
                            << "that is higher than our version ("
                            << versionMajor << "." << versionMinor
                            << ").\nThis probably won't work." << std::endl;
  }
  majorVersion = 1000 * majorVersion + minorVersion * 10 + patchVersion;

  _depickle(ss, rxn, majorVersion);
}
Example #4
0
  void UniformGrid3D::initFromText(const char *pkl,const unsigned int length){
    std::stringstream ss(std::ios_base::binary|std::ios_base::in|std::ios_base::out);
    ss.write(pkl,length);
    boost::int32_t tVers;
    streamRead(ss,tVers);
    tVers *= -1;
    if(tVers==0x1){
      
    } else {
      throw ValueErrorException("bad version in UniformGrid3D pickle");
    }
    boost::uint32_t tInt;
    streamRead(ss,tInt);
    d_numX=tInt;
    streamRead(ss,tInt);
    d_numY=tInt;
    streamRead(ss,tInt);
    d_numZ=tInt;
    streamRead(ss,d_spacing);
    double oX,oY,oZ;
    streamRead(ss,oX);
    streamRead(ss,oY);
    streamRead(ss,oZ);
    d_offSet = Point3D(oX,oY,oZ);

    boost::uint32_t pklSz;
    streamRead(ss,pklSz);
    char *buff = new char[pklSz];
    ss.read(buff,pklSz*sizeof(char));
    if(dp_storage) delete dp_storage;
    dp_storage = new RDKit::DiscreteValueVect(buff,pklSz);
    delete [] buff;
  }
Example #5
0
/* Documentation from Andrew's code on the structure of the arena:

The actual layout inside of the chunk is:
 <num_4byte_elements: 4 bytes> -- the number of 4 byte offsets.
 <num_8byte_elements: 4 bytes> -- the number of 8 byte offsets
 Note: the number of indicies is num_4byte_elements + num_8byte_elements + 1
 because even with no elements there will be the initial '\0\0\0\0'.

 <id 0> + NUL   -- the first string, with an added NUL terminator
 <id 1> + NUL   -- the second string, with an added NUL terminator
     ....
 <id N> + NUL   -- the last string, with an added NUL terminator

 <offset 0: 4 bytes>    -- the offset relative to the start of <text 0>.
                  (This always contains the 4 bytes "\0\0\0\0")
    ...
 <offset num_4byte_elements: 4 bytes>   -- the last offset stored in 4 bytes

      (Note: This next section exists only when <num 8 byte offsets> > 0)
 <offset num_4byte_elements+1: 8 bytes>    -- the first offset stored in 8
bytes
    ...
 <offset num_4byte_elements+num_8byte_elements: 8 bytes>   -- the last offset
stored in 8 bytes

To get the identifier for record at position P >= 0:
  chunk_size = size of the chunk
  num_4byte_elements = decode bytes[0:4] as uint32
  num_8byte_elements = decode bytes[4:8] as uint32
  if P >= num_4byte_elements + num_8byte_elements:
      record does not exist
  offset_start = chunk_size - num_4byte_elements*4 - num_8byte_elements*8
  if P < num_4byte_elements:
    start, end = decode bytes[offset_start:offset_start+8] as (uint32, uint32)
  elif P == N4:
    start, end = decode bytes[offset_start:offset_start+12] as (uint32,
uint64)
  else:
    start, end = decode bytes[offset_start:offset_start+16] as (uint64,
uint64)
  id = bytes[start:end-1]
 */
void extractIdsDetails(FPBReader_impl *dp_impl, boost::uint64_t sz) {
  PRECONDITION(dp_impl, "bad pointer");
  std::streampos start = dp_impl->istrm->tellg();
  dp_impl->idChunkOffset = start;
  streamRead(*dp_impl->istrm, dp_impl->num4ByteElements);
  streamRead(*dp_impl->istrm, dp_impl->num8ByteElements);

  dp_impl->idDataOffset = static_cast<boost::uint64_t>(start) + sz -
                          (dp_impl->num4ByteElements + 1) * 4 -
                          dp_impl->num8ByteElements * 8;
  dp_impl->istrm->seekg(start + static_cast<std::streampos>(sz),
                        std::ios_base::beg);
};
Example #6
0
void readByteString(GsfInput* stream, char*& str, UT_uint16* aLength) 
    throw(UT_Error) 
{
	UT_uint16 length;
	str = NULL;
	streamRead(stream, length);
	str = new char[length + 1];
	if (length)
		streamRead(stream, str, length);
	str[length] = 0;
	if (aLength)
		*aLength = length;
}
Example #7
0
/**
 * Read block from backing ciphertext file, decrypt it (normal mode)
 * or
 * Read block from backing plaintext file, then encrypt it (reverse mode)
 */
ssize_t CipherFileIO::readOneBlock(const IORequest &req) const {
  int bs = blockSize();
  off_t blockNum = req.offset / bs;

  ssize_t readSize = 0;
  IORequest tmpReq = req;

  // adjust offset if we have a file header
  if (haveHeader && !fsConfig->reverseEncryption) {
    tmpReq.offset += HEADER_SIZE;
  }
  readSize = base->read(tmpReq);

  bool ok;
  if (readSize > 0) {
    if (haveHeader && fileIV == 0)
      const_cast<CipherFileIO *>(this)->initHeader();

    if (readSize != bs) {
      rDebug("streamRead(data, %d, IV)", (int)readSize);
      ok = streamRead(tmpReq.data, (int)readSize, blockNum ^ fileIV);
    } else {
      ok = blockRead(tmpReq.data, (int)readSize, blockNum ^ fileIV);
    }

    if (!ok) {
      rDebug("decodeBlock failed for block %" PRIi64 ", size %i", blockNum,
             (int)readSize);
      readSize = -1;
    }
  } else
    rDebug("readSize zero for offset %" PRIi64, req.offset);

  return readSize;
}
Example #8
0
/**
 * @brief   Reads a whole line from the input channel.
 * @note    Input chars are echoed on the same stream object with the
 *          following exceptions:
 *          - DEL and BS are echoed as BS-SPACE-BS.
 *          - CR is echoed as CR-LF.
 *          - 0x4 is echoed as "^D".
 *          - Other values below 0x20 are not echoed.
 *          .
 *
 * @param[in] chp       pointer to a @p BaseSequentialStream object
 * @param[in] line      pointer to the line buffer
 * @param[in] size      buffer maximum length
 * @return              The operation status.
 * @retval true         the channel was reset or CTRL-D pressed.
 * @retval false        operation successful.
 *
 * @api
 */
bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size) {
  char *p = line;

  while (true) {
    char c;

    if (streamRead(chp, (uint8_t *)&c, 1) == 0)
      return true;
    if (c == 4) {
      chprintf(chp, "^D");
      return true;
    }
    if ((c == 8) || (c == 127)) {
      if (p != line) {
        streamPut(chp, c);
        streamPut(chp, 0x20);
        streamPut(chp, c);
        p--;
      }
      continue;
    }
    if (c == '\r') {
      chprintf(chp, "\r\n");
      *p = 0;
      return false;
    }
    if (c < 0x20)
      continue;
    if (p < line + size - 1) {
      streamPut(chp, c);
      *p++ = (char)c;
    }
  }
}
Example #9
0
void SimPlanet::unpackUpdate(Net::GhostManager *, BitStream *stream)
{
   streamRead(*stream);
   stream->read(&planet.size);
   stream->read(&planet.distance);
   if (manager)
      load();
}
Example #10
0
// the caller is responsible for calling delete[] on `data`
void readChunkDetails(std::istream &istrm, std::string &nm,
                      boost::uint64_t &sz) {
  streamRead(istrm, sz);
  char tag[tagNameSize + 1];
  tag[tagNameSize] = 0;
  istrm.read(tag, tagNameSize);
  nm = tag;
}
Example #11
0
void TransformCatalogEntry::initFromStream(std::istream &ss) {
  // the reaction:
  dp_transform = new ChemicalReaction();
  ReactionPickler::reactionFromPickle(ss, *dp_transform);

  std::int32_t tmpInt;
  // the bitId:
  streamRead(ss, tmpInt);
  setBitId(tmpInt);

  // the description:
  streamRead(ss, tmpInt);
  char *tmpText = new char[tmpInt + 1];
  ss.read(tmpText, tmpInt * sizeof(char));
  tmpText[tmpInt] = 0;
  d_descrip = tmpText;
  delete[] tmpText;
}
Example #12
0
 void NodeDataInterface::streamToOutput(AbstractWriteFacet &write_to_storeentity) {
         static char readBuffer[4096];
         size_t bytesRead;
         openStream(); 
         while((bytesRead = streamRead(readBuffer, 4096)) > 0){
              write_to_storeentity(readBuffer, bytesRead);
         }
         closeStream();
 }
Example #13
0
void FragmentCatalogEntry::initFromStream(std::istream &ss) {
  // the molecule:
  dp_mol = new ROMol();
  MolPickler::molFromPickle(ss, *dp_mol);

  boost::int32_t tmpInt;
  // the bitId:
  streamRead(ss, tmpInt);
  setBitId(tmpInt);

  // the description:
  streamRead(ss, tmpInt);
  char *tmpText = new char[tmpInt + 1];
  ss.read(tmpText, tmpInt * sizeof(char));
  tmpText[tmpInt] = 0;
  d_descrip = tmpText;
  delete[] tmpText;
}
Example #14
0
static THD_FUNCTION(net_rx1_thd, arg) {
    if_list[1].arg = arg;
    serial_datagram_rcv_handler_init(&port1_rx, port1_rx_buf, sizeof(port1_rx_buf), serial_datagram_rx_cb, &port_1_if_idx);
    while (1) {
        uint8_t buf;
        size_t len = streamRead((BaseSequentialStream*)arg, &buf, 1);
        serial_datagram_receive(&port1_rx, &buf, len);
        if (len == 0) {
            chThdSleepMilliseconds(10); // queue is probably reset, avoid busy loop
        }
    }
}
Example #15
0
Persistent::Base::Error SimPlanet::read(StreamIO &sio, int version, int b)
{
   Persistent::Base::Error error = SimNetObject::read(sio, version, b);
   if (error != Persistent::Base::Ok) 
      return error;
   streamRead(sio);
   if (version == CurrentVersion)
   {
      sio.read(&planet.size);
      sio.read(&planet.distance);
   }
	return (sio.getStatus() == STRM_OK) ? Ok : ReadError;
}
Example #16
0
void InputStream::streamReadAll (VoidBuffer buf) {
	static const int step = 256;
	int nb = step;
	int offset = 0;
	buf.realloc(step);
	while (nb) {
		nb = streamRead(buf + offset);
		if (nb) {
			offset += nb;
			buf.grow (nb);
		}
	}
	buf.reduce(step);
}
Example #17
0
void IE_Imp_StarOffice::readRecSize(GsfInput* aStream, UT_uint32& aSize, gsf_off_t* aEOR) throw(UT_Error) {
	// Yes, that's correct, only 3 bytes.
	guint8 buf [3];
	aSize = 0;
	streamRead(aStream, buf, 3);
	// buf content is little endian.
	aSize = buf [0] | (buf[1] << 8) | (buf [2] << 16);
	aSize -= 4; // Substract 4 for the rec type + size
	if (aSize == 0xFFFFFF && mDocHdr.nVersion >= SWG_LONGRECS) {
		// XXX need recsizes from header, see above
		UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
	}
	if (aEOR)
		*aEOR = gsf_input_tell(aStream) + aSize;
}
Example #18
0
/**
 * @brief   Reads a whole line from the input channel.
 * @note    Input chars are echoed on the same stream object with the
 *          following exceptions:
 *          - DEL and BS are echoed as BS-SPACE-BS.
 *          - CR is echoed as CR-LF.
 *          - 0x4 is echoed as "^D".
 *          - Other values below 0x20 are not echoed.
 *          .
 *
 * @param[in] chp       pointer to a @p BaseSequentialStream object
 * @param[in] line      pointer to the line buffer
 * @param[in] size      buffer maximum length
 * @return              The operation status.
 * @retval true         the channel was reset or CTRL-D pressed.
 * @retval false        operation successful.
 *
 * @api
 */
bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size) {
  char *p = line;

  while (true) {
    char c;

    if (streamRead(chp, (uint8_t *)&c, 1) == 0)
      return true;
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
    if (c == 4) {
      chprintf(chp, "^D");
      return true;
    }
#endif
    if ((c == 8) || (c == 127)) {
      if (p != line) {
        streamPut(chp, c);
        streamPut(chp, 0x20);
        streamPut(chp, c);
        p--;
      }
      continue;
    }
    if (c == '\r') {
      chprintf(chp, "\r\n");
      *p = 0;
      return false;
    }
    if (c < 0x20)
      continue;
    if (p < line + size - 1) {
      streamPut(chp, c);
      *p++ = (char)c;
    }
  }
}
Example #19
0
void FragCatalogEntry::initFromStream(std::istream &ss) {
  // the molecule:
  dp_mol = new ROMol();
  MolPickler::molFromPickle(ss, *dp_mol);

  std::int32_t tmpInt;
  // the bitId:
  streamRead(ss, tmpInt);
  setBitId(tmpInt);

  // the description:
  streamRead(ss, tmpInt);
  auto *tmpText = new char[tmpInt + 1];
  ss.read(tmpText, tmpInt * sizeof(char));
  tmpText[tmpInt] = 0;
  d_descrip = tmpText;
  delete[] tmpText;

  streamRead(ss, tmpInt);
  d_order = tmpInt;

  // now the map:
  streamRead(ss, tmpInt);
  for (int i = 0; i < tmpInt; i++) {
    std::int32_t key, value, size;
    streamRead(ss, key);
    streamRead(ss, size);
    INT_VECT tmpVect;
    tmpVect.clear();
    for (int j = 0; j < size; j++) {
      streamRead(ss, value);
      tmpVect.push_back(value);
    }
    d_aToFmap[key] = tmpVect;
  }
}
Example #20
0
  void DiscreteValueVect::initFromText(const char *pkl,const unsigned int len){
    std::stringstream ss(std::ios_base::binary|std::ios_base::in|std::ios_base::out);
    ss.write(pkl,len);
    boost::int32_t tVers;
    streamRead(ss,tVers);
    tVers *= -1;
    if(tVers==0x1){
      
    } else {
      throw ValueErrorException("bad version in DiscreteValueVect pickle");
    }
    boost::uint32_t tInt;
    streamRead(ss,tInt);
    d_type=static_cast<DiscreteValueType>(tInt);

    
    streamRead(ss,tInt);
    d_bitsPerVal=tInt;
    d_valsPerInt = BITS_PER_INT/d_bitsPerVal;
    streamRead(ss,tInt);
    d_mask=tInt;
    streamRead(ss,tInt);
    d_length=tInt;
    streamRead(ss,tInt);
    d_numInts=tInt;
    boost::uint32_t *data = new boost::uint32_t[d_numInts];
    ss.read((char *)data,d_numInts*sizeof(boost::uint32_t));

#if defined(BOOST_BIG_ENDIAN)
    boost::uint32_t *td = new boost::uint32_t[d_numInts];
    for(unsigned int i=0;i<d_numInts;++i) td[i]=EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(data[i]);
    d_data.reset(td);
    delete [] data;
#else    
    d_data.reset(data);
#endif    

  };
Example #21
0
UT_Error IE_Imp_StarOffice::_loadFile(GsfInput * input)
{
	try {
		UT_DEBUGMSG(("SDW: Starting import\n"));
		mOle = GSF_INFILE (gsf_infile_msole_new(input, NULL));
		if (!mOle)
			return UT_IE_BOGUSDOCUMENT;

		// firstly, load metadata
		SDWDocInfo::load(mOle, getDoc());

		mDocStream = gsf_infile_child_by_name(mOle, "StarWriterDocument");
		if (!mDocStream)
			return UT_IE_BOGUSDOCUMENT;

		gsf_off_t size = gsf_input_size(mDocStream);

		if (!appendStrux(PTX_Section, PP_NOPROPS))
			return UT_IE_NOMEMORY;

		UT_DEBUGMSG(("SDW: Attempting to load DocHdr...\n"));
		mDocHdr.load(mDocStream);
		UT_DEBUGMSG(("SDW: ...success\n"));

		// Ask for and verify the password
		if (mDocHdr.cryptor) {
			if (!mDocHdr.cryptor->SetPassword(GetPassword().c_str())) {
				UT_DEBUGMSG(("SDW: Wrong password\n"));
				return UT_IE_PROTECTED;
			}
		}

		// do the actual reading
		char type;
		bool done = false;
		UT_uint32 recSize;
		while (!done) {
			if (gsf_input_tell(mDocStream) == size)
				break;
			readChar(mDocStream, type);
			gsf_off_t eor;
			readRecSize(mDocStream, recSize, &eor);

			switch (type) {
				case SWG_CONTENTS: {
					gsf_off_t flagsEnd = 0;
					UT_uint32 nNodes;
					// sw/source/core/sw3io/sw3sectn.cxx#L129
					if (mDocHdr.nVersion >= SWG_LAYFRAMES) {
						UT_uint8 flags;
						readFlagRec(mDocStream, flags, &flagsEnd);
					}
					if (mDocHdr.nVersion >= SWG_LONGIDX)
						streamRead(mDocStream, nNodes);
					else {
						if (mDocHdr.nVersion >= SWG_LAYFRAMES) {
							UT_uint16 sectidDummy;
							streamRead(mDocStream, sectidDummy);
						}
						UT_uint16 nodes16;
						streamRead(mDocStream, nodes16);
						nNodes = (UT_uint32)nodes16;
					}
					if (flagsEnd) {
						UT_ASSERT(flagsEnd >= gsf_input_tell(mDocStream));
						if (gsf_input_tell(mDocStream) != flagsEnd) {
							UT_DEBUGMSG(("SDW: have not read all flags\n"));
							if (gsf_input_seek(mDocStream, flagsEnd, G_SEEK_SET))
								return UT_IE_BOGUSDOCUMENT;
						}
					}
					bool done2 = false;
					UT_uint32 size2;
					while (!done2) {
						readChar(mDocStream, type);
						gsf_off_t eor2;
						readRecSize(mDocStream, size2, &eor2);

						switch (type) {
							case SWG_TEXTNODE: { // sw/source/core/sw3io/sw3nodes.cxx#L788
								UT_DEBUGMSG(("SDW: Found Textnode! (start at 0x%08llX end at 0x%08llX)\n", 
											 (long long)gsf_input_tell(mDocStream), 
											 (long long)eor2));
								UT_uint8 flags;
								gsf_off_t newPos;
								readFlagRec(mDocStream, flags, &newPos);
								// XXX check flags
								if (gsf_input_seek(mDocStream, newPos, G_SEEK_SET))
									return UT_IE_BOGUSDOCUMENT;

								// Read the actual text
								UT_UCS4Char* str;
								readByteString(mDocStream, str);
								UT_UCS4String textNode(str);
								free(str);
								UT_DEBUGMSG(("SDW: ...length=%zu contents are: |%s|\n", textNode.length(), textNode.utf8_str()));

								// now get the attributes
								UT_String attrs;
								UT_String pAttrs;
								UT_Vector charAttributes;
								while (gsf_input_tell(mDocStream) < eor2) {
									char attVal;
									streamRead(mDocStream, attVal);
									UT_uint32 attSize;
									gsf_off_t eoa; // end of attribute
									readRecSize(mDocStream, attSize, &eoa);
									if (attVal == SWG_ATTRIBUTE) {
										TextAttr* a = new TextAttr;
										streamRead(mDocStream, *a, eoa);
										UT_DEBUGMSG(("SDW: ...found text-sub-node, which=0x%x, ver=0x%x, start=%u, end=%u - data:%s len:%llu data is:",
													 a->which, a->ver, a->start,
													 a->end, a->data?"Yes":"No",
													 (long long unsigned)a->dataLen));
#ifdef DEBUG
										hexdump(a->data, a->dataLen);
                    putc('\n', stderr);
#endif
										charAttributes.addItem(a);
									}
									else if (attVal == SWG_ATTRSET) {
									  // bah, yet another loop
										UT_DEBUGMSG(("SDW: ...paragraph attributes found\n"));
										while (gsf_input_tell(mDocStream) < eoa) {
											// reusing attVal and attSize
											streamRead(mDocStream, attVal);
											gsf_off_t eoa2; // end of attribute
											readRecSize(mDocStream, attSize, &eoa2);
											if (attVal == SWG_ATTRIBUTE) {
												TextAttr a;
												streamRead(mDocStream, a, eoa2);
                        if (!a.attrVal.empty()) {
  												if (a.isPara)
	  												UT_String_setProperty(pAttrs, a.attrName, a.attrVal);
		  										else
			  										UT_String_setProperty(attrs, a.attrName, a.attrVal);
                        }
						UT_DEBUGMSG(("SDW: ......found paragraph attr, which=0x%x, ver=0x%x, start=%u, end=%u (string now %s) Data:%s Len=%lld Data:", a.which, a.ver, (a.startSet?a.start:0), (a.endSet?a.end:0), attrs.c_str(), (a.data ? "Yes" : "No"), (long long)a.dataLen));
#ifdef DEBUG
												hexdump(a.data, a.dataLen);
                        putc('\n', stderr);
#endif
											}
											if (gsf_input_seek(mDocStream, eoa2, G_SEEK_SET))
												return UT_IE_BOGUSDOCUMENT;
										}
									}
									else {
										UT_DEBUGMSG(("SDW: ...unknown attribute '%c' found (start=%" GSF_OFF_T_FORMAT " end=%" GSF_OFF_T_FORMAT ")\n", attVal, gsf_input_tell(mDocStream), eoa));
									}
									if (gsf_input_seek(mDocStream, eoa, G_SEEK_SET))
										return UT_IE_BOGUSDOCUMENT;
								}

								PP_PropertyVector attributes = {
									"props",
									pAttrs.c_str()
								};
								// first, insert the paragraph
								if (!appendStrux(PTX_Block, attributes))
									return UT_IE_NOMEMORY;

								UT_String pca(attrs); // character attributes for the whole paragraph
								// now insert the spans of text
								UT_uint32 len = textNode.length();
								UT_uint32 lastInsPos = 0;
								for (UT_uint32 i = 1; i < len; i++) {
									bool doInsert = false; // whether there was an attribute change
									for (UT_sint32 j = 0; j < charAttributes.getItemCount(); j++) {
										const TextAttr* a = reinterpret_cast<const TextAttr*>(charAttributes[j]);
										// clear the last attribute, if set
										if (a->endSet && a->end == (i - 1)) {
											if (a->isOff) {
												UT_String propval = UT_String_getPropVal(pca, a->attrName);
												UT_String_setProperty(attrs, a->attrName, propval);
											}
											else
												UT_String_removeProperty(attrs, a->attrName);
										}

										// now set new attribute, if needed
										if (a->startSet && a->start == (i - 1)) {
											if (a->isPara)
												UT_String_setProperty(pAttrs, a->attrName, a->attrVal);
											else if (a->isOff)
												UT_String_removeProperty(attrs, a->attrName);
											else
												UT_String_setProperty(attrs, a->attrName, a->attrVal);
										}

										// insert if this is the last character, or if there was a format change
										if ((a->endSet && a->end == i) || (a->startSet && a->start == i))
											doInsert = true;
									}
									if (doInsert || i == (len - 1)) {
										attributes[1] = attrs.c_str();
										UT_DEBUGMSG(("SDW: Going to appendFmt with %s\n", attributes[1].c_str()));
										if (!appendFmt(attributes))
											return UT_IE_NOMEMORY; /* leave cast alone! */
										UT_DEBUGMSG(("SDW: About to insert %u-%u\n", lastInsPos, i));
										size_t spanLen = i - lastInsPos;
										if (i == (len - 1)) spanLen++;
										UT_UCS4String span = textNode.substr(lastInsPos, spanLen);
										appendSpan(span.ucs4_str(), spanLen);
										lastInsPos = i;
									}
								}

								UT_VECTOR_PURGEALL(TextAttr*, charAttributes);
								break;

							}
							case SWG_JOBSETUP: {
								// flags are apparently unused here. no idea why they are there.
								gsf_off_t newpos;
								UT_uint8 flags;
								readFlagRec(mDocStream, flags, &newpos);
								if (gsf_input_seek(mDocStream, newpos, G_SEEK_SET))
									return UT_IE_BOGUSDOCUMENT;
								UT_uint16 len, system;
								streamRead(mDocStream, len);
								streamRead(mDocStream, system);
								char printerName[64];
								streamRead(mDocStream, printerName, 64);
								char deviceName[32], portName[32], driverName[32];
								streamRead(mDocStream, deviceName, 32);
								streamRead(mDocStream, portName, 32);
								streamRead(mDocStream, driverName, 32);
								UT_DEBUGMSG(("SDW: Jobsetup: len %u sys 0x%x printer |%.64s| device |%.32s| port |%.32s| driver |%.32s|\n", len, system, printerName, deviceName, portName, driverName));

								if (system == JOBSET_FILE364_SYSTEM || system == JOBSET_FILE605_SYSTEM) {
									UT_uint16 len2, system2;
									streamRead(mDocStream, len2);
									streamRead(mDocStream, system2);
									UT_uint32 ddl; // driver data length
									streamRead(mDocStream, ddl);
									// now the interesting data
									UT_uint16 orient; // 0=portrait 1=landscape
									streamRead(mDocStream, orient);
									UT_uint16 paperBin;
									streamRead(mDocStream, paperBin);
									UT_uint16 paperFormat;
									streamRead(mDocStream, paperFormat);
									UT_uint32 width, height;
									streamRead(mDocStream, width);
									streamRead(mDocStream, height);
									UT_DEBUGMSG(("SDW: orient %u bin %u format %u width %u height %u\n", orient, paperBin, paperFormat, width, height));
									// rest of the data is ignored, seems to be printer specific anyway.
									// Use A4, Portrait by default
									PP_PropertyVector attributes = {
										"pagetype", "a4", // A4/Letter/...
										"orientation", "portrait",
										"width", "210",
										"height", "297",
										"units", "mm"
									};
									const char* sdwPaperToAbi[] = {
										"A3",
										"A4",
										"A5",
										"B4",
										"B5",
										"Letter",
										"Legal",
										"Tabloid/Ledger",
										"Custom"
									};
									if (paperFormat < sizeof(sdwPaperToAbi)/sizeof(*sdwPaperToAbi)) {
										attributes[1] = sdwPaperToAbi[paperFormat];
                                    }
									const char* sdwOrientToAbi[] = {
										"portrait",
										"landscape"
									};
									if (orient < sizeof(sdwOrientToAbi)/sizeof(*sdwOrientToAbi)) {
										attributes[3] = sdwOrientToAbi[orient];
                                    }
									attributes[5] = UT_std_string_sprintf("%f", static_cast<double>(width)/100);
									attributes[7] = UT_std_string_sprintf("%f", static_cast<double>(height)/100);

									getDoc()->setPageSizeFromFile(attributes);
								}
								break;

							}
							case SWG_EOF:
								done2 = true;
								break;
							default:
								UT_DEBUGMSG(("SDW: SWG_CONTENT: Skipping %u bytes for record type '%c' (starting at 0x%08llX)\n",
											 size2, type,
											 (long long)gsf_input_tell(mDocStream)));
						}
						if (gsf_input_seek(mDocStream, eor2, G_SEEK_SET))
							return UT_IE_BOGUSDOCUMENT;
					}
					break;
				}
				case SWG_STRINGPOOL:
				{
					if (mDocHdr.nVersion <= SWG_POOLIDS) {
						UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
						break;
					}
					UT_uint8 encoding;
					streamRead(mDocStream, encoding);
					UT_iconv_t cd = findConverter(encoding);
					if (!UT_iconv_isValid(cd))
						throw UT_IE_IMPORTERROR;
					UT_uint16 count;
					streamRead(mDocStream, count);
					while (count--) {
						UT_uint16 id;
						streamRead(mDocStream, id);
						char* str;
						UT_uint16 len;
						::readByteString(mDocStream, str, &len);
						if (id == IDX_NOCONV_FF) {
							UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
						}
						// FIXME: find a way to not have to copy and free 
						// the result of UT_convert_cd.... --hub
						UT_DEBUGMSG(("SDW: StringPool: found 0x%04x <-> %.*s\n", id, len, str));
						UT_UCS4Char* convertedString = reinterpret_cast<UT_UCS4Char*>(UT_convert_cd(str, len + 1, cd, NULL, NULL));
						mStringPool.insert(stringpool_map::value_type(id, convertedString));
						FREEP(convertedString);
                        delete [] str;
					}
                    UT_iconv_close(cd);
					break;
				}
				case SWG_COMMENT: // skip over comments
					break;
				case SWG_EOF:
					done = true;
					break;
				default:
					UT_DEBUGMSG(("SDW: Skipping %u bytes for record type '%c' (starting at 0x%08llX)\n", recSize, type, (long long)gsf_input_tell(mDocStream)));
			}
			// Seek to the end of the record, in case it wasn't read completely
			if (gsf_input_seek(mDocStream, eor, G_SEEK_SET))
				return UT_IE_BOGUSDOCUMENT;
		}

		UT_DEBUGMSG(("SDW: Done\n"));

		return UT_OK;
	}
	catch(UT_Error e) {
		UT_DEBUGMSG(("SDW: error %d\n", e));
		return e;
	}
	catch(...) {
		UT_DEBUGMSG(("SDW: Unknown error\n"));
		return UT_IE_BOGUSDOCUMENT;
	}
}
Example #22
0
File: shell.c Project: mabl/ChibiOS
/**
 * @brief   Reads a whole line from the input channel.
 * @note    Input chars are echoed on the same stream object with the
 *          following exceptions:
 *          - DEL and BS are echoed as BS-SPACE-BS.
 *          - CR is echoed as CR-LF.
 *          - 0x4 is echoed as "^D".
 *          - Other values below 0x20 are not echoed.
 *          .
 *
 * @param[in] scfg      pointer to a @p ShellConfig object
 * @param[in] line      pointer to the line buffer
 * @param[in] size      buffer maximum length
 * @param[in] shp       pointer to a @p ShellHistory object or NULL
 * @return              The operation status.
 * @retval true         the channel was reset or CTRL-D pressed.
 * @retval false        operation successful.
 *
 * @api
 */
bool shellGetLine(ShellConfig *scfg, char *line, unsigned size, ShellHistory *shp) {
  char *p = line;
  BaseSequentialStream *chp = scfg->sc_channel;
#if SHELL_USE_ESC_SEQ == TRUE
  bool escape = false;
  bool bracket = false;
#endif

#if SHELL_USE_HISTORY != TRUE
  (void) shp;
#endif

  while (true) {
    char c;

    if (streamRead(chp, (uint8_t *)&c, 1) == 0)
      return true;
#if SHELL_USE_ESC_SEQ == TRUE
    if (c == 27) {
      escape = true;
      continue;
    }
    if (escape) {
      escape = false;
      if (c == '[') {
        escape = true;
        bracket = true;
        continue;
      }
      if (bracket) {
        bracket = false;
#if SHELL_USE_HISTORY == TRUE
        if (c == 'A') {
          int len = get_history(shp, line, SHELL_HIST_DIR_BK);

          if (len > 0) {
            _shell_reset_cur(chp);
            _shell_clr_line(chp);
            chprintf(chp, "%s", line);
            p = line + len;
          }
          continue;
        }
        if (c == 'B') {
          int len = get_history(shp, line, SHELL_HIST_DIR_FW);

          if (len == 0)
            *line = 0;

          if (len >= 0) {
            _shell_reset_cur(chp);
            _shell_clr_line(chp);
            chprintf(chp, "%s", line);
            p = line + len;
          }
          continue;
        }
#endif
      }
      continue;
    }
#endif
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
    if (c == 4) {
      chprintf(chp, "^D");
      return true;
    }
#endif
    if ((c == 8) || (c == 127)) {
      if (p != line) {
        streamPut(chp, 0x08);
        streamPut(chp, 0x20);
        streamPut(chp, 0x08);
        p--;
      }
      continue;
    }
    if (c == '\r') {
      chprintf(chp, SHELL_NEWLINE_STR);
#if SHELL_USE_HISTORY == TRUE
      save_history(shp, line, p - line);
#endif
      *p = 0;
      return false;
    }
#if SHELL_USE_COMPLETION == TRUE
    if (c == '\t') {
      if (p < line + size - 1) {
        *p = 0;

        get_completions(scfg, line);
        int len = process_completions(scfg, line, p - line, size);
        if (len > 0) {
          write_completions(scfg, line, p - line);
          p = line + len;
        }
      }
      continue;
    }
#endif
#if SHELL_USE_HISTORY == TRUE
    if (c == 14) {
      int len = get_history(shp, line, SHELL_HIST_DIR_FW);

      if (len == 0)
        *line = 0;

      if (len >= 0) {
        _shell_reset_cur(chp);
        _shell_clr_line(chp);
        chprintf(chp, "%s", line);
        p = line + len;
      }
      continue;
    }
    if (c == 16) {
      int len = get_history(shp, line, SHELL_HIST_DIR_BK);

      if (len > 0) {
        _shell_reset_cur(chp);
        _shell_clr_line(chp);
        chprintf(chp, "%s", line);
        p = line + len;
      }
      continue;
    }
#endif
    if (c < 0x20)
      continue;
    if (p < line + size - 1) {
      streamPut(chp, c);
      *p++ = (char)c;
    }
  }
}
Example #23
0
int InputStream::streamRead(VoidBuffer buffer) {
    return streamRead(buffer, buffer.size());
}
Example #24
0
 static inline T streamRead(InputStreamHandle &in)
 {
     T t;
     streamRead(in, t);
     return t;
 }
Example #25
0
 static inline void streamRead(InputStreamHandle &in, T *dst, size_t numElements)
 {
     streamRead(*in, dst, numElements);
 }
Example #26
0
 static inline void streamRead(InputStreamHandle &in, std::vector<T> &dst)
 {
     streamRead(*in, dst);
 }
Example #27
0
 static inline void streamRead(InputStreamHandle &in, T &dst)
 {
     streamRead(*in, dst);
 }
Example #28
0
void streamRead(GsfInput* aStream, TextAttr& aAttr, gsf_off_t aEoa)
    throw(UT_Error)
{
	UT_uint8 flags;
	gsf_off_t newPos;
	readFlagRec(aStream, flags, &newPos);

	streamRead(aStream, aAttr.which);
	streamRead(aStream, aAttr.ver);
	if (flags & 0x10) {
		aAttr.startSet = true;
		streamRead(aStream, aAttr.start);
	}
	else
		aAttr.startSet = false;

	if (flags & 0x20) {
		aAttr.endSet = true;
		streamRead(aStream, aAttr.end);
	}
	else
		aAttr.endSet = false;

	if (gsf_input_seek(aStream, newPos, G_SEEK_SET))
		throw UT_IE_BOGUSDOCUMENT;

	gsf_off_t curPos = gsf_input_tell(aStream);
	if (curPos != aEoa) {
		// there is data
		aAttr.dataLen = aEoa - curPos;
		aAttr.data = new UT_uint8[aAttr.dataLen];
		streamRead(aStream, aAttr.data, aAttr.dataLen);
	}
  // LIST OF THE VALUES: http://ooo.ximian.com/lxr/source/sw/sw/inc/hintids.hxx#086
  // together with http://ooo.ximian.com/lxr/source/sw/sw/source/core/sw3io/sw3fmts.cxx#172
	switch (aAttr.which) {
		case 0x1004: // strikethrough
			aAttr.attrName = "text-decoration";
			if (!aAttr.data || aAttr.data[0])
				aAttr.attrVal = "line-through";
			else
				aAttr.isOff = true;
			break;
		case 0x1005: { // sub-/superscript
			if (aAttr.dataLen < 3)
				break;
			// first byte is size of text % of normal size
			UT_sint16 height = GSF_LE_GET_GINT16(aAttr.data + 1);	
			aAttr.attrName = "text-position";
			if (height > 0)
				aAttr.attrVal = "superscript";
			else if (height < 0)
				aAttr.attrVal = "subscript";
			else
				aAttr.isOff = true;
			break; }
		case 0x1006: { // font family
			if (!aAttr.data || aAttr.dataLen < 7)
				// 7 = 3 byte family etc., 2 byte name length, 2 byte style length
				break;
			aAttr.attrName = "font-family";
			// XXX TODO This code here assumes that the font names are in latin1
			UT_uint16 fontLen = GSF_LE_GET_GUINT16(aAttr.data + 3);
			UT_String_sprintf(aAttr.attrVal, "%.*s", fontLen, (aAttr.data + 5));
			
		break; }
    case 0x1007: // font height
      // structure: | height (2 byte, twips) | prop (?) (2 byte) (if version >= 2, if ver=1 1 byte) | unit (if version>=2) |
      // XXX we ignore "prop" and unit for now, they seem not used much
      aAttr.attrName = "font-size";
      if (aAttr.data)
        aAttr.attrVal = twipsToSizeString(GSF_LE_GET_GUINT16(aAttr.data));
      break;
		case 0x100a: // Italic
			aAttr.attrName = "font-style";
			if (!aAttr.data || aAttr.data[0]) // if there is data, first byte must be != 0
				// abiword doesn't support oblique, so always set italic
				aAttr.attrVal = "italic";
			else
				aAttr.isOff = true;
			break;
		case 0x100d: // Underline
			aAttr.attrName = "text-decoration";
			if (!aAttr.data || aAttr.data[0])
				aAttr.attrVal = "underline";
			else
				aAttr.isOff = true;
			break;
		case 0x100e: // Bold
			aAttr.attrName = "font-weight";
			if (!aAttr.data || aAttr.data[0] >= 8) // 8=Bold.
				aAttr.attrVal = "bold";
			else
				aAttr.isOff = true;
			break;
    case 0x4000: // line spacing
      aAttr.attrName = "line-height";
      aAttr.isPara = true;
      // prop space (s8) | inter space (s16) | height (u16) | rule (s8) | interrule (s8)
      if (aAttr.data && aAttr.dataLen >= 7) {
        // Abiword wants it as float value, StarOffice saves as percentage (e.g.
        // 150 for 1.5)
        float proportionalLineSpace = float(aAttr.data[0])/100;
        // But maybe we need to use the height - stored as twips, need points
        // (used for "exact" and "minimum" line spacing)
        // XXX inter-line spacing not supported by abiword (would be rule=0x00
        // interrule=0x02, value to use=inter space, unit twips)
        UT_String lineHeight = twipsToSizeString(GSF_LE_GET_GINT16(aAttr.data + 3));

        // We'll turn the bytes at 5 and 6 into a single integer, for easier
        // evaluation
        switch (GSF_LE_GET_GUINT16(aAttr.data + 5)) {
          case 0x0100: // proportional
            aAttr.attrVal = std_size_string(proportionalLineSpace);
            break;
          case 0x0001:
          case 0x0002:
            aAttr.attrVal = lineHeight;
            if (aAttr.data[5] == 2) // "minimum" case
              aAttr.attrVal += '+';
            break;
          default:
            UT_DEBUGMSG(("Unsupported linespacing: %02x %02x\n", aAttr.data[5], aAttr.data[6]));
        }
      }
      break;
		case 0x4001: // Alignment
			aAttr.attrName = "text-align";
			aAttr.isPara = true;
			if (aAttr.data) {
				switch (aAttr.data[0]) {
					case 0:
						aAttr.attrVal = "left";
						break;
					case 1:
						aAttr.attrVal = "right";
						break;
					case 2:
					case 4: // BLOCKLINE!? what's BLOCKLINE? I'm guessing justify.
						aAttr.attrVal = "justify";
						break;
					case 3:
						aAttr.attrVal = "center";
						break;
				}
			}
      break;
    case 0x4005: {// Tabstops
      aAttr.attrName = "tabstops";
      aAttr.isPara = true;
      // Data structure:
      // Count(8) | Position (in twips) (32) | Adjustment (8) | Decimal Separator (?) (8) | Fill character (8)
      // (total size per tab = 7)
      // UT_sint8 count = aAttr.data[0];
      for (UT_uint32 i = 1; (i + 6) < aAttr.dataLen; i += 7) {
        // Abiword wants: 12.3cm/L0, where 0 indicates what to fill with
        UT_uint16 posInTwips = GSF_LE_GET_GUINT32(aAttr.data + i);
        UT_String pos = twipsToSizeString(posInTwips);

        aAttr.attrVal += pos;
        aAttr.attrVal += '/';
        if (aAttr.data[i + 4] < sizeof(sTabAlignment)/sizeof(*sTabAlignment))
          aAttr.attrVal += sTabAlignment[aAttr.data[i + 4]];
        else
          aAttr.attrVal += 'L'; // fallback

        char fillIndex = '0';
        // Fill character
        switch (aAttr.data[i + 6]) {
          case '.':
            fillIndex = '1';
            break;
          case '-':
            fillIndex = '2';
            break;
          case '_':
            fillIndex = '3';
            break;
          case ' ':
            fillIndex = '0';
            break;
          default:
            UT_DEBUGMSG(("Filling with '%c' is not supported\n", aAttr.data[i + 6]));
        }
        aAttr.attrVal += fillIndex;
        aAttr.attrVal += ',';
      }
      }
      break;
    default:
      UT_DEBUGMSG(("SDW: unknown attribute 0x%x, compressed %d\n", aAttr.which, lcl_sw3io__CompressWhich(aAttr.which)));
	}

}
Example #29
0
void DocHdr::load(GsfInput* stream) throw(UT_Error) 
{
	UT_DEBUGMSG(("SDW: entering DocHdr::load\n"));
	static const char sw3hdr[] = "SW3HDR";
	static const char sw4hdr[] = "SW4HDR";
	static const char sw5hdr[] = "SW5HDR";
	char header[7];
	streamRead(stream, header, 7);
	if (memcmp(header, sw3hdr, sizeof(sw3hdr)) != 0 &&
	    memcmp(header, sw4hdr, sizeof(sw4hdr)) != 0 &&
	    memcmp(header, sw5hdr, sizeof(sw5hdr)) != 0)
		throw UT_IE_BOGUSDOCUMENT;

	streamRead(stream, cLen);
	streamRead(stream, nVersion);
	streamRead(stream, nFileFlags);
	streamRead(stream, nDocFlags);
	streamRead(stream, nRecSzPos);
	streamRead(stream, nDummy);
	streamRead(stream, nDummy16);
	streamRead(stream, cRedlineMode);
	streamRead(stream, nCompatVer);

	UT_DEBUGMSG(("SDW: clen %i nversion %i fileflags %i docflags %i recszpos %i readlinemode %i compatver %i\n",
			cLen, nVersion, nFileFlags, nDocFlags, nRecSzPos, cRedlineMode, nCompatVer));

	// (see sw/source/core/sw3io/sw3doc.cxx line 700)
	if (nVersion >= SWG_MAJORVERSION && nCompatVer > 0) {
		// File is in a too new format
		throw UT_IE_BOGUSDOCUMENT;
	}
	streamRead(stream, cPasswd, 16);

	streamRead(stream, cSet);
	streamRead(stream, cGui);
	streamRead(stream, nDate);
	streamRead(stream, nTime);
	UT_DEBUGMSG(("SDW: nDate %u nTime %u\n", nDate, nTime));

	// Find the name of the used encoding
	converter = findConverter(cSet);
	if (!UT_iconv_isValid(converter))
		throw UT_ERROR;

	if (nFileFlags & SWGF_BLOCKNAME) {
		char buf[64];
		streamRead(stream, buf, 64);
		UT_DEBUGMSG(("SDW: BLOCKNAME: %.64s\n", buf));
		// XXX verify that the string is really null terminated
		sBlockName = reinterpret_cast<UT_UCS4Char*>(UT_convert_cd(buf, strlen(buf) + 1, converter, NULL, NULL));
	}

	if (nRecSzPos != 0 && nVersion >= SWG_RECSIZES) {
		// Read the Recsizes
		// XXX to be done see sw/source/core/sw3io/sw3imp.cxx#L1070
		UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
	}

	if (nFileFlags & SWGF_BAD_FILE)
		throw UT_IE_BOGUSDOCUMENT;

	if (nFileFlags & SWGF_HAS_PASSWD)
		cryptor = new SDWCryptor(nDate, nTime, cPasswd);
	else
		cryptor = NULL;

}
Example #30
0
void readFlagRec(GsfInput* stream, UT_uint8& flags, gsf_off_t* newPos) throw(UT_Error) 
{
	streamRead(stream, flags);
	if (newPos)
		*newPos = gsf_input_tell(stream) + (flags & 0xF);
}