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 
11 static inline char tohex(int digit, char alphabase)
12 {
13  return (digit < 10 ? '0' : alphabase) + digit;
14 }
15 
16 static 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 
27 WvHexEncoder::WvHexEncoder(bool use_uppercase)
28 {
29  alphabase = (use_uppercase ? 'A' : 'a') - 10;
30  _reset();
31 }
32 
33 
35 {
36  return true;
37 }
38 
39 
40 bool 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 
68 bool 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 
95 void 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 
104 void 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 }
wvhex.h
WvEncoder::flushmemmem
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
WvHexEncoder::WvHexEncoder
WvHexEncoder(bool use_uppercase=false)
Creates a hex encoder.
Definition: wvhex.cc:27
WvBufBase< unsigned char >::putch
void putch(int ch)
Puts a single character into the buffer as an int.
Definition: wvbuf.h:76
WvBufBase< unsigned char >::getch
int getch()
Returns a single character from the buffer as an int.
Definition: wvbuf.h:66
unhexify
void unhexify(void *obuf, const char *ibuf)
Reverse the operation performed by hexify().
Definition: wvhex.cc:104
WvHexDecoder::WvHexDecoder
WvHexDecoder()
Creates a hex decoder.
Definition: wvhex.cc:54
WvHexEncoder::_encode
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvhex.cc:40
WvHexDecoder::_encode
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvhex.cc:68
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
hexify
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
WvHexEncoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvhex.cc:34
WvHexEncoder
A hex encoder.
Definition: wvhex.h:21
WvHexDecoder
A hex decoder.
Definition: wvhex.h:53
WvHexDecoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvhex.cc:60
WvEncoder::flush
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Definition: wvencoder.h:163
WvBufBaseCommonImpl::used
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition: wvbufbase.h:92
WvEncoder::seterror
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
Definition: wvencoder.h:375