/*
 * Copyright (c) 2004  Brian S. Dean <bsd@bdmicro.com>
 * 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 BRIAN S. DEAN ``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 BRIAN S. DEAN 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.
 * 
 */

/* $Id: hexdump.c,v 1.1 2010/10/19 05:18:47 bsd Exp $ */

#include <stdio.h>
#include <ctype.h>


#include "hexdump.h"



/*
 * part of hexdump - format the hex data part of the line
 */
int hexdump_line(char * buffer, unsigned char * p, int n, int pad)
{
  char * hexdata = "0123456789abcdef";
  char * b;
  uint8_t i, j;

  b = buffer;

  j = 0;
  for (i=0; i<n; i++) {
    if (i && ((i % 8) == 0))
      b[j++] = ' ';
    b[j++] = hexdata[(*(p+i) & 0xf0) >> 4];
    b[j++] = hexdata[(*(p+i) & 0x0f)];
    if (i < 15)
      b[j++] = ' ';
  }

  for (i=j; i<pad; i++)
    b[i] = ' ';

  b[i] = 0;

  for (i=0; i<pad; i++) {
    if (!((b[i] == '0') || (b[i] == ' ')))
      return 0;
  }

  return 1;
}


/*
 * part of hexdump - format ASCII part of the line
 */
int chardump_line(char * buffer, unsigned char * p, int n, int pad)
{
  uint8_t i;

  for (i=0; i<n; i++) {
    buffer[i] = *(p+i);
    if ((buffer[i] != ' ') && !(isalpha(buffer[i]) || isdigit(buffer[i]) || 
                                ispunct(buffer[i]))) {
      buffer[i] = '.';
    }
  }

  for (i=n; i<pad; i++)
    buffer[i] = ' ';

  buffer[i] = 0;

  return 0;
}


/*
 * display a hex dump in the usual format with each line containing 16
 * bytes of hex data followed by ASCII representation 
 */
char hxd_dst1[64];
char hxd_dst2[32];
int hexdump_buf(uint16_t startaddr, unsigned char * buf, uint16_t len)
{
  uint16_t addr;
  uint16_t i, n;
  unsigned char * p;

  addr = startaddr;
  i = 0;
  p = (unsigned char *)buf;
  while (len) {
    n = 16;
    if (n > len)
      n = len;
    hexdump_line(hxd_dst1, p, n, 48);
    chardump_line(hxd_dst2, p, n, 16);
    printf("%04x  %s  |%s|\n", addr, hxd_dst1, hxd_dst2);
    len -= n;
    addr += n;
    p += n;
  }

  return 0;
}



