在C中读取ELF文件的正确方法

发布于 2024-11-18 00:56:41 字数 414 浏览 11 评论 0原文

第一次在这里发帖,谷歌搜索了几个小时,没有找到任何东西。

我正在尝试编写一个客户端程序来读取不同的文件并将它们通过网络发送到服务器程序。 (出于备份目的。)

到目前为止,我已经使用 fread() 将文件放入缓冲区,其方式类似于: http://www.cplusplus.com/reference/clibrary/cstdio/fread/

它对于纯文本文件工作得很好,但是当我尝试传输可执行文件时,fread() 似乎不读取文件,只将“ELF”读入缓冲区。

我的问题是,将 ELF 文件的内容读入 C 缓冲区的正确方法是什么?

谢谢。

first time posting here, Googled for a few hours and I couldn't find anything.

I am trying to write a client program that reads different files and sends them over the web to a server program. (For backup purposes.)

So far I have used fread() to put the files into a buffer, in a way that is similar to this:
http://www.cplusplus.com/reference/clibrary/cstdio/fread/

It works fine for plain-text files, but when I try to transfer executable files, fread() doesn't seem to read the files, only reading "ELF" into the buffer.

My question is, what is the proper way to read the contents of an ELF file into a buffer in C?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-11-25 00:56:41

处理 ELF 文件的最佳方法是使用 ELF 库(libelf;Google 知道它),假设您可以使用它。 ELF 文件具有复杂的结构,并且库通常会使处理文件变得更容易。

如果您必须使用fread(),那么只要您认识到数据充满空或零字节并且普通字符串函数不会因为零而准确地处理数据,它就可以正常工作在数据中。特别是,像 printf() 这样的函数会在第一个零字节处停止打印字符串。

,您只需打开文件(以二进制模式;它在 Unix 上没有危害,但在 Windows 上至关重要),然后将大块读入内存并再次将其写出:

/* Copy the rest of f1 to f2 */
void fcopy(FILE *f1, FILE *f2)
{
    char            buffer[BUFSIZ];
    size_t          n;

    while ((n = fread(buffer, sizeof(char), sizeof(buffer), f1)) > 0)
    {
        if (fwrite(buffer, sizeof(char), n, f2) != n)
            err_syserr("write failed\n");
    }
}

要通过网络备份任意文件 err_syserr() 函数是一个简单的错误报告函数,它报告其参数中给出的错误并添加系统错误消息 (strerror(errno)) 并退出。

The best way to process ELF files is with the ELF library (libelf; Google knows about it), assuming that it is available to you. An ELF file has an elaborate structure and the library will usually make it easier to handle the files.

If you must use fread(), then it will work fine as long as you recognize that the data is full of null or zero bytes and that ordinary string functions will not process the data accurately because of the zeroes in the data. In particular, functions like printf() stop printing a string at the first zero byte.

For backing up arbitrary files over a network, you simply need to open the file (in binary mode; it does no harm on Unix and is crucial on Windows), and read large blocks into memory and write them out again:

/* Copy the rest of f1 to f2 */
void fcopy(FILE *f1, FILE *f2)
{
    char            buffer[BUFSIZ];
    size_t          n;

    while ((n = fread(buffer, sizeof(char), sizeof(buffer), f1)) > 0)
    {
        if (fwrite(buffer, sizeof(char), n, f2) != n)
            err_syserr("write failed\n");
    }
}

The err_syserr() function is a simple error reporting function that reports the error given in its argument(s) and adds the system error message (strerror(errno)) and exits.

十年不长 2024-11-25 00:56:41

读取二进制文件的正确方法就是使用fread()。但请记住,您不能将内容视为字符串,这意味着(除其他外)您不能使用 strlen() 来确定它们的长度。

相反,您必须将长度保存在单独的变量中。 fread() 的返回值将告诉您成功读取了多少条记录 - 如果您将记录大小设置为 1(fread() 的第二个参数),那么它将返回读取的字节数。

The proper way to read binary files is simply to use fread(). But remember that you cannot treat the contents as a string, which means (among other things) you cannot use strlen() to determine their length.

Instead, you must keep the length in a seperate variable. The return value of fread() will tell you how many records it successfully read - if you set the record size to 1 (the second parameter to fread()) then it will return the number of bytes read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文