diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-10-27 04:01:46 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-10-27 04:01:46 +0000 |
commit | 029b5d159023f4dad607359dbfaa2479e21fe9e5 (patch) | |
tree | 99c5ad1d6f4c77ce1a656f63194df442e0fdfded | |
parent | b57332230fb39c38ae1806348dbc4cd6c725d5db (diff) | |
download | libbu++-029b5d159023f4dad607359dbfaa2479e21fe9e5.tar.gz libbu++-029b5d159023f4dad607359dbfaa2479e21fe9e5.tar.bz2 libbu++-029b5d159023f4dad607359dbfaa2479e21fe9e5.tar.xz libbu++-029b5d159023f4dad607359dbfaa2479e21fe9e5.zip |
Added simple hex encoder/decoder filter.
-rw-r--r-- | src/hex.cpp | 69 | ||||
-rw-r--r-- | src/hex.h | 57 |
2 files changed, 126 insertions, 0 deletions
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/hex.h" | ||
9 | |||
10 | Bu::Hex::Hex( Bu::Stream &rNext, bool bUpperCase, int iChunk ) : | ||
11 | Bu::Filter( rNext ), | ||
12 | iChunk( iChunk ), | ||
13 | iPos( 0 ), | ||
14 | iIn( 0 ), | ||
15 | sChrs(bUpperCase?"0123456789ABCDEF":"0123456789abcdef") | ||
16 | { | ||
17 | } | ||
18 | |||
19 | Bu::Hex::~Hex() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | void Bu::Hex::start() | ||
24 | { | ||
25 | iPos = iIn = 0; | ||
26 | } | ||
27 | |||
28 | Bu::size Bu::Hex::stop() | ||
29 | { | ||
30 | return iPos; | ||
31 | } | ||
32 | |||
33 | Bu::size Bu::Hex::read( void *pBuf, Bu::size iBytes ) | ||
34 | { | ||
35 | Bu::size j; | ||
36 | uint8_t *puBuf = (uint8_t *)pBuf; | ||
37 | for( j = 0; j < iBytes; j++ ) | ||
38 | { | ||
39 | for(; iIn < 2; iIn++ ) | ||
40 | { | ||
41 | if( rNext.read( &cIn[iIn], 1 ) == 0 ) | ||
42 | return j; | ||
43 | if( cIn[iIn] == ' ' || cIn[iIn] == '\t' || | ||
44 | cIn[iIn] == '\n' || cIn[iIn] == '\r' ) | ||
45 | iIn--; | ||
46 | } | ||
47 | #define chr2nibble( c ) ((c>='0'&&c<='9')?(c-'0'):((c|0x60)-'a'+10)) | ||
48 | puBuf[j] = ((chr2nibble(cIn[0])<<4)|chr2nibble(cIn[1])); | ||
49 | iIn = 0; | ||
50 | } | ||
51 | return j; | ||
52 | } | ||
53 | |||
54 | Bu::size Bu::Hex::write( const void *pBuf, Bu::size iBytes ) | ||
55 | { | ||
56 | char cOut[2]; | ||
57 | uint8_t *puBuf = (uint8_t *)pBuf; | ||
58 | for( Bu::size j = 0; j < iBytes; j++ ) | ||
59 | { | ||
60 | cOut[0] = sChrs[(puBuf[j]&0xf0)>>4]; | ||
61 | cOut[1] = sChrs[(puBuf[j]&0x0f)]; | ||
62 | if( iChunk > 0 && iPos%iChunk == 0 && iPos>0 ) | ||
63 | rNext.write(" ", 1 ); | ||
64 | rNext.write( cOut, 2 ); | ||
65 | iPos++; | ||
66 | } | ||
67 | return iBytes; | ||
68 | } | ||
69 | |||
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_HEX_H | ||
9 | #define BU_HEX_H | ||
10 | |||
11 | #include "bu/filter.h" | ||
12 | |||
13 | namespace Bu | ||
14 | { | ||
15 | /** | ||
16 | * This very simple filter encodes to/decodes from hex encoded string data. | ||
17 | * The primary use of this filter is in debugging, use it with | ||
18 | * Bu::encodeStr to easily create hex dumps of string data, even other raw | ||
19 | * structures. | ||
20 | * | ||
21 | *@code | ||
22 | Bu::println("Hexdump: " + Bu::encodeStr<Bu::Hex>("Test data ;)") ); | ||
23 | @endcode | ||
24 | * Or... | ||
25 | *@code | ||
26 | complex_struct data; | ||
27 | ... | ||
28 | Bu::println("Hexdump: " + | ||
29 | Bu::encodeStr<Bu::Hex>( | ||
30 | Bu::String( &data, sizeof(data) ) | ||
31 | ) | ||
32 | ); | ||
33 | @endcode | ||
34 | **/ | ||
35 | class Hex : public Bu::Filter | ||
36 | { | ||
37 | public: | ||
38 | Hex( Bu::Stream &rNext, bool bUpperCase=false, int iChunk=-1 ); | ||
39 | virtual ~Hex(); | ||
40 | |||
41 | virtual void start(); | ||
42 | virtual Bu::size stop(); | ||
43 | |||
44 | virtual Bu::size read( void *pBuf, Bu::size iBytes ); | ||
45 | virtual Bu::size write( const void *pBuf, Bu::size iBytes ); | ||
46 | using Bu::Stream::write; | ||
47 | |||
48 | private: | ||
49 | int iChunk; | ||
50 | Bu::size iPos; | ||
51 | char cIn[2]; | ||
52 | int iIn; | ||
53 | const char *sChrs; | ||
54 | }; | ||
55 | }; | ||
56 | |||
57 | #endif | ||