Class: Yara::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/yara/compiler.rb

Overview

Public: Wrapper around the YARA-X YRX_COMPILER API.

This class allows adding multiple sources, defining globals before compilation, and building a YRX_RULES object that can be used by Scanner.

Defined Under Namespace

Classes: CompileError

Instance Method Summary collapse

Constructor Details

#initialize(flags = 0) ⇒ Compiler

Returns a new instance of Compiler.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/yara/compiler.rb', line 10

def initialize(flags = 0)
  @compiler_ptr_holder = ::FFI::MemoryPointer.new(:pointer)
  result = Yara::FFI.yrx_compiler_create(flags, @compiler_ptr_holder)
  if result != Yara::FFI.yrx_last_error && result != Yara::FFI::YRX_SUCCESS
    # Defensive: use yrx_last_error for message but prefer success check
  end

  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to create compiler: #{Yara::FFI.yrx_last_error}"
  end
  @compiler = @compiler_ptr_holder.get_pointer(0)
end

Instance Method Details

#add_source(src, origin = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yara/compiler.rb', line 23

def add_source(src, origin = nil)
  if origin
    result = Yara::FFI.yrx_compiler_add_source_with_origin(@compiler, src, origin)
  else
    result = Yara::FFI.yrx_compiler_add_source(@compiler, src)
  end

  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to add source: #{Yara::FFI.yrx_last_error}"
  end
  nil
end

#buildObject

Build and return a pointer to YRX_RULES. The caller is responsible for calling yrx_rules_destroy on the returned pointer when finished.



70
71
72
73
74
75
76
# File 'lib/yara/compiler.rb', line 70

def build
  rules_ptr = Yara::FFI.yrx_compiler_build(@compiler)
  if rules_ptr.nil? || rules_ptr.null?
    raise CompileError, "Failed to build rules: #{Yara::FFI.yrx_last_error}"
  end
  rules_ptr
end

#build_serializedObject

Public: Build and return a serialized representation of compiled rules.

Compiles the currently added sources (via add_source) into a YRX_RULES object and serializes it into a binary blob suitable for persistence or transport. The returned String contains the serialized rules and can be deserialized later with Yara::Scanner.from_serialized or the underlying yrx_rules_deserialize FFI call.

Returns a String containing binary serialized rules (raw bytes). Raises CompileError if compilation or serialization fails.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/yara/compiler.rb', line 88

def build_serialized
  rules_ptr = build

  buf_ptr_holder = ::FFI::MemoryPointer.new(:pointer)
  result = Yara::FFI.yrx_rules_serialize(rules_ptr, buf_ptr_holder)

  # Destroy the rules pointer after serialization (we own it from build)
  Yara::FFI.yrx_rules_destroy(rules_ptr)

  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to serialize rules: #{Yara::FFI.yrx_last_error}"
  end

  buf_ptr = buf_ptr_holder.get_pointer(0)
  buffer = Yara::FFI::YRX_BUFFER.new(buf_ptr)
  begin
    data_ptr = buffer[:data]
    length = buffer[:length]
    str = data_ptr.get_bytes(0, length)
    return str
  ensure
    Yara::FFI.yrx_buffer_destroy(buf_ptr)
  end
end

#define_global_bool(ident, value) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/yara/compiler.rb', line 44

def define_global_bool(ident, value)
  result = Yara::FFI.yrx_compiler_define_global_bool(@compiler, ident, !!value)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to define global bool #{ident}: #{Yara::FFI.yrx_last_error}"
  end
  nil
end

#define_global_float(ident, value) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/yara/compiler.rb', line 60

def define_global_float(ident, value)
  result = Yara::FFI.yrx_compiler_define_global_float(@compiler, ident, value)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to define global float #{ident}: #{Yara::FFI.yrx_last_error}"
  end
  nil
end

#define_global_int(ident, value) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/yara/compiler.rb', line 52

def define_global_int(ident, value)
  result = Yara::FFI.yrx_compiler_define_global_int(@compiler, ident, value)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to define global int #{ident}: #{Yara::FFI.yrx_last_error}"
  end
  nil
end

#define_global_str(ident, value) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/yara/compiler.rb', line 36

def define_global_str(ident, value)
  result = Yara::FFI.yrx_compiler_define_global_str(@compiler, ident, value)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to define global str #{ident}: #{Yara::FFI.yrx_last_error}"
  end
  nil
end

#destroyObject



151
152
153
154
# File 'lib/yara/compiler.rb', line 151

def destroy
  Yara::FFI.yrx_compiler_destroy(@compiler) if @compiler
  @compiler = nil
end

#errors_jsonObject

Return compilation errors as a parsed JSON object (Array of error objects). This uses yrx_compiler_errors_json which returns a YRX_BUFFER containing the JSON serialization. The buffer is destroyed after being converted.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/yara/compiler.rb', line 116

def errors_json
  buf_ptr_holder = ::FFI::MemoryPointer.new(:pointer)
  result = Yara::FFI.yrx_compiler_errors_json(@compiler, buf_ptr_holder)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to get errors JSON: #{Yara::FFI.yrx_last_error}"
  end

  buf_ptr = buf_ptr_holder.get_pointer(0)
  buffer = Yara::FFI::YRX_BUFFER.new(buf_ptr)
  data_ptr = buffer[:data]
  length = buffer[:length]
  json_str = data_ptr.read_string_length(length)
  Yara::FFI.yrx_buffer_destroy(buf_ptr)

  JSON.parse(json_str)
end

#finalizeObject

Ensure resources are cleaned up.



157
158
159
# File 'lib/yara/compiler.rb', line 157

def finalize
  destroy
end

#warnings_jsonObject

Return compilation warnings as parsed JSON (Array of warning objects).



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/yara/compiler.rb', line 134

def warnings_json
  buf_ptr_holder = ::FFI::MemoryPointer.new(:pointer)
  result = Yara::FFI.yrx_compiler_warnings_json(@compiler, buf_ptr_holder)
  if result != Yara::FFI::YRX_SUCCESS
    raise CompileError, "Failed to get warnings JSON: #{Yara::FFI.yrx_last_error}"
  end

  buf_ptr = buf_ptr_holder.get_pointer(0)
  buffer = Yara::FFI::YRX_BUFFER.new(buf_ptr)
  data_ptr = buffer[:data]
  length = buffer[:length]
  json_str = data_ptr.read_string_length(length)
  Yara::FFI.yrx_buffer_destroy(buf_ptr)

  JSON.parse(json_str)
end