WvStreams
wvhex.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Hex encoder and decoder.
6 */
7#include "wvhex.h"
8#include <ctype.h>
9
10
11static inline char tohex(int digit, char alphabase)
12{
13 return (digit < 10 ? '0' : alphabase) + digit;
14}
15
16static inline int fromhex(char digit)
17{
18 if (isdigit(digit))
19 return digit - '0';
20 if (isupper(digit))
21 return digit - 'A' + 10;
22 return digit - 'a' + 10;
23}
24
25/***** WvHexEncoder *****/
26
27WvHexEncoder::WvHexEncoder(bool use_uppercase)
28{
29 alphabase = (use_uppercase ? 'A' : 'a') - 10;
30 _reset();
31}
32
33
35{
36 return true;
37}
38
39
40bool WvHexEncoder::_encode(WvBuf &in, WvBuf &out, bool flush)
41{
42 while (in.used() != 0)
43 {
44 unsigned char byte = in.getch();
45 out.putch(tohex(byte >> 4, alphabase));
46 out.putch(tohex(byte & 15, alphabase));
47 }
48 return true;
49}
50
51
52/***** WvHexDecoder *****/
53
55{
56 _reset();
57}
58
59
61{
62 issecond = false;
63 first = 0;
64 return true;
65}
66
67
68bool WvHexDecoder::_encode(WvBuf &in, WvBuf &out, bool flush)
69{
70 while (in.used() != 0)
71 {
72 char ch = (char) in.getch();
73 if (isxdigit(ch))
74 {
75 int digit = fromhex(ch);
76 if ( (issecond = ! issecond) )
77 first = digit;
78 else
79 out.putch(first << 4 | digit);
80 continue;
81 }
82 if (isspace(ch))
83 continue;
84 seterror("invalid character '%s' in hex input", ch);
85 return false;
86 }
87 if (flush && issecond)
88 return false; // not enough hex digits supplied
89 return true;
90}
91
92
93/*** Compatibility ***/
94
95void hexify(char *obuf, const void *ibuf, size_t len)
96{
97 size_t outlen = len * 2 + 1;
98 WvHexEncoder(false /*use_uppercase*/).
99 flushmemmem(ibuf, len, obuf, & outlen);
100 obuf[outlen] = '\0';
101}
102
103
104void unhexify(void *obuf, const char *ibuf)
105{
106 size_t inlen = strlen(ibuf);
107 size_t outlen = inlen / 2;
108 WvHexDecoder().flushmemmem(ibuf, inlen, obuf, & outlen);
109}
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition: wvbufbase.h:92
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:24
int getch()
Returns a single character from the buffer as an int.
Definition: wvbuf.h:66
void putch(int ch)
Puts a single character into the buffer as an int.
Definition: wvbuf.h:76
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
Definition: wvencoder.h:375
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.
Definition: wvencoder.cc:132
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Definition: wvencoder.h:163
A hex decoder.
Definition: wvhex.h:54
virtual bool _reset()
Template method implementation of reset().
Definition: wvhex.cc:60
WvHexDecoder()
Creates a hex decoder.
Definition: wvhex.cc:54
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvhex.cc:68
A hex encoder.
Definition: wvhex.h:22
WvHexEncoder(bool use_uppercase=false)
Creates a hex encoder.
Definition: wvhex.cc:27
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvhex.cc:40
virtual bool _reset()
Template method implementation of reset().
Definition: wvhex.cc:34
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 '...
Definition: wvhex.cc:95
void unhexify(void *obuf, const char *ibuf)
Reverse the operation performed by hexify().
Definition: wvhex.cc:104