Update sdb and capstone

This commit is contained in:
pancake 2017-03-13 14:59:00 +01:00
parent d301ff28b4
commit 9a4a014e3c
8 changed files with 60 additions and 51 deletions

View File

@ -25,6 +25,7 @@ typedef char* (*DupValue)(void *);
typedef size_t (*CalcSize)(void *); typedef size_t (*CalcSize)(void *);
typedef ut32 (*HashFunction)(const char*); typedef ut32 (*HashFunction)(const char*);
typedef int (*ListComparator)(const char *a, const char *b); typedef int (*ListComparator)(const char *a, const char *b);
typedef bool (*HtForeachCallback)(void *user, const char *k, void *v);
/** ht **/ /** ht **/
@ -66,4 +67,6 @@ bool ht_delete(SdbHash* ht, const char* key);
// Find the value corresponding to the matching key. // Find the value corresponding to the matching key.
void* ht_find(SdbHash* ht, const char* key, bool* found); void* ht_find(SdbHash* ht, const char* key, bool* found);
HtKv* ht_find_kv(SdbHash* ht, const char* key, bool* found); HtKv* ht_find_kv(SdbHash* ht, const char* key, bool* found);
void ht_foreach(SdbHash *ht, HtForeachCallback cb, void *user);
SdbList* ht_foreach_list(SdbHash *ht, bool sorted);
#endif // __HT_H #endif // __HT_H

View File

