Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement more string ops in python bindings #4

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions crates/claripy/src/ast/string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(non_snake_case)]

use ast::bv::BV;

use crate::ast::bits::Bits;
use crate::prelude::*;

Expand All @@ -20,14 +22,58 @@ pyop!(StringS, strings, AstString, name: String, size: u32);
pyop!(StringV, stringv, AstString, value: String);
pyop!(StrLen, strlen, AstString, AstString);
pyop!(StrConcat, strconcat, AstString, AstString, AstString);
// pyop!(StrSubstr, strsubstr, AstString, AstString, BV, BV);

#[pyfunction]
pub fn StrSubstr(
py: Python,
base: PyRef<AstString>,
start: PyRef<BV>,
end: PyRef<BV>,
) -> Result<Py<AstString>, ClaripyError> {
py_ast_from_astref(py, GLOBAL_CONTEXT.strsubstr(&get_astref(base), &get_astref(start), &get_astref(end))?)
}

pyop!(StrContains, strcontains, AstString, AstString, AstString);
// pyop!(StrIndexOf, strindexof, AstString, AstString, BV);
// pyop!(StrReplace, strreplace, AstString, AstString, AstString, AstString);

#[pyfunction]
pub fn StrIndexOf(
py: Python,
haystack: PyRef<AstString>,
needle: PyRef<AstString>,
start: PyRef<BV>,
) -> Result<Py<BV>, ClaripyError> {
py_ast_from_astref(py, GLOBAL_CONTEXT.strindexof(&get_astref(haystack), &get_astref(needle), &get_astref(start))?)
}

#[pyfunction]
pub fn StrReplace(
py: Python,
haystack: PyRef<AstString>,
needle: PyRef<AstString>,
replacement: PyRef<AstString>,
) -> Result<Py<AstString>, ClaripyError> {
py_ast_from_astref(py, GLOBAL_CONTEXT.strreplace(&get_astref(haystack), &get_astref(needle), &get_astref(replacement))?)
}

pyop!(StrPrefixOf, strprefixof, AstString, AstString, AstString);
pyop!(StrSuffixOf, strsuffixof, AstString, AstString, AstString);
// pyop!(StrToBV, strtobv, BV, AstString, u32);
// pyop!(BVToStr, bvtostr, AstString, BV);

#[pyfunction]
pub fn StrToBV(
py: Python,
s: PyRef<AstString>,
) -> Result<Py<BV>, ClaripyError> {
py_ast_from_astref(py, GLOBAL_CONTEXT.strtobv(&get_astref(s))?)
}

#[pyfunction]
pub fn BVToStr(
py: Python,
bv: PyRef<BV>,
) -> Result<Py<AstString>, ClaripyError> {
py_ast_from_astref(py, GLOBAL_CONTEXT.bvtostr(&get_astref(bv))?)
}

pyop!(StrIsDigit, strisdigit, AstString, AstString);
pyop!(StrEq, streq, AstString, AstString, AstString);
pyop!(StrNeq, strneq, AstString, AstString, AstString);
Expand All @@ -41,14 +87,14 @@ pub(crate) fn import<'py>(_: Python, m: &Bound<'py, PyModule>) -> PyResult<()> {
StringV,
StrLen,
StrConcat,
// StrSubstr,
StrSubstr,
StrContains,
// StrIndexOf,
// StrReplace,
StrIndexOf,
StrReplace,
StrPrefixOf,
StrSuffixOf,
// StrToBV,
// BVToStr,
StrToBV,
BVToStr,
StrIsDigit,
StrEq,
StrNeq,
Expand Down
10 changes: 5 additions & 5 deletions crates/claripy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ pub fn claripy(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
ast::string::StringV,
ast::string::StrLen,
ast::string::StrConcat,
// ast::string::StrSubstr,
ast::string::StrSubstr,
ast::string::StrContains,
// ast::string::StrIndexOf,
// ast::string::StrReplace,
ast::string::StrIndexOf,
ast::string::StrReplace,
ast::string::StrPrefixOf,
ast::string::StrSuffixOf,
// ast::string::StrToBV,
// ast::string::BVToStr,
ast::string::StrToBV,
ast::string::BVToStr,
ast::string::StrIsDigit,
ast::string::StrEq,
ast::string::StrNeq,
Expand Down
Loading