Store binary file as a string inside my code - c

I'm creating a library to provide access to another library.
The library in question is a vendor, so not everyone should access this library.
One solution to do so is to put the library on a specific group (AIX) and then put everyone that can compile using it in the same group.
This solution don't works here because there are a lot of new people coming in and out, and the user that compiles (Its a process that do this operation) is not the same as the user that can access the code.
The solution i'm trying to archive is.
Every application have an pattern on their directory.
/Aplications/Group/...
So i can get the Group using the folder when the program runs.
What i am trying to do is.
Create a library that checks the directory and then loads the library using (loadAndInit) the dlopen function on AIX that already loads all the symbols, so i don't need the dlsym.
The problem is, i want to put the binary code of the library i want to load inside my source code.
WHY: Because if App -> Lib (Loads) -> Vendor, nothing can limit the developer from doing an link by hand or compiling by hand the App -> Vendor.
And the structure that call the compilation process cannot limit that.
What i have tried.
Convertlib
FILE* file=fopen("vendor.so", "rb");
int c;
do{
c=getc(file);
if(c > 0)
printf("\\x%02x", c);
}while(c!=EOF);
Then i have a little script that puts the output of this file as a
char *lib=(char* (malloc(sizeof(char) * /* lib size */));
lib="/*output of the Convertlib*/
Then i try to load it using fmemopen, so i dont create a temporary file just a file descriptor inside my process area.
To load it i do
FILE* vendorLib=fmemopen(lib, /*libSize*/, "r");
char path[50];
sprintf("/proc/%d/fd/%d", getpid(), fileno(vendorLib));
loadAndInit(path, 0, "");
If i call directly a lib (i have it to test) and dont load using the hex formated library it works.
But as i converted my binary code to hex and i'm trying to load it, it dont work.
Should i convert back to load it on the memory and use it as an library again?
This seems the only solution to work with, as the library is a vendor i cannot change it and it is the only way i see to limit the access because we have here more than 1000 programmers

I solved it using
printf("\x%02x", thechar);
Inside the getc loop
So i wrote it inside a memcpy then i copy to the memory so the binary file is stored as HEX data.
Works perfectly

Related

How to call one object file from another using c

I've been writing some simple console application in c and on linux and came across a problem. I need to run one object file from another.
When i want to open a text or binary file i would do something like this:
FILE* fp = fopen(path, mode);
and then just write to that file or read from it. But when it comes to object files i don't even know what to start with.
Globally, the situation is that there is a compiled object file which functionality needs to be tested (lets call it "target"). The test is driven by another compiled object file (let's call it "tester"). So, when i run the "tester" it queries for path to "target". When path is acquired, "tester" tests "target".
So, what i ask here is what this whole situation (coding of object files interaction) is called so i could go and rtfm? I lurked a bit and found some concepts like unit testing and APIs but brief view didn't give me strong impression that those things are what i'm looking for.
Thank you!

Basic UI Gtk with XML interfaces

I'm testing some GTK+ examples.
At some given function, a reference to some path of a XML file appears in
C code. It explains that the code in C is reading the XML content to
later compile it to be usable from the C code:
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/window.ui");
}
I can understand what is happening here, but not how is it reading the source XML? window.ui, in this case. Because the repo has no folder
as they mention (/org/gtk/exampleapp/).
So, in my function I expect to do something like:
static void my_style_window_class_init(MyStyleWindowClass *class) {
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class),
"window.ui");
}
All my XML content is in the same folder as *.c and *.h files. This is a testing decision and have no other meaning.
The _from_resource() part of the function name indicates that the path /org/gtk/exampleapp/window.ui is not a filesystem path, but rather a resource path. Resource paths tie into a feature of GLib called GResource which allows you to embed binary data inside a program or shared library.
You would write an XML file to describe what local files map to what resource paths, and then as part of your build process, you would convert that to a C source file with the glib-compile-resources tool. You then build that C source file into your program. The full details are on the page that I linked in the first paragraph.
(Note that these are not the same as the embedded resources in Windows executables, which use a different technology altogether, but work in similar ways.)
If you want to load something from a file, GLib and GTK+ and other libraries built on them provide a _from_file(), _from_data(), or _from_stream() alternative to the _from_resource() function. _from_file() reads the data from a file directly. _from_data() reads from memory. _from_stream() reads from a GStream, which is an object-oriented I/O endpoint defined by GLib in its GIO module. The function name suffix is optional; it varies.
In the case of gtk_widget_class_set_template_from_resource(), the equivalent provided is gtk_widget_class_set_template(), which follows the _from_data() pattern of reading from memory. The memory is stored in a GBytes object, so you have to read from your local file into the GBytes.
It's an oldie and the question seems answered but I'd like to take a direct approach and place solution - turns out that we can substitute this line
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/gtksourceview/tests/ui/test-widget.ui");
with this line to make the code work.
if (g_file_get_contents("test-widget.ui", &contents, &len, &err) == FALSE)
g_error("error reading test-widget.ui: %s", err->message);
bytes = g_bytes_new_take(contents, len);
gtk_widget_class_set_template(GTK_WIDGET_CLASS(klass), bytes);

