From 5645f6ab7130b601d025b4a98236465a09392cd5 Mon Sep 17 00:00:00 2001 From: Florian Briand Date: Thu, 4 Jul 2024 19:15:30 +0200 Subject: [PATCH] chore: setup debug project for binding C and Rust --- .gitignore | 17 +------ Cargo.lock | 16 +++++++ Cargo.toml | 8 ++++ README.md | 4 +- build.rs | 5 +++ lib/libp4pillondebuglib.a | Bin 0 -> 3180 bytes lib/p4pillondebuglib.o | Bin 0 -> 2872 bytes makefile | 32 ++++++++++++++ src/main.rs | 90 ++++++++++++++++++++++++++++++++++++++ src/p4pillondebuglib.c | 49 +++++++++++++++++++++ src/p4pillondebuglib.h | 14 ++++++ 11 files changed, 217 insertions(+), 18 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 lib/libp4pillondebuglib.a create mode 100644 lib/p4pillondebuglib.o create mode 100644 makefile create mode 100644 src/main.rs create mode 100644 src/p4pillondebuglib.c create mode 100644 src/p4pillondebuglib.h diff --git a/.gitignore b/.gitignore index 3ca43ae..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1 @@ -# ---> Rust -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb - +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..72980c3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "utils-debug-c-lib" +version = "0.1.0" +dependencies = [ + "libc", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..837c7be --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "utils-debug-c-lib" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +[dependencies] +libc = "0.2" diff --git a/README.md b/README.md index c25e7b4..de13168 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# Krys4lide +## À explorer -Logiciel de pharmacie \ No newline at end of file +- Générer des bindings RUST depuis des .h : https://jvns.ca/blog/2017/12/21/bindgen-is-awesome/ diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..f540504 --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +fn main() { + println!("cargo:rustc-link-search=native=./lib"); + println!("cargo:rustc-link-lib=static=p4pillondebuglib"); + println!("cargo:rerun-if-changed=lib/libp4pillondebuglib.a"); +} diff --git a/lib/libp4pillondebuglib.a b/lib/libp4pillondebuglib.a new file mode 100644 index 0000000000000000000000000000000000000000..f36c97a3b72d8e93fe251d72fdd62620df213248 GIT binary patch literal 3180 zcmc&$O=w(I6uxiLKVuW8#Xqd#ErPVReKYeCO)4dfhOw7YwFIU@sdQduUNSLn<_&M& zK%y-cMBxzvS-No1ovz#oMO*|0gD%9CD;Kj+%)&)o2!RITIq#kGa&tRD(S?WHd(QdJ z&%N*5bMNHj1vjiWr-lo1FO?>oVyWbeoi9oWe?a;yWvQ6zQr3or5QkxG?Khg9?*}58 zuS8)cWb)Nup#ci&Zli$%#kmFFSKiW9*Y9~h6|ZJ4b{IFz74F|{L=Wil+NUYlX}*M->+PLEoWKqxhYv!LR3z8 zS&WJiQC0D;YV{x@Z-0|0AN-zI@vSYjQrS|gkD)=uKdE@*E{dBtt$oLy5c z6+;jP#v6&JTD@X{-dAxxVJnb}Eb-TXKr9+I6&OBTj~-`vr3xu<(f@P3_||sZ+ghn^ z!v|SErqxL?6<6;Oj$d&v&aY*~%Sy6QxqtAtocOk~BfzJxHUw5V5&xzP&!kRSWmaFm zui_uHYA9^tv+}JV^cyFIxHvuivOIP1jp}Jx8negjl5~pBc=1KYk*8+7hE#4uhI6IU zcz{3$U`QCaCx{d|VXY&^WRcFj^e7pucV~pPl($YCJ$z&hj86lO18D@OS4Q(6<)%jt zDR2N4Z5I#+dI2Z4d%O1Qhy!(SVtZ~ealrVShy(3s|0FewWRD#G=5GG4$o?#HwqM(| z|4jBp-JY9E9mr-ftjhuM$Bv1kJ~Z$KBA(o{Jt4T~Aa#RCs!J$Kocr&Nfph<@8#wo0 z-@v*5gxy_kMeZ!{C`@^irO@+TJMxwyVK>~!74~emE9@{xm3q)>d2O(@gUGYt4L#S5 z-1;Kv^X;D934@LoM#~U`nY`wWxzKHSw3GkFB!pw>vBz{Olkx?f3)>7;oB#u!ahV;6 z*XW6wq=1J3M-h7m>B*Fd=Yb=RcS5H>&4tKl!e;@;oc}hy1hJS??=4UTuRn^4M&|nG z!G;*$S9TqugarnOb3F4`0cTlvb<7PJ@$5ST0=_M#`y>J8zPSDz{|*Saz8T*n0RI8j z$&AM?9CZF4Q$u}5!Fc|7-#MPof6&?(QlkUCO~pA17_MW+V@~tz0TO+52D+KDY&L6s S3@n4<%k*IL`^gE+_`d+x!vksn literal 0 HcmV?d00001 diff --git a/lib/p4pillondebuglib.o b/lib/p4pillondebuglib.o new file mode 100644 index 0000000000000000000000000000000000000000..4a1decf2a2ebab8314e4ab6ab64f7afc471c5fe5 GIT binary patch literal 2872 zcmc&!O^6gn6t152Kkhn;KOrWhgCH8U-5nK;1es)+Scn=~7z1K(duMuo-1(!Y2X+%h zAtcZ&3wz8V2XDzKw}6BkmISiIgXEM`4s%elhaB`EEV!_FU-x@6Wid-Y4?d`VufF%a zSJkgx)t$n{Pcn|9L>zTab!UPqb$@f$&L?JG4XA!q)bV3IzY)miza^8e{>|$6_KKb> ztmyeYaOn6i9WUL7xs1=kAHsBrxvb+>n^WkRf)E%lB}_ek#i4my$Js>9Q7-nxzY;>R za4c(be7zX$6??8oDX{44Tra-88n;&FimUWP&Yx*@QcTCiCye7Sp2XROPVqV=St>l= z_)kXtURYDaY3n70DktXul;NF})Tzw;rRO?+XjDT7l}i1n`&m7#l;2b8{P6Hu_t5#v z#l!B<3GakA*qvV4tl}mksLanvG-L*YgGf zPwv?ZA=vXSgP+upu(ueOc~U#V4$#Qbu;mB=$a9(+CiS|#w zSE5ig8{t$f8t(uJ7KN3drm8+MOR5=#rN(SZxLTho69Xi_T*iPoPHX63=Bi(51x-~A zs^CN9+02mLM`Lv@6_;#?jSr@9a=$i_E&L0N+x%+=ANNK0k%4bD@OuWn&A@+V+|K{N z;G6j$8Mv80%jbf(z|8-pft&mv4SWa1$o^feGDX;8DzVgPJP3494bW*BUt3i!?wR#kI z^oAa5Mt*6U=M%M-*9hy4AdF@y2AP7%8{?s04R|N7rd$d~>Ct1RP~P5DpW)19>x5>L6+0xD%#4vln8g4Id>OIbSw@h+>h`>@AuqS$_Z% zL+tfGARjQfuR`tPgvSM7E%Bm1CR}WjnzJ`##S3?Y47@Ei{R{*4zNG#Vf0GQXZ^utE zup&g(c09VU*ZF_T4fO-Z;rwymC0@?I*V+gf{BGR2yWHTdC@N U?ON|7Pp|kqKiKkqN&-9nKTXuX$^ZZW literal 0 HcmV?d00001 diff --git a/makefile b/makefile new file mode 100644 index 0000000..0da93cd --- /dev/null +++ b/makefile @@ -0,0 +1,32 @@ +# Detect the operating system +ifeq ($(OS),Windows_NT) + MKDIR = if not exist $(LIB_DIR) mkdir $(LIB_DIR) + RM = del +else + MKDIR = mkdir -p $(LIB_DIR) + RM = rm -f +endif + +CC = gcc +AR = ar +CFLAGS = -Wall -fPIC +SRC_DIR = src +LIB_DIR = lib +SRC_FILES = $(wildcard $(SRC_DIR)/*.c) +OBJ_FILES = $(SRC_FILES:$(SRC_DIR)/%.c=$(LIB_DIR)/%.o) +STATIC_LIB = $(LIB_DIR)/libp4pillondebuglib.a + +all: $(STATIC_LIB) + +$(STATIC_LIB): $(OBJ_FILES) + $(MKDIR) + $(AR) rcs $@ $^ + +$(LIB_DIR)/%.o: $(SRC_DIR)/%.c + $(MKDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + $(RM) $(OBJ_FILES) $(STATIC_LIB) + +.PHONY: all clean \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6526ca4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,90 @@ +extern crate libc; + +use libc::{ c_char, c_int, c_void, size_t }; +use std::ffi::CStr; +use std::ptr; + +extern { + fn hello() -> *const c_char; + fn helloPtr(result: *mut c_char); + fn helloPtrPtr(result: *mut *mut c_char, size: *mut size_t); + fn helloVoidPtrPtr(result: *mut *mut c_void, size: *mut size_t); + fn add(a: c_int, b: c_int) -> c_int; + fn addPtr(a: c_int, b: c_int, result: *mut c_int); + fn fillHexValues(result: *mut *mut c_void, size: *mut size_t); +} + +fn main() { + + unsafe { + let c_str = hello(); + let r_str = CStr::from_ptr(c_str).to_str().unwrap(); + println!("{} from C", r_str); + } + + let mut buffer: [c_char; 50] = [0; 50]; + unsafe { + helloPtr(buffer.as_mut_ptr()); + let c_str = CStr::from_ptr(buffer.as_ptr()); + let r_str = c_str.to_str().unwrap(); + println!("{} from C Ptr", r_str); + } + + let mut buffer: *mut c_char = ptr::null_mut(); + let mut size: size_t = 0; + unsafe { + helloPtrPtr(&mut buffer, &mut size); + + if !buffer.is_null() { + let c_str = CStr::from_ptr(buffer); + let r_str = c_str.to_str().unwrap(); + println!("{} from C Ptr Ptr (size: {})", r_str, size); + + libc::free(buffer as *mut c_void); + } + } + + let mut buffer: *mut c_void = ptr::null_mut(); + let mut size: size_t = 0; + unsafe { + helloVoidPtrPtr(&mut buffer, &mut size); + + if !buffer.is_null() { + let c_str = CStr::from_ptr(buffer as *const c_char); + let r_str = c_str.to_str().unwrap(); + println!("{} from C Void Ptr Ptr (size: {})", r_str, size); + + libc::free(buffer); + } + } + + unsafe { + let result = add(1, 2); + println!("Result of 1 + 2 = {}", result); + } + + unsafe { + let mut result = 0; + addPtr(1, 2, &mut result); + println!("Result Ptr of 1 + 2 = {}", result); + } + + let mut buffer: *mut c_void = ptr::null_mut(); + let mut size: size_t = 0; + unsafe { + fillHexValues(&mut buffer, &mut size); + + if !buffer.is_null() { + let hex_values = std::slice::from_raw_parts(buffer as *const u8, size); + for &byte in hex_values { + print!("{:02X} ", byte); + } + println!(); + + libc::free(buffer); + } + } + + + +} diff --git a/src/p4pillondebuglib.c b/src/p4pillondebuglib.c new file mode 100644 index 0000000..22348af --- /dev/null +++ b/src/p4pillondebuglib.c @@ -0,0 +1,49 @@ +// mylib.c +#include +#include +#include + +char* hello() { + return "Hello, World!"; +} + +void helloPtr(char *result) { + sprintf(result, "Hello, World!"); +} + +void helloPtrPtr(char **result, size_t *size) { + const char *message = "Hello, World!"; + *size = strlen(message) + 1; // +1 for null terminator + *result = (char *)malloc(*size); + if (*result) { + strcpy(*result, message); + } +} + +void helloVoidPtrPtr(void **result, size_t *size) { + const char *message = "Hello, World!"; + *size = strlen(message) + 1; // +1 for null terminator + *result = malloc(*size); + if (*result != NULL) { + strcpy((char *)*result, message); + } +} + +int add(int a, int b) { + return a + b; +} + +// Add function returning result in a pointer +void addPtr(int a, int b, int *result) { + *result = a + b; +} + +void fillHexValues(void **result, size_t *size) { + unsigned char values[5] = {0x05, 0xE7, 0x02, 0x00, 0x00}; + + *size = sizeof(values); + *result = malloc(*size); + if (*result != NULL) { + memcpy(*result, values, *size); + } +} diff --git a/src/p4pillondebuglib.h b/src/p4pillondebuglib.h new file mode 100644 index 0000000..462098f --- /dev/null +++ b/src/p4pillondebuglib.h @@ -0,0 +1,14 @@ +#ifndef P4PILLONDEBUGLIB_H +#define P4PILLONDEBUGLIB_H + +#include + +char* hello(); +void helloPtr(char *result); +void helloPtrPtr(char **result, size_t *size); +void helloVoidPtrPtr(void **result, size_t *size); +int add(int a, int b); +void addPtr(int a, int b, int *result); +void fillHexValues(void **result, size_t *size); + +#endif // P4PILLONDEBUGLIB_H \ No newline at end of file