hpkg 1.0.0

A native Rust crate to parse Haiku's binary package and repo formats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
use std::error;
use std::fmt;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};

use flate2::read::ZlibDecoder;
use zstd;

use crate::hpkg_common::*;

#[derive(Debug, Clone)]
#[repr(C)]
pub struct RepositoryHeaderV2 {
	pub magic: u32,
	pub header_size: u16,
	pub version: u16,
	pub total_size: u64,
	pub minor_version: u16,

	// Heap
	pub heap_compression: u16,
	pub heap_chunk_size: u32,
	pub heap_size_compressed: u64,
	pub heap_size_uncompressed: u64,

	// Repository info section
	pub info_length: u32,
	pub reserved1: u32,

	// Package attributes section
	pub package_length: u64,
	pub package_strings_length: u64,
	pub package_strings_count: u64,
}

/// Parsed repository metadata from the repository info section.
#[derive(Debug, Clone)]
pub struct RepositoryInfo {
	pub name: Option<String>,
	pub identifier: Option<String>,
	pub base_url: Option<String>,
	pub vendor: Option<String>,
	pub summary: Option<String>,
	pub priority: Option<u8>,
	pub architecture: Option<String>,
	pub license_names: Vec<String>,
	pub license_texts: Vec<String>,
}

/// Lightweight metadata for a package listed in a repository.
#[derive(Debug, Clone)]
pub struct PackageInfo {
	pub name: Option<String>,
	pub summary: Option<String>,
	pub description: Option<String>,
	pub vendor: Option<String>,
	pub packager: Option<String>,
	pub flags: u32,
	pub architecture: Option<String>,
	pub checksum: Option<String>,
	pub url: Option<String>,
	pub source_url: Option<String>,
	pub install_path: Option<String>,
	pub version_major: Option<String>,
	pub version_minor: Option<String>,
	pub version_micro: Option<String>,
	pub version_revision: Option<u64>,
	pub copyrights: Vec<String>,
	pub licenses: Vec<String>,
	pub provides: Vec<String>,
	pub requires: Vec<String>,
}

/// Representation of an HPKR repository cache file.
pub struct Repository {
	pub filename: Option<PathBuf>,
	pub header: Option<RepositoryHeaderV2>,
	pub info: RepositoryInfo,
	pub packages: Vec<PackageInfo>,

	heap_chunk_offsets: Vec<u64>,
	flattened_heap: Vec<u8>,
}

// ---------------------------------------------------------------------------
// RepositoryInfo
// ---------------------------------------------------------------------------

impl RepositoryInfo {
	pub fn new() -> RepositoryInfo {
		RepositoryInfo {
			name: None,
			identifier: None,
			base_url: None,
			vendor: None,
			summary: None,
			priority: None,
			architecture: None,
			license_names: Vec::new(),
			license_texts: Vec::new(),
		}
	}
}

// ---------------------------------------------------------------------------
// PackageInfo
// ---------------------------------------------------------------------------

impl PackageInfo {
	pub fn new() -> PackageInfo {
		PackageInfo {
			name: None,
			summary: None,
			description: None,
			vendor: None,
			packager: None,
			flags: 0,
			architecture: None,
			checksum: None,
			url: None,
			source_url: None,
			install_path: None,
			version_major: None,
			version_minor: None,
			version_micro: None,
			version_revision: None,
			copyrights: Vec::new(),
			licenses: Vec::new(),
			provides: Vec::new(),
			requires: Vec::new(),
		}
	}
}

// ---------------------------------------------------------------------------
// Repository header parsing
// ---------------------------------------------------------------------------

