what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

Utility To Dump head In Hexadecimal

Utility To Dump head In Hexadecimal
Posted Jun 30, 2009
Authored by aaron conole | Site aconole.brad-x.com

This is a simple head utility that outputs in hexadecimal format. Characters that are non-printable are replaced with the "." character.

tags | tool
systems | unix
SHA-256 | 0c5080928e90b08d6d6768e9822e4f8abf096c4a3266af155b8e438d2ed706fb

Utility To Dump head In Hexadecimal

Change Mirror Download
/**
* hex-head.c hex output of the head command.
*
* Written and Copyright (c) 2009 by Aaron Conole <apconole@yahoo.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the IMPLIED WARRANTY of MERCHANTIBILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#ifndef BUFSIZ
#define BUFSIZ 8192
#endif

const char hex_head_usage[] = ": [OPTIONS] [FILE]...\n";

int c_gi = 0, c_cnt=0;
long c_len = 0;
int scnt = 0;
char str[17];

int dump(void* b, int len, FILE *dump){
unsigned char *buf = b;
int cnt = 0;

FILE *out = stdout;

if(dump != NULL)
out = dump;

for ( ; c_gi < c_len; c_gi++ ){
if ( c_cnt % 16 == 0 ){
fprintf(out, " %s\n%04X: ", str, c_cnt);
memset(str, 0, 17);
scnt = 0;
}
if ( buf[cnt] < ' ' || buf[cnt] >= 127 )
str[scnt%16] = '.';
else
str[scnt%16] = buf[cnt];
fprintf(out, "%02X ", buf[cnt++]);
c_cnt++; scnt++; --len;
}

fflush(out);
return len;
}

int head(FILE *src, int len, int lines)
{
long lread = 0;
size_t rlen = 0;
int olen = 0;
char buf[BUFSIZ];

c_len = c_gi = c_cnt = 0;
memset(str, 0, 17);
scnt = 0;

lread = ftell(src);
len += lread;
c_len = len;

while((lines > 0) &&
(fgets(buf, sizeof(buf), src) != 0))
{
rlen = ftell(src) - lread;
lread += rlen;
if((len != -1) && (lread > len)) /*last group of bytes*/
{
rlen = rlen - (lread - len);
lines = 0;
}else if(len == -1)
{
c_len = lread;
}
olen = dump(buf, rlen, stdout);
if(buf[rlen - 1] == '\n')
lines--;
}
if(olen == 0)
fprintf(stdout, " %s\n", str);
else
fprintf(stdout, " %*s", 16+(16-olen%16)*2, str);

fflush(stdout);
fprintf(stdout, "\n");
return 0;
}

int main(int argc, char *argv[])
{
char *buf;
char opt;
int len = -1, lines = 10, tmplen, i;
char pr_head = 1;
FILE *fp;
if( argc <= 1 )
{
fprintf(stderr, "%s%s\n", argv[0], hex_head_usage);
return -1;
}

for (i = 1; i < argc; ++i)
{
if(argv[i][0] == '-')
{
opt = argv[i][1];
switch(opt)
{
case 'v':
pr_head = 1;
break;

case 'q':
pr_head = 0;
break;

case '-':
{
if(!strncmp("bytes", argv[i]+2, 5))
{
tmplen = 0;
tmplen = atoi(argv[i]+8);
len = tmplen;
}else if(!strncmp("lines", argv[i]+2, 5))
{
lines = atoi(argv[i]+8);
return -1;
}
else
{
printf("invalid options\n");
return -1;
}
}
break;
case 'c':
tmplen = 0;
if(++i < argc)
tmplen = atoi(argv[i]);
if(tmplen < 1)
{
printf("Invalid length field.\n");
return -1;
}
len = tmplen;
break;
case 'n':
tmplen = 0;
if(++i < argc)
tmplen = atoi(argv[i]);
if(tmplen < 1)
{
printf("Invalid lines field.\n");
return -1;
}
lines = tmplen;
break;
case 'h':
printf("invalid options\n");
return -1;
}
}
else
{
break;
}
}

if(i >= argc)
{
head(stdin, len, lines);
}
else
{
for(;i<argc;++i)
{
FILE *src = fopen(argv[i], "r");

if(src == NULL)
{
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[i], strerror(errno));
}
else
{
if(pr_head)
{
fprintf(stdout, "==> %s <==\n", argv[i]);
}
if(head(src, len, lines) == -1)
return -1;
if(i < argc - 1)
{
fprintf(stdout, "\n");
}
}
}
}
return 0;
}
Login or Register to add favorites

File Archive:

November 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Nov 1st
    30 Files
  • 2
    Nov 2nd
    0 Files
  • 3
    Nov 3rd
    0 Files
  • 4
    Nov 4th
    12 Files
  • 5
    Nov 5th
    44 Files
  • 6
    Nov 6th
    18 Files
  • 7
    Nov 7th
    9 Files
  • 8
    Nov 8th
    0 Files
  • 9
    Nov 9th
    0 Files
  • 10
    Nov 10th
    0 Files
  • 11
    Nov 11th
    0 Files
  • 12
    Nov 12th
    0 Files
  • 13
    Nov 13th
    0 Files
  • 14
    Nov 14th
    0 Files
  • 15
    Nov 15th
    0 Files
  • 16
    Nov 16th
    0 Files
  • 17
    Nov 17th
    0 Files
  • 18
    Nov 18th
    0 Files
  • 19
    Nov 19th
    0 Files
  • 20
    Nov 20th
    0 Files
  • 21
    Nov 21st
    0 Files
  • 22
    Nov 22nd
    0 Files
  • 23
    Nov 23rd
    0 Files
  • 24
    Nov 24th
    0 Files
  • 25
    Nov 25th
    0 Files
  • 26
    Nov 26th
    0 Files
  • 27
    Nov 27th
    0 Files
  • 28
    Nov 28th
    0 Files
  • 29
    Nov 29th
    0 Files
  • 30
    Nov 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close