You can embed any data into an EXIF data block in a JPG. There is no size limit that I am aware of, you just split it into mutliple EXIF data blocks if required.
You don t even need libexif. You can do it all manually in C code by programmatically doing the following (note that this only works with JPG files that already have other EXIF data in it):
Basically your JPG file (if it already has an EXIF header in it) will start with something like:
0xFF 0xD8
Then the very first Exif header will start with this:
0xFF 0xE0
The next comes the length of the Exif data block which is two bytes here:
0x00 0x10 //In this case it is 16 bytes (0x0010) long and it INCLUDES these two bytes of the header
Next comes the Exif data block s actual data (yours may be different). Note that it is 14 bytes long (or 14+2 = 16bytes, or 0x0010 as shown above):
0x4A 0x46 0x49 0x46 0x00 0x01 0x01 0x01 0x00 0x60 0x00 0x60 0x00 0x00
Now you can insert your XML Exif data block after this header starting with an exif data identifier (always the same):
0xFF 0xE1
Then the size of your XML file in bytes + 2 (note that if your file is larger than 0xFFFE in size then you must split it up into multiple EXIF data blocks):
0x07 0x7D //In this case it is 1917bytes long or 0x077D
Then insert your xml directly into the JPG at this point and leave the rest of the file as is.
See the picture below for a visual explanation (just right click and view image if you can t read it):

Here is my JPG with my dummy XML file in it. Right click, save, open it in a Hex editor and look for yourself:
Sample JPG with XML in EXIF data block
At the end just check you can still open the JPG and if you can you ve succeeded.
Here is a quick example in C++ (note I haven t debugged the code, just wrote it from memory so be warned!):
char yourdata[]="<xml> contents to </add>";
long yourdatalen = 0x18;
//open file
char * file;
long filelen=0;
std::ifstream infile;
infile.open("yourjpg.jpg",std::ios::binary| std::ios::in);
//find size of file
infile.seekg (0, ios::end);
filelen = infile.tellg();
infile.seekg (0, ios::beg);
//read contents of file
file = new char [filelen];
infile.read(file,filelen);
infile.close();
//lets parse through the file and find any exif headers
long x=0;
if ((file[0]==0xFF) && (file[1]==0xD8)){
//all good lets go!!
while ((file[x]!=0xFF) && (file[x+1]!=0xE1)) {
x++;
}
//were at the first EXIF data block! insert XML here
char * temp=file;
file = new char [filelen+yourdatalen+4];
memcpy(file,temp,x);
file[x+0]=0xFF;
file[x+1]=0xE1;
file[x+2]=int((yourdatalen+2)/0xFF); //note assumes that your xml file is less than 0xFFFE bytes long
file[x+3]=yourdatalen-int((yourdatalen+2)/0xFF);
memcpy(&file[x+4],yourdata,yourdatalen);
memcpy(&file[x+4+yourdatalen],temp[x],filelen-x);
delete [] temp;
//Save to file
std::ofstream ofile;
ofile.open("savejpg.jpg",std::ios::binary| std::ios::out);
ofile.write(file,yourdatalen+4+filelen);
ofile.close();
}
else {
//JPG file does not have exif data in it, you ll need to add it first or find another way of adding your data
}
//Clean up
delete [] file;
I used HxD but you can use any hex editor.