exploit the possibilities
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:

April 2024

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

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close