静态编译libmagic(c / c ++文件类型检测) [英] Compiling libmagic statically (c/c++ file type detection)

查看:930
本文介绍了静态编译libmagic(c / c ++文件类型检测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢那些帮助我使用上一个问题(链接仅供参考)。



我可以将文件 fileTypeTest.cpp libmagic.a magic ,我可以使用 g ++ -lmagic fileTypeTest.cpp fileTypeTest 。后来,我将测试看它是否运行在用MinGW编译的Windows中。



我计划使用 libmagic 在一个小的GUI应用程序,我想要编译它静态分发。我的问题是 libmagic 似乎需要外部文件, magic 。 (我实际上使用我自己的缩短和编译版本,magic_short.mgc,但我离题。)



一个hacky解决方案将代码文件到应用程序,根据需要创建(和删除)外部文件。



magic c $ c>是一个描述不同文件类型的属性的文本文件。当被要求识别文件时, libmagic 搜索 magic 。有一个编译版本, magic.mgc ,工作更快。我的应用程序只需要确定一些文件类型,然后决定如何处理它们,所以我将使用我自己的 magic_short 文件创建 magic_short .mgc

解决方案

这很棘手,我想你可以这样做顺便说一句,我已经下载了libmagic源码,并看着它...



有一个函数在里面调用 magic_read_entries minifile.c (这是我从

您可以附加 magic cat magic>> libmagic.a magic 为474443字节, libmagic.a 为38588字节。



magic.c 文件中,您需要更改 magichandle_t * magic_init(unsigned flags)函数,在函数结束时,添加 magic_read_entries 行,并修改函数本身以在库本身的偏移处读取,以拉入数据,将其视为指针到指向char的(char **)的指针,并使用它而不是从文件中读取。



现在函数 magic_read_entries 将不再使用,因为它不会再从文件中读取。函数`magichandle_t * magic_init(unsigned flags)'将负责加载条目,你应该确定。



如果你需要进一步的帮助,



编辑:
我使用了sourceforge.net的旧'libmagic',这里是我做的:


  1. 将下载的归档文件提取到我的主目录中,解压缩/解除归档将创建一个名为 libmagic 的文件夹。
  2. >
  3. 在libmagic中创建一个文件夹并调用

  4. 复制原始 magic.c strong> minifile.c 键入

  5. 使用所包含的diff输出高亮显示差异, em> source。



 
48a49,51
> #define MAGIC_DATA_OFFSET 0x971C
> #define MAGIC_STAT_LIB_NAMElibmagic.a
>
125a129,130​​
> / * magic_read_entries is obsolete ... * /
> magic_read_entries(mh,MAGIC_STAT_LIB_NAME);
251c256,262
<
---
>
> if(!fseek(fp,MAGIC_DATA_OFFSET,SEEK_SET)){
> if(ftell(fp)!= MAGIC_DATA_OFFSET)return 0;
> } else {
> return 0;
> }
>




  • 然后发出
  • 魔法文件(我从/ etc下复制,在Slackware Linux 12.2下)连接到libmagic.a文件,即 cat magic>> libmagic.a 。魔法的SHA校验和是(4abf536f2ada050ce945fbba796564342d6c9a61 magic),
    这里是魔术的确切数据
    (-rw-r - r-- 1 root root 474443 2007-06-03 00:52 / etc /
  • 这里是 minifile.c 源的diff,应用它并通过运行 源代码重建minifile可执行文件。




 
< magic_read_entries(mh,magic);
---
> / * magic_read_entries(mh,magic); * /

如果没有,您将需要通过修改MAGIC_DATA_OFFSET将偏移调整到库中进行读取。如果你愿意,我可以把魔术数据文件粘贴到pastebin。让我知道。



希望有帮助,
最好的问候,
汤姆。


Thanks to the guys that helped me with my previous question (linked just for reference).

I can place the files fileTypeTest.cpp, libmagic.a, and magic in a directory, and I can compile with g++ -lmagic fileTypeTest.cpp fileTypeTest. Later, I'll be testing to see if it runs in Windows compiled with MinGW.

I'm planning on using libmagic in a small GUI application, and I'd like to compile it statically for distribution. My problem is that libmagic seems to require the external file, magic. (I'm actually using my own shortened and compiled version, magic_short.mgc, but I digress.)

A hacky solution would be to code the file into the application, creating (and deleting) the external file as needed. How can I avoid this?

added for clarity:

magic is a text file that describes properties of different filetypes. When asked to identify a file, libmagic searches through magic. There is a compiled version, magic.mgc that works faster. My application only needs to identify a handful of filetypes before deciding what to do with them, so I'll be using my own magic_short file to create magic_short.mgc.

解决方案

This is tricky, I suppose you could do it this way... by the way, I have downloaded the libmagic source and looking at it...

There's a function in there called magic_read_entries within the minifile.c (this is the pure vanilla source that I downloaded from sourceforge where it is reading from the external file.

You could append the magic file (which is found in the /etc directory) to the end of the library code, like this cat magic >> libmagic.a. In my system, magic is 474443 bytes, libmagic.a is 38588 bytes.

In the magic.c file, you would need to change the magichandle_t* magic_init(unsigned flags) function, at the end of the function, add the line magic_read_entries and modify the function itself to read at the offset of the library itself to pull in the data, treat it as a pointer to pointer to char's (char **) and use that instead of reading from the file. Since you know where the offset is to the library data for reading, that should not be difficult.

Now the function magic_read_entries will no longer be used, as it is not going to be read from a file anymore. The function `magichandle_t* magic_init(unsigned flags)' will take care of loading the entries and you should be ok there.

If you need further help, let me know,

Edit: I have used the old 'libmagic' from sourceforge.net and here is what I did:

  1. Extracted the downloaded archive into my home directory, ungzipping/untarring the archive will create a folder called libmagic.
  2. Create a folder within libmagic and call it Test
  3. Copy the original magic.c and minifile.c into Test
  4. Using the enclosed diff output highlighting the difference, apply it onto the magic.c source.

48a49,51
> #define MAGIC_DATA_OFFSET     0x971C
> #define MAGIC_STAT_LIB_NAME "libmagic.a"
>
125a129,130
>       /* magic_read_entries is obsolete... */
>       magic_read_entries(mh, MAGIC_STAT_LIB_NAME);
251c256,262
<
---
>
>       if (!fseek(fp, MAGIC_DATA_OFFSET, SEEK_SET)){
>               if (ftell(fp) != MAGIC_DATA_OFFSET) return 0;
>       }else{
>               return 0;
>       }
>

  • Then issue make
  • The magic file (which I copied from /etc, under Slackware Linux 12.2) is concatenated to the libmagic.a file, i.e. cat magic >> libmagic.a. The SHA checksum for magic is (4abf536f2ada050ce945fbba796564342d6c9a61 magic), here's the exact data for magic (-rw-r--r-- 1 root root 474443 2007-06-03 00:52 /etc/file/magic) as found on my system.
  • Here's the diff for the minifile.c source, apply it and rebuild minifile executable by running make again.

40c40
<       magic_read_entries(mh,"magic");
---
>       /*magic_read_entries(mh,"magic");*/

It should work then. If not, you will need to adjust the offset into the library for reading by modifying the MAGIC_DATA_OFFSET. If you wish, I can stick up the magic data file into pastebin. Let me know.

Hope this helps, Best regards, Tom.

这篇关于静态编译libmagic(c / c ++文件类型检测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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