00001 #ifndef CRYPTOPP_HEX_H
00002 #define CRYPTOPP_HEX_H
00003
00004 #include "cryptlib.h"
00005 #include "filters.h"
00006
00007 NAMESPACE_BEGIN(CryptoPP)
00008
00010 class HexEncoder : public Filter
00011 {
00012 public:
00013 HexEncoder(BufferedTransformation *outQueue = NULL, bool uppercase = true);
00014
00015 void Put(byte inByte)
00016 {
00017 AttachedTransformation()->Put(m_vec[inByte >> 4]);
00018 AttachedTransformation()->Put(m_vec[inByte & 0x0F]);
00019 }
00020
00021 void Put(const byte *inString, unsigned int length);
00022
00023 private:
00024 const byte *m_vec;
00025 };
00026
00028 class HexDecoder : public Filter
00029 {
00030 public:
00031 HexDecoder(BufferedTransformation *outQueue = NULL)
00032 : Filter(outQueue) {last = -1;}
00033
00034 void Put(byte inByte)
00035 {
00036 int i=ConvToNumber(inByte);
00037 if (i >= 0)
00038 {
00039 if (last >= 0)
00040 {
00041 AttachedTransformation()->Put((last << 4) | i);
00042 last = -1;
00043 }
00044 else
00045 last = i;
00046 }
00047 }
00048
00049 void Put(const byte *inString, unsigned int length);
00050
00051 void MessageEnd(int propagate=-1);
00052
00053 private:
00054 static int ConvToNumber(byte inByte)
00055 {
00056 if (inByte >= '0' && inByte <= '9')
00057 return inByte - '0';
00058 if (inByte >= 'A' && inByte <= 'F')
00059 return inByte - 'A' + 10;
00060 if (inByte >= 'a' && inByte <= 'f')
00061 return inByte - 'a' + 10;
00062 return -1;
00063 }
00064
00065 int last;
00066 };
00067
00068 NAMESPACE_END
00069
00070 #endif