moved the hexdump function to libUtils
tho [Thu, 2 Aug 2007 13:55:18 +0000 (13:55 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@373 270642c3-0616-0410-b53a-bc976706d245

Examples/DVBAdapter/MPEdec.cc
Examples/DVBAdapter/ULEdec.cc
Examples/DVBAdapter/ULEdec.hh
Examples/MCSniffer/MCSniffer.cc
Examples/Sniffer/Sniffer.cc
Utils/Mainpage.dox
Utils/hexdump.ct [new file with mode: 0644]
Utils/hexdump.hh [new file with mode: 0644]

index 48c6b82..f3fe77e 100644 (file)
@@ -33,6 +33,7 @@
 #include "Packets/DefaultBundle/EthernetPacket.hh"
 #include "Packets/MPEGDVBBundle/DatagramSection.hh"
 #include "Utils/membind.hh"
+#include "Utils/hexdump.hh"
 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
 #include "Packets/ParseInt.hh"
 #include "Packets/Packet.hh"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    static const unsigned BLOCK_SIZE = 16;
-
-    template <class Iterator>
-    void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
-    {
-        unsigned offset (0);
-        std::string ascii;
-        for (; i != i_end; ++i, ++offset) {
-            switch (offset % BLOCK_SIZE) {
-            case 0:
-                if (!ascii.empty()) {
-                    stream << "  " << ascii << "\n";
-                    ascii = "";
-                }
-                stream << "  "
-                          << std::hex << std::setw(4) << std::setfill('0')
-                          << offset << ' ';
-                break;
-            case BLOCK_SIZE/2:
-                stream << " ";
-                ascii += ' ';
-                break;
-            }
-            stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
-                      << unsigned(*i);
-            ascii += (*i >= ' ' && *i < 126) ? *i : '.';
-        }
-        if (!ascii.empty()) {
-            for (; (offset % BLOCK_SIZE) != 0; ++offset) {
-                if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
-                    stream << " ";
-                stream << "   ";
-            }
-            stream << "  " << ascii << "\n";
-        }
-        stream << std::dec;
-    }
-}
-
-
 class MySniffer
 {
     senf::DVBDemuxSectionHandle handle;
@@ -113,7 +72,7 @@ private:
         senf::DatagramSection section (senf::DatagramSection::create(data));
         section.dump(std::cout);
         senf::PacketData & datagramData (section.last().data());
-        hexdump(datagramData.begin(), datagramData.end(), std::cout);
+        senf::hexdump(datagramData.begin(), datagramData.end(), std::cout);
     }
 };
 
index bbc4ed8..ea19430 100644 (file)
@@ -35,6 +35,7 @@
 #include "Packets/MPEGDVBBundle/TransportPacket.hh"
 #include "Packets/MPEGDVBBundle/SNDUPacket.hh"
 #include "Utils/membind.hh"
+#include "Utils/hexdump.hh"
 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
 #include "Packets/ParseInt.hh"
 #include "Packets/Packet.hh"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-template <class Iterator>
