11 static inline char tohex(
int digit,
char alphabase)
13 return (digit < 10 ?
'0' : alphabase) + digit;
16 static inline int fromhex(
char digit)
21 return digit -
'A' + 10;
22 return digit -
'a' + 10;
29 alphabase = (use_uppercase ?
'A' :
'a') - 10;
42 while (in.
used() != 0)
44 unsigned char byte = in.
getch();
45 out.
putch(tohex(
byte >> 4, alphabase));
46 out.
putch(tohex(
byte & 15, alphabase));
70 while (in.
used() != 0)
72 char ch = (char) in.
getch();
75 int digit = fromhex(ch);
76 if ( (issecond = ! issecond) )
79 out.
putch(first << 4 | digit);
84 seterror(
"invalid character '%s' in hex input", ch);
87 if (
flush && issecond)
95 void hexify(
char *obuf,
const void *ibuf,
size_t len)
97 size_t outlen = len * 2 + 1;
99 flushmemmem(ibuf, len, obuf, & outlen);
106 size_t inlen = strlen(ibuf);
107 size_t outlen = inlen / 2;
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
int getch()
Returns a single character from the buffer as an int.
void putch(int ch)
Puts a single character into the buffer as an int.
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
bool flushmemmem(const void *inmem, size_t inlen, void *outmem, size_t *outlen, bool finish=false)
Flushes data through the encoder from memory to memory.
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
virtual bool _reset()
Template method implementation of reset().
WvHexDecoder()
Creates a hex decoder.
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
WvHexEncoder(bool use_uppercase=false)
Creates a hex encoder.
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
virtual bool _reset()
Template method implementation of reset().
Hex functions for compatibility with older code.
void hexify(char *obuf, const void *ibuf, size_t len)
Write the contents of the binary string of length 'len' pointed to by 'ibuf' into the output buffer '...
void unhexify(void *obuf, const char *ibuf)
Reverse the operation performed by hexify().