fn parse_header<P: AsRef<Path>>(repo_file: P) -> Result<RepositoryHeaderV2, Box<dyn error::Error>> {
	let mut f = File::open(repo_file.as_ref())?;
	f.seek(SeekFrom::Start(0))?;
	let reader = BufReader::new(f);

	let mut header = read_struct::<RepositoryHeaderV2, _>(reader)?;
	let magic_bytes = header.magic.to_ne_bytes();
	if magic_bytes != [b'h', b'p', b'k', b'r'] {
		return Err(From::from(format!("Unknown magic: {:?}", magic_bytes)));
	}

	header.header_size = u16::from_be(header.header_size);
	header.version = u16::from_be(header.version);
	header.total_size = u64::from_be(header.total_size);
	header.minor_version = u16::from_be(header.minor_version);
	header.heap_compression = u16::from_be(header.heap_compression);
	header.heap_chunk_size = u32::from_be(header.heap_chunk_size);
	header.heap_size_compressed = u64::from_be(header.heap_size_compressed);
	header.heap_size_uncompressed = u64::from_be(header.heap_size_uncompressed);
	header.info_length = u32::from_be(header.info_length);
	header.reserved1 = u32::from_be(header.reserved1);
	header.package_length = u64::from_be(header.package_length);
	header.package_strings_length = u64::from_be(header.package_strings_length);
	header.package_strings_count = u64::from_be(header.package_strings_count);

	if header.version != 2 {
		return Err(From::from(format!("Unknown repo version: {}", header.version)));
	}

	if header.header_size as u64 + header.heap_size_compressed != header.total_size {
		return Err(From::from(format!("Invalid repo header lengths")));
	}

	Ok(header)
}

// ---------------------------------------------------------------------------
// Heap parsing (mirrors Package API)
// ---------------------------------------------------------------------------

impl Repository {
	fn heap_chunk_count(&self) -> Result<u64, Box<dyn error::Error>> {
		let header = self.header.as_ref().ok_or("No header loaded")?;
		let chunk_size = header.heap_chunk_size as u64;
		Ok((header.heap_size_uncompressed + chunk_size - 1) / chunk_size)
	}

	fn heap_chunkify(&mut self) -> Result<u64, Box<dyn error::Error>> {
		let chunks = self.heap_chunk_count()?;

		let (heap_compression, heap_chunk_size, header_size, heap_size_compressed) = {
			let h = self.header.as_ref().unwrap();
			(h.heap_compression, h.heap_chunk_size, h.header_size, h.heap_size_compressed)
		};

		self.heap_chunk_offsets.push(0);

		if heap_compression == 0 {
			for i in 1..chunks {
				self.heap_chunk_offsets.push(i as u64 * heap_chunk_size as u64);
			}
		} else {
			let filename = self.filename.as_ref().unwrap().clone();

			let chunk_size_table_len = (chunks - 1) * 2;
			if heap_size_compressed <= chunk_size_table_len {
				return Err(From::from(format!(
					"Compressed heap smaller than chunk size table"
				)));
			}
			let table_start = header_size as u64 + heap_size_compressed - chunk_size_table_len;

			let mut f = File::open(&filename)?;
			f.seek(SeekFrom::Start(table_start))?;
			let mut chunkbuffer = vec![0; chunk_size_table_len as usize];
			BufReader::new(&f).read_exact(&mut chunkbuffer)?;
			for chunk_index in 0..chunkbuffer.len() / 2 {
				let base = chunk_index * 2;
				let mut raw_cookies: u64 = ((chunkbuffer[base] as u64) << 8)
					| chunkbuffer[base + 1] as u64;
				raw_cookies += self.heap_chunk_offsets.last().unwrap() + 1;
				self.heap_chunk_offsets.push(raw_cookies);
			}
		}

		Ok(0)
	}

}

// ---------------------------------------------------------------------------
// String table and attribute value reading (mirrors Package API)
// ---------------------------------------------------------------------------

impl Repository {
	fn read_string_from(&self, offset: &mut usize) -> Result<&str, Box<dyn error::Error>> {
		let data = &self.flattened_heap;
		let start = *offset;
		while *offset < data.len() && data[*offset] != 0 {
			*offset += 1;
		}
		if *offset >= data.len() {
			return Err(From::from(
				"Unexpected end of heap data in string table".to_string(),
			));
		}
		let s = std::str::from_utf8(&data[start..*offset])?;
		*offset += 1;
		Ok(s)
	}

	fn parse_string_table(&self, offset: usize, count: u64) -> Result<Vec<String>, Box<dyn error::Error>> {
		let mut pos = offset;
		let mut table = Vec::with_capacity(count as usize);
		for _ in 0..count {
			let s = self.read_string_from(&mut pos)?.to_string();
			table.push(s);
		}
		Ok(table)
	}

