1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! JPEG metadata writer.
//!
//! Rewrites JPEG files with updated/added/removed metadata segments.
//! Mirrors ExifTool's WriteJPEG in Writer.pl.
use crate::error::{Error, Result};
/// JPEG segment to write.
#[derive(Debug, Clone)]
pub struct JpegSegment {
pub marker: u8,
pub data: Vec<u8>,
}
/// Rewrite a JPEG file, replacing/adding/removing metadata segments.
///
/// `source` is the original JPEG data.
/// `new_exif` is the new EXIF data (including "Exif\0\0" header), or None to keep existing.
/// `new_xmp` is the new XMP data (including namespace header), or None to keep existing.
/// `new_iptc` is the new IPTC/Photoshop data, or None to keep existing.
/// `new_comment` is the new JPEG comment, or None to keep existing.
/// `remove_exif` / `remove_xmp` / `remove_iptc` / `remove_comment`: if true, remove that segment.
#[allow(clippy::too_many_arguments)]
pub fn write_jpeg(
source: &[u8],
new_exif: Option<&[u8]>,
new_xmp: Option<&[u8]>,
new_iptc: Option<&[u8]>,
new_comment: Option<&str>,
remove_exif: bool,
remove_xmp: bool,
remove_iptc: bool,
remove_comment: bool,
) -> Result<Vec<u8>> {
if source.len() < 2 || source[0] != 0xFF || source[1] != 0xD8 {
return Err(Error::InvalidData("not a JPEG file".into()));
}
let mut output = Vec::with_capacity(source.len());
output.extend_from_slice(&[0xFF, 0xD8]); // SOI
let mut pos = 2;
let mut wrote_exif = false;
let mut wrote_xmp = false;
let mut wrote_iptc = false;
let mut wrote_comment = false;
let mut past_first_segment = false;
// Write new segments before the first existing segment
// (ExifTool writes EXIF first, then XMP, then IPTC)
while pos + 4 <= source.len() {
// Find next marker
if source[pos] != 0xFF {
pos += 1;
continue;
}
let marker = source[pos + 1];
pos += 2;
// Skip padding 0xFF bytes
if marker == 0xFF || marker == 0x00 {
continue;
}
// SOS: end of metadata - write any remaining new segments then copy rest
if marker == 0xDA {
// Before SOS, inject any new segments we haven't written yet
if !wrote_exif {
if let Some(exif) = new_exif {
write_app1_exif(&mut output, exif);
}
}
if !wrote_xmp {
if let Some(xmp) = new_xmp {
write_app1_xmp(&mut output, xmp);
}
}
if !wrote_iptc {
if let Some(iptc) = new_iptc {
write_app13_iptc(&mut output, iptc);
}
}
if !wrote_comment {
if let Some(comment) = new_comment {
write_com(&mut output, comment);
}
}
// Copy SOS marker and all remaining data (image data + EOI)
output.extend_from_slice(&[0xFF, marker]);
output.extend_from_slice(&source[pos..]);
return Ok(output);
}
// Markers without payload (restart markers, etc.)
if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) {
output.extend_from_slice(&[0xFF, marker]);
continue;
}
// Read segment length
if pos + 2 > source.len() {
break;
}
let seg_len = u16::from_be_bytes([source[pos], source[pos + 1]]) as usize;
if seg_len < 2 || pos + seg_len > source.len() {
break;
}
let seg_data = &source[pos + 2..pos + seg_len];
// On first non-SOI segment, inject new segments if needed
if !past_first_segment {
past_first_segment = true;
// Write new EXIF before existing segments (unless we'll replace an existing one)
if new_exif.is_some() && !is_exif_segment(marker, seg_data) {
if let Some(exif) = new_exif {
write_app1_exif(&mut output, exif);
wrote_exif = true;
}
}
}
// Decide what to do with this segment
match marker {
// APP1 - EXIF or XMP
0xE1 => {
if is_exif_segment(marker, seg_data) {
if remove_exif {
// Skip (remove)
} else if let Some(exif) = new_exif {
if !wrote_exif {
write_app1_exif(&mut output, exif);
wrote_exif = true;
}
// Skip original EXIF segment (replaced)
} else {
// Keep original
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
} else if is_xmp_segment(seg_data) {
if remove_xmp {
// Skip
} else if let Some(xmp) = new_xmp {
if !wrote_xmp {
write_app1_xmp(&mut output, xmp);
wrote_xmp = true;
}
} else {
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
} else {
// Other APP1 segment - keep
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
}
// APP13 - Photoshop/IPTC
0xED => {
if remove_iptc {
// Skip
} else if let Some(iptc) = new_iptc {
if !wrote_iptc {
write_app13_iptc(&mut output, iptc);
wrote_iptc = true;
}
} else {
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
}
// COM - Comment
0xFE => {
if remove_comment {
// Skip
} else if let Some(comment) = new_comment {
if !wrote_comment {
write_com(&mut output, comment);
wrote_comment = true;
}
} else {
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
}
// All other segments - copy as-is
_ => {
write_segment(&mut output, marker, &source[pos..pos + seg_len]);
}
}
pos += seg_len;
}
// If we never hit SOS (unusual but possible for metadata-only JPEG)
Ok(output)
}
fn is_exif_segment(_marker: u8, data: &[u8]) -> bool {
data.len() > 6 && data.starts_with(b"Exif\0\0")
}
fn is_xmp_segment(data: &[u8]) -> bool {
data.len() > 29 && data.starts_with(b"http://ns.adobe.com/xap/1.0/\0")
}
fn write_segment(output: &mut Vec<u8>, marker: u8, data: &[u8]) {
output.push(0xFF);
output.push(marker);
output.extend_from_slice(data);
}
fn write_app1_exif(output: &mut Vec<u8>, exif_data: &[u8]) {
// APP1 marker + length + "Exif\0\0" + TIFF data
let mut segment = Vec::new();
segment.extend_from_slice(b"Exif\0\0");
segment.extend_from_slice(exif_data);
let total_len = (segment.len() + 2) as u16; // +2 for length field itself
output.push(0xFF);
output.push(0xE1);
output.extend_from_slice(&total_len.to_be_bytes());
output.extend_from_slice(&segment);
}
fn write_app1_xmp(output: &mut Vec<u8>, xmp_data: &[u8]) {
let mut segment = Vec::new();
segment.extend_from_slice(b"http://ns.adobe.com/xap/1.0/\0");
segment.extend_from_slice(xmp_data);
// XMP can be large; split if needed (>65533 bytes)
if segment.len() + 2 > 65535 {
// Write only what fits; extended XMP not yet supported
let max = 65533;
let total_len = (max + 2) as u16;
output.push(0xFF);
output.push(0xE1);
output.extend_from_slice(&total_len.to_be_bytes());
output.extend_from_slice(&segment[..max]);
} else {
let total_len = (segment.len() + 2) as u16;
output.push(0xFF);
output.push(0xE1);
output.extend_from_slice(&total_len.to_be_bytes());
output.extend_from_slice(&segment);
}
}
fn write_app13_iptc(output: &mut Vec<u8>, iptc_data: &[u8]) {
let mut segment = Vec::new();
segment.extend_from_slice(b"Photoshop 3.0\0");
// Wrap in 8BIM resource block
segment.extend_from_slice(b"8BIM");
segment.extend_from_slice(&0x0404u16.to_be_bytes()); // IPTC resource ID
segment.push(0); // Pascal string name (length 0)
segment.push(0); // Padding
segment.extend_from_slice(&(iptc_data.len() as u32).to_be_bytes());
segment.extend_from_slice(iptc_data);
if iptc_data.len() % 2 != 0 {
segment.push(0); // Pad to even
}
let total_len = (segment.len() + 2) as u16;
output.push(0xFF);
output.push(0xED);
output.extend_from_slice(&total_len.to_be_bytes());
output.extend_from_slice(&segment);
}
fn write_com(output: &mut Vec<u8>, comment: &str) {
let data = comment.as_bytes();
let total_len = (data.len() + 2) as u16;
output.push(0xFF);
output.push(0xFE);
output.extend_from_slice(&total_len.to_be_bytes());
output.extend_from_slice(data);
}