Skip to content

Commit 2996bf7

Browse files
committed
hex/json: use util_json
Factorize the code to util_json.c
1 parent 971adf1 commit 2996bf7

File tree

8 files changed

+50
-39
lines changed

8 files changed

+50
-39
lines changed

src/TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
* use getopt_long() for each subcommand
2-
* move hex/sprintf to util json
32
* add a usage_long() for some complex commands
43
* improve main() using a table of token -> function
54
* factorize the ldexp() from double to uint16_t, XXX libm is not needed when:

src/mfr_fwdata.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ cmd_fwdata(int fd, int pretty) {
1818

1919
json_t *o = json_object();
2020
json_object_set_new(o, "len", json_integer(n));
21-
22-
char *hex = malloc(n * 2 + 1);
23-
for (int i = 0; i < n; i++)
24-
sprintf(hex + 2 * i, "%02X", b[i]);
25-
hex[n * 2] = '\0';
26-
json_object_set_new(o, "hex", json_string(hex));
27-
free(hex);
21+
json_add_hex_ascii(o, "hex", b, (size_t)n);
2822

2923
/* Extract printable runs >= 3 */
3024
json_t *runs = json_array();

src/mfr_ramp_data.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,8 @@ cmd_ramp_data(int fd, int argc, char *const *argv, int pretty) {
3030
return 1;
3131
}
3232

33-
char *hex = (char *) malloc((size_t) n * 2 + 1);
34-
for (int i = 0; i < n; i++)
35-
sprintf(hex + 2 * i, "%02X", buf[i]);
36-
hex[n * 2] = '\0';
37-
3833
json_t *o = json_object();
39-
json_object_set_new(o, "len", json_integer(n));
40-
json_object_set_new(o, "hex", json_string(hex));
41-
free(hex);
34+
json_add_len_and_hex(o, "hex", buf, (size_t)n);
4235

4336
json_print_or_pretty(o, pretty);
4437

src/mfr_snapshot.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,7 @@ cmd_snapshot(int fd, int argc, char * const *argv, int pretty) {
7878

7979
json_t *o = json_object();
8080
json_object_set_new(o, "len", json_integer(n));
81-
82-
char *hex = malloc(n * 2 + 1);
83-
for (int i = 0; i < n; i++)
84-
sprintf(hex + 2 * i, "%02X", blk[i]);
85-
hex[n * 2] = '\0';
86-
87-
json_object_set_new(o, "hex", json_string(hex));
88-
free(hex);
81+
json_add_hex_ascii(o, "hex", blk, (size_t)n);
8982

9083
if (decode && n >= 32) {
9184
json_object_set_new(o, "decoded", decode_snapshot_block(fd, blk, n));

src/mfr_status_data.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,8 @@ cmd_status_data(int fd, int argc, char *const *argv, int pretty) {
3030
return 1;
3131
}
3232

33-
char *hex = (char *) malloc((size_t) n * 2 + 1);
34-
for (int i = 0; i < n; i++)
35-
sprintf(hex + 2 * i, "%02X", buf[i]);
36-
hex[n * 2] = '\0';
37-
3833
json_t *o = json_object();
39-
json_object_set_new(o, "len", json_integer(n));
40-
json_object_set_new(o, "hex", json_string(hex));
41-
free(hex);
34+
json_add_len_and_hex(o, "hex", buf, (size_t)n);
4235

4336
json_print_or_pretty(o, pretty);
4437

src/mfr_user_data.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ cmd_user_data(int fd, int argc, char *const *argv, int pretty) {
2727
json_t *o = json_object();
2828
json_object_set_new(o, "len", json_integer(n));
2929
json_object_set_new(o, "ascii", json_stringn((char *) buf, n));
30-
31-
char *hex = malloc(n * 2 + 1);
32-
for (int i = 0; i < n; i++)
33-
sprintf(hex + 2 * i, "%02X", buf[i]);
34-
hex[n * 2] = '\0';
35-
json_object_set_new(o, "hex", json_string(hex));
36-
free(hex);
30+
json_add_hex_ascii(o, "hex", buf, (size_t)n);
3731

3832
json_print_or_pretty(o, pretty);
3933

src/util_json.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <jansson.h>
77

8+
/* TODO: replace with json_add_len_and_hex() */
89
static void
910
add_hex_ascii(json_t *dst, const uint8_t *buf, int n) {
1011
json_object_set_new(dst, "len", json_integer(n));
@@ -45,3 +46,45 @@ json_print_or_pretty(json_t *o, int pretty) {
4546

4647
json_decref(o);
4748
}
49+
50+
int
51+
json_add_hex_ascii(json_t *dst, const char *key, const void *bufv, size_t n) {
52+
if (!dst || !key || (!bufv && n))
53+
return -1;
54+
if (n > (SIZE_MAX / 2))
55+
return -1;
56+
57+
const uint8_t *buf = (const uint8_t *) bufv;
58+
size_t outlen = n * 2;
59+
60+
char *hex = (char *) malloc(outlen + 1);
61+
if (!hex)
62+
return -1;
63+
64+
static const char *HD = "0123456789ABCDEF";
65+
/* avoid using sprintf() */
66+
for (size_t i = 0; i < n; ++i) {
67+
hex[(i << 1)] = HD[(buf[i] >> 4) & 0xF];
68+
hex[(i << 1) + 1] = HD[buf[i] & 0xF];
69+
}
70+
hex[outlen] = '\0';
71+
72+
json_t *s = json_stringn(hex, outlen);
73+
free(hex);
74+
75+
if (!s)
76+
return -1;
77+
78+
return json_object_set_new(dst, key, s);
79+
}
80+
81+
int
82+
json_add_len_and_hex(json_t *dst, const char *key, const void *buf, size_t n) {
83+
if (!dst)
84+
return -1;
85+
86+
if (json_object_set_new(dst, "len", json_integer((json_int_t) n)) != 0)
87+
return -1;
88+
89+
return json_add_hex_ascii(dst, key, buf, n);
90+
}

src/util_json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
#include <stdint.h>
77

88
void json_print_or_pretty(json_t * o, int pretty);
9+
int json_add_hex_ascii(json_t *dst, const char *key, const void *buf, size_t n);
10+
int json_add_len_and_hex(json_t *dst, const char *key, const void *buf, size_t n);
911

1012
void rd_block_string(int fd, uint8_t cmd, const char *key, json_t *root);

0 commit comments

Comments
 (0)