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生成 hexDumps 的。感觉就像我在这里采取了完全错误的方法,但也许这项任务超出了我目前的 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 (" %s\n", 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] = '\0'; //Clears the next array buffLine
    }

    while ((i % 16) != 0)
    {
        printf ("  ");
        i++;
    }

    printf ("     %s\n", bufferLine);
}
4

1 回答 1

2

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

  • 您不检查是否有要打开的文件名。
  • 您不检查是否打开了已命名的文件。
  • 您没有一种机制来处理输出中的偏移量,因此第一个块之后的行开头的地址是错误的。
  • 您的代码测试一个零字节并在遇到一个时静默退出。这很糟糕——两次。曾经是因为用于处理二进制数据的程序必须处理零字节以及 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 file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    FILE *myfile = fopen(argv[1], "rb");
    if (myfile == 0)
    {
        fprintf(stderr, "%s: failed to open file '%s' for reading\n", 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(" %s\n", 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] = '\0';
    }

    while ((i % 16) != 0)
    {
        printf("  ");
        if (i % 2 == 1)
            putchar(' ');
        i++;
    }
    printf(" %s\n", bufferLine);

}

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

于 2019-02-03T08:33:30.223 回答