	fn read_attr_value(
		&self,
		offset: &mut usize,
		type_: u16,
		encoding: u16,
		string_table: &[String],
	) -> Result<AttrValue, Box<dyn error::Error>> {
		match type_ {
			HPKG_ATTR_TYPE_INT | HPKG_ATTR_TYPE_UINT => {
				let v: u64 = match encoding {
					0 => {
						let b = self.flattened_heap[*offset];
						*offset += 1;
						b as u64
					}
					1 => {
						let v = u16::from_be_bytes(
							self.flattened_heap[*offset..*offset + 2].try_into().unwrap(),
						);
						*offset += 2;
						v as u64
					}
					2 => {
						let v = u32::from_be_bytes(
							self.flattened_heap[*offset..*offset + 4].try_into().unwrap(),
						);
						*offset += 4;
						v as u64
					}
					3 => {
						let v = u64::from_be_bytes(
							self.flattened_heap[*offset..*offset + 8].try_into().unwrap(),
						);
						*offset += 8;
						v
					}
					_ => return Err(From::from(format!("Unknown int encoding {}", encoding))),
				};
				if type_ == HPKG_ATTR_TYPE_INT {
					Ok(AttrValue::Int(v as i64))
				} else {
					Ok(AttrValue::Uint(v))
				}
			}

			HPKG_ATTR_TYPE_STRING => {
				let s = if encoding == 0 {
					self.read_string_from(offset)?.to_string()
				} else {
					let idx = read_unsigned_leb128(&self.flattened_heap, offset)? as usize;
					if idx >= string_table.len() {
						return Err(From::from(format!(
							"String table index {} out of bounds",
							idx
						)));
					}
					string_table[idx].clone()
				};
				Ok(AttrValue::String(s))
			}

			HPKG_ATTR_TYPE_RAW => {
				let size = read_unsigned_leb128(&self.flattened_heap, offset)? as usize;
				if encoding == 0 {
					let data = self.flattened_heap[*offset..*offset + size].to_vec();
					*offset += size;
					Ok(AttrValue::Raw(data))
				} else {
					let heap_offset =
						read_unsigned_leb128(&self.flattened_heap, offset)? as usize;
					let data =
						self.flattened_heap[heap_offset..heap_offset + size].to_vec();
					Ok(AttrValue::Raw(data))
				}
			}

			_ => Err(From::from(format!("Unknown attribute type {}", type_))),
		}
	}

	fn skip_attribute_value(&self, offset: &mut usize, type_: u16, encoding: u16) {
		match type_ {
			HPKG_ATTR_TYPE_INT | HPKG_ATTR_TYPE_UINT => match encoding {
				0 => *offset += 1,
				1 => *offset += 2,
				2 => *offset += 4,
				3 => *offset += 8,
				_ => {}
			},
			HPKG_ATTR_TYPE_STRING => {
				if encoding == 0 {
					while *offset < self.flattened_heap.len()
						&& self.flattened_heap[*offset] != 0
					{
						*offset += 1;
					}
					if *offset < self.flattened_heap.len() {
						*offset += 1;
					}
				} else {
					let _ = read_unsigned_leb128(&self.flattened_heap, offset);
				}
			}
			HPKG_ATTR_TYPE_RAW => {
				if let Ok(size) = read_unsigned_leb128(&self.flattened_heap, offset) {
					if encoding == 0 {
						*offset += size as usize;
					} else {
						let _ = read_unsigned_leb128(&self.flattened_heap, offset);
					}
				}
			}
			_ => {}
		}
	}

	fn skip_attribute_tree(&self, offset: &mut usize) {
		let mut depth = 0usize;
		loop {
			if *offset >= self.flattened_heap.len() {
				return;
			}
			let Ok(tag_raw) = read_unsigned_leb128(&self.flattened_heap, offset) else {
				return;
			};
			if tag_raw == 0 {
				if depth == 0 {
					return;
				}
				depth -= 1;
				continue;
			}
			let (_id, type_, encoding, has_children) = decode_attribute_tag(tag_raw);
			self.skip_attribute_value(offset, type_, encoding);
			if has_children {
				depth += 1;
			}
		}
	}
}

// ---------------------------------------------------------------------------
// BMessage minimal parser for Repository Info section
// ---------------------------------------------------------------------------

