Esempio n. 1
0
void ZStreamWPos_Memory::pCopyFrom(const ZStreamR& iStreamR, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;
	while (iCount)
		{
		size_t countToRead = ZStream::sClampedSize(iCount, fCapacity, fPosition);
		size_t countRead;
		iStreamR.Read(fAddress + fPosition, countToRead, &countRead);
		if (countRead == 0)
			break;

		fPosition += countRead;
		if (fSize < fPosition)
			fSize = fPosition;
		iCount -= countRead;

		if (oCountRead)
			*oCountRead += countRead;
		if (oCountWritten)
			*oCountWritten += countRead;
		}
	}
Esempio n. 2
0
static void sReadString(const ZStreamR& iStreamR, string& oString)
	{
	uint32 theCount = sReadCount(iStreamR);
	oString.resize(theCount);
	if (theCount)
		iStreamR.Read(const_cast<char*>(oString.data()), theCount);
	}
Esempio n. 3
0
static void sReadRLE8(const ZStreamR& iStream,
	ZCoord iWidth, ZCoord iHeight, size_t iRowBytes, bool iFlip, uint8* iBuffer)
	{
	ZCoord currentRow = 0;
	ZCoord currentCol = 0;
	bool done = false;
	while (!done)
		{
		uint8 count = iStream.ReadUInt8();
		uint8 command = iStream.ReadUInt8();
		if (count == 0)
			{
			switch (command)
				{
				case 0: // Move to start of next row
					{
					currentRow += 1;
					currentCol = 0;
					break;
					}
				case 1: // All done
					{
					done = true;
					break;
					}
				case 2: // Offset by some relative amount
					{
					currentCol += iStream.ReadUInt8();
					currentRow += iStream.ReadUInt8();
					break;
					}
				default: // Absolute data follows -- the length is the value of 'command'
					{
					uint8* destAddress = iBuffer
						+ iRowBytes * (iFlip ? iHeight - currentRow - 1 : currentRow)
						+ currentCol;

					iStream.Read(destAddress, command);
					currentCol += command;
					// An odd number of bytes is followed by a pad byte.
					if ((command & 1) != 0)
						iStream.Skip(1);
					break;
					}
				}
			}
		else
			{
			// Store a run of bytes. The count is in 'count', the value is in 'command'.
			uint8* destAddress = iBuffer
				+ iRowBytes * (iFlip ? iHeight - currentRow - 1 : currentRow) + currentCol;

			for (int x = 0; x < count; ++x)
				*destAddress++ = command;
			currentCol += count;
			}
		}
	}
Esempio n. 4
0
static bool spReadMore(vector<char>& ioBuf, const ZStreamR& r)
{
    const size_t priorSize = ioBuf.size();
    const size_t newSize = priorSize + 4096;
    ioBuf.resize(newSize);
    size_t countRead;
    r.Read(&ioBuf[priorSize], newSize - priorSize, &countRead);
    ioBuf.resize(priorSize + countRead);
    return countRead != 0;
}
Esempio n. 5
0
static void sReadColorTable(const ZStreamR& iStream,
	size_t iCount, vector<ZRGBColorPOD>& oColorTable)
	{
	oColorTable.resize(iCount);

	vector<uint8> readColorTable(iCount * 3);
	iStream.Read(&readColorTable[0], readColorTable.size());

	const uint8* readColor = &readColorTable[0];
	ZRGBColorPOD* oColor = &oColorTable[0];
	for (size_t x = 0; x < iCount; ++x)
		{
		oColor->red = (*readColor++) * 0x101;
		oColor->green = (*readColor++) * 0x101;
		oColor->blue = (*readColor++) * 0x101;
		oColor->alpha = 0xFFFFU;
		++oColor;
		}
	}
Esempio n. 6
0
void ZStreamW_Memory::pCopyFrom(const ZStreamR& iStreamR, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;
	while (iCount)
		{
		size_t countRead;
		iStreamR.Read(fAddress, iCount, &countRead);
		if (countRead == 0)
			break;

		fAddress += countRead;
		iCount -= countRead;

		if (oCountRead)
			*oCountRead += countRead;
		if (oCountWritten)
			*oCountWritten += countRead;
		}
	}
