From 1b854be75e568cfd428b64a6c4a6779a913a8974 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 29 May 2017 14:02:50 +0200 Subject: [PATCH] Add file.lastpath to fix regression in projects using malloc:// --- libr/core/cconfig.c | 3 +++ libr/core/project.c | 29 ++++++++++++++++++++--------- libr/include/sdb/sdb.h | 7 ++++++- libr/include/sdb/types.h | 1 + shlr/sdb/src/cdb.c | 4 ++-- shlr/sdb/src/json/js0n.c | 4 ++-- shlr/sdb/src/sdb.h | 7 ++++++- shlr/sdb/src/types.h | 1 + shlr/sdb/src/util.c | 28 ++++++++++++++++++++++++++++ 9 files changed, 69 insertions(+), 15 deletions(-) diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 97acbabfc8..adca91c16f 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -1305,7 +1305,9 @@ static int cb_io_oxff(void *user, void *data) { } static int cb_filepath(void *user, void *data) { + RCore *core = (RCore *) user; RConfigNode *node = (RConfigNode *) data; + r_config_set (core->config, "file.lastpath", node->value); char *pikaboo = strstr (node->value, "://"); if (pikaboo) { if (pikaboo[3] == '/') { @@ -2462,6 +2464,7 @@ R_API int r_core_config_init(RCore *core) { SETPREF ("file.info", "true", "RBin info loaded"); SETPREF ("file.offset", "", "Offset where the file will be mapped at"); SETCB ("file.path", "", &cb_filepath, "Path of current file"); + SETPREF ("file.lastpath", "", "Path of current file"); SETPREF ("file.sha1", "", "SHA1 hash of current file"); SETPREF ("file.type", "", "Type of current file"); n = NODECB ("file.loadmethod", "fail", &cb_fileloadmethod); diff --git a/libr/core/project.c b/libr/core/project.c index 7bd716e5b5..f515a3f6bb 100644 --- a/libr/core/project.c +++ b/libr/core/project.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2010-2016 - pancake, maijin */ +/* radare - LGPL - Copyright 2010-2017 - pancake, maijin */ #include #include @@ -338,18 +338,18 @@ R_API RThread *r_core_project_load_bg(RCore *core, const char *prjName, const ch R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) { int askuser = 1; int ret, close_current_session = 1; - char *prj, *filepath, *oldbin; + char *oldbin; const char *newbin; ut64 mapaddr = 0; if (!prjfile || !*prjfile) { return false; } - prj = projectScriptPath (core, prjfile); + char *prj = projectScriptPath (core, prjfile); if (!prj) { eprintf ("Invalid project name '%s'\n", prjfile); return false; } - filepath = r_core_project_info (core, prj); + char *filepath = r_core_project_info (core, prj); // eprintf ("OPENING (%s) from %s\n", prj, r_config_get (core->config, "file.path")); /* if it is not an URI */ if (!filepath) { @@ -366,7 +366,11 @@ R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) { return false; } } - oldbin = strdup (r_config_get (core->config, "file.path")); + const char *file_path = r_config_get (core->config, "file.path"); + if (!file_path || !*file_path) { + file_path = r_config_get (core->config, "file.lastpath"); + } + oldbin = strdup (file_path); if (!strcmp (prjfile, r_config_get (core->config, "prj.name"))) { // eprintf ("Reloading project\n"); askuser = 0; @@ -396,10 +400,8 @@ R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) { fh = r_core_file_open (core, filepath, 0, 0); if (!fh) { eprintf ("Cannot open file '%s'\n", filepath); - free (oldbin); - free (filepath); - free (prj); - return false; + ret = false; + goto beach; } } @@ -415,9 +417,13 @@ R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) { ret = r_core_project_load (core, prjfile, prj); } newbin = r_config_get (core->config, "file.path"); + if (!newbin || !*newbin) { + newbin = r_config_get (core->config, "file.lastpath"); + } if (strcmp (oldbin, newbin)) { eprintf ("WARNING: file.path changed: %s => %s\n", oldbin, newbin); } +beach: free (oldbin); free (filepath); free (prj); @@ -444,6 +450,11 @@ R_API char *r_core_project_info(RCore *core, const char *prjfile) { file = r_str_new (buf + 15); break; } + if (!strncmp (buf, "\"e file.lastpath = ", 19)) { + buf[strlen (buf) - 2] = 0; + file = r_str_new (buf + 19); + break; + } // TODO: deprecate before 1.0 if (!strncmp (buf, "e file.path = ", 14)) { buf[strlen (buf) - 1] = 0; diff --git a/libr/include/sdb/sdb.h b/libr/include/sdb/sdb.h index 38eed2eb14..51615f1bc2 100644 --- a/libr/include/sdb/sdb.h +++ b/libr/include/sdb/sdb.h @@ -1,7 +1,7 @@ #ifndef SDB_H #define SDB_H -#if !defined(O_BINARY) && !defined(_MSC_VER) +#ifndef O_BINARY #define O_BINARY 0 #endif @@ -39,6 +39,11 @@ extern "C" { #ifndef _MSC_VER extern __attribute__((dllimport)) void *__cdecl _aligned_malloc(size_t, size_t); extern char *strdup (const char *); +#else +#include +#include +#include // for _aligned_malloc +#define ftruncate _chsize #endif #undef r_offsetof #define r_offsetof(type, member) ((unsigned long) (ut64)&((type*)0)->member) diff --git a/libr/include/sdb/types.h b/libr/include/sdb/types.h index 3c759dff6d..5481e4f774 100644 --- a/libr/include/sdb/types.h +++ b/libr/include/sdb/types.h @@ -6,6 +6,7 @@ #include #include #include +#include #undef eprintf #define eprintf(...) fprintf(stderr,__VA_ARGS__) diff --git a/shlr/sdb/src/cdb.c b/shlr/sdb/src/cdb.c index 24fb5beb57..1992493dac 100644 --- a/shlr/sdb/src/cdb.c +++ b/shlr/sdb/src/cdb.c @@ -101,8 +101,8 @@ bool cdb_read(struct cdb *c, char *buf, ut32 len, ut32 pos) { return false; } while (len > 0) { - ssize_t r = read (c->fd, buf, len); - if (r < 1 || (ut32) r != len) { + int r = (int)read (c->fd, buf, len); + if (r < 1 || (ut32)r != len) { return false; } buf += r; diff --git a/shlr/sdb/src/json/js0n.c b/shlr/sdb/src/json/js0n.c index 2d005f0582..b7af1ddf09 100644 --- a/shlr/sdb/src/json/js0n.c +++ b/shlr/sdb/src/json/js0n.c @@ -6,9 +6,9 @@ #include "rangstr.h" #ifdef _MSC_VER +#pragma message ("TODO: json not implemented for this platform") int js0n(const ut8 *js, RangstrType len, RangstrType *out) { -#pragma message("json/js0n.c: Unimplemented for this platform.") - return 0; + return 1; } #else #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) diff --git a/shlr/sdb/src/sdb.h b/shlr/sdb/src/sdb.h index 38eed2eb14..51615f1bc2 100644 --- a/shlr/sdb/src/sdb.h +++ b/shlr/sdb/src/sdb.h @@ -1,7 +1,7 @@ #ifndef SDB_H #define SDB_H -#if !defined(O_BINARY) && !defined(_MSC_VER) +#ifndef O_BINARY #define O_BINARY 0 #endif @@ -39,6 +39,11 @@ extern "C" { #ifndef _MSC_VER extern __attribute__((dllimport)) void *__cdecl _aligned_malloc(size_t, size_t); extern char *strdup (const char *); +#else +#include +#include +#include // for _aligned_malloc +#define ftruncate _chsize #endif #undef r_offsetof #define r_offsetof(type, member) ((unsigned long) (ut64)&((type*)0)->member) diff --git a/shlr/sdb/src/types.h b/shlr/sdb/src/types.h index 3c759dff6d..5481e4f774 100644 --- a/shlr/sdb/src/types.h +++ b/shlr/sdb/src/types.h @@ -6,6 +6,7 @@ #include #include #include +#include #undef eprintf #define eprintf(...) fprintf(stderr,__VA_ARGS__) diff --git a/shlr/sdb/src/util.c b/shlr/sdb/src/util.c index fc3ddb7443..0332599569 100644 --- a/shlr/sdb/src/util.c +++ b/shlr/sdb/src/util.c @@ -6,9 +6,37 @@ #if USE_MONOTONIC_CLOCK #include +#else +#ifdef _MSC_VER +#pragma message ("gettimeofday: Windows support is ugly here") +#include +int gettimeofday (struct timeval* p, void* tz) { + ULARGE_INTEGER ul; // As specified on MSDN. + FILETIME ft; + + // Returns a 64-bit value representing the number of + // 100-nanosecond intervals since January 1, 1601 (UTC). + GetSystemTimeAsFileTime (&ft); + + // Fill ULARGE_INTEGER low and high parts. + ul.LowPart = ft.dwLowDateTime; + ul.HighPart = ft.dwHighDateTime; + // Convert to microseconds. + ul.QuadPart /= 10ULL; + // Remove Windows to UNIX Epoch delta. + ul.QuadPart -= 11644473600000000ULL; + // Modulo to retrieve the microseconds. + p->tv_usec = (long)(ul.QuadPart % 1000000LL); + // Divide to retrieve the seconds. + p->tv_sec = (long)(ul.QuadPart / 1000000LL); + + return 0; +} + #else #include #endif +#endif SDB_API ut32 sdb_hash_len(const char *s, ut32 *len) { ut32 h = CDB_HASHSTART;