impl Repository {
	fn parse_repository_info_section(&mut self) -> Result<(), Box<dyn error::Error>> {
		let length = match &self.header {
			Some(h) => h.info_length as usize,
			None => return Err(From::from("No header loaded")),
		};

		if length == 0 {
			return Ok(());
		}

		let data = &self.flattened_heap[..length];

		// Scan for known field names in the BMessage-flattened data.
		// The Haiku BMessage format uses a complex binary encoding with
		// type_code (4 bytes), name_length (1 byte), count (4 bytes),
		// followed by the field name and then the data.
		//
		// We search for the field name strings and extract values based
		// on expected type.

		let fields: &[(&str, &str)] = &[
			("name", "string"),
			("identifier", "string"),
			("baseurl", "string"),
			("vendor", "string"),
			("summary", "string"),
			("licenseName", "string"),
			("licenseText", "string"),
			("priority", "int"),
			("architecture", "int"),
		];

		for &(field_name, field_type) in fields {
			// Search for the field name (null-terminated) in the data
			let name_bytes = field_name.as_bytes();
			let mut search_pos = 0;
			while search_pos + name_bytes.len() + 1 < data.len() {
				if data[search_pos..search_pos + name_bytes.len()] == *name_bytes
					&& data[search_pos + name_bytes.len()] == 0
				{
					// Found the field name. The value follows.
					let value_pos = search_pos + name_bytes.len() + 1;

					match field_type {
						"string" => {
							if value_pos + 4 <= data.len() {
								let str_len = u32::from_le_bytes(
									data[value_pos..value_pos + 4].try_into().unwrap(),
								) as usize;
								if value_pos + 4 + str_len <= data.len() && str_len > 0 {
									let s = String::from_utf8_lossy(
										&data[value_pos + 4..value_pos + 4 + str_len - 1],
									)
									.to_string();
									match field_name {
										"name" => self.info.name = Some(s),
										"identifier" => self.info.identifier = Some(s),
										"baseurl" => self.info.base_url = Some(s),
										"vendor" => self.info.vendor = Some(s),
										"summary" => self.info.summary = Some(s),
										"licenseName" => self.info.license_names.push(s),
										"licenseText" => self.info.license_texts.push(s),
										_ => {}
									}
								}
							}
						}
						"int" => {
							if value_pos + 1 <= data.len() {
								let v = data[value_pos] as u8;
								match field_name {
									"priority" => self.info.priority = Some(v),
									"architecture" => {
										self.info.architecture = Some(arch_to_string(v as u64))
									}
									_ => {}
								}
							}
						}
						_ => {}
					}
					break;
				}
				search_pos += 1;
			}
		}

		Ok(())
	}
}

// ---------------------------------------------------------------------------
// Package attributes parsing (list of packages from the repo)
// ---------------------------------------------------------------------------

impl Repository {
	fn parse_packages_section(&mut self) -> Result<(), Box<dyn error::Error>> {
		let header = match &self.header {
			Some(h) => h.clone(),
			None => return Err(From::from("No header loaded")),
		};

		if header.package_length == 0 {
			return Ok(());
		}

		let heap_size = header.heap_size_uncompressed as usize;
		let info_len = header.info_length as usize;

		if info_len + header.package_length as usize > heap_size {
			return Err(From::from(format!(
				"Package attributes section ({} @ {}) exceeds heap size ({})",
				header.package_length, info_len, heap_size
			)));
		}

		// Strings subsection at the start of the package attributes section
		let strings_len = header.package_strings_length as usize;
		let strings_count = header.package_strings_count;

		let section_start = info_len;
		let strings_offset = section_start;
		let main_offset = section_start + strings_len;

		let string_table = if strings_count > 0 {
			self.parse_string_table(strings_offset, strings_count)?
		} else {
			Vec::new()
		};

		// Parse the attribute tree — top-level entries are ATTR_PACKAGE
		let mut pos = main_offset;
		let end = section_start + header.package_length as usize;
		self.parse_package_list(&mut pos, &string_table, end)?;

		Ok(())
	}

