C/Linux :: 리눅스에서 파일 덤프... dump.c

2010/12/05 23:37

리눅스 프로그래밍 과제로 작성했던 dump 프로그램입니다.

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE 16

char * getDump (char *data, int size, char *out)
{
int offs;

for (offs = 0; offs < size; offs++)
{
sprintf (&out[offs * 3], "%02x ", (unsigned char)data[offs]);
}

return out;
}

char * getASCII (char *data, int size, char *out)
{
int offs;

for (offs = 0; offs < size; offs++)
{
if (data[offs] < 0x20 || data[offs] >= 0x7f)
sprintf (&out[offs], "%c", '.');
else
sprintf (&out[offs], "%c", data[offs]);
}

return out;
}

int main (int argc, char **argv)
{
char buf[BUFSIZE + 1] = {0};
char dumpbuf[BUFSIZE * 3 + 1] = {0};
char asciibuf[BUFSIZE + 1] = {0};
int fdes;
int offs = 0;

if (argc < 2)
{
printf ("[*] USAGE : $%s [file]\n", argv[0]);
exit (1);
}

fdes = open (argv[1], O_RDONLY);
if (fdes == -1)
{
perror ("[*] FILE");
exit (1);
}

while (read (fdes, buf, BUFSIZE) > 0)
{
printf ("%08x: %s %s\n", \
(offs++) * BUFSIZE, \
getDump (buf, BUFSIZE, dumpbuf), \
getASCII (buf, BUFSIZE, asciibuf));
memset (buf, 0, BUFSIZE);
}

close (fdes);
return 0;
}


사용자 삽입 이미지

크리에이티브 커먼즈 라이센스
Creative Commons License

6l4ck3y3 0x08 과거의 글모음/내 머리 속의 노트 , , , , ,

Trackback Address:http://hisjournal.net/blog/trackback/347