Пример #1
0
string BinaryNinja::UnescapeString(const string& s)
{
	DataBuffer buffer = DataBuffer::FromEscapedString(s);
	return string((const char*)buffer.GetData(), buffer.GetLength());
}
Пример #2
0
string BinaryReader::ReadString(size_t len)
{
	DataBuffer result = Read(len);
	return string((const char*)result.GetData(), len);
}
Пример #3
0
int DidUncompress::DisassemblePack(DC_HEAD* pPack,DataBuffer& buf)
{
	if(!pPack)return -1;

	DC_HEAD* pHead = (DC_HEAD*)pPack;
	DataBuffer t;
	if (pPack->m_cTag == DC_TAG && (pPack->m_wAttrib & DC_CPS_MASK) == DC_ZLIB_CPS)
	{
		ZLIB_HEAD* pZlibHead = (ZLIB_HEAD*)(pPack+1);
		if(t.Allocate(pZlibHead->m_dwSourceLen + sizeof(DC_HEAD) == -1))
			return -1;
		memcpy(t.GetData(),pPack,sizeof(DC_HEAD));
		int iDestLen = pZlibHead->m_dwSourceLen;
		char* pSrcData = (char*)(pZlibHead+1);
		uncompress(	(Bytef*)(t.GetData() + sizeof(DC_HEAD)), 
					(uLongf*)&iDestLen, 
					(Bytef*)pSrcData, 
					pPack->m_nLen-sizeof(ZLIB_HEAD));
		if(iDestLen != pZlibHead->m_dwSourceLen)
			return -1;
		pHead = (DC_HEAD*)t.GetData();
		pHead->m_nLen = pZlibHead->m_dwSourceLen;
	}

	if(pPack->m_cType == DCT_DID)
	{
		DC_DIDHead* pDidHead = (DC_DIDHead*)(pHead+1);
		int iDidHeadLen = pDidHead->GetLen();
		int len = 0;
		if (pPack->m_cTag == DC_TAG && (pPack->m_wAttrib & DC_CPS_MASK) == DC_NOCOMPRESS)
		{
			len = pHead->m_nLen - iDidHeadLen;
			if(buf.Allocate(len)!= -1)
			{
				memcpy(buf.GetData(),(char*)pDidHead + iDidHeadLen,len);
				return 1;
			}
			else
				return -1;
		}

		if(pPack->m_cTag == DC_TAG && (pPack->m_wAttrib & DC_CPS_MASK) == DC_XMLCID_CPS)
		{
			int did = pDidHead->GetDid();
			//LOG4CXX_INFO(logger_, "did:" << did);
			int num = pDidHead->GetRecNum();
			//LOG4CXX_INFO(logger_, "did num:" << num);
			//pDidHead->
			DC_DIDCompress* pDidCompress = (DC_DIDCompress*)((char*)pDidHead + iDidHeadLen);
			len = pDidCompress->GetUnComLen();
			//LOG4CXX_INFO(logger_, "len:" << len);
			int iCompressHeadLen = pDidCompress->GetLen();
			int iCompressLen = pHead->m_nLen - iDidHeadLen - iCompressHeadLen;
			//LOG4CXX_INFO(logger_, "did icompresslen:" << iCompressLen);
			char* pData = (char*)pDidCompress+iCompressHeadLen;

			if(!m_mapDidFile.count(did))
				return -1;

			map<int, TDidInfo>::iterator it = m_mapDidFile.find(did);
			if(!it->second.bInitialized)
				return -1;

			if(pDidHead->m_bFullRec == 1 && !it->second.bFull)
			{
				CDsCompressApi* puc = CDsCompressApi::CreateCompressObj();
				if(!puc) return -1;
				if(!puc->Initialize(	it->second.pDidStruct,
										did,
										pDidCompress->m_bCid,
										staticDay	
									)
				)
				{
					CDsCompressApi::DestroyCompressObj(puc);
					return -1;
				}

				if(buf.Allocate(len)!= -1)
				{
					puc->SetUnCompressOutputBuffer(buf.GetData(),len);
					if(puc->UnCompressData(pData,iCompressLen,num) == -1)
						return -1;
					if(puc->FinishUnCompressedData() != len)
						return -1;

					TStructForGetBmData* pObj = &m_mapStructForBmData[did];
					if(!pObj->pStkIDMapHash->empty())pObj->pStkIDMapHash->clear();
					memset(pObj->pFullData,0,pObj->iBufferSize);
					pObj->iTotalStk = 0;

					int iRecSize = it->second.pDidStruct->mDidInfo.m_dwRRecSize;
					char* record = buf.GetData();

					for(int i = 0;i < num;i++)
					{
						unsigned int stk_id = *((unsigned int*)record);
						if(!pObj->pStkIDMapHash->count(stk_id))
						{
							if (pObj->iTotalStk >= pObj->iBufferSize/iRecSize)
								return -1;
							(*(pObj->pStkIDMapHash))[stk_id] = pObj->iTotalStk;
							pObj->iTotalStk++;
							char* pPos = pObj->pFullData + ((pObj->iTotalStk-1) * iRecSize);

							//copy data to full data buffer
							memcpy(pPos, record, iRecSize);
						}
						else
						{
							int iPos = iRecSize * (*(pObj->pStkIDMapHash))[stk_id];

							if (iPos + iRecSize > pObj->iBufferSize)
								return -1;

							char* pPos = pObj->pFullData;
							pPos += iPos;

							if (*(unsigned int*)pPos != stk_id)
								return -1;

							//copy data to full data buffer
							memcpy(pPos, record, iRecSize);
						}
						record += iRecSize;
					}
					return 1;
				}
				else
					return -1;
			}
			else
			{
				if(buf.Allocate(len)!= -1)
				{
					if(!it->second.bInitialized)
					{
						LOG4CXX_ERROR(logger_, "uncompress did init failed! did:" << did);
					}
					it->second.pUnCompress->SetUnCompressOutputBuffer(buf.GetData(),len);
					if(it->second.pUnCompress->UnCompressData(pData,iCompressLen,num) == -1)
						return -1;
					int finish_uncompress_len = it->second.pUnCompress->FinishUnCompressedData();
					
					//LOG4CXX_INFO(logger_, "finish uncompress len:" << finish_uncompress_len);
					if(finish_uncompress_len != len)
						return -1;
					return 1;
				}
				else
					return -1;
			}
		}
	}
	else if (pPack->m_cType == DCT_DSDATA)
	{
/*
		DataBuffer t;
		if (pPack->m_cTag == DC_TAG && (pPack->m_wAttrib & DC_CPS_MASK) == DC_ZLIB_CPS)
		{
			 ZLIB_HEAD* pZlibHead = (ZLIB_HEAD*)(pPack+1);
			 if(t.Allocate(pZlibHead->m_dwSourceLen + sizeof(DC_HEAD) == -1))
				 return -1;
			 memcpy(t.GetData(),pPack,sizeof(DC_HEAD));
			 int iDestLen = pZlibHead->m_dwSourceLen;
			 char* pSrcData = (char*)(pZlibHead+1);
			 uncompress((Bytef*)(t.GetData() + sizeof(DC_HEAD)), 
						(uLongf*)&iDestLen, 
						(Bytef*)pSrcData, 
						pPack->m_nLen-sizeof(ZLIB_HEAD));
			 if(iDestLen != pZlibHead->m_dwSourceLen)
				 return -1;
			 pHead = (DC_HEAD*)t.GetData();
			 pHead->m_nLen = pZlibHead->m_dwSourceLen;
		}
*/

		DC_DSDATA* pDsData = (DC_DSDATA*)(pHead+1);
		int iDataLen =	pHead->m_nLen + sizeof(DC_DSLEN) - sizeof(DC_DSDATA) -
						pDsData->m_dwDidNum * sizeof(DC_DSLEN);

		if (pDsData->m_dwDidNum > 0)
		{
			DC_DSLEN* pDsLen = &(pDsData->mDsLen[0]);
			if (iDataLen < pDsLen->m_dwLen)
				return -1;
			char* pDidTemplateData = (char*)pDsLen + (pDsData->m_dwDidNum * sizeof(DC_DSLEN));
			for (int i = 0; i < pDsData->m_dwDidNum; i++)
			{
				if (iDataLen < pDsLen->m_dwLen)
					return -1;
				map<int, TDidInfo>::iterator it1 = m_mapDidFile.find(pDsLen->m_dwDid);
				if (it1 != m_mapDidFile.end() && it1->second.bNeedSynaDidStructFile)
				{
					CDidStructApi* pDidStruct = CDidStructApi::CreateDidStructObj();
					if (NULL == pDidStruct)
						return -1;
					if (pDidStruct->LoadMemXmlBuffer(pDidTemplateData, pDsLen->m_dwLen) < 0)
					{
						CDidStructApi::DestoryDidStructObj(pDidStruct);
						return -1;
					}

					char sFilePath[256] = {0};
					sprintf(sFilePath, "%u.xml\0", pDidStruct->mDidInfo.did);
					vector<TDidInfo*> vecDidFile;

					//一个模板文件可能同时支持多个did
					for (int i = 0; i < pDidStruct->m_pDids->m_wDidsNum; i++)
					{
						int did = pDidStruct->m_pDids->mDidName[i].m_dwDid;
						map<int, TDidInfo>::iterator it2 = m_mapDidFile.find(did);
						if (it2 != m_mapDidFile.end())
						{
							//删除旧文件
							remove(it2->second.strFilePath.c_str());
							vecDidFile.push_back(&it2->second);
						}
					}
					//写入新文件
					FILE* fp = fopen(sFilePath, "wb");
					if (NULL == fp)
					{
						CDidStructApi::DestoryDidStructObj(pDidStruct);
						return -1;
					}
					fwrite(pDidTemplateData, pDsLen->m_dwLen, 1, fp);
					fclose(fp);
					fp = NULL;
					for (int i = 0; i < vecDidFile.size(); i++)
					{
						TDidInfo* pDidFile = vecDidFile[i];
						pDidFile->bNeedSynaDidStructFile= false;
						pDidFile->strFilePath= sFilePath;
						if(UpdateDSData(pDidFile,pDidTemplateData,pDsLen->m_dwLen) == -1)
						{
							CDidStructApi::DestoryDidStructObj(pDidStruct);
							return -1;
						}
					}
					CDidStructApi::DestoryDidStructObj(pDidStruct);
					pDidStruct = NULL;
				}
				iDataLen -= pDsLen->m_dwLen;
				pDidTemplateData += pDsLen->m_dwLen;
				pDsLen++;
			}
		}
		return 1;
	}
	else if(pPack->m_cType == DCT_DSCRC)
	{
		DC_DSCRC* pDSCrc = (DC_DSCRC*)(pHead + 1);
		int iLen = pHead ->m_nLen;
		iLen -= sizeof(DC_DSCRC);
		for (int i = 0; i <= iLen; i += sizeof(DC_DSCRC))
		{
			map<int, TDidInfo>::iterator it = m_mapDidFile.find(pDSCrc->m_dwDid);
			if (it != m_mapDidFile.end())
			{
				it->second.bServerSupport = true;
				if (it->second.pDidStruct->m_dwCrc == pDSCrc->m_dwCrc)
					it->second.bNeedSynaDidStructFile = false;
				else
					it->second.bNeedSynaDidStructFile = true;
			}
			pDSCrc++;
		}
		return 1;
	}
	return -1;
}
Пример #4
0
bool BinaryReader::TryRead(DataBuffer& dest, size_t len)
{
	dest.SetSize(len);
	return TryRead(dest.GetData(), len);
}