/* ----------------------------------------------------------------------- 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; }