wirefmgr.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:74k
源码类别:

Symbian

开发平台:

C/C++

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