1use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
22use std::io::{Read, Write};
23
24use super::canon::{CLASSIFICATION_MASK, ChunkClassification};
25use super::error::{FafbError, FafbResult};
26use super::priority::Priority;
27
28pub const SECTION_ENTRY_SIZE: usize = 16;
30
31#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct SectionEntry {
34 pub name_index: u8,
36 pub priority: Priority,
38 pub offset: u32,
40 pub length: u32,
42 pub token_count: u16,
44 pub flags: u32,
46}
47
48impl SectionEntry {
49 pub fn new(name_index: u8, offset: u32, length: u32) -> Self {
52 Self {
53 name_index,
54 priority: Priority::medium(),
55 offset,
56 length,
57 token_count: estimate_tokens(length),
58 flags: 0,
59 }
60 }
61
62 pub fn with_priority(mut self, priority: Priority) -> Self {
64 self.priority = priority;
65 self
66 }
67
68 pub fn with_token_count(mut self, count: u16) -> Self {
70 self.token_count = count;
71 self
72 }
73
74 pub fn with_flags(mut self, flags: u32) -> Self {
76 self.flags = flags;
77 self
78 }
79
80 pub fn with_classification(mut self, classification: ChunkClassification) -> Self {
82 self.flags = (self.flags & !CLASSIFICATION_MASK) | classification.bits();
84 self
85 }
86
87 pub fn classification(&self) -> ChunkClassification {
89 ChunkClassification::from_bits(self.flags)
90 }
91
92 pub fn section_flags(&self) -> u32 {
94 self.flags & !CLASSIFICATION_MASK
95 }
96
97 pub fn write<W: Write>(&self, writer: &mut W) -> FafbResult<()> {
99 writer.write_u8(self.name_index)?;
100 writer.write_u8(self.priority.value())?;
101 writer.write_u32::<LittleEndian>(self.offset)?;
102 writer.write_u32::<LittleEndian>(self.length)?;
103 writer.write_u16::<LittleEndian>(self.token_count)?;
104 writer.write_u32::<LittleEndian>(self.flags)?;
105 Ok(())
106 }
107
108 pub fn to_bytes(&self) -> FafbResult<Vec<u8>> {
110 let mut buf = Vec::with_capacity(SECTION_ENTRY_SIZE);
111 self.write(&mut buf)?;
112 Ok(buf)
113 }
114
115 pub fn read<R: Read>(reader: &mut R) -> FafbResult<Self> {
117 let name_index = reader.read_u8()?;
118 let priority = Priority::from(reader.read_u8()?);
119 let offset = reader.read_u32::<LittleEndian>()?;
120 let length = reader.read_u32::<LittleEndian>()?;
121 let token_count = reader.read_u16::<LittleEndian>()?;
122 let flags = reader.read_u32::<LittleEndian>()?;
123
124 Ok(Self {
125 name_index,
126 priority,
127 offset,
128 length,
129 token_count,
130 flags,
131 })
132 }
133
134 pub fn from_bytes(data: &[u8]) -> FafbResult<Self> {
136 if data.len() < SECTION_ENTRY_SIZE {
137 return Err(FafbError::FileTooSmall {
138 expected: SECTION_ENTRY_SIZE,
139 actual: data.len(),
140 });
141 }
142 let mut cursor = std::io::Cursor::new(data);
143 Self::read(&mut cursor)
144 }
145
146 pub fn validate_bounds(&self, file_size: u32) -> FafbResult<()> {
148 let end =
152 self.offset
153 .checked_add(self.length)
154 .ok_or(FafbError::InvalidSectionTableOffset {
155 offset: self.offset,
156 file_size,
157 })?;
158
159 if end > file_size {
161 return Err(FafbError::InvalidSectionTableOffset {
162 offset: self.offset,
163 file_size,
164 });
165 }
166
167 Ok(())
168 }
169}
170
171#[derive(Debug, Clone, Default)]
173pub struct SectionTable {
174 entries: Vec<SectionEntry>,
175}
176
177impl SectionTable {
178 pub fn new() -> Self {
180 Self {
181 entries: Vec::new(),
182 }
183 }
184
185 pub fn with_capacity(capacity: usize) -> Self {
187 Self {
188 entries: Vec::with_capacity(capacity),
189 }
190 }
191
192 pub fn push(&mut self, entry: SectionEntry) {
194 self.entries.push(entry);
195 }
196
197 pub fn len(&self) -> usize {
199 self.entries.len()
200 }
201
202 pub fn is_empty(&self) -> bool {
204 self.entries.is_empty()
205 }
206
207 pub fn get(&self, index: usize) -> Option<&SectionEntry> {
209 self.entries.get(index)
210 }
211
212 pub fn get_by_name_index(&self, name_index: u8) -> Option<&SectionEntry> {
214 self.entries.iter().find(|e| e.name_index == name_index)
215 }
216
217 pub fn entries(&self) -> &[SectionEntry] {
219 &self.entries
220 }
221
222 pub fn entries_by_priority(&self) -> Vec<&SectionEntry> {
224 let mut sorted: Vec<_> = self.entries.iter().collect();
225 sorted.sort_by_key(|e| std::cmp::Reverse(e.priority));
226 sorted
227 }
228
229 pub fn entries_within_budget(&self, budget: u16) -> Vec<&SectionEntry> {
231 let mut result = Vec::new();
235 let mut remaining = budget;
236
237 for entry in self.entries_by_priority() {
238 if entry.token_count <= remaining {
239 result.push(entry);
240 remaining -= entry.token_count;
241 } else if entry.priority.is_critical() {
242 result.push(entry);
245 }
246 }
249
250 result
251 }
252
253 pub fn total_tokens(&self) -> u32 {
255 self.entries.iter().map(|e| e.token_count as u32).sum()
256 }
257
258 pub fn table_size(&self) -> usize {
260 self.entries.len() * SECTION_ENTRY_SIZE
261 }
262
263 pub fn write<W: Write>(&self, writer: &mut W) -> FafbResult<()> {
265 for entry in &self.entries {
266 entry.write(writer)?;
267 }
268 Ok(())
269 }
270
271 pub fn to_bytes(&self) -> FafbResult<Vec<u8>> {
273 let mut buf = Vec::with_capacity(self.table_size());
274 self.write(&mut buf)?;
275 Ok(buf)
276 }
277
278 pub fn read<R: Read>(reader: &mut R, count: usize) -> FafbResult<Self> {
280 let mut entries = Vec::with_capacity(count);
281 for _ in 0..count {
282 entries.push(SectionEntry::read(reader)?);
283 }
284 Ok(Self { entries })
285 }
286
287 pub fn from_bytes(data: &[u8], count: usize) -> FafbResult<Self> {
289 let expected_size = count * SECTION_ENTRY_SIZE;
290 if data.len() < expected_size {
291 return Err(FafbError::FileTooSmall {
292 expected: expected_size,
293 actual: data.len(),
294 });
295 }
296 let mut cursor = std::io::Cursor::new(data);
297 Self::read(&mut cursor, count)
298 }
299
300 pub fn validate_bounds(&self, file_size: u32) -> FafbResult<()> {
302 for entry in &self.entries {
303 entry.validate_bounds(file_size)?;
304 }
305 Ok(())
306 }
307}
308
309impl IntoIterator for SectionTable {
310 type Item = SectionEntry;
311 type IntoIter = std::vec::IntoIter<SectionEntry>;
312
313 fn into_iter(self) -> Self::IntoIter {
314 self.entries.into_iter()
315 }
316}
317
318impl<'a> IntoIterator for &'a SectionTable {
319 type Item = &'a SectionEntry;
320 type IntoIter = std::slice::Iter<'a, SectionEntry>;
321
322 fn into_iter(self) -> Self::IntoIter {
323 self.entries.iter()
324 }
325}
326
327fn estimate_tokens(byte_length: u32) -> u16 {
330 std::cmp::min(byte_length / 4, u16::MAX as u32) as u16
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 #[test]
342 fn test_section_entry_size() {
343 let entry = SectionEntry::new(0, 32, 100);
344 let bytes = entry.to_bytes().unwrap();
345 assert_eq!(bytes.len(), SECTION_ENTRY_SIZE);
346 assert_eq!(bytes.len(), 16);
347 }
348
349 #[test]
350 fn test_section_entry_roundtrip() {
351 let original = SectionEntry::new(5, 64, 256)
352 .with_priority(Priority::high())
353 .with_token_count(100)
354 .with_flags(0xDEADBEEF);
355
356 let bytes = original.to_bytes().unwrap();
357 let recovered = SectionEntry::from_bytes(&bytes).unwrap();
358
359 assert_eq!(original.name_index, recovered.name_index);
360 assert_eq!(original.priority, recovered.priority);
361 assert_eq!(original.offset, recovered.offset);
362 assert_eq!(original.length, recovered.length);
363 assert_eq!(original.token_count, recovered.token_count);
364 assert_eq!(original.flags, recovered.flags);
365 }
366
367 #[test]
368 fn test_token_estimation() {
369 assert_eq!(estimate_tokens(0), 0);
370 assert_eq!(estimate_tokens(4), 1);
371 assert_eq!(estimate_tokens(100), 25);
372 assert_eq!(estimate_tokens(1000), 250);
373 }
374
375 #[test]
376 fn test_token_estimation_cap() {
377 let huge = estimate_tokens(u32::MAX);
379 assert_eq!(huge, u16::MAX);
380 }
381
382 #[test]
383 fn test_section_table_empty() {
384 let table = SectionTable::new();
385 assert!(table.is_empty());
386 assert_eq!(table.len(), 0);
387 assert_eq!(table.table_size(), 0);
388 }
389
390 #[test]
391 fn test_section_table_push() {
392 let mut table = SectionTable::new();
393 table.push(SectionEntry::new(0, 32, 100));
394 table.push(SectionEntry::new(1, 132, 200));
395
396 assert_eq!(table.len(), 2);
397 assert_eq!(table.table_size(), 32);
398 }
399
400 #[test]
401 fn test_section_table_roundtrip() {
402 let mut original = SectionTable::new();
403 original.push(SectionEntry::new(0, 32, 100));
404 original.push(SectionEntry::new(1, 132, 200));
405 original.push(SectionEntry::new(2, 332, 500));
406
407 let bytes = original.to_bytes().unwrap();
408 assert_eq!(bytes.len(), 48); let recovered = SectionTable::from_bytes(&bytes, 3).unwrap();
411 assert_eq!(recovered.len(), 3);
412
413 for (orig, recv) in original.entries().iter().zip(recovered.entries().iter()) {
414 assert_eq!(orig.name_index, recv.name_index);
415 assert_eq!(orig.offset, recv.offset);
416 assert_eq!(orig.length, recv.length);
417 }
418 }
419
420 #[test]
421 fn test_section_table_get_by_name_index() {
422 let mut table = SectionTable::new();
423 table.push(SectionEntry::new(0, 32, 100));
424 table.push(SectionEntry::new(1, 132, 200));
425
426 let first = table.get_by_name_index(0);
427 assert!(first.is_some());
428 assert_eq!(first.unwrap().offset, 32);
429
430 assert!(table.get_by_name_index(9).is_none());
431 }
432
433 #[test]
434 fn test_section_table_priority_sorting() {
435 let mut table = SectionTable::new();
436 table.push(SectionEntry::new(2, 0, 100).with_priority(Priority::low()));
437 table.push(SectionEntry::new(0, 0, 100).with_priority(Priority::critical()));
438 table.push(SectionEntry::new(1, 0, 100).with_priority(Priority::high()));
439
440 let sorted = table.entries_by_priority();
441 assert_eq!(sorted[0].name_index, 0); assert_eq!(sorted[1].name_index, 1); assert_eq!(sorted[2].name_index, 2); }
445
446 #[test]
447 fn test_section_table_budget() {
448 let mut table = SectionTable::new();
449 table.push(
450 SectionEntry::new(0, 0, 100)
451 .with_priority(Priority::critical())
452 .with_token_count(50),
453 );
454 table.push(
455 SectionEntry::new(1, 0, 200)
456 .with_priority(Priority::high())
457 .with_token_count(100),
458 );
459 table.push(
460 SectionEntry::new(2, 0, 1000)
461 .with_priority(Priority::low())
462 .with_token_count(500),
463 );
464
465 let within_budget = table.entries_within_budget(200);
467 assert_eq!(within_budget.len(), 2);
468
469 assert!(within_budget.iter().any(|e| e.name_index == 0));
471 }
472
473 #[test]
474 fn test_section_table_total_tokens() {
475 let mut table = SectionTable::new();
476 table.push(SectionEntry::new(0, 0, 100).with_token_count(50));
477 table.push(SectionEntry::new(1, 0, 200).with_token_count(100));
478
479 assert_eq!(table.total_tokens(), 150);
480 }
481
482 #[test]
483 fn test_section_entry_validate_bounds() {
484 let entry = SectionEntry::new(0, 100, 50);
485
486 assert!(entry.validate_bounds(200).is_ok());
488
489 assert!(entry.validate_bounds(100).is_err());
491 }
492
493 #[test]
494 fn test_unknown_name_index_preserved() {
495 let entry = SectionEntry::new(0x99, 0, 100)
497 .with_priority(Priority::medium())
498 .with_token_count(25);
499
500 let bytes = entry.to_bytes().unwrap();
501 let recovered = SectionEntry::from_bytes(&bytes).unwrap();
502
503 assert_eq!(recovered.name_index, 0x99);
504 }
505}