Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Notes: tables kept after an empty version are read and written

Yashwanth Kamireddi071d6bf0ab3c
diff --git a/crates/quince-icloud/src/table.rs b/crates/quince-icloud/src/table.rs
index f84bf00..d6c8945 100644
--- a/crates/quince-icloud/src/table.rs
+++ b/crates/quince-icloud/src/table.rs
@@ -91,6 +91,23 @@ fn bytes<'a, 'f>(fs: &'f [(u64, Wire<'a>)], tag: u64) -> impl Iterator<Item = &'
})
}
+/// The version of a table's document that holds the table, and its place among them: the last
+/// with any data. A document may keep more than one (field 2 of the top): a table made on a
+/// newer phone was measured with an empty first version and the table in the second, which a
+/// reader of the first alone took for no table at all.
+fn live_version(document: &[u8]) -> Option<(usize, &[u8])> {
+ let top = fields(document)?;
+ let found = bytes(&top, 2)
+ .enumerate()
+ .filter(|(_, v)| {
+ fields(v)
+ .and_then(|f| bytes(&f, 3).next().map(|d| !d.is_empty()))
+ .unwrap_or(false)
+ })
+ .last();
+ found
+}
+
fn int(fs: &[(u64, Wire<'_>)], tag: u64) -> Option<u64> {
fs.iter().find_map(|(t, w)| match (t, w) {
(t, Wire::Int(v)) if *t == tag => Some(*v),
@@ -276,8 +293,7 @@ fn splice(message: &[u8], tag: u64, nth: usize, payload: &[u8]) -> Option<Vec<u8
/// Where a cell's text lives: the index of its text object among the document's objects.
fn cell_object(document: &[u8], row: usize, column: usize) -> Option<usize> {
- let top = fields(document)?;
- let version = fields(bytes(&top, 2).next()?)?;
+ let version = fields(live_version(document)?.1)?;
let data = fields(bytes(&version, 3).next()?)?;
let doc = Doc {
objects: bytes(&data, 3).collect(),
@@ -311,8 +327,7 @@ fn cell_object(document: &[u8], row: usize, column: usize) -> Option<usize> {
#[must_use]
pub fn with_cell(document: &[u8], row: usize, column: usize, text: &str) -> Option<Vec<u8>> {
let cell = cell_object(document, row, column)?;
- let top = fields(document)?;
- let version_bytes = bytes(&top, 2).next()?;
+ let (version_at, version_bytes) = live_version(document)?;
let version = fields(version_bytes)?;
let data_bytes = bytes(&version, 3).next()?;
let data = fields(data_bytes)?;
@@ -336,13 +351,13 @@ pub fn with_cell(document: &[u8], row: usize, column: usize, text: &str) -> Opti
&new_writers,
)?;
let new_version = splice(version_bytes, 3, 0, &new_data)?;
- splice(document, 2, 0, &new_version)
+ splice(document, 2, version_at, &new_version)
};
// The proof: the splice itself, with the string as it was, changes nothing.
let same = splice(object, 10, 0, string)
.and_then(|o| splice(data_bytes, 3, cell, &o))
.and_then(|d| splice(version_bytes, 3, 0, &d))
- .and_then(|v| splice(document, 2, 0, &v))?;
+ .and_then(|v| splice(document, 2, version_at, &v))?;
if same != document {
return None;
}
@@ -482,8 +497,7 @@ fn with_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
if at > count {
return None;
}
- let top = fields(document)?;
- let version_bytes = bytes(&top, 2).next()?;
+ let (version_at, version_bytes) = live_version(document)?;
let version = fields(version_bytes)?;
let data_bytes = bytes(&version, 3).next()?;
let data = fields(data_bytes)?;
@@ -768,7 +782,7 @@ fn with_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
}
}
let new_version = splice(version_bytes, 3, 0, &new_data)?;
- let out = splice(document, 2, 0, &new_version)?;
+ let out = splice(document, 2, version_at, &new_version)?;
// And it reads back as the same table with one empty row or column at `at`.
let mut expected = before;
@@ -824,8 +838,7 @@ fn without_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
if at >= count || count < 2 {
return None;
}
- let top = fields(document)?;
- let version_bytes = bytes(&top, 2).next()?;
+ let (version_at, version_bytes) = live_version(document)?;
let version = fields(version_bytes)?;
let data_bytes = bytes(&version, 3).next()?;
let data = fields(data_bytes)?;
@@ -1010,7 +1023,7 @@ fn without_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
}
}
let new_version = splice(version_bytes, 3, 0, &new_data)?;
- let out = splice(document, 2, 0, &new_version)?;
+ let out = splice(document, 2, version_at, &new_version)?;
let mut expected = before;
if column {
@@ -1093,8 +1106,7 @@ pub fn cells_from_base64(value: &str) -> Option<Vec<Vec<String>>> {
/// [`cells_from_base64`] over the inflated document.
#[must_use]
pub fn cells(document: &[u8]) -> Option<Vec<Vec<String>>> {
- let top = fields(document)?;
- let version = fields(bytes(&top, 2).next()?)?;
+ let version = fields(live_version(document)?.1)?;
let data = fields(bytes(&version, 3).next()?)?;
let doc = Doc {
objects: bytes(&data, 3).collect(),
diff --git a/crates/quince-icloud/tests/table.rs b/crates/quince-icloud/tests/table.rs
index 34ee165..9798406 100644
--- a/crates/quince-icloud/tests/table.rs
+++ b/crates/quince-icloud/tests/table.rs
@@ -15,3 +15,39 @@ fn an_empty_table_reads_as_two_by_two() {
]
);
}
+
+/// The same table as a newer phone keeps one: an empty version first, the table in the next
+/// (measured on a table in his notes, 2026-10-21). Read, and written, in the version that holds
+/// it — the empty one left exactly as it was.
+#[test]
+fn a_table_after_an_empty_version_is_read_and_written_where_it_lives() {
+ use quince_icloud::table::{cells, with_cell, with_column, with_row, without_row};
+ let one = std::fs::read(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/tests/data/table-empty-2x2.bin"
+ ))
+ .unwrap();
+ // `08 00` (field 1), then the empty version `12 06 08 00 10 00 1a 00`, then the rest.
+ assert_eq!(&one[..2], &[0x08, 0x00]);
+ let empty = [0x12, 0x06, 0x08, 0x00, 0x10, 0x00, 0x1a, 0x00];
+ let two = [&one[..2], &empty[..], &one[2..]].concat();
+ let blank = vec![vec![String::new(), String::new()]; 2];
+ assert_eq!(cells(&two), Some(blank));
+
+ let typed = with_cell(&two, 0, 1, "Hi").expect("a cell written in the live version");
+ assert_eq!(
+ &typed[..10],
+ &two[..10],
+ "the empty version is kept as it was"
+ );
+ assert_eq!(cells(&typed).unwrap()[0][1], "Hi");
+
+ let grown = with_row(&typed, 2).expect("a row added in the live version");
+ assert_eq!(&grown[..10], &two[..10]);
+ assert_eq!(cells(&grown).unwrap().len(), 3);
+ let wider = with_column(&grown, 0).expect("a column added");
+ assert_eq!(cells(&wider).unwrap()[0], vec!["", "", "Hi"]);
+ let back = without_row(&wider, 2).expect("a row removed");
+ assert_eq!(cells(&back).unwrap().len(), 2);
+ assert_eq!(&back[..10], &two[..10]);
+}
diff --git a/probe/examples/table_cell_in_note.rs b/probe/examples/table_cell_in_note.rs
index 2a69dbd..5f1489c 100644
--- a/probe/examples/table_cell_in_note.rs
+++ b/probe/examples/table_cell_in_note.rs
@@ -48,7 +48,7 @@ async fn main() -> Result<()> {
.count()
})
.collect::<Vec<_>>();
- if placed != [1] {
+ if placed.iter().filter(|&&n| n > 0).count() != 1 || placed.iter().sum::<usize>() != 1 {
return Err(anyhow!(
"that table is not placed once in one note titled {title:?} ({placed:?})"
));