1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use std::collections::HashMap;
67use crate::core::BinaryReader;
89const FINGERPRINT: &[u8] = "RIVE".as_bytes();
1011/// Rive file runtime header. The header is fonud at the beginning of every
12/// Rive runtime file, and begins with a specific 4-byte format: "RIVE".
13/// This is followed by the major and minor version of Rive used to create
14/// the file. Finally the owner and file ids are at the end of header; these
15/// unsigned integers may be zero.
16#[derive(Debug)]
17pub struct RuntimeHeader {
18 major_version: u32,
19// TODO(https://fxbug.dev/42165549)
20#[allow(unused)]
21minor_version: u32,
22// TODO(https://fxbug.dev/42165549)
23#[allow(unused)]
24file_id: u32,
25 property_to_field_index: HashMap<u32, u32>,
26}
2728impl RuntimeHeader {
29/// Reads the header from a binary buffer.
30pub fn read(reader: &mut BinaryReader<'_>) -> Option<Self> {
31if reader.read_bytes(FINGERPRINT.len())? != FINGERPRINT {
32return None;
33 }
3435let major_version = reader.read_var_u64()? as u32;
36let minor_version = reader.read_var_u64()? as u32;
37let file_id = reader.read_var_u64()? as u32;
3839let mut property_keys = Vec::new();
4041loop {
42let property_key = reader.read_var_u64()? as u32;
4344if property_key == 0 {
45break;
46 }
4748 property_keys.push(property_key);
49 }
5051let mut property_to_field_index = HashMap::new();
52let mut current_u32 = 0;
53let mut current_bit = 8;
5455for property_key in property_keys {
56if current_bit == 8 {
57 current_u32 = reader.read_u32()?;
58 current_bit = 0;
59 }
6061let field_index = (current_u32 >> current_bit) & 3;
62 property_to_field_index.insert(property_key, field_index);
63 current_bit += 2;
64 }
6566Some(Self { major_version, minor_version, file_id, property_to_field_index })
67 }
6869pub fn major_version(&self) -> u32 {
70self.major_version
71 }
7273pub fn property_file_id(&self, property_key: u32) -> Option<u32> {
74self.property_to_field_index.get(&property_key).cloned()
75 }
76}