	fn parse_package_list(
		&mut self,
		offset: &mut usize,
		string_table: &[String],
		end_bound: usize,
	) -> Result<(), Box<dyn error::Error>> {
		loop {
			if *offset >= end_bound || *offset >= self.flattened_heap.len() {
				return Ok(());
			}

			let tag_raw = read_unsigned_leb128(&self.flattened_heap, offset)?;
			if tag_raw == 0 {
				// End marker (shouldn't happen at top level, but handle gracefully)
				return Ok(());
			}

			let (id, type_, encoding, has_children) = decode_attribute_tag(tag_raw);

			if id != 54 {
				// ATTR_PACKAGE = 54 — skip any non-package top-level attributes
				self.read_attr_value(offset, type_, encoding, string_table)?;
				if has_children {
					self.skip_attribute_tree(offset);
				}
				continue;
			}

			// Read the package name
			let pkg_name = match self.read_attr_value(offset, type_, encoding, string_table)? {
				AttrValue::String(s) => s,
				_ => String::new(),
			};

			let mut pkg = PackageInfo::new();
			pkg.name = Some(pkg_name);

			if has_children {
				self.parse_package_attributes(offset, string_table, &mut pkg)?;
			}

			self.packages.push(pkg);
		}
	}

	fn parse_package_attributes(
		&self,
		offset: &mut usize,
		string_table: &[String],
		pkg: &mut PackageInfo,
	) -> Result<(), Box<dyn error::Error>> {
		loop {
			if *offset >= self.flattened_heap.len() {
				return Ok(());
			}

			let tag_raw = read_unsigned_leb128(&self.flattened_heap, offset)?;
			if tag_raw == 0 {
				return Ok(());
			}

			let (id, type_, encoding, has_children) = decode_attribute_tag(tag_raw);
			let value = self.read_attr_value(offset, type_, encoding, string_table)?;

			match id {
				15 => {
					// PACKAGE_NAME
					if let AttrValue::String(s) = &value {
						pkg.name = Some(s.clone());
					}
				}
				16 => {
					// PACKAGE_SUMMARY
					if let AttrValue::String(s) = &value {
						pkg.summary = Some(s.clone());
					}
				}
				17 => {
					// PACKAGE_DESCRIPTION
					if let AttrValue::String(s) = &value {
						pkg.description = Some(s.clone());
					}
				}
				18 => {
					// PACKAGE_VENDOR
					if let AttrValue::String(s) = &value {
						pkg.vendor = Some(s.clone());
					}
				}
				19 => {
					// PACKAGE_PACKAGER
					if let AttrValue::String(s) = &value {
						pkg.packager = Some(s.clone());
					}
				}
				20 => {
					// PACKAGE_FLAGS
					match &value {
						AttrValue::Uint(v) => pkg.flags = *v as u32,
						AttrValue::Int(v) => pkg.flags = *v as u32,
						_ => {}
					}
				}
				21 => {
					// PACKAGE_ARCHITECTURE
					match &value {
						AttrValue::Uint(v) => pkg.architecture = Some(arch_to_string(*v)),
						AttrValue::Int(v) => pkg.architecture = Some(arch_to_string(*v as u64)),
						_ => {}
					}
				}
				22 => {
					// PACKAGE_VERSION_MAJOR
					if let AttrValue::String(s) = &value {
						pkg.version_major = Some(s.clone());
					}
				}
				23 => {
					// PACKAGE_VERSION_MINOR
					if let AttrValue::String(s) = &value {
						pkg.version_minor = Some(s.clone());
					}
				}
				24 => {
					// PACKAGE_VERSION_MICRO
					if let AttrValue::String(s) = &value {
						pkg.version_micro = Some(s.clone());
					}
				}
				25 => {
					// PACKAGE_VERSION_REVISION
					match &value {
						AttrValue::Uint(v) => pkg.version_revision = Some(*v),
						AttrValue::Int(v) => pkg.version_revision = Some(*v as u64),
						_ => {}
					}
				}
				26 => {
					// PACKAGE_COPYRIGHT
					if let AttrValue::String(s) = &value {
						pkg.copyrights.push(s.clone());
					}
				}
				27 => {
					// PACKAGE_LICENSE
					if let AttrValue::String(s) = &value {
						pkg.licenses.push(s.clone());
					}
				}
				28 => {
					// PACKAGE_PROVIDES
					if let AttrValue::String(s) = &value {
						pkg.provides.push(s.clone());
					}
				}
				29 => {
					// PACKAGE_REQUIRES
					if let AttrValue::String(s) = &value {
						pkg.requires.push(s.clone());
					}
				}
				35 => {
					// PACKAGE_CHECKSUM
					if let AttrValue::String(s) = &value {
						pkg.checksum = Some(s.clone());
					}
				}
				38 => {
					// PACKAGE_URL
					if let AttrValue::String(s) = &value {
						pkg.url = Some(s.clone());
					}
				}
				39 => {
					// PACKAGE_SOURCE_URL
					if let AttrValue::String(s) = &value {
						pkg.source_url = Some(s.clone());
					}
				}
				40 => {
					// PACKAGE_INSTALL_PATH
					if let AttrValue::String(s) = &value {
						pkg.install_path = Some(s.clone());
					}
				}
				_ => {}
			}

			if has_children {
				self.skip_attribute_tree(offset);
			}
		}
	}
}

