00001 /* -*- Mode: C++ -*- 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * Hex encoder and hex decoder. 00006 */ 00007 #ifndef __WVHEX_H 00008 #define __WVHEX_H 00009 00010 #include "wvencoder.h" 00011 00012 /** 00013 * A hex encoder. 00014 * 00015 * The input data is transformed into a sequence of hexadecimal 00016 * characters. 00017 * 00018 * Supports reset(). 00019 * 00020 */ 00021 class WvHexEncoder : public WvEncoder 00022 { 00023 char alphabase; 00024 00025 public: 00026 /** 00027 * Creates a hex encoder. 00028 * 00029 * "use_uppercase" is if true, outputs hex codes A through Z 00030 * in upper case, otherwise output them in lower case 00031 * (the default) 00032 */ 00033 WvHexEncoder(bool use_uppercase = false); 00034 virtual ~WvHexEncoder() { } 00035 00036 protected: 00037 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush); 00038 virtual bool _reset(); // supported 00039 }; 00040 00041 00042 /** 00043 * A hex decoder. 00044 * 00045 * The input hex characters are paired and decoded into the 00046 * corresponding byte stream. Whitespace is skipped as is the 00047 * case of the hex codes A through Z. Other characters cause the 00048 * encoder to flag an error. 00049 * 00050 * Supports reset(). 00051 * 00052 */ 00053 class WvHexDecoder : public WvEncoder 00054 { 00055 bool issecond; 00056 int first; 00057 00058 public: 00059 /** Creates a hex decoder. */ 00060 WvHexDecoder(); 00061 virtual ~WvHexDecoder() { } 00062 00063 protected: 00064 virtual bool _encode(WvBuf &in, WvBuf &out, bool flush); 00065 virtual bool _reset(); // supported 00066 }; 00067 00068 /*** For compatibility with older code. ***/ 00069 00070 /** 00071 * Write the contents of the binary string of length 'len' pointed to by 'ibuf' 00072 * into the output buffer 'obuf' in hexadecimal format. 00073 * 00074 * For example, if len==4, ibuf=="ABCDEF", then obuf will contain "41424344" 00075 * with a terminating NULL character. 00076 * 00077 * This is useful to turn arbitrary binary into a simple printable format, so 00078 * that it can (for example) be written to a WvConf configuration file. 00079 * 00080 * obuf must be a buffer with at least (len * 2) + 1 bytes available. (two 00081 * digits for each byte of ibuf, plus a terminating NULL). 00082 */ 00083 void hexify(char *obuf, const void *ibuf, size_t len); 00084 00085 /** 00086 * Reverse the operation performed by hexify(). obuf must be a buffer large 00087 * enough to contain the entire binary output string; you can calculate this 00088 * size with (strlen(ibuf) / 2). obuf will NOT be automatically NULL-terminated. 00089 */ 00090 void unhexify(void *obuf, const char *ibuf); 00091 00092 #endif // __WVHEX_H