exiftool-rs 0.5.0

Read, write, and edit metadata in 93 file formats — a pure Rust reimplementation of ExifTool 13.53 with 100% tag name parity (194/194 test files)
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! MIE (Meta Information Encapsulation) file format reader.
//!
//! MIE is a format designed by Phil Harvey for flexible metadata storage.
//! It uses nested groups with typed data elements.
//! Mirrors ExifTool's MIE.pm.

use crate::error::{Error, Result};
use crate::tag::{Tag, TagGroup, TagId};
use crate::value::Value;

pub fn read_mie(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 4 || data[0] != b'~' {
        return Err(Error::InvalidData("not a MIE file".into()));
    }

    let mut tags = Vec::new();
    let mut pos = 0;
    // The top-level is the file itself, containing a "0MIE" directory
    parse_mie_group(data, &mut pos, "MIE-Top", &mut tags)?;
    Ok(tags)
}

fn parse_mie_group(
    data: &[u8],
    pos: &mut usize,
    group_name: &str,
    tags: &mut Vec<Tag>,
) -> Result<()> {
    while *pos + 4 <= data.len() {
        let sync = data[*pos];
        if sync != b'~' {
            break;
        }
        let format = data[*pos + 1];
        let tag_len = data[*pos + 2] as usize;
        let mut val_len = data[*pos + 3] as usize;
        *pos += 4;

        // Read tag name
        if tag_len > 0 && *pos + tag_len > data.len() {
            break;
        }
        let tag_name = if tag_len > 0 {
            let name =
                crate::encoding::decode_utf8_or_latin1(&data[*pos..*pos + tag_len]).to_string();
            *pos += tag_len;
            name
        } else {
            String::new()
        };

        // Multi-byte value length
        if val_len > 252 {
            let n = 1usize << (256 - val_len);
            if *pos + n > data.len() {
                break;
            }
            val_len = match n {
                1 => data[*pos] as usize,
                2 => u16::from_be_bytes([data[*pos], data[*pos + 1]]) as usize,
                4 => {
                    u32::from_be_bytes([data[*pos], data[*pos + 1], data[*pos + 2], data[*pos + 3]])
                        as usize
                }
                _ => break,
            };
            *pos += n;
        }

        // Group terminator (empty tag name)
        if tag_name.is_empty() {
            *pos += val_len; // skip terminator data
            break;
        }

        if *pos + val_len > data.len() {
            break;
        }

        let format_type = format & 0xfb; // strip compression bit
        let _is_compressed = format & 0x04 != 0;

        // MIE directory (format 0x10 or 0x18)
        if format_type == 0x10 || format_type == 0x18 {
            // Subdirectory - determine child group name
            let child_group = resolve_subdir_group(group_name, &tag_name);

            if val_len > 0 {
                // Embedded data subdirectory (EXIF, XMP, IPTC, ICC, etc.)
                let value_data = &data[*pos..*pos + val_len];
                match tag_name.as_str() {
                    "EXIF" => {
                        if let Ok(exif_tags) = crate::metadata::ExifReader::read(value_data) {
                            tags.extend(exif_tags);
                        }
                    }
                    "XMP" => {
                        if let Ok(xmp_tags) = crate::metadata::XmpReader::read(value_data) {
                            tags.extend(xmp_tags);
                        }
                    }
                    "IPTC" => {
                        if let Ok(iptc_tags) = crate::metadata::IptcReader::read(value_data) {
                            tags.extend(iptc_tags);
                        }
                    }
                    "ICCProfile" => {
                        if let Ok(icc_tags) = crate::formats::icc::read_icc(value_data) {
                            tags.extend(icc_tags);
                        }
                    }
                    _ => {
                        let mut sub_pos = 0;
                        let _ = parse_mie_group(value_data, &mut sub_pos, &child_group, tags);
                    }
                }
                *pos += val_len;
            } else {
                // Inline subdirectory — elements follow in-stream until group terminator
                let _ = parse_mie_group(data, pos, &child_group, tags);
            }
        } else if format_type == 0x80 {
            // Free space — skip
            *pos += val_len;
        } else {
            // Data element
            let value_data = &data[*pos..*pos + val_len];
            *pos += val_len;

            // Strip units from tag name: "Tag(unit)" -> "Tag"
            let clean_tag = if let Some(paren) = tag_name.find('(') {
                &tag_name[..paren]
            } else {
                &tag_name
            };

            // Resolve tag name based on group and MIE tag name
            let (resolved_name, family2) = resolve_tag_name(group_name, clean_tag);

            // Parse value based on format
            let value = parse_mie_value(format_type, value_data);

            tags.push(Tag {
                id: TagId::Text(resolved_name.clone()),
                name: resolved_name.clone(),
                description: resolved_name.clone(),
                group: TagGroup {
                    family0: "MIE".into(),
                    family1: group_name.into(),
                    family2,
                },
                print_value: value.to_display_string(),
                raw_value: value,
                priority: 0,
            });
        }
    }
    Ok(())
}