-void ULEdec::hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
-{
-    unsigned offset (0);
-    std::string ascii;
-    for (; i != i_end; ++i, ++offset) {
-        switch (offset % BLOCK_SIZE) {
-        case 0:
-            if (!ascii.empty()) {
-                stream << "  " << ascii << "\n";
-                ascii = "";
-            }
-            stream << "  "
-                      << std::hex << std::setw(4) << std::setfill('0')
-                      << offset << ' ';
-            break;
-        case BLOCK_SIZE/2:
-            stream << " ";
-            ascii += ' ';
-            break;
-        }
-        stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
-                  << unsigned(*i);
-        ascii += (*i >= ' ' && *i < 126) ? *i : '.';
-    }
-    if (!ascii.empty()) {
-        for (; (offset % BLOCK_SIZE) != 0; ++offset) {
-            if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
-                stream << " ";
-            stream << "   ";
-        }
-        stream << "  " << ascii << "\n";
-    }
-    stream << std::dec;
-}
-
 
 ULEdec::ULEdec()
 {
@@ -129,19 +94,35 @@ void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
     iterator payload_start = payloadData.begin();
     iterator payload_end = payloadData.end();
     
-    hexdump(payload_start, payload_end, std::cout);
+    senf::hexdump(payload_start, payload_end, std::cout);
     
     // Synchronize continuity counter
     this->priv_tscc = ts_packet->continuity_counter();
     
-    if (ts_packet->pusi() == 0) {
+    switch (ts_packet->pusi()) {
+    case 0: {
         switch (this->receiver_state) {
         case Idle:
             return;
         case Reassembly:
-            readContSNDUPacket( payload_start, payload_end );
+            payload_start = readContSNDUPacket( payload_start, payload_end );
+            if (isSDNUPacketComplete()) {
+                handleSNDUPacket();
+                this->receiver_state = Idle;
+                switch (std::distance( payload_start, payload_end ))
+                case 0:
+                case 1:
+                    return;
+                default:
+                    if ( (*payload_start++ | *payload_start++) != ULE_END_INDICATOR )
+                        std::cerr << "delimiting error\n";
+            } else {
+                BOOST_ASSERT( std::distance( payload_start, payload_end ) == 0 );
+            }
         }
-    } else {
+        break;
+    }
+    case 1: {
         // a PUSI value of 1 indicates the presence of a Payload Pointer.
         unsigned char payload_pointer = *payload_start++;
         if (payload_pointer > 181) {
@@ -152,23 +133,22 @@ void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
         case Idle:
             payload_start += payload_pointer;
             readNewSNDUPacket( payload_start, payload_end );
-            this->snduPacket.dump(std::cout);
             break;
         case Reassembly:
             // Reassembly Payload Pointer Checking
-            unsigned char sdnu_bytes_left = std::distance(
-                    this->snduPacketData_iter, this->snduPacket.data().end() );
-            if (sdnu_bytes_left != payload_pointer) {
+            unsigned char sndu_bytes_left = snduPacketBytesLeft();
+            if (sndu_bytes_left != payload_pointer) {
                 // delimiting error
                 std::cerr << "delimiting error\n";
                 payload_start += payload_pointer;
             } else {
                 readContSNDUPacket( payload_start, payload_end );
-                BOOST_ASSERT( this->snduPacketData_iter == this->snduPacket.data().end() );
+                BOOST_ASSERT( isSDNUPacketComplete() );
             }
             readNewSNDUPacket( payload_start, payload_end );
-        }       
+        }
     }
+    } // end pusi-switch
 
 }
 
@@ -220,8 +200,7 @@ ULEdec::iterator ULEdec::readNewSNDUPacket(iterator i_start, iterator i_end)
 ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i_start, iterator i_end)
 {
     unsigned char how_much = std::min(
-            std::distance( this->snduPacketData_iter, this->snduPacket.data().end() ),
-            std::distance( i_start, i_end ) );
+            snduPacketBytesLeft(), std::distance( i_start, i_end ) );
     copy_n(i_start, how_much, this->snduPacketData_iter);
     i_start += how_much;
     this->snduPacketData_iter += how_much;
@@ -229,6 +208,23 @@ ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i_start, iterator i_end)
 }
 
 
