示例#1
0
static void spWriteFreeFlush(SecBuffer& sb, const ZStreamW& w)
{
    if (not sb.pvBuffer)
        return;

    if (not sb.cbBuffer)
    {
        spPSFT->FreeContextBuffer(sb.pvBuffer);
        sb.pvBuffer = nullptr;
    }
    else
    {
        size_t countWritten = 0;
        w.Write(sb.pvBuffer, sb.cbBuffer, &countWritten);

        const bool shortWrite = (countWritten != sb.cbBuffer);
        sb.cbBuffer = 0;

        spPSFT->FreeContextBuffer(sb.pvBuffer);
        sb.pvBuffer = nullptr;

        if (shortWrite)
            ZStreamW::sThrowEndOfStream();
        w.Flush();
    }
}
示例#2
0
void ZTBQueryNode_ID_Constant::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(5);
	sWriteCount(iStreamW, fIDs.size());
	for (vector<uint64>::iterator i = fIDs.begin(), theEnd = fIDs.end();
		i != theEnd; ++i)
		{
		iStreamW.WriteUInt64(*i);
		}
	}
示例#3
0
static inline void sWriteCount(const ZStreamW& iStreamW, uint32 iCount)
	{
	if (iCount < 0xFF)
		{
		iStreamW.WriteUInt8(iCount);
		}
	else
		{
		iStreamW.WriteUInt8(0xFF);
		iStreamW.WriteUInt32(iCount);
		}
	}
示例#4
0
static void sWriteString(const ZStreamW& iStreamW, const string& iString)
	{
	size_t theSize = iString.size();
	sWriteCount(iStreamW, theSize);
	if (theSize)
		iStreamW.Write(iString.data(), theSize);
	}
示例#5
0
static void sNodeToStream(const ZStreamW& iStreamW, const ZRef<ZTBQueryNode>& iNode)
	{
	if (iNode)
		iNode->ToStream(iStreamW);
	else
		iStreamW.WriteUInt8(0);
	}
示例#6
0
static void sWriteColorTable(const ZStreamW& iStream,
	const ZRGBColorPOD* iColors, size_t iCountAvailable, size_t iCountNeeded)
	{
	iCountAvailable = min(iCountAvailable, iCountNeeded);

	vector<uint8> tempColorsVector(iCountNeeded * 3);
	uint8* currentDest = &tempColorsVector[0];

	for (size_t x = 0; x < iCountAvailable; ++x)
		{
		*currentDest++ = iColors->red >> 8;
		*currentDest++ = iColors->green >> 8;
		*currentDest++ = iColors->blue >> 8;
		++iColors;
		}

	for (size_t x = iCountAvailable; x < iCountNeeded; ++x)
		{
		*currentDest++ = 0;
		*currentDest++ = 0;
		*currentDest++ = 0;
		}

	iStream.Write(&tempColorsVector[0], tempColorsVector.size());
	}
示例#7
0
bool ZBlackBerryServer::Handler_ManagerChanged::Write(const ZStreamW& w)
	{
	ZGuardRMtxR locker(fMutex);

	if (fState == eState_SendChanged)
		{
		fState = eState_Quiet;
		locker.Release();
		w.WriteBool(true);
		return true;
		}
	else if (fState == eState_SendClosed)
		{
		locker.Release();
		w.WriteBool(false);
		return false;
		}

	return true;
	}
示例#8
0
void ZTBQueryNode_Combo::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(2);

	sWriteCount(iStreamW, fSort.size());
	for (vector<ZTBQuery::SortSpec>::iterator i = fSort.begin(); i != fSort.end(); ++i)
		(*i).ToStream(iStreamW);

	sWriteCount(iStreamW, fIntersections.size());
	for (vector<Intersection>::iterator i = fIntersections.begin(); i != fIntersections.end(); ++i)
		(*i).ToStream(iStreamW);
	}
示例#9
0
/// \sa ZBlackBerry::Device_Client::Write
bool ZBlackBerryServer::Handler_DeviceFinished::Write(const ZStreamW& w)
	{
	if (!fOpen)
		{
		if (ZLOG(s, eDebug + 2, "ZBlackBerryServer::Handler_DeviceFinished"))
			s << "Write false, return false";

		w.WriteBool(false);
		return false;
		}

	if (ZLOG(s, eDebug + 2, "ZBlackBerryServer::Handler_DeviceFinished"))
		s << "Write nothing, return true";

	return true;
	}
