| 1 | use crate::{ |
| 2 | Chunk, Error, ExGuid, FileType, PropertySets, Reference, RevisionIndex, Store, |
| 3 | op::content::{NATIVE_INDENTS, measurement_bytes}, |
| 4 | store::crc, |
| 5 | write::{append, append_list, fresh_guid, node}, |
| 6 | }; |
| 7 | use std::collections::BTreeMap; |
| 8 | use web_time::{SystemTime, UNIX_EPOCH}; |
| 9 | |
| 10 | type Result<T> = std::result::Result<T, Error>; |
| 11 | |
| 12 | pub(crate) fn string(value: &str) -> Vec<u8> { |
| 13 | value |
| 14 | .encode_utf16() |
| 15 | .chain([0]) |
| 16 | .flat_map(u16::to_le_bytes) |
| 17 | .collect() |
| 18 | } |
| 19 | |
| 20 | /// An author's initials as OneNote 2010 stores them beside the name (`0x1c001df8`, with |
| 21 | /// no MS-ONE entry): Office's default, the first letter of each word of the name. |
| 22 | pub(crate) fn initials(author: &str) -> String { |
| 23 | author |
| 24 | .split_whitespace() |
| 25 | .filter_map(|word| word.chars().next()) |
| 26 | .flat_map(char::to_uppercase) |
| 27 | .collect() |
| 28 | } |
| 29 | |
| 30 | /// An author object's properties as OneNote 2010 writes them: initials, then the name. |
| 31 | pub(crate) fn author_properties(name: &str) -> Vec<(u32, Vec<u8>)> { |
| 32 | vec![ |
| 33 | (0x1c001df8, string(&initials(name))), |
| 34 | (0x1c001d75, string(name)), |
| 35 | ] |
| 36 | } |
| 37 | |
| 38 | pub(crate) fn default_text_style() -> Vec<(u32, Vec<u8>)> { |
| 39 | vec![ |
| 40 | (0x14001c3b, 0x409_u32.to_le_bytes().to_vec()), |
| 41 | (0x1c001c0a, string("Calibri")), |
| 42 | (0x10001c0b, 22_u16.to_le_bytes().to_vec()), |
| 43 | ] |
| 44 | } |
| 45 | |
| 46 | /// The page margins MS-ONE requires on every page, in half inches, as OneNote 2010 sets them |
| 47 | /// on the pages it creates. |
| 48 | pub(crate) fn page_margins() -> Vec<(u32, Vec<u8>)> { |
| 49 | [ |
| 50 | (0x14001c4c, 1.0_f32), |
| 51 | (0x14001c4d, 1.0), |
| 52 | (0x14001c4e, 2.0), |
| 53 | (0x14001c4f, 2.0), |
| 54 | ] |
| 55 | .into_iter() |
| 56 | .map(|(id, value)| (id, value.to_le_bytes().to_vec())) |
| 57 | .collect() |
| 58 | } |
| 59 | |
| 60 | pub(crate) fn properties(values: &[(u32, Vec<u8>)]) -> Result<Vec<u8>> { |
| 61 | let mut streams: [Vec<u8>; 3] = std::array::from_fn(|_| Vec::new()); |
| 62 | let mut fields = Vec::new(); |
| 63 | for (id, value) in values { |
| 64 | let kind = (id >> 26) & 0x1f; |
| 65 | match kind { |
| 66 | 1 | 2 => assert!(value.is_empty()), |
| 67 | 3..=6 => { |
| 68 | assert_eq!(value.len(), 1 << (kind - 3)); |
| 69 | fields.extend(value); |
| 70 | } |
| 71 | 7 => { |
| 72 | if value.len() >= 0x40000000 { |
| 73 | return Err(Error { |
| 74 | offset: 0, |
| 75 | message: "Property data exceeds the format length limit", |
| 76 | }); |
| 77 | } |
| 78 | fields.extend_from_slice(&(value.len() as u32).to_le_bytes()); |
| 79 | fields.extend(value); |
| 80 | } |
| 81 | 8..=13 => { |
| 82 | assert!(value.len().is_multiple_of(4)); |
| 83 | if kind & 1 == 0 { |
| 84 | assert_eq!(value.len(), 4); |
| 85 | } else { |
| 86 | fields |
| 87 | .extend_from_slice(&u32::try_from(value.len() / 4).unwrap().to_le_bytes()); |
| 88 | } |
| 89 | streams[((kind - 8) / 2) as usize].extend(value); |
| 90 | } |
| 91 | _ => unreachable!(), |
| 92 | } |
| 93 | } |
| 94 | let mut data = Vec::new(); |
| 95 | let extended = !streams[2].is_empty(); |
| 96 | let last = if extended { |
| 97 | 2 |
| 98 | } else if !streams[1].is_empty() { |
| 99 | 1 |
| 100 | } else { |
| 101 | 0 |
| 102 | }; |
| 103 | for (index, stream) in streams.iter().enumerate().take(last + 1) { |
| 104 | let mut header = u32::try_from(stream.len() / 4).unwrap(); |
| 105 | assert!(header <= 0xffffff); |
| 106 | if index == 0 && last == 0 { |
| 107 | header |= 0x80000000; |
| 108 | } |
| 109 | if index < 2 && extended { |
| 110 | header |= 0x40000000; |
| 111 | } |
| 112 | data.extend_from_slice(&header.to_le_bytes()); |
| 113 | data.extend(stream); |
| 114 | } |
| 115 | data.extend_from_slice(&u16::try_from(values.len()).unwrap().to_le_bytes()); |
| 116 | for (id, _) in values { |
| 117 | data.extend_from_slice(&id.to_le_bytes()); |
| 118 | } |
| 119 | data.extend(fields); |
| 120 | data.resize(data.len().next_multiple_of(8), 0); |
| 121 | PropertySets::parse(&data)?; |
| 122 | Ok(data) |
| 123 | } |
| 124 | |
| 125 | struct NewObject { |
| 126 | id: u32, |
| 127 | jcid: u32, |
| 128 | properties: Vec<(u32, Vec<u8>)>, |
| 129 | } |
| 130 | |
| 131 | struct NewSpace { |
| 132 | id: u32, |
| 133 | roots: Vec<(u32, u32)>, |
| 134 | objects: Vec<NewObject>, |
| 135 | } |
| 136 | |
| 137 | /// Header bytes 128..148: `guidAncestor` and `crcName`, which OneNote checks against a |
| 138 | /// file's location and name; a mismatch makes it re-identify the file. |
| 139 | pub(crate) fn placement(ancestor: [u8; 16], name: &str) -> [u8; 20] { |
| 140 | let mut bytes = [0; 20]; |
| 141 | bytes[..16].copy_from_slice(&ancestor); |
| 142 | bytes[16..].copy_from_slice(&(!crc(u32::MAX, &string(name), FileType::Section)).to_le_bytes()); |
| 143 | bytes |
| 144 | } |
| 145 | |
| 146 | thread_local! { |
| 147 | /// The FILETIME `at` gives writers in place of the system clock. |
| 148 | static CLOCK: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) }; |
| 149 | } |
| 150 | |
| 151 | /// Runs `f` with the writers' clock reading `filetime`, as an op's modification time. |
| 152 | pub(crate) fn at<T>(filetime: u64, f: impl FnOnce() -> T) -> T { |
| 153 | struct Restore(Option<u64>); |
| 154 | impl Drop for Restore { |
| 155 | fn drop(&mut self) { |
| 156 | CLOCK.set(self.0); |
| 157 | } |
| 158 | } |
| 159 | let _restore = Restore(CLOCK.replace(Some(filetime))); |
| 160 | f() |
| 161 | } |
| 162 | |
| 163 | /// Seconds since 1980 as OneNote's Time32, and FILETIME, of the writers' clock. |
| 164 | pub(crate) fn current_timestamps() -> Result<(u32, u64)> { |
| 165 | let now = match CLOCK.get() { |
| 166 | Some(filetime) => (filetime / 10_000_000) |
| 167 | .checked_sub(11644473600) |
| 168 | .ok_or(Error { |
| 169 | offset: 0, |
| 170 | message: "An edit time precedes the Unix epoch", |
| 171 | })?, |
| 172 | None => SystemTime::now() |
| 173 | .duration_since(UNIX_EPOCH) |
| 174 | .map_err(|_| Error { |
| 175 | offset: 0, |
| 176 | message: "System time precedes the Unix epoch", |
| 177 | })? |
| 178 | .as_secs(), |
| 179 | }; |
| 180 | let modified = now |
| 181 | .checked_sub(315532800) |
| 182 | .and_then(|time| u32::try_from(time).ok()) |
| 183 | .ok_or(Error { |
| 184 | offset: 0, |
| 185 | message: "System time exceeds the OneNote Time32 range", |
| 186 | })?; |
| 187 | Ok((modified, (now + 11644473600) * 10000000)) |
| 188 | } |
| 189 | |
| 190 | /// Creates a section with one page and one plain-text paragraph, without a template. |
| 191 | /// Text must not contain NUL or line-feed characters. |
| 192 | pub fn create_section(file_name: &str, text: &str, author: &str) -> Result<Vec<u8>> { |
| 193 | if file_name.contains(['/', '\\', '\0']) |
| 194 | || !file_name.to_ascii_lowercase().ends_with(".one") |
| 195 | || text.contains(['\0', '\n']) |
| 196 | || author.contains('\0') |
| 197 | { |
| 198 | return Err(Error { |
| 199 | offset: 0, |
| 200 | message: "Invalid section name, paragraph text, or author", |
| 201 | }); |
| 202 | } |
| 203 | let (modified, timestamp) = current_timestamps()?; |
| 204 | let timestamp = timestamp.to_le_bytes().to_vec(); |
| 205 | let page_guid = fresh_guid()?; |
| 206 | let metadata = vec![ |
| 207 | (0x1c001c30, page_guid.to_vec()), |
| 208 | (0x1c001cf3, string(&crate::edit::automatic_title(text))), |
| 209 | (0x14001d82, 40_u32.to_le_bytes().to_vec()), |
| 210 | (0x1400348b, 40_u32.to_le_bytes().to_vec()), |
| 211 | (0x14001dff, 1_u32.to_le_bytes().to_vec()), |
| 212 | (0x18001c65, timestamp.clone()), |
| 213 | ]; |
| 214 | let id = |value: u32| value.to_le_bytes().to_vec(); |
| 215 | let last_modified = || (0x14001d7a, modified.to_le_bytes().to_vec()); |
| 216 | let spaces = vec![ |
| 217 | NewSpace { |
| 218 | id: 1, |
| 219 | roots: vec![(1, 10), (2, 11)], |
| 220 | objects: vec![ |
| 221 | NewObject { |
| 222 | id: 10, |
| 223 | jcid: 0x60007, |
| 224 | properties: vec![ |
| 225 | (0x1c001c30, fresh_guid()?.to_vec()), |
| 226 | (0x18001c65, timestamp.clone()), |
| 227 | (0x24001c20, id(12)), |
| 228 | ], |
| 229 | }, |
| 230 | NewObject { |
| 231 | id: 11, |
| 232 | jcid: 0x20031, |
| 233 | properties: vec![ |
| 234 | (0x14001d82, id(40)), |
| 235 | (0x1400348b, id(40)), |
| 236 | (0x14001cbe, vec![0x8a, 0xa8, 0xe4, 0]), |
| 237 | ], |
| 238 | }, |
| 239 | NewObject { |
| 240 | id: 12, |
| 241 | jcid: 0x60008, |
| 242 | properties: vec![ |
| 243 | (0x1c001c30, fresh_guid()?.to_vec()), |
| 244 | (0x18001c65, timestamp), |
| 245 | (0x2c001d63, id(257)), |
| 246 | ], |
| 247 | }, |
| 248 | ], |
| 249 | }, |
| 250 | NewSpace { |
| 251 | id: 257, |
| 252 | roots: vec![(1, 20), (2, 21)], |
| 253 | objects: vec![ |
| 254 | NewObject { |
| 255 | id: 20, |
| 256 | jcid: 0x60037, |
| 257 | properties: vec![(0x24001c1f, id(22))], |
| 258 | }, |
| 259 | NewObject { |
| 260 | id: 21, |
| 261 | jcid: 0x20030, |
| 262 | properties: metadata, |
| 263 | }, |
| 264 | NewObject { |
| 265 | id: 22, |
| 266 | jcid: 0x6000b, |
| 267 | properties: [ |
| 268 | vec![ |
| 269 | last_modified(), |
| 270 | (0x24001c20, id(23)), |
| 271 | (0x1c001d75, string(author)), |
| 272 | (0x1c001df8, string(&initials(author))), |
| 273 | (0x1c001d3c, string(&crate::edit::automatic_title(text))), |
| 274 | ], |
| 275 | page_margins(), |
| 276 | ] |
| 277 | .concat(), |
| 278 | }, |
| 279 | NewObject { |
| 280 | id: 23, |
| 281 | jcid: 0x6000c, |
| 282 | properties: vec![ |
| 283 | last_modified(), |
| 284 | (0x24001c20, id(24)), |
| 285 | (0x0c001c03, vec![1]), |
| 286 | (0x1c001c12, measurement_bytes(&NATIVE_INDENTS, 4)?), |
| 287 | (0x14001c14, 1_f32.to_le_bytes().to_vec()), |
| 288 | (0x14001c15, 1_f32.to_le_bytes().to_vec()), |
| 289 | (0x14001c1b, 13_f32.to_le_bytes().to_vec()), |
| 290 | (0x14001c1c, 0.6_f32.to_le_bytes().to_vec()), |
| 291 | ], |
| 292 | }, |
| 293 | NewObject { |
| 294 | id: 24, |
| 295 | jcid: 0x6000d, |
| 296 | properties: vec![ |
| 297 | last_modified(), |
| 298 | (0x24001c1f, id(25)), |
| 299 | (0x0c001c03, vec![1]), |
| 300 | (0x20001d78, id(26)), |
| 301 | (0x20001d79, id(26)), |
| 302 | (0x14001d09, modified.to_le_bytes().to_vec()), |
| 303 | ], |
| 304 | }, |
| 305 | NewObject { |
| 306 | id: 25, |
| 307 | jcid: 0x6000e, |
| 308 | properties: vec![ |
| 309 | last_modified(), |
| 310 | (0x1c001c22, string(text)), |
| 311 | (0x24001e13, id(27)), |
| 312 | (0x10001cfe, 0x409_u16.to_le_bytes().to_vec()), |
| 313 | ], |
| 314 | }, |
| 315 | NewObject { |
| 316 | id: 26, |
| 317 | jcid: 0x120001, |
| 318 | properties: author_properties(author), |
| 319 | }, |
| 320 | NewObject { |
| 321 | id: 27, |
| 322 | jcid: 0x12004d, |
| 323 | properties: default_text_style(), |
| 324 | }, |
| 325 | ], |
| 326 | }, |
| 327 | ]; |
| 328 | create(file_name, FileType::Section, spaces) |
| 329 | } |
| 330 | |
| 331 | /// Creates a section without pages, coloured `color` (COLORREF, `None` for no colour), as |
| 332 | /// OneNote 2010 leaves one whose pages are all deleted: its section node lists no pages. |
| 333 | /// Pages come as `op::SectionOp::Create` edits. |
| 334 | pub fn create_empty_section(file_name: &str, color: Option<u32>) -> Result<Vec<u8>> { |
| 335 | if file_name.contains(['/', '\\', '\0']) || !file_name.to_ascii_lowercase().ends_with(".one") { |
| 336 | return Err(Error { |
| 337 | offset: 0, |
| 338 | message: "Invalid section name", |
| 339 | }); |
| 340 | } |
| 341 | let timestamp = current_timestamps()?.1.to_le_bytes().to_vec(); |
| 342 | let id = |value: u32| value.to_le_bytes().to_vec(); |
| 343 | let spaces = vec![NewSpace { |
| 344 | id: 1, |
| 345 | roots: vec![(1, 10), (2, 11)], |
| 346 | objects: vec![ |
| 347 | NewObject { |
| 348 | id: 10, |
| 349 | jcid: 0x60007, |
| 350 | properties: vec![ |
| 351 | (0x1c001c30, fresh_guid()?.to_vec()), |
| 352 | (0x18001c65, timestamp), |
| 353 | ], |
| 354 | }, |
| 355 | NewObject { |
| 356 | id: 11, |
| 357 | jcid: 0x20031, |
| 358 | properties: vec![ |
| 359 | (0x14001d82, id(40)), |
| 360 | (0x1400348b, id(40)), |
| 361 | (0x14001cbe, id(color.unwrap_or(0xffff_ffff))), |
| 362 | ], |
| 363 | }, |
| 364 | ], |
| 365 | }]; |
| 366 | create(file_name, FileType::Section, spaces) |
| 367 | } |
| 368 | |
| 369 | /// Creates a notebook table of contents from section filenames and file identities. |
| 370 | pub fn create_table_of_contents(file_name: &str, sections: &[(&str, [u8; 16])]) -> Result<Vec<u8>> { |
| 371 | if file_name.contains(['/', '\\', '\0']) |
| 372 | || !file_name.to_ascii_lowercase().ends_with(".onetoc2") |
| 373 | || sections.len() > 0xfffffe |
| 374 | { |
| 375 | return Err(Error { |
| 376 | offset: 0, |
| 377 | message: "Invalid table-of-contents filename or section count", |
| 378 | }); |
| 379 | } |
| 380 | let mut objects = Vec::new(); |
| 381 | let mut children = Vec::new(); |
| 382 | let mut names = std::collections::BTreeSet::new(); |
| 383 | let mut identities = std::collections::BTreeSet::new(); |
| 384 | for (index, (name, identity)) in sections.iter().enumerate() { |
| 385 | if name.contains(['/', '\\', '\0']) |
| 386 | || !name.to_ascii_lowercase().ends_with(".one") |
| 387 | || !names.insert(name.to_lowercase()) |
| 388 | || *identity == [0; 16] |
| 389 | || !identities.insert(*identity) |
| 390 | { |
| 391 | return Err(Error { |
| 392 | offset: 0, |
| 393 | message: "Invalid or duplicate section filename or identity", |
| 394 | }); |
| 395 | } |
| 396 | let id = (((index + 1) as u32) << 8) | 10; |
| 397 | children.extend_from_slice(&id.to_le_bytes()); |
| 398 | objects.push(NewObject { |
| 399 | id, |
| 400 | jcid: 0x20001, |
| 401 | properties: vec![ |
| 402 | (0x1c001d94, identity.to_vec()), |
| 403 | (0x14001cb9, ((index + 1) as u32).to_le_bytes().to_vec()), |
| 404 | (0x1c001d6b, string(name)), |
| 405 | (0x14001cbe, vec![0xff; 4]), |
| 406 | ], |
| 407 | }); |
| 408 | } |
| 409 | objects.push(NewObject { |
| 410 | id: 10, |
| 411 | jcid: 0x20001, |
| 412 | properties: vec![(0x24001cf6, children)], |
| 413 | }); |
| 414 | create( |
| 415 | file_name, |
| 416 | FileType::TableOfContents, |
| 417 | vec![NewSpace { |
| 418 | id: 1, |
| 419 | roots: vec![(1, 10)], |
| 420 | objects, |
| 421 | }], |
| 422 | ) |
| 423 | } |
| 424 | |
| 425 | fn create(file_name: &str, file_type: FileType, spaces: Vec<NewSpace>) -> Result<Vec<u8>> { |
| 426 | let is_section = file_type == FileType::Section; |
| 427 | let maximum = spaces |
| 428 | .iter() |
| 429 | .flat_map(|space| { |
| 430 | std::iter::once(space.id).chain(space.objects.iter().map(|object| object.id)) |
| 431 | }) |
| 432 | .max() |
| 433 | .unwrap(); |
| 434 | let guids = (0..=(maximum >> 8)) |
| 435 | .map(|_| fresh_guid()) |
| 436 | .collect::<Result<Vec<_>>>()?; |
| 437 | let exguid = |compact: u32| ExGuid { |
| 438 | guid: guids[(compact >> 8) as usize], |
| 439 | n: compact & 255, |
| 440 | }; |
| 441 | let mut output = vec![0; 1024]; |
| 442 | let mut root_payload = Vec::new(); |
| 443 | exguid(spaces[0].id).encode(&mut root_payload); |
| 444 | let mut root = Vec::new(); |
| 445 | let mut log = Vec::new(); |
| 446 | let mut list_id = 16_u32; |
| 447 | let mut hashed = Vec::new(); |
| 448 | for space in spaces { |
| 449 | let mut counts = BTreeMap::<u32, u32>::new(); |
| 450 | for (_, id) in &space.roots { |
| 451 | *counts.entry(*id).or_default() += 1; |
| 452 | } |
| 453 | for object in &space.objects { |
| 454 | for (property, value) in &object.properties { |
| 455 | if matches!((property >> 26) & 0x1f, 8 | 9) { |
| 456 | for id in value.chunks_exact(4) { |
| 457 | *counts |
| 458 | .entry(u32::from_le_bytes(id.try_into().unwrap())) |
| 459 | .or_default() += 1; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | let group_id = ExGuid { |
| 465 | guid: fresh_guid()?, |
| 466 | n: 1, |
| 467 | }; |
| 468 | let mut group_payload = Vec::new(); |
| 469 | group_id.encode(&mut group_payload); |
| 470 | let mut group = if is_section { |
| 471 | vec![node(0xb4, None, &group_payload)?, node(0x22, None, &[])?] |
| 472 | } else { |
| 473 | vec![node(0x21, None, &[0])?] |
| 474 | }; |
| 475 | for (index, guid) in guids.iter().enumerate() { |
| 476 | let mut entry = (index as u32).to_le_bytes().to_vec(); |
| 477 | entry.extend_from_slice(guid); |
| 478 | group.push(node(0x24, None, &entry)?); |
| 479 | } |
| 480 | group.push(node(0x28, None, &[])?); |
| 481 | let mut override_crc = if is_section { u32::MAX } else { 0 }; |
| 482 | for object in space.objects { |
| 483 | let data = properties(&object.properties)?; |
| 484 | let chunk = append(&mut output, &data)?; |
| 485 | let mut payload = object.id.to_le_bytes().to_vec(); |
| 486 | let mut flags = 0; |
| 487 | for (property, value) in object.properties { |
| 488 | if !value.is_empty() { |
| 489 | match (property >> 26) & 0x1f { |
| 490 | 8 | 9 => flags |= 1, |
| 491 | 10..=13 => flags |= 2, |
| 492 | _ => {} |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | if is_section { |
| 497 | payload.extend_from_slice(&object.jcid.to_le_bytes()); |
| 498 | payload.push(flags); |
| 499 | } else { |
| 500 | let body = 1_u64 | (u64::from(flags & 1) << 16); |
| 501 | payload.extend_from_slice(&body.to_le_bytes()[..6]); |
| 502 | } |
| 503 | let count = counts.get(&object.id).copied().unwrap_or(0).to_le_bytes(); |
| 504 | payload.extend_from_slice(&count); |
| 505 | override_crc = crc(override_crc, &count, file_type); |
| 506 | let readonly = object.jcid & 0x100000 != 0; |
| 507 | if readonly { |
| 508 | let hash = md5::compute(&data).0; |
| 509 | payload.extend_from_slice(&hash); |
| 510 | hashed.push(node(0xc2, Some(Reference::Data(chunk)), &hash)?); |
| 511 | } |
| 512 | group.push(node( |
| 513 | if !is_section { |
| 514 | 0x2e |
| 515 | } else if readonly { |
| 516 | 0xc5 |
| 517 | } else { |
| 518 | 0xa5 |
| 519 | }, |
| 520 | Some(Reference::Data(chunk)), |
| 521 | &payload, |
| 522 | )?); |
| 523 | } |
| 524 | let group_chunk = if is_section { |
| 525 | group.push(node(0xb8, None, &[])?); |
| 526 | Some(append_list(&mut output, list_id, &group)?) |
| 527 | } else { |
| 528 | None |
| 529 | }; |
| 530 | let group_count = group.len(); |
| 531 | let mut start = Vec::new(); |
| 532 | exguid(space.id).encode(&mut start); |
| 533 | start.extend_from_slice(&0_u32.to_le_bytes()); |
| 534 | let mut revision = Vec::new(); |
| 535 | ExGuid { |
| 536 | guid: fresh_guid()?, |
| 537 | n: 1, |
| 538 | } |
| 539 | .encode(&mut revision); |
| 540 | ExGuid::default().encode(&mut revision); |
| 541 | if !is_section { |
| 542 | revision.extend_from_slice(&0_u64.to_le_bytes()); |
| 543 | } |
| 544 | revision.extend_from_slice(&1_u32.to_le_bytes()); |
| 545 | revision.extend_from_slice(&0_u16.to_le_bytes()); |
| 546 | let mut manifest = vec![ |
| 547 | node(0x14, None, &start)?, |
| 548 | node(if is_section { 0x1e } else { 0x1b }, None, &revision)?, |
| 549 | ]; |
| 550 | if let Some(chunk) = group_chunk { |
| 551 | manifest.push(node( |
| 552 | 0xb0, |
| 553 | Some(Reference::NodeList(chunk)), |
| 554 | &group_payload, |
| 555 | )?); |
| 556 | let mut overrides = vec![0; 8]; |
| 557 | overrides.extend_from_slice(&(!override_crc).to_le_bytes()); |
| 558 | manifest.push(node( |
| 559 | 0x84, |
| 560 | Some(Reference::Data(Chunk { |
| 561 | offset: u64::MAX, |
| 562 | length: 0, |
| 563 | })), |
| 564 | &overrides, |
| 565 | )?); |
| 566 | } else { |
| 567 | manifest.extend(group); |
| 568 | } |
| 569 | for (role, oid) in space.roots { |
| 570 | let mut payload = Vec::new(); |
| 571 | if is_section { |
| 572 | exguid(oid).encode(&mut payload); |
| 573 | } else { |
| 574 | payload.extend_from_slice(&oid.to_le_bytes()); |
| 575 | } |
| 576 | payload.extend_from_slice(&role.to_le_bytes()); |
| 577 | manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?); |
| 578 | } |
| 579 | manifest.push(node(0x1c, None, &[])?); |
| 580 | let manifest_chunk = append_list(&mut output, list_id + 1, &manifest)?; |
| 581 | let mut space_payload = Vec::new(); |
| 582 | exguid(space.id).encode(&mut space_payload); |
| 583 | let space_nodes = vec![ |
| 584 | node(0xc, None, &space_payload)?, |
| 585 | node(0x10, Some(Reference::NodeList(manifest_chunk)), &[])?, |
| 586 | ]; |
| 587 | let space_chunk = append_list(&mut output, list_id + 2, &space_nodes)?; |
| 588 | root.push(node( |
| 589 | 8, |
| 590 | Some(Reference::NodeList(space_chunk)), |
| 591 | &space_payload, |
| 592 | )?); |
| 593 | for (id, count) in [ |
| 594 | (list_id, group_count), |
| 595 | (list_id + 1, manifest.len()), |
| 596 | (list_id + 2, space_nodes.len()), |
| 597 | ] { |
| 598 | if id == list_id && !is_section { |
| 599 | continue; |
| 600 | } |
| 601 | log.extend_from_slice(&id.to_le_bytes()); |
| 602 | log.extend_from_slice(&u32::try_from(count).unwrap().to_le_bytes()); |
| 603 | } |
| 604 | list_id += 3; |
| 605 | } |
| 606 | root.push(node(4, None, &root_payload)?); |
| 607 | let root_chunk = append_list(&mut output, list_id, &root)?; |
| 608 | log.extend_from_slice(&list_id.to_le_bytes()); |
| 609 | log.extend_from_slice(&u32::try_from(root.len()).unwrap().to_le_bytes()); |
| 610 | let hashed_chunk = if !hashed.is_empty() { |
| 611 | let chunk = append_list(&mut output, list_id + 1, &hashed)?; |
| 612 | log.extend_from_slice(&(list_id + 1).to_le_bytes()); |
| 613 | log.extend_from_slice(&u32::try_from(hashed.len()).unwrap().to_le_bytes()); |
| 614 | Some(chunk) |
| 615 | } else { |
| 616 | None |
| 617 | }; |
| 618 | finish( |
| 619 | &mut output, |
| 620 | file_type, |
| 621 | log, |
| 622 | hashed_chunk, |
| 623 | root_chunk, |
| 624 | placement([0; 16], file_name), |
| 625 | )?; |
| 626 | let store = Store::parse(&output)?; |
| 627 | RevisionIndex::parse(&store)?.validate_current()?; |
| 628 | Ok(output) |
| 629 | } |
| 630 | |
| 631 | /// Ends an image whose lists `output` holds: the transaction log committing `log`'s |
| 632 | /// entries, then the header naming it, the root list and the hashed-chunk list. |
| 633 | fn finish( |
| 634 | output: &mut Vec<u8>, |
| 635 | file_type: FileType, |
| 636 | mut log: Vec<u8>, |
| 637 | hashed: Option<Chunk>, |
| 638 | root: Chunk, |
| 639 | placement: [u8; 20], |
| 640 | ) -> Result<()> { |
| 641 | let is_section = file_type == FileType::Section; |
| 642 | let checksum = if is_section { |
| 643 | !crc(u32::MAX, &log, file_type) |
| 644 | } else { |
| 645 | crc(0, &log, file_type) |
| 646 | }; |
| 647 | log.extend_from_slice(&1_u32.to_le_bytes()); |
| 648 | log.extend_from_slice(&checksum.to_le_bytes()); |
| 649 | log.extend_from_slice(&u64::MAX.to_le_bytes()); |
| 650 | log.extend_from_slice(&0_u32.to_le_bytes()); |
| 651 | let log_chunk = append(output, &log)?; |
| 652 | output[..16].copy_from_slice(&[ |
| 653 | 0xe4, 0x52, 0x5c, 0x7b, 0x8c, 0xd8, 0xa7, 0x4d, 0xae, 0xb1, 0x53, 0x78, 0xd0, 0x29, 0x96, |
| 654 | 0xd3, |
| 655 | ]); |
| 656 | if !is_section { |
| 657 | output[..16].copy_from_slice(&[ |
| 658 | 0xa1, 0x2f, 0xff, 0x43, 0xd9, 0xef, 0x76, 0x4c, 0x9e, 0xe2, 0x10, 0xea, 0x57, 0x22, |
| 659 | 0x76, 0x5f, |
| 660 | ]); |
| 661 | } |
| 662 | output[16..32].copy_from_slice(&fresh_guid()?); |
| 663 | output[48..64].copy_from_slice(&[ |
| 664 | 0x3f, 0xdd, 0x9a, 0x10, 0x1b, 0x91, 0xf5, 0x49, 0xa5, 0xd0, 0x17, 0x91, 0xed, 0xc8, 0xae, |
| 665 | 0xd8, |
| 666 | ]); |
| 667 | for at in [64, 68, 72, 76] { |
| 668 | output[at..at + 4] |
| 669 | .copy_from_slice(&(if is_section { 42_u32 } else { 27_u32 }).to_le_bytes()); |
| 670 | } |
| 671 | for at in [88, 112] { |
| 672 | output[at..at + 4].copy_from_slice(&u32::MAX.to_le_bytes()); |
| 673 | } |
| 674 | output[96..100].copy_from_slice(&1_u32.to_le_bytes()); |
| 675 | output[128..148].copy_from_slice(&placement); |
| 676 | for (at, chunk) in hashed |
| 677 | .map(|chunk| (148, chunk)) |
| 678 | .into_iter() |
| 679 | .chain([(160, log_chunk), (172, root)]) |
| 680 | { |
| 681 | output[at..at + 8].copy_from_slice(&chunk.offset.to_le_bytes()); |
| 682 | output[at + 8..at + 12].copy_from_slice(&(chunk.length as u32).to_le_bytes()); |
| 683 | } |
| 684 | let length = output.len() as u64; |
| 685 | output[196..204].copy_from_slice(&length.to_le_bytes()); |
| 686 | output[212..228].copy_from_slice(&fresh_guid()?); |
| 687 | output[228..236].copy_from_slice(&1_u64.to_le_bytes()); |
| 688 | output[236..252].copy_from_slice(&fresh_guid()?); |
| 689 | Ok(()) |
| 690 | } |
| 691 | |
| 692 | /// A new section file declaring only its root object space `root`, which has no revision |
| 693 | /// yet, placed as `placement`: what a whole section's revisions are appended to. |
| 694 | pub(crate) fn skeleton(root: ExGuid, placement: [u8; 20]) -> Result<Vec<u8>> { |
| 695 | let mut output = vec![0; 1024]; |
| 696 | let mut payload = Vec::new(); |
| 697 | root.encode(&mut payload); |
| 698 | let mut start = payload.clone(); |
| 699 | start.extend_from_slice(&0_u32.to_le_bytes()); |
| 700 | let manifest = append_list(&mut output, 16, &[node(0x14, None, &start)?])?; |
| 701 | let space = [ |
| 702 | node(0xc, None, &payload)?, |
| 703 | node(0x10, Some(Reference::NodeList(manifest)), &[])?, |
| 704 | ]; |
| 705 | let space = append_list(&mut output, 17, &space)?; |
| 706 | let roots = [ |
| 707 | node(8, Some(Reference::NodeList(space)), &payload)?, |
| 708 | node(4, None, &payload)?, |
| 709 | ]; |
| 710 | let roots = append_list(&mut output, 18, &roots)?; |
| 711 | let mut log = Vec::new(); |
| 712 | for (id, count) in [(16_u32, 1_u32), (17, 2), (18, 2)] { |
| 713 | log.extend_from_slice(&id.to_le_bytes()); |
| 714 | log.extend_from_slice(&count.to_le_bytes()); |
| 715 | } |
| 716 | finish(&mut output, FileType::Section, log, None, roots, placement)?; |
| 717 | Ok(output) |
| 718 | } |
| 719 | |
| 720 | #[cfg(test)] |
| 721 | mod tests { |
| 722 | #[test] |
| 723 | fn initials_take_each_word_first_letter() { |
| 724 | // OneNote 2010 stores "snow" with "S" in the native corpus. |
| 725 | assert_eq!(super::initials("snow"), "S"); |
| 726 | assert_eq!(super::initials("Clover"), "C"); |
| 727 | assert_eq!(super::initials(" Snowbound Test "), "ST"); |
| 728 | assert_eq!(super::initials("émile zola"), "ÉZ"); |
| 729 | assert_eq!(super::initials(""), ""); |
| 730 | } |
| 731 | } |