@@ -1232,7 +1232,7 @@ int process_compile_externals(
1232
1232
1233
1233
int process_match_externals (
1234
1234
PyObject * externals ,
1235
- YR_RULES * rules )
1235
+ YR_SCANNER * scanner )
1236
1236
{
1237
1237
PyObject * key ;
1238
1238
PyObject * value ;
@@ -1247,8 +1247,8 @@ int process_match_externals(
1247
1247
1248
1248
if (PyBool_Check (value ))
1249
1249
{
1250
- result = yr_rules_define_boolean_variable (
1251
- rules ,
1250
+ result = yr_scanner_define_boolean_variable (
1251
+ scanner ,
1252
1252
identifier ,
1253
1253
PyObject_IsTrue (value ));
1254
1254
}
@@ -1258,15 +1258,15 @@ int process_match_externals(
1258
1258
else if (PyLong_Check (value ) || PyInt_Check (value ))
1259
1259
#endif
1260
1260
{
1261
- result = yr_rules_define_integer_variable (
1262
- rules ,
1261
+ result = yr_scanner_define_integer_variable (
1262
+ scanner ,
1263
1263
identifier ,
1264
1264
PyLong_AsLongLong (value ));
1265
1265
}
1266
1266
else if (PyFloat_Check (value ))
1267
1267
{
1268
- result = yr_rules_define_float_variable (
1269
- rules ,
1268
+ result = yr_scanner_define_float_variable (
1269
+ scanner ,
1270
1270
identifier ,
1271
1271
PyFloat_AsDouble (value ));
1272
1272
}
@@ -1277,8 +1277,8 @@ int process_match_externals(
1277
1277
if (str == NULL )
1278
1278
return ERROR_INVALID_ARGUMENT ;
1279
1279
1280
- result = yr_rules_define_string_variable (
1281
- rules , identifier , str );
1280
+ result = yr_scanner_define_string_variable (
1281
+ scanner , identifier , str );
1282
1282
}
1283
1283
else
1284
1284
{
@@ -1289,7 +1289,7 @@ int process_match_externals(
1289
1289
return ERROR_INVALID_ARGUMENT ;
1290
1290
}
1291
1291
1292
- // yr_rules_define_xxx_variable returns ERROR_INVALID_ARGUMENT if the
1292
+ // yr_scanner_define_xxx_variable returns ERROR_INVALID_ARGUMENT if the
1293
1293
// variable wasn't previously defined in the compilation phase. Ignore
1294
1294
// those errors because we don't want the "scan" method being aborted
1295
1295
// because of the "externals" dictionary having more keys than those used
@@ -1644,46 +1644,49 @@ static PyObject* Rules_match(
1644
1644
}
1645
1645
}
1646
1646
1647
+ YR_SCANNER * scanner ;
1648
+ if (yr_scanner_create (object -> rules , & scanner ) != 0 ) {
1649
+ return PyErr_Format (
1650
+ PyExc_Exception ,
1651
+ "could not create scanner" );
1652
+ }
1653
+
1647
1654
if (externals != NULL && externals != Py_None )
1648
1655
{
1649
1656
if (PyDict_Check (externals ))
1650
1657
{
1651
- if (process_match_externals (externals , object -> rules ) != ERROR_SUCCESS )
1658
+ if (process_match_externals (externals , scanner ) != ERROR_SUCCESS )
1652
1659
{
1653
- // Restore original externals provided during compiling.
1654
- process_match_externals (object -> externals , object -> rules );
1655
-
1656
1660
PyBuffer_Release (& data );
1661
+ yr_scanner_destroy (scanner );
1657
1662
return NULL ;
1658
1663
}
1659
1664
}
1660
1665
else
1661
1666
{
1662
1667
PyBuffer_Release (& data );
1668
+ yr_scanner_destroy (scanner );
1663
1669
return PyErr_Format (
1664
1670
PyExc_TypeError ,
1665
1671
"'externals' must be a dictionary" );
1666
1672
}
1667
1673
}
1668
1674
1669
- if (fast != NULL )
1675
+ if (fast != NULL && PyObject_IsTrue ( fast ) == 1 )
1670
1676
{
1671
- fast_mode = ( PyObject_IsTrue ( fast ) == 1 );
1677
+ yr_scanner_set_flags ( scanner , SCAN_FLAGS_FAST_MODE );
1672
1678
}
1673
1679
1680
+ yr_scanner_set_timeout (scanner , timeout );
1681
+ yr_scanner_set_callback (scanner , yara_callback , & callback_data );
1682
+
1674
1683
if (filepath != NULL )
1675
1684
{
1676
1685
callback_data .matches = PyList_New (0 );
1677
1686
1678
1687
Py_BEGIN_ALLOW_THREADS
1679
1688
1680
- error = yr_rules_scan_file (
1681
- object -> rules ,
1682
- filepath ,
1683
- fast_mode ? SCAN_FLAGS_FAST_MODE : 0 ,
1684
- yara_callback ,
1685
- & callback_data ,
1686
- timeout );
1689
+ error = yr_scanner_scan_file (scanner , filepath );
1687
1690
1688
1691
Py_END_ALLOW_THREADS
1689
1692
}
@@ -1693,14 +1696,10 @@ static PyObject* Rules_match(
1693
1696
1694
1697
Py_BEGIN_ALLOW_THREADS
1695
1698
1696
- error = yr_rules_scan_mem (
1697
- object -> rules ,
1699
+ error = yr_scanner_scan_mem (
1700
+ scanner ,
1698
1701
(unsigned char * ) data .buf ,
1699
- (size_t ) data .len ,
1700
- fast_mode ? SCAN_FLAGS_FAST_MODE : 0 ,
1701
- yara_callback ,
1702
- & callback_data ,
1703
- timeout );
1702
+ (size_t ) data .len );
1704
1703
1705
1704
Py_END_ALLOW_THREADS
1706
1705
}
@@ -1710,29 +1709,13 @@ static PyObject* Rules_match(
1710
1709
1711
1710
Py_BEGIN_ALLOW_THREADS
1712
1711
1713
- error = yr_rules_scan_proc (
1714
- object -> rules ,
1715
- pid ,
1716
- fast_mode ? SCAN_FLAGS_FAST_MODE : 0 ,
1717
- yara_callback ,
1718
- & callback_data ,
1719
- timeout );
1712
+ error = yr_scanner_scan_proc (scanner , pid );
1720
1713
1721
1714
Py_END_ALLOW_THREADS
1722
1715
}
1723
1716
1724
1717
PyBuffer_Release (& data );
1725
-
1726
- // Restore original externals provided during compiling.
1727
- if (object -> externals != NULL )
1728
- {
1729
- if (process_match_externals (
1730
- object -> externals , object -> rules ) != ERROR_SUCCESS )
1731
- {
1732
- Py_DECREF (callback_data .matches );
1733
- return NULL ;
1734
- }
1735
- }
1718
+ yr_scanner_destroy (scanner );
1736
1719
1737
1720
if (error != ERROR_SUCCESS )
1738
1721
{
0 commit comments