示例#10
0
void ZStreamR_Memory::pCopyTo(const ZStreamW& iStreamW, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;
	while (iCount)
		{
		size_t countWritten;
		iStreamW.Write(fAddress, iCount, &countWritten);
		if (countWritten == 0)
			break;
		fAddress += countWritten;
		iCount -= countWritten;
		if (oCountRead)
			*oCountRead += countWritten;
		if (oCountWritten)
			*oCountWritten += countWritten;
		}
	}
示例#11
0
void ZStreamR_Source::pCopyTo(const ZStreamW& iStreamW, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;
	while (iCount)
		{
		uint64 countToWrite = min(iCount, uint64(fDataSize - fOffset));
		size_t countWritten;
		iStreamW.Write(fData + fOffset, countToWrite, &countWritten);
		if (countWritten == 0)
			break;
		fOffset = (fOffset + countWritten) % fDataSize;
		iCount -= countWritten;
		if (oCountRead)
			*oCountRead += countWritten;
		if (oCountWritten)
			*oCountWritten += countWritten;
		}
	}
示例#12
0
void ZStreamRPos_Memory::pCopyTo(const ZStreamW& iStreamW, uint64 iCount,
	uint64* oCountRead, uint64* oCountWritten)
	{
	if (oCountRead)
		*oCountRead = 0;
	if (oCountWritten)
		*oCountWritten = 0;
	while (iCount)
		{
		size_t countToWrite = ZStream::sClampedSize(iCount, fSize, fPosition);
		size_t countWritten;
		iStreamW.Write(fAddress + fPosition, countToWrite, &countWritten);
		if (countWritten == 0)
			break;
		fPosition += countWritten;
		iCount -= countWritten;
		if (oCountRead)
			*oCountRead += countWritten;
		if (oCountWritten)
			*oCountWritten += countWritten;
		}
	}
示例#13
0
void ZTBQueryNode_All::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(1);
	}
示例#14
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;
			}
		}
	}
示例#15
0
void ZTBQueryNode_ID_FromSource::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(6);
	fSourcePropName.ToStream(iStreamW);
	sNodeToStream(iStreamW, fSourceNode);
	}
示例#16
0
void ZTBSpec::Comparator::ToStream(const ZStreamW& iStreamW) const
	{
	iStreamW.WriteUInt8(fRel);
	iStreamW.WriteUInt8(fStrength);
	}
示例#17
0
void ZTBQueryNode_First::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(4);
	fPropName.ToStream(iStreamW);
	sNodeToStream(iStreamW, fSourceNode);
	}
示例#18
0
void ZTBQueryNode_Difference::ToStream(const ZStreamW& iStreamW)
	{
	iStreamW.WriteUInt8(3);
	sNodeToStream(iStreamW, fLeftNode);
	sNodeToStream(iStreamW, fRightNode);
	}
