hxinfcod.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:37k
- /* ***** 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_FILE
- static const char HX_THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////
- static UINT16 FourByteAlign(UINT16 number)
- {
- return( ((number+3)>>2)<<2 );
- }
- //*******************************************************************
- // CHXInfoEncoder
- //*******************************************************************
- // IUnknown interface listing
- BEGIN_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 ' ' 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 ' ' 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 pointer
- STDMETHODIMP_(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 pointer
- STDMETHODIMP_(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="