@ -16,6 +16,9 @@ extern "C" {
#include "cdb_make.h" #include "cdb_make.h"
#include "sdb_version.h" #include "sdb_version.h"
#undef r_offsetof
#define r_offsetof(type, member) ((unsigned long) &((type*)0)->member)
/* Key value sizes */ /* Key value sizes */
#define SDB_MIN_VALUE 1 #define SDB_MIN_VALUE 1
#define SDB_MAX_VALUE 0xffffff #define SDB_MAX_VALUE 0xffffff

View File

@ -41,14 +41,10 @@
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#if __CYGWIN__
#define HAVE_MMAN 1
#define ULLFMT "ll" #define ULLFMT "ll"
#elif __SDB_WINDOWS__ #if __SDB_WINDOWS__ && !__CYGWIN__
#define HAVE_MMAN 0 #define HAVE_MMAN 0
#define ULLFMT "I64"
#else #else
#define ULLFMT "ll"
#define HAVE_MMAN 1 #define HAVE_MMAN 1
#endif #endif

View File

@ -23,9 +23,9 @@ CS_PATCHES=0
else else
CS_TAR= CS_TAR=
CS_URL=$(GIT_PREFIX)github.com/aquynh/capstone.git CS_URL=$(GIT_PREFIX)github.com/aquynh/capstone.git
CS_UPD=20170227 CS_UPD=20170313
CS_BRA=next CS_BRA=next
CS_TIP=5b84c691784b5f0adc3c100d6ec5d5bb71ec8532 CS_TIP=f7a3cc25965e4d7d4f3488394b24f1d4650de905
# REVERT THIS COMMIT BECAUSE ITS WRONG # REVERT THIS COMMIT BECAUSE ITS WRONG
CS_REV= CS_REV=
CS_PATCHES=1 CS_PATCHES=1

View File

@ -11,7 +11,9 @@
SDB_API char *sdb_json_get (Sdb *s, const char *k, const char *p, ut32 *cas) { SDB_API char *sdb_json_get (Sdb *s, const char *k, const char *p, ut32 *cas) {
Rangstr rs; Rangstr rs;
char *u, *v = sdb_get (s, k, cas); char *u, *v = sdb_get (s, k, cas);
if (!v) return NULL; if (!v) {
return NULL;
}
rs = json_get (v, p); rs = json_get (v, p);
u = rangstr_dup (&rs); u = rangstr_dup (&rs);
free (v); free (v);
@ -61,7 +63,7 @@ static int findkey(Rangstr *rs) {
return -1; return -1;
} }
static bool isstring (const char *s) { static bool isstring(const char *s) {
if (!strcmp (s, "true")) { if (!strcmp (s, "true")) {
return false; return false;
} }
@ -106,7 +108,7 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
b = malloc (p_len + v_len + 8); b = malloc (p_len + v_len + 8);
if (b) { if (b) {
int is_str = isstring (v); int is_str = isstring (v);
const char *q = is_str ? "\"" : ""; const char *q = is_str? "\"": "";
sprintf (b, "{\"%s\":%s%s%s}", p, q, v, q); sprintf (b, "{\"%s\":%s%s%s}", p, q, v, q);
#if 0 #if 0
/* disabled because it memleaks */ /* disabled because it memleaks */
@ -129,7 +131,7 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
char *b = malloc (b_len); char *b = malloc (b_len);
if (b) { if (b) {
int curlen, is_str = isstring (v); int curlen, is_str = isstring (v);
const char *q = is_str?"\"":""; const char *q = is_str? "\"": "";
const char *e = ""; // XX: or comma const char *e = ""; // XX: or comma
if (js[0] && js[1] != '}') { if (js[0] && js[1] != '}') {
e = ","; e = ",";
@ -143,8 +145,8 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
} }
// invalid json? // invalid json?
return false; return false;
} }
#define WLEN(x) (int)(size_t)(end[x]-beg[x]) #define WLEN(x) (int) (size_t) (end[x] - beg[x])
beg[0] = js; beg[0] = js;
end[0] = rs.p + rs.f; end[0] = rs.p + rs.f;
@ -188,7 +190,7 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
idx += len[1]; idx += len[1];
if (is_str) { if (is_str) {
// TODO: add quotes // TODO: add quotes
if (beg[2][0]!='"') { if (beg[2][0] != '"') {
str[idx] = '"'; str[idx] = '"';
idx++; idx++;
} }
@ -206,14 +208,14 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
rs.f -= 2; rs.f -= 2;
kidx = findkey (&rs); kidx = findkey (&rs);
len[0] = R_MAX (1, kidx - 1); len[0] = R_MAX (1, kidx - 1);
if (kidx == 1){ if (kidx == 1) {
if (beg[2][0] == '"') { if (beg[2][0] == '"') {
beg[2]++; beg[2]++;
} }
beg[2]++; beg[2]++;
len[2]--; len[2]--;
} }
str = malloc (len[0] + len[2]+1); str = malloc (len[0] + len[2] + 1);
if (!str) { if (!str) {
return false; return false;
} }
@ -228,18 +230,18 @@ SDB_API bool sdb_json_set (Sdb *s, const char *k, const char *p, const char *v,
return true; return true;
} }
SDB_API const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...) { SDB_API const char *sdb_json_format(SdbJsonString *s, const char *fmt, ...) {
char *arg_s, *x, tmp[128]; char *arg_s, *x, tmp[128];
ut64 arg_l; ut64 arg_l;
int i, arg_i; int i, arg_i;
float arg_f; float arg_f;
va_list ap; va_list ap;
#define JSONSTR_ALLOCATE(y) \ #define JSONSTR_ALLOCATE(y)\
if (s->len+y>s->blen) {\ if (s->len + y > s->blen) {\
s->blen *= 2;\ s->blen *= 2;\
x = realloc (s->buf, s->blen);\ x = realloc (s->buf, s->blen);\
if (!x) { \ if (!x) {\
va_end (ap); \ va_end (ap);\
return NULL;\ return NULL;\
}\ }\
s->buf = x;\ s->buf = x;\
@ -274,14 +276,14 @@ SDB_API const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...) {
JSONSTR_ALLOCATE (32); JSONSTR_ALLOCATE (32);
arg_f = va_arg (ap, double); arg_f = va_arg (ap, double);
snprintf (tmp, sizeof (tmp), "%f", arg_f); snprintf (tmp, sizeof (tmp), "%f", arg_f);
memcpy (s->buf+s->len, tmp, strlen (tmp)); memcpy (s->buf + s->len, tmp, strlen (tmp));
s->len += strlen (tmp); s->len += strlen (tmp);
break; break;
case 'l': case 'l':
JSONSTR_ALLOCATE (32); JSONSTR_ALLOCATE (32);
arg_l = va_arg (ap, ut64); arg_l = va_arg (ap, ut64);
snprintf (tmp, sizeof (tmp), "0x%"ULLFMT"x", arg_l); snprintf (tmp, sizeof (tmp), "0x%"ULLFMT "x", arg_l);
memcpy (s->buf+s->len, tmp, strlen (tmp)); memcpy (s->buf + s->len, tmp, strlen (tmp));
s->len += strlen (tmp); s->len += strlen (tmp);
break; break;
case 'd': case 'd':
@ -289,16 +291,17 @@ SDB_API const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...) {
JSONSTR_ALLOCATE (32); JSONSTR_ALLOCATE (32);
arg_i = va_arg (ap, int); arg_i = va_arg (ap, int);
snprintf (tmp, sizeof (tmp), "%d", arg_i); snprintf (tmp, sizeof (tmp), "%d", arg_i);
memcpy (s->buf+s->len, tmp, strlen (tmp)); memcpy (s->buf + s->len, tmp, strlen (tmp));
s->len += strlen (tmp); s->len += strlen (tmp);
break; break;
case 's': case 's':
arg_s = va_arg (ap, char *); arg_s = va_arg (ap, char *);
JSONSTR_ALLOCATE (strlen (arg_s)+3); JSONSTR_ALLOCATE (strlen (arg_s) + 3);
s->buf[s->len++] = '"'; s->buf[s->len++] = '"';
for (i=0; arg_s[i]; i++) { for (i = 0; arg_s[i]; i++) {
if (arg_s[i]=='"') if (arg_s[i] == '"') {
s->buf[s->len++] = '\\'; s->buf[s->len++] = '\\';
}
s->buf[s->len++] = arg_s[i]; s->buf[s->len++] = arg_s[i];
} }
s->buf[s->len++] = '"'; s->buf[s->len++] = '"';
@ -316,7 +319,9 @@ SDB_API const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...) {
#if 0 #if 0
int main () { int main () {
SdbJsonString s = {0}; SdbJsonString s = {
0
};
sdb_json_format (&s, "[{%s:%d},%b]", "Hello \"world\"", 1024, 3); sdb_json_format (&s, "[{%s:%d},%b]", "Hello \"world\"", 1024, 3);
printf ("%s\n", sdb_json_format (&s, 0)); printf ("%s\n", sdb_json_format (&s, 0));
sdb_json_format_free (&s); sdb_json_format_free (&s);

View File

@ -112,7 +112,7 @@ static char *stdin_slurp(int *sz) {
if (!*next) { if (!*next) {
next = NULL; next = NULL;
} else { } else {
// continue; // continue;
} }
} else { } else {
next = NULL; next = NULL;
@ -157,9 +157,11 @@ static void synchronize(int sig UNUSED) {
} }
#endif #endif
static int sdb_grep_dump(const char *db, int fmt, bool grep, static int sdb_grep_dump(const char *db, int fmt, bool grep,
const char *expgrep) { const char *expgrep) {
char *v; char *v;
char k[SDB_MAX_KEY] = {0}; char k[SDB_MAX_KEY] = {
0
};
const char *comma = ""; const char *comma = "";
Sdb *s = sdb_new (NULL, db, 0); Sdb *s = sdb_new (NULL, db, 0);
if (!s) { if (!s) {
@ -180,7 +182,7 @@ static int sdb_grep_dump(const char *db, int fmt, bool grep,
printf ("%s\"%s\":%s", comma, k, v); printf ("%s\"%s\":%s", comma, k, v);
} else if (sdb_isnum (v)) { } else if (sdb_isnum (v)) {
printf ("%s\"%s\":%llu", comma, k, sdb_atoi (v)); printf ("%s\"%s\":%llu", comma, k, sdb_atoi (v));
} else if (*v=='{' || *v=='[') { } else if (*v == '{' || *v == '[') {
printf ("%s\"%s\":%s", comma, k, v); printf ("%s\"%s\":%s", comma, k, v);
} else { } else {
printf ("%s\"%s\":\"%s\"", comma, k, v); printf ("%s\"%s\":\"%s\"", comma, k, v);
@ -189,7 +191,7 @@ static int sdb_grep_dump(const char *db, int fmt, bool grep,
break; break;
case MODE_ZERO: case MODE_ZERO:
printf ("%s=%s", k, v); printf ("%s=%s", k, v);
fwrite ("", 1,1, stdout); fwrite ("", 1, 1, stdout);
break; break;
default: default:
printf ("%s=%s\n", k, v); printf ("%s=%s\n", k, v);
@ -228,7 +230,7 @@ static int insertkeys(Sdb *s, const char **args, int nargs, int mode) {
break; break;
case '=': case '=':
if (strchr (args[i], '=')) { if (strchr (args[i], '=')) {
char *v, *kv = (char *)strdup (args[i]); char *v, *kv = (char *) strdup (args[i]);
v = strchr (kv, '='); v = strchr (kv, '=');
if (v) { if (v) {
*v++ = 0; *v++ = 0;
@ -252,7 +254,7 @@ static int createdb(const char *f, const char **args, int nargs) {
} }
insertkeys (s, args, nargs, '='); insertkeys (s, args, nargs, '=');
sdb_config (s, options); sdb_config (s, options);
for (;(line = stdin_slurp (NULL));) { for (; (line = stdin_slurp (NULL));) {
if ((eq = strchr (line, '='))) { if ((eq = strchr (line, '='))) {
*eq++ = 0; *eq++ = 0;
sdb_disk_insert (s, line, eq); sdb_disk_insert (s, line, eq);
@ -266,7 +268,7 @@ static int createdb(const char *f, const char **args, int nargs) {
static int showusage(int o) { static int showusage(int o) {
printf ("usage: sdb [-0cdehjJv|-D A B] [-|db] " printf ("usage: sdb [-0cdehjJv|-D A B] [-|db] "
"[.file]|[-=]|[-+][(idx)key[:json|=value] ..]\n"); "[.file]|[-=]|[-+][(idx)key[:json|=value] ..]\n");
if (o==2) { if (o == 2) {
printf (" -0 terminate results with \\x00\n" printf (" -0 terminate results with \\x00\n"
" -c count the number of keys database\n" " -c count the number of keys database\n"
" -d decode base64 from stdin\n" " -d decode base64 from stdin\n"
@ -282,7 +284,7 @@ static int showusage(int o) {
} }
static int showversion(void) { static int showversion(void) {
printf ("sdb "SDB_VERSION"\n"); printf ("sdb "SDB_VERSION "\n");
fflush (stdout); fflush (stdout);
return 0; return 0;
} }
@ -308,7 +310,7 @@ static int jsonIndent() {
static int base64encode() { static int base64encode() {
char *out; char *out;
int len = 0; int len = 0;
ut8 *in = (ut8*)stdin_slurp (&len); ut8 *in = (ut8 *) stdin_slurp (&len);
if (!in) { if (!in) {
return 0; return 0;
} }
@ -326,7 +328,7 @@ static int base64encode() {
static int base64decode() { static int base64decode() {
ut8 *out; ut8 *out;
int len, ret = 1; int len, ret = 1;
char *in = (char*)stdin_slurp (&len); char *in = (char *) stdin_slurp (&len);
if (in) { if (in) {
out = sdb_decode (in, &len); out = sdb_decode (in, &len);
if (out) { if (out) {
@ -341,10 +343,12 @@ static int base64decode() {
return ret; return ret;
} }
static int dbdiff (const char *a, const char *b) { static int dbdiff(const char *a, const char *b) {
int n = 0; int n = 0;
char *v; char *v;
char k[SDB_MAX_KEY] = {0}; char k[SDB_MAX_KEY] = {
0
};
const char *v2; const char *v2;
Sdb *A = sdb_new (NULL, a, 0); Sdb *A = sdb_new (NULL, a, 0);
Sdb *B = sdb_new (NULL, b, 0); Sdb *B = sdb_new (NULL, b, 0);
@ -426,7 +430,7 @@ int main(int argc, const char **argv) {
return showusage (1); return showusage (1);
} }
break; break;
case 'c': return (argc < 3)? showusage (1) : showcount (argv[2]); case 'c': return (argc < 3)? showusage (1): showcount (argv[2]);
case 'v': return showversion (); case 'v': return showversion ();
case 'h': return showusage (2); case 'h': return showusage (2);
case 'e': return base64encode (); case 'e': return base64encode ();
@ -440,7 +444,7 @@ int main(int argc, const char **argv) {
if (argc > 2) { if (argc > 2) {
return sdb_dump (argv[db0 + 1], MODE_JSON); return sdb_dump (argv[db0 + 1], MODE_JSON);
} }
return jsonIndent(); return jsonIndent ();
default: default:
eprintf ("Invalid flag %s\n", arg); eprintf ("Invalid flag %s\n", arg);
break; break;
@ -478,7 +482,7 @@ int main(int argc, const char **argv) {
if (kvs < argc) { if (kvs < argc) {
save |= insertkeys (s, argv + argi + 2, argc - kvs, '-'); save |= insertkeys (s, argv + argi + 2, argc - kvs, '-');
} }
for (;(line = stdin_slurp (NULL));) { for (; (line = stdin_slurp (NULL));) {
save |= sdb_query (s, line); save |= sdb_query (s, line);
if (fmt) { if (fmt) {
fflush (stdout); fflush (stdout);

View File

@ -32,8 +32,10 @@ extern "C" {
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <io.h> #include <io.h>
extern void *__cdecl _aligned_malloc(size_t, size_t); extern __attribute__((dllimport)) void *__cdecl _aligned_malloc(size_t, size_t);
extern char *strdup (const char *); extern char *strdup (const char *);
#undef r_offsetof
#define r_offsetof(type, member) ((unsigned long) (ut64)&((type*)0)->member)
//#define SDB_MODE 0 //#define SDB_MODE 0
#define SDB_MODE _S_IWRITE | _S_IREAD #define SDB_MODE _S_IWRITE | _S_IREAD
#else #else

View File

@ -41,14 +41,10 @@
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#if __CYGWIN__
#define HAVE_MMAN 1
#define ULLFMT "ll" #define ULLFMT "ll"
#elif __SDB_WINDOWS__ #if __SDB_WINDOWS__ && !__CYGWIN__
#define HAVE_MMAN 0 #define HAVE_MMAN 0
#define ULLFMT "I64"
#else #else
#define ULLFMT "ll"
#define HAVE_MMAN 1 #define HAVE_MMAN 1
#endif #endif