示例#19
0
static bool spWriteFully(const SecBuffer& sb, const ZStreamW& w)
{
    size_t countWritten;
    w.Write(sb.pvBuffer, sb.cbBuffer, &countWritten);
    return countWritten == sb.cbBuffer;
}
示例#20
0
void ZDCPixmapEncoder_GIF::Imp_Write(const ZStreamW& iStream,
	const void* iBaseAddress,
	const ZDCPixmapNS::RasterDesc& iRasterDesc,
	const ZDCPixmapNS::PixelDesc& iPixelDesc,
	const ZRect& iBounds)
	{
	ZRef<ZDCPixmapNS::PixelDescRep_Indexed> thePixelDescRep_Indexed
		= ZRefDynamicCast<ZDCPixmapNS::PixelDescRep_Indexed>(iPixelDesc.GetRep());
	ZAssertStop(2, thePixelDescRep_Indexed);

	if (fTransparent)
		iStream.WriteString("GIF89a");
	else
		iStream.WriteString("GIF87a");
	iStream.WriteUInt16LE(iBounds.Width());
	iStream.WriteUInt16LE(iBounds.Height());

	uint8 globalStrmFlags = 0;
	globalStrmFlags |= 0x80; // hasGlobalColorTable
	globalStrmFlags |= 0x70; // colorResolution (8 bits per component)
	// globalStrmFlags |= 0x08; // set this if the color table is sorted in priority order

	ZAssertStop(2, iRasterDesc.fPixvalDesc.fDepth > 0 && iRasterDesc.fPixvalDesc.fDepth <= 8);
	
	globalStrmFlags |= iRasterDesc.fPixvalDesc.fDepth - 1; // globalColorTableSize & depth
	iStream.WriteUInt8(globalStrmFlags);

	iStream.WriteUInt8(0); // backgroundColorIndex
	iStream.WriteUInt8(0); // Pixel aspect ratio -- 0 == none specified.

	const ZRGBColorPOD* theColors;
	size_t theColorsCount;
	thePixelDescRep_Indexed->GetColors(theColors, theColorsCount);
	sWriteColorTable(iStream, theColors, theColorsCount, 1 << iRasterDesc.fPixvalDesc.fDepth);

	if (fTransparent)
		{
		iStream.WriteUInt8('!'); // Extension block
		iStream.WriteUInt8(0xF9); // Graphic Control Extension
		iStream.WriteUInt8(4); // Block size
		// The next byte encodes four fields:
		// 3 bits, Reserved == 0
		// 3 bits, Disposal Method == none (0),
		// 1 bit, User Input Flag == none (0)
		// 1 bit, Transparent Color Flag = yes (1)
		iStream.WriteUInt8(1);
		iStream.WriteUInt16LE(0); // Delay time
		iStream.WriteUInt8(fTransparentPixval);
		iStream.WriteUInt8(0); // Block terminator
		}

	iStream.WriteUInt8(','); // Start of image
	iStream.WriteUInt16LE(0); // Origin h
	iStream.WriteUInt16LE(0); // Origin v
	iStream.WriteUInt16LE(iBounds.Width());
	iStream.WriteUInt16LE(iBounds.Height());

	uint8 localStrmFlags = 0;
//	localStrmFlags |= 0x80; // hasLocalColorTable
	if (fInterlace)
		localStrmFlags |= 0x40; // interlaced
//	localStrmFlags |= 0x20; // sorted
//	localStrmFlags |= 0x70; // localColorTableSize
	iStream.WriteUInt8(localStrmFlags);

	iStream.WriteUInt8(iRasterDesc.fPixvalDesc.fDepth); // Initial code size.

	{ // Scope theSC.
	StreamW_Chunk theSC(iStream);

	ZStreamW_LZWEncode* theSLZW = nil;
	ZStreamW_LZWEncodeNoPatent* theSLZWNP = nil;

	ZStreamW* theStream;
	if (fNoPatent)
		{
		theSLZWNP = new ZStreamW_LZWEncodeNoPatent(iRasterDesc.fPixvalDesc.fDepth, theSC);
		theStream = theSLZWNP;
		}
	else
		{
		theSLZW = new ZStreamW_LZWEncode(iRasterDesc.fPixvalDesc.fDepth, theSC);
		theStream = theSLZW;
		}

	ZDCPixmapNS::PixvalDesc destPixvalDesc(8, true);

	vector<uint8> theRowBufferVector(iBounds.Width());
	void* theRowBuffer = &theRowBufferVector[0];

	try
		{
		if (fInterlace)
			{
			for (int pass = 0; pass < 4; ++pass)
				{
				for (ZCoord currentY = iBounds.top + sInterlaceStart[pass];
					currentY < iBounds.bottom; currentY += sInterlaceIncrement[pass])
					{
					const void* sourceRowAddress
						= iRasterDesc.CalcRowAddress(iBaseAddress, currentY);

					ZDCPixmapNS::sBlitRowPixvals(
						sourceRowAddress, iRasterDesc.fPixvalDesc, iBounds.left,
						theRowBuffer, destPixvalDesc, 0,
						iBounds.Width());

					theStream->Write(theRowBuffer, iBounds.Width());
					}
				}
			}
		else
			{
			for (ZCoord currentY = iBounds.top; currentY < iBounds.bottom; ++currentY)
				{
				const void* sourceRowAddress
					= iRasterDesc.CalcRowAddress(iBaseAddress, currentY);

				ZDCPixmapNS::sBlitRowPixvals(
					sourceRowAddress, iRasterDesc.fPixvalDesc, iBounds.left,
					theRowBuffer, destPixvalDesc, 0,
					iBounds.Width());
				theStream->Write(theRowBuffer, iBounds.Width());
				}
			}
		}
	catch (...)
		{
		delete theSLZW;
		delete theSLZWNP;
		throw;
		}

	delete theSLZW;
	delete theSLZWNP;
	}

	iStream.WriteUInt8(';'); // Trailer.
	}