Esempio n. 7
0
/**
Copy data from \a iStreamR to \a iStreamW by reading it into a buffer and writing
from that buffer. If more than 8K is to be copied we try to allocate an 8K buffer
in the heap. If less than 8K is to be copied, or the heap buffer could not be allocated,
we use a 1K buffer on the stack.
*/
void sCopyReadToWrite(const ZStreamR& iStreamR, const ZStreamW& iStreamW, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;

	if (iCount == 0)
		return;

	if (iCount > 8192)
		{
		// Try to allocate and use an 8K heap-based buffer.
		if (uint8* heapBuffer = new(nothrow) uint8[8192])
			{
			try
				{
				uint64 countRemaining = iCount;
				while (countRemaining > 0)
					{
					size_t countRead;
					iStreamR.Read(heapBuffer, min(countRemaining, uint64(8192)), &countRead);
					if (countRead == 0)
						break;
					if (oCountRead)
						*oCountRead += countRead;
					countRemaining -= countRead;
					uint8* tempSource = heapBuffer;
					while (countRead > 0)
						{
						size_t countWritten;
						iStreamW.Write(tempSource, countRead, &countWritten);
						if (countWritten == 0)
							{
							countRemaining = 0;
							break;
							}
						tempSource += countWritten;
						countRead -= countWritten;
						if (oCountWritten)
							*oCountWritten += countWritten;
						}
					}
				}
			catch (...)
				{
				delete[] heapBuffer;
				throw;
				}
			delete[] heapBuffer;
			return;
			}
		}

	// We'll get to here if we need to move 8192 bytes or less, or if
	// allocation of the heap buffer failed.

	// Use a stack-based 1024 byte buffer if we're moving less than 8K and thus will iterate
	// fewer than 8 times. Previously we'd unconditionally used one of size 4096, but that's
	// fairly large and can contribute to blowing the stack on MacOS.
	uint8 localBuffer[1024];
	uint64 countRemaining = iCount;
	while (countRemaining > 0)
		{
		size_t countRead;
		iStreamR.Read(localBuffer, min(countRemaining, uint64(sizeof(localBuffer))), &countRead);
		if (countRead == 0)
			break;
		if (oCountRead)
			*oCountRead += countRead;
		countRemaining -= countRead;
		uint8* tempSource = localBuffer;
		while (countRead > 0)
			{
			size_t countWritten;
			iStreamW.Write(tempSource, countRead, &countWritten);
			if (countWritten == 0)
				{
				countRemaining = 0;
				break;
				}
			tempSource += countWritten;
			countRead -= countWritten;
			if (oCountWritten)
				*oCountWritten += countWritten;
			}
		}
	}
Esempio n. 8
0
static void sUnpackFromStream(const ZStreamR& inStream,
	ZCoord inSourceWidth, ZCoord inSourceHeight,
	unsigned short inSourceRowBytes, short inSourcePixelSize,
	const ZRGBColorPOD* inSourceColors, size_t inSourceColorTableSize,
	void* inDestBaseAddress,
	const ZDCPixmapNS::RasterDesc& inDestRasterDesc,
	const ZDCPixmapNS::PixelDesc& inDestPixelDesc)
	{
	// We're only supporting indexed pixels right now
	ZAssert(inSourcePixelSize == 1 || inSourcePixelSize == 2
		|| inSourcePixelSize == 4 || inSourcePixelSize == 8);

	ZDCPixmapNS::PixelDesc sourcePixelDesc(inSourceColors, inSourceColorTableSize);
	ZDCPixmapNS::PixvalDesc sourcePixvalDesc(inSourcePixelSize, true);

	if (inSourceRowBytes < 8)
		{
		// ah-ha!  The bits aren't actually packed. This will be easy.
		uint8 lineBuffer[8];
		for (ZCoord theScanLine = 0; theScanLine < inSourceHeight; ++theScanLine)
			{
			inStream.Read(lineBuffer, inSourceRowBytes);
			void* destRowAddress = inDestRasterDesc.CalcRowAddress(inDestBaseAddress, theScanLine);
			ZDCPixmapNS::sBlitRow(lineBuffer, sourcePixvalDesc, sourcePixelDesc, 0,
				destRowAddress, inDestRasterDesc.fPixvalDesc, inDestPixelDesc, 0,
				inSourceWidth);
			}
		}
	else
		{
		// Sometimes we get rows with length > rowBytes.  Allocate some extra for slop.
		// Also note that the data is stored as multiples of rowBytes, which may represent more
		// pixels than inSourceWidth (if inSourceWidth % 4 != 0). So we have to check for
		// destinationH < inSourceWidth before we actually blit to the destination.
		vector<uint8> lineBufferVector(inSourceRowBytes + 80);
		uint8* lineBuffer = &lineBufferVector[0];
		ZDCPixmapNS::PixvalAccessor destAccessor(inDestRasterDesc.fPixvalDesc);
		for (ZCoord theScanLine = 0; theScanLine < inSourceHeight; ++theScanLine)
			{
			void* destRowAddress = inDestRasterDesc.CalcRowAddress(inDestBaseAddress, theScanLine);

			size_t lineLen;
			if (inSourceRowBytes > 250 || inSourcePixelSize > 8)
				lineLen = inStream.ReadUInt16();
			else
				lineLen = inStream.ReadUInt8();
			if (lineLen > inSourceRowBytes + 80)
				::sThrowBadFormat();
			inStream.Read(lineBuffer, lineLen);

			ZCoord destinationH = 0;
			for (size_t j = 0; j < lineLen; /*no increment*/)
				{
				if (lineBuffer[j] & 0x80)
					{
					size_t repeatCount = (lineBuffer[j++] ^ 0xFF) + 2;
					ZRGBColorPOD theRGBColor;
					sourcePixelDesc.AsRGBColor(lineBuffer[j++], theRGBColor);
					uint32 destPixval = inDestPixelDesc.AsPixval(theRGBColor);
					for (size_t k = 0; k < repeatCount; ++k)
						{
						if (destinationH < inSourceWidth)
							destAccessor.SetPixval(destRowAddress, destinationH, destPixval);
						destinationH += 1;
						}
					}
				else
					{
					size_t realLength = lineBuffer[j] + 1;
					if (inSourceWidth > destinationH)
						{
						size_t countToCopy = min(realLength, size_t(inSourceWidth - destinationH));
						ZDCPixmapNS::sBlitRow(lineBuffer,
							sourcePixvalDesc, sourcePixelDesc, j + 1,
							destRowAddress,
							inDestRasterDesc.fPixvalDesc, inDestPixelDesc, destinationH,
							countToCopy);
						destinationH += countToCopy;
						}
					j += realLength + 1;
					}
				}
			}
		}
	}