+void ULEdec::handleSNDUPacket()
+{
+    this->snduPacket.dump(std::cout);
+}
+
+
+inline bool ULEdec::isSDNUPacketComplete()
+{
+    return this->snduPacketData_iter == this->snduPacket.data().end();
+}
+
+inline ULEdec::iterator::difference_type ULEdec::snduPacketBytesLeft()
+{
+    return std::distance( this->snduPacketData_iter, this->snduPacket.data().end() );
+}
+
+
 int main(int argc, char const * argv[])
 {
     try {
index 9ec78b0..288efaf 100644 (file)
@@ -64,19 +64,17 @@ private:
     bool priv_sndu_type_1;
     iterator snduPacketData_iter;
 
-    static const unsigned BLOCK_SIZE = 16;
-    
-    template <class Iterator>
-    void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream);
-    
     void handleEvent(senf::FileHandle, senf::Scheduler::EventId event);
-    
     void handleTSPacket(senf::TransportPacket tsPacket);
+    void handleSNDUPacket();
     
     iterator readNewSNDUPacket(iterator i_start, iterator i_end);
     iterator readContSNDUPacket(iterator i_start, iterator i_end);
     iterator readRawSNDUPacketData(iterator i_start, iterator i_end);
     
+    inline bool isSDNUPacketComplete(); 
+    inline iterator::difference_type snduPacketBytesLeft();
+    
 };
 
 \f
index 8976c3d..fd288d2 100644 (file)
 #include "Scheduler/Scheduler.hh"
 #include "Packets/DefaultBundle/EthernetPacket.hh"
 #include "Utils/membind.hh"
+#include "Utils/hexdump.hh"
 
 
 //#include "MCSniffer.mpp"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    static const unsigned BLOCK_SIZE = 16;
-
-    template <class Iterator>
-    void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
-    {
-        unsigned offset (0);
-        std::string ascii;
-        for (; i != i_end; ++i, ++offset) {
-            switch (offset % BLOCK_SIZE) {
-            case 0:
-                if (!ascii.empty()) {
-                    stream << "  " << ascii << "\n";
-                    ascii = "";
-                }
-                stream << "  "
-                          << std::hex << std::setw(4) << std::setfill('0')
-                          << offset << ' ';
-                break;
-            case BLOCK_SIZE/2:
-                stream << " ";
-                ascii += ' ';
-                break;
-            }
-            stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
-                      << unsigned(*i);
-            ascii += (*i >= ' ' && *i < 126) ? *i : '.';
-        }
-        if (!ascii.empty()) {
-            for (; (offset % BLOCK_SIZE) != 0; ++offset) {
-                if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
-                    stream << " ";
-                stream << "   ";
-            }
-            stream << "  " << ascii << "\n";
-        }
-        stream << std::dec;
-    }
-}
-
-
 class MCSniffer
 {
     senf::UDPv4ClientSocketHandle sock;
@@ -103,7 +62,7 @@ private:
         senf::EthernetPacket packet (
             senf::EthernetPacket::create(data));
         packet.dump(stream);
-        hexdump(packet.last().data().begin(),
+        senf::hexdump(packet.last().data().begin(),
                 packet.last().data().end(),
                 stream);
         stream << "\n\n";
index 05a0dbc..450b789 100644 (file)
 #include "Socket/Protocols/Raw/PacketSocketHandle.hh"
 #include "Scheduler/Scheduler.hh"
 #include "Utils/membind.hh"
+#include "Utils/hexdump.hh"
 #include "Packets/DefaultBundle/EthernetPacket.hh"
 
 //#include "Sniffer.mpp"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    static const unsigned BLOCK_SIZE = 16;
-
-    template <class Iterator>
-    void hexdump(Iterator i, Iterator const & i_end)
-    {
-        unsigned offset (0);
-        std::string ascii;
-        for (; i != i_end; ++i, ++offset) {
-            switch (offset % BLOCK_SIZE) {
-            case 0:
-                if (!ascii.empty()) {
-                    std::cout << "  " << ascii << "\n";
-                    ascii = "";
-                }
-                std::cout << "  "
-                          << std::hex << std::setw(4) << std::setfill('0')
-                          << offset << ' ';
-                break;
-            case BLOCK_SIZE/2:
-                std::cout << " ";
-                ascii += ' ';
-                break;
-            }
-            std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0')
-                      << unsigned(*i);
-            ascii += (*i >= ' ' && *i < 126) ? *i : '.';
-        }
-        if (!ascii.empty()) {
-            for (; (offset % BLOCK_SIZE) != 0; ++offset) {
-                if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
-                    std::cout << " ";
-                std::cout << "   ";
-            }
-            std::cout << "  " << ascii << "\n";
-        }
-        std::cout << std::dec;
-    }
-
-}
-
 int loop_main (int argc, char const * argv[])
 {
     try {
@@ -92,8 +51,10 @@ int loop_main (int argc, char const * argv[])
                                              senf::EthernetPacket::noinit));
             sock.read(packet.data(),0);
             packet.dump(std::cout);
-            hexdump(packet.last().data().begin(),
-                    packet.last().data().end());
+            senf::hexdump(
+                    packet.last().data().begin(),
+                    packet.last().data().end(),
+                    std::cout);
             std::cout << "\n\n";
         }
     }