示例#21
0
void ZTBQuery::SortSpec::ToStream(const ZStreamW& iStreamW) const
	{
	fPropName.ToStream(iStreamW);
	iStreamW.WriteBool(fAscending);
	iStreamW.WriteUInt8(fStrength);
	}
示例#22
0
bool ZWebDAV::sHandle_GET(const ZTrail& iPrefix, ZNode iRoot, const ZStreamR&, const ZStreamW& iStreamW, const ZTuple& iHeader, const ZTrail& iTrail, const ZTuple& iParam)
	{
	ZNode theNode = iRoot.Trail(iTrail);

	ZHTTP::Response r;
	r.Set("date", sAsString_WebDAV(ZTime::sNow()));

	if (iHeader.Has("range"))
		{
		if (const ZLog::S& s = ZLog::S(ZLog::eInfo, "ZWebDAV"))
			s << "GET with range:\n" << iHeader;
		}

	if (theNode.Exists())
		{
		r.SetResult(200);
		if (theNode.CanHaveChildren())
			{
			r.Set("Content-Type", "text/html; charset=\"utf-8\"");
			r.Set("Transfer-Encoding", "chunked");
			r.Send(iStreamW);

			ZHTTP::StreamW_Chunked chunkedStream(iStreamW);
			ZStrimW_StreamUTF8 theStrimW(chunkedStream);
			ZStrimW_ML s(false, theStrimW);
			s.Begin("html");
				s.Begin("title");
					s << theNode.Name();
				s.End("title");
				s.Begin("body");
					for (ZNodeIter i = theNode; i; i.Advance())
						{
						s.Begin("p");
							s.Begin("a");
								if (i.Current().CanHaveChildren())
									{
									s.Attr("href", ZHTTP::sEncodeComponent(i.Current().Name()) + "/");
									s << i.Current().Name() << "/";
									}
								else
									{
									s.Attr("href", ZHTTP::sEncodeComponent(i.Current().Name()));
									s << i.Current().Name();
									}
							s.End("a");
						s.End("p");
						}
				s.End("body");
			
			s.End("html");
			}
		else if (ZRef<ZStreamerRPos> theStreamer = theNode.OpenRPos())
			{
			const ZStreamRPos& theStreamRPos = theStreamer->GetStreamRPos();
			uint64 sentSize = theStreamRPos.GetSize();

			if (ZTupleValue rangeParam = iHeader.GetValue("range"))
				{
				vector<pair<size_t, size_t> > ranges;
				if (!ZHTTP::sOrganizeRanges(sentSize, rangeParam, ranges))
					{
					iStreamW.WriteString("HTTP/1.1 406 Unsatisfiable range\r\n\r\n");
					return false;
					}
				r.SetResult(206, "Partial Content");

				r.Set("Content-Range", ZString::sFormat("bytes %d-%d/%d", ranges.front().first, ranges.front().second - 1, sentSize));

				theStreamRPos.SetPosition(ranges.front().first);
				sentSize = ranges.front().second - ranges.front().first;
				}
			else
				{
				r.SetResult(200);
				}
			
			string theMIMEType = "application/octet-stream";
			ZTupleValue theMIMEValue;
			if (theNode.GetProp("MIMEType", theMIMEValue))
				{
				string asString;
				if (theMIMEValue.GetString(asString))
					theMIMEType = asString;
				}
			r.Set("Content-Type", theMIMEType);

			ZTupleValue theValue;
			if (theNode.GetProp("lastModified", theValue))
				{
				if (ZTime theTime = theValue.GetTime())
					r.Set("Last-Modified", sAsString_WebDAV(theTime));
				}

			r.Set("Content-Transfer-Encoding", "binary");
			r.Set("Content-Length", ZString::sFromUInt64(sentSize));

			r.Send(iStreamW);

			iStreamW.CopyFrom(theStreamRPos, sentSize);
			}
		}
	else
		{
		r.SetResult(404);
		r.Send(iStreamW);
		r.Set("Content-Length", 0);
		}
	return true;
	}
