2019-11-06 20:55:36 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2017-2019 - wargio */
|
|
|
|
|
|
|
|
#include <r_util.h>
|
|
|
|
#include <r_cons.h>
|
|
|
|
|
|
|
|
typedef float ft32;
|
|
|
|
typedef double ft64;
|
|
|
|
|
|
|
|
#define WIRE_VARINT 0 // int32, int64, uint32, uint64, sint32, sint64, bool, enum
|
|
|
|
#define WIRE_64_BIT 1 // fixed64, sfixed64, double
|
|
|
|
#define WIRE_LEN_DELIM 2 // string, bytes, embedded messages, packed repeated fields
|
|
|
|
#define WIRE_START_GRP 3 // groups (deprecated)
|
|
|
|
#define WIRE_END_GRP 4 // groups (deprecated)
|
|
|
|
#define WIRE_32_BIT 5 // fixed32, sfixed32, float
|
|
|
|
|
|
|
|
static const char* s_wire(const ut8 byte) {
|
|
|
|
switch (byte) {
|
|
|
|
case WIRE_VARINT:
|
|
|
|
return "[VARINT]";
|
|
|
|
case WIRE_64_BIT:
|
|
|
|
return "[64_BIT]";
|
|
|
|
case WIRE_LEN_DELIM:
|
|
|
|
return "[LEN_DELIM]";
|
|
|
|
case WIRE_START_GRP:
|
|
|
|
return "[START_GROUP]";
|
|
|
|
case WIRE_END_GRP:
|
|
|
|
return "[END_GROUP]";
|
|
|
|
case WIRE_32_BIT:
|
|
|
|
return "[32_BIT]";
|
|
|
|
default:
|
|
|
|
return "[UNKN]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:13:08 +00:00
|
|
|
static void pad(RStrBuf *sb, ut32 count) {
|
2019-11-06 20:55:36 +00:00
|
|
|
ut32 i;
|
|
|
|
for (i = 0; i < count; i++) {
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, " ");
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_string(const ut8* start, const ut8* end) {
|
|
|
|
while (start < end) {
|
|
|
|
// TODO UTF-8 Support.
|
|
|
|
if (!IS_PRINTABLE (*start)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:13:08 +00:00
|
|
|
static void decode_array(RStrBuf *sb, const ut8* start, const ut8* end) {
|
2019-11-06 20:55:36 +00:00
|
|
|
while (start < end) {
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, "%02x ", *start);
|
2019-11-06 20:55:36 +00:00
|
|
|
start++;
|
|
|
|
}
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, "\n");
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:13:08 +00:00
|
|
|
static void decode_buffer(RStrBuf *sb, const ut8* start, const ut8* end, ut32 padcnt, bool debug) {
|
2019-11-06 20:55:36 +00:00
|
|
|
size_t bytes_read = 0;
|
|
|
|
ut32 var32 = 0;
|
|
|
|
ut64 var64 = 0;
|
|
|
|
const ut8* buffer = start;
|
2020-04-05 13:02:46 +00:00
|
|
|
while (buffer >= start && buffer < end) {
|
2019-11-06 20:55:36 +00:00
|
|
|
if (!*buffer) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-06 22:13:08 +00:00
|
|
|
//ut8 byte = *buffer;
|
2020-04-14 15:21:58 +00:00
|
|
|
ut8 number = buffer[0] >> 3;
|
|
|
|
ut8 wire = buffer[0] & 0x3;
|
2019-11-06 20:55:36 +00:00
|
|
|
buffer++;
|
|
|
|
if (buffer < start || buffer >= end) {
|
|
|
|
eprintf ("\ninvalid buffer pointer.\n");
|
|
|
|
break;
|
2020-04-14 15:21:58 +00:00
|
|
|
} else if (wire > WIRE_32_BIT) {
|
|
|
|
eprintf ("\nunknown wire id (%u).\n", wire);
|
2019-11-06 20:55:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-14 15:21:58 +00:00
|
|
|
if (wire != WIRE_END_GRP) {
|
2019-11-06 22:13:08 +00:00
|
|
|
pad (sb, padcnt);
|
2019-11-06 20:55:36 +00:00
|
|
|
if (debug) {
|
2020-04-14 15:21:58 +00:00
|
|
|
r_strbuf_appendf (sb, "%u %-13s", number, s_wire(wire));
|
2019-11-06 20:55:36 +00:00
|
|
|
} else {
|
2020-04-14 15:21:58 +00:00
|
|
|
r_strbuf_appendf (sb, "%u", number);
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 15:21:58 +00:00
|
|
|
switch (wire) {
|
2019-11-06 20:55:36 +00:00
|
|
|
case WIRE_VARINT:
|
|
|
|
{
|
|
|
|
st64* i = (st64*) &var64;
|
|
|
|
bytes_read = read_u64_leb128 (buffer, end, &var64);
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, ": %"PFMT64u" | %"PFMT64d"\n", var64, *i);
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WIRE_64_BIT:
|
|
|
|
{
|
|
|
|
ft64* f = (ft64*) &var64;
|
|
|
|
st64* i = (st64*) &var64;
|
|
|
|
bytes_read = read_u64_leb128 (buffer, end, &var64);
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, ": %"PFMT64u" | %"PFMT64d" | %f\n", var64, *i, *f);
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WIRE_LEN_DELIM:
|
|
|
|
{
|
|
|
|
bytes_read = read_u64_leb128 (buffer, end, &var64);
|
|
|
|
const ut8* ps = buffer + bytes_read;
|
|
|
|
const ut8* pe = ps + var64;
|
|
|
|
if (ps > buffer && pe <= end) {
|
|
|
|
if (is_string (ps, pe)) {
|
2020-02-18 09:27:12 +00:00
|
|
|
r_strbuf_appendf (sb, ": \"%.*s\"\n", (int)var64, (const char*) ps);
|
2019-11-06 20:55:36 +00:00
|
|
|
} else {
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, " {\n");
|
|
|
|
decode_buffer (sb, ps, pe, padcnt + 1, debug);
|
|
|
|
pad (sb, padcnt);
|
|
|
|
r_strbuf_appendf (sb, "}\n");
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
bytes_read += var64;
|
|
|
|
} else {
|
|
|
|
eprintf ("\ninvalid delimited length (%"PFMT64u").\n", var64);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WIRE_START_GRP:
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, " {\n");
|
2019-11-06 20:55:36 +00:00
|
|
|
padcnt++;
|
|
|
|
break;
|
|
|
|
case WIRE_END_GRP:
|
|
|
|
if (padcnt > 1) {
|
|
|
|
padcnt--;
|
|
|
|
}
|
2019-11-06 22:13:08 +00:00
|
|
|
pad (sb, padcnt);
|
|
|
|
r_strbuf_appendf (sb, "}\n");
|
2019-11-06 20:55:36 +00:00
|
|
|
break;
|
|
|
|
case WIRE_32_BIT:
|
|
|
|
{
|
|
|
|
ft32* f = (ft32*) &var32;
|
|
|
|
st32* i = (st32*) &var32;
|
|
|
|
bytes_read = read_u32_leb128 (buffer, end, &var32);
|
2019-11-06 22:13:08 +00:00
|
|
|
r_strbuf_appendf (sb, ": %u | %d | %f\n", var32, *i, *f);
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-11-06 22:13:08 +00:00
|
|
|
decode_array (sb, buffer - 1, end);
|
2019-11-06 20:55:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer += bytes_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:13:08 +00:00
|
|
|
R_API char *r_protobuf_decode(const ut8* start, const ut64 size, bool debug) {
|
2019-11-06 20:55:36 +00:00
|
|
|
if (!start || !size) {
|
|
|
|
eprintf ("Invalid buffer pointer or size.\n");
|
2019-11-06 22:13:08 +00:00
|
|
|
return NULL;
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|
|
|
|
const ut8* end = start + size;
|
2019-11-06 22:13:08 +00:00
|
|
|
RStrBuf *sb = r_strbuf_new ("");
|
|
|
|
decode_buffer (sb, start, end, 0u, debug);
|
|
|
|
return r_strbuf_drain (sb);
|
2019-11-06 20:55:36 +00:00
|
|
|
}
|