libfreeform 0.1.0

Parser for Apple Freeform pasteboard data: PencilKit ink, the CRLNativeData object graph, and the TSUDescription manifest
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
//! CRL native-archive integration.
//!
//! The envelope parser establishes record boundaries. This module never scans
//! the archive globally or associates values by byte distance: item decoders
//! receive only the record that owns the item. A TSU description is decoded and
//! joined separately so an invalid optional manifest cannot discard native
//! data.

mod assets;
mod connector;
mod envelope;
mod native_ink;
mod shape;
mod text_table;

use std::collections::{BTreeMap, BTreeSet};

use envelope::{NativeArchive, NativeRecord, parse_native_archive};

pub use super::types::TsuEntry;
use super::{
   bplist::{Plist, parse_bplist},
   types::{
      FreeformAsset, FreeformBoardItem, FreeformConnectorEndpoint, FreeformDecodeError,
      FreeformGeometry, FreeformItemKind, FreeformNative, FreeformStyle, TsuValue,
   },
};

/// Parses a `TSUDescription` plist into its ordered board-item routing entries.
///
/// TSU is deliberately strict: a description must be a dictionary containing
/// a `boardItems` array, and every entry must have a string `class`. Every hint
/// is converted recursively so nested values are retained without stringifying
/// or silently dropping channels.
pub fn parse_tsu_description(data: &[u8]) -> Result<Vec<TsuEntry>, FreeformDecodeError> {
   let plist = parse_bplist(data)
      .ok_or_else(|| FreeformDecodeError::invalid("TSUDescription: invalid binary plist"))?;
   let root = plist
      .as_dict()
      .ok_or_else(|| FreeformDecodeError::invalid("TSUDescription: root is not a dictionary"))?;
   let board_items = root
      .get("boardItems")
      .and_then(Plist::as_array)
      .ok_or_else(|| FreeformDecodeError::invalid("TSUDescription: missing boardItems array"))?;

   board_items
      .iter()
      .enumerate()
      .map(|(index, item)| parse_tsu_entry(index, item))
      .collect()
}

fn parse_tsu_entry(index: usize, item: &Plist) -> Result<TsuEntry, FreeformDecodeError> {
   let item = item.as_dict().ok_or_else(|| {
      FreeformDecodeError::invalid(format!(
         "TSUDescription: boardItems[{index}] is not a dictionary"
      ))
   })?;
   let class_name = item
      .get("class")
      .and_then(Plist::as_str)
      .filter(|class_name| !class_name.is_empty())
      .ok_or_else(|| {
         FreeformDecodeError::invalid(format!("TSUDescription: boardItems[{index}] has no class"))
      })?;
   let hints = item
      .iter()
      .filter(|(key, _)| key.as_str() != "class")
      .map(|(key, value)| Ok((key.clone(), tsu_value(value)?)))
      .collect::<Result<BTreeMap<_, _>, FreeformDecodeError>>()?;

   Ok(TsuEntry {
      class_name: class_name
         .strip_prefix("Freeform.")
         .unwrap_or(class_name)
         .to_owned(),
      hints,
   })
}

fn tsu_value(value: &Plist) -> Result<TsuValue, FreeformDecodeError> {
   match value {
      Plist::Null => Ok(TsuValue::Null),
      Plist::Bool(value) => Ok(TsuValue::Bool(*value)),
      Plist::Int(value) => Ok(TsuValue::Integer(*value)),
      Plist::Real(value) if value.is_finite() => Ok(TsuValue::Real(*value)),
      Plist::Real(_) => Err(FreeformDecodeError::invalid("TSUDescription: non-finite real")),
      Plist::String(value) => Ok(TsuValue::String(value.clone())),
      Plist::Data(value) => Ok(TsuValue::Data(value.clone())),
      Plist::Array(values) => values
         .iter()
         .map(tsu_value)
         .collect::<Result<Vec<_>, _>>()
         .map(TsuValue::Array),
      Plist::Dict(values) => values
         .iter()
         .map(|(key, value)| Ok((key.clone(), tsu_value(value)?)))
         .collect::<Result<BTreeMap<_, _>, FreeformDecodeError>>()
         .map(TsuValue::Dictionary),
      Plist::Uid(_) | Plist::OrderedSet(_) | Plist::Set(_) => {
         Err(FreeformDecodeError::invalid("TSUDescription: value cannot be represented losslessly"))
      },
   }
}

