diff --git a/other_lang_bindings/go/README.md b/other_lang_bindings/go/README.md index 88dbe7a..3ac1412 100644 --- a/other_lang_bindings/go/README.md +++ b/other_lang_bindings/go/README.md @@ -1,6 +1,8 @@ # Golang binding for Unishox -Please see `main.go` under `src/unishox`, which uses `cgo` for calling the Unishox C Library. +## Unishox 2 + +Please see `main.go` under `src/unishox`, which uses `cgo` for calling the Unishox2 C Library. To run the example, please change folder to `src/unishox` and execute: @@ -15,4 +17,22 @@ It works with the entire Unicode character set. For example: ``` go main.go "Hello World 🙂🙂" ``` +## Unishox 3 Alpha + +For using Unishox3 Alpha, please see `main_usx3_alpha.go` under `src/unishox`, which uses `cgo` for calling the Unishox3 C++ Library. + +At first, the C++ library needs to be compile to a shared so file using the given sh file, after chaing folder to `src/unishox`: + +``` +sh compile_usx3_so.sh +``` +Before compiling, please ensure `marisa` library is installed using `sudo port install marisa-trie` or `sudo yum -y install marisa` depending on your OS. + +To run the example, execute: + +``` +go run main_usx3_alpha.go "Hello World" +``` + +and it should show the compressed bytes and uncompressed original string. diff --git a/other_lang_bindings/go/src/unishox/compile_usx3_so.sh b/other_lang_bindings/go/src/unishox/compile_usx3_so.sh new file mode 100644 index 0000000..faab944 --- /dev/null +++ b/other_lang_bindings/go/src/unishox/compile_usx3_so.sh @@ -0,0 +1,2 @@ +g++ -std=c++11 -shared -o libusx3_alpha_wrapper.so usx3_alpha_wrapper.cpp ../../../../Unishox3_Alpha/unishox3.cpp -lmarisa + diff --git a/other_lang_bindings/go/src/unishox/main_usx3_alpha.go b/other_lang_bindings/go/src/unishox/main_usx3_alpha.go new file mode 100644 index 0000000..de4b84e --- /dev/null +++ b/other_lang_bindings/go/src/unishox/main_usx3_alpha.go @@ -0,0 +1,74 @@ +// main.go +package main + +// #cgo CFLAGS: -g -Wall +// #cgo LDFLAGS: -L. -lusx3_alpha_wrapper -lstdc++ +// #include "usx3_alpha_wrapper.h" +// #include +import "C" +import ( + "os" + "fmt" + "unsafe" +) + +func compress(str_to_compress string) []byte { + + cstr := C.CString(str_to_compress) + defer C.free(unsafe.Pointer(cstr)) + clen := C.int(len(str_to_compress)) + //defer C.free(unsafe.Pointer(&clen)) // Not sure if clen to be freed + + // Allocate sufficient buffer for holding compressed data + // should be a little larger than the input string for safety + ptr := C.malloc(C.sizeof_char * 1000) + defer C.free(unsafe.Pointer(ptr)) + + size := C.unishox3a_compress_simple(cstr, clen, (*C.char)(ptr)) + + compressed_bytes := C.GoBytes(ptr, size) + return compressed_bytes + +} + +func decompress(compressed_bytes []byte) string { + + cbytes := C.CBytes(compressed_bytes) + defer C.free(unsafe.Pointer(cbytes)) + + // Allocate sufficient buffer for receiving decompressed string + ptr := C.malloc(C.sizeof_char * 1000) + defer C.free(unsafe.Pointer(ptr)) + dlen := C.int(len(compressed_bytes)) + // defer C.free(unsafe.Pointer(&dlen)) // Not sure if dlen to be freed + + str_size := C.unishox3a_decompress_simple((*C.char)(cbytes), dlen, (*C.char) (ptr)) + + orig_string := C.GoStringN((*C.char)(ptr), str_size) + + return orig_string + +} + +func main() { + + if (len(os.Args) < 2) { + fmt.Println("Usage: go run main.go ") + return + } + + compressed_bytes := compress(os.Args[1]) + fmt.Print("Compressed bytes: ") + fmt.Println(compressed_bytes) + fmt.Print("Length: ") + fmt.Println(len(compressed_bytes)) + println() + + orig_string := decompress(compressed_bytes) + fmt.Print("Original String: ") + fmt.Println(orig_string) + fmt.Print("Length (utf-8 bytes): ") + fmt.Println(len(orig_string)) + +} + diff --git a/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.cpp b/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.cpp new file mode 100644 index 0000000..e08a2cb --- /dev/null +++ b/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.cpp @@ -0,0 +1,12 @@ +#include "../../../../Unishox3_Alpha/unishox3.h" + +unishox3 usx3; +extern "C" { +int unishox3a_compress_simple(const char *in, int len, char *out) { + return usx3.compress(in, len, out); +} + +int unishox3a_decompress_simple(const char *in, int len, char *out) { + return usx3.decompress(in, len, out); +} +} diff --git a/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.h b/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.h new file mode 100644 index 0000000..f025bc5 --- /dev/null +++ b/other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.h @@ -0,0 +1,2 @@ +int unishox3a_compress_simple(const char *in, int len, char *out); +int unishox3a_decompress_simple(const char *in, int len, char *out);