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
/* Copyright 2022-2023 Danny McClanahan */
/* SPDX-License-Identifier: BSD-3-Clause */
#[cfg(feature = "compiler")]
use crate::{
error::HyperscanCompileError,
expression::{Expression, ExpressionSet, Literal, LiteralSet},
flags::{
platform::{CpuFeatures, TuneFamily},
Flags, Mode,
},
};
use crate::{error::HyperscanRuntimeError, hs, state::Scratch};
#[cfg(feature = "compiler")]
use once_cell::sync::Lazy;
use std::{
borrow::Cow,
ffi::CStr,
mem::{self, MaybeUninit},
ops,
os::raw::c_char,
ptr, slice, str,
};
#[derive(Debug)]
#[repr(transparent)]
pub struct Database(*mut NativeDb);
pub type NativeDb = hs::hs_database;
impl Database {
pub const unsafe fn from_native(p: *mut NativeDb) -> Self { Self(p) }
pub fn as_ref_native(&self) -> &NativeDb { unsafe { &*self.0 } }
pub fn as_mut_native(&mut self) -> &mut NativeDb { unsafe { &mut *self.0 } }
pub fn allocate_scratch(&self) -> Result<Scratch, HyperscanRuntimeError> {
let mut scratch = Scratch::new();
scratch.setup_for_db(self)?;
Ok(scratch)
}
///```
/// # fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, database::*, matchers::*};
///
/// let expr: Expression = "(he)ll".parse()?;
/// let db = Database::compile(&expr, Flags::UTF8, Mode::BLOCK, None)?;
///
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// scratch
/// .scan_sync(&db, "hello".into(), |m| {
/// matches.push(unsafe { m.source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["hell"]);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile(
expression: &Expression,
flags: Flags,
mode: Mode,
platform: Option<&Platform>,
) -> Result<Self, HyperscanCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
HyperscanRuntimeError::copy_from_native_compile_error(
unsafe {
hs::hs_compile(
expression.as_ptr(),
flags.into_native(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
///```
/// # fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, database::*, matchers::*};
///
/// let expr: Literal = "he\0ll".parse()?;
/// let db = Database::compile_literal(&expr, Flags::default(), Mode::BLOCK, None)?;
///
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// scratch
/// .scan_sync(&db, "he\0llo".into(), |m| {
/// matches.push(unsafe { m.source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["he\0ll"]);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile_literal(
literal: &Literal,
flags: Flags,
mode: Mode,
platform: Option<&Platform>,
) -> Result<Self, HyperscanCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
HyperscanRuntimeError::copy_from_native_compile_error(
unsafe {
hs::hs_compile_lit(
literal.as_ptr(),
flags.into_native(),
literal.as_bytes().len(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
///```
/// # fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, database::*, matchers::*};
///
/// let a_expr: Expression = "a+".parse()?;
/// let b_expr: Expression = "b+".parse()?;
///
/// // Example of providing ExprExt info (not available in ::compile()!):
/// let ext = ExprExt::from_min_length(1);
///
/// let expr_set = ExpressionSet::from_exprs([&a_expr, &b_expr])
/// .with_flags([Flags::UTF8, Flags::UTF8])
/// .with_ids([ExprId(1), ExprId(2)])
/// .with_exts([None, Some(&ext)]);
///
/// let db = Database::compile_multi(&expr_set, Mode::BLOCK, None)?;
///
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// scratch
/// .scan_sync(&db, "aardvark".into(), |m| {
/// matches.push(unsafe { m.source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["a", "aa", "aardva"]);
///
/// matches.clear();
/// scratch
/// .scan_sync(&db, "imbibe".into(), |m| {
/// matches.push(unsafe { m.source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["imb", "imbib"]);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile_multi(
expression_set: &ExpressionSet,
mode: Mode,
platform: Option<&Platform>,
) -> Result<Self, HyperscanCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
HyperscanRuntimeError::copy_from_native_compile_error(
unsafe {
if let Some(exts_ptr) = expression_set.exts_ptr() {
hs::hs_compile_ext_multi(
expression_set.expressions_ptr(),
expression_set.flags_ptr(),
expression_set.ids_ptr(),
exts_ptr,
expression_set.num_elements(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
} else {
hs::hs_compile_multi(
expression_set.expressions_ptr(),
expression_set.flags_ptr(),
expression_set.ids_ptr(),
expression_set.num_elements(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
}
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
///```
/// # fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, database::*, matchers::{*, contiguous_slice::*}};
///
/// let hell_lit: Literal = "he\0ll".parse()?;
/// let free_lit: Literal = "fr\0e\0e".parse()?;
/// let lit_set = LiteralSet::from_lits([&hell_lit, &free_lit])
/// .with_flags([Flags::default(), Flags::default()])
/// .with_ids([ExprId(2), ExprId(1)]);
///
/// let db = Database::compile_multi_literal(&lit_set, Mode::BLOCK, None)?;
///
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<(u32, &str)> = Vec::new();
/// scratch
/// .scan_sync(
/// &db,
/// "he\0llo".into(),
/// |Match { id: ExpressionIndex(id), source, .. }| {
/// matches.push((id, unsafe { source.as_str() }));
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &[(2, "he\0ll")]);
///
/// matches.clear();
/// scratch
/// .scan_sync(
/// &db,
/// "fr\0e\0edom".into(),
/// |Match { id: ExpressionIndex(id), source, .. }| {
/// matches.push((id, unsafe { source.as_str() }));
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &[(1, "fr\0e\0e")]);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile_multi_literal(
literal_set: &LiteralSet,
mode: Mode,
platform: Option<&Platform>,
) -> Result<Self, HyperscanCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
HyperscanRuntimeError::copy_from_native_compile_error(
unsafe {
hs::hs_compile_lit_multi(
literal_set.literals_ptr(),
literal_set.flags_ptr(),
literal_set.ids_ptr(),
literal_set.lengths_ptr(),
literal_set.num_elements(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*};
///
/// let expr: Expression = "a+".parse()?;
/// let db = expr.compile(Flags::UTF8, Mode::BLOCK)?;
/// let db_size = db.database_size()?;
///
/// // Size may vary across architectures:
/// assert_eq!(db_size, 936);
/// assert!(db_size > 500);
/// assert!(db_size < 2000);
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub fn database_size(&self) -> Result<usize, HyperscanRuntimeError> {
let mut ret: MaybeUninit<usize> = MaybeUninit::uninit();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_database_size(self.as_ref_native(), ret.as_mut_ptr())
})?;
Ok(unsafe { ret.assume_init() })
}
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*};
///
/// let expr: Expression = "a+".parse()?;
/// let db = expr.compile(Flags::UTF8, Mode::STREAM)?;
/// let stream_size = db.stream_size()?;
///
/// // Size may vary across architectures:
/// assert_eq!(stream_size, 18);
/// assert!(stream_size > 10);
/// assert!(stream_size < 20);
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub fn stream_size(&self) -> Result<usize, HyperscanRuntimeError> {
let mut ret: MaybeUninit<usize> = MaybeUninit::uninit();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_stream_size(self.as_ref_native(), ret.as_mut_ptr())
})?;
Ok(unsafe { ret.assume_init() })
}
pub fn info(&self) -> Result<DbInfo, HyperscanRuntimeError> { DbInfo::extract_db_info(self) }
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, matchers::{*, contiguous_slice::*}};
///
/// let expr: Expression = "a+".parse()?;
/// let db = expr.compile(Flags::SOM_LEFTMOST, Mode::BLOCK)?.serialize()?.deserialize_db()?;
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// scratch
/// .scan_sync(&db, "aardvark".into(), |Match { source, .. }| {
/// matches.push(unsafe { source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["a", "aa", "a"]);
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub fn serialize(&self) -> Result<SerializedDb<'static>, HyperscanRuntimeError> {
SerializedDb::serialize_db(self)
}
pub unsafe fn try_drop(&mut self) -> Result<(), HyperscanRuntimeError> {
HyperscanRuntimeError::from_native(unsafe { hs::hs_free_database(self.as_mut_native()) })
}
}
impl ops::Drop for Database {
fn drop(&mut self) {
unsafe {
self.try_drop().unwrap();
}
}
}
unsafe impl Send for Database {}
unsafe impl Sync for Database {}
#[derive(Debug)]
pub struct MiscAllocation {
data: *mut u8,
len: usize,
}
unsafe impl Send for MiscAllocation {}
unsafe impl Sync for MiscAllocation {}
impl MiscAllocation {
pub const fn as_ptr(&self) -> *mut u8 { self.data }
pub const fn len(&self) -> usize { self.len }
pub const fn is_empty(&self) -> bool { self.len() == 0 }
pub const fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.data, self.len) } }
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
}
pub unsafe fn free(&mut self) { crate::free_misc(self.data) }
}
impl ops::Drop for MiscAllocation {
fn drop(&mut self) {
unsafe {
self.free();
}
}
}
#[derive(Debug)]
pub struct DbInfo(MiscAllocation);
impl DbInfo {
const fn without_null(&self) -> impl slice::SliceIndex<[u8], Output=[u8]> { ..(self.0.len() - 1) }
pub fn as_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.0.as_slice()[self.without_null()]) }
}
pub fn as_mut_str(&mut self) -> &mut str {
let without_null = self.without_null();
unsafe { str::from_utf8_unchecked_mut(&mut self.0.as_mut_slice()[without_null]) }
}
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, database::*};
///
/// let expr: Expression = "a+".parse()?;
/// let db = expr.compile(Flags::UTF8, Mode::BLOCK)?;
/// let info = DbInfo::extract_db_info(&db)?;
/// assert_eq!(info.as_str(), "Version: 5.4.2 Features: AVX2 Mode: BLOCK");
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub fn extract_db_info(db: &Database) -> Result<Self, HyperscanRuntimeError> {
let mut info = ptr::null_mut();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_database_info(db.as_ref_native(), &mut info)
})?;
let len = unsafe { CStr::from_ptr(info) }.to_bytes_with_nul().len();
assert!(len > 0);
let ret = MiscAllocation {
data: unsafe { mem::transmute(info) },
len,
};
Ok(Self(ret))
}
}
#[derive(Debug)]
pub enum DbAllocation<'a> {
Misc(MiscAllocation),
Rust(Cow<'a, [u8]>),
}
impl<'a> DbAllocation<'a> {
pub fn as_ptr(&self) -> *const u8 {
match self {
Self::Misc(misc) => misc.as_ptr(),
Self::Rust(cow) => cow.as_ptr(),
}
}
pub fn len(&self) -> usize {
match self {
Self::Misc(misc) => misc.len(),
Self::Rust(cow) => cow.len(),
}
}
pub fn is_empty(&self) -> bool { self.len() == 0 }
pub fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.as_ptr(), self.len()) } }
}
impl DbAllocation<'static> {
pub fn from_cloned_data(s: &DbAllocation) -> Self {
let newly_allocated: Vec<u8> = s.as_slice().to_vec();
Self::Rust(Cow::Owned(newly_allocated))
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct SerializedDb<'a>(pub DbAllocation<'a>);
impl SerializedDb<'static> {
pub fn serialize_db(db: &Database) -> Result<Self, HyperscanRuntimeError> {
let mut data = ptr::null_mut();
let mut len: usize = 0;
HyperscanRuntimeError::from_native(unsafe {
hs::hs_serialize_database(db.as_ref_native(), &mut data, &mut len)
})?;
let data = data as *mut u8;
Ok(Self(DbAllocation::Misc(MiscAllocation { data, len })))
}
pub fn from_cloned_data(s: &SerializedDb) -> Self {
let SerializedDb(ref s) = s;
Self(DbAllocation::from_cloned_data(s))
}
}
impl<'a> SerializedDb<'a> {
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*};
///
/// let expr: Expression = "a+".parse()?;
/// let serialized_db = expr.compile(Flags::UTF8, Mode::BLOCK)?.serialize()?;
/// let info = serialized_db.extract_db_info()?;
/// assert_eq!(info.as_str(), "Version: 5.4.2 Features: AVX2 Mode: BLOCK");
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub fn extract_db_info(&self) -> Result<DbInfo, HyperscanRuntimeError> {
let mut info = ptr::null_mut();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_serialized_database_info(self.as_ptr(), self.len(), &mut info)
})?;
let len = unsafe { CStr::from_ptr(info) }.to_bytes_with_nul().len();
assert!(len > 0);
let ret = MiscAllocation {
data: info as *mut u8,
len,
};
Ok(DbInfo(ret))
}
fn as_ptr(&self) -> *const c_char { unsafe { mem::transmute(self.0.as_ptr()) } }
pub fn len(&self) -> usize { self.0.len() }
pub fn is_empty(&self) -> bool { self.0.is_empty() }
pub fn deserialize_db(&self) -> Result<Database, HyperscanRuntimeError> {
let mut deserialized: MaybeUninit<*mut hs::hs_database> = MaybeUninit::uninit();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_deserialize_database(self.as_ptr(), self.len(), deserialized.as_mut_ptr())
})?;
let deserialized = unsafe { deserialized.assume_init() };
Ok(unsafe { Database::from_native(deserialized) })
}
/// Return the size of the allocation necessary for a subsequent call to
/// [`Self::deserialize_db_at()`].
pub fn deserialized_size(&self) -> Result<usize, HyperscanRuntimeError> {
let mut deserialized_size: MaybeUninit<usize> = MaybeUninit::uninit();
HyperscanRuntimeError::from_native(unsafe {
hs::hs_serialized_database_size(self.as_ptr(), self.len(), deserialized_size.as_mut_ptr())
})?;
let deserialized_size = unsafe { deserialized_size.assume_init() };
Ok(deserialized_size)
}
/// Like [`Self::deserialize_db()`], but points into an existing allocation
/// instead of making a new allocation through the allocator from
/// [`crate::alloc::set_db_allocator()`]!
///
/// **Safety: `db` must point to an allocation at least
/// [`Self::deserialized_size()`] in size!**
///
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), hyperscan::error::HyperscanError> {
/// use hyperscan::{expression::*, flags::*, matchers::{*, contiguous_slice::*}, database::*};
/// use std::mem;
///
/// let expr: Expression = "a+".parse()?;
/// let serialized_db = expr.compile(Flags::SOM_LEFTMOST, Mode::BLOCK)?.serialize()?;
///
/// // Allocate a vector with sufficient capacity for the deserialized db:
/// let mut db_data: Vec<u8> = Vec::with_capacity(serialized_db.deserialized_size()?);
/// let db = unsafe {
/// let db_ptr: *mut NativeDb = mem::transmute(db_data.as_mut_ptr());
/// serialized_db.deserialize_db_at(db_ptr)?;
/// // Wrap in ManuallyDrop to avoid freeing memory owned by the `db_data` vector.
/// mem::ManuallyDrop::new(Database::from_native(db_ptr))
/// };
///
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// scratch
/// .scan_sync(&db, "aardvark".into(), |Match { source, ..}| {
/// matches.push(unsafe { source.as_str() });
/// MatchResult::Continue
/// })?;
/// assert_eq!(&matches, &["a", "aa", "a"]);
/// Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub unsafe fn deserialize_db_at(&self, db: *mut NativeDb) -> Result<(), HyperscanRuntimeError> {
HyperscanRuntimeError::from_native(hs::hs_deserialize_database_at(
self.as_ptr(),
self.len(),
db,
))
}
}
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct Platform(hs::hs_platform_info);
#[cfg(feature = "compiler")]
static CACHED_PLATFORM: Lazy<Platform> = Lazy::new(|| Platform::populate().unwrap());
#[cfg(feature = "compiler")]
impl Platform {
pub fn tune(&self) -> TuneFamily { TuneFamily::from_native(self.0.tune) }
pub fn set_tune(&mut self, tune: TuneFamily) { self.0.tune = tune.into_native(); }
pub fn cpu_features(&self) -> CpuFeatures { CpuFeatures::from_native(self.0.cpu_features) }
pub fn set_cpu_features(&mut self, cpu_features: CpuFeatures) {
self.0.cpu_features = cpu_features.into_native();
}
fn populate() -> Result<Self, HyperscanRuntimeError> {
let mut s = mem::MaybeUninit::<hs::hs_platform_info>::uninit();
HyperscanRuntimeError::from_native(unsafe { hs::hs_populate_platform(s.as_mut_ptr()) })?;
Ok(unsafe { Self(s.assume_init()) })
}
pub fn get() -> &'static Self { &CACHED_PLATFORM }
pub(crate) fn as_ref_native(&self) -> &hs::hs_platform_info { &self.0 }
}
#[cfg(feature = "chimera")]
#[cfg_attr(docsrs, doc(cfg(feature = "chimera")))]
pub mod chimera {
#[cfg(feature = "compiler")]
use super::Platform;
#[cfg(feature = "compiler")]
use crate::{
error::chimera::ChimeraCompileError,
expression::chimera::{ChimeraExpression, ChimeraExpressionSet, ChimeraMatchLimits},
flags::chimera::{ChimeraFlags, ChimeraMode},
};
use crate::{error::chimera::ChimeraRuntimeError, hs, state::chimera::ChimeraScratch};
use std::{ffi::CStr, mem, ops, ptr, slice, str};
#[derive(Debug)]
#[repr(transparent)]
pub struct ChimeraDb(*mut NativeChimeraDb);
pub type NativeChimeraDb = hs::ch_database;
impl ChimeraDb {
pub const unsafe fn from_native(p: *mut NativeChimeraDb) -> Self { Self(p) }
pub fn as_ref_native(&self) -> &hs::ch_database { unsafe { &*self.0 } }
pub fn as_mut_native(&mut self) -> &mut hs::ch_database { unsafe { &mut *self.0 } }
///```
/// # fn main() -> Result<(), hyperscan::error::chimera::ChimeraError> {
/// use hyperscan::{expression::chimera::*, flags::chimera::*, database::chimera::*};
///
/// let expr: ChimeraExpression = "(he)ll".parse()?;
/// # #[allow(unused_variables)]
/// let db = ChimeraDb::compile(&expr, ChimeraFlags::UTF8, ChimeraMode::NOGROUPS, None)?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile(
expression: &ChimeraExpression,
flags: ChimeraFlags,
mode: ChimeraMode,
platform: Option<&Platform>,
) -> Result<Self, ChimeraCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
ChimeraRuntimeError::copy_from_native_compile_error(
unsafe {
hs::ch_compile(
expression.as_ptr(),
flags.into_native(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
///```
/// # fn main() -> Result<(), hyperscan::error::chimera::ChimeraError> {
/// use hyperscan::{expression::{*, chimera::*}, flags::chimera::*, database::chimera::*, matchers::chimera::*};
///
/// let a_expr: ChimeraExpression = "a+".parse()?;
/// let b_expr: ChimeraExpression = "b+".parse()?;
/// let exprs = ChimeraExpressionSet::from_exprs([&a_expr, &b_expr])
/// .with_flags([ChimeraFlags::UTF8, ChimeraFlags::UTF8])
/// .with_ids([ExprId(1), ExprId(2)])
/// .with_limits(ChimeraMatchLimits { match_limit: 30, match_limit_recursion: 30 });
/// let db = ChimeraDb::compile_multi(&exprs, ChimeraMode::NOGROUPS, None)?;
/// let mut scratch = db.allocate_scratch()?;
///
/// let mut matches: Vec<&str> = Vec::new();
/// let e = |_| ChimeraMatchResult::Continue;
/// scratch.scan_sync(&db, "aardvark imbibbe".into(), |ChimeraMatch { source, .. }| {
/// matches.push(unsafe { source.as_str() });
/// ChimeraMatchResult::Continue
/// }, e)?;
/// assert_eq!(&matches, &["aa", "a", "b", "bb"]);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "compiler")]
#[cfg_attr(docsrs, doc(cfg(feature = "compiler")))]
pub fn compile_multi(
exprs: &ChimeraExpressionSet,
mode: ChimeraMode,
platform: Option<&Platform>,
) -> Result<Self, ChimeraCompileError> {
let mut db = ptr::null_mut();
let mut compile_err = ptr::null_mut();
ChimeraRuntimeError::copy_from_native_compile_error(
unsafe {
if let Some(ChimeraMatchLimits {
match_limit,
match_limit_recursion,
}) = exprs.limits()
{
hs::ch_compile_ext_multi(
exprs.expressions_ptr(),
exprs.flags_ptr(),
exprs.ids_ptr(),
exprs.num_elements(),
mode.into_native(),
match_limit,
match_limit_recursion,
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
} else {
hs::ch_compile_multi(
exprs.expressions_ptr(),
exprs.flags_ptr(),
exprs.ids_ptr(),
exprs.num_elements(),
mode.into_native(),
platform
.map(|p| mem::transmute(p.as_ref_native()))
.unwrap_or(ptr::null()),
&mut db,
&mut compile_err,
)
}
},
compile_err,
)?;
Ok(unsafe { Self::from_native(db) })
}
pub fn get_db_size(&self) -> Result<usize, ChimeraRuntimeError> {
let mut database_size: usize = 0;
ChimeraRuntimeError::from_native(unsafe {
hs::ch_database_size(self.as_ref_native(), &mut database_size)
})?;
Ok(database_size)
}
pub fn info(&self) -> Result<ChimeraDbInfo, ChimeraRuntimeError> {
ChimeraDbInfo::extract_db_info(self)
}
pub fn allocate_scratch(&self) -> Result<ChimeraScratch, ChimeraRuntimeError> {
let mut scratch = ChimeraScratch::new();
scratch.setup_for_db(self)?;
Ok(scratch)
}
pub unsafe fn try_drop(&mut self) -> Result<(), ChimeraRuntimeError> {
ChimeraRuntimeError::from_native(hs::ch_free_database(self.as_mut_native()))
}
}
impl ops::Drop for ChimeraDb {
fn drop(&mut self) {
unsafe {
self.try_drop().unwrap();
}
}
}
#[derive(Debug)]
pub struct ChimeraMiscAllocation {
data: *mut u8,
len: usize,
}
unsafe impl Send for ChimeraMiscAllocation {}
unsafe impl Sync for ChimeraMiscAllocation {}
impl ChimeraMiscAllocation {
pub const fn as_ptr(&self) -> *mut u8 { self.data }
pub const fn len(&self) -> usize { self.len }
pub const fn is_empty(&self) -> bool { self.len() == 0 }
pub const fn as_slice(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.data, self.len) } }
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
}
pub unsafe fn free(&mut self) { crate::free_misc_chimera(self.data) }
}
impl ops::Drop for ChimeraMiscAllocation {
fn drop(&mut self) {
unsafe {
self.free();
}
}
}
#[derive(Debug)]
pub struct ChimeraDbInfo(ChimeraMiscAllocation);
impl ChimeraDbInfo {
const fn without_null(&self) -> impl slice::SliceIndex<[u8], Output=[u8]> {
..(self.0.len() - 1)
}
pub fn as_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.0.as_slice()[self.without_null()]) }
}
pub fn as_mut_str(&mut self) -> &mut str {
let without_null = self.without_null();
unsafe { str::from_utf8_unchecked_mut(&mut self.0.as_mut_slice()[without_null]) }
}
///```
/// # fn main() -> Result<(), hyperscan::error::chimera::ChimeraError> {
/// use hyperscan::{expression::chimera::*, flags::chimera::*, database::chimera::*};
///
/// let expr: ChimeraExpression = "a+".parse()?;
/// let db = expr.compile(ChimeraFlags::UTF8, ChimeraMode::NOGROUPS)?;
/// let info = ChimeraDbInfo::extract_db_info(&db)?;
/// assert_eq!(info.as_str(), "Chimera Version: 5.4.2 Features: AVX2 Mode: BLOCK");
/// # Ok(())
/// # }
/// ```
pub fn extract_db_info(db: &ChimeraDb) -> Result<Self, ChimeraRuntimeError> {
let mut info = ptr::null_mut();
ChimeraRuntimeError::from_native(unsafe {
hs::ch_database_info(db.as_ref_native(), &mut info)
})?;
let len = unsafe { CStr::from_ptr(info) }.to_bytes_with_nul().len();
assert!(len > 0);
let ret = ChimeraMiscAllocation {
data: unsafe { mem::transmute(info) },
len,
};
Ok(Self(ret))
}
}
}