From 029b5d159023f4dad607359dbfaa2479e21fe9e5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 27 Oct 2011 04:01:46 +0000 Subject: Added simple hex encoder/decoder filter. --- src/hex.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hex.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/hex.cpp create mode 100644 src/hex.h (limited to 'src') diff --git a/src/hex.cpp b/src/hex.cpp new file mode 100644 index 0000000..2a04c6f --- /dev/null +++ b/src/hex.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/hex.h" + +Bu::Hex::Hex( Bu::Stream &rNext, bool bUpperCase, int iChunk ) : + Bu::Filter( rNext ), + iChunk( iChunk ), + iPos( 0 ), + iIn( 0 ), + sChrs(bUpperCase?"0123456789ABCDEF":"0123456789abcdef") +{ +} + +Bu::Hex::~Hex() +{ +} + +void Bu::Hex::start() +{ + iPos = iIn = 0; +} + +Bu::size Bu::Hex::stop() +{ + return iPos; +} + +Bu::size Bu::Hex::read( void *pBuf, Bu::size iBytes ) +{ + Bu::size j; + uint8_t *puBuf = (uint8_t *)pBuf; + for( j = 0; j < iBytes; j++ ) + { + for(; iIn < 2; iIn++ ) + { + if( rNext.read( &cIn[iIn], 1 ) == 0 ) + return j; + if( cIn[iIn] == ' ' || cIn[iIn] == '\t' || + cIn[iIn] == '\n' || cIn[iIn] == '\r' ) + iIn--; + } +#define chr2nibble( c ) ((c>='0'&&c<='9')?(c-'0'):((c|0x60)-'a'+10)) + puBuf[j] = ((chr2nibble(cIn[0])<<4)|chr2nibble(cIn[1])); + iIn = 0; + } + return j; +} + +Bu::size Bu::Hex::write( const void *pBuf, Bu::size iBytes ) +{ + char cOut[2]; + uint8_t *puBuf = (uint8_t *)pBuf; + for( Bu::size j = 0; j < iBytes; j++ ) + { + cOut[0] = sChrs[(puBuf[j]&0xf0)>>4]; + cOut[1] = sChrs[(puBuf[j]&0x0f)]; + if( iChunk > 0 && iPos%iChunk == 0 && iPos>0 ) + rNext.write(" ", 1 ); + rNext.write( cOut, 2 ); + iPos++; + } + return iBytes; +} + diff --git a/src/hex.h b/src/hex.h new file mode 100644 index 0000000..3595fae --- /dev/null +++ b/src/hex.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_HEX_H +#define BU_HEX_H + +#include "bu/filter.h" + +namespace Bu +{ + /** + * This very simple filter encodes to/decodes from hex encoded string data. + * The primary use of this filter is in debugging, use it with + * Bu::encodeStr to easily create hex dumps of string data, even other raw + * structures. + * + *@code + Bu::println("Hexdump: " + Bu::encodeStr("Test data ;)") ); + @endcode + * Or... + *@code + complex_struct data; + ... + Bu::println("Hexdump: " + + Bu::encodeStr( + Bu::String( &data, sizeof(data) ) + ) + ); + @endcode + **/ + class Hex : public Bu::Filter + { + public: + Hex( Bu::Stream &rNext, bool bUpperCase=false, int iChunk=-1 ); + virtual ~Hex(); + + virtual void start(); + virtual Bu::size stop(); + + virtual Bu::size read( void *pBuf, Bu::size iBytes ); + virtual Bu::size write( const void *pBuf, Bu::size iBytes ); + using Bu::Stream::write; + + private: + int iChunk; + Bu::size iPos; + char cIn[2]; + int iIn; + const char *sChrs; + }; +}; + +#endif -- cgit v1.2.3