一个简单的 C 程序是否可以模仿默认的 'xxd' 命令,使其 diff'd 输出返回 0?



我正在尝试编写一个C可执行文件,该文件将产生与默认xxd命令相同的输出。例如,假设我有一个相当小的文本文件test.txt和一个可执行文件myxxd

因此,我首先使用进行比较

$ touch correct-xxdoutput.txt test-output.txt
$ xxd test.txt > correct-xxdoutput.txt

然后使用我的可执行文件进行相同的操作,但到不同的输出文件:

$ ./myxxd test.txt > test-output.txt
$ diff correct-xxdoutput.txt test-output.txt
$

我已经接近了一些猜测,但不知何故,我的格式总是错误的,而且我并不真正理解xxd是如何生成hexDump的。感觉我在这里采取了一种完全错误的方法,但以我目前的C知识水平,这项任务可能超出了我的潜力。

我的代码(另请参阅:https://pastebin.com/Vjkm8Wb4):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 256
//Prototypes
void hexDump(void*, int);
int main(int argc, char *argv[])
{
//Create and open filestream
FILE *myfile;
myfile =fopen(argv[1],"rb");
for ( ; ; )
{
unsigned char buffer[SIZE];
size_t n = fread(buffer, 1, SIZE, myfile);
if (n > 0)
hexDump(buffer, n);
if (n < SIZE)
break;
}
fclose(myfile);
return 0;
}

void hexDump (void *addr, int len)
{
int i;
unsigned char bufferLine[17];
unsigned char *pc = (unsigned char*)addr;
for (i = 0; i < len; i++)
{
if ((i % 16) == 0)
{
if (i != 0)
printf (" %sn", bufferLine);
if (pc[i] == 0x00) exit(0);
printf ("%08x: ", i);
}
// Prints Hexcdoes that represent each chars.
printf ("%02x", pc[i]);
if ((i % 2) == 1)
printf (" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
{
bufferLine[i % 16] = '.';
}
else
{
bufferLine[i % 16] = pc[i];
}    
bufferLine[(i % 16) + 1] = ''; //Clears the next array buffLine
}
while ((i % 16) != 0)
{
printf ("  ");
i++;
}
printf ("     %sn", bufferLine);
}

您的代码存在多个问题,包括:

  • 您没有检查是否有要打开的文件名
  • 您没有检查是否打开了名为的文件
  • 您没有处理输出偏移量的机制,因此第一个块之后的行开头的地址是错误的
  • 您的代码测试零字节,并在遇到零字节时静默退出。这太糟糕了——两次。曾经,因为一个旨在处理二进制数据的程序必须处理零字节以及从1到255的值;还有一次是因为静默退出(并声称用exit(0)引导成功)是不好的。您应该报告问题(针对标准错误,而非标准输出),并以错误状态(非零状态)退出

核心格式似乎基本上还可以;在文件末尾填充短数据行也有问题。

我想出了这个代码,它与你的代码非常相似(但经过重新格式化,至少符合我的一些风格偏见——但大多数时候我的风格与你的风格相差不远):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
void hexDump(size_t, void *, int);
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s filen", argv[0]);
exit(EXIT_FAILURE);
}
FILE *myfile = fopen(argv[1], "rb");
if (myfile == 0)
{
fprintf(stderr, "%s: failed to open file '%s' for readingn", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
unsigned char buffer[SIZE];
size_t n;
size_t offset = 0;
while ((n = fread(buffer, 1, SIZE, myfile)) > 0)
{
hexDump(offset, buffer, n);
if (n < SIZE)
break;
offset += n;
}
fclose(myfile);
return 0;
}
void hexDump(size_t offset, void *addr, int len)
{
int i;
unsigned char bufferLine[17];
unsigned char *pc = (unsigned char *)addr;
for (i = 0; i < len; i++)
{
if ((i % 16) == 0)
{
if (i != 0)
printf(" %sn", bufferLine);
// Bogus test for zero bytes!
//if (pc[i] == 0x00)
//    exit(0);
printf("%08zx: ", offset);
offset += (i % 16 == 0) ? 16 : i % 16;
}
printf("%02x", pc[i]);
if ((i % 2) == 1)
printf(" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
{
bufferLine[i % 16] = '.';
}
else
{
bufferLine[i % 16] = pc[i];
}
bufferLine[(i % 16) + 1] = '';
}
while ((i % 16) != 0)
{
printf("  ");
if (i % 2 == 1)
putchar(' ');
i++;
}
printf(" %sn", bufferLine);
}

当在原始源代码上运行并与系统xxd的输出进行比较时,没有任何差异。我还对照了一个只有16个字符的文件(abcdefghijklmno加上一行换行符)进行了检查;那里的产出也是一样的。我检查了它自己的二进制文件,发现并修复了零字节和未经通知的早期退出问题。

最新更新