Accessing Local Application Files In CLI Enviroment?

I'm not sure how to ask this, but I'll do my best. Anyway, my situation is this: I have files that I want my app/executable to be able to access at all times that are relative to the executable's path or in the same directory as the program. Since I want my program to be able to run in any CWD and still get everything it needs. I want to know what's the best way or method to get a file that's relative to my executable, that won't mess up the user's CWD?
The approach I had in my head is this:
Get program's directory using OS dependent function.
Use some string manipulation to get the program's home directory.
Append to the string the file I require during run time without having to use any hat trick CWD commands.
Not sure if Windows/Linux already has something up it's sleeves to deal with this, but I thought I should know if there is a better alternative.
Thank you!
If the files, that you want to access from your C binary, are in the same directory or subdirectory, you can access directly by using ".", this is the current directory.
or you can simply use the getwd function.
char *getwd(char *buf);
Typically, you can use like this (this snippet will print you, the current directory of the C binary):
int main()
{
char buf[4096];
getwd(buf);
printf(but);
}

Allegro load_bitmap not working

i'm trying to load bitmap like this:
BITMAP *image = load_bitmap("picture.bmp", NULL);
when I test it:
if (image == NULL)
printf("No image loaded\n");
it prints No image loaded so load_bitmap doesn't work ... i have also tried absolute path but still nothing.
Im using Ubuntu and allegro 4.2
Some suggestions?
Did you try placing the image on the same location as the executable? After that is solved check this things also if still getting the error:
Is really a *.bmp file? A file of a different type can not be converted by just renaming it.
Is the file you are trying to read actually called like that? Check for spelling both in code and in the file explorer.
Does the program run correctly if executed from the file explorer or command-line but not from the IDE? If that is the case, then you should change the configuration of the workspace or project you are currently using so that the execution directory is the same as the one where the image file is located.
If all else fails then try following the steps of the tutorial again, perhaps you made something wrong. By the way, if this is your first C++ project I recommend you that instead go to more basic stuff and stick to the command-line for a while until you get the hang of the facilities the language and its libraries have to offer.

Execute a C program from another program in gcc

I need to include a .h file to my project which will be supplied at the runtime. Since .h files are linked at linking time i am unable to include .h file. So i decided to write a dummy program which would create .h file and then i would call my actual program. Is there anyway to do this. Or any other solution is possible. I basically need to create a .h file before my program starts execution and need to link it up to my program.
i actually should take a file which is created by user, parse the file and then create a structure with the fields present in that file.for example if the file contains the following data:-
fno:int:4,fname:char:30,ftype:int:4
then i should create a structure like
struct somename
{
int fno;
char fname[30];
int ftype
};
Then i should be able to create instances of the structure created. This is what i like to do
dlopen is a solution. It allows to load dynamic library at runtime.
Compile your dummy program as a dynamic library.
Make use of dlopen on your .so
Call any function you need as if it has been linked by gcc (see dlsym).
What you can do is:
create .h file
fork
if in child: execve
if in father: wait (or not, depends on what you want to do)
I would use a Makefile; your program would receive the header file at runtime, (perhaps check it?) then execve() the make command passing the name of the file.
However, this sounds very cumbersome; perhaps you are trying to achieve something with the wrong tool. Maybe you want to use some scripting first? Or write two separate programs..? What are you trying to do?

Resources