【发布时间】:2020-08-02 12:13:59
【问题描述】:
我正在尝试用 C 重新创建一个 hexdump 程序。这就是我目前所拥有的
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef unsigned char byte;
void openfile(FILE **fptr, char *fname)
{
*fptr = fopen(fname, "r");
if(fptr == NULL)
{
fprintf(stderr, "How dare you... \n");
exit(-1);
}
}
int readContents(FILE *fptr, int *filesize, byte *array)
{
return (fread(array, 1, *filesize, fptr)); // read all bytes from the file
}
int printit(FILE **fptr, int *filesize, byte *array)
{
byte sixteen[17];
readContents(*fptr, filesize, array);
int idx;
for(idx = 0; idx <= *filesize; idx++)
{
if((idx%16)==0)
{
printf("%08x ", idx);
}
if((idx%8)==0)
{
printf(" ");
}
printf("%02x ", array[idx]);
if((idx+1)%16==0)
{
printf("\n");
}
}
for(idx = 0; idx <= *filesize; idx++)
{
if((idx%16)==0)
printf("%c ", array[idx]);
}
}
int main(int argc, char *argv[])
{
char filename[30];
if(argc > 1)
{
strcpy(filename, argv[1]);
}
else
{
printf("Please enter a filename: ");
scanf(" %[^\n]s", filename);
}
FILE *ptr;
openfile(&ptr, filename);
fseek(ptr, 0, SEEK_END);
int filelen = ftell(ptr);
printf("Length of file is: %d\n", filelen);
fseek(ptr, 0, SEEK_SET);
byte *bytes = malloc(filelen);
printit(&ptr, &filelen, bytes);
printf("\n");
free(bytes);
fclose(ptr);
}
偏移位置和十六进制成功地出现在输出中,但我似乎无法弄清楚我应该如何在右侧添加一个包含文件内容明文的列
这是没有它的输出的样子
00000010 03 00 3e 00 01 00 00 00 70 09 00 00 00 00 00 00
00000020 40 00 00 00 00 00 00 00 98 2b 00 00 00 00 00 00
00000030 00 00 00 00 40 00 38 00 09 00 40 00 1d 00 1c 00
00000040 06 00 00 00 04 00 00 00 40 00 00 00 00 00 00 00
00000050 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00
00000060 f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00
00000070 08 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00
00000080 38 02 00 00 00 00 00 00 38 02 00 00 00 00 00 00
00000090 38 02 00 00 00 00 00 00 1c 00 00 00 00 00 00 00
000000a0 1c 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
000000b0 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000000d0 38 0f 00 00 00 00 00 00 38 0f 00 00 00 00 00 00
000000e0 00 00 20 00 00 00 00 00 01 00 00 00 06 00 00 00
000000f0 50 1d 00 00 00 00 00 00 50 1d 20 00 00 00 00 00
00000100 50 1d 20 00 00 00 00 00 c0 02 00 00 00 00 00 00
00000110 e0 02 00 00 00 00 00 00 00 00 20 00 00 00 00 00
00000120 02 00 00 00 06 00 00 00 60 1d 00 00 00 00 00 00
00000130 60 1d 20 00 00 00 00 00 60 1d 20 00 00 00 00 00
00000140 f0 01 00 00 00 00 00 00 f0 01 00 00 00 00 00 00
00000150 08 00 00 00 00 00 00 00 04 00 00 00 04 00 00 00
00000160 54 02 00 00 00 00 00 00 54 02 00 00 00 00 00 00
00000170 54 02 00 00 00 00 00 00 44 00 00 00 00 00 00 00
00000180 44 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
00000190 50 e5 74 64 04 00 00 00 f0 0d 00 00 00 00 00 00
000001a0 f0 0d 00 00 00 00 00 00 f0 0d 00 00 00 00 00 00
000001b0 3c 00 00 00 00 00 00 00 3c 00 00 00 00 00 00 00
000001c0 04 00 00 00 00 00 00 00 51 e5 74 64 06 00 00 00
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
这是明文出现在末尾的地方
@ @ � 8 8 8 P P � ` � T T D P � < R P � d o G G ` J o + D u �
� h . e n a c o s i i i L . I C n e b u P p � � p � � � � � � � � � � � � � � � � � � � � � � 1 � � H 9 � ] H ) H � ] � � � U � � � � 5 � � H � U � � � � � � H , � � � � H � � � � � d � A � � L A � n : D . V x f F d f � X � �
� � � A p
�
� � � � � ` � � � � 6 V G . 4 8 t � � 0 X ` � P ` � 0 7 X m P � � � ` � � � K g � � � � ' C e 0 � + f z w � � � X f m o o d a n n e A _ M _ F L _ p . a . e r 5 @ t L G b L a @ l _ _ s c @ b o 5 @ @ T e b I f 2 I a r t . n . s n e l n r m f m c 8 T t � � � � � 0 B X p ` p � 0 P X ` P 0 @ x �
我也碰巧在读一个 ELF 文件,仅供参考
编辑:我忘了粘贴#include stdio.h
【问题讨论】:
-
贴出的代码无法编译!该代码使用的函数如下:
printf()和fopen()和fclose()和fread()以及定义的项目,如FILE*和NULL。要使用这些项目,代码需要以下语句:#include <stdio.h> -
函数:
fread()返回一个size_t,而不是int,而函数:printit()忽略了函数的返回值:readContents()为什么是函数:@ 987654336@ 在不修改该指针时被传递FILE **? -
您输出 16 位十六进制值,当您完成输出最后一个十六进制值时,然后打印一个可选的分隔符,例如
" | ",然后循环遍历这 16 个字节中的每一个输出可打印值if (isprint()),否则输出' ',然后为该输出行打印'\n'。 -
关于:
for(idx = 0; idx <= *filesize; idx++) { if((idx%16)==0) printf("%c ", array[idx]); }这只会输出一个字符。注意<=将执行一个太多的循环。建议:<。建议:for(idx = 0; idx % 16; idx++) { if( isprint( array[idx] ) { printf("%c ", array[idx]); } else { printf( "%c ", '.' ); } } -
关于错误信息:
fprintf(stderr, "How dare you... \n");当错误来自 C 库函数时,应将您的错误信息和系统认为发生错误的文本原因输出到stderr。建议:perror( "How dare you..." );处理这两个动作。此外,“你怎么敢……”告诉用户没有任何用处。建议:perror( "fopen failed" );