📄 hxinfcod.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: hxinfcod.cpp,v 1.5.32.3 2004/07/09 01:48:15 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//**************************************************************************** * * This file contains implementation of classes that support * the encoding of raw bytes of upgrade negotiation messages * into a clear text representaion of the raw data. * */#include "hxinfcod.h"#include "hxassert.h"#include "hlxclib/string.h"//#include "hlxclib/stdio.h"#include "netbyte.h"#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILEstatic const char HX_THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////static UINT16 FourByteAlign(UINT16 number){ return( ((number+3)>>2)<<2 );}//*******************************************************************// CHXInfoEncoder//*******************************************************************// IUnknown interface listingBEGIN_INTERFACE_LIST(CHXInfoEncoder) INTERFACE_LIST_ENTRY(IID_IHXObjOutStream, IHXObjOutStream)END_INTERFACE_LIST/////////////////////////////////////////////////////////////////////CHXInfoEncoder::CHXInfoEncoder(){ m_FinalBuffer = NULL; m_bFinalBufferAllocFlag = FALSE; m_nFinalLen = 0; m_nOffset = 0;}/////////////////////////////////////////////////////////////////////// get ptr to encoded data - either the data buffer that's been Dump()'d// to, or a secondary buffer where data was further encoded (ie,// Hex'd or perplex'd).STDMETHODIMP_(const char*) CHXInfoEncoder::GetBuffer(){ if (m_FinalBuffer) return((char*)m_FinalBuffer); return((char*)m_Buffer.GetPtr());}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::GetLength(){ if (m_FinalBuffer) return(m_nFinalLen); return(m_nOffset);}/////////////////////////////////////////////////////////////////////CHXInfoEncoder::~CHXInfoEncoder(){ if ( (m_bFinalBufferAllocFlag==TRUE) && (m_FinalBuffer!=NULL) ) { delete [] m_FinalBuffer; }}/////////////////////////////////////////////////////////////////////void CHXInfoEncoder::EncodeObj(IHXStreamableObj* pObj){ this->Initialize(); this->WriteObj(pObj);}/////////////////////////////////////////////////////////////////////void CHXInfoEncoder::PerplexEncodeObj(IHXStreamableObj* pObj){ this->Initialize(); this->WriteObj(pObj); this->Perplex();}/////////////////////////////////////////////////////////////////////BOOL CHXInfoEncoder::Perplex(){ // no data to perplex? if (m_nOffset==0) return(FALSE); // if final buffer already exists, return error..... // That means we already did an encoding, and the user should // call Initialize() before the encoder is reused. if (m_FinalBuffer!=NULL) return(FALSE); // Make sure we're 4byte aligned. Needed by Perplex alg. UINT32 nAlign = (m_nOffset) % sizeof(ULONG32); if (nAlign > 0) { m_Buffer.EnsureValidOffset(m_nOffset+sizeof(ULONG32)-nAlign); for (; nAlign < sizeof(ULONG32); nAlign++) { m_Buffer.SetUCHAR(m_nOffset++,0); } } // calc size of Perplexed buffer. m_nFinalLen = m_nOffset * Perplex_PER_ULONG32 / sizeof(ULONG32); // alloc mem for final perplexed buffer // Add one to length 'cause low level perplex adds // a '\0' to the resulting string m_bFinalBufferAllocFlag = TRUE; m_FinalBuffer = new UCHAR[m_nFinalLen+1]; HX_ASSERT(m_FinalBuffer!=NULL); if( m_FinalBuffer==NULL) { m_nFinalLen=0; return(FALSE); } else { CHXInfoEncoder::DumpToPerplex((char*)m_FinalBuffer, m_nFinalLen+1, m_Buffer.GetPtr(), m_nOffset); } return(TRUE);}/////////////////////////////////////////////////////////////////////void CHXInfoEncoder::HexEncodeObj(IHXStreamableObj* pObj){ this->Initialize(); this->WriteObj(pObj); this->Hex();}/////////////////////////////////////////////////////////////////////BOOL CHXInfoEncoder::Hex(){ // no data to perplex? if (m_nOffset==0) return(FALSE); // if final buffer already exists, return error..... // That means we already did an encoding, and the user should // call Initialize() before the encoder is reused. if (m_FinalBuffer!=NULL) return(FALSE); // calc size of hexed buffer. m_nFinalLen = m_nOffset * 2; // alloc mem for final perplexed buffer m_bFinalBufferAllocFlag = TRUE; // Add one to length 'cause low level perplex adds // a '\0' to the resulting string m_FinalBuffer = new UCHAR[m_nFinalLen+1]; HX_ASSERT(m_FinalBuffer!=NULL); if( m_FinalBuffer==NULL) { m_nFinalLen=0; return(FALSE); } else { CHXInfoEncoder::DumpToHex((char*)m_FinalBuffer, m_Buffer.GetPtr(), m_nOffset); } return(TRUE);}/////////////////////////////////////////////////////////////////////STDMETHODIMP CHXInfoEncoder::Initialize(){ if (m_bFinalBufferAllocFlag==TRUE) { if (m_FinalBuffer!=NULL) delete [] m_FinalBuffer; } m_bFinalBufferAllocFlag = FALSE; m_FinalBuffer = NULL; m_nFinalLen = 0; m_Buffer.Free(); m_nOffset=0; return(HXR_OK);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteObj(IHXStreamableObj* pObj) { UINT32 nLen=this->GetOffset(); if (pObj->WriteObjToBits(this)==HXR_OK) return this->GetOffset()-nLen;#if 0 // reading object this->Seek(nLen);#endif return(0);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUCHAR(UCHAR nValue){ m_Buffer.SafeMemCopy(m_nOffset,&nValue, sizeof(nValue)); m_nOffset += sizeof(nValue); return sizeof(nValue);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUINT16(UINT16 nValue){ UINT16 temp16 = WToNet(nValue); m_Buffer.SafeMemCopy(m_nOffset,&temp16, sizeof(temp16)); m_nOffset += sizeof(temp16); return sizeof(temp16);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUINT32(UINT32 nValue){ UINT32 temp32 = DwToNet(nValue); m_Buffer.SafeMemCopy(m_nOffset, &temp32, sizeof(temp32)); m_nOffset += sizeof(temp32); return sizeof(temp32);}/////////////////////////////////////////////////////////////////////// Dump at a specific position. Doesn't advance offset pointerSTDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUINT16At(UINT32 nOffset, UINT16 nValue){ UINT16 temp16 = WToNet(nValue); if (m_Buffer.IsValidOffset(nOffset+sizeof(temp16))==TRUE) { m_Buffer.MemCopy(nOffset,&temp16, sizeof(temp16)); } return sizeof(temp16);}/////////////////////////////////////////////////////////////////////// Dump at a specific position. Doesn't advance offset pointerSTDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUINT32At(UINT32 nOffset, UINT32 nValue){ UINT32 temp32 = DwToNet(nValue); if (m_Buffer.IsValidOffset(nOffset+sizeof(temp32))==TRUE) { m_Buffer.MemCopy(nOffset, &temp32, sizeof(temp32)); } return sizeof(temp32);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteString(const char* szValue){ if (szValue==NULL) { szValue="\0"; } UINT32 FieldLen = strlen(szValue); HX_ASSERT(FieldLen < 0xFF); // ensure the buffer has enough room for us. if (m_Buffer.EnsureValidOffset(m_nOffset+FieldLen+1)==TRUE) { m_Buffer.SetUCHAR(m_nOffset, (UCHAR)FieldLen ); m_Buffer.MemCopy(m_nOffset + 1, szValue, FieldLen); } FieldLen ++; //account for first SIZE byte m_nOffset += FieldLen; return FieldLen;}/////////////////////////////////////////////////////////////////////// Similar to DumpString, except this one is handy when you need// to concat 2 or 3 strings and Dump them. Instead of allocating// a buffer, concating the strings and calling DumpString, use// this routine instead. It will take 2 or 3 strings as a parameters,// and dump them concatenated. The original strings are not modified.STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteStringCat(const char* szValue1,const char* szValue2,const char* szValue3){ if (szValue1==NULL) { szValue1="\0"; } if (szValue2==NULL) { szValue2="\0"; } if (szValue3==NULL) { szValue3="\0"; } UINT16 nLen1=strlen(szValue1); UINT16 nLen2=strlen(szValue2); UINT16 nLen3=strlen(szValue3); UINT32 FieldLen = nLen1 + nLen2 + nLen3; HX_ASSERT(FieldLen < 0xFF); // ensure the buffer has enough room for us. if (m_Buffer.EnsureValidOffset(m_nOffset+FieldLen+1)==TRUE) { m_Buffer.SetUCHAR(m_nOffset, (UCHAR)FieldLen ); m_nOffset++; if (nLen1) { m_Buffer.MemCopy(m_nOffset, szValue1, nLen1); m_nOffset+=nLen1; } if (nLen2) { m_Buffer.MemCopy(m_nOffset, szValue2, nLen2); m_nOffset+=nLen2; } if (nLen3) { m_Buffer.MemCopy(m_nOffset, szValue3, nLen3); m_nOffset+=nLen3; } } FieldLen ++; //account for first SIZE byte return FieldLen;}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteLargeString(const char* szValue){ if (szValue==NULL) { szValue="\0"; } // get the length of the string UINT32 FieldLen = strlen(szValue); HX_ASSERT(FieldLen < 0xFFFF); // dump the size (this increments m_nOffset for us too) UINT32 nSize = WriteUINT16((UINT16)FieldLen); m_Buffer.SafeMemCopy(m_nOffset, szValue, FieldLen); m_nOffset += FieldLen; return (FieldLen+nSize);}/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteBuffer(const char* szBuffer, UINT32 nSize){ HX_ASSERT(szBuffer!=NULL); if (szBuffer!=NULL) { m_Buffer.SafeMemCopy(m_nOffset, szBuffer, nSize); } m_nOffset += nSize; return (nSize);}#if 0/////////////////////////////////////////////////////////////////////STDMETHODIMP_(UINT32) CHXInfoEncoder::WriteUTCTime(UTCTimeRep& TimeRep){ return(this->WriteUINT32( TimeRep.asUTCTimeT() ));// return this->WriteString(( TimeRep.asUTCString() );}#endif//*******************************************************************// CHXInfoDecoder//*******************************************************************// IUnknown interface listingBEGIN_INTERFACE_LIST(CHXInfoDecoder) INTERFACE_LIST_ENTRY(IID_IHXObjInStream, IHXObjInStream)END_INTERFACE_LIST/////////////////////////////////////////////////////////////////////BOOL CHXInfoDecoder::DecodeObj(const UCHAR* buffer, UINT32 nSize, IHXStreamableObj* pObj){ HX_ASSERT(buffer!=NULL); if (buffer==NULL) { return(FALSE); } this->Initialize(); // Casting away the const, so we must promise not to mess with the // contents... m_Buffer = (UCHAR *)buffer; m_nDecodedLen = nSize; // ReadObj returns 0 on failure return (this->ReadObj(pObj)>0);}/////////////////////////////////////////////////////////////////////BOOL CHXInfoDecoder::PerplexDecodeObj(const char* szPerplex, IHXStreamableObj* pObj){ if (szPerplex) { return this->PerplexDecodeObj(szPerplex,strlen(szPerplex),pObj); } return(FALSE);}/////////////////////////////////////////////////////////////////////// We'll unperplex the data into our own buffer, then decode the obj.BOOL CHXInfoDecoder::PerplexDecodeObj(const char* szPerplex, UINT32 nPlexLen, IHXStreamableObj* pObj){ UINT32 nBitLen = nPlexLen * sizeof(ULONG32) / Perplex_PER_ULONG32; UCHAR* Buffer = new UCHAR[nBitLen + 4]; HX_ASSERT(Buffer!=NULL); if (Buffer==NULL) return(FALSE); UINT32 nBitLenActual = CHXInfoDecoder::SetFromPerplex(szPerplex, Buffer, nBitLen+4, nPlexLen); BOOL bRetVal = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -