/*
 * Tentun - covert tunneling project
 *
 * Copyright (C) 2003 David Gordon
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 * Define some debugging functions.
 */


#include "debug.h"
#include "stdio.h"

extern int debuglevel;


void dump_buffer_in_hex(int debug, const char *buf, int size)
{
    unsigned char *buffer = (unsigned char *) buf;
    int i;

    /* quick exit if debuglevel not set */
    if ( DEBUG_TENTUN && !(debuglevel & debug) ) return;

    printf("Dump: ");

    for (i = 0; i < size; i++) {
	printf("%X ", buffer[i]);
    }

    printf("\n");
}


/* if possible, print ascii characters, otherwise hex */
void dump_buffer_ascii(int debug, const char *buf, int size)
{
    unsigned char *buffer = (unsigned char *) buf;
    int i;

    /* quick exit if debuglevel not set */
    if ( DEBUG_TENTUN && !(debuglevel & debug) ) return;

    printf("Dump: ");

    for (i = 0; i < size; i++) {
      if (buffer[i] == ' ') printf("\n");
      else {
	if (buffer[i] > 31 && buffer[i] < 126) {
	  printf("%c", buffer[i]);
	}
	else printf("%X ", buffer[i]);
      }
    }

    printf("\n");
}
