Skip to content

Commit

Permalink
restructuring to include ffi and not be a program
Browse files Browse the repository at this point in the history
  • Loading branch information
yashsinghcodes committed Oct 7, 2024
1 parent 20f6fbf commit f0725b9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
23 changes: 23 additions & 0 deletions go/cmd/ffi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

/*
#include <stdio.h>
#include <stdlib.h>
static inline void c_print(const char* s) {
printf("%s\n", s);
fflush(stdout);
}
*/
import "C"
import (
"github.com/Shuffle/indicator-parser/go/parser"
)

//export ParseIOC
func ParseIOC(value *C.char, typesJSON *C.char) *C.char {
goValue := C.GoString(value)
result := parser.ParseIndicatorToJSON(goValue)
return C.CString(result)
}

func main() {}
41 changes: 41 additions & 0 deletions go/parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package parser

import (
"encoding/json"
"fmt"
"log"
"os"

ioc "github.com/Shuffle/indicator-parser/go/ioc"
)

var logger *log.Logger

func init() {
logFile, err := os.OpenFile("ioc_parser.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
logger = log.New(logFile, "", log.LstdFlags)
}

func logPrint(s string) {
logger.Println(s)
fmt.Println(s)
}

// ParseIndicator parses an IOC string and returns the results
func ParseIndicator(value string) []ioc.Indicator {
return ioc.Parse(value, []ioc.IndicatorType{})
}

// ParseIndicatorToJSON parses an IOC string and returns the results as JSON
func ParseIndicatorToJSON(value string) string {
results := ParseIndicator(value)
jsonData, err := json.Marshal(results)
if err != nil {
logPrint(fmt.Sprintf("Error marshaling results: %v", err))
return "[]"
}
return string(jsonData)
}

0 comments on commit f0725b9

Please sign in to comment.