diff --git a/libr/include/sdb/sdb.h b/libr/include/sdb/sdb.h index 4ad43ec0be..4583afc9a2 100644 --- a/libr/include/sdb/sdb.h +++ b/libr/include/sdb/sdb.h @@ -105,7 +105,7 @@ SdbKv *sdb_dump_next (Sdb* s); int sdb_dump_dupnext (Sdb* s, char **key, char **value); /* numeric */ -char *sdb_itoa(ut64 n, char *s); +char *sdb_itoa(ut64 n, char *s, int base); ut64 sdb_atoi(const char *s); ut64 sdb_getn (Sdb* s, const char *key, ut32 *cas); int sdb_setn (Sdb* s, const char *key, ut64 v, ut32 cas); @@ -187,7 +187,8 @@ char *sdb_apop(Sdb *s, const char *key, ut32 *cas); /* Util.c */ int sdb_check_value(const char *s); int sdb_check_key(const char *s); -SDB_API int sdb_isnum (const char *s); +int sdb_isnum (const char *s); +int sdb_numbase (const char *s); #ifdef __cplusplus } diff --git a/shlr/sdb/src/query.c b/shlr/sdb/src/query.c index 9bf6ee7c39..5b4e3de8f1 100644 --- a/shlr/sdb/src/query.c +++ b/shlr/sdb/src/query.c @@ -122,6 +122,7 @@ next_quote: sdb_concat (s, cmd+1, val, 0); } } else { + int base = sdb_numbase (sdb_getc (s, cmd+1, 0)); if (json) { *json = 0; if (*cmd=='+') n = sdb_json_inc (s, cmd+1, json+1, d, 0); @@ -131,10 +132,19 @@ next_quote: if (*cmd=='+') n = sdb_inc (s, cmd+1, d, 0); else n = sdb_dec (s, cmd+1, d, 0); } - w = snprintf (buf, len-1, "%"ULLFMT"d", n); - if (w<0 || (size_t)w>len) { - buf = malloc (0xff); - snprintf (buf, 0xff, "%"ULLFMT"d", n); + // TODO: keep base here + if (base==16) { + w = snprintf (buf, len-1, "0x%"ULLFMT"x", n); + if (w<0 || (size_t)w>len) { + buf = malloc (0xff); + snprintf (buf, 0xff, "0x%"ULLFMT"x", n); + } + } else { + w = snprintf (buf, len-1, "%"ULLFMT"d", n); + if (w<0 || (size_t)w>len) { + buf = malloc (0xff); + snprintf (buf, 0xff, "%"ULLFMT"d", n); + } } } return buf; diff --git a/shlr/sdb/src/sdb.h b/shlr/sdb/src/sdb.h index 4ad43ec0be..4583afc9a2 100644 --- a/shlr/sdb/src/sdb.h +++ b/shlr/sdb/src/sdb.h @@ -105,7 +105,7 @@ SdbKv *sdb_dump_next (Sdb* s); int sdb_dump_dupnext (Sdb* s, char **key, char **value); /* numeric */ -char *sdb_itoa(ut64 n, char *s); +char *sdb_itoa(ut64 n, char *s, int base); ut64 sdb_atoi(const char *s); ut64 sdb_getn (Sdb* s, const char *key, ut32 *cas); int sdb_setn (Sdb* s, const char *key, ut64 v, ut32 cas); @@ -187,7 +187,8 @@ char *sdb_apop(Sdb *s, const char *key, ut32 *cas); /* Util.c */ int sdb_check_value(const char *s); int sdb_check_key(const char *s); -SDB_API int sdb_isnum (const char *s); +int sdb_isnum (const char *s); +int sdb_numbase (const char *s); #ifdef __cplusplus } diff --git a/shlr/sdb/src/sdba.c b/shlr/sdb/src/sdba.c index 8c7bd99255..0f1f33e5c0 100644 --- a/shlr/sdb/src/sdba.c +++ b/shlr/sdb/src/sdba.c @@ -82,7 +82,7 @@ SDB_API char *sdb_aget(Sdb *s, const char *key, int idx, ut32 *cas) { SDB_API int sdb_ainsn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) { char valstr[64]; - return sdb_ains (s, key, idx, sdb_itoa (val, valstr), cas); + return sdb_ains (s, key, idx, sdb_itoa (val, valstr, 10), cas); } // TODO: done, but there's room for improvement @@ -124,15 +124,14 @@ SDB_API int sdb_ains(Sdb *s, const char *key, int idx, const char *val, ut32 cas SDB_API int sdb_asetn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) { char valstr[64]; - return sdb_aset (s, key, idx, sdb_itoa (val, valstr), cas); + return sdb_aset (s, key, idx, sdb_itoa (val, valstr, 10), cas); } SDB_API int sdb_aaddn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) { - char valstr[64]; - sdb_itoa (val, valstr); - if (sdb_aexists (s, key, valstr)) + char valstr[64], *vs = sdb_itoa (val, valstr, 10); + if (sdb_aexists (s, key, vs)) return 0; - return sdb_aadd (s, key, idx, valstr, cas); + return sdb_aadd (s, key, idx, vs, cas); } SDB_API int sdb_aadd(Sdb *s, const char *key, int idx, const char *val, ut32 cas) { diff --git a/shlr/sdb/src/sdbn.c b/shlr/sdb/src/sdbn.c index 50020b8322..5808810e1f 100644 --- a/shlr/sdb/src/sdbn.c +++ b/shlr/sdb/src/sdbn.c @@ -24,9 +24,10 @@ SDB_API ut64 sdb_getn(Sdb *s, const char *key, ut32 *cas) { } SDB_API int sdb_setn(Sdb *s, const char *key, ut64 v, ut32 cas) { - char b[128]; - sdb_itoa (v, b); - return sdb_set (s, key, b, cas); + char *val, b[128]; + int numbase = sdb_numbase (sdb_getc (s, key, NULL)); + val = sdb_itoa (v, b, numbase); + return sdb_set (s, key, val, cas); } SDB_API ut64 sdb_inc(Sdb *s, const char *key, ut64 n2, ut32 cas) { diff --git a/shlr/sdb/src/util.c b/shlr/sdb/src/util.c index 7f8eb5f046..c90ed82274 100644 --- a/shlr/sdb/src/util.c +++ b/shlr/sdb/src/util.c @@ -54,25 +54,30 @@ SDB_API ut32 sdb_hash(const char *s, int len) { return h; } -/* TODO: find algorithm without strrev */ -/* TODO: try to avoid the use of heap */ -static void __strrev(char *s, int len) { - int i, j = len -1; - for (i=0; i 0); - s[i] = '\0'; - __strrev (s, i); - return s; + memset (s, 0, 65); + if (base==16) { + static const char* lookup = "0123456789abcdef"; + do { + s[i--] = lookup[(n % 16)]; + if (i==0) break; + } while(n/=16); + s[i--] = 'x'; + s[i--] = '0'; + } else { + do { + s[i--] = (n % 10) + '0'; + if (i==0) break; + } while(n/=10); + } + return s+i+1; } SDB_API ut64 sdb_atoi(const char *s) { @@ -121,3 +126,11 @@ SDB_API int sdb_isnum (const char *s) { return 1; return 0; } + +SDB_API int sdb_numbase (const char *s) { + if (!s) return 10; + if (!strncmp (s, "0x", 2)) + return 16; + if (*s=='0' && s[1]) return 8; + return 10; +}