Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
A borrowed document brings the writer, not the other note's history
Yashwanth Kamireddi79f032145263
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rsindex c376402..f759c58 100644--- a/crates/quince-icloud/src/ckdocument.rs+++ b/crates/quince-icloud/src/ckdocument.rs@@ -226,6 +226,12 @@ fn write_varint(mut value: u64, out: &mut Vec<u8>) {/// Which field of the note holds an attribute run.const ATTRIBUTE_RUN: u64 = 5;+/// Records inside the note saying which writer produced which stretch of text.+///+/// Measured against the text that was there when they were written, so an edit makes them+/// false. See the comment where they are dropped.+const PROVENANCE: u64 = 3;+/// A protobuf varint field: `(tag << 3) | 0`, then the value.fn varint_field(tag: u64, value: u64) -> Vec<u8> {let mut out = Vec::with_capacity(8);@@ -672,6 +678,69 @@ pub fn add_merge_records_for_test(original: &str) -> String {B64.encode(gzip(&rebuilt))}+/// What a document says about the note it belongs to: whether it names a writer, and whether+/// it still carries a record of which stretch of text came from where.+#[doc(hidden)]+#[must_use]+pub fn debug_note_fields(original: &str) -> (bool, bool) {+ let Ok(raw) = B64.decode(original.trim()) else {+ return (false, false);+ };+ let Some(plain) = inflate(&raw) else {+ return (false, false);+ };+ let Some(note) = field_at(&plain, &TEXT_PATH[..2]) else {+ return (false, false);+ };+ let (mut history, mut writer) = (false, false);+ let mut reader = Reader::new(note);+ while !reader.done() {+ let Some(key) = reader.varint() else { break };+ match key >> 3 {+ PROVENANCE => history = true,+ 4 => writer = true,+ _ => {}+ }+ if reader.skip(key & 7).is_none() {+ break;+ }+ }+ (writer, history)+}++/// A document borrowed from another note, with that note's history taken out.+///+/// The records at [`PROVENANCE`] say which writer produced which stretch of text, and their+/// lengths are measured against the text that was there. Carried onto a different note they+/// describe something that is not in it, and Apple's reader believes them: a fifteen-character+/// note carrying a longer note's records came out on a phone as the same word four times over,+/// with gaps where the arithmetic ran off the end.+///+/// The writer itself — the record at field 4 — is not history and does travel. A phone stamps+/// every note it makes with the same writer, so that much is true of a new note as well.+///+/// Returns `None` when the document cannot be read, so a caller keeps whatever it had.+#[must_use]+pub fn without_history(original: &str) -> Option<String> {+ let raw = B64.decode(original.trim()).ok()?;+ let plain = inflate(&raw)?;+ let note = field_at(&plain, &TEXT_PATH[..2])?;++ let mut kept = Vec::with_capacity(note.len());+ let mut reader = Reader::new(note);+ while !reader.done() {+ let start = reader.at;+ let key = reader.varint()?;+ let (field, wire) = (key >> 3, key & 7);+ reader.skip(wire)?;+ if field != PROVENANCE {+ kept.extend_from_slice(note.get(start..reader.at)?);+ }+ }++ Some(B64.encode(gzip(&rewrite(&plain, &TEXT_PATH[..2], &kept)?)))+}+/// Whether a document carries the merge records Apple's own reader needs.////// Inside the note message Apple keeps records saying which writer produced which stretch ofdiff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rsindex e4205d4..2adf7d0 100644--- a/crates/quince-icloud/src/notes.rs+++ b/crates/quince-icloud/src/notes.rs@@ -430,7 +430,11 @@ pub async fn create_cloudkit(fn document_like_apples(template: Option<&str>, body: &str) -> String {template.filter(|document| crate::ckdocument::has_merge_records(document))- .and_then(|document| crate::ckdocument::replace_text_in_base64(document, body))+ // The borrowed note's history goes; the writer stays. Carrying another note's record+ // of which stretch of text came from where made a phone show a short note as the same+ // word repeated, with gaps where the arithmetic ran off the end of it.+ .and_then(crate::ckdocument::without_history)+ .and_then(|document| crate::ckdocument::replace_text_in_base64(&document, body)).unwrap_or_else(|| crate::ckdocument::text_to_base64(body))}@@ -1045,18 +1049,37 @@ mod template_tests {crate::ckdocument::add_merge_records_for_test(&built)}+ /// A new note keeps the writer from the note it was shaped by, and none of its history.+ ///+ /// The history is lengths measured against text that is not in this note. Carried over, a+ /// fifteen-character note came out on a phone as the same word four times with gaps in+ /// between — worse than the blank it was meant to fix.#[test]- fn a_new_note_takes_the_shape_of_one_apple_wrote() {+ fn a_new_note_takes_the_writer_but_not_the_other_notes_history() {let template = apples_shape("Something Apple wrote");+ assert_eq!(+ crate::ckdocument::debug_note_fields(&template),+ (true, true),+ "the fixture has both, or this proves nothing"+ );+let made = document_like_apples(Some(&template), "Something written here");assert_eq!(crate::ckdocument::text_from_base64(&made).as_deref(),Some("Something written here"));- assert!(- crate::ckdocument::has_merge_records(&made),- "which is what makes a phone show it"- );+ let (writer, history) = crate::ckdocument::debug_note_fields(&made);+ assert!(writer, "the writer travels");+ assert!(!history, "the other note's history does not");+ }++ /// A note's own history is its own, and an edit leaves it alone.+ #[test]+ fn editing_a_note_keeps_the_notes_own_history() {+ let mine = apples_shape("What I wrote before");+ let edited =+ crate::ckdocument::replace_text_in_base64(&mine, "What I wrote after").unwrap();+ assert_eq!(crate::ckdocument::debug_note_fields(&edited), (true, true));}/// A new account has no note to learn from. Building from nothing is the only option, and