@@ -127,8 +88,10 @@ private:
             senf::EthernetPacket::create(senf::EthernetPacket::noinit));
         sock.read(packet.data(),0);
         packet.dump(std::cout);
-        hexdump(packet.last().data().begin(),
-                packet.last().data().end());
+        senf::hexdump(
+                packet.last().data().begin(),
+                packet.last().data().end(),
+                std::cout);
         std::cout << "\n\n";
     }
 };
index 474b40f..b0670e6 100644 (file)
@@ -38,6 +38,9 @@ namespace senf {
 
     <dt>\ref TypeIdValue</dt><dd>class wrapping a typeid in a way that it can be used like any other
     value type, e.g. as the key in a map.</dd>
+    
+    <dt>\ref hexdump<dt>
+        <dd>a simple but usefull function to write binary data in in hexadecimal format.</dd>
 
     </dl>
  */
diff --git a/Utils/hexdump.ct b/Utils/hexdump.ct
new file mode 100644 (file)
index 0000000..350c8c5
--- /dev/null
@@ -0,0 +1,84 @@
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief hexdump non-inline template implementation */
+
+//#include "hexdump.ih"
+
+// Custom includes
+#include <iomanip> 
+
+//#include "hexdump.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+template <class Iterator>
+prefix_ void senf::hexdump(Iterator i, Iterator const & i_end, std::ostream& stream, unsigned block_size=16)
+{
+    unsigned offset (0);
+    std::string ascii;
+    for (; i != i_end; ++i, ++offset) {
+        switch ((offset % block_size)==0) {
+        case true:
+            if (!ascii.empty()) {
+                stream << "  " << ascii << "\n";
+                ascii = "";
+            }
+            stream << "  "
+                      << std::hex << std::setw(4) << std::setfill('0')
+                      << offset << ' ';
+            break;
+        case false:
+            stream << " ";
+            ascii += ' ';
+            break;
+        }
+        stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
+                  << unsigned(*i);
+        ascii += (*i >= ' ' && *i < 126) ? *i : '.';
+    }
+    if (!ascii.empty()) {
+        for (; (offset % block_size) != 0; ++offset) {
+            if ((offset % block_size) == block_size/2)
+                stream << " ";
+            stream << "   ";
+        }
+        stream << "  " << ascii << "\n";
+    }
+    stream << std::dec;
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "hexdump.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
diff --git a/Utils/hexdump.hh b/Utils/hexdump.hh
new file mode 100644 (file)
index 0000000..9001097
--- /dev/null
@@ -0,0 +1,59 @@
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief public header for hexdump */
+
+#ifndef HH_hexdump_
+#define HH_hexdump_ 1
+
+// Custom includes
+#include <iostream>
+
+//#include "hexdump.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace senf {
+
+    /** \brief write the contents from Iterator i to i_end to the output stream in hexadecimal format.
+     */
+    template <class Iterator>
+    void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream, unsigned block_size=16);
+}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "hexdump.cci"
+#include "hexdump.ct"
+//#include "hexdump.cti"
+//#include "hexdump.mpp"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End: