Rizin
unix-like reverse engineering framework and cli tools
num.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 #include <rz_types.h>
6 
7 // check if key exists and if it's a number.. rename?
8 RZ_API bool sdb_num_exists(Sdb *s, const char *key) {
9  const char *o = sdb_const_get(s, key, NULL);
10  return o ? (*o >= '0' && *o <= '9') : false;
11 }
12 
13 RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas) {
14  const char *v = sdb_const_get(s, key, cas);
15  return (!v || *v == '-') ? 0LL : sdb_atoi(v);
16 }
17 
18 RZ_API int sdb_num_add(Sdb *s, const char *key, ut64 v, ut32 cas) {
19  char *val, b[SDB_NUM_BUFSZ];
20  int numbase = sdb_num_base(sdb_const_get(s, key, NULL));
21  val = sdb_itoa(v, b, numbase);
22  return sdb_add(s, key, val, cas);
23 }
24 
25 RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas) {
26  char *val, b[SDB_NUM_BUFSZ];
27  int numbase = sdb_num_base(sdb_const_get(s, key, NULL));
28  val = sdb_itoa(v, b, numbase);
29  return sdb_set(s, key, val, cas);
30 }
31 
32 RZ_API ut64 sdb_num_inc(Sdb *s, const char *key, ut64 n2, ut32 cas) {
33  ut32 c;
34  ut64 n = sdb_num_get(s, key, &c);
35  ut64 res = n + n2;
36  if ((cas && c != cas) || res < n) {
37  return 0LL;
38  }
39  sdb_num_set(s, key, res, cas);
40  return res;
41 }
42 
43 RZ_API ut64 sdb_num_dec(Sdb *s, const char *key, ut64 n2, ut32 cas) {
44  ut32 c;
45  ut64 n = sdb_num_get(s, key, &c);
46  if (cas && c != cas) {
47  return 0LL;
48  }
49  if (n2 > n) {
50  sdb_set(s, key, "0", cas);
51  return 0LL; // XXX must be -1LL?
52  }
53  n -= n2;
54  sdb_num_set(s, key, n, cas);
55  return n;
56 }
57 
58 RZ_API int sdb_num_min(Sdb *db, const char *k, ut64 n, ut32 cas) {
59  const char *a = sdb_const_get(db, k, NULL);
60  return (!a || n < sdb_atoi(a))
61  ? sdb_num_set(db, k, n, cas)
62  : 0;
63 }
64 
65 RZ_API int sdb_num_max(Sdb *db, const char *k, ut64 n, ut32 cas) {
66  const char *a = sdb_const_get(db, k, NULL);
67  return (!a || n > sdb_atoi(a))
68  ? sdb_num_set(db, k, n, cas)
69  : 0;
70 }
71 
72 RZ_API int sdb_bool_set(Sdb *db, const char *str, bool v, ut32 cas) {
73  return sdb_set(db, str, v ? "true" : "false", cas);
74 }
75 
76 RZ_API bool sdb_bool_get(Sdb *db, const char *str, ut32 *cas) {
77  const char *b = sdb_const_get(db, str, cas);
78  return b && (!strcmp(b, "1") || !strcmp(b, "true"));
79 }
80 
81 /* pointers */
82 
83 RZ_API int sdb_ptr_set(Sdb *db, const char *key, void *p, ut32 cas) {
84  return sdb_num_set(db, key, (ut64)(size_t)p, cas);
85 }
86 
87 RZ_API void *sdb_ptr_get(Sdb *db, const char *key, ut32 *cas) {
88  return (void *)(size_t)sdb_num_get(db, key, cas);
89 }
ut16 val
Definition: armass64_const.h:6
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
uint32_t ut32
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
void * p
Definition: libc.cpp:67
int n
Definition: mipsasm.c:19
RZ_API void * sdb_ptr_get(Sdb *db, const char *key, ut32 *cas)
Definition: num.c:87
RZ_API int sdb_ptr_set(Sdb *db, const char *key, void *p, ut32 cas)
Definition: num.c:83
RZ_API int sdb_num_max(Sdb *db, const char *k, ut64 n, ut32 cas)
Definition: num.c:65
RZ_API int sdb_num_add(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:18
RZ_API bool sdb_bool_get(Sdb *db, const char *str, ut32 *cas)
Definition: num.c:76
RZ_API bool sdb_num_exists(Sdb *s, const char *key)
Definition: num.c:8
RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:25
RZ_API int sdb_num_min(Sdb *db, const char *k, ut64 n, ut32 cas)
Definition: num.c:58
RZ_API int sdb_bool_set(Sdb *db, const char *str, bool v, ut32 cas)
Definition: num.c:72
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13
RZ_API ut64 sdb_num_dec(Sdb *s, const char *key, ut64 n2, ut32 cas)
Definition: num.c:43
RZ_API ut64 sdb_num_inc(Sdb *s, const char *key, ut64 n2, ut32 cas)
Definition: num.c:32
static RzSocket * s
Definition: rtr.c:28
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
RZ_API int sdb_add(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:351
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88
#define SDB_NUM_BUFSZ
Definition: sdb.h:51
RZ_API int sdb_num_base(const char *s)
Definition: util.c:221
int size_t
Definition: sftypes.h:40
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()