Mixe for Privacy and Anonymity in the Internet
DOM_Output.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #ifndef __DOM_OUTPUT__
00029 #define __DOM_OUTPUT__
00030 #ifndef ONLY_LOCAL_PROXY
00031 #include "../CAQueue.hpp"
00032 
00033 enum OUTPUT_FORMAT { OF_DEFAULT, OF_NULL_TERMINATED, OF_NEWLINE };
00034 
00035 class MemFormatTarget: public XMLFormatTarget
00036   {
00037 #define MEM_FORMART_TARGET_SPACE 1024
00038     public:
00039       MemFormatTarget()
00040         {
00041           m_pQueue=new CAQueue();
00042           m_Buff=new UINT8[MEM_FORMART_TARGET_SPACE];
00043           m_aktIndex=0;
00044         }
00045 
00046       ~MemFormatTarget()
00047         {
00048           delete m_pQueue;
00049           m_pQueue = NULL;
00050           delete[] m_Buff;
00051           m_Buff = NULL;
00052         }
00053 
00054 #if (XERCES_VERSION_MAJOR <3)
00055       virtual void writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const /*formatter*/)
00056 #else
00057       virtual void writeChars(const XMLByte* const toWrite, const XMLSize_t count, XMLFormatter* const /*formatter*/)
00058 #endif
00059         {
00060           const XMLByte* write=toWrite;
00061           UINT32 c=count;
00062           while(c>0)
00063             {
00064               UINT32 space=MEM_FORMART_TARGET_SPACE-m_aktIndex;
00065               if(space>=c)
00066                 {
00067                   memcpy(m_Buff+m_aktIndex,write,c);
00068                   m_aktIndex+=c;
00069                   return;
00070                 }
00071               else
00072                 {
00073                   memcpy(m_Buff+m_aktIndex,write,space);
00074                   write+=space;
00075                   c-=space;
00076                   m_pQueue->add(m_Buff,MEM_FORMART_TARGET_SPACE);
00077                   m_aktIndex=0;
00078                 }
00079             }
00080         }
00081 
00089       SINT32 dumpMem(UINT8* buff,UINT32* size)
00090         {
00091           if(buff==NULL||size==NULL)
00092             return E_UNKNOWN;
00093           if(m_aktIndex>0&&m_pQueue->add(m_Buff,m_aktIndex)!=E_SUCCESS)
00094             return E_UNKNOWN;
00095           m_aktIndex=0;
00096           if(*size<m_pQueue->getSize())
00097             return E_SPACE;
00098           if(m_pQueue->peek(buff,size)!=E_SUCCESS)
00099             return E_UNKNOWN;
00100           return E_SUCCESS;
00101         }
00102 
00109       UINT8* dumpMem(UINT32* size, OUTPUT_FORMAT a_outputFormat)
00110         {
00111           if(size==NULL)
00112             return NULL;
00113           if(m_aktIndex>0&&m_pQueue->add(m_Buff,m_aktIndex)!=E_SUCCESS)
00114             return NULL;
00115           m_aktIndex=0;
00116           *size=m_pQueue->getSize();
00117           if (OF_NULL_TERMINATED == a_outputFormat)
00118           {
00119             *size += 1;
00120           }
00121           else if (OF_NEWLINE == a_outputFormat)
00122           {
00123             *size += 2;
00124           }
00125           UINT8* tmp=new UINT8[*size];
00126           if(m_pQueue->peek(tmp,size)!=E_SUCCESS)
00127             {
00128               delete[] tmp;
00129               tmp = NULL;
00130             }
00131           if (OF_NULL_TERMINATED == a_outputFormat)
00132           {
00133             tmp[*size] = NULL;
00134           }
00135           else if (OF_NEWLINE == a_outputFormat)
00136           {
00137             tmp[*size+1] = NULL;
00138             tmp[*size] = '\n';
00139           }
00140           return tmp;
00141         }
00142 
00143     private:
00144       CAQueue* m_pQueue;
00145       UINT8* m_Buff;
00146       UINT32 m_aktIndex;
00147   };
00148 
00149 class DOM_Output
00150   {
00151     public:
00161       static SINT32 dumpToMem(const DOMNode* node,UINT8* buff, UINT32* size)
00162         {
00163           DOM_Output out;
00164           if( out.dumpNode(node,false)!=E_SUCCESS)
00165             return E_UNKNOWN;
00166           return out.m_pFormatTarget->dumpMem(buff,size);
00167         }
00168 
00169       
00177       static UINT8* dumpToMem(const DOMNode* node,UINT32* size)
00178         {
00179           return dumpToMem(node, size, OF_DEFAULT);
00180         }
00181 
00182 
00183 
00191       static UINT8* dumpToString(const DOMNode* node, bool a_bAddNewLine)
00192         {
00193           UINT32 dummy;
00194           if (a_bAddNewLine)
00195           {
00196             return dumpToMem(node, &dummy, OF_NEWLINE);
00197           }
00198           else
00199           {
00200             return dumpToMem(node, &dummy, OF_NULL_TERMINATED);
00201           }
00202         }
00203 
00204       
00213       static SINT32 makeCanonical(const DOMNode* node,UINT8* buff,UINT32* size)
00214         {
00215           DOM_Output out;
00216           if( out.dumpNode(node,true)!=E_SUCCESS)
00217             return E_UNKNOWN;
00218           return out.m_pFormatTarget->dumpMem(buff,size);
00219         }
00220 
00221     
00228       static UINT8* makeCanonical(const DOMNode* node,UINT32* size)
00229         {
00230           DOM_Output out;
00231           if( out.dumpNode(node,true)!=E_SUCCESS)
00232             return NULL;
00233           return out.m_pFormatTarget->dumpMem(size, OF_DEFAULT);
00234         }
00235 
00236     private:
00237       DOM_Output()
00238         {
00239           m_pFormatTarget=new MemFormatTarget();
00240           #if (_XERCES_VERSION >= 20300) //XMl-Version since Xerces 2.3.0
00241             m_pFormatter=new XMLFormatter(m_UTF8,m_1_0, m_pFormatTarget,
00242                                             XMLFormatter::NoEscapes, XMLFormatter::UnRep_Fail);
00243           #else
00244             m_pFormatter=new XMLFormatter(m_UTF8, m_pFormatTarget,
00245                                             XMLFormatter::NoEscapes, XMLFormatter::UnRep_Fail);
00246           #endif
00247         }
00248       ~DOM_Output()
00249         {
00250           delete m_pFormatTarget;
00251           m_pFormatTarget = NULL;
00252           delete m_pFormatter;
00253           m_pFormatter = NULL;
00254         }
00255 
00256 
00264       static UINT8* dumpToMem(const DOMNode* node,UINT32* size, OUTPUT_FORMAT a_outputFormat)
00265         {
00266           DOM_Output out;
00267           if( out.dumpNode(node,false)!=E_SUCCESS)
00268             return NULL;
00269           return out.m_pFormatTarget->dumpMem(size, a_outputFormat);
00270         }
00271 
00272 
00273       SINT32 dumpNode(const DOMNode* toWrite,bool bCanonical);
00274       XMLFormatter*       m_pFormatter;
00275       MemFormatTarget*    m_pFormatTarget;
00276       static const XMLCh  m_XML[41]; 
00277       static const XMLCh  m_UTF8[6]; 
00278       static const XMLCh  m_1_0[4]; 
00279 };
00280 #endif
00281 #endif //ONLY_LOCAL_PROXY