@@ -8,25 +8,94 @@ package yara
8
8
9
9
// #include <yara.h>
10
10
import "C"
11
- import "strconv"
11
+ import (
12
+ "fmt"
13
+ )
12
14
13
15
// Error encapsulates the C API error codes.
14
- type Error int
16
+ type Error struct {
17
+ // YARA error code.
18
+ Code int
19
+ // Namespace in which the error occurred, if applicable. It can be empty.
20
+ Namespace string
21
+ // Rule in which the error occurred, if applicable. It can be empty.
22
+ RuleIdentifier string
23
+ // String in which the error occurred, if applicable. It can be empty.
24
+ StringIdentifier string
25
+ }
15
26
16
- func (e Error ) Error () string {
17
- if str , ok := errorStrings [int (e )]; ok {
27
+ func (e Error ) Error () (errorString string ) {
28
+ if e .Namespace != "" && e .RuleIdentifier != "" {
29
+ errorString = fmt .Sprintf ("%s caused by rule \" %s:%s\" " ,
30
+ errorCodeToString (e .Code ), e .Namespace , e .RuleIdentifier )
31
+ if e .StringIdentifier != "" {
32
+ errorString += fmt .Sprintf (" string %s" , e .StringIdentifier )
33
+ }
34
+ } else {
35
+ errorString = errorCodeToString (e .Code )
36
+ }
37
+ return errorString
38
+ }
39
+
40
+ func errorCodeToString (errorCode int ) string {
41
+ if str , ok := errorStrings [errorCode ]; ok {
18
42
return str
19
43
}
20
- return "unknown YARA error " + strconv . Itoa ( int ( e ) )
44
+ return fmt . Sprintf ( "unknown error %d" , errorCode )
21
45
}
22
46
23
47
func newError (code C.int ) error {
24
- if code != 0 {
25
- return Error ( code )
48
+ if code == C . ERROR_SUCCESS {
49
+ return nil
26
50
}
27
- return nil
51
+ return Error { Code : int ( code )}
28
52
}
29
53
54
+ const (
55
+ ERROR_SUCCESS = C .ERROR_SUCCESS
56
+ ERROR_INSUFFICIENT_MEMORY = C .ERROR_INSUFFICIENT_MEMORY
57
+ ERROR_COULD_NOT_ATTACH_TO_PROCESS = C .ERROR_COULD_NOT_ATTACH_TO_PROCESS
58
+ ERROR_COULD_NOT_OPEN_FILE = C .ERROR_COULD_NOT_OPEN_FILE
59
+ ERROR_COULD_NOT_MAP_FILE = C .ERROR_COULD_NOT_MAP_FILE
60
+ ERROR_INVALID_FILE = C .ERROR_INVALID_FILE
61
+ ERROR_CORRUPT_FILE = C .ERROR_CORRUPT_FILE
62
+ ERROR_UNSUPPORTED_FILE_VERSION = C .ERROR_UNSUPPORTED_FILE_VERSION
63
+ ERROR_INVALID_REGULAR_EXPRESSION = C .ERROR_INVALID_REGULAR_EXPRESSION
64
+ ERROR_INVALID_HEX_STRING = C .ERROR_INVALID_HEX_STRING
65
+ ERROR_SYNTAX_ERROR = C .ERROR_SYNTAX_ERROR
66
+ ERROR_LOOP_NESTING_LIMIT_EXCEEDED = C .ERROR_LOOP_NESTING_LIMIT_EXCEEDED
67
+ ERROR_DUPLICATED_LOOP_IDENTIFIER = C .ERROR_DUPLICATED_LOOP_IDENTIFIER
68
+ ERROR_DUPLICATED_IDENTIFIER = C .ERROR_DUPLICATED_IDENTIFIER
69
+ ERROR_DUPLICATED_TAG_IDENTIFIER = C .ERROR_DUPLICATED_TAG_IDENTIFIER
70
+ ERROR_DUPLICATED_META_IDENTIFIER = C .ERROR_DUPLICATED_META_IDENTIFIER
71
+ ERROR_DUPLICATED_STRING_IDENTIFIER = C .ERROR_DUPLICATED_STRING_IDENTIFIER
72
+ ERROR_UNREFERENCED_STRING = C .ERROR_UNREFERENCED_STRING
73
+ ERROR_UNDEFINED_STRING = C .ERROR_UNDEFINED_STRING
74
+ ERROR_UNDEFINED_IDENTIFIER = C .ERROR_UNDEFINED_IDENTIFIER
75
+ ERROR_MISPLACED_ANONYMOUS_STRING = C .ERROR_MISPLACED_ANONYMOUS_STRING
76
+ ERROR_INCLUDES_CIRCULAR_REFERENCE = C .ERROR_INCLUDES_CIRCULAR_REFERENCE
77
+ ERROR_INCLUDE_DEPTH_EXCEEDED = C .ERROR_INCLUDE_DEPTH_EXCEEDED
78
+ ERROR_WRONG_TYPE = C .ERROR_WRONG_TYPE
79
+ ERROR_EXEC_STACK_OVERFLOW = C .ERROR_EXEC_STACK_OVERFLOW
80
+ ERROR_SCAN_TIMEOUT = C .ERROR_SCAN_TIMEOUT
81
+ ERROR_TOO_MANY_SCAN_THREADS = C .ERROR_TOO_MANY_SCAN_THREADS
82
+ ERROR_CALLBACK_ERROR = C .ERROR_CALLBACK_ERROR
83
+ ERROR_INVALID_ARGUMENT = C .ERROR_INVALID_ARGUMENT
84
+ ERROR_TOO_MANY_MATCHES = C .ERROR_TOO_MANY_MATCHES
85
+ ERROR_INTERNAL_FATAL_ERROR = C .ERROR_INTERNAL_FATAL_ERROR
86
+ ERROR_NESTED_FOR_OF_LOOP = C .ERROR_NESTED_FOR_OF_LOOP
87
+ ERROR_INVALID_FIELD_NAME = C .ERROR_INVALID_FIELD_NAME
88
+ ERROR_UNKNOWN_MODULE = C .ERROR_UNKNOWN_MODULE
89
+ ERROR_NOT_A_STRUCTURE = C .ERROR_NOT_A_STRUCTURE
90
+ ERROR_NOT_INDEXABLE = C .ERROR_NOT_INDEXABLE
91
+ ERROR_NOT_A_FUNCTION = C .ERROR_NOT_A_FUNCTION
92
+ ERROR_INVALID_FORMAT = C .ERROR_INVALID_FORMAT
93
+ ERROR_TOO_MANY_ARGUMENTS = C .ERROR_TOO_MANY_ARGUMENTS
94
+ ERROR_WRONG_ARGUMENTS = C .ERROR_WRONG_ARGUMENTS
95
+ ERROR_WRONG_RETURN_TYPE = C .ERROR_WRONG_RETURN_TYPE
96
+ ERROR_DUPLICATED_STRUCTURE_MEMBER = C .ERROR_DUPLICATED_STRUCTURE_MEMBER
97
+ )
98
+
30
99
// FIXME: This should be generated from yara/error.h
31
100
var errorStrings = map [int ]string {
32
101
C .ERROR_INSUFICIENT_MEMORY : "insufficient memory" ,
0 commit comments