I'm developing a linux app programmed in C which processes gdk pixbuf images and then should send them to a remote server via ftp (libcurl). The images are saved into a gchar buffer with gdk_pixbuf_save_to_buffer.
The problem is I don't know how to use the data in this buffer in conjunction with libcurl read callback function to send the image properly to the remote server. All my attempts so far have produced random bytes into the resulting file.
There is an example of the libcurl read callback function which looks like this:
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t retcode = fread(ptr, size, nmemb, stream);
return retcode;
}
I would need to do my own callback function that gets the gchar buffer as input instead of file stream and reads size*nmemb bytes from it and stores it to *ptr.
This is related to my previous question.
The read_callback()
is a function which CURL calls when it need to obtain data that will be uploaded to the server. Imagine that read_callback()
is something similar to fread()
. When called it performs any voodoo-mumbo-jumbo that is needed, but in the end uploadable data must be stored in *ptr
buffer, which is curl's internal buffer. For your in-memory buffer memcpy()
will do just fine as body of read_callback()
, so you don't need real fread()
at all.
size * nmemb
tells you how big buffer curl has reserved for a single chunk of data. The last void*
is a pointer which was set by CURLOPT_READDATA option - it's a do-what-ever-you-need-with-it kind of pointer, so it can point to a structure containing data which you're uploading and a some additional info e.g. current progress.
You may use this as a sample:
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
struct transfer
{
gchar *buf;
gsize total;
size_t uploaded;
};
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
struct transfer * tr = data;
size_t left = tr->total - tr->uploaded;
size_t max_chunk = size * nmemb;
size_t retcode = left < max_chunk ? left : max_chunk;
memcpy(ptr, tr->buf + tr->uploaded, retcode); // <-- voodoo-mumbo-jumbo :-)
tr->uploaded += retcode; // <-- save progress
return retcode;
}
int main()
{
GdkPixbuf * gbuffer = NULL;
GError * error = NULL;
gchar * buffer;
gsize size;
g_type_init();
gbuffer = gdk_pixbuf_new_from_file("g.png", &error);
gdk_pixbuf_save_to_buffer(gbuffer, &buffer, &size, "jpeg", &error, NULL);
struct transfer tr = {buffer, size, 0};
CURL *easyhandle = curl_easy_init();
curl_easy_setopt(easyhandle, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(easyhandle, CURLOPT_READDATA, &tr); // <-- this will be *data in read_callback()
curl_easy_setopt(easyhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(easyhandle, CURLOPT_URL, "http://example.com/upload.php");
CURLcode rc = curl_easy_perform(easyhandle);
}
来源:https://stackoverflow.com/questions/9453996/sending-an-image-to-ftp-server-from-gchar-buffer-libcurl