Ejemplo n.º 1
0
void PdfFileSpec::EmbeddFileFromMem( PdfObject* pStream, const unsigned char* data, ptrdiff_t size ) const
{
    PdfMemoryInputStream memstream(reinterpret_cast<const char*>(data),size);
    pStream->GetStream()->Set( &memstream );

    // Add additional information about the embedded file to the stream
    PdfDictionary params;
    params.AddKey( "Size", static_cast<pdf_int64>(size) );
    pStream->GetDictionary().AddKey("Params", params );
}
Ejemplo n.º 2
0
PutObjectOutcomeCallable s3be::put_one(libbruce::be::putblock_t &block)
{
    //std::cerr << "PUT " << block.id << std::endl;
    io::basic_array_source<char> memstream((char*)block.mem.ptr(), block.mem.size());

    std::shared_ptr<std::stringstream> ss = std::make_shared<std::stringstream>();

    io::filtering_ostream in;
    in.push(io::zlib_compressor());
    in.push(*ss);
    io::copy(memstream, in);

    PutObjectRequest request;
    request.SetBucket(m_bucket);
    request.SetKey(m_prefix + boost::lexical_cast<std::string>(block.id));
    request.SetContentType("application/octet-stream");
    request.SetBody(ss);
    request.SetContentLength(ss->str().size());

    return m_s3->PutObjectCallable(request);
}
Ejemplo n.º 3
0
//---------------------------------------------------------------------------
void tTVPSusiePicturePlugin::Load(void *callbackdata,
		tTVPGraphicSizeCallback sizecallback,
		tTVPGraphicScanLineCallback scanlinecallback,
		tTJSBinaryStream *src,
		tjs_int keyidx,
		tTVPGraphicLoadMode mode)
{
	bool bitmaplocked = false;
	HLOCAL bitmap = NULL;
	bool infolocked = false;
	HLOCAL info = NULL;

	// load source to memory
	tjs_uint64 size = src->GetSize();
	tjs_uint8 * source = new tjs_uint8[(tjs_int)size];

	try
	{
		src->ReadBuffer(source, static_cast<tjs_uint>(size) );

		// call GetPicture
		int r = GetPicture((LPSTR)source, (long)size, 0x01, &info, &bitmap,
			(FARPROC)ProgressCallback, 0);
		if((r&0xff) != 0) TVPThrowExceptionMessage(TVPSusiePluginError, ttstr(r));

		// setup bitmapinfoheader
		TVP_WIN_BITMAPINFOHEADER bi;
		memset(&bi, 0, sizeof(bi));
		BITMAPINFOHEADER *srcbi = (BITMAPINFOHEADER *)LocalLock(info);
		infolocked = true;
		tjs_int datasize = LocalSize(bitmap);
		void * data = (void*) LocalLock(bitmap);
		bitmaplocked = true;

		if(srcbi->biSize == 12)
		{
			// OS/2 bitmap header
			bi.biSize = srcbi->biSize;
			bi.biWidth = srcbi->biWidth;
			bi.biHeight = srcbi->biHeight;
			bi.biPlanes = srcbi->biPlanes;
			bi.biBitCount = srcbi->biBitCount;
			bi.biClrUsed = 0;
			bi.biCompression = BI_RGB;
		}
		else if(srcbi->biSize == 40)
		{
			// Windows bitmap header
			bi.biSize = srcbi->biSize;
			bi.biWidth = srcbi->biWidth;
			bi.biHeight = srcbi->biHeight;
			bi.biPlanes = srcbi->biPlanes;
			bi.biBitCount = srcbi->biBitCount;
			bi.biCompression = srcbi->biCompression;
			bi.biSizeImage = srcbi->biSizeImage;
			bi.biXPelsPerMeter = srcbi->biXPelsPerMeter;
			bi.biYPelsPerMeter = srcbi->biYPelsPerMeter;
			bi.biClrUsed = srcbi->biClrUsed;
			bi.biClrImportant = srcbi->biClrImportant;
		}
		else
		{
			// not supported bitmap format
			TVPThrowExceptionMessage(TVPImageLoadError,
				TJS_W("Non-supported bitmap header was given from susie plug-in."));

		}

		// create reference memory stream for bitmap pixel data
		tTVPMemoryStream memstream(data, datasize);

		if(bi.biClrUsed == 0 && bi.biBitCount <= 8)
			bi.biClrUsed = 1 << bi.biBitCount;

		// pass information to TVPInternalLoadBMP
		TVPInternalLoadBMP(callbackdata, sizecallback, scanlinecallback,
			bi, ((tjs_uint8*)srcbi) + bi.biSize, &memstream, keyidx, AlphaType,
				mode);

	}
	catch(...)
	{
		delete [] source;
		if(bitmaplocked) LocalUnlock(bitmap);
		if(bitmap) LocalFree(bitmap);
		if(infolocked) LocalUnlock(info);
		if(info) LocalFree(info);
		throw;
	}

	delete [] source;
	if(bitmaplocked) LocalUnlock(bitmap);
	if(bitmap) LocalFree(bitmap);
	if(infolocked) LocalUnlock(info);
	if(info) LocalFree(info);
}
void
Messenger::onDataReceived(
	::net::IStream::TId streamId,
	const unsigned char* buf,
	size_t bufSize)
{
	util::ScopedLock lock(&s_sync);

	try
	{
		// Append data to corresponding data buffer

		TStreamData::iterator dd = m_streamData.find(streamId);
		if (dd == m_streamData.end())
		{
			dd = m_streamData.insert(std::make_pair(streamId, TData(0))).first;
		}

		TData& data = dd->second;

		// Required data length
		size_t dataSize = data.size();
		size_t requiredLen = dataSize + bufSize;
		size_t availableLen = data.capacity();

#ifndef NDEBUG
		{
			char buf2[128];
			memset(buf2, 0, sizeof(buf2));
			sprintf(buf2, "%p data before: ", reinterpret_cast<void*>(streamId));
			OutputDebugStringA(buf2);

			if (0 < data.size())
				dumpBinBuffer(&data[0], data.size());
			else
				OutputDebugStringA("<empty>\n");
		}
#endif // !NDEBUG

		// Resize data buffer if it is not large enough
		if (availableLen < requiredLen)
		{
			// Reserve 20% more
			size_t reserve = static_cast<size_t>(requiredLen * 1.2);

			if (reserve < KMSG_INITIAL_DATA_BUF_SIZE)
				reserve = KMSG_INITIAL_DATA_BUF_SIZE;

			data.reserve(reserve);
		}

		data.resize(requiredLen);

		// Copy data to buffer
		memcpy(&data[dataSize], buf, bufSize);

		// Try to extract message from data
		while (sizeof(MessageHeader) <= (dataSize = data.size()))
		{
			MessageHeader header;
			memset(&header, 0, sizeof(header));

			assert(data.size() == dataSize);

			unsigned char* pData = &data[0];
			memcpy(&header, pData, sizeof(header));

			util::T_UI4 messageSize = header.payloadSize + sizeof(MessageHeader); // two bytes at the beginning are message type and payload length
			if (messageSize <= dataSize)
			{
				TMessagePtr message;

				try
				{
					if (!m_messageFactory)
					{
						assert(!"Message factory must be set before receiving messages");
						throw std::logic_error("Internal error");
					}

					chkptr(m_messageFactory);
					message = m_messageFactory->createMessage(header.messageType);

					{
						util::ScopedLock lock(&m_memStreamSync);
						util::MemoryStream memstream(pData + sizeof(MessageHeader), pData + messageSize);
						message->load(memstream);
					}

					// Remove message bytes from data
					{
						TData::iterator bb = data.begin();
						std::advance(bb, messageSize);

#ifndef NDEBUG
						size_t dataSizeBefore = data.size();
						assert(dataSizeBefore >= messageSize);
#endif // !NDEBUG

						data.erase(data.begin(), bb);

#ifndef NDEBUG
						size_t dataSizeAfter = data.size();
						assert(dataSizeAfter + messageSize == dataSizeBefore);
						assert(dd->second.size() == dataSizeAfter);

						{
							char buf2[128];
							memset(buf2, 0, sizeof(buf2));
							sprintf(buf2, "%p data after: ", reinterpret_cast<void*>(streamId));
							OutputDebugStringA(buf2);

							if (0 < data.size())
								dumpBinBuffer(&data[0], data.size());
							else
								OutputDebugStringA("<empty>\n");
						}
#endif // !NDEBUG
					}

					TMessengerDelegates::iterator ii = m_messengerDelegates.find(streamId);
					if (ii == m_messengerDelegates.end())
					{
						assert(0);
						throw util::Error("Stream not found");
					}

					IMessengerDelegate* delegate_ = ii->second;
					chkptr(delegate_);

					delegate_->onMessageReceived(streamId, message);
				}
				catch (const std::exception& x)
				{
#ifndef NDEBUG
					const char* szMsg = x.what();
#endif
					// Unknown messages and message handling errors are simple discarded
					ignore_unused(x);
					assert(!"Unknown message type or message handling error");
				}
				catch (...)
				{
					// Unknown messages and message handling errors are simple discarded
					assert(!"Unknown message type or message handling error");
				}
			}
			else
			{
				// Not enough data
				break;
			}
		}
	}
	catch (const std::exception& x)
	{
		// Some error (probably bad_alloc) occured while processing stream data.
		// In this case stream is assumed to be in incosistent state and thus is not used any more.
		net::StreamListener::instance().closeStream(streamId, x.what());
	}
	catch (...)
	{
		// Some unknown error occured while processing stream data.
		// In this case stream is assumed to be in incosistent state and thus is not used any more.
		net::StreamListener::instance().closeStream(streamId, "Unknown error");
	}
}