fn parse_mie_value(format_type: u8, data: &[u8]) -> Value {
    match format_type {
        0x20 | 0x28 => {
            // ASCII string or UTF-8
            Value::String(
                crate::encoding::decode_utf8_or_latin1(data)
                    .trim_end_matches('\0')
                    .to_string(),
            )
        }
        0x30 | 0x38 => {
            // String list (null-separated)
            let s = crate::encoding::decode_utf8_or_latin1(data)
                .trim_end_matches('\0')
                .to_string();
            Value::String(s.replace('\0', ", "))
        }
        0x40 => {
            // int8u
            if data.len() == 1 {
                Value::U8(data[0])
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x41 => {
            // int16u
            if data.len() >= 2 {
                let mut vals = Vec::new();
                let mut i = 0;
                while i + 2 <= data.len() {
                    vals.push(u16::from_be_bytes([data[i], data[i + 1]]));
                    i += 2;
                }
                if vals.len() == 1 {
                    Value::U16(vals[0])
                } else {
                    Value::String(
                        vals.iter()
                            .map(|v| v.to_string())
                            .collect::<Vec<_>>()
                            .join(" "),
                    )
                }
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x42 => {
            // int32u
            if data.len() >= 4 {
                Value::U32(u32::from_be_bytes([data[0], data[1], data[2], data[3]]))
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x48 => {
            // int8s
            if data.len() == 1 {
                Value::I16(data[0] as i8 as i16)
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x49 => {
            // int16s
            if data.len() >= 2 {
                Value::I16(i16::from_be_bytes([data[0], data[1]]))
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x52 | 0x53 => {
            // rational32u or rational64u
            if format_type == 0x53 && data.len() >= 8 {
                let num = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
                let den = u32::from_be_bytes([data[4], data[5], data[6], data[7]]);
                Value::URational(num, den)
            } else if format_type == 0x52 && data.len() >= 4 {
                let num = u16::from_be_bytes([data[0], data[1]]) as u32;
                let den = u16::from_be_bytes([data[2], data[3]]) as u32;
                Value::URational(num, den)
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x5a | 0x5b => {
            // rational32s or rational64s
            if format_type == 0x5b && data.len() >= 8 {
                let num = i32::from_be_bytes([data[0], data[1], data[2], data[3]]);
                let den = i32::from_be_bytes([data[4], data[5], data[6], data[7]]);
                Value::IRational(num, den)
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x72 => {
            // float
            if data.len() >= 4 {
                let f = f32::from_be_bytes([data[0], data[1], data[2], data[3]]);
                Value::String(format!("{}", f))
            } else {
                Value::Binary(data.to_vec())
            }
        }
        0x73 => {
            // double
            if data.len() >= 8 {
                let f = f64::from_be_bytes([
                    data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
                ]);
                Value::String(format!("{}", f))
            } else {
                Value::Binary(data.to_vec())
            }
        }
        _ => {
            // undef / binary
            if data.is_empty() {
                Value::String(String::new())
            } else {
                Value::Binary(data.to_vec())
            }
        }
    }
}

fn resolve_subdir_group(parent: &str, tag_name: &str) -> String {
    match (parent, tag_name) {
        ("MIE-Top", "0MIE") => "MIE-Main".into(),
        ("MIE-Main", "Meta") => "MIE-Meta".into(),
        ("MIE-Meta", "Audio") => "MIE-Audio".into(),
        ("MIE-Meta", "Camera") => "MIE-Camera".into(),
        ("MIE-Meta", "Document") => "MIE-Doc".into(),
        ("MIE-Meta", "Geo") => "MIE-Geo".into(),
        ("MIE-Meta", "Image") => "MIE-Image".into(),
        ("MIE-Meta", "MakerNotes") => "MIE-MakerNotes".into(),
        ("MIE-Meta", "Preview") => "MIE-Preview".into(),
        ("MIE-Meta", "Thumbnail") => "MIE-Thumbnail".into(),
        ("MIE-Meta", "Video") => "MIE-Video".into(),
        ("MIE-Camera", "Flash") => "MIE-Flash".into(),
        ("MIE-Camera", "Lens") => "MIE-Lens".into(),
        ("MIE-Camera", "Orientation") => "MIE-Orient".into(),
        ("MIE-Geo", "GPS") => "MIE-GPS".into(),
        ("MIE-Geo", "UTM") => "MIE-UTM".into(),
        ("MIE-Lens", "Extender") => "MIE-Extender".into(),
        ("MIE-MakerNotes", "Canon") => "MIE-Canon".into(),
        _ => format!("MIE-{}", tag_name),
    }
}

/// Resolve MIE internal tag names to ExifTool-compatible names.
fn resolve_tag_name(group: &str, tag: &str) -> (String, String) {
    match group {
        "MIE-Main" => {
            let name = match tag {
                "0Type" => "SubfileType",
                "0Vers" => "MIEVersion",
                "1Directory" => "SubfileDirectory",
                "1Name" => "SubfileName",
                "2MIME" => "SubfileMIMEType",
                "data" => "SubfileData",
                "rsrc" => "SubfileResource",
                "zmd5" => "MD5Digest",
                "zmie" => "TrailerSignature",
                _ => tag,
            };
            (name.into(), "Other".into())
        }
        "MIE-Doc" => {
            let name = match tag {
                "Author" => "Author",
                "Comment" => "Comment",
                "Contributors" => "Contributors",
                "Copyright" => "Copyright",
                "CreateDate" => "CreateDate",
                "EMail" => "Email",
                "Keywords" => "Keywords",
                "ModifyDate" => "ModifyDate",
                "OriginalDate" => "DateTimeOriginal",
                "Phone" => "PhoneNumber",
                "References" => "References",
                "Software" => "Software",
                "Title" => "Title",
                "URL" => "URL",
                _ => tag,
            };
            (name.into(), "Document".into())
        }
        "MIE-Geo" => (tag.into(), "Location".into()),
        "MIE-GPS" => {
            let name = match tag {
                "Altitude" => "GPSAltitude",
                "Bearing" => "GPSDestBearing",
                "Datum" => "GPSMapDatum",
                "Differential" => "GPSDifferential",
                "Distance" => "GPSDestDistance",
                "Heading" => "GPSTrack",
                "Latitude" => "GPSLatitude",
                "Longitude" => "GPSLongitude",
                "MeasureMode" => "GPSMeasureMode",
                "Satellites" => "GPSSatellites",
                "Speed" => "GPSSpeed",
                "DateTime" => "GPSDateTime",
                _ => tag,
            };
            (name.into(), "Location".into())
        }
        "MIE-Image" => {
            let name = match tag {
                "0Type" => "FullSizeImageType",
                "1Name" => "FullSizeImageName",
                "BitDepth" => "BitDepth",
                "ColorSpace" => "ColorSpace",
                "Components" => "ComponentsConfiguration",
                "Compression" => "CompressionRatio",
                "ImageSize" => "ImageSize",
                "Resolution" => "Resolution",
                "data" => "FullSizeImage",
                _ => tag,
            };
            (name.into(), "Image".into())
        }
        "MIE-Preview" => {
            let name = match tag {
                "0Type" => "PreviewImageType",
                "1Name" => "PreviewImageName",
                "ImageSize" => "PreviewImageSize",
                "data" => "PreviewImage",
                _ => tag,
            };
            (name.into(), "Image".into())
        }
        "MIE-Thumbnail" => {
            let name = match tag {
                "0Type" => "ThumbnailImageType",
                "1Name" => "ThumbnailImageName",
                "ImageSize" => "ThumbnailImageSize",
                "data" => "ThumbnailImage",
                _ => tag,
            };
            (name.into(), "Image".into())
        }
        "MIE-Camera" => {
            let name = match tag {
                "Brightness" => "Brightness",
                "ColorTemperature" => "ColorTemperature",
                "Contrast" => "Contrast",
                "DigitalZoom" => "DigitalZoom",
                "ExposureComp" => "ExposureCompensation",
                "ExposureMode" => "ExposureMode",
                "ExposureTime" => "ExposureTime",
                "FirmwareVersion" => "FirmwareVersion",
                "FocusMode" => "FocusMode",
                "ISO" => "ISO",
                "ISOSetting" => "ISOSetting",
                "ImageNumber" => "ImageNumber",
                "ImageQuality" => "ImageQuality",
                "ImageStabilization" => "ImageStabilization",
                "Make" => "Make",
                "MeasuredEV" => "MeasuredEV",
                "Model" => "Model",
                "OwnerName" => "OwnerName",
                "Saturation" => "Saturation",
                "SerialNumber" => "SerialNumber",
                "Sharpness" => "Sharpness",
                "ShootingMode" => "ShootingMode",
                _ => tag,
            };
            (name.into(), "Camera".into())
        }
        "MIE-Lens" => {
            let name = match tag {
                "FNumber" => "FNumber",
                "FocalLength" => "FocalLength",
                "FocusDistance" => "FocusDistance",
                "Make" => "LensMake",
                "MaxAperture" => "MaxAperture",
                "MaxApertureAtMaxFocal" => "MaxApertureAtMaxFocal",
                "MaxFocalLength" => "MaxFocalLength",
                "MinAperture" => "MinAperture",
                "MinFocalLength" => "MinFocalLength",
                "Model" => "LensModel",
                "OpticalZoom" => "OpticalZoom",
                "SerialNumber" => "LensSerialNumber",
                _ => tag,
            };
            (name.into(), "Camera".into())
        }
        "MIE-Flash" => {
            let name = match tag {
                "ExposureComp" => "FlashExposureComp",
                "Fired" => "FlashFired",
                "GuideNumber" => "FlashGuideNumber",
                "Make" => "FlashMake",
                "Mode" => "FlashMode",
                "Model" => "FlashModel",
                "SerialNumber" => "FlashSerialNumber",
                "Type" => "FlashType",
                _ => tag,
            };
            (name.into(), "Camera".into())
        }
        "MIE-Orient" => (tag.into(), "Camera".into()),
        "MIE-Audio" => {
            let name = match tag {
                "0Type" => "RelatedAudioFileType",
                "1Name" => "RelatedAudioFileName",
                "SampleBits" => "SampleBits",
                "Channels" => "Channels",
                "Compression" => "AudioCompression",
                "Duration" => "Duration",
                "SampleRate" => "SampleRate",
                "data" => "RelatedAudioFile",
                _ => tag,
            };
            (name.into(), "Audio".into())
        }
        "MIE-Video" => {
            let name = match tag {
                "0Type" => "RelatedVideoFileType",
                "1Name" => "RelatedVideoFileName",
                "Codec" => "Codec",
                "Duration" => "Duration",
                "data" => "RelatedVideoFile",
                _ => tag,
            };
            (name.into(), "Video".into())
        }
        _ => (tag.into(), "Other".into()),
    }
}