From 08ff4e493a4ab4e5b25d64b32d95290b24b2dc87 Mon Sep 17 00:00:00 2001 From: dequis Date: Fri, 1 May 2015 02:33:14 -0300 Subject: [PATCH] Always use ut32 for unix timestamps. Also handle endianness properly Because it turns out sizeof(time_t) is 8 in 64 bit platforms, but that results in really broken 'pt' results, where half of the lines are 'invalid date' Also, endianness handling was swapped in the case of 'pt' and non-existent in the case of 'rax2 -t' (the latter actually did a weird hack to swap endianness twice and get valid results). Now rax2 -t supports the -e parameter to swap endianness. The usage of r_mem_copyendian with r_print now matches what's done in other places, with !p->big_endian instead of p->big_endian, because 0 means swap and 1 means do nothing. Terrible function. This also fixes some valgrind warnings about uninitialized values (when copying 4 bytes to a 8 byte time_t) --- binr/rax2/rax2.c | 5 ++--- libr/core/cmd_print.c | 14 +++++++------- libr/util/p_date.c | 8 ++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/binr/rax2/rax2.c b/binr/rax2/rax2.c index e181fa2806..50a7042344 100644 --- a/binr/rax2/rax2.c +++ b/binr/rax2/rax2.c @@ -259,9 +259,8 @@ static int rax (char *str, int len, int last) { } else if (flags & 2048) { // -t ut32 n = r_num_math (num, str); RPrint *p = r_print_new (); - p->big_endian = 0; // TODO: honor endian here - r_mem_copyendian ((ut8*) &n, (ut8*) &n, 8, 0); // fix endian here - r_print_date_unix (p, (const ut8*)&n, sizeof (ut64)); + r_mem_copyendian ((ut8*) &n, (ut8*) &n, 4, !(flags & 2)); + r_print_date_unix (p, (const ut8*)&n, sizeof (ut32)); r_print_free (p); return R_TRUE; } else if (flags & 4096) { // -E diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index f407230614..fefbaad7bf 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -2782,12 +2782,12 @@ static int cmd_print(void *data, const char *input) { switch (input[1]) { case ' ': case '\0': - for (l=0; lprint, core->block+l, sizeof (time_t)); + for (l=0; lprint, core->block+l, sizeof (ut32)); break; case 'd': - for (l=0; lprint, core->block+l, 4); + for (l=0; lprint, core->block+l, sizeof (ut32)); break; case 'n': core->print->big_endian = !core->print->big_endian; @@ -2798,9 +2798,9 @@ static int cmd_print(void *data, const char *input) { case '?':{ const char* help_msg[] = { "Usage: pt", "[dn]", "print timestamps", - "pt", "", "print unix time (32 bit `cfg.big_endian`", - "ptd","", "print dos time (32 bit `cfg.big_endian`", - "ptn","", "print ntfs time (64 bit `cfg.big_endian`", + "pt", "", "print unix time (32 bit `cfg.bigendian`)", + "ptd","", "print dos time (32 bit `cfg.bigendian`)", + "ptn","", "print ntfs time (64 bit `cfg.bigendian`)", NULL}; r_core_cmd_help (core, help_msg); } diff --git a/libr/util/p_date.c b/libr/util/p_date.c index d3d2d9429a..4d3224793c 100644 --- a/libr/util/p_date.c +++ b/libr/util/p_date.c @@ -31,13 +31,13 @@ R_API int r_print_date_dos(RPrint *p, ut8 *buf, int len) { } R_API int r_print_date_unix(RPrint *p, const ut8 *buf, int len) { - time_t t; + time_t t = 0; char s[256]; int ret = 0; const struct tm* time; - if (p != NULL && len >= sizeof(t)) { - r_mem_copyendian ((ut8*)&t, buf, sizeof(time_t), p->big_endian); + if (p != NULL && len >= sizeof(ut32)) { + r_mem_copyendian ((ut8*)&t, buf, sizeof(ut32), !p->big_endian); // "%d:%m:%Y %H:%M:%S %z", if (p->datefmt[0]) { t += p->datezone * (60*60); @@ -93,7 +93,7 @@ R_API int r_print_date_w32(RPrint *p, const ut8 *buf, int len) { char datestr[256]; if (p && len >= sizeof (ut64)) { - r_mem_copyendian ((ut8*)&l, buf, sizeof (ut64), p->big_endian); + r_mem_copyendian ((ut8*)&l, buf, sizeof (ut64), !p->big_endian); l /= 10000000; // 100ns to s l = (l > L ? l-L : 0); // isValidUnixTime? t = (time_t) l; // TODO limit above!