/// Decodes the self-contained `CRLNativeData` tier without requiring TSU.
///
/// The returned items retain their exact bounded record bytes. Without a TSU
/// routing manifest they are deliberately `Unknown`; use [`join_crl_tsu`] only
/// after the independent TSU tier has decoded successfully.
pub fn decode_crl_native(data: &[u8]) -> Result<FreeformNative, FreeformDecodeError> {
   let archive = parse_native_archive(data)?;
   Ok(native_from_archive(&archive))
}

fn native_from_archive(archive: &NativeArchive<'_>) -> FreeformNative {
   let items = archive
      .item_ids
      .iter()
      .enumerate()
      .map(|(index, uuid)| board_item_from_record(index, uuid, archive.record(uuid)))
      .collect();

   FreeformNative {
      paste_id: archive.paste_id.clone(),
      compatibility: archive.compatibility.clone(),
      items,
      assets: BTreeMap::new(),
      raw_manifest: archive.manifest.to_vec(),
      raw_index: archive.raw_index.to_vec(),
      raw_archive: archive.archive.to_vec(),
   }
}

fn board_item_from_record(
   index: usize,
   uuid: &str,
   record: Option<&NativeRecord<'_>>,
) -> FreeformBoardItem {
   FreeformBoardItem {
      index,
      uuid: uuid.to_owned(),
      parent_id: None,
      class_name: None,
      hints: BTreeMap::new(),
      geometry: FreeformGeometry::default(),
      style: FreeformStyle::default(),
      kind: FreeformItemKind::Unknown,
      raw_data: record.map_or_else(Vec::new, |record| record.bytes.to_vec()),
   }
}

/// Correlates a successfully decoded native tier with a successfully decoded
/// TSU tier and dispatches class-specific, record-bounded decoders.
///
/// Correlation is positional because both formats define an ordered top-level
/// board-item list. A cardinality mismatch is never guessed or partially
/// joined. On success, every item gets exactly one TSU entry.
pub fn join_crl_tsu(
   mut native: FreeformNative,
   entries: &[TsuEntry],
) -> Result<FreeformNative, FreeformDecodeError> {
   if native.items.len() != entries.len() {
      return Err(FreeformDecodeError::correlation(format!(
         "CRLNativeData has {} board items but TSUDescription has {}",
         native.items.len(),
         entries.len()
      )));
   }

   let item_ids = native
      .items
      .iter()
      .map(|item| item.uuid.clone())
      .collect::<Vec<_>>();
   for (item, entry) in native.items.iter_mut().zip(entries) {
      item.class_name = Some(entry.class_name.clone());
      item.hints = entry.hints.clone();
      if item.raw_data.is_empty() {
         continue;
      }
      let raw_data = std::mem::take(&mut item.raw_data);
      let record = NativeRecord { owner_id: item.uuid.clone(), offset: 0, bytes: &raw_data };
      let decoded = apply_class_decoder(item, &entry.class_name, &record, &item_ids);
      item.raw_data = raw_data;
      let asset = match decoded {
         Ok(asset) => asset,
         Err(error) if error.kind == super::types::FreeformDecodeErrorKind::CorrelationMismatch => {
            return Err(error);
         },
         Err(_) => continue,
      };
      if let Some(asset) = asset {
         if native.assets.insert(asset.id.clone(), asset).is_some() {
            return Err(FreeformDecodeError::correlation(
               "multiple native asset descriptors share an identifier",
            ));
         }
      }
   }
   apply_parent_ids(&mut native.items)?;

   Ok(native)
}

