#ifndef __HEXDUMPER_H__ #define __HEXDUMPER_H__ #include #include // object to output very configurable hexdumps. // // todo: add option to specify separator // xx:xx:xx:xx ':' // xx xx xx xx ' ' // xxxxxxxx none class HexDumper { public: static const size_t npos= (size_t)-1; enum DumpUnitType { DUMPUNIT_BYTE=0, DUMPUNIT_WORD=1, DUMPUNIT_DWORD=2 }; enum DumpFormat { DUMP_HEX_ASCII, // [offset] 50 51 52 53 0d 0a 00 01 PQRS.... DUMP_HEX, // [offset] 50 51 52 53 0d 0a 00 01 DUMP_ASCII, // [offset] PQRS.... DUMP_STRINGS, // [offset] "PQRS",0d,0a,00,01 DUMP_RAW, // [offset] PQRS^M^J^@^A }; HexDumper(size_t ofs=0, DumpUnitType t=DUMPUNIT_BYTE, DumpFormat f=DUMP_HEX_ASCII, bool withoffset=true, bool summarize=true) : m_offset(ofs), m_dumpunit(t), m_dumpformat(f), m_bWithOffset(withoffset), m_bSummarize(summarize), m_separator(' ') { } ~HexDumper(); // sets current offset HexDumper& offset(size_t ofs) { m_offset= ofs; return *this; } // sets nr of items printed per line // not applicable for 'strings', and 'raw' output // 0 means all on one line. HexDumper& width(size_t width) { m_width= width; return *this; } // sets dump units HexDumper& unit(DumpUnitType t) { m_dumpunit= t; return *this; } HexDumper& byte() { m_dumpunit= DUMPUNIT_BYTE; return *this; } HexDumper& word() { m_dumpunit= DUMPUNIT_WORD; return *this; } HexDumper& dword() { m_dumpunit= DUMPUNIT_DWORD; return *this; } // sets type of output HexDumper& format(DumpFormat t) { m_dumpformat= t; return *this; } HexDumper& hexascii() { m_dumpformat= DUMP_HEX_ASCII ; return *this; } HexDumper& hex() { m_dumpformat= DUMP_HEX ; return *this; } HexDumper& ascii() { m_dumpformat= DUMP_ASCII ; return *this; } HexDumper& strings() { m_dumpformat= DUMP_STRINGS ; return *this; } // raw, hash, crc are specializations handled by the caller. HexDumper& withoffset(bool flag=true) { m_bWithOffset= flag; return *this; } HexDumper& summarize(bool flag=true) { m_bSummarize= flag; return *this; } // separator HexDumper& separator(char c) { m_separator= c; return *this; } template void dump(const std::vector& buf, size_t ofs=0, size_t count=npos); template void dump(typename std::vector::const_iterator start, typename std::vector::const_iterator end); template void dump(const T*buf, size_t len); template void dump(const T*start, const T*end); size_t dumpline(const unsigned char*buf, size_t len); // flush hexdumper state void end(); // retrieve current output buffer std::string getstring(); private: typedef std::pair line_item_t; std::vector m_out; bool m_bWithOffset; bool m_bSummarize; char m_separator; DumpFormat m_dumpformat; DumpUnitType m_dumpunit; size_t m_offset; size_t m_width; }; template void HexDumper::dump(const std::vector& buf, size_t ofs/*=0*/, size_t count/*=npos*/) { dump((const void*)&buf[ofs], count*sizeof(T)); } template void HexDumper::dump(typename std::vector::const_iterator start, typename std::vector::const_iterator end) { dump((const void*)&(*start), (end-start)*sizeof(T)); } template void HexDumper::dump(const T*buf, size_t len) { // todo } template void HexDumper::dump(const T*start, const T*end) { // todo } #endif