Skip to content

Commit

Permalink
refactor: c-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Aug 10, 2023
1 parent 9f45d27 commit 164c925
Show file tree
Hide file tree
Showing 42 changed files with 1,290 additions and 902 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,22 @@ static-library:
-buildmode=c-archive \
-tags="${BUILD_TAGS}" \
-o ./build/lib/libgowaku.a \
./library/
./library/c/
@echo "Static library built:"
sed -i "s/#include <cgo_utils.h>//gi" ./build/lib/libgowaku.h
@ls -la ./build/lib/libgowaku.*

dynamic-library:
@echo "Building shared library..."
rm -f ./build/lib/libgowaku.$(GOBIN_SHARED_LIB_EXT)*
$(GOBIN_SHARED_LIB_CFLAGS) $(GOBIN_SHARED_LIB_CGO_LDFLAGS) ${GOBIN} build \
-buildmode=c-shared \
-tags="${BUILD_TAGS}" \
-o ./build/lib/libgowaku.$(GOBIN_SHARED_LIB_EXT) \
./library/
./library/c/
sed -i "s/#include <cgo_utils.h>//gi" ./build/lib/libgowaku.h
ifeq ($(detected_OS),Linux)
cd ./build/lib && \
ls -lah . && \
mv ./libgowaku.$(GOBIN_SHARED_LIB_EXT) ./libgowaku.$(GOBIN_SHARED_LIB_EXT).0 && \
ln -s ./libgowaku.$(GOBIN_SHARED_LIB_EXT).0 ./libgowaku.$(GOBIN_SHARED_LIB_EXT)
endif
Expand All @@ -152,14 +154,14 @@ mobile-android:
@echo "Android target: ${ANDROID_TARGET} (override with ANDROID_TARGET var)"
gomobile init && \
${GOBIN} get -d golang.org/x/mobile/cmd/gomobile && \
gomobile bind -v -target=android -androidapi=${ANDROID_TARGET} -ldflags="-s -w" -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o ./build/lib/gowaku.aar ./mobile
gomobile bind -v -target=android -androidapi=${ANDROID_TARGET} -ldflags="-s -w" -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o ./build/lib/gowaku.aar ./library/mobile
@echo "Android library built:"
@ls -la ./build/lib/*.aar ./build/lib/*.jar

mobile-ios:
gomobile init && \
${GOBIN} get -d golang.org/x/mobile/cmd/gomobile && \
gomobile bind -target=ios -ldflags="-s -w" -tags="nowatchdog ${BUILD_TAGS}" $(BUILD_FLAGS) -o ./build/lib/Gowaku.xcframework ./mobile
gomobile bind -target=ios -ldflags="-s -w" -tags="nowatchdog ${BUILD_TAGS}" $(BUILD_FLAGS) -o ./build/lib/Gowaku.xcframework ./library/mobile
@echo "IOS library built:"
@ls -la ./build/lib/*.xcframework

Expand Down
2 changes: 1 addition & 1 deletion ci/Jenkinsfile.nix-flake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pipeline {
}
stage('Check') {
steps {
sh 'ldd ./result/bin/library'
sh 'ldd ./result/bin/c'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/c-bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ build:
+ mkdir -p build
$(CC) \
-I../../build/lib/ \
main.c \
main.c base64.c \
../../build/lib/libgowaku.a \
-lm \
-pthread \
Expand Down
152 changes: 152 additions & 0 deletions examples/c-bindings/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@

#include "base64.h"
#include <string.h>

// source: https://nachtimwald.com/2017/11/18/base64-encode-and-decode-in-c/

size_t b64_encoded_size(size_t inlen)
{
size_t ret;

ret = inlen;
if (inlen % 3 != 0)
ret += 3 - (inlen % 3);
ret /= 3;
ret *= 4;

return ret;
}

const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char *b64_encode(const unsigned char *in, size_t len)
{
char *out;
size_t elen;
size_t i;
size_t j;
size_t v;

if (in == NULL || len == 0)
return NULL;

elen = b64_encoded_size(len);
out = malloc(elen+1);
out[elen] = '\0';

for (i=0, j=0; i<len; i+=3, j+=4) {
v = in[i];
v = i+1 < len ? v << 8 | in[i+1] : v << 8;
v = i+2 < len ? v << 8 | in[i+2] : v << 8;

out[j] = b64chars[(v >> 18) & 0x3F];
out[j+1] = b64chars[(v >> 12) & 0x3F];
if (i+1 < len) {
out[j+2] = b64chars[(v >> 6) & 0x3F];
} else {
out[j+2] = '=';
}
if (i+2 < len) {
out[j+3] = b64chars[v & 0x3F];
} else {
out[j+3] = '=';
}
}

return out;
}

size_t b64_decoded_size(const char *in)
{
size_t len;
size_t ret;
size_t i;

if (in == NULL)
return 0;

len = strlen(in);
ret = len / 4 * 3;

for (i=len; i-->0; ) {
if (in[i] == '=') {
ret--;
} else {
break;
}
}

return ret;
}

int b64_isvalidchar(char c)
{
if (c >= '0' && c <= '9')
return 1;
if (c >= 'A' && c <= 'Z')
return 1;
if (c >= 'a' && c <= 'z')
return 1;
if (c == '+' || c == '/' || c == '=')
return 1;
return 0;
}


int b64invs[] = { 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51 };


void b64_generate_decode_table()
{
int inv[80];
size_t i;

memset(inv, -1, sizeof(inv));
for (i=0; i<sizeof(b64chars)-1; i++) {
inv[b64chars[i]-43] = i;
}
}


int b64_decode(const char *in, unsigned char *out, size_t outlen)
{
size_t len;
size_t i;
size_t j;
int v;

if (in == NULL || out == NULL)
return 0;

len = strlen(in);
if (outlen < b64_decoded_size(in) || len % 4 != 0)
return 0;

for (i=0; i<len; i++) {
if (!b64_isvalidchar(in[i])) {
return 0;
}
}

for (i=0, j=0; i<len; i+=4, j+=3) {
v = b64invs[in[i]-43];
v = (v << 6) | b64invs[in[i+1]-43];
v = in[i+2]=='=' ? v << 6 : (v << 6) | b64invs[in[i+2]-43];
v = in[i+3]=='=' ? v << 6 : (v << 6) | b64invs[in[i+3]-43];

out[j] = (v >> 16) & 0xFF;
if (in[i+2] != '=')
out[j+1] = (v >> 8) & 0xFF;
if (in[i+3] != '=')
out[j+2] = v & 0xFF;
}

return 1;
}


15 changes: 15 additions & 0 deletions examples/c-bindings/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#ifndef _BASE64_H_
#define _BASE64_H_

#include <stdlib.h>

size_t b64_encoded_size(size_t inlen);

char *b64_encode(const unsigned char *in, size_t len);

size_t b64_decoded_size(const char *in);

int b64_decode(const char *in, unsigned char *out, size_t outlen);

#endif
Loading

0 comments on commit 164c925

Please sign in to comment.