fn apply_class_decoder(
   item: &mut FreeformBoardItem,
   class_name: &str,
   record: &NativeRecord<'_>,
   item_ids: &[String],
) -> Result<Option<FreeformAsset>, FreeformDecodeError> {
   let common = shape::decode_shape(record).ok();
   if let Some((_, _, geometry, style)) = &common {
      item.geometry = geometry.clone();
      item.style = style.clone();
   }

   match class_name {
      "CRLWPShapeItem" | "CRLShapeItem" => {
         let (preset, path) = common.map_or((None, None), |(preset, path, ..)| (preset, path));
         item.kind = FreeformItemKind::Shape { preset, path };
      },
      "CRLWPTextBoxItem" | "CRLTextBoxItem" => {
         item.kind = FreeformItemKind::TextBox { text: text_table::decode_text(record).ok() };
      },
      "CRLWPStickyNoteItem" | "CRLStickyNoteItem" => {
         item.kind = FreeformItemKind::StickyNote { text: text_table::decode_text(record).ok() };
      },
      "CRLWPTableItem" | "CRLTableItem" => {
         let (row_heights, column_widths, cells) =
            text_table::decode_table(record).unwrap_or_default();
         item.kind = FreeformItemKind::Table { row_heights, column_widths, cells };
      },
      "CRLConnectionLineItem" | "CRLConnectorItem" => {
         let (tail, head, routing, path, horizontal_flip, vertical_flip) =
            connector::decode_connector(record)?;
         validate_connector_references(&tail, &head, item_ids)?;
         item.geometry.horizontal_flip = horizontal_flip;
         item.geometry.vertical_flip = vertical_flip;
         item.kind = FreeformItemKind::Connector { tail, head, routing, path };
      },
      "CRLGroupItem" => {
         let (child_ids, counter_transform) = connector::decode_group(record)?;
         validate_group_references(&child_ids, item_ids)?;
         item.kind = FreeformItemKind::Group { child_ids, counter_transform };
      },
      "CRLInkItem" | "CRLNativeInkItem" | "CRLFreehandDrawingItem" => {
         item.kind = FreeformItemKind::Ink { strokes: native_ink::decode_native_ink(record)? };
      },
      "CRLImageItem" => {
         let asset = assets::decode_asset(record);
         item.kind = match assets::decode_media(record) {
            FreeformItemKind::Image { asset_id, crop, mask } => {
               FreeformItemKind::Image { asset_id, crop, mask }
            },
            _ => FreeformItemKind::Image { asset_id: None, crop: None, mask: None },
         };
         return Ok(Some(asset));
      },
      "CRLMediaItem" | "CRLMovieItem" => {
         let asset = assets::decode_asset(record);
         item.kind = match assets::decode_media(record) {
            FreeformItemKind::Media { asset_id, media_type } => {
               FreeformItemKind::Media { asset_id, media_type }
            },
            _ => FreeformItemKind::Media { asset_id: None, media_type: None },
         };
         return Ok(Some(asset));
      },
      "CRLFileItem" => {
         let asset = assets::decode_asset(record);
         item.kind = match assets::decode_media(record) {
            FreeformItemKind::File { asset_id, filename } => {
               FreeformItemKind::File { asset_id, filename }
            },
            _ => FreeformItemKind::File { asset_id: None, filename: None },
         };
         return Ok(Some(asset));
      },
      "CRLURLItem" | "CRLUrlItem" => {
         item.kind = match assets::decode_media(record) {
            FreeformItemKind::Url { url, title } => FreeformItemKind::Url { url, title },
            _ => FreeformItemKind::Url { url: None, title: None },
         };
      },
      "CRLUsdzItem" | "CRLUSDZItem" => {
         let asset = assets::decode_asset(record);
         item.kind = match assets::decode_media(record) {
            FreeformItemKind::Usdz { asset_id, spatial_transform } => {
               FreeformItemKind::Usdz { asset_id, spatial_transform }
            },
            _ => FreeformItemKind::Usdz { asset_id: None, spatial_transform: None },
         };
         return Ok(Some(asset));
      },
      _ => {},
   }
   Ok(None)
}

fn validate_connector_references(
   tail: &FreeformConnectorEndpoint,
   head: &FreeformConnectorEndpoint,
   item_ids: &[String],
) -> Result<(), FreeformDecodeError> {
   for endpoint in [tail, head] {
      if let Some(item_id) = &endpoint.item_id {
         if !item_ids.iter().any(|candidate| candidate == item_id) {
            return Err(FreeformDecodeError::correlation(format!(
               "connector references missing board item {item_id}"
            )));
         }
      }
   }
   Ok(())
}

fn validate_group_references(
   child_ids: &[String],
   item_ids: &[String],
) -> Result<(), FreeformDecodeError> {
   for child_id in child_ids {
      if !item_ids.iter().any(|candidate| candidate == child_id) {
         return Err(FreeformDecodeError::correlation(format!(
            "group references missing board item {child_id}"
         )));
      }
   }
   Ok(())
}

