如何嵌入一个文件到一个可执行文件? [英] How to embed a file into an executable file?

查看:129
本文介绍了如何嵌入一个文件到一个可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题,第一个已经解决。

I have two problems, the first has been solved.

当前问题结果
如果我嵌入需要的库加载一个文件,如JPEG图像或MP3音乐,我将需要使用的文件作为输入到库中。然而,每一个库是不同的,使用一种方式来获得一个文件作为输入,输入可以是文件名或文件*指针(从libc中的文件接口)。

Current problem
If I embed a file that requires a library to load it, such as a jpeg image or a mp3 music, I will need to use the file as input to the library. However, each library is different and uses a way to get a file as input, the input may be the file name or a FILE* pointer (from libc's file interface).

我想知道如何与名访问嵌入的文件。这将是低效的,如果我创建一个临时文件,有另一种方式?我可以映射文件名到内存?我的平台是Windows和Linux。

I would like to know how to access an embedded file with a name. It will be inefficient if I create a temporary file, is there another way? Can I map a file name to memory? My platforms are Windows and Linux.

如果 show_file(为const char *名称)是从库中的函数,我将需要一个字符串来打开该文件。结果
我已经看到了这些问题:结果
如何获得缓冲的文件描述符内存?结果
获取文件名从文件描述符使用C

If show_file(const char* name) is a function from a library, I will need a string to open the file.
I have seen these questions:
How to get file descriptor of buffer in memory?
Getting Filename from file descriptor in C

和以下code是我的解决方案。这是一个好的解决方案吗?它是低效的?

and the following code is my solution. Is it a good solution? Is it inefficient?

# include <stdio.h>
# include <unistd.h>

extern char _binary_data_txt_start;
extern const void* _binary_data_txt_size;
const size_t len = (size_t)&_binary_data_txt_size;

void show_file(const char* name){
    FILE* file = fopen(name, "r");
    if (file == NULL){
        printf("Error (show_file): %s\n", name);
        return;
    }

    while (true){
        char ch = fgetc(file);
        if (feof(file) )
            break;
        putchar( ch );
    }
    printf("\n");

    fclose(file);
}

int main(){

    int fpipe[2];
    pipe(fpipe);

    if( !fork() ){
        for( int buffsize = len, done = 0; buffsize>done; ){
            done += write( fpipe[1], &_binary_data_txt_start + done, buffsize-done );
        }
        _exit(0);
    }

    close(fpipe[1]);
    char name[200];
    sprintf(name, "/proc/self/fd/%d", fpipe[0] );

    show_file(name);

    close(fpipe[0]);
}


的另一个问题(解决)结果
我试图嵌入Linux上的文件,与海湾合作委员会,和它的工作。不过,我试图做同样的事情在Windows上,使用MinGW,它并没有编译。


The other problem (solved)
I tried to embed a file on Linux, with GCC, and it worked. However, I tried to do the same thing on Windows, with Mingw, and it did not compile.

在code是:

# include <stdio.h>

extern char _binary_data_txt_start;
extern char _binary_data_txt_end;

int main(){
    for (char* my_file = &_binary_data_txt_start; my_file <= &_binary_data_txt_end; my_file++)
        putchar(*my_file);
    printf("\n");
}

编译命令是:

objcopy --input-target binary --output-target elf32-i386 --binary-architecture i386 data.txt data.o
g++ main.cpp data.o -o test.exe

在Windows上,我得到以下编译器错误:

On Windows, I get the following compiler error:

undefined reference to `_binary_data_txt_start'
undefined reference to `_binary_data_txt_end'

我试图取代 ELF32-I386 I386-PC-的mingw32 ,但我仍然得到同样的错误。

I tried to replace elf32-i386 with i386-pc-mingw32, but I still get the same error.

推荐答案

我认为,这与MinGW的工作,你将需要删除的领先从.c文件的名称下划线。请参见使用gcc MinGW的了解一些细节嵌入二进制斑点。

I think that for this to work with MinGW you'll need to remove the leading underscore from the names in the .c file. See Embedding binary blobs using gcc mingw for some details.

看看使用以下帮助:

extern char binary_data_txt_start;
extern char binary_data_txt_end;

如果你需要同出一源,为Linux或MinGW的构建工作,你可能需要使用preprocessor有在不同的环境中使用正确的名称。

If you need the same source to work for Linux or MinGW builds, you might need to use the preprocessor to have the right name used in the different environments.

这篇关于如何嵌入一个文件到一个可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