// ---------------------------------------------------------------------------
// Display / Debug
// ---------------------------------------------------------------------------

impl fmt::Display for Repository {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		let name = self.info.name.as_deref().unwrap_or("(unknown)");
		let vendor = self.info.vendor.as_deref().unwrap_or("(unknown)");
		let summary = self.info.summary.as_deref().unwrap_or("(unknown)");
		let arch = self.info.architecture.as_deref().unwrap_or("(unknown)");
		write!(
			f,
			"repository. Name {:?}, Vendor {:?}, Summary {:?}, Arch {:?}",
			name, vendor, summary, arch
		)
	}
}

impl fmt::Debug for Repository {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		writeln!(f, "Haiku Repository")?;
		if let Some(ref h) = self.header {
			writeln!(f, "  header: {} v{}.{}", h.total_size, h.version, h.minor_version)?;
			writeln!(f, "  compression: {}", h.heap_compression)?;
			writeln!(f, "  heap: {} -> {} uncompressed",
				h.heap_size_compressed, h.heap_size_uncompressed)?;
			writeln!(f, "  info_length: {}", h.info_length)?;
			writeln!(f, "  packages: {} entries ({} strings)",
				h.package_length, h.package_strings_count)?;
		}
		writeln!(f, "  name: {:?}", self.info.name)?;
		writeln!(f, "  vendor: {:?}", self.info.vendor)?;
		writeln!(f, "  summary: {:?}", self.info.summary)?;
		writeln!(f, "  architecture: {:?}", self.info.architecture)?;
		writeln!(f, "  packages count: {}", self.packages.len())?;
		for pkg in self.packages.iter().take(5) {
			writeln!(f, "    - {}", pkg.name.as_deref().unwrap_or("(unnamed)"))?;
		}
		if self.packages.len() > 5 {
			writeln!(f, "    ... and {} more", self.packages.len() - 5)?;
		}
		Ok(())
	}
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

impl Repository {
	pub fn new() -> Repository {
		Repository {
			filename: None,
			header: None,
			info: RepositoryInfo::new(),
			packages: Vec::new(),
			heap_chunk_offsets: Vec::new(),
			flattened_heap: Vec::new(),
		}
	}

