@@ -22,6 +22,7 @@ limitations under the License.
22
22
23
23
#if PY_VERSION_HEX >= 0x02060000
24
24
#include "bytesobject.h"
25
+ #include "structseq.h"
25
26
#elif PY_VERSION_HEX < 0x02060000
26
27
#define PyBytes_AsString PyString_AsString
27
28
#define PyBytes_Check PyString_Check
@@ -421,6 +422,21 @@ typedef struct _CALLBACK_DATA
421
422
422
423
} CALLBACK_DATA ;
423
424
425
+ static PyStructSequence_Field RuleString_Fields [] = {
426
+ {"namespace" , "Namespace of the rule" },
427
+ {"rule" , "Identifier of the rule" },
428
+ {"string" , "Identifier of the string" },
429
+ {NULL }
430
+ };
431
+
432
+ static PyStructSequence_Desc RuleString_Desc = {
433
+ "RuleString" ,
434
+ "Named tuple tying together rule identifier and string identifier" ,
435
+ RuleString_Fields ,
436
+ (sizeof (RuleString_Fields ) / sizeof (RuleString_Fields [0 ])) - 1
437
+ };
438
+
439
+ static PyTypeObject RuleString_Type = {0 };
424
440
425
441
// Forward declarations for handling module data.
426
442
PyObject * convert_structure_to_python (
@@ -683,7 +699,11 @@ static int handle_too_many_matches(
683
699
PyGILState_STATE gil_state = PyGILState_Ensure ();
684
700
685
701
PyObject * warning_type = NULL ;
686
- PyObject * identifier = NULL ;
702
+ PyObject * string_identifier = NULL ;
703
+ PyObject * rule_identifier = NULL ;
704
+ PyObject * namespace_identifier = NULL ;
705
+ PyObject * rule_string = NULL ;
706
+ YR_RULE * rule = NULL ;
687
707
688
708
int result = CALLBACK_CONTINUE ;
689
709
@@ -705,14 +725,49 @@ static int handle_too_many_matches(
705
725
{
706
726
Py_INCREF (data -> warnings_callback );
707
727
708
- identifier = PyBytes_FromString (string -> identifier );
728
+ string_identifier = PY_STRING (string -> identifier );
729
+
730
+ if (string_identifier == NULL )
731
+ {
732
+ result = CALLBACK_ERROR ;
733
+ goto _exit ;
734
+ }
735
+
736
+ rule = & context -> rules -> rules_table [string -> rule_idx ];
737
+ rule_identifier = PY_STRING (rule -> identifier );
738
+
739
+ if (rule_identifier == NULL )
740
+ {
741
+ result = CALLBACK_ERROR ;
742
+ goto _exit ;
743
+ }
744
+
745
+ namespace_identifier = PY_STRING (rule -> ns -> name );
709
746
710
- if (identifier == NULL )
747
+ if (namespace_identifier == NULL )
711
748
{
712
749
result = CALLBACK_ERROR ;
713
750
goto _exit ;
714
751
}
715
752
753
+ rule_string = PyStructSequence_New (& RuleString_Type );
754
+
755
+ if (rule_string == NULL )
756
+ {
757
+ result = CALLBACK_ERROR ;
758
+ goto _exit ;
759
+ }
760
+
761
+ PyStructSequence_SET_ITEM (rule_string , 0 , namespace_identifier );
762
+ PyStructSequence_SET_ITEM (rule_string , 1 , rule_identifier );
763
+ PyStructSequence_SET_ITEM (rule_string , 2 , string_identifier );
764
+
765
+ // PyStructSequenece steals the reference so we NULL these
766
+ // so that Py_XDECREF() can be used in _exit label
767
+ namespace_identifier = NULL ;
768
+ rule_identifier = NULL ;
769
+ string_identifier = NULL ;
770
+
716
771
warning_type = PyLong_FromLong (CALLBACK_MSG_TOO_MANY_MATCHES );
717
772
718
773
if (warning_type == NULL )
@@ -724,7 +779,7 @@ static int handle_too_many_matches(
724
779
PyObject * callback_result = PyObject_CallFunctionObjArgs (
725
780
data -> warnings_callback ,
726
781
warning_type ,
727
- identifier ,
782
+ rule_string ,
728
783
NULL );
729
784
730
785
if (callback_result != NULL )
@@ -748,7 +803,10 @@ static int handle_too_many_matches(
748
803
749
804
_exit :
750
805
751
- Py_XDECREF (identifier );
806
+ Py_XDECREF (namespace_identifier );
807
+ Py_XDECREF (rule_identifier );
808
+ Py_XDECREF (string_identifier );
809
+ Py_XDECREF (rule_string );
752
810
Py_XDECREF (warning_type );
753
811
Py_XDECREF (data -> warnings_callback );
754
812
@@ -2563,6 +2621,8 @@ MOD_INIT(yara)
2563
2621
if (PyType_Ready (& Match_Type ) < 0 )
2564
2622
return MOD_ERROR_VAL ;
2565
2623
2624
+ PyStructSequence_InitType (& RuleString_Type , & RuleString_Desc );
2625
+
2566
2626
PyModule_AddObject (m , "Rule" , (PyObject * ) & Rule_Type );
2567
2627
PyModule_AddObject (m , "Rules" , (PyObject * ) & Rules_Type );
2568
2628
PyModule_AddObject (m , "Match" , (PyObject * ) & Match_Type );
0 commit comments