예제 #1
0
파일: yara.go 프로젝트: rayyang2000/yara-1
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
}
예제 #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
}