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.

Write the version fields Apple's own reader expects

Yashwanth Kamireddib42a62e381ed
diff --git a/Cargo.lock b/Cargo.lock
index c401515..2846d4a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3264,6 +3264,7 @@ version = "0.0.0"
dependencies = [
"anyhow",
"base64 0.22.1",
+ "flate2",
"jiff",
"quince-auth",
"quince-core",
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rs
index 57ffe22..0265dcc 100644
--- a/crates/quince-icloud/src/ckdocument.rs
+++ b/crates/quince-icloud/src/ckdocument.rs
@@ -242,13 +242,23 @@ fn utf16_len(text: &str) -> u64 {
text.encode_utf16().count() as u64
}
-/// The document Apple accepts: the text nested at [`TEXT_PATH`], with an attribute run
-/// covering it.
+/// The document Apple accepts: version, then a document, then a note holding the text and an
+/// attribute run covering it.
///
-/// The run is the part that was missing. Apple's clients map styling onto the text through
-/// these runs, and a note or reminder written without one arrives with nothing to render —
-/// which is exactly how everything created from here showed up blank on the phone. A bare
-/// `{ length }` is a complete run; Apple writes exactly that for unstyled text.
+/// Two things had to be right and only one of them was.
+///
+/// The attribute run is how Apple's clients map styling onto text, and something written with
+/// no run has nothing to render against. A bare `{ length }` is a complete run, which is what
+/// Apple writes for unstyled text.
+///
+/// The version fields are the other half, and they were simply missing. Every document the
+/// phone writes opens `08 00` — a version at the top — and its inner document opens `08 00
+/// 10 00` before the note itself. Ours went straight to the note. Our own reader did not mind
+/// and handed the text back perfectly, which is why this survived so long; Apple's reader is
+/// stricter, and a reminder or note created here arrived on the phone with nothing in it.
+///
+/// Checked by laying our bytes beside a reminder the phone wrote: the same fields, in the same
+/// order, differing only in the text.
fn document_bytes(text: &str) -> Vec<u8> {
let mut note = length_delimited(TEXT_PATH[2], text.as_bytes());
note.extend(length_delimited(
@@ -256,11 +266,14 @@ fn document_bytes(text: &str) -> Vec<u8> {
&varint_field(1, utf16_len(text)),
));
- let mut nested = note;
- for tag in TEXT_PATH[..2].iter().rev() {
- nested = length_delimited(*tag, &nested);
- }
- nested
+ // The note, inside a document that states its versions, inside a store that states its own.
+ let mut document = varint_field(1, 0);
+ document.extend(varint_field(2, 0));
+ document.extend(length_delimited(TEXT_PATH[1], &note));
+
+ let mut store = varint_field(1, 0);
+ store.extend(length_delimited(TEXT_PATH[0], &document));
+ store
}
fn gzip(bytes: &[u8]) -> Vec<u8> {
diff --git a/probe/Cargo.toml b/probe/Cargo.toml
index 537b84e..b021d26 100644
--- a/probe/Cargo.toml
+++ b/probe/Cargo.toml
@@ -14,6 +14,7 @@ rpassword = "7"
jiff = "0.2.37"
base64 = "0.22"
uuid.workspace = true
+flate2 = "1"
quince-auth = { path = "../crates/quince-auth" }
quince-core = { path = "../crates/quince-core" }
quince-icloud = { path = "../crates/quince-icloud" }
diff --git a/src/screens/CalendarView.tsx b/src/screens/CalendarView.tsx
index 37353c5..b456033 100644
--- a/src/screens/CalendarView.tsx
+++ b/src/screens/CalendarView.tsx
@@ -16,6 +16,17 @@ const ZONE = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
const startOfDay = (d: Date) =>
Math.floor(new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime() / 1000);
+/**
+ * Local midnight the morning after, as unix seconds.
+ *
+ * Not midnight plus 86,400. On the two days a year a clock moves, a day is twenty-three hours
+ * or twenty-five, so adding a fixed day lands an hour either side of midnight and takes an
+ * extra evening's events into the view or leaves one out. Asking the calendar for the next
+ * date is right wherever the viewer is.
+ */
+const startOfNextDay = (d: Date) =>
+ Math.floor(new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1).getTime() / 1000);
+
const isoDate = (seconds: number) => {
const d = new Date(seconds * 1000);
const pad = (n: number) => String(n).padStart(2, "0");
@@ -109,9 +120,10 @@ function placement(
day: Date,
column: number
): React.CSSProperties {
- const dayStart = new Date(day.getFullYear(), day.getMonth(), day.getDate()).getTime() / 1000;
+ const dayStart = startOfDay(day);
+ const dayEnd = startOfNextDay(day);
const from = Math.max(event.startsAt, dayStart);
- const to = Math.min(event.endsAt, dayStart + 86_400);
+ const to = Math.min(event.endsAt, dayEnd);
const minutesIn = (from - dayStart) / 60;
// A zero-length event still has to be readable, so nothing is thinner than a quarter hour.
const minutesLong = Math.max((to - from) / 60, 15);
@@ -200,18 +212,18 @@ export function CalendarView() {
// and a year view does not fetch it twelve times over.
const range = useMemo(() => {
if (view === "day") {
- return { from: startOfDay(anchor), to: startOfDay(anchor) + 86_400 };
+ return { from: startOfDay(anchor), to: startOfNextDay(anchor) };
}
if (view === "week") {
- return { from: startOfDay(weekDays[0]), to: startOfDay(weekDays[6]) + 86_400 };
+ return { from: startOfDay(weekDays[0]), to: startOfNextDay(weekDays[6]) };
}
if (view === "year") {
return {
from: startOfDay(new Date(anchor.getFullYear(), 0, 1)),
- to: startOfDay(new Date(anchor.getFullYear(), 11, 31)) + 86_400,
+ to: startOfNextDay(new Date(anchor.getFullYear(), 11, 31)),
};
}
- return { from: startOfDay(days[0]), to: startOfDay(days[41]) + 86_400 };
+ return { from: startOfDay(days[0]), to: startOfNextDay(days[41]) };
}, [view, anchor, days, weekDays]);
const read = useCallback(async () => {
diff --git a/src/screens/RemindersView.tsx b/src/screens/RemindersView.tsx
index bdf55bb..71e35e5 100644
--- a/src/screens/RemindersView.tsx
+++ b/src/screens/RemindersView.tsx
@@ -29,11 +29,22 @@ function endOfToday(): number {
);
}
+/**
+ * How many days apart two moments are, counted on a calendar rather than in milliseconds.
+ *
+ * Dividing by 86,400,000 is wrong on the two days a year a clock moves: a day is then
+ * twenty-three hours or twenty-five, and something due tomorrow evening comes out as today.
+ * Counting dates is right wherever the viewer is, including the places that never change.
+ */
+function daysApart(from: Date, to: Date): number {
+ const noon = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12).getTime();
+ return Math.round((noon(to) - noon(from)) / 86_400_000);
+}
+
function dueLabel(seconds: number): { text: string; overdue: boolean } {
const due = new Date(seconds * 1000);
const now = new Date();
- const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate());
- const days = Math.floor((due.getTime() - midnight.getTime()) / 86_400_000);
+ const days = daysApart(now, due);
const time = due.toLocaleTimeString(undefined, {
hour: "numeric",
minute: "2-digit",