wirefmgr.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:75k
源码类别:

Symbian

开发平台:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: wirefmgr.cpp,v 1.2.24.1 2004/07/09 01:54:51 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. // include
  50. #include "hxtypes.h"
  51. #include "hxwintyp.h"
  52. #include "hxresult.h"
  53. #include "hxcom.h"
  54. #include "ihxpckts.h"
  55. #include "hxvsurf.h"
  56. #include "hxfiles.h"
  57. #include "hxcomm.h"
  58. #include "hxerror.h"
  59. // hxmisc
  60. #include "unkimp.h"
  61. #include "baseobj.h"
  62. // hxcont
  63. #include "hxstring.h"
  64. #include "hxslist.h"
  65. #include "hxmap.h"
  66. // pxcomlib
  67. #include "pxrect.h"
  68. #include "pxcolor.h"
  69. #include "pxeffect.h"
  70. #include "hxslist.h"
  71. #include "hxmap.h"
  72. #include "gstring.h"
  73. #include "pxutil.h"
  74. #include "nestbuff.h"
  75. #include "wirefmgr.h"
  76. // hxdebug
  77. #include "errdbg.h"
  78. #include "hxheap.h"
  79. #ifdef _DEBUG
  80. #undef HX_THIS_FILE     
  81. static char HX_THIS_FILE[] = __FILE__;
  82. #endif
  83. #define BASE_STREAM_VERSION      HX_ENCODE_PROD_VERSION(0, 0, 0, 0)
  84. #define U2_STREAM_VERSION        HX_ENCODE_PROD_VERSION(1, 1, 0, 0)
  85. #define COOKIE_STREAM_VERSION    HX_ENCODE_PROD_VERSION(1, 2, 0, 0)
  86. #define CACHINGAD_STREAM_VERSION HX_ENCODE_PROD_VERSION(1, 3, 0, 0)
  87. #define OPACITY_STREAM_VERSION   HX_ENCODE_PROD_VERSION(1, 4, 0, 0)
  88. const UINT32 PXWireFormatManager::m_ulHighestSupportedStreamVersion = OPACITY_STREAM_VERSION;
  89. PXWireFormatManager::PXWireFormatManager()
  90. {
  91.     Reset();
  92.     m_lRefCount            = 0;
  93.     m_pContext             = NULL;
  94.     m_pCommonClassFactory  = NULL;
  95.     m_pTitleStr            = NULL;
  96.     m_pAuthorStr           = NULL;
  97.     m_pCopyrightStr        = NULL;
  98.     m_pDefaultURLStr       = NULL;
  99.     m_pCodecMimeList       = NULL;
  100.     m_pFXPackageMimeList   = NULL;
  101.     m_pMapMimeToOpaqueSize = NULL;
  102.     m_pASMRuleBook         = NULL;
  103.     m_pStreamMimeType      = NULL;
  104. }
  105. PXWireFormatManager::~PXWireFormatManager()
  106. {
  107.     Deallocate();
  108. }
  109. HX_RESULT PXWireFormatManager::CreateObject(PXWireFormatManager** ppObj)
  110. {
  111.     HX_RESULT retVal = HXR_FAIL;
  112.     if (ppObj)
  113.     {
  114.         PXWireFormatManager* pObj = new PXWireFormatManager();
  115.         if (pObj)
  116.         {
  117.             *ppObj = pObj;
  118.             retVal = HXR_OK;
  119.         }
  120.     }
  121.     return retVal;
  122. }
  123. STDMETHODIMP PXWireFormatManager::QueryInterface(REFIID riid, void** ppvObj)
  124. {
  125.     HX_RESULT retVal = HXR_OK;
  126.     if (IsEqualIID(riid, IID_IUnknown))
  127.     {
  128.         AddRef();
  129.         *ppvObj = (IUnknown*) this;
  130.     }
  131.     else
  132.     {
  133.         *ppvObj = NULL;
  134.         retVal  = HXR_NOINTERFACE;
  135.     }
  136.     return retVal;
  137. }
  138. STDMETHODIMP_(UINT32) PXWireFormatManager::AddRef()
  139. {
  140.     return InterlockedIncrement(&m_lRefCount);
  141. }
  142. STDMETHODIMP_(UINT32) PXWireFormatManager::Release()
  143. {
  144.     
  145.     if (InterlockedDecrement(&m_lRefCount) > 0)
  146.         return m_lRefCount;
  147.     delete this;
  148.     return 0;
  149. }
  150. HX_RESULT PXWireFormatManager::Init(IUnknown* pContext, UINT32 ulStreamVersion)
  151. {
  152.     HX_RESULT retVal = HXR_OK;
  153.     // Clear out everything
  154.     Deallocate();
  155.     Reset();
  156.     // Check to make sure the stream version is not too late
  157.     if ((HX_GET_MAJOR_VERSION(ulStreamVersion) < HX_GET_MAJOR_VERSION(m_ulHighestSupportedStreamVersion) ||
  158.          (HX_GET_MAJOR_VERSION(ulStreamVersion) == HX_GET_MAJOR_VERSION(m_ulHighestSupportedStreamVersion) &&
  159.           HX_GET_MINOR_VERSION(ulStreamVersion) <= HX_GET_MINOR_VERSION(m_ulHighestSupportedStreamVersion))) &&
  160.         pContext)
  161.     {
  162.         // Save the calling context
  163.         m_pContext = pContext;
  164.         m_pContext->AddRef();
  165.         // Set the stream version
  166.         m_ulStreamVersion = ulStreamVersion;
  167.         // Create a list for the codec mimes
  168.         HX_DELETE(m_pCodecMimeList);
  169.         m_pCodecMimeList = new CHXSimpleList();
  170.         if (m_pCodecMimeList)
  171.         {
  172.             // Create a list for the fx package mimes
  173.             HX_DELETE(m_pFXPackageMimeList);
  174.             m_pFXPackageMimeList = new CHXSimpleList();
  175.             if (m_pFXPackageMimeList)
  176.             {
  177.                 // Create a map
  178.                 HX_DELETE(m_pMapMimeToOpaqueSize);
  179.                 m_pMapMimeToOpaqueSize = new CHXMapStringToOb();
  180.                 if (m_pMapMimeToOpaqueSize)
  181.                 {
  182.                     // Add the number of header bytes for the codecs we know about
  183.                     // XXXMEH - this is a hack - there should be NO codec-specific
  184.                     // code in here, since it attaches some dependence in between
  185.                     // codecs and the renderer.
  186.                     m_pMapMimeToOpaqueSize->SetAt("image/vndr.rn-realpix.jpeg", (void*) 12);
  187.                     m_pMapMimeToOpaqueSize->SetAt("image/vnd.rn-realpix.gif",   (void*)  8);
  188.                     m_pMapMimeToOpaqueSize->SetAt("image/vnd.rn-realpix.png",   (void*)  4);
  189.                     // Get an IHXCommonClassFactory interface
  190.                     retVal = m_pContext->QueryInterface(IID_IHXCommonClassFactory,
  191.                                                         (void**) &m_pCommonClassFactory);
  192.                 }
  193.                 else
  194.                 {
  195.                     retVal = HXR_OUTOFMEMORY;
  196.                 }
  197.             }
  198.             else
  199.             {
  200.                 retVal = HXR_OUTOFMEMORY;
  201.             }
  202.         }
  203.         else
  204.         {
  205.             retVal = HXR_OUTOFMEMORY;
  206.         }
  207.     }
  208.     else
  209.     {
  210.         retVal = HXR_INVALID_PARAMETER;
  211.     }
  212.     if (FAILED(retVal))
  213.     {
  214.         Deallocate();
  215.         Reset();
  216.     }
  217.     return retVal;
  218. }
  219. HX_RESULT PXWireFormatManager::GetFileHeader(REF(IHXValues*) rpFileHeader,
  220.                                              const char* pszAcceptMetaInfo)
  221. {
  222.     HX_RESULT retVal = HXR_OK;
  223.     // Create an IHXValues object
  224.     IHXValues* pValues = NULL;
  225.     retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXValues,
  226.                                                                 (void**) &pValues);
  227.     if (SUCCEEDED(retVal))
  228.     {
  229.         // Set properties
  230.         pValues->SetPropertyULONG32("StreamCount",    1);
  231.         pValues->SetPropertyULONG32("IsRealDataType", 1);
  232.         // If there are TAC properties, then set them
  233.         if (m_pTitleStr)
  234.         {
  235.             pValues->SetPropertyBuffer("Title", m_pTitleStr);
  236.         }
  237.         if (m_pAuthorStr)
  238.         {
  239.             pValues->SetPropertyBuffer("Author", m_pAuthorStr);
  240.         }
  241.         if (m_pCopyrightStr)
  242.         {
  243.             pValues->SetPropertyBuffer("Copyright", m_pCopyrightStr);
  244.         }
  245.         // Check if we need to set live stream flag
  246.         if (m_bIsLive)
  247.         {
  248.             pValues->SetPropertyULONG32("LiveStream", 1);
  249.         }
  250.         // Check if we need to set the MinimizeLatency flag
  251.         if (m_bMinimizeLatency)
  252.         {
  253.             pValues->SetPropertyULONG32("MinimizeLatency", 1);
  254.         }
  255.         // Add the requested meta info (if any)
  256.         if (pszAcceptMetaInfo)
  257.         {
  258.             // Create the IHXValues of available meta info
  259.             IHXValues* pMetaInfo = NULL;
  260.             m_pCommonClassFactory->CreateInstance(CLSID_IHXValues, (void**) &pMetaInfo);
  261.             if (pMetaInfo)
  262.             {
  263.                 // Add the available meta info
  264.                 pMetaInfo->SetPropertyULONG32("Width",   m_ulDisplayWidth);
  265.                 pMetaInfo->SetPropertyULONG32("Height",  m_ulDisplayHeight);
  266.                 pMetaInfo->SetPropertyULONG32("Bitrate", m_ulBitrate);
  267.                 // Add any requested meta info the file header
  268.                 AddMetaInfo(pMetaInfo, pszAcceptMetaInfo, pValues);
  269.             }
  270.             HX_RELEASE(pMetaInfo);
  271.         }
  272.         // Assign to the out parameter
  273.         HX_RELEASE(rpFileHeader);
  274.         rpFileHeader = pValues;
  275.         rpFileHeader->AddRef();
  276.     }
  277.     HX_RELEASE(pValues);
  278.     return retVal;
  279. }
  280. HX_RESULT PXWireFormatManager::GetStreamHeader(REF(IHXValues*) rpStreamHeader)
  281. {
  282.     HX_RESULT retVal = HXR_FAIL;
  283.     // Check to see if we have valid values for all the
  284.     // required members
  285.     if (m_pStreamMimeType && m_pASMRuleBook && m_ulBitrate &&
  286.         m_ulDuration && m_pCommonClassFactory)
  287.     {
  288.         IHXValues* pValues = NULL;
  289.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXValues,
  290.                                                                     (void**) &pValues);
  291.         if (SUCCEEDED(retVal))
  292.         {
  293.             // Set some properties
  294.             pValues->SetPropertyCString("MimeType",          m_pStreamMimeType);
  295.             pValues->SetPropertyCString("ASMRuleBook",       m_pASMRuleBook);
  296.             pValues->SetPropertyULONG32("StreamNumber",      0);
  297.             pValues->SetPropertyULONG32("MaxBitRate",        m_ulBitrate);
  298.             pValues->SetPropertyULONG32("AvgBitRate",        m_ulBitrate);
  299.             pValues->SetPropertyULONG32("StartTime",         m_ulStart);
  300.             pValues->SetPropertyULONG32("Preroll",           m_ulPreroll);
  301.             pValues->SetPropertyULONG32("PreData",           m_ulPreData);
  302.             pValues->SetPropertyULONG32("Duration",          m_ulDuration);
  303.             pValues->SetPropertyULONG32("StreamVersion",     m_ulStreamVersion);
  304.             pValues->SetPropertyULONG32("ContentVersion",    m_ulContentVersion);
  305.             pValues->SetPropertyULONG32("RendererFlags",     m_ulRendererFlags);
  306.             pValues->SetPropertyULONG32("BackgroundOpacity", m_ulBackgroundOpacity);
  307.             if (m_bPreDataAtStart)
  308.             {
  309.                 pValues->SetPropertyULONG32("PredataAtStart",   1);
  310.             }
  311.             if (m_bPrerollAfterSeek)
  312.             {
  313.                 pValues->SetPropertyULONG32("PrerollAfterSeek", 1);
  314.             }
  315.             // Create an IHXBuffer
  316.             IHXBuffer* pOpBuffer = NULL;
  317.             retVal                = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  318.                                                                           (void**) &pOpBuffer);
  319.             if (SUCCEEDED(retVal))
  320.             {
  321.                 // Compute the size. When we call PackStreamHeader(,FALSE), it
  322.                 // only computes the size and doesn't pack the stream header.
  323.                 BYTE*  pBuf   = NULL;
  324.                 UINT32 ulSize = PackStreamHeader(pBuf, FALSE);
  325.                 retVal        = pOpBuffer->SetSize(ulSize);
  326.                 if (SUCCEEDED(retVal))
  327.                 {
  328.                     if (HX_GET_MAJOR_VERSION(m_ulStreamVersion) <=
  329.                         HX_GET_MAJOR_VERSION(m_ulHighestSupportedStreamVersion) &&
  330.                         HX_GET_MINOR_VERSION(m_ulStreamVersion) <=
  331.                         HX_GET_MINOR_VERSION(m_ulHighestSupportedStreamVersion))
  332.                     {
  333.                         // Now we call PackStreamHeader(,TRUE), which actually
  334.                         // packs the stream header
  335.                         pBuf = pOpBuffer->GetBuffer();
  336.                         PackStreamHeader(pBuf, TRUE);
  337.                     }
  338.                     else
  339.                     {
  340.                         retVal = HXR_FAIL;
  341.                     }
  342.                     if (SUCCEEDED(retVal))
  343.                     {
  344.                         // Set the opaque data property
  345.                         pValues->SetPropertyBuffer("OpaqueData", pOpBuffer);
  346.                         // Assign the out parameter
  347.                         HX_RELEASE(rpStreamHeader);
  348.                         rpStreamHeader = pValues;
  349.                         rpStreamHeader->AddRef();
  350.                     }
  351.                 }
  352.             }
  353.             HX_RELEASE(pOpBuffer);
  354.         }
  355.         HX_RELEASE(pValues);
  356.     }
  357.     return retVal;
  358. }
  359. HX_RESULT PXWireFormatManager::OnHeader(IHXValues* pHeader)
  360. {
  361.     HX_RESULT retVal = HXR_FAIL;
  362.     if (pHeader)
  363.     {
  364.         pHeader->GetPropertyULONG32("Duration",          m_ulDuration);
  365.         pHeader->GetPropertyULONG32("ContentVersion",    m_ulContentVersion);
  366.         pHeader->GetPropertyULONG32("RendererFlags",     m_ulRendererFlags);
  367.         pHeader->GetPropertyULONG32("BackgroundOpacity", m_ulBackgroundOpacity);
  368.         IHXBuffer* pBuffer = NULL;
  369.         retVal              = pHeader->GetPropertyBuffer("OpaqueData", pBuffer);
  370.         if (SUCCEEDED(retVal))
  371.         {
  372.             if (m_ulStreamVersion <= m_ulHighestSupportedStreamVersion)
  373.             {
  374.                 // Get the display width and display height
  375.                 BYTE *pBuf = pBuffer->GetBuffer();
  376.                 UnPack32(pBuf, m_ulDisplayWidth);
  377.                 UnPack32(pBuf, m_ulDisplayHeight);
  378.                 // Get default URL
  379.                 HX_RELEASE(m_pDefaultURLStr);
  380.                 retVal = UnPackStringBuffer(pBuf, &m_pDefaultURLStr);
  381.                 if (SUCCEEDED(retVal))
  382.                 {
  383.                     // Unpack total mimes
  384.                     UINT32 ulTotalMimes = 0;
  385.                     UnPack32(pBuf, ulTotalMimes);
  386.                     // Unpack codec mimes
  387.                     UINT32 ulNumCodecs = 0;
  388.                     UnPack32(pBuf, ulNumCodecs);
  389.                     ReleaseAllCodecMimes();
  390.                     if (ulNumCodecs > 0)
  391.                     {
  392.                         for (UINT32 i = 0; i < ulNumCodecs; i++)
  393.                         {
  394.                             IHXBuffer* pMime = NULL;
  395.                             retVal            = UnPackStringBuffer(pBuf, &pMime);
  396.                             if (SUCCEEDED(retVal))
  397.                             {
  398.                                 retVal = AddCodecMime(pMime);
  399.                             }
  400.                             HX_RELEASE(pMime);
  401.                             if (FAILED(retVal))
  402.                             {
  403.                                 break;
  404.                             }
  405.                         }
  406.                     }
  407.                     if (SUCCEEDED(retVal))
  408.                     {
  409.                         // Unpack fxpackage mimes
  410.                         UINT32 ulNumFXPackages = 0;
  411.                         UnPack32(pBuf, ulNumFXPackages);
  412.                         ReleaseAllFXPackageMimes();
  413.                         if (ulNumFXPackages > 0)
  414.                         {
  415.                             for (UINT32 i = 0; i < ulNumFXPackages; i++)
  416.                             {
  417.                                 IHXBuffer* pMime = NULL;
  418.                                 retVal            = UnPackStringBuffer(pBuf, &pMime);
  419.                                 if (SUCCEEDED(retVal))
  420.                                 {
  421.                                     retVal = AddFXPackageMime(pMime);
  422.                                 }
  423.                                 HX_RELEASE(pMime);
  424.                                 if (FAILED(retVal))
  425.                                 {
  426.                                     break;
  427.                                 }
  428.                             }
  429.                         }
  430.                         if (SUCCEEDED(retVal))
  431.                         {
  432.                             // If the stream version is greater than or equal to 1.1.0.0,
  433.                             // then get the background color
  434.                             if (m_ulStreamVersion >= U2_STREAM_VERSION)
  435.                             {
  436.                                 UnPack32(pBuf, m_ulBackgroundColor);
  437.                             }
  438.                         }
  439.                     }
  440.                 }
  441.             }
  442.             else
  443.             {
  444.                 retVal = HXR_FAIL;
  445.             }
  446.         }
  447.         HX_RELEASE(pBuffer);
  448.     }
  449.     if (FAILED(retVal))
  450.     {
  451.         // We had an error, so reset the state
  452.         m_ulDisplayWidth  = 0;
  453.         m_ulDisplayHeight = 0;
  454.         HX_RELEASE(m_pDefaultURLStr);
  455.         ReleaseAllCodecMimes();
  456.         ReleaseAllFXPackageMimes();
  457.     }
  458.     return retVal;
  459. }
  460. HX_RESULT PXWireFormatManager::GetPacketType(IHXPacket* pPacket, REF(UINT32) rulType)
  461. {
  462.     HX_RESULT retVal = HXR_OK;
  463.     if (pPacket)
  464.     {
  465.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  466.         if (pBuffer)
  467.         {
  468.             BYTE* pBuf = pBuffer->GetBuffer();
  469.             if (pBuf)
  470.             {
  471.                 UnPack32(pBuf, rulType);
  472.             }
  473.             else
  474.             {
  475.                 retVal = HXR_FAIL;
  476.             }
  477.         }
  478.         else
  479.         {
  480.             retVal = HXR_FAIL;
  481.         }
  482.         HX_RELEASE(pBuffer);
  483.     }
  484.     else
  485.     {
  486.         retVal = HXR_INVALID_PARAMETER;
  487.     }
  488.     return retVal;
  489. }
  490. HX_RESULT PXWireFormatManager::GetImageHeaderInfo(IHXPacket* pPacket, REF(UINT32) rulHandle,
  491.                                                   REF(UINT32) rulFileLength, REF(UINT32) rulFlags,
  492.                                                   IHXBuffer** ppMimeStr, REF(UINT32) rulOpaqueSize)
  493. {
  494.     HX_RESULT retVal = HXR_OK;
  495.     if (pPacket && ppMimeStr)
  496.     {
  497.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  498.         if (pBuffer)
  499.         {
  500.             BYTE* pBuf = pBuffer->GetBuffer();
  501.             if (pBuf)
  502.             {
  503.                 UINT32 ulType = 0xFFFFFFFF;
  504.                 UnPack32(pBuf, ulType);
  505.                 if (ulType == kPacketTypeImageHeader)
  506.                 {
  507.                     UINT32      ulHandle = 0;
  508.                     UINT32      ulLength = 0;
  509.                     UINT32      ulFlags  = 0;
  510.                     IHXBuffer* pMimeStr = NULL;
  511.                     UnPack32(pBuf, ulHandle);
  512.                     UnPack32(pBuf, ulLength);
  513.                     UnPack32(pBuf, ulFlags);
  514.                     retVal = UnPackStringBuffer(pBuf, &pMimeStr);
  515.                     if (SUCCEEDED(retVal))
  516.                     {
  517.                         // Assign out parameters
  518.                         rulHandle      = ulHandle;
  519.                         rulFileLength  = ulLength;
  520.                         rulFlags       = ulFlags;
  521.                         *ppMimeStr     = pMimeStr;
  522.                         (*ppMimeStr)->AddRef();
  523.                         // XXXMEH - this SHOULD come from the image header data,
  524.                         // instead of having it hard-coded in the renderer. Having it
  525.                         // hard-coded introduces codec<->renderer dependencies where
  526.                         // none should exist. However, the current wire format does
  527.                         // not have this information.
  528.                         rulOpaqueSize  = GetOpaqueSize((const char*) pMimeStr->GetBuffer());
  529.                     }
  530.                     HX_RELEASE(pMimeStr);
  531.                 }
  532.                 else
  533.                 {
  534.                     retVal = HXR_FAIL;
  535.                 }
  536.             }
  537.             else
  538.             {
  539.                 retVal = HXR_FAIL;
  540.             }
  541.         }
  542.         else
  543.         {
  544.             retVal = HXR_FAIL;
  545.         }
  546.         HX_RELEASE(pBuffer);
  547.     }
  548.     else
  549.     {
  550.         retVal = HXR_INVALID_PARAMETER;
  551.     }
  552.     return retVal;
  553. }
  554. HX_RESULT PXWireFormatManager::SetImageHeaderInfo(UINT32 ulHandle, UINT32 ulFileLength, UINT32 ulFlags,
  555.                                                   const char* pszMime, UINT32 ulTimeStamp, REF(IHXPacket*) rpPacket)
  556. {
  557.     HX_RESULT retVal = HXR_FAIL;
  558.     if (pszMime)
  559.     {
  560.         IHXBuffer* pMimeStr = NULL;
  561.         retVal               = SetString(pszMime, pMimeStr);
  562.         if (SUCCEEDED(retVal))
  563.         {
  564.             retVal = SetImageHeaderInfo(ulHandle, ulFileLength, ulFlags, pMimeStr, ulTimeStamp, rpPacket);
  565.         }
  566.         HX_RELEASE(pMimeStr);
  567.     }
  568.     return retVal;
  569. }
  570. HX_RESULT PXWireFormatManager::SetImageHeaderInfo(UINT32 ulHandle, UINT32 ulFileLength, UINT32 ulFlags,
  571.                                                   IHXBuffer* pMimeStr, UINT32 ulTimeStamp, REF(IHXPacket*) rpPacket)
  572. {
  573.     HX_RESULT retVal = HXR_FAIL;
  574.     if (ulHandle && ulFileLength && pMimeStr)
  575.     {
  576.         // Create an IHXBuffer
  577.         IHXBuffer* pBuffer = NULL;
  578.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  579.                                                                     (void**) &pBuffer);
  580.         if (SUCCEEDED(retVal))
  581.         {
  582.             // Compute the size
  583.             UINT32 ulSize = 0;
  584.             ulSize       += 16; // packet type, handle, length, flags
  585.             ulSize       += 2;  // mime type string length
  586.             UINT32 ulLen  = strlen((const char*) pMimeStr->GetBuffer());
  587.             if (ulLen)
  588.             {
  589.                 ulSize += ulLen + 1;
  590.             }
  591.             // Set the size of the buffer
  592.             retVal = pBuffer->SetSize(ulSize);
  593.             if (SUCCEEDED(retVal))
  594.             {
  595.                 // Pack the buffer
  596.                 BYTE* pBuf = pBuffer->GetBuffer();
  597.                 Pack32(pBuf,           kPacketTypeImageHeader);
  598.                 Pack32(pBuf,           ulHandle);
  599.                 Pack32(pBuf,           ulFileLength);
  600.                 Pack32(pBuf,           ulFlags);
  601.                 PackStringBuffer(pBuf, pMimeStr);
  602.                 // Create an IHXPacket
  603.                 IHXPacket* pPacket = NULL;
  604.                 retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket,
  605.                                                                             (void**) &pPacket);
  606.                 if (SUCCEEDED(retVal))
  607.                 {
  608.                     retVal = pPacket->Set(pBuffer,           // opaque data
  609.                                           ulTimeStamp,       // time stamp
  610.                                           0,                 // stream 0
  611.                                           HX_ASM_SWITCH_ON, // ASM flag
  612.                                           1);                // ASM rule 1
  613.                     if (SUCCEEDED(retVal))
  614.                     {
  615.                         // Assign to out parameter
  616.                         HX_RELEASE(rpPacket);
  617.                         rpPacket = pPacket;
  618.                         rpPacket->AddRef();
  619.                     }
  620.                 }
  621.                 HX_RELEASE(pPacket);
  622.             }
  623.         }
  624.         HX_RELEASE(pBuffer);
  625.     }
  626.     return retVal;
  627. }
  628. HX_RESULT PXWireFormatManager::GetImageDataHandle(IHXPacket* pPacket, REF(UINT32) rulHandle)
  629. {
  630.     HX_RESULT retVal = HXR_FAIL;
  631.     if (pPacket)
  632.     {
  633.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  634.         if (pBuffer)
  635.         {
  636.             BYTE* pBuf = pBuffer->GetBuffer();
  637.             if (pBuf)
  638.             {
  639.                 // Make sure this is an image data packet
  640.                 UINT32 ulType = 0xFFFFFFFF;
  641.                 UnPack32(pBuf, ulType);
  642.                 if (ulType == kPacketTypeImageData)
  643.                 {
  644.                     // Unpack the handle
  645.                     UnPack32(pBuf, rulHandle);
  646.                     retVal = HXR_OK;
  647.                 }
  648.             }
  649.         }
  650.         HX_RELEASE(pBuffer);
  651.     }
  652.     return retVal;
  653. }
  654. HX_RESULT PXWireFormatManager::GetImageDataInfo(IHXPacket* pPacket, UINT32 ulOpaqueSize,
  655.                                                 REF(IHXBuffer*) rpOpaque, REF(IHXBuffer*) rpData)
  656. {
  657.     HX_RESULT retVal = HXR_FAIL;
  658.     if (pPacket)
  659.     {
  660.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  661.         if (pBuffer)
  662.         {
  663.             BYTE* pBuf = pBuffer->GetBuffer();
  664.             if (pBuf)
  665.             {
  666.                 // Make sure this is an image data packet
  667.                 UINT32 ulType = 0xFFFFFFFF;
  668.                 UnPack32(pBuf, ulType);
  669.                 if (ulType == kPacketTypeImageData)
  670.                 {
  671.                     // Create a nested buffer class
  672.                     CHXNestedBuffer *pOpaque = NULL;
  673.                     retVal = CHXNestedBuffer::CreateObject(&pOpaque);
  674.                     if (SUCCEEDED(retVal))
  675.                     {
  676.                         // Addref the object
  677.                         pOpaque->AddRef();
  678.                         // Init the object
  679.                         retVal = pOpaque->Init(pBuffer, 8, ulOpaqueSize);
  680.                         if (SUCCEEDED(retVal))
  681.                         {
  682.                             // Create a nested buffer class
  683.                             CHXNestedBuffer *pData = NULL;
  684.                             retVal = CHXNestedBuffer::CreateObject(&pData);
  685.                             if (SUCCEEDED(retVal))
  686.                             {
  687.                                 // Addref the object
  688.                                 pData->AddRef();
  689.                                 // Init the object
  690.                                 UINT32 ulDataOffset = 8 + ulOpaqueSize;
  691.                                 retVal = pData->Init(pBuffer, ulDataOffset, pBuffer->GetSize() - ulDataOffset);
  692.                                 if (SUCCEEDED(retVal))
  693.                                 {
  694.                                     // Assign the out parameters
  695.                                     HX_RELEASE(rpOpaque);
  696.                                     rpOpaque = pOpaque;
  697.                                     rpOpaque->AddRef();
  698.                                     HX_RELEASE(rpData);
  699.                                     rpData   = pData;
  700.                                     rpData->AddRef();
  701.                                     retVal = HXR_OK;
  702.                                 }
  703.                             }
  704.                             HX_RELEASE(pData);
  705.                         }
  706.                     }
  707.                     HX_RELEASE(pOpaque);
  708.                 }
  709.             }
  710.         }
  711.         HX_RELEASE(pBuffer);
  712.     }
  713.     return retVal;
  714. }
  715. HX_RESULT PXWireFormatManager::SetImageDataInfo(UINT32 ulHandle, IHXBuffer* pData, IHXBuffer* pOpaque,
  716.                                                 UINT32 ulPacketIndex, UINT32 ulTimeStamp, BOOL bRequired,
  717.                                                 REF(IHXPacket*) rpPacket)
  718. {
  719.     HX_RESULT retVal = HXR_FAIL;
  720.     if (ulHandle && pData)
  721.     {
  722.         // Create an IHXBuffer
  723.         IHXBuffer* pBuffer = NULL;
  724.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  725.                                                                     (void**) &pBuffer);
  726.         if (SUCCEEDED(retVal))
  727.         {
  728.             // Compute the size
  729. // XXXMEH - move seq num into opaque temporarily
  730. //            UINT32 ulSize = 12; // packet type, handle, seq num
  731.             UINT32 ulSize = 8; // packet type, handle
  732.             if (pOpaque)
  733.             {
  734.                 ulSize += pOpaque->GetSize();
  735.             }
  736.             ulSize += pData->GetSize();
  737.             // Set the size of the buffer
  738.             retVal = pBuffer->SetSize(ulSize);
  739.             if (SUCCEEDED(retVal))
  740.             {
  741.                 // Pack the buffer
  742.                 //
  743.                 // First we pack the generic (codec-agnostic) header
  744.                 BYTE* pBuf = pBuffer->GetBuffer();
  745.                 Pack32(pBuf, kPacketTypeImageData);
  746.                 Pack32(pBuf, ulHandle);
  747. // XXXMEH - move seq num into opaque temporarily
  748. //                Pack32(pBuf, ulPacketIndex);
  749.                 // Now we pack the codec-specific per-packet opaque data
  750.                 if (pOpaque)
  751.                 {
  752.                     memcpy(pBuf, pOpaque->GetBuffer(), pOpaque->GetSize()); /* Flawfinder: ignore */
  753.                     pBuf += pOpaque->GetSize();
  754.                 }
  755.                 // Now we pack the codec-specific data straight from the file
  756.                 memcpy(pBuf, pData->GetBuffer(), pData->GetSize()); /* Flawfinder: ignore */
  757.                 pBuf += pData->GetSize();
  758.                 // Create an IHXPacket
  759.                 IHXPacket* pPacket = NULL;
  760.                 retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket,
  761.                                                                             (void**) &pPacket);
  762.                 if (SUCCEEDED(retVal))
  763.                 {
  764.                     retVal = pPacket->Set(pBuffer,              // opaque data
  765.                                           ulTimeStamp,          // time stamp
  766.                                           0,                    // stream 0
  767.                                           HX_ASM_SWITCH_ON,    // ASM flag
  768.                                           (bRequired ? 1 : 0)); // ASM rule (0 if not required, 1 if required)
  769.                     if (SUCCEEDED(retVal))
  770.                     {
  771.                         // Assign to out parameter
  772.                         HX_RELEASE(rpPacket);
  773.                         rpPacket = pPacket;
  774.                         rpPacket->AddRef();
  775.                     }
  776.                 }
  777.                 HX_RELEASE(pPacket);
  778.             }
  779.         }
  780.         HX_RELEASE(pBuffer);
  781.     }
  782.     return retVal;
  783. }
  784. HX_RESULT PXWireFormatManager::GetEffectType(IHXPacket* pPacket, REF(UINT32) rulType)
  785. {
  786.     HX_RESULT retVal = HXR_FAIL;
  787.     if (pPacket)
  788.     {
  789.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  790.         if (pBuffer)
  791.         {
  792.             BYTE* pBuf = pBuffer->GetBuffer();
  793.             if (pBuf)
  794.             {
  795.                 UINT32 ulPacketType = 0;
  796.                 UnPack32(pBuf, ulPacketType);
  797.                 if (ulPacketType == PXWireFormatManager::kPacketTypeEffect)
  798.                 {
  799.                     UINT32 ulFlags      = 0;
  800.                     UINT32 ulEffectType = 0;
  801.                     UnPack32(pBuf, ulFlags);
  802.                     UnPack32(pBuf, rulType);
  803.                     retVal = HXR_OK;
  804.                 }
  805.             }
  806.         }
  807.         HX_RELEASE(pBuffer);
  808.     }
  809.     return retVal;
  810. }
  811. HX_RESULT PXWireFormatManager::GetEffectInfo(IHXPacket* pPacket, PXEffect** ppEffect)
  812. {
  813.     HX_RESULT retVal = HXR_OK;
  814.     if (pPacket && ppEffect)
  815.     {
  816.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  817.         if (pBuffer)
  818.         {
  819.             BYTE* pBuf = pBuffer->GetBuffer();
  820.             if (pBuf)
  821.             {
  822.                 UINT32 ulType = 0xFFFFFFFF;
  823.                 UnPack32(pBuf, ulType);
  824.                 if (ulType == kPacketTypeEffect)
  825.                 {
  826.                     // Create an effect object
  827.                     PXEffect* pEffect = NULL;
  828.                     retVal            = PXEffect::CreateObject(&pEffect);
  829.                     if (SUCCEEDED(retVal))
  830.                     {
  831.                         // AddRef the object
  832.                         pEffect->AddRef();
  833.                         // Initialize it from the opaque data
  834.                         pBuf -= 4; // back up to the beginning
  835.                         pEffect->UnPack(pBuf, HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  836.                                               HX_GET_MINOR_VERSION(m_ulStreamVersion));
  837.                         if (SUCCEEDED(retVal))
  838.                         {
  839.                             // Assign the out parameter
  840.                             *ppEffect = pEffect;
  841.                             (*ppEffect)->AddRef();
  842.                         }
  843.                     }
  844.                     // Release our ref on the packet
  845.                     HX_RELEASE(pEffect);
  846.                 }
  847.                 else
  848.                 {
  849.                     retVal = HXR_FAIL;
  850.                 }
  851.             }
  852.             else
  853.             {
  854.                 retVal = HXR_FAIL;
  855.             }
  856.         }
  857.         else
  858.         {
  859.             retVal = HXR_FAIL;
  860.         }
  861.         HX_RELEASE(pBuffer);
  862.     }
  863.     else
  864.     {
  865.         retVal = HXR_INVALID_PARAMETER;
  866.     }
  867.     return retVal;
  868. }
  869. HX_RESULT PXWireFormatManager::SetEffectPacketParameters(IHXPacket* pPacket,
  870. UINT32 ulEffectType, UINT32 ulStart, UINT32 ulDuration)
  871. {
  872.     HX_RESULT retVal = HXR_FAIL;
  873.     if (pPacket)
  874.     {
  875.         IHXBuffer* pOldBuffer    = NULL;
  876.         UINT32      ulOldTime     = 0;
  877.         UINT16      usOldStrNum   = 0;
  878.         UINT8       ucOldASMFlags = 0;
  879.         UINT16      usOldRuleNum  = 0;
  880.         retVal = pPacket->Get(pOldBuffer, ulOldTime, usOldStrNum, ucOldASMFlags,
  881.     usOldRuleNum);
  882.         if (SUCCEEDED(retVal))
  883.         {
  884.             PXEffect* pEffect = NULL;
  885.             retVal            = PXEffect::CreateObject(&pEffect);
  886.             if (SUCCEEDED(retVal))
  887.             {
  888. pEffect->AddRef();
  889.                 BYTE* pBuf = pOldBuffer->GetBuffer();
  890.                 pEffect->UnPack(pBuf,
  891.                                 HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  892.                                 HX_GET_MINOR_VERSION(m_ulStreamVersion));
  893.                 // Update the parameters
  894.                 pEffect->SetEffectType((BYTE) ulEffectType);
  895.                 pEffect->SetStart(ulStart);
  896.                 pEffect->SetDuration(ulDuration);
  897.                 // Get a new buffer for it
  898.                 IHXBuffer* pNewBuffer = NULL;
  899.                 retVal                 = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  900.                                                                                (void**) &pNewBuffer);
  901.                 if (SUCCEEDED(retVal))
  902.                 {
  903.                     // Set its size
  904.                     retVal = pNewBuffer->SetSize(pEffect->PackedSize(HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  905.                                                                      HX_GET_MINOR_VERSION(m_ulStreamVersion)));
  906.                     if (SUCCEEDED(retVal))
  907.                     {
  908.                         // Pack the new effect
  909.                         BYTE* pBuf = pNewBuffer->GetBuffer();
  910.                         pEffect->Pack(pBuf,
  911.                                       HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  912.                                       HX_GET_MINOR_VERSION(m_ulStreamVersion));
  913.                         // Set the new buffer into the packet
  914.                         retVal = pPacket->Set(pNewBuffer, ulOldTime, usOldStrNum,
  915.                                               ucOldASMFlags, usOldRuleNum);
  916.                     }
  917.                 }
  918.                 HX_RELEASE(pNewBuffer);
  919.             }
  920.             HX_RELEASE(pEffect);
  921.         }
  922.         HX_RELEASE(pOldBuffer);
  923.     }
  924.     return retVal;
  925. }
  926. HX_RESULT PXWireFormatManager::SetEffectInfo(PXEffect* pEffect, UINT32 ulTimeStamp, REF(IHXPacket*) rpPacket)
  927. {
  928.     HX_RESULT retVal = HXR_FAIL;
  929.     if (pEffect)
  930.     {
  931.         // Create an IHXBuffer
  932.         IHXBuffer* pBuffer = NULL;
  933.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  934.                                                                     (void**) &pBuffer);
  935.         if (SUCCEEDED(retVal))
  936.         {
  937.             // Compute the size
  938.             UINT32 ulSize = pEffect->PackedSize(HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  939.                                                 HX_GET_MINOR_VERSION(m_ulStreamVersion));
  940.             // Set the size of the buffer
  941.             retVal = pBuffer->SetSize(ulSize);
  942.             if (SUCCEEDED(retVal))
  943.             {
  944.                 // Pack the buffer
  945.                 BYTE* pBuf = pBuffer->GetBuffer();
  946.                 pEffect->Pack(pBuf,
  947.                               HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  948.                               HX_GET_MINOR_VERSION(m_ulStreamVersion));
  949.                 // Create an IHXPacket
  950.                 IHXPacket* pPacket = NULL;
  951.                 retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket,
  952.                                                                             (void**) &pPacket);
  953.                 if (SUCCEEDED(retVal))
  954.                 {
  955.                     retVal = pPacket->Set(pBuffer,           // opaque data
  956.                                           ulTimeStamp,       // time stamp
  957.                                           0,                 // stream 0
  958.                                           HX_ASM_SWITCH_ON, // ASM flag
  959.                                           1);                // ASM rule 1
  960.                     if (SUCCEEDED(retVal))
  961.                     {
  962.                         // Assign to out parameter
  963.                         HX_RELEASE(rpPacket);
  964.                         rpPacket = pPacket;
  965.                         rpPacket->AddRef();
  966.                     }
  967.                 }
  968.                 HX_RELEASE(pPacket);
  969.             }
  970.         }
  971.         HX_RELEASE(pBuffer);
  972.     }
  973.     return retVal;
  974. }
  975. HX_RESULT PXWireFormatManager::SetCookieInfo(UINT32 ulNumCookies, IHXBuffer** ppURL, IHXBuffer** ppCookie,
  976.                                              UINT32 ulTimeStamp, REF(IHXPacket*) rpPacket)
  977. {
  978.     HX_RESULT retVal = HXR_OK;
  979.     if (ulNumCookies && ppURL && ppCookie)
  980.     {
  981.         // Compute size of packet
  982.         UINT32 ulSize = 8;
  983.         UINT32 i      = 0;
  984.         for (i = 0; i < ulNumCookies; i++)
  985.         {
  986.             if (ppURL[i] && ppCookie[i])
  987.             {
  988.                 ulSize += 4;                      // Size of URL
  989.                 ulSize += ppURL[i]->GetSize();    // URL
  990.                 ulSize += 4;                      // Size of Set-Cookie
  991.                 ulSize += ppCookie[i]->GetSize(); // Set-Cookie
  992.             }
  993.             else
  994.             {
  995.                 retVal = HXR_FAIL;
  996.             }
  997.         }
  998.         if (SUCCEEDED(retVal))
  999.         {
  1000.             // Create opaque data buffer
  1001.             IHXBuffer* pBuffer = NULL;
  1002.             retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer, (void**) &pBuffer);
  1003.             if (SUCCEEDED(retVal))
  1004.             {
  1005.                 // Set its size
  1006.                 retVal = pBuffer->SetSize(ulSize);
  1007.                 if (SUCCEEDED(retVal))
  1008.                 {
  1009.                     // Pack the buffer
  1010.                     BYTE* pBuf = pBuffer->GetBuffer();
  1011.                     Pack32(pBuf, kPacketTypeCookie);
  1012.                     Pack32(pBuf, ulNumCookies);
  1013.                     for (i = 0; i < ulNumCookies; i++)
  1014.                     {
  1015.                         Pack32(pBuf, ppURL[i]->GetSize());
  1016.                         memcpy(pBuf, ppURL[i]->GetBuffer(), ppURL[i]->GetSize()); /* Flawfinder: ignore */
  1017.                         pBuf += ppURL[i]->GetSize();
  1018.                         Pack32(pBuf, ppCookie[i]->GetSize());
  1019.                         memcpy(pBuf, ppCookie[i]->GetBuffer(), ppCookie[i]->GetSize()); /* Flawfinder: ignore */
  1020.                         pBuf += ppCookie[i]->GetSize();
  1021.                     }
  1022.                     // Create an IHXPacket object
  1023.                     IHXPacket* pPacket = NULL;
  1024.                     retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket, (void**) &pPacket);
  1025.                     if (SUCCEEDED(retVal))
  1026.                     {
  1027.                         // Set the packet parameters
  1028.                         retVal = pPacket->Set(pBuffer,           // opaque data
  1029.                                               ulTimeStamp,       // time stamp
  1030.                                               0,                 // stream 0
  1031.                                               HX_ASM_SWITCH_ON, // ASM flag
  1032.                                               0);                // ASM rule 0
  1033.                         if (SUCCEEDED(retVal))
  1034.                         {
  1035.                             // Assign the out parameters
  1036.                             HX_RELEASE(rpPacket);
  1037.                             rpPacket = pPacket;
  1038.                             rpPacket->AddRef();
  1039.                         }
  1040.                     }
  1041.                     HX_RELEASE(pPacket);
  1042.                 }
  1043.             }
  1044.             HX_RELEASE(pBuffer);
  1045.         }
  1046.     }
  1047.     else
  1048.     {
  1049.         retVal = HXR_FAIL;
  1050.     }
  1051.     return retVal;
  1052. }
  1053. HX_RESULT PXWireFormatManager::GetNumCookies(IHXPacket* pPacket, REF(UINT32) rulNumCookies)
  1054. {
  1055.     HX_RESULT retVal = HXR_FAIL;
  1056.     if (pPacket)
  1057.     {
  1058.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  1059.         if (pBuffer)
  1060.         {
  1061.             BYTE*  pBuf         = pBuffer->GetBuffer();
  1062.             UINT32 ulPacketType = 0;
  1063.             UINT32 ulNumCookies = 0;
  1064.             UnPack32(pBuf, ulPacketType);
  1065.             UnPack32(pBuf, ulNumCookies);
  1066.             if (ulPacketType == kPacketTypeCookie)
  1067.             {
  1068.                 retVal        = HXR_OK;
  1069.                 rulNumCookies = ulNumCookies;
  1070.             }
  1071.         }
  1072.         HX_RELEASE(pBuffer);
  1073.     }
  1074.     return retVal;
  1075. }
  1076. HX_RESULT PXWireFormatManager::GetCookie(IHXPacket* pPacket, UINT32 ulIndex,
  1077.                                          REF(IHXBuffer*) rpURL, REF(IHXBuffer*) rpBuffer)
  1078. {
  1079.     HX_RESULT retVal = HXR_FAIL;
  1080.     if (pPacket)
  1081.     {
  1082.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  1083.         if (pBuffer)
  1084.         {
  1085.             BYTE*  pBuf         = pBuffer->GetBuffer();
  1086.             UINT32 ulPacketType = 0;
  1087.             UINT32 ulNumCookies = 0;
  1088.             UnPack32(pBuf, ulPacketType);
  1089.             UnPack32(pBuf, ulNumCookies);
  1090.             if (ulPacketType == kPacketTypeCookie &&
  1091.                 ulIndex      <  ulNumCookies)
  1092.             {
  1093.                 // Advance up to the proper cookie
  1094.                 UINT32 ulURLSize    = 0;
  1095.                 UINT32 ulCookieSize = 0;
  1096.                 for (UINT32 i = 0; i < ulIndex; i++)
  1097.                 {
  1098.                     UnPack32(pBuf, ulURLSize);
  1099.                     pBuf += ulURLSize;
  1100.                     UnPack32(pBuf, ulCookieSize);
  1101.                     pBuf += ulCookieSize;
  1102.                 }
  1103.                 // Get the size of the URL
  1104.                 UnPack32(pBuf, ulURLSize);
  1105.                 // Get the offset of the URL
  1106.                 UINT32 ulURLOffset = pBuf - pBuffer->GetBuffer();
  1107.                 // Advance to the cookie
  1108.                 pBuf += ulURLSize;
  1109.                 // Create a nested buffer class
  1110.                 CHXNestedBuffer *pNestedURLBuffer = NULL;
  1111.                 retVal = CHXNestedBuffer::CreateObject(&pNestedURLBuffer);
  1112.                 if (SUCCEEDED(retVal))
  1113.                 {
  1114.                     // Addref the object
  1115.                     pNestedURLBuffer->AddRef();
  1116.                     // Init the object
  1117.                     retVal = pNestedURLBuffer->Init(pBuffer, ulURLOffset, ulURLSize);
  1118.                     if (SUCCEEDED(retVal))
  1119.                     {
  1120.                         // Get the size of the cookie
  1121.                         UnPack32(pBuf, ulCookieSize);
  1122.                         // Get the offset of this cookie
  1123.                         UINT32 ulCookieOffset = pBuf - pBuffer->GetBuffer();
  1124.                         // Create a nested buffer class
  1125.                         CHXNestedBuffer *pNestedCookieBuffer = NULL;
  1126.                         retVal = CHXNestedBuffer::CreateObject(&pNestedCookieBuffer);
  1127.                         if (SUCCEEDED(retVal))
  1128.                         {
  1129.                             // Addref the object
  1130.                             pNestedCookieBuffer->AddRef();
  1131.                             // Init the object
  1132.                             retVal = pNestedCookieBuffer->Init(pBuffer, ulCookieOffset, ulCookieSize);
  1133.                             if (SUCCEEDED(retVal))
  1134.                             {
  1135.                                 // Assign the nested buffer to the out parameter
  1136.                                 HX_RELEASE(rpURL);
  1137.                                 retVal = pNestedURLBuffer->QueryInterface(IID_IHXBuffer, (void**) &rpURL);
  1138.                                 if (SUCCEEDED(retVal))
  1139.                                 {
  1140.                                     HX_RELEASE(rpBuffer);
  1141.                                     retVal = pNestedCookieBuffer->QueryInterface(IID_IHXBuffer, (void**) &rpBuffer);
  1142.                                 }
  1143.                             }
  1144.                         }
  1145.                         HX_RELEASE(pNestedCookieBuffer);
  1146.                     }
  1147.                 }
  1148.                 HX_RELEASE(pNestedURLBuffer);
  1149.             }
  1150.         }
  1151.         HX_RELEASE(pBuffer);
  1152.     }
  1153.     return retVal;
  1154. }
  1155. HX_RESULT PXWireFormatManager::SetNoOpInfo(UINT32 ulSize, UINT32 ulTimeStamp, REF(IHXPacket*) rpPacket)
  1156. {
  1157.     HX_RESULT retVal = HXR_FAIL;
  1158.     if (ulSize >= 4)
  1159.     {
  1160.         IHXBuffer* pBuffer = NULL;
  1161.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer, (void**) &pBuffer);
  1162.         if (SUCCEEDED(retVal))
  1163.         {
  1164.             retVal = pBuffer->SetSize(ulSize);
  1165.             if (SUCCEEDED(retVal))
  1166.             {
  1167.                 BYTE* pBuf = pBuffer->GetBuffer();
  1168.                 Pack32(pBuf, 0xFFFFFFFF);
  1169.                 
  1170.                 IHXPacket* pPacket = NULL;
  1171.                 retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket, (void**) &pPacket);
  1172.                 if (SUCCEEDED(retVal))
  1173.                 {
  1174.                     // Set the packet parameters
  1175.                     retVal = pPacket->Set(pBuffer,           // opaque data
  1176.                                           ulTimeStamp,       // time stamp
  1177.                                           0,                 // stream 0
  1178.                                           HX_ASM_SWITCH_ON, // ASM flag
  1179.                                           0);                // ASM rule 0
  1180.                     if (SUCCEEDED(retVal))
  1181.                     {
  1182.                         // Assign the out parameters
  1183.                         HX_RELEASE(rpPacket);
  1184.                         rpPacket = pPacket;
  1185.                         rpPacket->AddRef();
  1186.                     }
  1187.                 }
  1188.                 HX_RELEASE(pPacket);
  1189.             }
  1190.         }
  1191.         HX_RELEASE(pBuffer);
  1192.     }
  1193.     return retVal;
  1194. }
  1195. HX_RESULT PXWireFormatManager::GetBackChannelInfo(IHXPacket* pPacket, REF(UINT32) rulHandle,
  1196.                                                   REF(UINT32) rulTimeStopped)
  1197. {
  1198.     HX_RESULT retVal = HXR_FAIL;
  1199.     if (pPacket)
  1200.     {
  1201.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  1202.         if (pBuffer)
  1203.         {
  1204.             BYTE* pBuf = pBuffer->GetBuffer();
  1205.             if (pBuf)
  1206.             {
  1207.                 UINT32 ulOpCode = UnPackUINT32(pBuf);
  1208.                 if (ulOpCode == 0x34414453) // "4ADS"
  1209.                 {
  1210.                     rulHandle      = UnPackUINT32(pBuf);
  1211.                     rulTimeStopped = UnPackUINT32(pBuf);
  1212.                     retVal         = HXR_OK;
  1213.                 }
  1214.             }
  1215.         }
  1216.         HX_RELEASE(pBuffer);
  1217.     }
  1218.     return retVal;
  1219. }
  1220. HX_RESULT PXWireFormatManager::SetBackChannelInfo(UINT32 ulHandle, UINT32 ulTimeStopped,
  1221.                                                   REF(IHXPacket*) rpPacket)
  1222. {
  1223.     HX_RESULT retVal = HXR_OK;
  1224.     if (ulHandle)
  1225.     {
  1226.         IHXBuffer* pBuffer = NULL;
  1227.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  1228.                                                                     (void**) &pBuffer);
  1229.         if (SUCCEEDED(retVal))
  1230.         {
  1231.             retVal = pBuffer->SetSize(12);
  1232.             if (SUCCEEDED(retVal))
  1233.             {
  1234.                 // Pack the opaque buffer
  1235.                 BYTE* pBuf = pBuffer->GetBuffer();
  1236.                 PackUINT32(pBuf, 0x34414453);
  1237.                 PackUINT32(pBuf, ulHandle);
  1238.                 PackUINT32(pBuf, ulTimeStopped);
  1239.                 // Create the packet
  1240.                 IHXPacket* pPacket = NULL;
  1241.                 retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket,
  1242.                                                                             (void**) &pPacket);
  1243.                 if (SUCCEEDED(retVal))
  1244.                 {
  1245.                     // Set the packet parameters
  1246.                     retVal = pPacket->Set(pBuffer,            // opaque data
  1247.                                           0,                  // time stamp
  1248.                                           0,                  // stream 0
  1249.                                           HX_ASM_SWITCH_OFF, // ASM flag
  1250.                                           0);                 // 
  1251.                     if (SUCCEEDED(retVal))
  1252.                     {
  1253.                         // Pass the packet back
  1254.                         HX_RELEASE(rpPacket);
  1255.                         rpPacket = pPacket;
  1256.                         rpPacket->AddRef();
  1257.                     }
  1258.                 }
  1259.                 HX_RELEASE(pPacket);
  1260.             }
  1261.         }
  1262.         HX_RELEASE(pBuffer);
  1263.     }
  1264.     else
  1265.     {
  1266.         retVal = HXR_INVALID_PARAMETER;
  1267.     }
  1268.     return retVal;
  1269. }
  1270. UINT32 PXWireFormatManager::GetEffectWireSize(PXEffect* pEffect)
  1271. {
  1272.     UINT32 ulRet = 0;
  1273.     if (pEffect)
  1274.     {
  1275.         ulRet += pEffect->PackedSize(HX_GET_MAJOR_VERSION(m_ulStreamVersion),
  1276.                                      HX_GET_MINOR_VERSION(m_ulStreamVersion));
  1277.         ulRet += kIHXPacketOverhead;
  1278.     }
  1279.     return ulRet;
  1280. }
  1281. UINT32 PXWireFormatManager::GetImageWireSize(IHXBuffer* pStreamMimeStr, UINT32 ulImageFileSize)
  1282. {
  1283.     UINT32 ulRet = 0;
  1284.     if (pStreamMimeStr && ulImageFileSize)
  1285.     {
  1286.         // First we add the image header packet
  1287.         //
  1288.         // Add fixed size of image header packet
  1289.         ulRet += 18;
  1290.         // Add stream mime type string length of image header packet
  1291.         UINT32 ulMimeLen = strlen((const char*) pStreamMimeStr->GetBuffer());
  1292.         if (ulMimeLen)
  1293.         {
  1294.             ulRet += ulMimeLen + 1;
  1295.         }
  1296.         // Add the IHXPacket overhead of the image header packet
  1297.         ulRet += kIHXPacketOverhead;
  1298.         // Now we make a worst-case estimate of the number of packets
  1299.         // for this image.
  1300.         UINT32 ulWorstCaseNumPackets = (ulImageFileSize + kMinImageDataPacketSize - 1) / kMinImageDataPacketSize;
  1301.         // Now, based on this estimated number of packets we can
  1302.         // compute the overhead
  1303.         UINT32 ulWorstCaseOverhead   = (kIHXPacketOverhead + 8 + GetOpaqueSize((const char*) pStreamMimeStr->GetBuffer())) *
  1304.                                        ulWorstCaseNumPackets;
  1305.         // Now add the wire size for the image data packets - this
  1306.         // is the file size plus the worst case overhead
  1307.         ulRet += ulImageFileSize;
  1308.         ulRet += ulWorstCaseOverhead;
  1309.     }
  1310.     return ulRet;
  1311. }
  1312. UINT32 PXWireFormatManager::GetPacketSize(IHXPacket* pPacket)
  1313. {
  1314.     UINT32 ulSize = 0;
  1315.     if (pPacket)
  1316.     {
  1317.         IHXBuffer* pBuffer = pPacket->GetBuffer();
  1318.         if (pBuffer)
  1319.         {
  1320.             ulSize = pBuffer->GetSize();
  1321.         }
  1322.         HX_RELEASE(pBuffer);
  1323.     }
  1324.     return ulSize;
  1325. }
  1326. UINT32 PXWireFormatManager::GetOpaqueSize(const char* pszMimeType)
  1327. {
  1328.     UINT32 ulRetVal = kDefaultOpaqueSize;
  1329.     if (m_pMapMimeToOpaqueSize)
  1330.     {
  1331.         void *pVoid = NULL;
  1332.         if (m_pMapMimeToOpaqueSize->Lookup(pszMimeType, pVoid))
  1333.         {
  1334.             ulRetVal = (UINT32) pVoid;
  1335.         }
  1336.     }
  1337.     return ulRetVal;
  1338. }
  1339. void PXWireFormatManager::ReleaseAllCodecMimes()
  1340. {
  1341.     if (m_pCodecMimeList)
  1342.     {
  1343.         LISTPOSITION pos = m_pCodecMimeList->GetHeadPosition();
  1344.         while (pos)
  1345.         {
  1346.             IHXBuffer* pBuffer = (IHXBuffer*) m_pCodecMimeList->GetNext(pos);
  1347.             HX_RELEASE(pBuffer);
  1348.         }
  1349.         m_pCodecMimeList->RemoveAll();
  1350.     }
  1351. }
  1352. void PXWireFormatManager::ReleaseAllFXPackageMimes()
  1353. {
  1354.     if (m_pFXPackageMimeList)
  1355.     {
  1356.         LISTPOSITION pos = m_pFXPackageMimeList->GetHeadPosition();
  1357.         while (pos)
  1358.         {
  1359.             IHXBuffer* pBuffer = (IHXBuffer*) m_pFXPackageMimeList->GetNext(pos);
  1360.             HX_RELEASE(pBuffer);
  1361.         }
  1362.         m_pFXPackageMimeList->RemoveAll();
  1363.     }
  1364. }
  1365. void PXWireFormatManager::Deallocate()
  1366. {
  1367.     HX_RELEASE(m_pContext);
  1368.     HX_RELEASE(m_pCommonClassFactory);
  1369.     HX_RELEASE(m_pTitleStr);
  1370.     HX_RELEASE(m_pAuthorStr);
  1371.     HX_RELEASE(m_pCopyrightStr);
  1372.     HX_RELEASE(m_pDefaultURLStr);
  1373.     ReleaseAllCodecMimes();
  1374.     HX_DELETE(m_pCodecMimeList);
  1375.     ReleaseAllFXPackageMimes();
  1376.     HX_DELETE(m_pFXPackageMimeList);
  1377.     HX_DELETE(m_pMapMimeToOpaqueSize);
  1378.     HX_RELEASE(m_pASMRuleBook);
  1379.     HX_RELEASE(m_pStreamMimeType);
  1380. }
  1381. void PXWireFormatManager::Reset()
  1382. {
  1383.     m_ulStreamVersion     = HX_ENCODE_PROD_VERSION(0, 0, 0, 0);
  1384.     m_ulStart             = 0;
  1385.     m_ulDuration          = 0;
  1386.     m_bIsLive             = FALSE;
  1387.     m_bMinimizeLatency    = FALSE;
  1388.     m_ulPreroll           = 0;
  1389.     m_bPrerollAfterSeek   = TRUE;
  1390.     m_ulPreData           = 0;
  1391.     m_bPreDataAtStart     = TRUE;
  1392.     m_ulBitrate           = 0;
  1393.     m_ulDisplayWidth      = 0;
  1394.     m_ulDisplayHeight     = 0;
  1395.     m_bDefaultAspectFlag  = TRUE;
  1396.     m_ulDefaultMaxFps     = 0;
  1397.     m_ulContentVersion    = HX_ENCODE_PROD_VERSION(0, 0, 0, 0);
  1398.     m_ulBackgroundColor   = 0x00000000;
  1399.     m_ulBackgroundOpacity = 255;
  1400.     m_pCodecListPos       = NULL;
  1401.     m_pFXPackageListPos   = NULL;
  1402.     m_ulRendererFlags     = 0;
  1403. }
  1404. UINT32 PXWireFormatManager::PackStreamHeader(BYTE*& rpBuf, BOOL bPack)
  1405. {
  1406.     UINT32 ulSize = 0;
  1407.     if (HX_GET_MAJOR_VERSION(m_ulStreamVersion) <=
  1408.         HX_GET_MAJOR_VERSION(m_ulHighestSupportedStreamVersion) &&
  1409.         HX_GET_MINOR_VERSION(m_ulStreamVersion) <=
  1410.         HX_GET_MINOR_VERSION(m_ulHighestSupportedStreamVersion))
  1411.     {
  1412.         // Pack/add display width and height
  1413.         ulSize += 8;
  1414.         if (bPack)
  1415.         {
  1416.             Pack32(rpBuf, m_ulDisplayWidth);
  1417.             Pack32(rpBuf, m_ulDisplayHeight);
  1418.         }
  1419.         
  1420.         // Pack/add size of default URL
  1421.         ulSize += 2; // default URL size
  1422.         if (m_pDefaultURLStr)
  1423.         {
  1424.             UINT32 ulLen = (UINT32) strlen((const char*) m_pDefaultURLStr->GetBuffer());
  1425.             if (ulLen > 0)
  1426.             {
  1427.                 ulSize += ulLen + 1;
  1428.             }
  1429.         }
  1430.         if (bPack)
  1431.         {
  1432.             PackStringBuffer(rpBuf, m_pDefaultURLStr);
  1433.         }
  1434.         // Pack/add total mime count
  1435.         ulSize += 4;
  1436.         if (bPack)
  1437.         {
  1438.             Pack32(rpBuf, m_pCodecMimeList->GetCount() + m_pFXPackageMimeList->GetCount());
  1439.         }
  1440.         // Pack/add codec mime count
  1441.         ulSize += 4;
  1442.         if (bPack)
  1443.         {
  1444.             Pack32(rpBuf, m_pCodecMimeList->GetCount());
  1445.         }
  1446.         // Pack/add all the codec mime strings
  1447.         if (m_pCodecMimeList->GetCount() > 0)
  1448.         {
  1449.             // Loop through codec mime list and pack/add up strings
  1450.             LISTPOSITION pos = m_pCodecMimeList->GetHeadPosition();
  1451.             while (pos)
  1452.             {
  1453.                 ulSize             += 2; // Add string size
  1454.                 IHXBuffer* pBuffer = (IHXBuffer*) m_pCodecMimeList->GetNext(pos);
  1455.                 if (pBuffer)
  1456.                 {
  1457.                     UINT32 ulLen = (UINT32) strlen((const char*) pBuffer->GetBuffer());
  1458.                     if (ulLen > 0)
  1459.                     {
  1460.                         ulSize += ulLen + 1;
  1461.                     }
  1462.                 }
  1463.                 if (bPack)
  1464.                 {
  1465.                     PackStringBuffer(rpBuf, pBuffer);
  1466.                 }
  1467.             }
  1468.         }
  1469.         // Pack/add fxpackage mime count
  1470.         ulSize += 4;
  1471.         if (bPack)
  1472.         {
  1473.             Pack32(rpBuf, m_pFXPackageMimeList->GetCount());
  1474.         }
  1475.         // Pack/add all the fxpackage mime strings
  1476.         if (m_pFXPackageMimeList->GetCount() > 0)
  1477.         {
  1478.             // Loop through the fxpackage mime list and pack/add the strings
  1479.             LISTPOSITION pos = m_pFXPackageMimeList->GetHeadPosition();
  1480.             while (pos)
  1481.             {
  1482.                 ulSize += 2; // Add string size
  1483.                 IHXBuffer* pBuffer = (IHXBuffer*) m_pFXPackageMimeList->GetNext(pos);
  1484.                 if (pBuffer)
  1485.                 {
  1486.                     UINT32 ulLen = (UINT32) strlen((const char*) pBuffer->GetBuffer());
  1487.                     if (ulLen > 0)
  1488.                     {
  1489.                         ulSize += ulLen + 1;
  1490.                     }
  1491.                 }
  1492.                 if (bPack)
  1493.                 {
  1494.                     PackStringBuffer(rpBuf, pBuffer);
  1495.                 }
  1496.             }
  1497.         }
  1498.         // Add the size of the background color, if the
  1499.         // content version/stream version is 1.1.0.0
  1500.         if (m_ulContentVersion >= U2_STREAM_VERSION)
  1501.         {
  1502.             ulSize += 4;
  1503.             if (bPack)
  1504.             {
  1505.                 Pack32(rpBuf, m_ulBackgroundColor);
  1506.             }
  1507.         }
  1508.     }
  1509.     return ulSize;
  1510. }
  1511. UINT32 PXWireFormatManager::UnPackUINT32(REF(BYTE*) rpBuffer)
  1512. {
  1513.     UINT32 ulRet = ((rpBuffer[0] << 24) & 0xFF000000) |
  1514.                    ((rpBuffer[1] << 16) & 0x00FF0000) |
  1515.                    ((rpBuffer[2] <<  8) & 0x0000FF00) |
  1516.                    ( rpBuffer[3]        & 0x000000FF);
  1517.     rpBuffer    += 4;
  1518.     return ulRet;
  1519. }
  1520. UINT32 PXWireFormatManager::UnPackUINT16(REF(BYTE*) rpBuffer)
  1521. {
  1522.     UINT32 ulRet = ((rpBuffer[0] << 8) & 0x0000FF00) |
  1523.                    ( rpBuffer[1]       & 0x000000FF);
  1524.     rpBuffer    += 2;
  1525.     return ulRet;
  1526. }
  1527. UINT32 PXWireFormatManager::UnPackUINT32_LE(REF(BYTE*) rpBuffer)
  1528. {
  1529.     UINT32 ulRet = ((rpBuffer[3] << 24) & 0xFF000000) |
  1530.                    ((rpBuffer[2] << 16) & 0x00FF0000) |
  1531.                    ((rpBuffer[1] <<  8) & 0x0000FF00) |
  1532.                    ( rpBuffer[0]        & 0x000000FF);
  1533.     rpBuffer    += 4;
  1534.     return ulRet;
  1535. }
  1536. UINT32 PXWireFormatManager::UnPackUINT16_LE(REF(BYTE*) rpBuffer)
  1537. {
  1538.     UINT32 ulRet = ((rpBuffer[1] << 8) & 0x0000FF00) |
  1539.                    ( rpBuffer[0]       & 0x000000FF);
  1540.     rpBuffer    += 2;
  1541.     return ulRet;
  1542. }
  1543. UINT32 PXWireFormatManager::UnPackBYTE(REF(BYTE*) rpBuffer)
  1544. {
  1545.     UINT32 ulRet = *rpBuffer++;
  1546.     return ulRet;
  1547. }
  1548. UINT32 PXWireFormatManager::UnPackVUINT(REF(BYTE*) rpBuffer)
  1549. {
  1550.     // Format of VUINT is:
  1551.     // 
  1552.     // 11bb bbbb  bbbb bbbb  bbbb bbbb  bbbb bbbb (value in range [0,0x3FFFFFFF])
  1553.     // 10bb bbbb  bbbb bbbb  (value in range [0,0x3FFF])
  1554.     // 0bbb bbbb (value in range [0,0x7F])
  1555.     //
  1556.     UINT32 ulRet       = 0;
  1557.     UINT32 ulFirstByte = rpBuffer[0];
  1558.     if (ulFirstByte & 0x00000080)
  1559.     {
  1560.         if (ulFirstByte & 0x00000040)
  1561.         {
  1562.             // This is a 4-byte value in range [0,0x3FFFFFFF]
  1563.             // but we have to mask out the highest two bits
  1564.             ulRet = UnPackUINT32(rpBuffer) & 0x3FFFFFFF;
  1565.         }
  1566.         else
  1567.         {
  1568.             // This is a 2-byte value in range [0,0x3FFF],
  1569.             // but we have to mask out the highest two bits
  1570.             ulRet = UnPackUINT16(rpBuffer) & 0x00003FFF;
  1571.         }
  1572.     }
  1573.     else
  1574.     {
  1575.         // This is a single byte value in range [0,0x7F]
  1576.         ulRet = UnPackBYTE(rpBuffer);
  1577.     }
  1578.     return ulRet;
  1579. }
  1580. void PXWireFormatManager::PackUINT32(REF(BYTE*) rpBuffer, UINT32 ulValue)
  1581. {
  1582.     rpBuffer[0] = (BYTE) ((ulValue >> 24) & 0x000000FF);
  1583.     rpBuffer[1] = (BYTE) ((ulValue >> 16) & 0x000000FF);
  1584.     rpBuffer[2] = (BYTE) ((ulValue >>  8) & 0x000000FF);
  1585.     rpBuffer[3] = (BYTE) ( ulValue        & 0x000000FF);
  1586.     rpBuffer   += 4;
  1587. }
  1588. void PXWireFormatManager::PackUINT16(REF(BYTE*) rpBuffer, UINT32 ulValue)
  1589. {
  1590.     HX_ASSERT(ulValue <= 0x0000FFFF);
  1591.     rpBuffer[0] = (BYTE) ((ulValue >> 8) & 0x000000FF);
  1592.     rpBuffer[1] = (BYTE) ( ulValue       & 0x000000FF);
  1593.     rpBuffer   += 2;
  1594. }
  1595. void PXWireFormatManager::PackBYTE(REF(BYTE*) rpBuffer, UINT32 ulValue)
  1596. {
  1597.     HX_ASSERT(ulValue <= 0x000000FF);
  1598.     rpBuffer[0] = (BYTE) ulValue;
  1599.     rpBuffer++;
  1600. }
  1601. void PXWireFormatManager::PackVUINT(REF(BYTE*) rpBuffer, UINT32 ulValue)
  1602. {
  1603.     // Format of VUINT is:
  1604.     // 
  1605.     // 11bb bbbb  bbbb bbbb  bbbb bbbb  bbbb bbbb (value in range [0,0x3FFFFFFF])
  1606.     // 10bb bbbb  bbbb bbbb  (value in range [0,0x3FFF])
  1607.     // 0bbb bbbb (value in range [0,0x7F])
  1608.     //
  1609.     HX_ASSERT(ulValue <= 0x3FFFFFFF);
  1610.     if (ulValue > 0x3FFF)
  1611.     {
  1612.         PackUINT32(rpBuffer, (ulValue | 0xC0000000));
  1613.     }
  1614.     else if (ulValue > 0x7F && ulValue <= 0x3FFF)
  1615.     {
  1616.         PackUINT16(rpBuffer, (ulValue | 0x00008000));
  1617.     }
  1618.     else
  1619.     {
  1620.         PackBYTE(rpBuffer, ulValue);
  1621.     }
  1622. }
  1623. UINT32 PXWireFormatManager::GetMask(UINT32 ulBitPos, UINT32 ulNumBits)
  1624. {
  1625.     UINT32 ulRet  = (1 << ulNumBits) - 1;
  1626.     INT32  lShift = (INT32) ulBitPos - ulNumBits + 1;
  1627.     if (lShift >= 0)
  1628.     {
  1629.         ulRet <<= lShift;
  1630.     }
  1631.     else
  1632.     {
  1633.         ulRet >>= -lShift;
  1634.     }
  1635.     return ulRet;
  1636. }
  1637. UINT32 PXWireFormatManager::GetUnsignedBits(REF(BYTE*) rpBuf, REF(UINT32) rulBitPos, UINT32 ulNumBits)
  1638. {
  1639.     UINT32 ulRet = 0;
  1640.     while (ulNumBits)
  1641.     {
  1642.         UINT32 ulBitsAvailable = rulBitPos + 1;
  1643.         if (ulNumBits >= ulBitsAvailable)
  1644.         {
  1645.             // We're gonna use the whole current byte and advance it
  1646.             ulRet     |= (*rpBuf & GetMask(rulBitPos, ulBitsAvailable)) << (ulNumBits - ulBitsAvailable);
  1647.             ulNumBits -= ulBitsAvailable;
  1648.             // Advance the buffer
  1649.             rpBuf++;
  1650.             rulBitPos = 7;
  1651.         }
  1652.         else
  1653.         {
  1654.             // We're only gonna use part of the current byte
  1655.             ulRet     |= (*rpBuf & GetMask(rulBitPos, ulNumBits)) >> (ulBitsAvailable - ulNumBits);
  1656.             // Advance the buffer
  1657.             rulBitPos -= ulNumBits;
  1658.             ulNumBits -= ulNumBits;
  1659.         }
  1660.     }
  1661.     return ulRet;
  1662. }
  1663. INT32 PXWireFormatManager::GetSignedBits(REF(BYTE*) rpBuf, REF(UINT32) rulBitPos, UINT32 ulNumBits)
  1664. {
  1665.     INT32 lRet = (INT32) GetUnsignedBits(rpBuf, rulBitPos, ulNumBits);
  1666.     if (lRet & (1L << (ulNumBits - 1)))
  1667.     {
  1668.         lRet |= (-1L << ulNumBits);
  1669.     }
  1670.     return lRet;
  1671. }
  1672. HX_RESULT PXWireFormatManager::SetTitle(const char* pszTitle)
  1673. {
  1674.     return SetString(pszTitle, m_pTitleStr);
  1675. }
  1676. HX_RESULT PXWireFormatManager::SetTitle(IHXBuffer* pTitleStr)
  1677. {
  1678.     return SetString(pTitleStr, m_pTitleStr);
  1679. }
  1680. HX_RESULT PXWireFormatManager::GetTitle(REF(IHXBuffer*) rpTitleStr)
  1681. {
  1682.     return SetString(m_pTitleStr, rpTitleStr);
  1683. }
  1684. HX_RESULT PXWireFormatManager::SetAuthor(const char* pszAuthor)
  1685. {
  1686.     return SetString(pszAuthor, m_pAuthorStr);
  1687. }
  1688. HX_RESULT PXWireFormatManager::SetAuthor(IHXBuffer* pAuthorStr)
  1689. {
  1690.     return SetString(pAuthorStr, m_pAuthorStr);
  1691. }
  1692. HX_RESULT PXWireFormatManager::GetAuthor(REF(IHXBuffer*) rpAuthorStr)
  1693. {
  1694.     return SetString(m_pAuthorStr, rpAuthorStr);
  1695. }
  1696. HX_RESULT PXWireFormatManager::SetCopyright(const char* pszCopyright)
  1697. {
  1698.     return SetString(pszCopyright, m_pCopyrightStr);
  1699. }
  1700. HX_RESULT PXWireFormatManager::SetCopyright(IHXBuffer* pCopyrightStr)
  1701. {
  1702.     return SetString(pCopyrightStr, m_pCopyrightStr);
  1703. }
  1704. HX_RESULT PXWireFormatManager::GetCopyright(REF(IHXBuffer*) rpCopyrightStr)
  1705. {
  1706.     return SetString(m_pCopyrightStr, rpCopyrightStr);
  1707. }
  1708. HX_RESULT PXWireFormatManager::SetDefaultURL(const char* pszDefaultURL)
  1709. {
  1710.     return SetString(pszDefaultURL, m_pDefaultURLStr);
  1711. }
  1712. HX_RESULT PXWireFormatManager::SetDefaultURL(IHXBuffer* pDefaultURLStr)
  1713. {
  1714.     return SetString(pDefaultURLStr, m_pDefaultURLStr);
  1715. }
  1716. HX_RESULT PXWireFormatManager::GetDefaultURL(REF(IHXBuffer*) rpDefaultURLStr)
  1717. {
  1718.     return SetString(m_pDefaultURLStr, rpDefaultURLStr);
  1719. }
  1720. HX_RESULT PXWireFormatManager::SetASMRuleBook(const char* pszRuleBook)
  1721. {
  1722.     return SetString(pszRuleBook, m_pASMRuleBook);
  1723. }
  1724. HX_RESULT PXWireFormatManager::SetASMRuleBook(IHXBuffer* pRuleBookStr)
  1725. {
  1726.     return SetString(pRuleBookStr, m_pASMRuleBook);
  1727. }
  1728. HX_RESULT PXWireFormatManager::GetASMRuleBook(REF(IHXBuffer*) rpRuleBookStr)
  1729. {
  1730.     return SetString(m_pASMRuleBook, rpRuleBookStr);
  1731. }
  1732. HX_RESULT PXWireFormatManager::SetStreamMimeType(const char* pszMime)
  1733. {
  1734.     return SetString(pszMime, m_pStreamMimeType);
  1735. }
  1736. HX_RESULT PXWireFormatManager::SetStreamMimeType(IHXBuffer* pMimeStr)
  1737. {
  1738.     return SetString(pMimeStr, m_pStreamMimeType);
  1739. }
  1740. HX_RESULT PXWireFormatManager::GetStreamMimeType(REF(IHXBuffer*) rpMimeStr)
  1741. {
  1742.     return SetString(m_pStreamMimeType, rpMimeStr);
  1743. }
  1744. HX_RESULT PXWireFormatManager::AddStringToList(const char* pszStr, CHXSimpleList* pList)
  1745. {
  1746.     HX_RESULT retVal = HXR_OK;
  1747.     if (pszStr && pList)
  1748.     {
  1749.         IHXBuffer* pBuffer = NULL;
  1750.         retVal              = SetString(pszStr, pBuffer);
  1751.         if (SUCCEEDED(retVal))
  1752.         {
  1753.             retVal = AddStringToList(pBuffer, pList);
  1754.         }
  1755.         HX_RELEASE(pBuffer);
  1756.     }
  1757.     else
  1758.     {
  1759.         retVal = HXR_INVALID_PARAMETER;
  1760.     }
  1761.     return retVal;
  1762. }
  1763. HX_RESULT PXWireFormatManager::AddStringToList(IHXBuffer* pBufferStr, CHXSimpleList* pList)
  1764. {
  1765.     HX_RESULT retVal = HXR_OK;
  1766.     if (pBufferStr && pList)
  1767.     {
  1768.         // Let's make sure the string is not already in the list
  1769.         BOOL         bFoundMatch = FALSE;
  1770.         LISTPOSITION pos         = pList->GetHeadPosition();
  1771.         while (pos)
  1772.         {
  1773.             IHXBuffer* pStr = (IHXBuffer*) pList->GetNext(pos);
  1774.             if (pStr)
  1775.             {
  1776.                 if (!strcmp((const char*) pBufferStr->GetBuffer(),
  1777.                             (const char*) pStr->GetBuffer()))
  1778.                 {
  1779.                     bFoundMatch = TRUE;
  1780.                 }
  1781.             }
  1782.         }
  1783.         if (!bFoundMatch)
  1784.         {
  1785.             // AddRef the buffer before it goes on the list
  1786.             pBufferStr->AddRef();
  1787.             // Add the buffer to the tail of the list
  1788.             pList->AddTail((void*) pBufferStr);
  1789.         }
  1790.     }
  1791.     else
  1792.     {
  1793.         retVal = HXR_INVALID_PARAMETER;
  1794.     }
  1795.     return retVal;
  1796. }
  1797. HX_RESULT PXWireFormatManager::SetString(const char* pszStr, REF(IHXBuffer*) rpBufferStr)
  1798. {
  1799.     HX_RESULT retVal = HXR_FAIL;
  1800.     if (pszStr && m_pCommonClassFactory)
  1801.     {
  1802.         IHXBuffer* pBuffer = NULL;
  1803.         retVal              = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
  1804.                                                                     (void**) &pBuffer);
  1805.         if (SUCCEEDED(retVal))
  1806.         {
  1807.             // Set the size
  1808.             retVal = pBuffer->Set((const BYTE*) pszStr, strlen(pszStr) + 1);
  1809.             if (SUCCEEDED(retVal))
  1810.             {
  1811.                 // Assign the out parameter
  1812.                 HX_RELEASE(rpBufferStr);
  1813.                 rpBufferStr = pBuffer;
  1814.                 rpBufferStr->AddRef();
  1815.             }
  1816.         }
  1817.         HX_RELEASE(pBuffer);
  1818.     }
  1819.     return retVal;
  1820. }
  1821. HX_RESULT PXWireFormatManager::SetString(IHXBuffer* pBuffer, REF(IHXBuffer*) rpBufferStr)
  1822. {
  1823.     HX_RESULT retVal = HXR_OK;
  1824.     if (pBuffer)
  1825.     {
  1826.         HX_RELEASE(rpBufferStr);
  1827.         rpBufferStr = pBuffer;
  1828.         rpBufferStr->AddRef();
  1829.     }
  1830.     else
  1831.     {
  1832.         retVal = HXR_FAIL;
  1833.     }
  1834.     return retVal;
  1835. }
  1836. HX_RESULT PXWireFormatManager::GetFirstString(REF(LISTPOSITION) rPos, CHXSimpleList* pList, IHXBuffer** ppBuffer)
  1837. {
  1838.     HX_RESULT retVal = HXR_OK;
  1839.     if (pList && ppBuffer)
  1840.     {
  1841.         // Set default
  1842.         *ppBuffer = NULL;
  1843.         rPos = pList->GetHeadPosition();
  1844.         if (rPos)
  1845.         {
  1846.             *ppBuffer = (IHXBuffer*) pList->GetNext(rPos);
  1847.             (*ppBuffer)->AddRef();
  1848.         }
  1849.         else
  1850.         {
  1851.             retVal = HXR_FAIL;
  1852.         }
  1853.     }
  1854.     else
  1855.     {
  1856.         retVal = HXR_INVALID_PARAMETER;
  1857.     }
  1858.     return retVal;
  1859. }
  1860. HX_RESULT PXWireFormatManager::GetNextString(REF(LISTPOSITION) rPos, CHXSimpleList* pList, IHXBuffer** ppBuffer)
  1861. {
  1862.     HX_RESULT retVal = HXR_OK;
  1863.     if (pList && ppBuffer)
  1864.     {
  1865.         // Set default
  1866.         *ppBuffer = NULL;
  1867.         if (rPos)
  1868.         {
  1869.             *ppBuffer = (IHXBuffer*) pList->GetNext(rPos);
  1870.             (*ppBuffer)->AddRef();
  1871.         }
  1872.         else
  1873.         {
  1874.             retVal = HXR_FAIL;
  1875.         }
  1876.     }
  1877.     else
  1878.     {
  1879.         retVal = HXR_INVALID_PARAMETER;
  1880.     }
  1881.     return retVal;
  1882. }
  1883. void PXWireFormatManager::SetBackgroundColor(BYTE ucRed, BYTE ucGreen, BYTE ucBlue)
  1884. {
  1885.     m_ulBackgroundColor = (ucRed << 16) | (ucGreen << 8) | ucBlue;
  1886. }
  1887. void PXWireFormatManager::SetBackgroundColor(UINT32 ulColor)
  1888. {
  1889.     m_ulBackgroundColor = ulColor;
  1890. }
  1891. void PXWireFormatManager::GetBackgroundColor(REF(BYTE) rucRed, REF(BYTE) rucGreen, REF(BYTE) rucBlue) const
  1892. {
  1893.     rucRed   = (BYTE) ((m_ulBackgroundColor & 0x00FF0000) >> 16);
  1894.     rucGreen = (BYTE) ((m_ulBackgroundColor & 0x0000FF00) >>  8);
  1895.     rucBlue  = (BYTE)  (m_ulBackgroundColor & 0x000000FF);
  1896. }
  1897. UINT32 PXWireFormatManager::GetBackgroundColor() const
  1898. {
  1899.     return m_ulBackgroundColor;
  1900. }
  1901. void PXWireFormatManager::SetRealPixAdsFlag(BOOL bFlag)
  1902. {
  1903.     if (bFlag)
  1904.     {
  1905.         m_ulRendererFlags |= kRealPixAdsFlagMask;
  1906.     }
  1907.     else
  1908.     {
  1909.         m_ulRendererFlags &= ~kRealPixAdsFlagMask;
  1910.     }
  1911. }
  1912. BOOL PXWireFormatManager::GetRealPixAdsFlag() const
  1913. {
  1914.     return (m_ulRendererFlags & kRealPixAdsFlagMask ? TRUE : FALSE);
  1915. }
  1916. void PXWireFormatManager::SetRPACachingAdFlag(BOOL bFlag)
  1917. {
  1918.     if (bFlag)
  1919.     {
  1920.         m_ulRendererFlags |= kRPACachingAdFlagMask;
  1921.     }
  1922.     else
  1923.     {
  1924.         m_ulRendererFlags &= ~kRPACachingAdFlagMask;
  1925.     }
  1926. }
  1927. BOOL PXWireFormatManager::GetRPACachingAdFlag() const
  1928. {
  1929.     return (m_ulRendererFlags & kRPACachingAdFlagMask ? TRUE : FALSE);
  1930. }
  1931. UINT32 PXWireFormatManager::GetNumCodecMimes() const
  1932. {
  1933.     return (m_pCodecMimeList ? m_pCodecMimeList->GetCount() : 0);
  1934. }
  1935. UINT32 PXWireFormatManager::GetNumFXPackageMimes() const
  1936. {
  1937.     return (m_pFXPackageMimeList ? m_pFXPackageMimeList->GetCount() : 0);
  1938. }
  1939. void PXWireFormatManager::SetLiveRealPixFlag(BOOL bFlag)
  1940. {
  1941.     if (bFlag)
  1942.     {
  1943.         m_ulRendererFlags |= kLiveRealPixMask;
  1944.     }
  1945.     else
  1946.     {
  1947.         m_ulRendererFlags &= ~kLiveRealPixMask;
  1948.     }
  1949. }
  1950. BOOL PXWireFormatManager::GetLiveRealPixAds() const
  1951. {
  1952.     return (m_ulRendererFlags & kLiveRealPixMask ? TRUE : FALSE);
  1953. }
  1954. void PXWireFormatManager::AddMetaInfo(IHXValues* pAvailableMetaInfo,
  1955.                                       const char* pszRequestedInfo,
  1956.                                       IHXValues* pFileHeader)
  1957. {
  1958.     if (pAvailableMetaInfo && pszRequestedInfo &&
  1959.         pFileHeader && m_pCommonClassFactory)
  1960.     {
  1961.         // Create a new string with the requested info
  1962.         char* pszReqInfo = new char [strlen(pszRequestedInfo) + 1];
  1963.         if (pszReqInfo)
  1964.         {
  1965.             // Copy the string
  1966.             strcpy(pszReqInfo, pszRequestedInfo); /* Flawfinder: ignore */
  1967.             // Identify the delimiters
  1968.             const char* pszDelimit = " ,trn";
  1969.             // Initialize the variables needed for iterating
  1970.             // through IHXValues properties
  1971.             HX_RESULT   rv      = HXR_OK;
  1972.             IHXBuffer* pValue  = NULL;
  1973.             UINT32      ulValue = 0;
  1974.             // Make the first call to strtok().
  1975.             char* pszToken = strtok(pszReqInfo, pszDelimit);
  1976.             while (pszToken)
  1977.             {
  1978.                 // Did we get the instruction to add all meta info?
  1979.                 if (*pszToken == '*')
  1980.                 {
  1981.                     // We should add all meta info
  1982.                     //
  1983.                     // Loop through the Meta Info CStrings
  1984.                     const char* pszName = NULL;
  1985.                     rv = pAvailableMetaInfo->GetFirstPropertyCString(pszName, pValue);
  1986.                     while (SUCCEEDED(rv))
  1987.                     {
  1988.                         // Add it to the header
  1989.                         pFileHeader->SetPropertyCString(pszName, pValue);
  1990.                         HX_RELEASE(pValue);
  1991.                         rv = pAvailableMetaInfo->GetNextPropertyCString(pszName, pValue);
  1992.                     }
  1993.                     // Loop through the Meta Info ULONG32s
  1994.                     rv = pAvailableMetaInfo->GetFirstPropertyULONG32(pszName, ulValue);
  1995.                     while (SUCCEEDED(rv))
  1996.                     {
  1997.                         // Add it to the header
  1998.                         pFileHeader->SetPropertyULONG32(pszName, ulValue);
  1999.                         rv = pAvailableMetaInfo->GetNextPropertyULONG32(pszName, ulValue);
  2000.                     }
  2001.                     // Loop through the Meta Info Buffers
  2002.                     rv = pAvailableMetaInfo->GetFirstPropertyBuffer(pszName, pValue);
  2003.                     while (SUCCEEDED(rv))
  2004.                     {
  2005.                         // Add it to the header
  2006.                         pFileHeader->SetPropertyBuffer(pszName, pValue);
  2007.                         HX_RELEASE(pValue);
  2008.                         rv = pAvailableMetaInfo->GetNextPropertyBuffer(pszName, pValue);
  2009.                     }
  2010.                     // We've added all the meta info, so we can break out of
  2011.                     // this parsing loop
  2012.                     break;
  2013.                 }
  2014.                 // Now we have a particular meta info name. Therefore,
  2015.                 // we look for it in our supplied list of meta
  2016.                 // info. We don't know whether it's a ULONG32, CString,
  2017.                 // or Buffer property, so we have to check all three.
  2018.                 //
  2019.                 // Is this property a CString property?
  2020.                 rv = pAvailableMetaInfo->GetPropertyCString(pszToken, pValue);
  2021.                 if (SUCCEEDED(rv))
  2022.                 {
  2023.                     pFileHeader->SetPropertyCString(pszToken, pValue);
  2024.                 }
  2025.                 HX_RELEASE(pValue);
  2026.                 if (FAILED(rv))
  2027.                 {
  2028.                     // It wasn't a CString property, so is it a ULONG32 property?
  2029.                     rv = pAvailableMetaInfo->GetPropertyULONG32(pszToken, ulValue);
  2030.                     if (SUCCEEDED(rv))
  2031.                     {
  2032.                         pFileHeader->SetPropertyULONG32(pszToken, ulValue);
  2033.                     }
  2034.                     if (FAILED(rv))
  2035.                     {
  2036.                         // It wasn't a ULONG32 property, so was it a Buffer property?
  2037.                         rv = pAvailableMetaInfo->GetPropertyBuffer(pszToken, pValue);
  2038.                         if (SUCCEEDED(rv))
  2039.                         {
  2040.                             pFileHeader->SetPropertyBuffer(pszToken, pValue);
  2041.                         }
  2042.                         HX_RELEASE(pValue);
  2043.                     }
  2044.                 }
  2045.                 // Get the next token
  2046.                 pszToken = strtok(NULL, pszDelimit);
  2047.             }
  2048.         }
  2049. HX_VECTOR_DELETE(pszReqInfo);
  2050.     }
  2051. }