1

I try to insert some EXIF Data in a JPEG stream. I am using libexif for that. The libexif example shows how to open a file -> add the data -> and then save the file. What I have is a pointer (void * JPEGPointer) to a JPEG stream. So I want to just add the from libexif generated data to this pointer for sending it via FTP. Not saving to a file. The following code is basically the example from libexif with some not working memcpy() attempts from myself. The JPG File is corrupted after the memcpy so i can not open it with any Software. I think i am copying the exif to the wrong position. I can not use any aditional library unfortunately.

// Set the image options
exif_data_set_option(exif, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
exif_data_set_data_type(exif, EXIF_DATA_TYPE_COMPRESSED);
exif_data_set_byte_order(exif, FILE_BYTE_ORDER);

// Create the mandatory EXIF fields with default data
exif_data_fix(exif);

/* Create a EXIF_TAG_USER_COMMENT tag. This one must be handled
 * differently because that tag isn't automatically created and
 * allocated by exif_data_fix(), nor can it be created using
 * exif_entry_initialize() so it must be explicitly allocated here.
 */
entry = createTag(exif, EXIF_IFD_EXIF, EXIF_TAG_USER_COMMENT,
                  sizeof(ASCII_COMMENT) + sizeof(FILE_COMMENT) - 2);
/* Write the special header needed for a comment tag */
memcpy(entry->data, ASCII_COMMENT, sizeof(ASCII_COMMENT)-1);
/* Write the actual comment text, without the trailing NUL character */
memcpy(entry->data+8, FILE_COMMENT, sizeof(FILE_COMMENT)-1);
/* createTag() happens to set the format and components correctly for
 * EXIF_TAG_USER_COMMENT, so there is nothing more to do. */
/* Get a pointer to the EXIF data block we just created */
exif_data_save_data(exif, &exif_data, &exif_data_len);
assert(exif_data != NULL);

//copy exif data to my JPEGPointer...not working like that
memcpy(JPEGPointer+ exif_data_len * sizeof(uint8_t), JPEGPointer, sizeof(JPEGPointer)+exif_data_len);

free(exif_data);
exif_data_unref(exif);

Any ideas how to put that EXIF data to the correct position in the JPEG data?

Miatorret
  • 11
  • 2
  • 1
    Can you please elaborate on the problem you have with the code you show? Do you get build errors? Crashes ar runtime? Unexpected results? Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). And if possible try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. – Some programmer dude Aug 29 '17 at 08:00
  • Okay, i edited it. The JPG is corrupted after memcpy, so i think i copy the exif data to the wrong position. – Miatorret Aug 29 '17 at 08:08
  • yeah, your `memcpy` doesn't look right, what you're doing is copying the the first `exif_data_len + 4?` elements onto the next `exif_data_len` elements of `JPEGPointer` thereby overwriting data within. You might consider not combining them at all and write them through ftp independently. – kmdreko Aug 29 '17 at 08:24

0 Answers0