fn apply_parent_ids(items: &mut [FreeformBoardItem]) -> Result<(), FreeformDecodeError> {
   let mut parents = BTreeMap::new();
   for item in items.iter() {
      if let FreeformItemKind::Group { child_ids, .. } = &item.kind {
         for child_id in child_ids {
            if child_id == &item.uuid {
               return Err(FreeformDecodeError::correlation(format!(
                  "group {} contains itself",
                  item.uuid
               )));
            }
            if let Some(previous) = parents.insert(child_id.clone(), item.uuid.clone()) {
               return Err(FreeformDecodeError::correlation(format!(
                  "board item {child_id} belongs to both {previous} and {}",
                  item.uuid
               )));
            }
         }
      }
   }
   for child_id in parents.keys() {
      let mut visited = BTreeSet::new();
      let mut current = child_id.as_str();
      while let Some(parent_id) = parents.get(current) {
         if !visited.insert(current) {
            return Err(FreeformDecodeError::correlation(format!(
               "groups contain a parent cycle through {current}"
            )));
         }
         current = parent_id;
      }
   }
   for item in items {
      item.parent_id = parents.remove(&item.uuid);
   }
   if let Some((child_id, parent_id)) = parents.into_iter().next() {
      return Err(FreeformDecodeError::correlation(format!(
         "group {parent_id} references missing board item {child_id}"
      )));
   }
   Ok(())
}

#[cfg(test)]
mod tests {
   use std::collections::HashMap;

   use super::*;

   const NATIVE: &[u8] = include_bytes!("../fixtures/real-board.crlnative");
   const TSU: &[u8] = include_bytes!("../fixtures/real-board.tsudescription");
   #[test]
   fn native_records_remain_unknown_and_lossless_without_tsu() {
      let native = decode_crl_native(NATIVE).expect("native fixture parses");
      assert_eq!(native.items.len(), 10);
      assert!(native.items.iter().any(|item| !item.raw_data.is_empty()));
      assert_eq!(native.paste_id, "42753C09-5C97-4F7E-A652-F842FEFFCEAD");
      assert!(native.items.iter().all(|item| item.class_name.is_none()));
      assert_eq!(native.raw_archive, parse_native_archive(NATIVE).unwrap().archive);
   }

   #[test]
   fn parsed_tsu_joins_by_equal_ordered_cardinality() {
      let entries = parse_tsu_description(TSU).expect("TSU fixture parses");
      let native = join_crl_tsu(decode_crl_native(NATIVE).unwrap(), &entries).expect("tiers join");
      for (item, entry) in native.items.iter().zip(entries.iter()) {
         assert_eq!(item.class_name.as_deref(), Some(entry.class_name.as_str()));
      }
   }

   #[test]
   fn malformed_tsu_is_an_independent_invalid_tier() {
      assert_eq!(
         parse_tsu_description(b"not a plist").unwrap_err().kind,
         super::super::types::FreeformDecodeErrorKind::Invalid
      );
      assert_eq!(decode_crl_native(NATIVE).unwrap().items.len(), 10);
   }

   #[test]
   fn tsu_values_remain_recursive_and_reject_unrepresentable_plist_types() {
      let value = Plist::Dict(HashMap::from([(
         "nested".into(),
         Plist::Array(vec![Plist::Bool(true), Plist::Data(vec![7, 8])]),
      )]));
      assert_eq!(
         tsu_value(&value).unwrap(),
         TsuValue::Dictionary(BTreeMap::from([(
            "nested".into(),
            TsuValue::Array(vec![TsuValue::Bool(true), TsuValue::Data(vec![7, 8])]),
         )]))
      );
      assert_eq!(
         tsu_value(&Plist::Uid(1)).unwrap_err().kind,
         super::super::types::FreeformDecodeErrorKind::Invalid
      );
   }
   #[test]
   fn mismatch_is_a_correlation_error_without_partial_join() {
      let native = decode_crl_native(NATIVE).unwrap();
      let entries = parse_tsu_description(TSU).unwrap();
      let error = join_crl_tsu(native, &entries[..entries.len() - 1]).unwrap_err();
      assert_eq!(error.kind, super::super::types::FreeformDecodeErrorKind::CorrelationMismatch);
   }

   #[test]
   fn unknown_item_retains_its_bounded_raw_record() {
      let bytes = [0xde, 0xad, 0xbe, 0xef];
      let record = NativeRecord { owner_id: "item-id".into(), offset: 17, bytes: &bytes };
      let item = board_item_from_record(0, "item-id", Some(&record));
      assert!(matches!(item.kind, FreeformItemKind::Unknown));
      assert_eq!(item.raw_data, bytes);
   }
}