c: using copyfile.h for Mac - c

I am new to c. I am playing with the Mac OSX copyfile.h function to move a file using c. Header file found here: http://bit.ly/IGMSec
int copyfile(const char *from, const char *to, copyfile_state_t state, copyfile_flags_t flags);
Here is my code:
#include <copyfile.h>
int main() {
int success;
const char* from = "hello.text";
const char* to = "/toGo/hello.txt";
copyfile_state_t state = copyfile_init();
copyfile_flags_t flags = "COPYFILE_MOVE";
success = copyfile(from, to, state, flags);
printf ("%d\n", success);
exit(0);
}
I added copyfile_init() function to initialize the state. And I get compiling issues now but I think I am getting in the right direction.
$ gcc move.c
Undefined symbols for architecture x86_64:
"_copyfile_init", referenced from:
_main in ccXJr5oN.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Based on what I've seen online this is a linking issue. So I added the link tag but it's not finding the file, and this header is supposed to be in OSX source.
$ gcc move.c -lcopyfile
ld: library not found for -lcopyfile

COPYFILE_MOVE is a flag constant. It doesn't belong in a string. To actually transfer the file's data, you'll also need to set the COPYFILE_DATA flag.
Also, you can leave out the state parameter. (You didn't bother to initialize it anyway.) Just pass NULL instead.
This should work:
#include <stdio.h>
#include <copyfile.h>
int main() {
int success;
const char* from = "hello.text";
const char* to = "/toGo/hello.txt";
copyfile_flags_t flags = COPYFILE_MOVE | COPYFILE_DATA;
success = copyfile(from, to, NULL, flags);
printf ("%d\n", success);
return 0;
}

Related

clang: _rotl32 not found for architecture arm64

On Apple M1, I'm trying to compile the following code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
inline uint32_t rotl32(uint32_t x, int32_t bits)
{
return x<<bits | x>>(32-bits); // C idiom
}
uint32_t bad_hash32(char const *input) {
uint32_t result = 0xC0FF117;
while (*input) {
result ^= *input++;
result = rotl32(result, 5);
}
return result;
}
int main(int argc, char* argv[])
{
char const* const input = argv[1];
printf("%08x\n", bad_hash32(input));
return 0;
}
Command:
gcc bad_hash.c -o bad_hash
It produces the following error:
Undefined symbols for architecture arm64:
"_rotl32", referenced from:
_bad_hash32 in bad_hash-4c8a24.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What's the issue here? I tried to upgrade gcc to the latest.
Clang version:
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
Removing the inline specifier fixes the issue.
Though I still don't know why this happens.

C call function type error

I'm trying to compile this code which call func from "libcfmapi.so" to decrypt "cfg" file
#include <stdlib.h>
#include <stdio.h>
int restorebackup(const char *tmp_cfg_name,const char *xml_cfg_name);
int ATP_CFM_ExtCustomImportEncryptedUserCfgFile(const char *tmp_cfg_name);
int main(int argc, char **argv)
{
int ret;
if(argc < 3)
{
printf("specify temp config file name.\n");
exit(1);
}
ret=restorebackup(argv[1],argv[2]);
return ret;
}
int restorebackup(const char *tmp_cfg_name,const char *xml_cfg_name)
{
int ret=0;
//ret = ATP_CFM_ExtDigVerifyFile(tmp_cfg_name,tmp_cfg_name);
if(ret != 0)
{
printf("Verify File failed.\n");
return ret;
}
ret = ATP_CFM_ExtCustomImportEncryptedUserCfgFile(tmp_cfg_name);
return ret;
}
but got error regarding func type declare
root#kali:~/debian-qemu# gcc h.c -o demo
/tmp/ccVbt5NT.o: In function `restorebackup':
h.c:(.text+0x8c): undefined reference to `ATP_CFM_ExtCustomImportEncryptedUserCfgFile'
collect2: error: ld returned 1 exit status
any help appreciated
The reason you're getting this error is because you're not linking against the required library, libcfmapi.so.
This is not a library you would expect to find in your Debian system as it is unique to the BT device you're trying to hack.
In short - get the lib from your device, cross compile to the device architecture against the lib you extracted from the device and you should be fine.
More information based on Ishay Peled answer:
readelf -s <pulled library> | grep ATP_CFM_ExtCustomImportEncryptedUserCfgFile
I suspect the problem isn't that function you call doesn't exist but rather there are no functions being displayed it is most likely empty just like nm result:
nm: libcfmapi.so: no symbols
do the command without piping to grep, my bet is your output is:
readelf -s libcfmapi.so
Dynamic symbol information is not available for displaying symbols.
If someone knows the way of getting the headers from the file, i believe then you can find your function you require then link and run (i too am trying to use libcfmapi.so, but lack the programming/reversing knowledge required).
when using a library, then must:
include that library in the link statement via
-l cfmapi
include the header file for that library in the source code:
#include <cfmapi.h>

problems compiling xmlrpc-c program

I'm trying out the examples in the xmlrpc-c documentation:
#include <stdio.h>
#include <xmlrpc.h>
#include <xmlrpc_server.h>
//#include <xmlrpc_server_abyss.h>
#include <xmlrpc_abyss.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>
static xmlrpc_value *
sample_add(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int
main (int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
I try to compile using gcc:
gcc source.c
nohting fancy and I get:
/tmp/ccfGuc6A.o: In function sample_add':
source.c:(.text+0x38): undefined reference toxmlrpc_decompose_value'
source.c:(.text+0x6d): undefined reference to xmlrpc_build_value'
/tmp/ccfGuc6A.o: In functionmain':
source.c:(.text+0x96): undefined reference to xmlrpc_env_init'
source.c:(.text+0xa5): undefined reference toxmlrpc_registry_new'
source.c:(.text+0xd8): undefined reference to xmlrpc_registry_add_method'
source.c:(.text+0x117): undefined reference toxmlrpc_server_abyss'
collect2: error: ld returned 1 exit status
these functions exist in the:
/usr/include/xmlrpc-c/base.h
whihc I have referenced:
include
I think I'm not passing the right options to link, I don't know how it's done though.
thanks
You definitely don't pass the correct argument for the linker. Just including a header file doesn't actually make the linker link with the library, you need to use the -l (lower-case L) option to tell the linker which libraries you need to link with, like
gcc source.c -lxmlrpc
I believe that xml-rpc-c comes with a helper program, intended to help you get the linking right. Its documented here
http://xmlrpc-c.sourceforge.net/doc/xmlrpc-c-config.html

When I include git2.h from libgit2, I can access data structures, but not functions

Here is some code from Ben Straub's (link blog) that I am basing this on:
static int do_clone(const char *url, const char *path)
{
git_repository *repo = NULL;
int ret = git_clone(&repo, url, path, NULL);
git_repository_free(repo);
return ret;
}
And here is my code:
#include <git2.h>
int main(void) {
git_repository *out = NULL;
git_clone(&out, "https://github.com/lehitoskin/racketball", "/home/maxwell", NULL);
return 0;
}
I am very inexperienced with C, so I apologize for having such elementary problems. Anyway, here is the error my compiler gives me:
maxwell#max-pc ~ $ gcc -I libgit2/include gitfun.c
/tmp/ccB64nPh.o: In function `main':
gitfun.c:(.text+0x31): undefined reference to `git_clone'
collect2: error: ld returned 1 exit status
Why can't I call git_clone this way?
It looks like you didn't link to the library. Add -lgit2 if libgit2 is the lib name.
gcc -I libgit2/include gitfun.c -L<path to lib> -l<libname minus the "lib" part>
IOW, you compile fine but when the linker goes looking for git_clone it can't find it because you haven't specified the library that it is in.

Reversing a string in C x86 error

I am trying to do a simple strrev on a string and I keep getting this error when I compile it on my mac
Undefined symbols for architecture x86_64:
"_strrev", referenced from:
_main in cc1zSAum.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
My code is:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]){
char str[] = "Hello world";
char * test;
test = strrev(str);
printf("%s",test);
return 0;
}
I tried playing around with the strrev line
but nothing is working
Any help would be appreciated
Thanks
There's no standard C function by that name. I did a little Googling and it looks like a few compilers and/or C library implementations have included such a function as a non-standard extension, but you can't expect any arbitrary library to have one.
Would be simple enough to write your own, though -- could be a one line loop body, even.

Resources