示例#23
0
bool ZTSWatcherServerAsync::Write(const ZStreamW& iStreamW)
{
    bool wroteAnything = false;

    ZMutexLocker locker(fMutex);
    if (fSendClose)
    {
        iStreamW.WriteUInt8(eResp_Close);
        return false;
    }
    else if (fIDsNeeded)
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Write, sending IDs";
        const size_t countNeeded = fIDsNeeded;
        fIDsNeeded = 0;
        locker.Release();

        uint64 theBaseID;
        size_t countIssued;

        bool success = fTSWatcher->AllocateIDs(countNeeded, theBaseID, countIssued);

        if (!success)
        {
            iStreamW.WriteUInt8(eResp_Close);
            return false;
        }

        iStreamW.WriteUInt8(eResp_IDs);
        iStreamW.WriteUInt64(theBaseID);
        iStreamW.WriteCount(countIssued);

        wroteAnything = true;

        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Write, sent IDs";
    }
    else if (fSyncNeeded)
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Write, sync";
        fSyncNeeded = false;

        vector<uint64> removedIDs;
        vector<uint64> addedIDs;
        vector<int64> removedQueries;
        vector<ZTSWatcher::AddedQueryCombo> addedQueries;
        vector<uint64> writtenTupleIDs;
        vector<ZTuple> writtenTuples;

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

        locker.Release();

        vector<uint64> watcherAddedIDs;
        vector<uint64> changedTupleIDs;
        vector<ZTuple> changedTuples;
        map<int64, vector<uint64> > changedQueries;

        using ZUtil_STL::sFirstOrNil;
        bool success = fTSWatcher->Sync(
                           sFirstOrNil(removedIDs), removedIDs.size(),
                           sFirstOrNil(addedIDs), addedIDs.size(),
                           sFirstOrNil(removedQueries), removedQueries.size(),
                           sFirstOrNil(addedQueries), addedQueries.size(),
                           watcherAddedIDs,
                           changedTupleIDs, changedTuples,
                           sFirstOrNil(writtenTupleIDs), sFirstOrNil(writtenTuples), writtenTupleIDs.size(),
                           changedQueries);

        if (!success)
        {
            iStreamW.WriteUInt8(eResp_Close);
            return false;
        }

        iStreamW.WriteUInt8(eResp_SyncResults);

        iStreamW.WriteCount(watcherAddedIDs.size());
        for (vector<uint64>::const_iterator
                i = watcherAddedIDs.begin(), theEnd = watcherAddedIDs.end();
                i != theEnd; ++i)
        {
            iStreamW.WriteUInt64(*i);
        }

        iStreamW.WriteCount(changedTupleIDs.size());
        vector<ZTuple>::const_iterator iterCT = changedTuples.begin();
        for (vector<uint64>::const_iterator
                i = changedTupleIDs.begin(), theEnd = changedTupleIDs.end();
                i != theEnd; ++i, ++iterCT)
        {
            iStreamW.WriteUInt64(*i);
            (*iterCT).ToStream(iStreamW);
        }

        iStreamW.WriteCount(changedQueries.size());
        for (map<int64, vector<uint64> >::const_iterator
                i = changedQueries.begin(), theEnd = changedQueries.end();
                i != theEnd; ++i)
        {
            iStreamW.WriteInt64((*i).first);
            const vector<uint64>& theVector = (*i).second;
            iStreamW.WriteCount(theVector.size());
            for (vector<uint64>::const_iterator j = theVector.begin(); j != theVector.end(); ++j)
                iStreamW.WriteUInt64(*j);
        }

        wroteAnything = true;
    }
    else if (fCallbackNeeded)
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Write, callback";
        fCallbackNeeded = false;
        locker.Release();
        iStreamW.WriteUInt8(eResp_SyncSuggested);

        wroteAnything = true;
    }

    if (wroteAnything)
        ZStreamerWriter::Wake();
    else
        iStreamW.Flush();

    return true;
}