	pub fn load<P: AsRef<Path>>(repo_file: P) -> Result<Repository, Box<dyn error::Error>> {
		let mut repo = Repository::new();
		repo.filename = Some(repo_file.as_ref().to_path_buf());

		let header = self::parse_header(repo_file)?;

		// Validate that header + heap match the total file size
		if header.header_size as u64 + header.heap_size_compressed != header.total_size {
			return Err(From::from(format!("Invalid repo file: header + heap != total_size")));
		}

		repo.header = Some(header);
		repo.heap_chunkify()?;

		// Inflate the heap — read compressed chunks from the file and decompress
		{
			let chunks = repo.heap_chunk_count()?;
			let filename = repo.filename.as_ref().unwrap().clone();
			let header = repo.header.as_ref().unwrap();

			let heap_size = header.heap_size_uncompressed as usize;
			let mut flat = vec![0u8; heap_size];

			let mut dest_offset = 0usize;
			let chunk_count = chunks as usize;
			for chunk_index in 0..chunk_count {
				let chunk_offset = header.header_size as usize
					+ if header.heap_compression == 0 {
						(chunk_index as u64 * header.heap_chunk_size as u64) as usize
					} else {
						repo.heap_chunk_offsets[chunk_index] as usize
					};
				let remaining = heap_size - dest_offset;
				let chunk_len = remaining.min(header.heap_chunk_size as usize);

				let mut f = File::open(&filename)?;
				f.seek(SeekFrom::Start(chunk_offset as u64))?;

				if header.heap_compression == 0 {
					f.read_exact(&mut flat[dest_offset..dest_offset + chunk_len])?;
					dest_offset += chunk_len;
				} else {
					// Read compressed chunk size
					let compressed_size: usize = if chunk_index + 1 < chunk_count {
						(repo.heap_chunk_offsets[chunk_index + 1]
							- repo.heap_chunk_offsets[chunk_index]) as usize
					} else {
						let chunk_table_len = (chunk_count as u64 - 1) * 2;
						let total_compressed = header.heap_size_compressed - chunk_table_len;
						(total_compressed
							- repo.heap_chunk_offsets[chunk_index]) as usize
					};

					let mut compressed = vec![0u8; compressed_size];
					f.read_exact(&mut compressed)?;

					if compressed_size < chunk_len {
						let mut reader: Box<dyn Read> = match header.heap_compression {
							B_HPKG_COMPRESSION_ZLIB => {
								Box::new(ZlibDecoder::new(&compressed[..]))
							}
							B_HPKG_COMPRESSION_ZSTD => Box::new(
								zstd::stream::read::Decoder::new(&compressed[..])?,
							),
							_ => {
								return Err(From::from(format!(
									"Unknown repo heap compression: {}",
									header.heap_compression
								)))
							}
						};
						reader.read_exact(&mut flat[dest_offset..dest_offset + chunk_len])?;
					} else {
						flat[dest_offset..dest_offset + chunk_len]
							.copy_from_slice(&compressed[..chunk_len]);
					}
					dest_offset += chunk_len;
				}
			}

			repo.flattened_heap = flat;
		}

		// Parse repository info section (at start of flattened heap)
		repo.parse_repository_info_section()?;

		// Parse package attributes section
		repo.parse_packages_section()?;

		Ok(repo)
	}

	pub fn repository_info(&self) -> &RepositoryInfo {
		&self.info
	}

	pub fn packages(&self) -> &[PackageInfo] {
		&self.packages
	}
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_new_repository() {
		let repo = Repository::new();
		assert!(repo.header.is_none());
		assert!(repo.info.name.is_none());
	}

	#[test]
	fn test_load_valid_repository() {
		let repo = match Repository::load("sample/repo") {
			Ok(o) => o,
			Err(e) => {
				println!("ERROR: {}", e);
				assert!(false);
				return;
			}
		};
		assert!(repo.header.is_some());
	}

	#[test]
	fn test_load_invalid_repository() {
		assert!(Repository::load("sample/not-repo").is_err());
	}

	#[test]
	fn test_total_size() {
		let metadata = match std::fs::metadata("sample/repo") {
			Ok(o) => o,
			Err(e) => {
				println!("ERROR: {}", e);
				assert!(false);
				return;
			}
		};
		let hpkr = match Repository::load("sample/repo") {
			Ok(o) => o,
			Err(e) => {
				println!("ERROR: {}", e);
				assert!(false);
				return;
			}
		};
		let header = match hpkr.header {
			Some(o) => o,
			None => {
				println!("ERROR: Invalid Header!");
				assert!(false);
				return;
			}
		};
		assert_eq!(metadata.len(), header.total_size);
	}

	#[test]
	fn test_repository_info() {
		let repo = match Repository::load("sample/repo") {
			Ok(o) => o,
			Err(e) => {
				println!("ERROR: {}", e);
				assert!(false);
				return;
			}
		};
		println!("Repository Info: {:?}", repo.info);
		println!("{:?}", repo);
		assert!(
			repo.info.name.is_some() || repo.info.vendor.is_some(),
			"Expected at least some repository metadata, got {:?}",
			repo.info
		);
	}

	#[test]
	fn test_repository_packages() {
		let repo = match Repository::load("sample/repo") {
			Ok(o) => o,
			Err(e) => {
				println!("ERROR: {}", e);
				assert!(false);
				return;
			}
		};
		println!("Found {} packages in repository", repo.packages.len());
		for pkg in &repo.packages {
			println!(
				"  {} (vendor={:?}, arch={:?})",
				pkg.name.as_deref().unwrap_or("(unnamed)"),
				pkg.vendor,
				pkg.architecture
			);
		}
	}
}