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)
This commit is contained in:
dequis 2015-05-01 02:33:14 -03:00 committed by pancake
parent 68f2d5acdc
commit 08ff4e493a
3 changed files with 13 additions and 14 deletions

View File

@ -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

View File

@ -2782,12 +2782,12 @@ static int cmd_print(void *data, const char *input) {
switch (input[1]) {
case ' ':
case '\0':
for (l=0; l<len; l+=sizeof (time_t))
r_print_date_unix (core->print, core->block+l, sizeof (time_t));
for (l=0; l<len; l+=sizeof (ut32))
r_print_date_unix (core->print, core->block+l, sizeof (ut32));
break;
case 'd':
for (l=0; l<len; l+=4)
r_print_date_dos (core->print, core->block+l, 4);
for (l=0; l<len; l+=sizeof (ut32))
r_print_date_dos (core->print, 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);
}

View File

@ -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!