-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_darwin.go
49 lines (40 loc) · 900 Bytes
/
compiler_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package sb
// #cgo LDFLAGS: -lsandbox
// #include <stdlib.h>
// #include "compiler_darwin.h"
import "C"
import (
"bytes"
"errors"
"fmt"
"io"
"unsafe"
)
// Compile reads a sbpl file and compiles it into binary form.
func Compile(in io.Reader, out io.Writer) error {
buf := new(bytes.Buffer)
size, err := buf.ReadFrom(in)
if err != nil {
return err
}
if size == 0 {
return errors.New("input is empty")
}
params := C.sandbox_create_params()
if params == nil {
return errors.New("creating sandbox params")
}
var cerr *C.char
cs := C.CString(buf.String())
defer C.free(unsafe.Pointer(cs))
profile := C.sandbox_compile_string(cs, params, &cerr)
defer C.sandbox_free_profile(profile)
if profile == nil {
return fmt.Errorf("compiling profile: %s", C.GoString(cerr))
}
if profile.blob != nil {
b := C.GoBytes(profile.blob, profile.len)
out.Write(b)
}
return nil
}