简体   繁体   English

如何获取网络缓冲区并将其传递给期望在C中使用FILE的函数

[英]How can I take a network buffer and pass it into a function that is expecting a FILE in C

I am looking to be able to download a file over a network socket from an HTTP server (A jpeg). 我希望能够通过网络套接字从HTTP服务器(A jpeg)下载文件。 I then need to pass the downloaded file into c-jpeg-steganography http://code.google.com/p/c-jpeg-steganography/ . 然后,我需要将下载的文件传递到c-jpeg-steganography http://code.google.com/p/c-jpeg-steganography/中 I can use libcurl to download the file and then pass the filename into the library, but I would prefer to not do that if I don't have to. 我可以使用libcurl下载文件,然后将文件名传递到库中,但是如果不需要的话,我不想这样做。 Can I download the file and then pass it directly into c-jpeg-steganography easily? 我可以下载文件,然后轻松地将其直接传递到c-jpeg-steganography吗? Is there an easy way to modify the c-jpeg-steganography libraries to work? 有没有简单的方法可以修改c-jpeg-steganography库以使其正常工作? Is the writing to a temporary file not a bad way to do this? 写入临时文件不是执行此操作的好方法吗?

I am very much a C noob... please go easy. 我非常C菜鸟...请放轻松。 Most of my experience is with languages that let me cheat and do all the work for me. 我大部分的经验是使用让我作弊并为我完成所有工作的语言。

The steganography site doesn't give the API details AFAICS, which is a nuisance. 隐秘术站点没有提供API详细信息AFAICS,这很麻烦。

However, judging from your question, it appears that there is a steganographic function that takes an open FILE * (you never get to deal with FILE ) which contains the unmodified image, and presumably the information you are trying to conceal in the image. 但是,从您的问题来看,似乎存在一个隐写函数,该函数带有一个打开的FILE * (您永远无法处理FILE ),其中包含未修改的图像,并且可能是您试图隐藏在图像中的信息。 In that case, you can arrange to download the file via libcurl into an open file (which may not have a name); 在这种情况下,您可以安排通过libcurl将文件下载到一个打开的文件(可能没有名称)中。 you can then rewind that FILE * and pass it to the steganography library. 然后,您可以倒带该FILE *并将其传递给隐写术库。

Having downloaded the library, the two main functions provided are: 下载该库后,提供的两个主要功能是:

steganolab_encode steganolab_encode

/**
 * Modifies specified jpeg file so that it contains given message.
 * @param file - jpeg file name
 * @param data - message to embed
 * @param len - message length
 * @param password - key string to cipher data and set up PRNG
 * @param DCT_radius - which DCT coefficients shall be used to carry
 *      information, i^2+j^2 <= R^2
 * @param stats - pointer to structure to put statistics to. The caller
 * must take care about freeing the structure properly. NULL pass
 * disables statistics output.  The object don't need to be freed if the
 * function fails, and it contains no message.
 * @return              0: All OK
 *                              not 0: Failed
 **/
int steganolab_encode(const char * file, const char * data,
        unsigned int len, const char * password, uint8_t DCT_radius,
        struct steganolab_statistics * stats);

steganolab_decode steganolab_decode

/**
 * Reads steganographic message from specified file
 * @param file - jpeg file name
 * @param data - pointer to pointer to string to put data to
 * @param len - pointer to push obtained buffer length to
 * @param password - secred string for cipher and PRNG
 * @param DCT_radius - which DCT coefficients shall be used to carry
 *      information, i^2+j^2 <= R^2
 * @param stats - statistics object. Free is up to the caller.
 * NULL disables the feature.  The object don't need to be freed if the
 * function fails, and it contains no message.
 * @return      0: All OK
 *                      not 0: fail (no buffers need freeing in this case)
 */
int steganolab_decode(const char * file, char ** data,
        unsigned int * len, const char * password, uint8_t DCT_radius,
        struct steganolab_statistics * stats);

Since the functions expect file names, you will have to provide file names. 由于函数需要文件名,因此您必须提供文件名。 Or you will have to rewrite the code to take whatever it is you have in mind - a buffer or an open file stream. 否则您将不得不重写代码以记住您想要的一切-缓冲区或打开的文件流。

(Aside: the code insists on using the archaic and non-standard header <malloc.h> to obtain a definition of size_t . It compiles to object if you replace each reference to <malloc.h> (or "malloc.h" ) with <stdlib.h> ; it might in theory compile with <stddef.h> if the only needed declaration is size_t .) (此外:代码坚持使用古老的和非标准头文件<malloc.h>来获取size_t的定义。如果将每个引用替换为<malloc.h> (或"malloc.h" ),它将编译为对象。)使用<stdlib.h> ;如果唯一需要的声明是size_t ,则理论上可以使用<stddef.h>编译。)

There's no standard way to pretend that a memory buffer is a file so you really are limited to one of the two solutions you posit: 没有标准的方法可以假装内存缓冲区是文件,因此您实际上仅限于所提出的两种解决方案之一:

  • write the information to a file then pass the file name to the library; 将信息写入文件,然后将文件名传递给库; or 要么
  • re-write the library to take an in-memory buffer. 重新编写该库以获取内存中的缓冲区。

I know the way I'd initially do it, that's the first option. 我知道最初的方式,这是第一个选择。 That's the easiest way (as in quickest-to-market). 这是最简单的方法(就像最快上市一样)。

From examination of the source, the work involved in changing it to process memory rather than a file is non-trivial. 从源代码的检查来看,将其更改为进程内存而不是文件所涉及的工作是不平凡的。

The encode , decode and estimate functions all call a single worker uber-function which receives the file name, opens it and, in turn, passes the file handle around quite a bit to other functions. encodedecodeestimate函数都调用一个worker uber函数,该函数接收文件名,将其打开,然后将文件句柄传递给其他函数。

It's doable but the effort is substantially larger than just using a temporary file (although keep in mind it would only have to be done once, and the resultant code would then be available for all the world to use). 它是可行的,但是工作量远远大于仅使用临时文件(尽管请记住,它只需要做一次,然后生成的代码将可供全世界使用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能将指向固定大小数组的指针的地址传递给期望指向C中指针的函数? - Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C? 如何将 C 字符串传递给需要 Zig 字符串的 Zig 函数? - How to pass a C string into a Zig function expecting a Zig string? C-如何gzip将文件的大块解压缩到内存缓冲区中 - C - How can I gzip decompress chunks of a file into a memory buffer 如何在C中将XML文件读入缓冲区? - How can I read an XML file into a buffer in C? 如何通过缓冲区传递整数? - How can I pass integer through the buffer? 如何使用swig调用C函数,期望从Java获得结构数组? - How can I call a C function expecting an array of structs from Java using swig? 如何传递可以从本机写入C#的空字符串缓冲区? - How can I pass an empty string buffer that can be written to from native to c#? 如何使用 ctypes.create_string_buffer 将其作为 char * 参数传递给 C function? - How do I use ctypes.create_string_buffer to pass it as a char * argument for a C function? 为什么我在尝试将 &#39;char (*c)[6]&#39; 传递给需要 &#39;const char (*c)[6]&#39; 的函数时收到警告? - Why do I get a warning trying to pass a 'char (*c)[6]' to a function expecting a 'const char (*c)[6]'? 我将如何使用 c 语言将文件名的路径传递给 void *buffer? - how will i pass the path of a file name to void *buffer using c language?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM