|
| 1 | +use std::ops::Range; |
| 2 | + |
| 3 | +use nom::IResult; |
| 4 | + |
| 5 | +use super::BoxHolder; |
| 6 | +use crate::exif::TiffHeader; |
| 7 | + |
| 8 | +/// Size of a UUID in bytes |
| 9 | +pub const UUID_SIZE: usize = 16; |
| 10 | + |
| 11 | +/// Canon CMT box types |
| 12 | +const CMT_BOX_TYPES: &[&str] = &["CMT1", "CMT2", "CMT3"]; |
| 13 | + |
| 14 | +/// Canon's UUID for CR3 files: 85c0b687-820f-11e0-8111-f4ce462b6a48 |
| 15 | +pub const CANON_UUID: [u8; 16] = [ |
| 16 | + 0x85, 0xc0, 0xb6, 0x87, 0x82, 0x0f, 0x11, 0xe0, 0x81, 0x11, 0xf4, 0xce, 0x46, 0x2b, 0x6a, 0x48, |
| 17 | +]; |
| 18 | + |
| 19 | +/// Represents Canon's UUID box containing CMT (Canon Metadata) boxes. |
| 20 | +/// |
| 21 | +/// Canon CR3 files store EXIF metadata in a proprietary UUID box format. |
| 22 | +/// The UUID box contains three CMT (Canon Metadata) sub-boxes: |
| 23 | +/// - CMT1: Main EXIF IFD0 data (camera settings, basic metadata) |
| 24 | +/// - CMT2: ExifIFD data (detailed EXIF information) |
| 25 | +/// - CMT3: MakerNotes data (Canon-specific metadata) |
| 26 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 27 | +pub struct CanonUuidBox { |
| 28 | + /// CMT1 contains the main EXIF IFD0 data (primary metadata) |
| 29 | + cmt1_offset: Option<Range<usize>>, |
| 30 | + /// CMT2 contains the ExifIFD data (detailed EXIF information) |
| 31 | + cmt2_offset: Option<Range<usize>>, |
| 32 | + /// CMT3 contains the MakerNotes data (Canon-specific metadata) |
| 33 | + cmt3_offset: Option<Range<usize>>, |
| 34 | +} |
| 35 | + |
| 36 | +impl CanonUuidBox { |
| 37 | + /// Returns the offset range for the primary EXIF data (CMT1). |
| 38 | + pub fn exif_data_offset(&self) -> Option<&Range<usize>> { |
| 39 | + // For CR3, we primarily use CMT1 which contains the main EXIF IFD0 data |
| 40 | + self.cmt1_offset.as_ref() |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns the offset range for the ExifIFD data (CMT2). |
| 44 | + #[allow(dead_code)] // API method for future use |
| 45 | + pub fn cmt2_data_offset(&self) -> Option<&Range<usize>> { |
| 46 | + self.cmt2_offset.as_ref() |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns the offset range for the MakerNotes data (CMT3). |
| 50 | + #[allow(dead_code)] // API method for future use |
| 51 | + pub fn cmt3_data_offset(&self) -> Option<&Range<usize>> { |
| 52 | + self.cmt3_offset.as_ref() |
| 53 | + } |
| 54 | + |
| 55 | + /// Parses Canon's UUID box to extract CMT (Canon Metadata) box offsets. |
| 56 | + pub fn parse<'a>(uuid_data: &'a [u8], full_input: &'a [u8]) -> IResult<&'a [u8], CanonUuidBox> { |
| 57 | + // Validate input sizes |
| 58 | + if uuid_data.len() < UUID_SIZE { |
| 59 | + tracing::error!( |
| 60 | + "Canon UUID box data too small: {} bytes, expected at least {}", |
| 61 | + uuid_data.len(), |
| 62 | + UUID_SIZE |
| 63 | + ); |
| 64 | + return nom::combinator::fail(uuid_data); |
| 65 | + } |
| 66 | + |
| 67 | + if full_input.is_empty() { |
| 68 | + tracing::error!("Full input is empty for Canon UUID box parsing"); |
| 69 | + return nom::combinator::fail(uuid_data); |
| 70 | + } |
| 71 | + |
| 72 | + // Skip the UUID header |
| 73 | + let mut remain = &uuid_data[UUID_SIZE..]; |
| 74 | + let mut cmt1_offset = None; |
| 75 | + let mut cmt2_offset = None; |
| 76 | + let mut cmt3_offset = None; |
| 77 | + |
| 78 | + tracing::debug!( |
| 79 | + "Parsing Canon UUID box with {} bytes of CMT data", |
| 80 | + remain.len() |
| 81 | + ); |
| 82 | + |
| 83 | + // Parse CMT boxes within the Canon UUID box |
| 84 | + while !remain.is_empty() { |
| 85 | + let (new_remain, bbox) = match BoxHolder::parse(remain) { |
| 86 | + Ok(result) => result, |
| 87 | + Err(e) => { |
| 88 | + tracing::warn!( |
| 89 | + "Failed to parse CMT box, continuing with partial data: {:?}", |
| 90 | + e |
| 91 | + ); |
| 92 | + break; // Stop parsing but return what we found so far |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + let box_type = bbox.box_type(); |
| 97 | + if CMT_BOX_TYPES.contains(&box_type) { |
| 98 | + // Calculate offset safely using slice bounds checking |
| 99 | + let data_start = bbox.data.as_ptr() as usize; |
| 100 | + let input_start = full_input.as_ptr() as usize; |
| 101 | + |
| 102 | + // Ensure the data pointer is within the input bounds |
| 103 | + if data_start < input_start || data_start >= input_start + full_input.len() { |
| 104 | + tracing::warn!("CMT box data pointer outside input bounds"); |
| 105 | + remain = new_remain; |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + let start_offset = data_start - input_start; |
| 110 | + let body_start = start_offset + bbox.header_size(); |
| 111 | + let body_end = start_offset + bbox.data.len(); |
| 112 | + |
| 113 | + // Validate offset ranges are within bounds |
| 114 | + if body_end > full_input.len() { |
| 115 | + tracing::warn!( |
| 116 | + "CMT box body extends beyond input bounds: {}..{} > {}", |
| 117 | + body_start, |
| 118 | + body_end, |
| 119 | + full_input.len() |
| 120 | + ); |
| 121 | + remain = new_remain; |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + let offset_range = body_start..body_end; |
| 126 | + |
| 127 | + // Validate CMT box data has minimum size and reasonable content |
| 128 | + let cmt_data = &full_input[offset_range.clone()]; |
| 129 | + if !Self::validate_cmt_data(box_type, cmt_data) { |
| 130 | + tracing::warn!("CMT box {} failed validation, skipping", box_type); |
| 131 | + remain = new_remain; |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + match box_type { |
| 136 | + "CMT1" => { |
| 137 | + cmt1_offset = Some(offset_range); |
| 138 | + tracing::debug!("Found CMT1 (IFD0) at offset {}..{}", body_start, body_end); |
| 139 | + } |
| 140 | + "CMT2" => { |
| 141 | + cmt2_offset = Some(offset_range); |
| 142 | + tracing::debug!( |
| 143 | + "Found CMT2 (ExifIFD) at offset {}..{}", |
| 144 | + body_start, |
| 145 | + body_end |
| 146 | + ); |
| 147 | + } |
| 148 | + "CMT3" => { |
| 149 | + cmt3_offset = Some(offset_range); |
| 150 | + tracing::debug!( |
| 151 | + "Found CMT3 (MakerNotes) at offset {}..{}", |
| 152 | + body_start, |
| 153 | + body_end |
| 154 | + ); |
| 155 | + } |
| 156 | + _ => unreachable!("box_type should be one of CMT1, CMT2, or CMT3"), |
| 157 | + } |
| 158 | + } else { |
| 159 | + // Skip unknown boxes within Canon UUID |
| 160 | + tracing::debug!("Skipping unknown box type: {}", box_type); |
| 161 | + } |
| 162 | + |
| 163 | + remain = new_remain; |
| 164 | + } |
| 165 | + |
| 166 | + Ok(( |
| 167 | + remain, |
| 168 | + CanonUuidBox { |
| 169 | + cmt1_offset, |
| 170 | + cmt2_offset, |
| 171 | + cmt3_offset, |
| 172 | + }, |
| 173 | + )) |
| 174 | + } |
| 175 | + |
| 176 | + /// Validates CMT box data for basic integrity. |
| 177 | + fn validate_cmt_data(box_type: &str, data: &[u8]) -> bool { |
| 178 | + // Minimum size check - CMT boxes should have at least 8 bytes |
| 179 | + if data.len() < 8 { |
| 180 | + tracing::warn!("CMT box {} too small: {} bytes", box_type, data.len()); |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + match box_type { |
| 185 | + "CMT1" => { |
| 186 | + // CMT1 should start with TIFF header - validate using TiffHeader::parse |
| 187 | + if TiffHeader::parse(data).is_ok() { |
| 188 | + tracing::debug!("CMT1 has valid TIFF header"); |
| 189 | + true |
| 190 | + } else { |
| 191 | + tracing::warn!("CMT1 does not have valid TIFF header"); |
| 192 | + false |
| 193 | + } |
| 194 | + } |
| 195 | + "CMT2" | "CMT3" => { |
| 196 | + // CMT2 and CMT3 should also be TIFF format, but we're more lenient |
| 197 | + // since they might have different internal structures |
| 198 | + if data.len() >= 8 { |
| 199 | + tracing::debug!("CMT box {} has sufficient size", box_type); |
| 200 | + true |
| 201 | + } else { |
| 202 | + tracing::warn!("CMT box {} too small for valid data", box_type); |
| 203 | + false |
| 204 | + } |
| 205 | + } |
| 206 | + _ => { |
| 207 | + tracing::warn!("Unknown CMT box type: {}", box_type); |
| 208 | + false |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments