mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-07-30 03:28:32 -07:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
e88c04f924
15 changed files with 818 additions and 45 deletions
|
@ -100,7 +100,7 @@ $(OBJDIR)/fullimage.data.o: $(OBJDIR)/fullimage.data.bin.z
|
||||||
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=compressed_data $^ $@
|
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=compressed_data $^ $@
|
||||||
|
|
||||||
$(OBJDIR)/fullimage.elf: $(OBJDIR)/fullimage.nodata.o $(OBJDIR)/fullimage.data.o
|
$(OBJDIR)/fullimage.elf: $(OBJDIR)/fullimage.nodata.o $(OBJDIR)/fullimage.data.o
|
||||||
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^
|
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-e,_osimage_entry,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^
|
||||||
|
|
||||||
tarbin: $(OBJS)
|
tarbin: $(OBJS)
|
||||||
$(TAR) $(TARFLAGS) ../proxmark3-$(platform)-bin.tar $(OBJS:%=armsrc/%) $(OBJS:%.s19=armsrc/%.elf)
|
$(TAR) $(TARFLAGS) ../proxmark3-$(platform)-bin.tar $(OBJS:%=armsrc/%) $(OBJS:%.s19=armsrc/%.elf)
|
||||||
|
|
|
@ -340,7 +340,7 @@ static void hitag2_handle_reader_command(byte_t* rx, const size_t rxlen, byte_t*
|
||||||
|
|
||||||
// Unknown command
|
// Unknown command
|
||||||
default:
|
default:
|
||||||
Dbprintf("Uknown command: %02x %02x",rx[0],rx[1]);
|
Dbprintf("Unknown command: %02x %02x",rx[0],rx[1]);
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1450,6 +1450,6 @@ void ReaderHitag(hitag_function htf, hitag_data* htd) {
|
||||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||||
//Dbprintf("frame received: %d",frame_count);
|
//Dbprintf("frame received: %d",frame_count);
|
||||||
//DbpString("All done");
|
//DbpString("All done");
|
||||||
cmd_send(CMD_ACK,bSuccessful,0,0,(byte_t*)tag.sectors,48);
|
cmd_send(CMD_ACK,bSuccessful,0,0,(byte_t*)tag.sectors,48);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,7 @@ CMDSRCS = nonce2key/crapto1.c\
|
||||||
loclass/ikeys.c \
|
loclass/ikeys.c \
|
||||||
loclass/elite_crack.c\
|
loclass/elite_crack.c\
|
||||||
loclass/fileutils.c\
|
loclass/fileutils.c\
|
||||||
|
whereami.c\
|
||||||
mifarehost.c\
|
mifarehost.c\
|
||||||
crc.c \
|
crc.c \
|
||||||
crc16.c \
|
crc16.c \
|
||||||
|
|
|
@ -35,8 +35,10 @@ int CmdHF14AMifare(const char *Cmd)
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
|
|
||||||
//flush queue
|
//flush queue
|
||||||
while (ukbhit()) getchar();
|
while (ukbhit()) {
|
||||||
|
int c = getchar(); (void) c;
|
||||||
|
}
|
||||||
|
|
||||||
// wait cycle
|
// wait cycle
|
||||||
while (true) {
|
while (true) {
|
||||||
printf(".");
|
printf(".");
|
||||||
|
@ -303,7 +305,8 @@ int CmdHF14AMfDump(const char *Cmd)
|
||||||
|
|
||||||
// Read keys A from file
|
// Read keys A from file
|
||||||
for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
|
for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
|
||||||
if (fread( keyA[sectorNo], 1, 6, fin ) == 0) {
|
size_t bytes_read = fread(keyA[sectorNo], 1, 6, fin);
|
||||||
|
if (bytes_read != 6) {
|
||||||
PrintAndLog("File reading error.");
|
PrintAndLog("File reading error.");
|
||||||
fclose(fin);
|
fclose(fin);
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -312,7 +315,8 @@ int CmdHF14AMfDump(const char *Cmd)
|
||||||
|
|
||||||
// Read keys B from file
|
// Read keys B from file
|
||||||
for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
|
for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
|
||||||
if (fread( keyB[sectorNo], 1, 6, fin ) == 0) {
|
size_t bytes_read = fread(keyB[sectorNo], 1, 6, fin);
|
||||||
|
if (bytes_read != 6) {
|
||||||
PrintAndLog("File reading error.");
|
PrintAndLog("File reading error.");
|
||||||
fclose(fin);
|
fclose(fin);
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -467,16 +471,17 @@ int CmdHF14AMfRestore(const char *Cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
|
for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
|
||||||
if (fread(keyA[sectorNo], 1, 6, fkeys) == 0) {
|
size_t bytes_read = fread(keyA[sectorNo], 1, 6, fkeys);
|
||||||
|
if (bytes_read != 6) {
|
||||||
PrintAndLog("File reading error (dumpkeys.bin).");
|
PrintAndLog("File reading error (dumpkeys.bin).");
|
||||||
|
|
||||||
fclose(fkeys);
|
fclose(fkeys);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
|
for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
|
||||||
if (fread(keyB[sectorNo], 1, 6, fkeys) == 0) {
|
size_t bytes_read = fread(keyB[sectorNo], 1, 6, fkeys);
|
||||||
|
if (bytes_read != 6) {
|
||||||
PrintAndLog("File reading error (dumpkeys.bin).");
|
PrintAndLog("File reading error (dumpkeys.bin).");
|
||||||
fclose(fkeys);
|
fclose(fkeys);
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -496,7 +501,8 @@ int CmdHF14AMfRestore(const char *Cmd)
|
||||||
UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};
|
UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};
|
||||||
memcpy(c.d.asBytes, key, 6);
|
memcpy(c.d.asBytes, key, 6);
|
||||||
|
|
||||||
if (fread(bldata, 1, 16, fdump) == 0) {
|
size_t bytes_read = fread(bldata, 1, 16, fdump);
|
||||||
|
if (bytes_read != 16) {
|
||||||
PrintAndLog("File reading error (dumpdata.bin).");
|
PrintAndLog("File reading error (dumpdata.bin).");
|
||||||
fclose(fdump);
|
fclose(fdump);
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -920,6 +926,7 @@ int CmdHF14AMfChk(const char *Cmd)
|
||||||
if (!p) {
|
if (!p) {
|
||||||
PrintAndLog("Cannot allocate memory for defKeys");
|
PrintAndLog("Cannot allocate memory for defKeys");
|
||||||
free(keyBlock);
|
free(keyBlock);
|
||||||
|
fclose(f);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
keyBlock = p;
|
keyBlock = p;
|
||||||
|
@ -1448,7 +1455,7 @@ int CmdHF14AMfELoad(const char *Cmd)
|
||||||
|
|
||||||
len = param_getstr(Cmd,nameParamNo,filename);
|
len = param_getstr(Cmd,nameParamNo,filename);
|
||||||
|
|
||||||
if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
|
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
|
||||||
|
|
||||||
fnameptr += len;
|
fnameptr += len;
|
||||||
|
|
||||||
|
@ -1547,7 +1554,7 @@ int CmdHF14AMfESave(const char *Cmd)
|
||||||
|
|
||||||
len = param_getstr(Cmd,nameParamNo,filename);
|
len = param_getstr(Cmd,nameParamNo,filename);
|
||||||
|
|
||||||
if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
|
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
|
||||||
|
|
||||||
// user supplied filename?
|
// user supplied filename?
|
||||||
if (len < 1) {
|
if (len < 1) {
|
||||||
|
@ -1823,7 +1830,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
len = strlen(Cmd);
|
len = strlen(Cmd);
|
||||||
if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
|
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
|
||||||
|
|
||||||
memcpy(filename, Cmd, len);
|
memcpy(filename, Cmd, len);
|
||||||
fnameptr += len;
|
fnameptr += len;
|
||||||
|
@ -1864,6 +1871,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
|
||||||
|
|
||||||
if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
|
if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
|
||||||
PrintAndLog("Can't set magic card block: %d", blockNum);
|
PrintAndLog("Can't set magic card block: %d", blockNum);
|
||||||
|
fclose(f);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
blockNum++;
|
blockNum++;
|
||||||
|
@ -1992,7 +2000,7 @@ int CmdHF14AMfCSave(const char *Cmd) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
len = strlen(Cmd);
|
len = strlen(Cmd);
|
||||||
if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;
|
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
|
||||||
|
|
||||||
if (len < 1) {
|
if (len < 1) {
|
||||||
// get filename
|
// get filename
|
||||||
|
@ -2112,11 +2120,14 @@ int CmdHF14AMfSniff(const char *Cmd){
|
||||||
uint16_t traceLen = resp.arg[1];
|
uint16_t traceLen = resp.arg[1];
|
||||||
len = resp.arg[2];
|
len = resp.arg[2];
|
||||||
|
|
||||||
if (res == 0) return 0; // we are done
|
if (res == 0) { // we are done
|
||||||
|
free(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (res == 1) { // there is (more) data to be transferred
|
if (res == 1) { // there is (more) data to be transferred
|
||||||
if (pckNum == 0) { // first packet, (re)allocate necessary buffer
|
if (pckNum == 0) { // first packet, (re)allocate necessary buffer
|
||||||
if (traceLen > bufsize) {
|
if (traceLen > bufsize || buf == NULL) {
|
||||||
uint8_t *p;
|
uint8_t *p;
|
||||||
if (buf == NULL) { // not yet allocated
|
if (buf == NULL) { // not yet allocated
|
||||||
p = malloc(traceLen);
|
p = malloc(traceLen);
|
||||||
|
|
|
@ -274,7 +274,7 @@ int CmdTIWrite(const char *Cmd)
|
||||||
UsbCommand c = {CMD_WRITE_TI_TYPE};
|
UsbCommand c = {CMD_WRITE_TI_TYPE};
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
res = sscanf(Cmd, "%012" PRIx64 " %012" PRIx64 " %012" PRIx64 "", &c.arg[0], &c.arg[1], &c.arg[2]);
|
res = sscanf(Cmd, "%012" SCNx64 " %012" SCNx64 " %012" SCNx64 "", &c.arg[0], &c.arg[1], &c.arg[2]);
|
||||||
|
|
||||||
if (res == 2) c.arg[2]=0;
|
if (res == 2) c.arg[2]=0;
|
||||||
if (res < 2)
|
if (res < 2)
|
||||||
|
|
|
@ -30,7 +30,6 @@
|
||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
|
||||||
static int CmdHelp(const char *Cmd);
|
static int CmdHelp(const char *Cmd);
|
||||||
static int CmdList(const char *Cmd);
|
static int CmdList(const char *Cmd);
|
||||||
static int CmdRun(const char *Cmd);
|
static int CmdRun(const char *Cmd);
|
||||||
|
@ -77,7 +76,10 @@ int CmdList(const char *Cmd)
|
||||||
{
|
{
|
||||||
DIR *dp;
|
DIR *dp;
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
dp = opendir ("./scripts/");
|
char script_directory_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + 1];
|
||||||
|
strcpy(script_directory_path, get_my_executable_directory());
|
||||||
|
strcat(script_directory_path, LUA_SCRIPTS_DIRECTORY);
|
||||||
|
dp = opendir(script_directory_path);
|
||||||
|
|
||||||
if (dp != NULL)
|
if (dp != NULL)
|
||||||
{
|
{
|
||||||
|
@ -149,17 +151,19 @@ int CmdRun(const char *Cmd)
|
||||||
suffix = ".lua";
|
suffix = ".lua";
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[256];
|
char script_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + strlen(script_name) + strlen(suffix) + 1];
|
||||||
snprintf(buf, sizeof buf, "./scripts/%s%s", script_name, suffix);
|
strcpy(script_path, get_my_executable_directory());
|
||||||
|
strcat(script_path, LUA_SCRIPTS_DIRECTORY);
|
||||||
printf("--- Executing: %s, args'%s'\n",buf,arguments);
|
strcat(script_path, script_name);
|
||||||
|
strcat(script_path, suffix);
|
||||||
|
|
||||||
|
printf("--- Executing: %s%s, args '%s'\n", script_name, suffix, arguments);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// run the Lua script
|
// run the Lua script
|
||||||
|
|
||||||
int error = luaL_loadfile(lua_state, buf);
|
int error = luaL_loadfile(lua_state, script_path);
|
||||||
if(!error)
|
if(!error)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#include "sleep.h"
|
#include "sleep.h"
|
||||||
#include "cmdparser.h"
|
#include "cmdparser.h"
|
||||||
#include "cmdhw.h"
|
#include "cmdhw.h"
|
||||||
|
#include "whereami.h"
|
||||||
|
|
||||||
|
|
||||||
// a global mutex to prevent interlaced printing from different threads
|
// a global mutex to prevent interlaced printing from different threads
|
||||||
pthread_mutex_t print_lock;
|
pthread_mutex_t print_lock;
|
||||||
|
@ -195,6 +197,33 @@ static void dumpAllHelp(int markdown)
|
||||||
dumpCommandsRecursive(cmds, markdown);
|
dumpCommandsRecursive(cmds, markdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *my_executable_path = NULL;
|
||||||
|
static char *my_executable_directory = NULL;
|
||||||
|
|
||||||
|
const char const *get_my_executable_path(void)
|
||||||
|
{
|
||||||
|
return my_executable_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char const *get_my_executable_directory(void)
|
||||||
|
{
|
||||||
|
return my_executable_directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_my_executable_path(void)
|
||||||
|
{
|
||||||
|
int path_length = wai_getExecutablePath(NULL, 0, NULL);
|
||||||
|
if (path_length != -1) {
|
||||||
|
my_executable_path = (char*)malloc(path_length + 1);
|
||||||
|
int dirname_length = 0;
|
||||||
|
if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) {
|
||||||
|
my_executable_path[path_length] = '\0';
|
||||||
|
my_executable_directory = (char *)malloc(dirname_length + 2);
|
||||||
|
strncpy(my_executable_directory, my_executable_path, dirname_length+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
|
|
||||||
|
@ -217,6 +246,9 @@ int main(int argc, char* argv[]) {
|
||||||
dumpAllHelp(1);
|
dumpAllHelp(1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_my_executable_path();
|
||||||
|
|
||||||
// Make sure to initialize
|
// Make sure to initialize
|
||||||
struct main_loop_arg marg = {
|
struct main_loop_arg marg = {
|
||||||
.usb_present = 0,
|
.usb_present = 0,
|
||||||
|
|
|
@ -17,5 +17,7 @@
|
||||||
#define PROXPROMPT "proxmark3> "
|
#define PROXPROMPT "proxmark3> "
|
||||||
|
|
||||||
void SendCommand(UsbCommand *c);
|
void SendCommand(UsbCommand *c);
|
||||||
|
const char const *get_my_executable_path(void);
|
||||||
|
const char const *get_my_executable_directory(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -536,7 +536,11 @@ int set_pm3_libraries(lua_State *L)
|
||||||
|
|
||||||
//-- Last but not least, add to the LUA_PATH (package.path in lua)
|
//-- Last but not least, add to the LUA_PATH (package.path in lua)
|
||||||
// so we can load libraries from the ./lualib/ - directory
|
// so we can load libraries from the ./lualib/ - directory
|
||||||
setLuaPath(L,"./lualibs/?.lua");
|
char libraries_path[strlen(get_my_executable_directory()) + strlen(LUA_LIBRARIES_DIRECTORY) + strlen(LUA_LIBRARIES_WILDCARD) + 1];
|
||||||
|
strcpy(libraries_path, get_my_executable_directory());
|
||||||
|
strcat(libraries_path, LUA_LIBRARIES_DIRECTORY);
|
||||||
|
strcat(libraries_path, LUA_LIBRARIES_WILDCARD);
|
||||||
|
setLuaPath(L, libraries_path);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,10 @@
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
|
||||||
|
#define LUA_LIBRARIES_DIRECTORY "lualibs/"
|
||||||
|
#define LUA_SCRIPTS_DIRECTORY "scripts/"
|
||||||
|
#define LUA_LIBRARIES_WILDCARD "?.lua"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief set_libraries loads the core components of pm3 into the 'pm3'
|
* @brief set_libraries loads the core components of pm3 into the 'pm3'
|
||||||
* namespace within the given lua_State
|
* namespace within the given lua_State
|
||||||
|
|
662
client/whereami.c
Normal file
662
client/whereami.c
Normal file
|
@ -0,0 +1,662 @@
|
||||||
|
// (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
|
||||||
|
// https://github.com/gpakosz/whereami
|
||||||
|
|
||||||
|
// in case you want to #include "whereami.c" in a larger compilation unit
|
||||||
|
#if !defined(WHEREAMI_H)
|
||||||
|
#include <whereami.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
|
||||||
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WAI_MALLOC)
|
||||||
|
#define WAI_MALLOC(size) malloc(size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WAI_FREE)
|
||||||
|
#define WAI_FREE(p) free(p)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WAI_REALLOC)
|
||||||
|
#define WAI_REALLOC(p, size) realloc(p, size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WAI_NOINLINE
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#define WAI_NOINLINE __declspec(noinline)
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#define WAI_NOINLINE __attribute__((noinline))
|
||||||
|
#else
|
||||||
|
#error unsupported compiler
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#define WAI_RETURN_ADDRESS() _ReturnAddress()
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#define WAI_RETURN_ADDRESS() __builtin_extract_return_addr(__builtin_return_address(0))
|
||||||
|
#else
|
||||||
|
#error unsupported compiler
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma warning(push, 3)
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#include <intrin.h>
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
wchar_t buffer1[MAX_PATH];
|
||||||
|
wchar_t buffer2[MAX_PATH];
|
||||||
|
wchar_t* path = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
DWORD size;
|
||||||
|
int length_, length__;
|
||||||
|
|
||||||
|
size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
|
||||||
|
|
||||||
|
if (size == 0)
|
||||||
|
break;
|
||||||
|
else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0])))
|
||||||
|
{
|
||||||
|
DWORD size_ = size;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
wchar_t* path_;
|
||||||
|
|
||||||
|
path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2);
|
||||||
|
if (!path_)
|
||||||
|
break;
|
||||||
|
size_ *= 2;
|
||||||
|
path = path_;
|
||||||
|
size = GetModuleFileNameW(module, path, size_);
|
||||||
|
}
|
||||||
|
while (size == size_);
|
||||||
|
|
||||||
|
if (size == size_)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
path = buffer1;
|
||||||
|
|
||||||
|
if (!_wfullpath(buffer2, path, MAX_PATH))
|
||||||
|
break;
|
||||||
|
length_ = (int)wcslen(buffer2);
|
||||||
|
length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_ , out, capacity, NULL, NULL);
|
||||||
|
|
||||||
|
if (length__ == 0)
|
||||||
|
length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL);
|
||||||
|
if (length__ == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (length__ <= capacity && dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length__ - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '\\')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
length = length__;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path != buffer1)
|
||||||
|
WAI_FREE(path);
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
WAI_NOINLINE
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
WAI_NOINLINE
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
HMODULE module;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable: 4054)
|
||||||
|
#endif
|
||||||
|
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module))
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__linux__)
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#ifndef __STDC_FORMAT_MACROS
|
||||||
|
#define __STDC_FORMAT_MACROS
|
||||||
|
#endif
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#if !defined(WAI_PROC_SELF_EXE)
|
||||||
|
#define WAI_PROC_SELF_EXE "/proc/self/exe"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
resolved = realpath(WAI_PROC_SELF_EXE, buffer);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined(WAI_PROC_SELF_MAPS_RETRY)
|
||||||
|
#define WAI_PROC_SELF_MAPS_RETRY 5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WAI_PROC_SELF_MAPS)
|
||||||
|
#define WAI_PROC_SELF_MAPS "/proc/self/maps"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__ANDROID__) || defined(ANDROID)
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WAI_NOINLINE
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
int length = -1;
|
||||||
|
FILE* maps = NULL;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < WAI_PROC_SELF_MAPS_RETRY; ++i)
|
||||||
|
{
|
||||||
|
maps = fopen(WAI_PROC_SELF_MAPS, "r");
|
||||||
|
if (!maps)
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX];
|
||||||
|
uint64_t low, high;
|
||||||
|
char perms[5];
|
||||||
|
uint64_t offset;
|
||||||
|
uint32_t major, minor;
|
||||||
|
char path[PATH_MAX];
|
||||||
|
uint32_t inode;
|
||||||
|
|
||||||
|
if (!fgets(buffer, sizeof(buffer), maps))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
|
||||||
|
{
|
||||||
|
uint64_t addr = (uint64_t)(uintptr_t)WAI_RETURN_ADDRESS();
|
||||||
|
if (low <= addr && addr <= high)
|
||||||
|
{
|
||||||
|
char* resolved;
|
||||||
|
|
||||||
|
resolved = realpath(path, buffer);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
#if defined(__ANDROID__) || defined(ANDROID)
|
||||||
|
if (length > 4
|
||||||
|
&&buffer[length - 1] == 'k'
|
||||||
|
&&buffer[length - 2] == 'p'
|
||||||
|
&&buffer[length - 3] == 'a'
|
||||||
|
&&buffer[length - 4] == '.')
|
||||||
|
{
|
||||||
|
int fd = open(path, O_RDONLY);
|
||||||
|
char* begin;
|
||||||
|
char* p;
|
||||||
|
|
||||||
|
begin = (char*)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
p = begin + offset;
|
||||||
|
|
||||||
|
while (p >= begin) // scan backwards
|
||||||
|
{
|
||||||
|
if (*((uint32_t*)p) == 0x04034b50UL) // local file header found
|
||||||
|
{
|
||||||
|
uint16_t length_ = *((uint16_t*)(p + 26));
|
||||||
|
|
||||||
|
if (length + 2 + length_ < (int)sizeof(buffer))
|
||||||
|
{
|
||||||
|
memcpy(&buffer[length], "!/", 2);
|
||||||
|
memcpy(&buffer[length + 2], p + 30, length_);
|
||||||
|
length += 2 + length_;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
p -= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
munmap(begin, offset);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(maps);
|
||||||
|
|
||||||
|
if (length != -1)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
|
||||||
|
#define _DARWIN_BETTER_REALPATH
|
||||||
|
#include <mach-o/dyld.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer1[PATH_MAX];
|
||||||
|
char buffer2[PATH_MAX];
|
||||||
|
char* path = buffer1;
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
uint32_t size = (uint32_t)sizeof(buffer1);
|
||||||
|
if (_NSGetExecutablePath(path, &size) == -1)
|
||||||
|
{
|
||||||
|
path = (char*)WAI_MALLOC(size);
|
||||||
|
if (!_NSGetExecutablePath(path, &size))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolved = realpath(path, buffer2);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path != buffer1)
|
||||||
|
WAI_FREE(path);
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
WAI_NOINLINE
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
Dl_info info;
|
||||||
|
|
||||||
|
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||||
|
{
|
||||||
|
resolved = realpath(info.dli_fname, buffer);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__QNXNTO__)
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
#if !defined(WAI_PROC_SELF_EXE)
|
||||||
|
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer1[PATH_MAX];
|
||||||
|
char buffer2[PATH_MAX];
|
||||||
|
char* resolved = NULL;
|
||||||
|
FILE* self_exe = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
self_exe = fopen(WAI_PROC_SELF_EXE, "r");
|
||||||
|
if (!self_exe)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!fgets(buffer1, sizeof(buffer1), self_exe))
|
||||||
|
break;
|
||||||
|
|
||||||
|
resolved = realpath(buffer1, buffer2);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(self_exe);
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
Dl_info info;
|
||||||
|
|
||||||
|
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||||
|
{
|
||||||
|
resolved = realpath(info.dli_fname, buffer);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
|
||||||
|
defined(__FreeBSD_kernel__) || defined(__NetBSD__)
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer1[PATH_MAX];
|
||||||
|
char buffer2[PATH_MAX];
|
||||||
|
char* path = buffer1;
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
|
||||||
|
size_t size = sizeof(buffer1);
|
||||||
|
|
||||||
|
if (sysctl(mib, (u_int)(sizeof(mib) / sizeof(mib[0])), path, &size, NULL, 0) != 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
resolved = realpath(path, buffer2);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path != buffer1)
|
||||||
|
WAI_FREE(path);
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
WAI_NOINLINE
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||||
|
{
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
char* resolved = NULL;
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
Dl_info info;
|
||||||
|
|
||||||
|
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||||
|
{
|
||||||
|
resolved = realpath(info.dli_fname, buffer);
|
||||||
|
if (!resolved)
|
||||||
|
break;
|
||||||
|
|
||||||
|
length = (int)strlen(resolved);
|
||||||
|
if (length <= capacity)
|
||||||
|
{
|
||||||
|
memcpy(out, resolved, length);
|
||||||
|
|
||||||
|
if (dirname_length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = length - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (out[i] == '/')
|
||||||
|
{
|
||||||
|
*dirname_length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#error unsupported platform
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
65
client/whereami.h
Normal file
65
client/whereami.h
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
// (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
|
||||||
|
// https://github.com/gpakosz/whereami
|
||||||
|
|
||||||
|
#ifndef WHEREAMI_H
|
||||||
|
#define WHEREAMI_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WAI_FUNCSPEC
|
||||||
|
#define WAI_FUNCSPEC
|
||||||
|
#endif
|
||||||
|
#ifndef WAI_PREFIX
|
||||||
|
#define WAI_PREFIX(function) wai_##function
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the path to the current executable.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
|
||||||
|
* retrieve the length of the path
|
||||||
|
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
|
||||||
|
* - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
|
||||||
|
* path
|
||||||
|
* - add a terminal NUL character with `path[length] = '\0';`
|
||||||
|
*
|
||||||
|
* @param out destination buffer, optional
|
||||||
|
* @param capacity destination buffer capacity
|
||||||
|
* @param dirname_length optional recipient for the length of the dirname part
|
||||||
|
* of the path.
|
||||||
|
*
|
||||||
|
* @return the length of the executable path on success (without a terminal NUL
|
||||||
|
* character), otherwise `-1`
|
||||||
|
*/
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the path to the current module
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
|
||||||
|
* the length of the path
|
||||||
|
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
|
||||||
|
* - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
|
||||||
|
* - add a terminal NUL character with `path[length] = '\0';`
|
||||||
|
*
|
||||||
|
* @param out destination buffer, optional
|
||||||
|
* @param capacity destination buffer capacity
|
||||||
|
* @param dirname_length optional recipient for the length of the dirname part
|
||||||
|
* of the path.
|
||||||
|
*
|
||||||
|
* @return the length of the module path on success (without a terminal NUL
|
||||||
|
* character), otherwise `-1`
|
||||||
|
*/
|
||||||
|
WAI_FUNCSPEC
|
||||||
|
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef WHEREAMI_H
|
|
@ -1,9 +1,4 @@
|
||||||
#define __STDC_FORMAT_MACROS
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#define llx PRIx64
|
|
||||||
#define lli PRIi64
|
|
||||||
|
|
||||||
// Test-file: test2.c
|
|
||||||
#include "crapto1.h"
|
#include "crapto1.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -62,7 +57,7 @@ int main (int argc, char *argv[]) {
|
||||||
crypto1_word(t, uid ^ nt, 0);
|
crypto1_word(t, uid ^ nt, 0);
|
||||||
crypto1_word(t, nr1_enc, 1);
|
crypto1_word(t, nr1_enc, 1);
|
||||||
if (ar1_enc == (crypto1_word(t, 0, 0) ^ prng_successor(nt, 64))) {
|
if (ar1_enc == (crypto1_word(t, 0, 0) ^ prng_successor(nt, 64))) {
|
||||||
printf("\nFound Key: [%012"llx"]\n\n",key);
|
printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,4 @@
|
||||||
#define __STDC_FORMAT_MACROS
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#define llx PRIx64
|
|
||||||
#define lli PRIi64
|
|
||||||
|
|
||||||
// Test-file: test2.c
|
|
||||||
#include "crapto1.h"
|
#include "crapto1.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -103,7 +98,7 @@ int main (int argc, char *argv[]) {
|
||||||
lfsr_rollback_word(revstate, nr_enc, 1);
|
lfsr_rollback_word(revstate, nr_enc, 1);
|
||||||
lfsr_rollback_word(revstate, uid ^ nt, 0);
|
lfsr_rollback_word(revstate, uid ^ nt, 0);
|
||||||
crypto1_get_lfsr(revstate, &key);
|
crypto1_get_lfsr(revstate, &key);
|
||||||
printf("\nFound Key: [%012"llx"]\n\n",key);
|
printf("\nFound Key: [%012" PRIx64"]\n\n",key);
|
||||||
crypto1_destroy(revstate);
|
crypto1_destroy(revstate);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#include "crapto1.h"
|
#include "crapto1.h"
|
||||||
#define __STDC_FORMAT_MACROS
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#define llx PRIx64
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
typedef unsigned char byte_t;
|
typedef unsigned char byte_t;
|
||||||
|
|
||||||
|
@ -20,13 +18,13 @@ int main(const int argc, const char* argv[]) {
|
||||||
}
|
}
|
||||||
sscanf(argv[1],"%08x",&uid);
|
sscanf(argv[1],"%08x",&uid);
|
||||||
sscanf(argv[2],"%08x",&nt);
|
sscanf(argv[2],"%08x",&nt);
|
||||||
sscanf(argv[3],"%016"llx,&par_info);
|
sscanf(argv[3],"%016" SCNx64,&par_info);
|
||||||
sscanf(argv[4],"%016"llx,&ks_info);
|
sscanf(argv[4],"%016" SCNx64,&ks_info);
|
||||||
|
|
||||||
// Reset the last three significant bits of the reader nonce
|
// Reset the last three significant bits of the reader nonce
|
||||||
nr &= 0xffffff1f;
|
nr &= 0xffffff1f;
|
||||||
|
|
||||||
printf("\nuid(%08x) nt(%08x) par(%016"llx") ks(%016"llx")\n\n",uid,nt,par_info,ks_info);
|
printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n",uid,nt,par_info,ks_info);
|
||||||
|
|
||||||
for (pos=0; pos<8; pos++)
|
for (pos=0; pos<8; pos++)
|
||||||
{
|
{
|
||||||
|
@ -52,7 +50,7 @@ int main(const int argc, const char* argv[]) {
|
||||||
state = lfsr_common_prefix(nr,rr,ks3x,par);
|
state = lfsr_common_prefix(nr,rr,ks3x,par);
|
||||||
lfsr_rollback_word(state,uid^nt,0);
|
lfsr_rollback_word(state,uid^nt,0);
|
||||||
crypto1_get_lfsr(state,&key_recovered);
|
crypto1_get_lfsr(state,&key_recovered);
|
||||||
printf("\nkey recovered: %012"llx"\n\n",key_recovered);
|
printf("\nkey recovered: %012" PRIx64 "\n\n",key_recovered);
|
||||||
crypto1_destroy(state);
|
crypto1_destroy(state);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue