Skip to content

Commit

Permalink
Unishox3 Alpha golang binding progress
Browse files Browse the repository at this point in the history
  • Loading branch information
siara-cc committed Sep 28, 2023
1 parent a96733a commit ca7c8f3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
22 changes: 21 additions & 1 deletion other_lang_bindings/go/README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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.
2 changes: 2 additions & 0 deletions other_lang_bindings/go/src/unishox/compile_usx3_so.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
g++ -std=c++11 -shared -o libusx3_alpha_wrapper.so usx3_alpha_wrapper.cpp ../../../../Unishox3_Alpha/unishox3.cpp -lmarisa

74 changes: 74 additions & 0 deletions other_lang_bindings/go/src/unishox/main_usx3_alpha.go
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
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 <string to compress>")
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))

}

12 changes: 12 additions & 0 deletions other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions other_lang_bindings/go/src/unishox/usx3_alpha_wrapper.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ca7c8f3

Please sign in to comment.