OggPacket.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 
00032 #include "stdafx.h"
00033 #include <libOOOgg/OggPacket.h>
00034 
00035 //LEAK CHECK::: 20041018 - OK.
00036 OggPacket::OggPacket(void)
00037         :       mPacketSize(0)
00038         ,       mPacketData(NULL)
00039         ,       mIsContinuation(false)
00040         ,       mIsTruncated(false)
00041                 
00042 {
00043 
00044 }
00045 
00046 //Accepts responsibility for inPackData pointer - deletes it in destructor.
00047 OggPacket::OggPacket(unsigned char* inPackData, unsigned long inPacketSize, bool inIsTruncated, bool inIsContinuation)
00048         :       mPacketSize(inPacketSize)
00049         ,       mPacketData(inPackData)
00050         ,       mIsContinuation(inIsContinuation)
00051         ,       mIsTruncated(inIsTruncated)
00052                 
00053 {
00054 }
00055 
00056 //This method creates a pointer which the caller is responsible for.
00057 OggPacket* OggPacket::clone() {
00058         //Make a new buffer for packet data
00059         unsigned char* locBuff = new unsigned char[mPacketSize];                        //Given to constructor of OggPacket
00060 
00061         //Copy the packet data into the new buffer
00062         memcpy((void*)locBuff, (const void*)mPacketData, mPacketSize);
00063 
00064         //Create the new packet - accepts locBuff poitner
00065         OggPacket* retPack = new OggPacket(locBuff, mPacketSize, mIsTruncated, mIsContinuation);        //Gives this pointer to the caller.
00066         return retPack;
00067 }
00068 
00069 
00070 OggPacket::~OggPacket(void)
00071 {
00072         //Now deletes the packetData
00073         delete[] mPacketData;
00074 
00075         //Should be careful about allowing this to be copied...
00076 }
00077 
00078 string OggPacket::toPackDumpString() {
00079         string retStr = "";
00080 
00081 
00083         //Needs dataSize and data pointer
00084 
00085         //Put the stream in hex mode with a fill character of 0
00086         
00087         //cout << setfill('0');
00088 
00089         //Loop through every character of data
00090         for (unsigned long i = 0; i < mPacketSize; i++) {
00091                 //If it is the end of the previous hex dump line or first line)
00092                 if ( (i % HEX_DUMP_LINE_LENGTH == 0) ) {
00093                         //And this is not the first line
00094                         if ( i != 0 ) {
00095                                 //Put the characters on the end
00096                                 retStr.append(dumpNCharsToString( &mPacketData[i - HEX_DUMP_LINE_LENGTH],  HEX_DUMP_LINE_LENGTH));
00097                         }
00098 
00099                         //At the start of the line write out the base address in an 8 hex-digit field 
00100                         //NOTE::: Just decimal for now.
00101                         //cout << setw(8) << i << ": ";
00102                         retStr.append(padField(StringHelper::numToString(i), 8, '0') + ": ");
00103                 }
00104 
00105                 //Write out the value of the character in a 2 hex-digit field
00106                 //cout << setw(2) << (int)mPageData[i] << " ";
00107                 retStr.append(StringHelper::charToHexString(mPacketData[i]) + " ");
00108         }
00109 
00110         //Find out how many leftover charcters didn't get written out.
00111         unsigned long locLeftovers = (mPacketSize % HEX_DUMP_LINE_LENGTH);
00112 
00113         locLeftovers = (locLeftovers > 0)       ? (locLeftovers)        
00114                                                                                 : (HEX_DUMP_LINE_LENGTH);
00115 
00116 
00117         //If there was any data in this dump
00118         if ( mPacketSize > 0 ) {
00119                 //Dump the last part out
00120                 retStr.append(dumpNCharsToString( &mPacketData[mPacketSize - locLeftovers], locLeftovers ));
00121         }
00122 
00123         retStr+= "==============================================================================\n" ;
00124         //Put the stream back to decimal mode
00125         //dec(cout);
00126 
00127         return retStr;
00128 }
00129 
00130 string OggPacket::padField(string inString, unsigned long inPadWidth, unsigned char inPadChar) {
00131         //NOTE::: Need check for string being  bigger than pad space
00132         string retStr = "";
00133         retStr.append(inPadWidth - inString.length(), inPadChar);
00134         retStr.append(inString);
00135 
00136         return retStr;
00137 }
00138 string OggPacket::dumpNCharsToString(unsigned char* inStartPoint, unsigned long inNumChars) {
00139         //NOTE::: Also needs reworking
00140         //const unsigned char BELL = 7;  // unused
00141         //Set the fill character back to space ' '
00142         //cout << setfill(' ');
00143 
00144 
00145         //Put some space after the hex section
00146         unsigned long locPadding = 3 * (HEX_DUMP_LINE_LENGTH - inNumChars) + 4;
00147         //cout << setw(locPadding) << "    ";
00148         
00149         string retStr = padField("    ", locPadding, ' ');   
00150         
00151         //Loop through the characters
00152         for (unsigned long i = 0; i < inNumChars; i++) {
00153 
00154                 //If they are *not* going to mess up the layout (i.e. the thing is printable)
00155                 if ( (inStartPoint[i] >= 32) && (inStartPoint[i] <= 126) ) {
00156                         //Write them out
00157                         retStr += (char)inStartPoint[i];                                                
00158                 } else {
00159                         //Otherwise just write a placeholder char
00160                         retStr += ((char) '.');
00161                 }
00162         }
00163         retStr += "\n";
00164         
00165         return retStr;
00166         
00167 }
00168 
00169 
00170 
00171 unsigned long OggPacket::packetSize() const {
00172         return mPacketSize;
00173 }
00174 
00179 unsigned char* OggPacket::packetData() {
00180         return mPacketData;
00181 }
00182 bool OggPacket::isTruncated() const {
00183         return mIsTruncated;
00184 }
00185 bool OggPacket::isContinuation() const {
00186         return mIsContinuation;
00187 }
00188 
00189 //bool OggPacket::isComplete() const {
00190 //      return mIsComplete;
00191 //}
00192 
00193 //void OggPacket::setIsComplete(bool inIsComplete) {
00194 //      mIsComplete = inIsComplete;
00195 //}
00196 void OggPacket::setPacketSize(unsigned long inPacketSize) {
00197         mPacketSize = inPacketSize;
00198 }
00199 
00205 void OggPacket::setPacketData(unsigned char* inPacketData) {
00206         mPacketData = inPacketData;
00207 }
00208 
00209 //Only views the incoming pointer.
00210 void OggPacket::merge(const OggPacket* inMorePacket) {
00211         //Make a new buffer the size of both data segs together
00212         unsigned char* locBuff = new unsigned char[mPacketSize + inMorePacket->mPacketSize];            //This is put into the member vvariable, where it will be deleted in destructor.
00213         //Copy this packets data to the start
00214         memcpy((void*)locBuff, (const void*)mPacketData, mPacketSize);
00215         //Copy the next packets data after it
00216         memcpy((void*)(locBuff + mPacketSize), (const void*)inMorePacket->mPacketData, inMorePacket->mPacketSize);
00217         //Delete our original packet data
00218         delete[] mPacketData;
00219         //Now make our data be the combined data
00220         mPacketData = locBuff;
00221         //Make the size the sum of both packets
00222         mPacketSize += inMorePacket->mPacketSize;
00223 
00224         //If the next part of the packet isn't complete then this packet is not complete.
00225         //mIsComplete = inMorePacket->mIsComplete;
00226         //The new packet is truncated only if the incoming packet is
00227         mIsTruncated = inMorePacket->mIsTruncated;    //->isTruncated();
00228 
00229         //This is not a continuation... a continuation is a packet that does not start at the start of the real packet.
00230         mIsContinuation = false;
00231 
00232 }

Generated on Tue Feb 15 14:54:21 2005 for oggdsf by  doxygen 1.3.9