Example #1
0
func (c *Compiler) AddString(ns, rule string) error {
	cns := C.CString(ns)
	crule := C.CString(rule)
	errors := C.yr_compiler_add_string(c.handle, crule, cns)
	C.free(unsafe.Pointer(crule))
	C.free(unsafe.Pointer(cns))

	if errors > 0 {
		return fmt.Errorf("libyara: failed to compile rule")
	}

	return nil
}
Example #2
0
// AddString compiles rules from a string. Rules are added to the
// specified namespace.
func (c *Compiler) AddString(rules string, namespace string) (err error) {
	var ns *C.char
	if namespace != "" {
		ns = C.CString(namespace)
		defer C.free(unsafe.Pointer(ns))
	}
	crules := C.CString(rules)
	defer C.free(unsafe.Pointer(crules))
	id := callbackData.Put(c)
	defer callbackData.Delete(id)
	C.yr_compiler_set_callback(c.cptr, C.YR_COMPILER_CALLBACK_FUNC(C.compilerCallback), unsafe.Pointer(id))
	numErrors := int(C.yr_compiler_add_string(c.cptr, crules, ns))
	if numErrors > 0 {
		var buf [1024]C.char
		msg := C.GoString(C.yr_compiler_get_error_message(
			c.cptr, (*C.char)(unsafe.Pointer(&buf[0])), 1024))
		err = errors.New(msg)
	}
	return
}