📄 perplex.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: perplex.cpp,v 1.5.32.3 2004/07/09 01:48:16 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 ***** *///*******************************************************************//*************************** INCLUDE FILES *************************//*******************************************************************#include "perplex.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//*******************************************************************//************************* INTERFACE LISTING ***********************//*******************************************************************BEGIN_INTERFACE_LIST(CHXPerplex) INTERFACE_LIST_ENTRY(IID_IHXPerplex, IHXPerplex)END_INTERFACE_LIST//*******************************************************************//*************************** CONSTRUCTOR ***************************//*******************************************************************CHXPerplex::CHXPerplex(){;}//*******************************************************************//*************************** DESTRUCTOR ****************************//*******************************************************************CHXPerplex::~CHXPerplex(){ ;}//*******************************************************************//*************************** Perplex() *****************************//*******************************************************************STDMETHODIMP CHXPerplex::Perplex(IHXBuffer *pInBuf, IHXBuffer *pOutBuf){ UINT32 FinalLen; // length of encoded data CHXPerplexBuffer pInPBuf; // Temp buffer to keep code // copy the IRMA Input Buffer into the CHXPerplex Input Buffer pInPBuf.SafeMemCopy(0, (const void*) pInBuf->GetBuffer(), (UINT32)pInBuf->GetSize()); // Make sure the input buffer size is a multiple of 4bytes as // needed by Perplex alg. Padding with '\0' otherwise. UINT32 nAlign = (pInBuf->GetSize()) % sizeof(ULONG32); UINT32 Offset = (UINT32)pInBuf->GetSize(); if (nAlign > 0) { pInPBuf.EnsureValidOffset(Offset+sizeof(ULONG32)-nAlign); for (; nAlign < sizeof(ULONG32); nAlign++) { pInPBuf.SetUCHAR(Offset++,0); } } FinalLen = Offset * Perplex_PER_ULONG32 / sizeof(ULONG32); // calc size of the outout (Perplexed) buffer. // alloc mem for final perplexed buffer // Add one to length 'cause low level perplex adds // a '\0' to the resulting string pOutBuf->SetSize(FinalLen+1); if (pOutBuf->GetBuffer() == NULL) return(HXR_FAIL); CHXPerplex::DumpToPerplex((char*)(pOutBuf->GetBuffer()), FinalLen+1, pInPBuf.GetPtr(), Offset); return(HXR_OK);}//*******************************************************************//*************************** DePerplex() ***************************//*******************************************************************// Parameters://// const char* Perplex// Pointer to a buffer that contains a Perplexidecimal encoding //// UCHAR* Bits// Pointer to a buffer that will contain decoded bytes of information//// int nSize// Input size in bytesSTDMETHODIMP CHXPerplex::DePerplex(IHXBuffer *pInBuf, IHXBuffer *pOutBuf){ const char* pPerplex = (const char *)pInBuf->GetBuffer(); UCHAR* Bits = NULL; UINT32 nSize = pInBuf->GetSize(); if (!nSize) { pOutBuf->SetSize(0); return HXR_OK; } nSize--; // -1 is for the extra character added in Perplex UINT32 ndxBits; UINT32 ndxPerplex = 0; ULONG32 temp32; UINT32 ulOutSize = 2*nSize+100; pOutBuf->SetSize(ulOutSize); // Make it bigger since the real size isn't known yet Bits = (UCHAR *)pOutBuf->GetBuffer(); for (ndxBits = 0; ndxPerplex < nSize; ) { temp32 = FromPerplex(&pPerplex[ndxPerplex]); ndxPerplex+=Perplex_PER_ULONG32; if (ndxBits+4 <= ulOutSize) memcpy(&Bits[ndxBits],&temp32,sizeof(temp32)); /* Flawfinder: ignore */ ndxBits+=sizeof(temp32); } pOutBuf->SetSize(ndxBits); return HXR_OK;}//*******************************************************************//************************* MapFromPerplex **************************//*******************************************************************// Converts appropriate characters for Perplex encoding into a ULONG32.// A copy of this is kept in in pnmisc\pub\win\pnmisc16.h. If you change // this (which is very unlikely), then be sure to change it there. static const char zPerplexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E' };UCHAR CHXPerplex::MapFromPerplex(char Perplex){ UCHAR temp = sizeof(zPerplexChars)/sizeof(zPerplexChars[0]); for (UCHAR n = 0; n < temp; n++) { if (Perplex == zPerplexChars[n]) return n; } return 0;}//*******************************************************************//*************************** MapToPerplex **************************//*******************************************************************// Converts a digit ordinal to the Perplex digit...char CHXPerplex::MapToPerplex(UCHAR cPerplex){ #ifdef _DEBUG int size_zPerplexChars = sizeof(zPerplexChars); int size_zPerplexChars0 = sizeof(zPerplexChars[0]); HX_ASSERT(cPerplex < size_zPerplexChars/size_zPerplexChars0);#endif return zPerplexChars[cPerplex];}//*******************************************************************//*************************** FromPerplex ***************************//*******************************************************************// Converts appropriate characters for Perplex encoding into a ULONG32.ULONG32 CHXPerplex::FromPerplex(const char* Perplex){ ULONG32 value = 0; ULONG32 PerplexBase = 1; for (int n = 0; n < Perplex_PER_ULONG32; n++) { value += MapFromPerplex(Perplex[n]) * PerplexBase; PerplexBase *= Perplex_BASE; } // Convert to local byte order! value = DwToHost(value); return value;}//*******************************************************************//**************************** ToPerplex ****************************//*******************************************************************// Converts a ULONG32 into the appropriate characters for Perplex encoding.void CHXPerplex::ToPerplex(ULONG32 Input, char* pPerplex){ ULONG32 value; UCHAR charValue; value = DwToNet(Input); // Convert to net byte order! for (int n=0; n < Perplex_PER_ULONG32; n++) { charValue = (UCHAR)(value % Perplex_BASE); pPerplex[n] = MapToPerplex(charValue); value = value / Perplex_BASE; }}//*******************************************************************//*************************** DumpToPerplex *************************//*******************************************************************// Parameters://// char* pPerplex// Pointer to buffer to be filled with Perplexadecimal void CHXPerplex::DumpToPerplex(char* pPerplex, UINT32 ulPerplexSize, UCHAR* Bits, UINT32 nSize){ UINT32 ndxBits; UINT32 ndxPerplex = 0; ULONG32 temp32; for (ndxBits = 0; ndxBits < nSize; ) { if (ndxBits+4 <= nSize) memcpy(&temp32,&Bits[ndxBits],sizeof(temp32)); /* Flawfinder: ignore */ ndxBits+=sizeof(temp32); if (ndxPerplex+Perplex_PER_ULONG32 <= ulPerplexSize) ToPerplex(temp32,&pPerplex[ndxPerplex]); ndxPerplex+=Perplex_PER_ULONG32; } pPerplex[ndxPerplex] = '\0';}//*******************************************************************//***** ******//***** ******//***** ******//***** ******//***** ******//***** ******//***** CHXPerplexBuffer ******//***** ******//***** ******//***** ******//***** ******//***** ******//***** ******//*******************************************************************//*******************************************************************//****************** CHXPerplexBuffer CONSTRUCTOR *******************//*******************************************************************CHXPerplexBuffer::CHXPerplexBuffer() : m_nSize(0), m_pData(NULL), m_nGrowBy(DEFAULT_GROW_SIZE){}//*******************************************************************//****************** CHXPerplexBuffer CONSTRUCTOR *******************//*******************************************************************CHXPerplexBuffer::CHXPerplexBuffer(UINT32 nSize, UINT32 nGrowBy) : m_nSize(0), m_pData(NULL), m_nGrowBy(nGrowBy){ Resize(nSize);}//*******************************************************************//******************* CHXPerplexBuffer DESTRUCTOR *******************//*******************************************************************CHXPerplexBuffer::~CHXPerplexBuffer(){ Free();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -