pub struct Header { /* private fields */ }Expand description
Wraps an AAHeader handle.
Implementations§
Source§impl Header
impl Header
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Wraps AAHeaderCreate.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
99fn main() -> Result<(), Box<dyn std::error::Error>> {
100 let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
101 let mut stream = ByteStream::custom(SharedByteCallbacks { inner: byte_state })?;
102 stream.write_all(b"hello custom stream")?;
103 stream.seek(0, 0)?;
104 let mut buffer = vec![0_u8; 19];
105 stream.read(&mut buffer)?;
106 assert_eq!(&buffer, b"hello custom stream");
107
108 let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
109 let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
110 inner: archive_state.clone(),
111 })?;
112 let mut header = Header::new()?;
113 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
114 header.append_field_string(FieldKey::PAT, "custom.txt")?;
115 header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
116 header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
117 writer.write_header(&header)?;
118 writer.write_blob(FieldKey::DAT, &buffer)?;
119
120 let mut reader = ArchiveStream::custom(SharedArchiveCallbacks {
121 inner: archive_state,
122 })?;
123 let decoded_header = reader.read_header()?.expect("header");
124 assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
125 let mut decoded = vec![0_u8; buffer.len()];
126 reader.read_blob(FieldKey::DAT, &mut decoded)?;
127 assert_eq!(decoded, buffer);
128
129 println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
130 Ok(())
131}9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn from_encoded_data(data: &[u8]) -> Result<Self>
pub fn from_encoded_data(data: &[u8]) -> Result<Self>
Wraps AAHeaderCreateWithEncodedData.
Examples found in repository?
More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}Sourcepub fn from_path(
key_set: &FieldKeySet,
dir: &str,
path: &str,
flags: ArchiveFlags,
) -> Result<Self>
pub fn from_path( key_set: &FieldKeySet, dir: &str, path: &str, flags: ArchiveFlags, ) -> Result<Self>
Wraps AAHeaderCreateWithPath.
Sourcepub fn field_count(&self) -> u32
pub fn field_count(&self) -> u32
Wraps AAHeaderGetKeyIndex.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}Sourcepub fn field_type(&self, index: u32) -> Result<FieldType>
pub fn field_type(&self, index: u32) -> Result<FieldType>
Wraps AAHeaderGetFieldType.
Sourcepub fn payload_size(&self) -> u64
pub fn payload_size(&self) -> u64
Wraps AAHeaderRemoveField.
Sourcepub fn remove_field(&mut self, index: u32) -> Result<()>
pub fn remove_field(&mut self, index: u32) -> Result<()>
Wraps AAHeaderRemoveField.
Sourcepub fn set_field_flag(&mut self, index: u32, key: FieldKey) -> Result<()>
pub fn set_field_flag(&mut self, index: u32, key: FieldKey) -> Result<()>
Wraps AAHeaderSetFieldFlag.
Sourcepub fn set_field_uint(
&mut self,
index: u32,
key: FieldKey,
value: u64,
) -> Result<()>
pub fn set_field_uint( &mut self, index: u32, key: FieldKey, value: u64, ) -> Result<()>
Wraps AAHeaderSetFieldUInt.
Sourcepub fn set_field_string(
&mut self,
index: u32,
key: FieldKey,
value: &str,
) -> Result<()>
pub fn set_field_string( &mut self, index: u32, key: FieldKey, value: &str, ) -> Result<()>
Wraps AAHeaderSetFieldString.
Sourcepub fn set_field_hash(
&mut self,
index: u32,
key: FieldKey,
function: HashFunction,
value: &[u8],
) -> Result<()>
pub fn set_field_hash( &mut self, index: u32, key: FieldKey, function: HashFunction, value: &[u8], ) -> Result<()>
Wraps AAHeaderSetFieldHash.
Sourcepub fn set_field_timespec(
&mut self,
index: u32,
key: FieldKey,
value: Timespec,
) -> Result<()>
pub fn set_field_timespec( &mut self, index: u32, key: FieldKey, value: Timespec, ) -> Result<()>
Wraps AAHeaderSetFieldTimespec.
Sourcepub fn set_field_blob(
&mut self,
index: u32,
key: FieldKey,
size: u64,
) -> Result<()>
pub fn set_field_blob( &mut self, index: u32, key: FieldKey, size: u64, ) -> Result<()>
Wraps AAHeaderSetFieldBlob.
Sourcepub fn append_field_flag(&mut self, key: FieldKey) -> Result<()>
pub fn append_field_flag(&mut self, key: FieldKey) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Sourcepub fn append_field_uint(&mut self, key: FieldKey, value: u64) -> Result<()>
pub fn append_field_uint(&mut self, key: FieldKey, value: u64) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
99fn main() -> Result<(), Box<dyn std::error::Error>> {
100 let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
101 let mut stream = ByteStream::custom(SharedByteCallbacks { inner: byte_state })?;
102 stream.write_all(b"hello custom stream")?;
103 stream.seek(0, 0)?;
104 let mut buffer = vec![0_u8; 19];
105 stream.read(&mut buffer)?;
106 assert_eq!(&buffer, b"hello custom stream");
107
108 let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
109 let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
110 inner: archive_state.clone(),
111 })?;
112 let mut header = Header::new()?;
113 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
114 header.append_field_string(FieldKey::PAT, "custom.txt")?;
115 header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
116 header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
117 writer.write_header(&header)?;
118 writer.write_blob(FieldKey::DAT, &buffer)?;
119
120 let mut reader = ArchiveStream::custom(SharedArchiveCallbacks {
121 inner: archive_state,
122 })?;
123 let decoded_header = reader.read_header()?.expect("header");
124 assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
125 let mut decoded = vec![0_u8; buffer.len()];
126 reader.read_blob(FieldKey::DAT, &mut decoded)?;
127 assert_eq!(decoded, buffer);
128
129 println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
130 Ok(())
131}9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn append_field_string(&mut self, key: FieldKey, value: &str) -> Result<()>
pub fn append_field_string(&mut self, key: FieldKey, value: &str) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
99fn main() -> Result<(), Box<dyn std::error::Error>> {
100 let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
101 let mut stream = ByteStream::custom(SharedByteCallbacks { inner: byte_state })?;
102 stream.write_all(b"hello custom stream")?;
103 stream.seek(0, 0)?;
104 let mut buffer = vec![0_u8; 19];
105 stream.read(&mut buffer)?;
106 assert_eq!(&buffer, b"hello custom stream");
107
108 let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
109 let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
110 inner: archive_state.clone(),
111 })?;
112 let mut header = Header::new()?;
113 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
114 header.append_field_string(FieldKey::PAT, "custom.txt")?;
115 header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
116 header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
117 writer.write_header(&header)?;
118 writer.write_blob(FieldKey::DAT, &buffer)?;
119
120 let mut reader = ArchiveStream::custom(SharedArchiveCallbacks {
121 inner: archive_state,
122 })?;
123 let decoded_header = reader.read_header()?.expect("header");
124 assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
125 let mut decoded = vec![0_u8; buffer.len()];
126 reader.read_blob(FieldKey::DAT, &mut decoded)?;
127 assert_eq!(decoded, buffer);
128
129 println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
130 Ok(())
131}9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn append_field_hash(
&mut self,
key: FieldKey,
function: HashFunction,
value: &[u8],
) -> Result<()>
pub fn append_field_hash( &mut self, key: FieldKey, function: HashFunction, value: &[u8], ) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}Sourcepub fn append_field_timespec(
&mut self,
key: FieldKey,
value: Timespec,
) -> Result<()>
pub fn append_field_timespec( &mut self, key: FieldKey, value: Timespec, ) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}Sourcepub fn append_field_blob(&mut self, key: FieldKey, size: u64) -> Result<()>
pub fn append_field_blob(&mut self, key: FieldKey, size: u64) -> Result<()>
Wraps AAHeaderGetFieldUInt.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
99fn main() -> Result<(), Box<dyn std::error::Error>> {
100 let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
101 let mut stream = ByteStream::custom(SharedByteCallbacks { inner: byte_state })?;
102 stream.write_all(b"hello custom stream")?;
103 stream.seek(0, 0)?;
104 let mut buffer = vec![0_u8; 19];
105 stream.read(&mut buffer)?;
106 assert_eq!(&buffer, b"hello custom stream");
107
108 let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
109 let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
110 inner: archive_state.clone(),
111 })?;
112 let mut header = Header::new()?;
113 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
114 header.append_field_string(FieldKey::PAT, "custom.txt")?;
115 header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
116 header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
117 writer.write_header(&header)?;
118 writer.write_blob(FieldKey::DAT, &buffer)?;
119
120 let mut reader = ArchiveStream::custom(SharedArchiveCallbacks {
121 inner: archive_state,
122 })?;
123 let decoded_header = reader.read_header()?.expect("header");
124 assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
125 let mut decoded = vec![0_u8; buffer.len()];
126 reader.read_blob(FieldKey::DAT, &mut decoded)?;
127 assert_eq!(decoded, buffer);
128
129 println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
130 Ok(())
131}9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn field_uint(&self, index: u32) -> Result<u64>
pub fn field_uint(&self, index: u32) -> Result<u64>
Wraps AAHeaderGetFieldUInt.
Sourcepub fn field_string(&self, index: u32) -> Result<String>
pub fn field_string(&self, index: u32) -> Result<String>
Wraps AAHeaderGetFieldString.
Sourcepub fn field_hash(&self, index: u32) -> Result<HashValue>
pub fn field_hash(&self, index: u32) -> Result<HashValue>
Wraps AAHeaderGetFieldHash.
Sourcepub fn field_timespec(&self, index: u32) -> Result<Timespec>
pub fn field_timespec(&self, index: u32) -> Result<Timespec>
Wraps AAHeaderGetFieldTimespec.
Sourcepub fn field_blob(&self, index: u32) -> Result<BlobDescription>
pub fn field_blob(&self, index: u32) -> Result<BlobDescription>
Wraps AAHeaderGetFieldBlob.
Sourcepub fn field_value(&self, index: u32) -> Result<HeaderFieldValue>
pub fn field_value(&self, index: u32) -> Result<HeaderFieldValue>
Wraps the field_value convenience for Header.
Sourcepub fn uint_with_key(&self, key: FieldKey) -> Result<Option<u64>>
pub fn uint_with_key(&self, key: FieldKey) -> Result<Option<u64>>
Wraps the uint_with_key convenience for Header.
Sourcepub fn string_with_key(&self, key: FieldKey) -> Result<Option<String>>
pub fn string_with_key(&self, key: FieldKey) -> Result<Option<String>>
Wraps the string_with_key convenience for Header.
Sourcepub fn hash_with_key(&self, key: FieldKey) -> Result<Option<HashValue>>
pub fn hash_with_key(&self, key: FieldKey) -> Result<Option<HashValue>>
Wraps AAHeaderGetEncodedSize.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}Sourcepub fn timespec_with_key(&self, key: FieldKey) -> Result<Option<Timespec>>
pub fn timespec_with_key(&self, key: FieldKey) -> Result<Option<Timespec>>
Wraps AAHeaderGetEncodedSize.
Sourcepub fn blob_with_key(&self, key: FieldKey) -> Result<Option<BlobDescription>>
pub fn blob_with_key(&self, key: FieldKey) -> Result<Option<BlobDescription>>
Wraps AAHeaderGetEncodedSize.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn value_with_key(&self, key: FieldKey) -> Result<Option<HeaderFieldValue>>
pub fn value_with_key(&self, key: FieldKey) -> Result<Option<HeaderFieldValue>>
Wraps AAHeaderGetEncodedData.
Sourcepub fn entry_type(&self) -> Result<Option<EntryType>>
pub fn entry_type(&self) -> Result<Option<EntryType>>
Wraps AAHeaderGetEncodedData.
Sourcepub fn path(&self) -> Result<Option<String>>
pub fn path(&self) -> Result<Option<String>>
Wraps AAHeaderGetEncodedData.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}More examples
99fn main() -> Result<(), Box<dyn std::error::Error>> {
100 let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
101 let mut stream = ByteStream::custom(SharedByteCallbacks { inner: byte_state })?;
102 stream.write_all(b"hello custom stream")?;
103 stream.seek(0, 0)?;
104 let mut buffer = vec![0_u8; 19];
105 stream.read(&mut buffer)?;
106 assert_eq!(&buffer, b"hello custom stream");
107
108 let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
109 let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
110 inner: archive_state.clone(),
111 })?;
112 let mut header = Header::new()?;
113 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
114 header.append_field_string(FieldKey::PAT, "custom.txt")?;
115 header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
116 header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
117 writer.write_header(&header)?;
118 writer.write_blob(FieldKey::DAT, &buffer)?;
119
120 let mut reader = ArchiveStream::custom(SharedArchiveCallbacks {
121 inner: archive_state,
122 })?;
123 let decoded_header = reader.read_header()?.expect("header");
124 assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
125 let mut decoded = vec![0_u8; buffer.len()];
126 reader.read_blob(FieldKey::DAT, &mut decoded)?;
127 assert_eq!(decoded, buffer);
128
129 println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
130 Ok(())
131}9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let data = b"hello from apple archive".to_vec();
11 let artifact_dir = artifact_dir("aa-archive-stream");
12 let archive_path = artifact_dir.join("sample.aar");
13 let archive_path = path_string(&archive_path);
14
15 let mut header = Header::new()?;
16 let regular_file = u64::from(b'F');
17 let data_len = u64::try_from(data.len())?;
18 header.append_field_uint(FieldKey::TYP, regular_file)?;
19 header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20 header.append_field_uint(FieldKey::SIZ, data_len)?;
21 header.append_field_blob(FieldKey::DAT, data_len)?;
22
23 let byte_stream = ByteStream::open_with_path(
24 &archive_path,
25 OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26 DEFAULT_FILE_MODE,
27 )?;
28 let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29 archive.write_header(&header)?;
30 archive.write_blob(FieldKey::DAT, &data)?;
31 archive.close()?;
32
33 let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35 let header = archive.read_header()?.expect("archive entry");
36 assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37 let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38 let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39 archive.read_blob(FieldKey::DAT, &mut decoded)?;
40 assert_eq!(decoded, data);
41 assert!(archive.read_header()?.is_none());
42 archive.close()?;
43
44 println!("archive path={archive_path}");
45 println!("✅ AppleArchive round-trip OK");
46 Ok(())
47}Sourcepub fn encoded_size(&self) -> usize
pub fn encoded_size(&self) -> usize
Wraps AAHeaderGetEncodedData.
Sourcepub fn encoded_data(&self) -> Result<Vec<u8>>
pub fn encoded_data(&self) -> Result<Vec<u8>>
Wraps AAHeaderGetEncodedData.
Examples found in repository?
More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let mut header = Header::new()?;
5 header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6 header.append_field_string(FieldKey::PAT, "notes.txt")?;
7 header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8 header.append_field_timespec(
9 FieldKey::MTM,
10 Timespec {
11 seconds: 1_234,
12 nanoseconds: 56,
13 },
14 )?;
15 header.append_field_blob(FieldKey::DAT, 512)?;
16
17 let encoded = header.encoded_data()?;
18 let decoded = Header::from_encoded_data(&encoded)?;
19
20 assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21 assert_eq!(
22 decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23 vec![7_u8; 32]
24 );
25 assert_eq!(
26 decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27 BlobDescription {
28 size: 512,
29 offset: 0
30 }
31 );
32 let field_count = decoded.field_count();
33 println!("fields={field_count}");
34 println!("✅ AppleArchive header encode/decode OK");
35 Ok(())
36}