use std::marker::PhantomData;
use crate::object::ArenaIndex;
use crate::BufferArena;
use crate::ContentType;
use crate::ExtractRequestBuilder;
use crate::ExtractionRequest;
use crate::FindigsIterator;
use crate::FindingMetadata;
use crate::InternalFinding;
use crate::NoMetadata;
use crate::Object;
use crate::ScanObserver;
use varmap::{VarMap, VarMapPool};
pub struct Context<T: ContentType, M: FindingMetadata = NoMetadata> {
pub(crate) global: VarMap,
pub(crate) objects: Vec<Object>,
pub(crate) path_arena: BufferArena,
pub(crate) varmap_pool: VarMapPool,
pub(crate) local_varmap_handle: Option<varmap::PoolHandle>,
pub(crate) current_object_index: Option<u32>,
pub(crate) extraction_requests_stack: Vec<ExtractionRequest<T>>,
pub(crate) findings: Vec<InternalFinding<M>>,
pub(crate) observer: Option<Box<dyn ScanObserver<T, M>>>,
pub(crate) store_findings: bool,
}
impl<T: ContentType, M: FindingMetadata> Context<T, M> {
pub(crate) fn new(observer: Option<Box<dyn ScanObserver<T, M>>>, store_findings: bool) -> Self {
Self {
global: VarMap::new(),
objects: Vec::with_capacity(16),
path_arena: BufferArena::new(),
varmap_pool: VarMapPool::new(),
local_varmap_handle: None,
current_object_index: None,
extraction_requests_stack: Vec::with_capacity(16),
findings: Vec::with_capacity(16),
observer,
store_findings,
}
}
pub(crate) fn clear(&mut self) {
self.global.clear();
self.objects.clear();
self.path_arena.clear();
self.varmap_pool.clear(varmap::ClearStrategy::KeepSmallestN(128));
self.local_varmap_handle = None;
self.current_object_index = None;
self.extraction_requests_stack.clear();
self.findings.clear();
}
#[inline(always)]
pub fn global(&mut self) -> &mut VarMap {
&mut self.global
}
pub fn request_extract(&mut self, content_type: T) -> ExtractRequestBuilder<'_, T, M> {
ExtractRequestBuilder::new(self, content_type)
}
pub fn local(&mut self) -> &mut VarMap {
if self.local_varmap_handle.is_none() {
let handle = self.varmap_pool.allocate();
self.local_varmap_handle = Some(handle);
if let Some(idx) = self.current_object_index {
self.objects[idx as usize].varmap_handle = Some(handle);
}
}
self.varmap_pool.get_mut(self.local_varmap_handle.unwrap()).unwrap()
}
#[inline(always)]
pub fn objects_scanned(&self) -> u32 {
self.objects.len() as u32
}
pub fn add_finding(&mut self, finding: &str, source: Option<&str>, metadata: Option<M>) {
if let Some(objindex) = self.current_object_index {
if let Some(observer) = self.observer.as_mut() {
let path = unsafe { std::str::from_utf8_unchecked(self.path_arena.get(self.objects[objindex as usize].path).unwrap_or_default()) };
observer.on_finding(path, finding, source, metadata.as_ref());
}
if self.store_findings {
let source_index = if let Some(source) = source {
self.path_arena.alloc(source.as_bytes())
} else {
ArenaIndex::INVALID
};
self.findings.push(InternalFinding {
objindex,
finding: self.path_arena.alloc(finding.as_bytes()),
source: source_index,
metadata,
});
}
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct ScanContentHandle {
pub(crate) index: u32,
}
pub struct ScanResult<'a, T: ContentType, M: FindingMetadata = NoMetadata> {
pub(crate) context: &'a Context<T, M>,
_extra: PhantomData<(T, M)>,
}
impl<'a, T: ContentType, M: FindingMetadata> ScanResult<'a, T, M> {
pub(crate) fn new(context: &'a Context<T, M>) -> Self {
Self {
context,
_extra: PhantomData,
}
}
pub fn global(&self) -> &VarMap {
&self.context.global
}
pub fn objects_scanned(&self) -> u32 {
self.context.objects.len() as u32
}
pub fn root(&self) -> Option<ScanContentHandle> {
if self.context.objects.is_empty() {
None
} else {
Some(ScanContentHandle { index: 0 })
}
}
pub fn parent(&self, handle: ScanContentHandle) -> Option<ScanContentHandle> {
let object = self.context.objects.get(handle.index as usize)?;
if object.parent_index as usize >= self.context.objects.len() {
None
} else {
Some(ScanContentHandle { index: object.parent_index })
}
}
pub fn next_sibling(&self, handle: ScanContentHandle) -> Option<ScanContentHandle> {
let object = self.context.objects.get(handle.index as usize)?;
if object.next_sibling_index as usize >= self.context.objects.len() {
None
} else {
Some(ScanContentHandle {
index: object.next_sibling_index,
})
}
}
pub fn child(&self, handle: ScanContentHandle) -> Option<ScanContentHandle> {
let object = self.context.objects.get(handle.index as usize)?;
if object.first_child_index as usize >= self.context.objects.len() {
None
} else {
Some(ScanContentHandle {
index: object.first_child_index,
})
}
}
pub fn local(&self, handle: ScanContentHandle) -> Option<&VarMap> {
let object = self.context.objects.get(handle.index as usize)?;
if let Some(varmap_handle) = object.varmap_handle {
self.context.varmap_pool.get(varmap_handle)
} else {
None
}
}
pub fn path(&self, handle: ScanContentHandle) -> Option<&str> {
let object = self.context.objects.get(handle.index as usize)?;
let path = self.context.path_arena.get(object.path)?;
std::str::from_utf8(path).ok()
}
pub fn content_type(&self, handle: ScanContentHandle) -> Option<T> {
let object = self.context.objects.get(handle.index as usize)?;
T::from_u16(object.type_id)
}
pub fn findings(&self) -> FindigsIterator<'a, T, M> {
FindigsIterator::new(self.context)
}
}