Skip to content

Commit

Permalink
Add helpers for working with Base16 (i.e. hex) encoded data.
Browse files Browse the repository at this point in the history
  • Loading branch information
caladri committed Jun 13, 2016
1 parent 37cc319 commit 15b77b7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions LAYOUT
Expand Up @@ -4,6 +4,7 @@ Directory Description
============= =============================================================
common Non-specific functionality that is of general interest, e.g.
the logging system, test infrastructure and Buffer class.
common/data Data formats and encodings.
common/thread Threading infrastructure.
common/timer Time sampling infrastructure.
common/uuid UUID API.
Expand Down
1 change: 1 addition & 0 deletions common/Makefile
@@ -1,3 +1,4 @@
SUBDIR+=data
SUBDIR+=thread
SUBDIR+=timer
SUBDIR+=uuid
Expand Down
3 changes: 3 additions & 0 deletions common/data/Makefile
@@ -0,0 +1,3 @@
SUBDIR+=

include ../../common/subdir.mk
107 changes: 107 additions & 0 deletions common/data/base16.h
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2016 Juli Mallett. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef COMMON_DATA_BASE16_H
#define COMMON_DATA_BASE16_H

/*
* Base16 (i.e. hex) encoding and decoding for Buffers.
*/
struct Base16 {
static bool decode(Buffer *out, const Buffer *in)
{
if (in->length() % 2 != 0) {
ERROR("/base16") << "Refusing to decode data with odd number of hex digits.";
return (false);
}

/*
* We use a temporary buffer rather than maintaining a
* lot of internal state to avoid copies. The Buffer
* code itself much more sophisticated than we need to
* be here and gets the job done easily.
*/
Buffer encoded(*in);
Buffer decoded;
while (encoded.length() >= 2) {
uint8_t d[2];

encoded.moveout(d, sizeof d);

uint8_t b = 0x00;

if (d[0] >= '0' && d[0] <= '9')
b |= d[0] - '0';
else if (d[0] >= 'a' && d[0] <= 'f')
b |= 0xa + (d[0] - 'a');
else if (d[0] >= 'A' && d[0] <= 'F')
b |= 0xa + (d[0] - 'A');
else {
ERROR("/base16") << "Could not decode data with malformed hex digit (0 in pair.)";
return (false);
}
b <<= 4;

if (d[1] >= '0' && d[1] <= '9')
b |= d[1] - '0';
else if (d[1] >= 'a' && d[1] <= 'f')
b |= 0xa + (d[1] - 'a');
else if (d[1] >= 'A' && d[1] <= 'F')
b |= 0xa + (d[1] - 'A');
else {
ERROR("/base16") << "Could not decode data with malformed hex digit (1 in pair.)";
return (false);
}

decoded.append(b);
}

decoded.moveout(out);
return (true);
}

static Buffer encode(const Buffer *in)
{
static const char hexchars[] = "0123456789abcdef";
Buffer out;

Buffer::SegmentIterator iter(in->segments());
while (!iter.end()) {
const BufferSegment *seg = *iter;
const uint8_t *p;

for (p = seg->data(); p < seg->end(); p++) {
out.append((uint8_t)hexchars[(*p & 0xf0) >> 4]);
out.append((uint8_t)hexchars[*p & 0xf]);
}

iter.next();
}

return (out);
}
};

#endif /* !COMMON_DATA_BASE16_H */

0 comments on commit 15b77b7

Please sign in to comment.