blob: 9c97411eb5c30b5338801b8bf6e7387a8e844a0a [file] [log] [blame]
Jooyung Hanf1e00862021-06-25 12:02:33 +09001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Read/write metadata blob for VM payload image. The blob is supposed to be used as a metadata
16//! partition in the VM payload image.
17//! The layout of metadata blob is like:
18//! 4 bytes : size(N) in big endian
19//! N bytes : protobuf message for Metadata
20
21use protobuf::Message;
22use std::io;
23use std::io::Read;
24use std::io::Write;
25
26pub use microdroid_metadata::metadata::{ApexPayload, ApkPayload, Metadata};
27
28/// Reads a metadata from a reader
29pub fn read_metadata<T: Read>(mut r: T) -> io::Result<Metadata> {
30 let mut buf = [0u8; 4];
31 r.read_exact(&mut buf)?;
32 let size = i32::from_be_bytes(buf);
33 Ok(Metadata::parse_from_reader(&mut r.take(size as u64))?)
34}
35
36/// Writes a metadata to a writer
37pub fn write_metadata<T: Write>(metadata: &Metadata, mut w: T) -> io::Result<()> {
38 let mut buf = Vec::new();
39 metadata.write_to_writer(&mut buf)?;
40 w.write_all(&(buf.len() as i32).to_be_bytes())?;
41 w.write_all(&buf)
42}