int32 CFile::Write(CMemoryStream& oStream, int32 nWriteSize) { if(nWriteSize <= 0) return 0; uint32 nRest = oStream.GetSize() - oStream.GetPosition(); if(nRest < (uint32)nWriteSize) nWriteSize = nRest; int32 nRet, nOldCopySize = nWriteSize; while(nWriteSize) { uint32 nBlockSize; char* pBuf = oStream.GetBuf(nBlockSize); if(nBlockSize > (uint32)nWriteSize) nBlockSize = nWriteSize; nRet = Write(pBuf, nBlockSize); if(nRet < 0) { if(nWriteSize == nOldCopySize) return nRet; break; } if(nRet == 0) break; nWriteSize -= nRet; oStream.Seek(nRet); } return nOldCopySize - nWriteSize; }
void _ms_writestring2(const v8::FunctionCallbackInfo<v8::Value>& args) { CMemoryStream* pNative = unwrap_memorystream(args.This()); v8::String::Value data(args[0]); pNative->WriteString2((wchar_t*)*data, data.length()); args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent())); }
uint32 CRdbTableAccess::WriteRedoForUpdate(uint32 nMaxRollBackRecordQuantity) { if(m_pTabDef->m_pBaseAttr->nStorage == RDB_MEMORY_TABLE) return 0; char sRedo[256], *pRedo = sRedo; const char* sLogName = m_pTabDef->GetLogName(); const char* sTableName = (const char*)m_pTabDef->m_pBaseAttr->sTableName; *(uint32*)pRedo = RDO_UPDATE; pRedo += sizeof(uint32); CString::StringCopy(pRedo, sLogName); pRedo += CString::StringLength(sLogName) + 1; CString::StringCopy(pRedo, sTableName); pRedo += CString::StringLength(sTableName) + 1; CMemoryStream oStream; oStream.SetLocalCode(true); if(!oStream.Write(sRedo, pRedo-sRedo)) return 1; if(!oStream.Write(nMaxRollBackRecordQuantity)) return 1; if(!m_oIdxAttr.Write(oStream)) return 1; if(!m_oSetAttr.Write(oStream)) return 1; return g_pRedo->WriteRedoStream(&oStream); }
TEST(Matrices,SerializeCMatrixD) { CMatrixDouble A(3,2, dat_A); CMatrixFixedNumeric<double,3,2> fA; CMatrixD As = A; CMemoryStream membuf; membuf << As; membuf.Seek(0); membuf >> fA; EXPECT_NEAR(0,fabs((CMatrixDouble(fA) - A).sum()), 1e-9); try { // Now, if we try to de-serialize into the wrong type, we should get an exception: membuf.Seek(0); CMatrixFixedNumeric<double,2,2> fB; membuf >> fB; // Wrong size! GTEST_FAIL() << "Exception not launched when it was expected!"; } catch(...) { // OK, exception occurred, as expected } }
/* ----------------------------------------------------------------------- ObjectToRawString ----------------------------------------------------------------------- */ void utils::ObjectToRawString(const CSerializable *o, std::string & out_vector) { try { CMemoryStream tmp; tmp.WriteObject(o); size_t N = tmp.getTotalBytesCount(); out_vector.resize(N); if (N) { os::memcpy( &out_vector[0],N,tmp.getRawBufferData(), N ); } } catch (std::bad_alloc &e) { throw e; } catch(std::exception &e) { std::cerr << "[ObjectToOctetVector] Exception: " << e.what() << std::endl; } catch(...) { std::cerr << "[ObjectToOctetVector] Unknown exception" << std::endl; } }
void CMUBmd::BmdSkeleton::BmdBone::load(CMemoryStream& s, const std::vector<BmdAnim>& setBmdAnim) { s.read(bEmpty); if (bEmpty) { return; } s.read((unsigned char*)szName,32); s.read(nParent); for (size_t i=0; i<setBmdAnim.size();++i) { for (size_t j=0; j<setBmdAnim[i].uFrameCount;++j) { Vec3D vTrans; s.read(vTrans); setTrans.push_back(vTrans); } for (size_t j=0; j<setBmdAnim[i].uFrameCount;++j) { Vec3D vRotate; s.read(vRotate); setRotate.push_back(vRotate); } } }
// Create a set of classes, then serialize and deserialize to test possible // bugs: TEST(NavTests, Serialization_WriteReadToMem) { for (size_t i = 0; i < sizeof(lstClasses) / sizeof(lstClasses[0]); i++) { try { CMemoryStream buf; { CSerializable* o = static_cast<CSerializable*>(lstClasses[i]->createObject()); buf << *o; delete o; } CSerializable::Ptr recons; buf.Seek(0); buf >> recons; } catch (std::exception& e) { GTEST_FAIL() << "Exception during serialization test for class '" << lstClasses[i]->className << "':\n" << e.what() << endl; } } }
// Create a set of classes, then serialize and deserialize to test possible // bugs: TEST(SerializeTestOpenGL, WriteReadToMem) { const mrpt::utils::TRuntimeClassId* lstClasses[] = { CLASS_ID(CAxis), CLASS_ID(CBox), CLASS_ID(CFrustum), CLASS_ID(CDisk), CLASS_ID(CGridPlaneXY), #if MRPT_HAS_OPENCV // These classes need CImage serialization CLASS_ID(CMesh), CLASS_ID(CTexturedPlane), #endif CLASS_ID(COpenGLViewport), CLASS_ID(CPointCloud), CLASS_ID(CPointCloudColoured), CLASS_ID(CSetOfLines), CLASS_ID(CSetOfTriangles), CLASS_ID(CSphere), CLASS_ID(CCylinder), CLASS_ID(CGeneralizedCylinder), CLASS_ID(CPolyhedron), CLASS_ID(CArrow), CLASS_ID(CCamera), CLASS_ID(CEllipsoid), CLASS_ID(CGridPlaneXZ), CLASS_ID(COpenGLScene), CLASS_ID(CSetOfObjects), CLASS_ID(CSimpleLine), CLASS_ID(CText), CLASS_ID(CText3D), CLASS_ID(CEllipsoidInverseDepth2D), CLASS_ID(CEllipsoidInverseDepth3D), CLASS_ID(CEllipsoidRangeBearing2D), CLASS_ID(COctoMapVoxels) }; for (size_t i = 0; i < sizeof(lstClasses) / sizeof(lstClasses[0]); i++) { try { CMemoryStream buf; { CSerializable* o = static_cast<CSerializable*>(lstClasses[i]->createObject()); buf << *o; delete o; } CSerializable::Ptr recons; buf.Seek(0); buf >> recons; } catch (std::exception& e) { GTEST_FAIL() << "Exception during serialization test for class '" << lstClasses[i]->className << "':\n" << e.what() << endl; } } }
// IStream members STDMETHODIMP CMemoryStream::Clone(IStream **ppstm) { TraceEnter(); if (NULL == ppstm) { return E_INVALIDARG; } CMemoryStream *pMemoryStream = new CMemoryStream(m_pbBuffer, m_nSize); if (NULL == pMemoryStream) { return E_INVALIDARG; } LARGE_INTEGER dlibMove; dlibMove.QuadPart = m_nPos; DWORD dwOrigin = STREAM_SEEK_SET; ULARGE_INTEGER dlibNewPosition; HRESULT hr = pMemoryStream->Seek(dlibMove, dwOrigin, &dlibNewPosition); if (FAILED(hr)) { pMemoryStream->Release(); return hr; } else { *ppstm = (IStream *)pMemoryStream; return S_OK; } }
void _ms_write_long(const v8::FunctionCallbackInfo<v8::Value>& args) { CMemoryStream* pNative = unwrap_memorystream(args.This()); LONG arg = (LONG)args[0]->Int32Value(); pNative->WriteLONG(arg); args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent())); }
void _ms_write_double(const v8::FunctionCallbackInfo<v8::Value>& args) { CMemoryStream* pNative = unwrap_memorystream(args.This()); double arg = (double)args[0]->NumberValue(); pNative->WriteLONG((LONG)(arg * 100000)); args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent())); }
void CMUBmd::BmdSkeleton::BmdAnim::load(CMemoryStream& s) { s.read(uFrameCount); s.read(bOffset); if (bOffset) { s.readVector(vOffset,uFrameCount); } }
void _ms_write_bool(const v8::FunctionCallbackInfo<v8::Value>& args) { CMemoryStream* pNative = unwrap_memorystream(args.This()); BYTE arg = (BYTE)args[0]->BooleanValue(); pNative->WriteBYTE(arg ? 1 : 0); args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent())); }
void _ms_copy(const v8::FunctionCallbackInfo<v8::Value>& args) { CMemoryStream* pNative = unwrap_memorystream(args.This()); CMemoryStream* pNative2 = unwrap_memorystream(args[0]->ToObject()); size_t pos = (size_t)args[1]->Uint32Value(); size_t len = (size_t)args[2]->Uint32Value(); pNative->Copy(pNative2, pos, len); args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent())); }
/* ----------------------------------------------------------------------- Used to pass CORBA-like object into a MRPT object. See doc about "Integration with BABEL". ----------------------------------------------------------------------- */ void utils::StringToObject(const std::string &str, CSerializablePtr &obj) { MRPT_START obj.clear_unique(); if (str.empty()) return; CMemoryStream tmp; size_t n; size_t i,lastIdx; obj.clear_unique(); n = str.size(); // Scan the string to decode it: // ---------------------------------- lastIdx = 0; const char *data = str.c_str(); unsigned char c; for (i=0;i<n && (c=data[i])!=0;i++) { // Search for first "0x01" byte: if ( c == 0x01 ) { // Copy all till now: tmp.WriteBuffer( &data[lastIdx], i - lastIdx + 1 ); i+=1; // +1 from "for" loop lastIdx = i+1; // And decode: // 0x01 0x01 --> 0x01 // 0x01 0x02 --> 0x00 if (data[i]==0x01) ((unsigned char*)tmp.getRawBufferData())[tmp.getTotalBytesCount()-1] = (unsigned char)0x01; else ((unsigned char*)tmp.getRawBufferData())[tmp.getTotalBytesCount()-1] = (unsigned char)0x00; } } // end for i // Copy the rest: if ( (n-lastIdx) > 0) tmp.WriteBuffer( &data[lastIdx], n - lastIdx ); // And the '\0' char: char dummy = '\0'; tmp.WriteBuffer( &dummy, sizeof(char) ); tmp.Seek(0,CStream::sFromBeginning); obj = tmp.ReadObject(); MRPT_END }
vector<CHttpClientResponse*> GZipUnZipTcpStream(unsigned char* pszData, unsigned int nLen) { vector<CHttpClientResponse*> ret; int nPackSize = 0; int nDelta = 0;//目前读取的长度 while (nDelta + 4 < nLen) { //读取头长度 unsigned char chPackSizes[4] = {0}; memcpy(chPackSizes, pszData, 4); pszData += 4; nDelta += 4; //获取头长度 nPackSize = *((int*)&chPackSizes[0]); if (nLen - nDelta < nPackSize) { break;; } unsigned char* pszBuffer = new unsigned char[nPackSize]; memset(pszBuffer, 0, nPackSize); memcpy(pszBuffer, pszData, nPackSize); pszData += nPackSize; nDelta += nPackSize; int nLen = 0; unsigned char* pszOut = NULL; if (pszBuffer[0] == 0x1f && pszBuffer[1] == 0x8b && pszBuffer[2] == 0x08 && pszBuffer[3] == 0x00) { unsigned char* pszOut; int nLen = ZipUtils::ccInflateMemory((unsigned char*)pszBuffer, nPackSize, (unsigned char**)&pszOut); if (nLen > 0) { CHttpClientResponse* pResp = new CHttpClientResponse(); CMemoryStream* pMem = new CMemoryStream(); pResp->SetTarget(pMem); pMem->WriteBuffer((char*)pszOut, nLen); ret.push_back(pResp); delete []pszOut; } } else { CHttpClientResponse* pResp = new CHttpClientResponse(); CMemoryStream* pMem = new CMemoryStream(); pResp->SetTarget(pMem); pMem->WriteBuffer((char*)pszBuffer, nPackSize); ret.push_back(pResp); } delete []pszBuffer; } return ret; }
// Serialize and deserialize complex STL types TEST(SerializeTestBase, STL_serialization) { try { // std::vector<> { CMemoryStream buf; std::vector<double> a,b; a.resize(30); for (size_t i=0;i<a.size();i++) a[i]=50-i; buf << a; buf.Seek(0); buf >> b; EXPECT_TRUE(a==b); } // std::list<...> { CMemoryStream buf; std::list<std::map<double,std::set<std::string> > > a,b; // Fill with random: mrpt::random::CRandomGenerator rng; const size_t N = rng.drawUniform(10,30); for (size_t i=0;i<N;i++) { std::map<double,std::set<std::string> > d; const size_t M = rng.drawUniform(4,9); for (size_t j=0;j<M;j++) { std::set<std::string> & dd = d[ rng.drawGaussian1D_normalized() ]; const size_t L = rng.drawUniform(2,15); for (size_t k=0;k<L;k++) dd.insert(mrpt::format("%f", rng.drawGaussian1D_normalized() )); } a.push_back(d); } buf << a; buf.Seek(0); buf >> b; EXPECT_TRUE(a==b); } } catch(std::exception &e) { GTEST_FAIL() << "Exception:\n" << e.what() << endl; } }
static bool UnPackFieldDefine(CMemoryStream & oStream, CRdbFieldDef& oDefine) { uint32 nLen; if(!oStream.Read(nLen)) return false; oDefine.sFieldName = new char[nLen+1]; if(!oStream.Read(oDefine.sFieldName, nLen+1)) return false; if(!oStream.Read(nLen)) return false; if(nLen) { oDefine.sDefault = new char[nLen+1]; if(!oStream.Read(oDefine.sDefault, nLen+1)) return false; } if(!oStream.Read(oDefine.nType)) return false; if(!oStream.Read(oDefine.nLen)) return false; if(!oStream.Read(oDefine.bNotNull)) return false; if(!oStream.Read(oDefine.nRecSize)) return false; return true; }
static bool PackFieldDefine(CMemoryStream & oStream, CRdbFieldDef& oDefine) { uint32 nLen = CString::StringLength(oDefine.sFieldName); if(!oStream.Write(nLen)) return false; if(!oStream.Write(oDefine.sFieldName, nLen+1)) return false; if(!oDefine.sDefault) nLen = 0; else nLen = CString::StringLength(oDefine.sDefault); if(!oStream.Write(nLen)) return false; if(nLen && !oStream.Write(oDefine.sDefault, nLen+1)) return false; if(!oStream.Write(oDefine.nType)) return false; if(!oStream.Write(oDefine.nLen)) return false; if(!oStream.Write(oDefine.bNotNull)) return false; if(!oStream.Write(oDefine.nRecSize)) return false; return true; }
// Create a set of classes, then serialize and deserialize to test possible bugs: TEST(SerializeTestObs, WriteReadToMem) { const mrpt::utils::TRuntimeClassId* lstClasses[] = { // Observations: CLASS_ID(CObservation2DRangeScan), CLASS_ID(CObservation3DRangeScan), CLASS_ID(CObservationBearingRange), CLASS_ID(CObservationBatteryState), CLASS_ID(CObservationWirelessPower), CLASS_ID(CObservationRFID), CLASS_ID(CObservationBeaconRanges), CLASS_ID(CObservationComment), CLASS_ID(CObservationGasSensors), CLASS_ID(CObservationGPS), CLASS_ID(CObservationImage), CLASS_ID(CObservationReflectivity), CLASS_ID(CObservationIMU), CLASS_ID(CObservationOdometry), CLASS_ID(CObservationRange), CLASS_ID(CObservationStereoImages), CLASS_ID(CObservationCANBusJ1939), CLASS_ID(CObservationRawDAQ), // Actions: CLASS_ID(CActionRobotMovement2D), CLASS_ID(CActionRobotMovement3D) }; for (size_t i=0;i<sizeof(lstClasses)/sizeof(lstClasses[0]);i++) { try { CMemoryStream buf; { CSerializable* o = static_cast<CSerializable*>(lstClasses[i]->createObject()); buf << *o; delete o; } CSerializablePtr recons; buf.Seek(0); buf >> recons; } catch(std::exception &e) { GTEST_FAIL() << "Exception during serialization test for class '"<< lstClasses[i]->className <<"':\n" << e.what() << endl; } } }
void CImageDict::LoadRaw(const BYTE* pBuffer, unsigned int unSize, unsigned int unWidth, unsigned int unHeight) { CMemoryStream* pStream = new CMemoryStream(unSize); if (!pStream) return; pStream->Write(pBuffer, unSize); SetStream(m_pXref, pStream); Add("Type", "XObject"); Add("Subtype", "Image"); Add("ColorSpace", "DeviceRGB"); Add("Width", unWidth); Add("Height", unHeight); Add("BitsPerComponent", 8); SetFilter(STREAM_FILTER_FLATE_DECODE); }
//---------------------------------------------------------------------------------------------------- CMouseEventResult UIViewCreatorDataSource::dbOnMouseMoved (const CPoint& where, const CButtonState& buttons, int32_t row, int32_t column, CDataBrowser* browser) { if (mouseDownRow >= 0 && buttons.isLeftButton ()) { SharedPointer<UISelection> selection = createSelection (); CMemoryStream stream (1024, 1024, false); if (selection->store (stream, description)) { stream.end (); CDropSource* dropSource = new CDropSource (stream.getBuffer (), static_cast<uint32_t> (stream.tell ()), CDropSource::kText); browser->doDrag (dropSource); dropSource->forget (); } mouseDownRow = -1; } return kMouseEventNotHandled; }
int32 CFile::Read(CMemoryStream& oStream, int32 nReadSize, uint32 nTimeOut) { if(nReadSize <= 0) return 0; if(oStream.GetSize() == oStream.GetPosition()) { if(!oStream.ExtendSize(nReadSize)) return 0; } uint32 nBlockSize; char* pBuf = oStream.GetBuf(nBlockSize); if(nBlockSize > (uint32)nReadSize) nBlockSize = nReadSize; int32 nRet = Read(pBuf, nBlockSize, nTimeOut);//??????? if(nRet > 0) oStream.Seek(nRet); return nRet; }
bool PackTableDefine(CMemoryStream & oStream, CRdbTableDef& oDefine) { uint32 nLen = CString::StringLength(oDefine.sTableName); if(!oStream.Write(nLen)) return false; if(!oStream.Write(oDefine.sTableName, nLen+1)) return false; if(!oStream.Write(oDefine.nFieldCount)) return false; for(uint32 i=0; i<oDefine.nFieldCount; ++i) { if(!PackFieldDefine(oStream, oDefine.pFieldDefines[i])) return false; } if(!oStream.Write(oDefine.nStorage)) return false; return true; }
void CImageDict::LoadRaw(const BYTE* pBgra, unsigned int unWidth, unsigned int unHeight) { CMemoryStream* pStream = new CMemoryStream(3 * unWidth * unHeight); if (!pStream) return; for (unsigned int unIndex = 0, unSize = 4 * unWidth * unHeight; unIndex < unSize; unIndex += 4) { pStream->Write(pBgra + unIndex, 3); } SetStream(m_pXref, pStream); Add("Type", "XObject"); Add("Subtype", "Image"); Add("ColorSpace", "DeviceRGB"); Add("Width", unWidth); Add("Height", unHeight); Add("BitsPerComponent", 8); SetFilter(STREAM_FILTER_FLATE_DECODE); }
// Create a set of classes, then serialize and deserialize to test possible // bugs: TEST(SerializeTestMaps, WriteReadToMem) { const mrpt::utils::TRuntimeClassId* lstClasses[] = { CLASS_ID(CBeacon), CLASS_ID(CBeaconMap), CLASS_ID(CColouredPointsMap), CLASS_ID(CGasConcentrationGridMap2D), CLASS_ID(CWirelessPowerGridMap2D), CLASS_ID(CHeightGridMap2D), CLASS_ID(CReflectivityGridMap2D), CLASS_ID(COccupancyGridMap2D), CLASS_ID(CSimplePointsMap), CLASS_ID(CRandomFieldGridMap3D), CLASS_ID(CWeightedPointsMap), CLASS_ID(COctoMap), CLASS_ID(CColouredOctoMap)}; for (size_t i = 0; i < sizeof(lstClasses) / sizeof(lstClasses[0]); i++) { try { CMemoryStream buf; { CSerializable* o = static_cast<CSerializable*>(lstClasses[i]->createObject()); buf << *o; delete o; } CSerializable::Ptr recons; buf.Seek(0); buf >> recons; } catch (std::exception& e) { GTEST_FAIL() << "Exception during serialization test for class '" << lstClasses[i]->className << "':\n" << e.what() << endl; } } }
// Create a set of classes, then serialize and deserialize to test possible bugs: TEST(SerializeTestBase, CArray) { try { CMemoryStream buf; CArrayDouble<5> a, b; for (CArrayDouble<5>::Index i=0;i<a.size();i++) a[i] = i+10; buf << a; buf.Seek(0); buf >> b; EXPECT_TRUE(a==b); } catch(std::exception &e) { GTEST_FAIL() << "Exception:\n" << e.what() << endl; } }
void CImageDict::LoadJpeg(BYTE* pBuffer, int nBufferSize, unsigned int unWidth, unsigned int unHeight, bool bGrayScale) { CMemoryStream* pStream = new CMemoryStream(); if (!pStream) return; pStream->Write(pBuffer, nBufferSize); SetStream(m_pXref, pStream); Add("Type", "XObject"); Add("Subtype", "Image"); Add("Height", unHeight); Add("Width", unWidth); if (bGrayScale) Add("ColorSpace", "DeviceGray"); else Add("ColorSpace", "DeviceRGB"); Add("BitsPerComponent", 8); SetFilter(STREAM_FILTER_DCT_DECODE); }
void CImageDict::LoadSMask(const BYTE* pBuffer, unsigned int unSize, unsigned int unWidth, unsigned int unHeight) { CMemoryStream* pStream = new CMemoryStream(unSize); if (!pStream) return; pStream->Write(pBuffer, unSize); CImageDict* pImageSMask = new CImageDict(m_pXref, m_pDocument); if (!pImageSMask) return; pImageSMask->SetStream(m_pXref, pStream); pImageSMask->Add("Type", "XObject"); pImageSMask->Add("Subtype", "Image"); pImageSMask->Add("ColorSpace", "DeviceGray"); pImageSMask->Add("Width", unWidth); pImageSMask->Add("Height", unHeight); pImageSMask->Add("BitsPerComponent", 8); pImageSMask->SetFilter(STREAM_FILTER_FLATE_DECODE); Add("SMask", pImageSMask); }
bool CMUBmd::LoadFile(const std::string& strFilename) { IOReadBase* pRead = IOReadBase::autoOpen(strFilename); if (!pRead) { return false; } size_t uFileSize = pRead->GetSize(); CMemoryStream s; s.resize(uFileSize); pRead->Read(s.getBuffer(),uFileSize); IOReadBase::autoClose(pRead); unsigned long uTag; s.read(uTag); if (0x0a444d42==uTag)//BMD. { } else if (0x0c444d42==uTag)//BMD. { unsigned long uEncodeSize; s.read(uEncodeSize); if (uEncodeSize!=uFileSize-8) { return false; } decryptMuBuffer(s.getPt(), uFileSize-8); } else { return false; } s.read(head); assert(head.uSubCount<50); setBmdSub.resize(head.uSubCount); for (size_t i=0; i<setBmdSub.size(); ++i) { setBmdSub[i].load(s); } bmdSkeleton.load(s, head.uBoneCount,head.uAnimCount); for (size_t i=0;i<bmdSkeleton.setBmdBone.size();++i) { bmdSkeleton.calcLocalMatrix(i); } nFrameCount = 0; for (size_t i=0; i<head.uAnimCount; ++i) { nFrameCount+=bmdSkeleton.setBmdAnim[i].uFrameCount; } return true; }