bool ZTSWatcherServerAsync::Read(const ZStreamR& iStreamR)
{
    if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
        s << "Read, start";

    EReq theReq = (EReq)iStreamR.ReadUInt8();

    switch (theReq)
    {
    case eReq_Close:
    {
        ZMutexLocker locker(fMutex);
        fSendClose = true;
        locker.Release();
        ZStreamerWriter::Wake();
        return false;
    }
    case eReq_IDs:
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Read, eReq_IDs";
        const size_t theIDsNeeded = iStreamR.ReadCount();
        ZMutexLocker locker(fMutex);
        fIDsNeeded += theIDsNeeded;
        locker.Release();
        ZStreamerWriter::Wake();
        break;
    }
    case eReq_Sync:
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Read, eReq_Sync";
        vector<uint64> removedIDs;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            removedIDs.reserve(theCount);
            while (theCount--)
                removedIDs.push_back(iStreamR.ReadUInt64());
        }

        vector<uint64> addedIDs;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            addedIDs.reserve(theCount);
            while (theCount--)
                addedIDs.push_back(iStreamR.ReadUInt64());
        }

        vector<int64> removedQueries;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            removedQueries.reserve(theCount);
            while (theCount--)
                removedQueries.push_back(iStreamR.ReadInt64());
        }

        vector<ZTSWatcher::AddedQueryCombo> addedQueries;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            addedQueries.reserve(theCount);
            while (theCount--)
            {
                const int64 theRefcon = iStreamR.ReadInt64();
                const bool thePrefetch = iStreamR.ReadBool();
                const size_t theSize = iStreamR.ReadCount();

                ZTSWatcher::AddedQueryCombo theCombo(theSize);
                theCombo.fRefcon = theRefcon;
                theCombo.fPrefetch = thePrefetch;

                iStreamR.Read(theCombo.fMemoryBlock.GetPtrMutable(), theSize);

                addedQueries.push_back(theCombo);
            }
        }

        vector<uint64> writtenTupleIDs;
        vector<ZTuple> writtenTuples;
        bool writeNeededSort = false;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            writtenTupleIDs.reserve(theCount);
            writtenTuples.reserve(theCount);
            uint64 priorID = 0;
            while (theCount--)
            {
                const uint64 currentID = iStreamR.ReadUInt64();
                if (priorID >= currentID)
                    writeNeededSort = true;
                priorID = currentID;

                writtenTupleIDs.push_back(currentID);
                writtenTuples.push_back(ZTuple(iStreamR));
            }

            if (writeNeededSort)
                spSort(writtenTupleIDs, writtenTuples);
        }

        ZMutexLocker locker(fMutex);

        ZAssert(fRemovedIDs.empty());
        ZAssert(fAddedIDs.empty());
        ZAssert(fRemovedQueries.empty());
        ZAssert(fAddedQueries.empty());
        ZAssert(fWrittenTupleIDs.empty());
        ZAssert(fWrittenTuples.empty());
        ZAssert(!fSyncNeeded);

        fRemovedIDs.swap(removedIDs);
        fAddedIDs.swap(addedIDs);
        fRemovedQueries.swap(removedQueries);
        fAddedQueries.swap(addedQueries);
        fWrittenTupleIDs.swap(writtenTupleIDs);
        fWrittenTuples.swap(writtenTuples);

        fSyncNeeded = true;

        locker.Release();
        ZStreamerWriter::Wake();
        break;
    }
    }
    return true;
}