开发者

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). I then need to pass the downloaded file into 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 开发者_如何学Pythonif I don't have to. Can I download the file and then pass it directly into c-jpeg-steganography easily? Is there an easy way to modify the c-jpeg-steganography libraries to work? Is the writing to a temporary file not a bad way to do this?

I am very much a C noob... please go easy. 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.

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. In that case, you can arrange to download the file via libcurl into an open file (which may not have a name); you can then rewind that FILE * and pass it to the steganography library.

Having downloaded the library, the two main functions provided are:

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

/**
 * 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.)


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.

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