-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unishox3 Alpha golang binding progress
- Loading branch information
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |