Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
/* to_str.c
|
|
|
|
* Routines for utilities to convert various other types to strings.
|
2001-04-01 02:47:56 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-04-01 02:47:56 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-04-01 02:47:56 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-04-01 02:47:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-04-01 02:47:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-04-01 02:47:56 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
2001-04-01 02:47:56 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-02-27 21:41:12 +00:00
|
|
|
#include <string.h>
|
2001-04-01 02:47:56 +00:00
|
|
|
#include <time.h>
|
2010-02-27 21:41:12 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2013-12-19 20:53:44 +00:00
|
|
|
#include "wmem/wmem.h"
|
2011-12-14 02:46:16 +00:00
|
|
|
#include "proto.h"
|
2010-02-27 21:41:12 +00:00
|
|
|
#include "to_str.h"
|
2013-11-09 11:26:41 +00:00
|
|
|
#include "to_str-int.h"
|
2013-03-01 23:53:11 +00:00
|
|
|
#include "strutil.h"
|
2001-04-01 02:47:56 +00:00
|
|
|
|
2007-01-31 23:26:29 +00:00
|
|
|
/*
|
|
|
|
* If a user _does_ pass in a too-small buffer, this is probably
|
|
|
|
* going to be too long to fit. However, even a partial string
|
|
|
|
* starting with "[Buf" should provide enough of a clue to be
|
|
|
|
* useful.
|
|
|
|
*/
|
|
|
|
#define BUF_TOO_SMALL_ERR "[Buffer too small]"
|
|
|
|
|
2013-06-28 19:34:48 +00:00
|
|
|
static inline char
|
2013-06-28 22:52:24 +00:00
|
|
|
low_nibble_of_octet_to_hex(guint8 oct)
|
2013-06-28 19:34:48 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
/* At least one version of Apple's C compiler/linker is buggy, causing
|
|
|
|
a complaint from the linker about the "literal C string section"
|
|
|
|
not ending with '\0' if we initialize a 16-element "char" array with
|
|
|
|
a 16-character string, the fact that initializing such an array with
|
|
|
|
such a string is perfectly legitimate ANSI C nonwithstanding, the 17th
|
|
|
|
'\0' byte in the string nonwithstanding. */
|
|
|
|
static const gchar hex_digits[16] =
|
|
|
|
{ '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
|
|
|
|
|
|
return hex_digits[oct & 0xF];
|
2013-06-28 19:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
2014-09-04 01:39:04 +00:00
|
|
|
byte_to_hex(char *out, guint32 dword)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
*out++ = low_nibble_of_octet_to_hex(dword >> 4);
|
|
|
|
*out++ = low_nibble_of_octet_to_hex(dword);
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 21:41:12 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
word_to_hex(char *out, guint16 word)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
out = byte_to_hex(out, word >> 8);
|
|
|
|
out = byte_to_hex(out, word);
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 20:48:21 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
word_to_hex_npad(char *out, guint16 word)
|
|
|
|
{
|
2011-05-05 20:48:21 +00:00
|
|
|
if (word >= 0x1000)
|
2013-06-28 22:52:24 +00:00
|
|
|
*out++ = low_nibble_of_octet_to_hex((guint8)(word >> 12));
|
2011-05-05 20:48:21 +00:00
|
|
|
if (word >= 0x0100)
|
2013-06-28 22:52:24 +00:00
|
|
|
*out++ = low_nibble_of_octet_to_hex((guint8)(word >> 8));
|
2011-05-05 20:48:21 +00:00
|
|
|
if (word >= 0x0010)
|
2013-06-28 22:52:24 +00:00
|
|
|
*out++ = low_nibble_of_octet_to_hex((guint8)(word >> 4));
|
|
|
|
*out++ = low_nibble_of_octet_to_hex((guint8)(word >> 0));
|
2011-05-05 20:48:21 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-02-27 21:41:12 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
dword_to_hex(char *out, guint32 dword)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
out = byte_to_hex(out, dword >> 24);
|
|
|
|
out = byte_to_hex(out, dword >> 16);
|
|
|
|
out = byte_to_hex(out, dword >> 8);
|
|
|
|
out = byte_to_hex(out, dword);
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 21:41:12 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
dword_to_hex_punct(char *out, guint32 dword, char punct)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
out = byte_to_hex(out, dword >> 24);
|
|
|
|
*out++ = punct;
|
|
|
|
out = byte_to_hex(out, dword >> 16);
|
|
|
|
*out++ = punct;
|
|
|
|
out = byte_to_hex(out, dword >> 8);
|
|
|
|
*out++ = punct;
|
|
|
|
out = byte_to_hex(out, dword);
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 02:46:16 +00:00
|
|
|
/*
|
|
|
|
* This does *not* null-terminate the string. It returns a pointer
|
|
|
|
* to the position in the string following the last character it
|
|
|
|
* puts there, so that the caller can either put the null terminator
|
|
|
|
* in or can append more stuff to the buffer.
|
|
|
|
*
|
|
|
|
* There needs to be at least len * 2 bytes left in the buffer.
|
|
|
|
*/
|
2010-02-27 21:41:12 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
bytes_to_hexstr(char *out, const guint8 *ad, guint32 len)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
guint32 i;
|
2011-12-14 02:46:16 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
if (!ad)
|
|
|
|
REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_hexstr()");
|
2011-12-14 02:03:05 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
out = byte_to_hex(out, ad[i]);
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 02:46:16 +00:00
|
|
|
/*
|
|
|
|
* This does *not* null-terminate the string. It returns a pointer
|
|
|
|
* to the position in the string following the last character it
|
|
|
|
* puts there, so that the caller can either put the null terminator
|
|
|
|
* in or can append more stuff to the buffer.
|
|
|
|
*
|
|
|
|
* There needs to be at least len * 3 - 1 bytes left in the buffer.
|
|
|
|
*/
|
2010-02-27 21:41:12 +00:00
|
|
|
char *
|
2014-09-04 01:39:04 +00:00
|
|
|
bytes_to_hexstr_punct(char *out, const guint8 *ad, guint32 len, char punct)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
guint32 i;
|
2011-12-14 02:46:16 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
if (!ad)
|
|
|
|
REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_hexstr_punct()");
|
2011-12-14 02:46:16 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
out = byte_to_hex(out, ad[0]);
|
|
|
|
for (i = 1; i < len; i++) {
|
|
|
|
*out++ = punct;
|
|
|
|
out = byte_to_hex(out, ad[i]);
|
|
|
|
}
|
|
|
|
return out;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 13:29:35 +00:00
|
|
|
/* Max string length for displaying byte string. */
|
|
|
|
#define MAX_BYTE_STR_LEN 48
|
|
|
|
|
2002-12-09 21:34:58 +00:00
|
|
|
/* Routine to convert a sequence of bytes to a hex string, one byte/two hex
|
|
|
|
* digits at at a time, with a specified punctuation character between
|
2005-08-28 06:44:06 +00:00
|
|
|
* the bytes.
|
2002-12-09 21:34:58 +00:00
|
|
|
*
|
2001-04-01 02:47:56 +00:00
|
|
|
* If punct is '\0', no punctuation is applied (and thus
|
2002-12-10 07:39:48 +00:00
|
|
|
* the resulting string is (len-1) bytes shorter)
|
2001-04-01 02:47:56 +00:00
|
|
|
*/
|
2013-12-19 20:53:44 +00:00
|
|
|
const gchar *
|
2014-09-04 01:39:04 +00:00
|
|
|
bytestring_to_str(wmem_allocator_t *scope, const guint8 *ad, const guint32 len, const char punct)
|
|
|
|
{
|
2013-12-19 20:53:44 +00:00
|
|
|
gchar *buf;
|
2015-01-07 19:43:15 +00:00
|
|
|
guint32 buflen = len;
|
2015-01-07 13:29:35 +00:00
|
|
|
gchar *buf_ptr;
|
|
|
|
int truncated = 0;
|
|
|
|
|
|
|
|
if (!punct)
|
|
|
|
return bytes_to_str(scope, ad, len);
|
2013-12-19 20:53:44 +00:00
|
|
|
|
|
|
|
if (!ad)
|
|
|
|
REPORT_DISSECTOR_BUG("Null pointer passed to bytestring_to_str()");
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return wmem_strdup(scope, "");
|
|
|
|
|
2015-01-07 13:29:35 +00:00
|
|
|
buf=(gchar *)wmem_alloc(scope, MAX_BYTE_STR_LEN+3+1);
|
|
|
|
if (buflen > MAX_BYTE_STR_LEN/3) { /* bd_len > 16 */
|
|
|
|
truncated = 1;
|
|
|
|
buflen = MAX_BYTE_STR_LEN/3;
|
|
|
|
}
|
2013-12-19 20:53:44 +00:00
|
|
|
|
2015-01-07 13:29:35 +00:00
|
|
|
buf_ptr = bytes_to_hexstr_punct(buf, ad, buflen, punct); /* max MAX_BYTE_STR_LEN-1 bytes */
|
2013-12-19 20:53:44 +00:00
|
|
|
|
2015-01-07 13:29:35 +00:00
|
|
|
if (truncated) {
|
|
|
|
*buf_ptr++ = punct; /* 1 byte */
|
|
|
|
buf_ptr = g_stpcpy(buf_ptr, "..."); /* 3 bytes */
|
|
|
|
}
|
2013-12-19 20:53:44 +00:00
|
|
|
|
2015-01-07 13:29:35 +00:00
|
|
|
*buf_ptr = '\0';
|
2013-12-19 20:53:44 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-12-29 05:21:15 +00:00
|
|
|
char *
|
2015-01-10 15:26:55 +00:00
|
|
|
bytes_to_str(wmem_allocator_t *scope, const guint8 *bd, int bd_len)
|
2014-12-29 05:21:15 +00:00
|
|
|
{
|
|
|
|
gchar *cur;
|
|
|
|
gchar *cur_ptr;
|
|
|
|
int truncated = 0;
|
|
|
|
|
|
|
|
if (!bd)
|
|
|
|
REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_str()");
|
|
|
|
|
2015-01-10 15:26:55 +00:00
|
|
|
cur=(gchar *)wmem_alloc(scope, MAX_BYTE_STR_LEN+3+1);
|
2014-12-29 05:21:15 +00:00
|
|
|
if (bd_len <= 0) { cur[0] = '\0'; return cur; }
|
|
|
|
|
|
|
|
if (bd_len > MAX_BYTE_STR_LEN/2) { /* bd_len > 24 */
|
|
|
|
truncated = 1;
|
|
|
|
bd_len = MAX_BYTE_STR_LEN/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_ptr = bytes_to_hexstr(cur, bd, bd_len); /* max MAX_BYTE_STR_LEN bytes */
|
|
|
|
|
|
|
|
if (truncated)
|
|
|
|
cur_ptr = g_stpcpy(cur_ptr, "..."); /* 3 bytes */
|
|
|
|
|
|
|
|
*cur_ptr = '\0'; /* 1 byte */
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2010-02-23 19:28:38 +00:00
|
|
|
static int
|
2014-09-04 01:39:04 +00:00
|
|
|
guint32_to_str_buf_len(const guint32 u)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
if (u >= 1000000000)return 10;
|
|
|
|
if (u >= 100000000) return 9;
|
2014-07-30 02:46:32 +00:00
|
|
|
if (u >= 10000000) return 8;
|
|
|
|
if (u >= 1000000) return 7;
|
|
|
|
if (u >= 100000) return 6;
|
|
|
|
if (u >= 10000) return 5;
|
|
|
|
if (u >= 1000) return 4;
|
|
|
|
if (u >= 100) return 3;
|
|
|
|
if (u >= 10) return 2;
|
2013-12-19 15:27:44 +00:00
|
|
|
|
|
|
|
return 1;
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:12:32 +00:00
|
|
|
static const char fast_strings[][4] = {
|
2013-12-19 15:27:44 +00:00
|
|
|
"0", "1", "2", "3", "4", "5", "6", "7",
|
|
|
|
"8", "9", "10", "11", "12", "13", "14", "15",
|
|
|
|
"16", "17", "18", "19", "20", "21", "22", "23",
|
|
|
|
"24", "25", "26", "27", "28", "29", "30", "31",
|
|
|
|
"32", "33", "34", "35", "36", "37", "38", "39",
|
|
|
|
"40", "41", "42", "43", "44", "45", "46", "47",
|
|
|
|
"48", "49", "50", "51", "52", "53", "54", "55",
|
|
|
|
"56", "57", "58", "59", "60", "61", "62", "63",
|
|
|
|
"64", "65", "66", "67", "68", "69", "70", "71",
|
|
|
|
"72", "73", "74", "75", "76", "77", "78", "79",
|
|
|
|
"80", "81", "82", "83", "84", "85", "86", "87",
|
|
|
|
"88", "89", "90", "91", "92", "93", "94", "95",
|
|
|
|
"96", "97", "98", "99", "100", "101", "102", "103",
|
|
|
|
"104", "105", "106", "107", "108", "109", "110", "111",
|
|
|
|
"112", "113", "114", "115", "116", "117", "118", "119",
|
|
|
|
"120", "121", "122", "123", "124", "125", "126", "127",
|
|
|
|
"128", "129", "130", "131", "132", "133", "134", "135",
|
|
|
|
"136", "137", "138", "139", "140", "141", "142", "143",
|
|
|
|
"144", "145", "146", "147", "148", "149", "150", "151",
|
|
|
|
"152", "153", "154", "155", "156", "157", "158", "159",
|
|
|
|
"160", "161", "162", "163", "164", "165", "166", "167",
|
|
|
|
"168", "169", "170", "171", "172", "173", "174", "175",
|
|
|
|
"176", "177", "178", "179", "180", "181", "182", "183",
|
|
|
|
"184", "185", "186", "187", "188", "189", "190", "191",
|
|
|
|
"192", "193", "194", "195", "196", "197", "198", "199",
|
|
|
|
"200", "201", "202", "203", "204", "205", "206", "207",
|
|
|
|
"208", "209", "210", "211", "212", "213", "214", "215",
|
|
|
|
"216", "217", "218", "219", "220", "221", "222", "223",
|
|
|
|
"224", "225", "226", "227", "228", "229", "230", "231",
|
|
|
|
"232", "233", "234", "235", "236", "237", "238", "239",
|
|
|
|
"240", "241", "242", "243", "244", "245", "246", "247",
|
|
|
|
"248", "249", "250", "251", "252", "253", "254", "255"
|
2010-02-23 19:28:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-09-04 01:39:04 +00:00
|
|
|
guint32_to_str_buf(guint32 u, gchar *buf, int buf_len)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
int str_len = guint32_to_str_buf_len(u)+1;
|
2010-02-23 19:28:38 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
gchar *bp = &buf[str_len];
|
2010-02-23 19:28:38 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
if (buf_len < str_len) {
|
|
|
|
g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
|
|
|
|
return;
|
|
|
|
}
|
2010-02-23 19:28:38 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
*--bp = '\0';
|
2010-02-23 19:28:38 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
uint_to_str_back(bp, u);
|
2010-02-23 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2001-04-01 02:47:56 +00:00
|
|
|
#define PLURALIZE(n) (((n) > 1) ? "s" : "")
|
|
|
|
#define COMMA(do_it) ((do_it) ? ", " : "")
|
|
|
|
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
/*
|
|
|
|
* Maximum length of a string showing days/hours/minutes/seconds.
|
|
|
|
* (Does not include the terminating '\0'.)
|
2008-09-30 02:56:11 +00:00
|
|
|
* Includes space for a '-' sign for any negative components.
|
|
|
|
* -12345 days, 12 hours, 12 minutes, 12.123 seconds
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
*/
|
2005-04-15 21:38:42 +00:00
|
|
|
#define TIME_SECS_LEN (10+1+4+2+2+5+2+2+7+2+2+7+4)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a value in seconds and fractions of a second to a string,
|
|
|
|
* giving time in days, hours, minutes, and seconds, and put the result
|
|
|
|
* into a buffer.
|
2001-09-14 07:10:13 +00:00
|
|
|
* "is_nsecs" says that "frac" is microseconds if true and milliseconds
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
* if false.
|
2005-04-12 19:59:08 +00:00
|
|
|
* If time is negative, add a '-' to all non-null components.
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
*/
|
|
|
|
static void
|
2014-06-16 06:42:14 +00:00
|
|
|
time_secs_to_str_buf(gint32 time_val, const guint32 frac, const gboolean is_nsecs,
|
|
|
|
wmem_strbuf_t *buf)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
int hours, mins, secs;
|
|
|
|
const gchar *msign = "";
|
|
|
|
gboolean do_comma = FALSE;
|
|
|
|
|
|
|
|
if(time_val == G_MININT32) { /* That Which Shall Not Be Negated */
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "Unable to cope with time value %d", time_val);
|
2013-12-19 15:27:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(time_val < 0){
|
|
|
|
time_val = -time_val;
|
|
|
|
msign = "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
secs = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
mins = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
hours = time_val % 24;
|
|
|
|
time_val /= 24;
|
|
|
|
|
|
|
|
if (time_val != 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u day%s", msign, time_val, PLURALIZE(time_val));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
msign="";
|
|
|
|
}
|
|
|
|
if (hours != 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%s%u hour%s", COMMA(do_comma), msign, hours, PLURALIZE(hours));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
msign="";
|
|
|
|
}
|
|
|
|
if (mins != 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%s%u minute%s", COMMA(do_comma), msign, mins, PLURALIZE(mins));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
msign="";
|
|
|
|
}
|
|
|
|
if (secs != 0 || frac != 0) {
|
|
|
|
if (frac != 0) {
|
|
|
|
if (is_nsecs)
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%s%u.%09u seconds", COMMA(do_comma), msign, secs, frac);
|
2013-12-19 15:27:44 +00:00
|
|
|
else
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%s%u.%03u seconds", COMMA(do_comma), msign, secs, frac);
|
2013-12-19 15:27:44 +00:00
|
|
|
} else
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%s%u second%s", COMMA(do_comma), msign, secs, PLURALIZE(secs));
|
2013-12-19 15:27:44 +00:00
|
|
|
}
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
time_secs_to_str(wmem_allocator_t *scope, const gint32 time_val)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
{
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_t *buf;
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
if (time_val == 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
return wmem_strdup(scope, "0 seconds");
|
2013-12-19 15:27:44 +00:00
|
|
|
}
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1, TIME_SECS_LEN+1);
|
|
|
|
|
|
|
|
time_secs_to_str_buf(time_val, 0, FALSE, buf);
|
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 02:07:17 +00:00
|
|
|
static void
|
2014-06-16 17:54:20 +00:00
|
|
|
time_secs_to_str_buf_unsigned(guint32 time_val, const guint32 frac, const gboolean is_nsecs,
|
|
|
|
wmem_strbuf_t *buf)
|
2009-06-25 02:07:17 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
int hours, mins, secs;
|
|
|
|
gboolean do_comma = FALSE;
|
|
|
|
|
|
|
|
secs = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
mins = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
hours = time_val % 24;
|
|
|
|
time_val /= 24;
|
|
|
|
|
|
|
|
if (time_val != 0) {
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%u day%s", time_val, PLURALIZE(time_val));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
}
|
|
|
|
if (hours != 0) {
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u hour%s", COMMA(do_comma), hours, PLURALIZE(hours));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
}
|
|
|
|
if (mins != 0) {
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u minute%s", COMMA(do_comma), mins, PLURALIZE(mins));
|
2013-12-19 15:27:44 +00:00
|
|
|
do_comma = TRUE;
|
|
|
|
}
|
|
|
|
if (secs != 0 || frac != 0) {
|
|
|
|
if (frac != 0) {
|
|
|
|
if (is_nsecs)
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u.%09u seconds", COMMA(do_comma), secs, frac);
|
2013-12-19 15:27:44 +00:00
|
|
|
else
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u.%03u seconds", COMMA(do_comma), secs, frac);
|
2013-12-19 15:27:44 +00:00
|
|
|
} else
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
|
2013-12-19 15:27:44 +00:00
|
|
|
}
|
2009-06-25 02:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2014-06-16 17:54:20 +00:00
|
|
|
time_secs_to_str_unsigned(wmem_allocator_t *scope, const guint32 time_val)
|
2009-06-25 02:07:17 +00:00
|
|
|
{
|
2014-06-16 17:54:20 +00:00
|
|
|
wmem_strbuf_t *buf;
|
2009-06-25 02:07:17 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
if (time_val == 0) {
|
2014-06-16 17:54:20 +00:00
|
|
|
return wmem_strdup(scope, "0 seconds");
|
2013-12-19 15:27:44 +00:00
|
|
|
}
|
2009-06-25 02:07:17 +00:00
|
|
|
|
2014-06-16 17:54:20 +00:00
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1, TIME_SECS_LEN+1);
|
|
|
|
|
|
|
|
time_secs_to_str_buf_unsigned(time_val, 0, FALSE, buf);
|
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
2009-06-25 02:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-13 00:27:51 +00:00
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
time_msecs_to_str(wmem_allocator_t *scope, gint32 time_val)
|
2001-07-13 00:27:51 +00:00
|
|
|
{
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_t *buf;
|
2013-12-19 15:27:44 +00:00
|
|
|
int msecs;
|
|
|
|
|
|
|
|
if (time_val == 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
return wmem_strdup(scope, "0 seconds");
|
2013-12-19 15:27:44 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1+3+1, TIME_SECS_LEN+1+3+1);
|
|
|
|
|
|
|
|
if (time_val<0) {
|
2013-12-19 15:27:44 +00:00
|
|
|
/* oops we got passed a negative time */
|
|
|
|
time_val= -time_val;
|
|
|
|
msecs = time_val % 1000;
|
|
|
|
time_val /= 1000;
|
|
|
|
time_val= -time_val;
|
|
|
|
} else {
|
|
|
|
msecs = time_val % 1000;
|
|
|
|
time_val /= 1000;
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:42:14 +00:00
|
|
|
time_secs_to_str_buf(time_val, msecs, FALSE, buf);
|
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
2001-07-13 00:27:51 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:13:09 +00:00
|
|
|
static const char mon_names[12][4] = {
|
2001-04-01 02:47:56 +00:00
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec"
|
|
|
|
};
|
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
static const gchar *
|
|
|
|
get_zonename(struct tm *tmp)
|
|
|
|
{
|
2010-12-01 01:31:49 +00:00
|
|
|
#if defined(HAVE_TM_ZONE)
|
|
|
|
return tmp->tm_zone;
|
|
|
|
#else
|
|
|
|
if ((tmp->tm_isdst != 0) && (tmp->tm_isdst != 1)) {
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
# if defined(HAVE_TZNAME)
|
|
|
|
return tzname[tmp->tm_isdst];
|
|
|
|
|
|
|
|
# elif defined(_WIN32)
|
|
|
|
/* Windows C Runtime: */
|
|
|
|
/* _tzname is encoded using the "system default ansi code page" */
|
|
|
|
/* ("which is not necessarily the same as the C library locale"). */
|
|
|
|
/* So: _tzname must be converted to UTF8 before use. */
|
|
|
|
/* Alternative: use Windows GetTimeZoneInformation() to get the */
|
2013-12-19 15:27:44 +00:00
|
|
|
/* timezone name in UTF16 and convert same to UTF8. */
|
2010-12-01 01:31:49 +00:00
|
|
|
/* XXX: the result is that the timezone name will be based upon the */
|
|
|
|
/* system code page (iow: the charset of the system). */
|
|
|
|
/* Since Wireshark is not internationalized, it would seem more */
|
|
|
|
/* correct to show the timezone name in English, no matter what */
|
2013-12-19 15:27:44 +00:00
|
|
|
/* the system code page, but I don't how to do that (or if it's */
|
|
|
|
/* really even possible). */
|
|
|
|
/* In any case converting to UTF8 presumably at least keeps GTK */
|
|
|
|
/* happy. (A bug was reported wherein Wireshark crashed in GDK */
|
|
|
|
/* on a "Japanese version of Windows XP" when trying to copy */
|
|
|
|
/* the date/time string (containing a copy of _tz_name) to the */
|
|
|
|
/* clipboard). */
|
2010-12-01 01:31:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
static char *ws_tzname[2] = {NULL, NULL};
|
|
|
|
|
2010-12-21 21:01:01 +00:00
|
|
|
/* The g_malloc'd value returned from g_locale_to_utf8() is */
|
|
|
|
/* cached for all further use so there's no need to ever */
|
|
|
|
/* g_free() that value. */
|
2010-12-01 01:31:49 +00:00
|
|
|
if (ws_tzname[tmp->tm_isdst] == NULL) {
|
|
|
|
ws_tzname[tmp->tm_isdst] = g_locale_to_utf8(_tzname[tmp->tm_isdst], -1, NULL, NULL, NULL);
|
|
|
|
if (ws_tzname[tmp->tm_isdst] == NULL) {
|
|
|
|
ws_tzname[tmp->tm_isdst] = "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ws_tzname[tmp->tm_isdst];
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
return tmp->tm_isdst ? "?DT" : "?ST";
|
|
|
|
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-01 02:47:56 +00:00
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolute_time_display_e fmt,
|
2013-12-19 15:27:44 +00:00
|
|
|
gboolean show_zone)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
struct tm *tmp = NULL;
|
|
|
|
const char *zonename = "???";
|
|
|
|
gchar *buf = NULL;
|
2001-04-01 02:47:56 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
switch (fmt) {
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
tmp = gmtime(&abs_time->secs);
|
|
|
|
zonename = "UTC";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
tmp = localtime(&abs_time->secs);
|
|
|
|
if (tmp) {
|
|
|
|
zonename = get_zonename(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-12-19 15:27:44 +00:00
|
|
|
if (tmp) {
|
|
|
|
switch (fmt) {
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d.%09ld %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d.%09ld",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d.%09ld %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d.%09ld",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup(scope, "Not representable");
|
2013-12-19 15:27:44 +00:00
|
|
|
return buf;
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, const absolute_time_display_e fmt,
|
2013-12-19 15:27:44 +00:00
|
|
|
gboolean show_zone)
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
struct tm *tmp = NULL;
|
|
|
|
const char *zonename = "???";
|
|
|
|
gchar *buf = NULL;
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
switch (fmt) {
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
tmp = gmtime(&abs_time);
|
|
|
|
zonename = "UTC";
|
|
|
|
break;
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
tmp = localtime(&abs_time);
|
|
|
|
if (tmp) {
|
|
|
|
zonename = get_zonename(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-12-19 15:27:44 +00:00
|
|
|
if (tmp) {
|
|
|
|
switch (fmt) {
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup(scope, "Not representable");
|
2013-12-19 15:27:44 +00:00
|
|
|
return buf;
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-03 18:18:50 +00:00
|
|
|
display_signed_time(gchar *buf, int buflen, const gint32 sec, gint32 frac,
|
2013-12-19 15:27:44 +00:00
|
|
|
const to_str_time_res_t units)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2014-04-30 19:39:54 +00:00
|
|
|
/* this buffer is not NUL terminated */
|
|
|
|
gint8 num_buf[16]; /* max: '-2147483648', '.1000000000' */
|
|
|
|
gint8 *num_end = &num_buf[16];
|
|
|
|
gint8 *num_ptr;
|
|
|
|
int num_len;
|
|
|
|
|
|
|
|
if (buflen < 1)
|
|
|
|
return;
|
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
/* If the fractional part of the time stamp is negative,
|
2001-04-01 02:47:56 +00:00
|
|
|
print its absolute value and, if the seconds part isn't
|
|
|
|
(the seconds part should be zero in that case), stick
|
|
|
|
a "-" in front of the entire time stamp. */
|
2001-09-14 07:10:13 +00:00
|
|
|
if (frac < 0) {
|
|
|
|
frac = -frac;
|
2009-07-17 15:59:24 +00:00
|
|
|
if (sec >= 0) {
|
2009-07-20 12:40:55 +00:00
|
|
|
buf[0] = '-';
|
2009-07-17 15:59:24 +00:00
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
}
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = int_to_str_back(num_end, sec);
|
|
|
|
|
|
|
|
num_len = MIN((int) (num_end - num_ptr), buflen);
|
|
|
|
memcpy(buf, num_ptr, num_len);
|
|
|
|
buf += num_len;
|
|
|
|
buflen -= num_len;
|
|
|
|
|
|
|
|
switch (units) {
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_SECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
default:
|
|
|
|
/* no fraction */
|
|
|
|
num_ptr = NULL;
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_DSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 1);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_CSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 2);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_MSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 3);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_USECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 6);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_NSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 9);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
}
|
2014-04-30 19:39:54 +00:00
|
|
|
|
|
|
|
if (num_ptr != NULL)
|
|
|
|
{
|
|
|
|
*(--num_ptr) = '.';
|
|
|
|
|
|
|
|
num_len = MIN((int) (num_end - num_ptr), buflen);
|
|
|
|
memcpy(buf, num_ptr, num_len);
|
|
|
|
buf += num_len;
|
|
|
|
buflen -= num_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to NUL terminate, we know that buffer had at least 1 byte */
|
|
|
|
if (buflen == 0)
|
|
|
|
buf--;
|
|
|
|
*buf = '\0';
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2006-12-05 03:19:51 +00:00
|
|
|
|
|
|
|
void
|
2010-04-03 18:18:50 +00:00
|
|
|
display_epoch_time(gchar *buf, int buflen, const time_t sec, gint32 frac,
|
2013-12-19 15:27:44 +00:00
|
|
|
const to_str_time_res_t units)
|
2006-12-05 03:19:51 +00:00
|
|
|
{
|
|
|
|
double elapsed_secs;
|
|
|
|
|
2007-01-29 20:25:26 +00:00
|
|
|
elapsed_secs = difftime(sec,(time_t)0);
|
2006-12-05 03:19:51 +00:00
|
|
|
|
|
|
|
/* This code copied from display_signed_time; keep it in case anyone
|
|
|
|
is looking at captures from before 1970 (???).
|
|
|
|
If the fractional part of the time stamp is negative,
|
|
|
|
print its absolute value and, if the seconds part isn't
|
|
|
|
(the seconds part should be zero in that case), stick
|
|
|
|
a "-" in front of the entire time stamp. */
|
|
|
|
if (frac < 0) {
|
|
|
|
frac = -frac;
|
2009-07-17 15:59:24 +00:00
|
|
|
if (elapsed_secs >= 0) {
|
|
|
|
if (buflen < 1) {
|
2013-12-19 15:27:44 +00:00
|
|
|
return;
|
2009-07-17 15:59:24 +00:00
|
|
|
}
|
2009-07-20 12:40:55 +00:00
|
|
|
buf[0] = '-';
|
2009-07-17 15:59:24 +00:00
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
}
|
2006-12-05 03:19:51 +00:00
|
|
|
}
|
|
|
|
switch (units) {
|
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_SECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f", elapsed_secs);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_DSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%01d", elapsed_secs, frac);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_CSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%02d", elapsed_secs, frac);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_MSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%03d", elapsed_secs, frac);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_USECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%06d", elapsed_secs, frac);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_NSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%09d", elapsed_secs, frac);
|
|
|
|
break;
|
2006-12-05 03:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
/*
|
|
|
|
* Display a relative time as days/hours/minutes/seconds.
|
|
|
|
*/
|
2001-04-01 02:47:56 +00:00
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
rel_time_to_str(wmem_allocator_t *scope, const nstime_t *rel_time)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
{
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_t *buf;
|
2010-01-28 18:45:46 +00:00
|
|
|
gint32 time_val;
|
2001-09-14 07:10:13 +00:00
|
|
|
gint32 nsec;
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
/* If the nanoseconds part of the time stamp is negative,
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
print its absolute value and, if the seconds part isn't
|
|
|
|
(the seconds part should be zero in that case), stick
|
|
|
|
a "-" in front of the entire time stamp. */
|
2010-01-28 18:45:46 +00:00
|
|
|
time_val = (gint) rel_time->secs;
|
2001-09-14 07:10:13 +00:00
|
|
|
nsec = rel_time->nsecs;
|
2010-01-28 18:45:46 +00:00
|
|
|
if (time_val == 0 && nsec == 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
return wmem_strdup(scope, "0.000000000 seconds");
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
2014-06-16 06:42:14 +00:00
|
|
|
|
|
|
|
buf = wmem_strbuf_sized_new(scope, 1+TIME_SECS_LEN+1+6+1, 1+TIME_SECS_LEN+1+6+1);
|
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
if (nsec < 0) {
|
|
|
|
nsec = -nsec;
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_c(buf, '-');
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
|
|
|
/*
|
2001-09-14 07:10:13 +00:00
|
|
|
* We assume here that "rel_time->secs" is negative
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
* or zero; if it's not, the time stamp is bogus,
|
|
|
|
* with a positive seconds and negative microseconds.
|
|
|
|
*/
|
2010-01-28 18:45:46 +00:00
|
|
|
time_val = (gint) -rel_time->secs;
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 06:42:14 +00:00
|
|
|
time_secs_to_str_buf(time_val, nsec, TRUE, buf);
|
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
#define REL_TIME_SECS_LEN (1+10+1+9+1)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it. Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).
Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()". Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a relative time as seconds.
|
|
|
|
*/
|
|
|
|
gchar *
|
2014-06-16 17:54:20 +00:00
|
|
|
rel_time_to_secs_str(wmem_allocator_t *scope, const nstime_t *rel_time)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
gchar *buf;
|
2005-07-22 08:27:20 +00:00
|
|
|
|
2014-06-16 17:54:20 +00:00
|
|
|
buf=(gchar *)wmem_alloc(scope, REL_TIME_SECS_LEN);
|
2005-07-22 08:27:20 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
display_signed_time(buf, REL_TIME_SECS_LEN, (gint32) rel_time->secs,
|
|
|
|
rel_time->nsecs, TO_STR_TIME_RES_T_NSECS);
|
|
|
|
return buf;
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
/*
|
|
|
|
* Generates a string representing the bits in a bitfield at "bit_offset" from an 8 bit boundary
|
|
|
|
* with the length in bits of no_of_bits based on value.
|
|
|
|
* Ex: ..xx x...
|
|
|
|
*/
|
2003-06-23 09:15:08 +00:00
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
char *
|
2012-04-04 21:54:22 +00:00
|
|
|
decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value)
|
2002-12-09 21:34:58 +00:00
|
|
|
{
|
2009-09-06 15:06:41 +00:00
|
|
|
guint64 mask = 0,tmp;
|
|
|
|
char *str;
|
2012-12-09 23:12:59 +00:00
|
|
|
int bit, str_p = 0;
|
2009-09-06 15:06:41 +00:00
|
|
|
int i;
|
2002-12-08 02:32:36 +00:00
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
mask = 1;
|
|
|
|
mask = mask << (no_of_bits-1);
|
2003-01-21 05:04:07 +00:00
|
|
|
|
2012-12-09 23:12:59 +00:00
|
|
|
/* Prepare the string, 256 pos for the bits and zero termination, + 64 for the spaces */
|
2015-01-17 21:55:46 +00:00
|
|
|
str=(char *)wmem_alloc0(wmem_packet_scope(), 256+64);
|
2009-09-06 15:06:41 +00:00
|
|
|
for(bit=0;bit<((int)(bit_offset&0x07));bit++){
|
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-09-06 15:06:41 +00:00
|
|
|
}
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '.';
|
|
|
|
str_p++;
|
2009-09-06 15:06:41 +00:00
|
|
|
}
|
2009-05-15 18:38:13 +00:00
|
|
|
|
|
|
|
/* read the bits for the int */
|
|
|
|
for(i=0;i<no_of_bits;i++){
|
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
if(bit&&(!(bit%8))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
bit++;
|
|
|
|
tmp = value & mask;
|
|
|
|
if(tmp != 0){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '1';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
} else {
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '0';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
mask = mask>>1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;bit%8;bit++){
|
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '.';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2002-12-08 02:32:36 +00:00
|
|
|
|
2001-04-01 02:47:56 +00:00
|
|
|
/* Generate, into "buf", a string showing the bits of a bitfield.
|
|
|
|
Return a pointer to the character after that string. */
|
2005-08-20 02:33:33 +00:00
|
|
|
/*XXX this needs a buf_len check */
|
2001-04-01 02:47:56 +00:00
|
|
|
char *
|
2014-09-17 16:39:22 +00:00
|
|
|
other_decode_bitfield_value(char *buf, const guint64 val, const guint64 mask, const int width)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
int i;
|
2014-09-17 16:39:22 +00:00
|
|
|
guint64 bit;
|
2013-12-19 15:27:44 +00:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
p = buf;
|
2014-10-13 12:17:35 +00:00
|
|
|
bit = G_GUINT64_CONSTANT(1) << (width - 1);
|
2013-12-19 15:27:44 +00:00
|
|
|
for (;;) {
|
|
|
|
if (mask & bit) {
|
|
|
|
/* This bit is part of the field. Show its value. */
|
|
|
|
if (val & bit)
|
|
|
|
*p++ = '1';
|
|
|
|
else
|
|
|
|
*p++ = '0';
|
|
|
|
} else {
|
|
|
|
/* This bit is not part of the field. */
|
|
|
|
*p++ = '.';
|
|
|
|
}
|
|
|
|
bit >>= 1;
|
|
|
|
i++;
|
|
|
|
if (i >= width)
|
|
|
|
break;
|
|
|
|
if (i % 4 == 0)
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
2003-12-08 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2014-09-17 16:39:22 +00:00
|
|
|
decode_bitfield_value(char *buf, const guint64 val, const guint64 mask, const int width)
|
2003-12-08 23:40:13 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
char *p;
|
2003-12-08 23:40:13 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
p = other_decode_bitfield_value(buf, val, mask, width);
|
|
|
|
p = g_stpcpy(p, " = ");
|
2013-08-15 09:33:52 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
return p;
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
/*
|
2013-12-19 15:27:44 +00:00
|
|
|
This function is very fast and this function is called a lot.
|
2015-01-05 16:12:08 +00:00
|
|
|
XXX update the address_to_str stuff to use this function.
|
2013-12-19 15:27:44 +00:00
|
|
|
*/
|
2009-09-06 15:06:41 +00:00
|
|
|
void
|
2010-04-03 18:18:50 +00:00
|
|
|
ip_to_str_buf(const guint8 *ad, gchar *buf, const int buf_len)
|
2009-09-06 15:06:41 +00:00
|
|
|
{
|
|
|
|
register gchar const *p;
|
|
|
|
register gchar *b=buf;
|
|
|
|
|
|
|
|
if (buf_len < MAX_IP_STR_LEN) {
|
2014-09-04 01:39:04 +00:00
|
|
|
g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR ); /* Let the unexpected value alert user */
|
2009-09-06 15:06:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p=fast_strings[*ad++];
|
|
|
|
do {
|
|
|
|
*b++=*p;
|
|
|
|
p++;
|
|
|
|
} while(*p);
|
|
|
|
*b++='.';
|
|
|
|
|
|
|
|
p=fast_strings[*ad++];
|
|
|
|
do {
|
|
|
|
*b++=*p;
|
|
|
|
p++;
|
|
|
|
} while(*p);
|
|
|
|
*b++='.';
|
|
|
|
|
|
|
|
p=fast_strings[*ad++];
|
|
|
|
do {
|
|
|
|
*b++=*p;
|
|
|
|
p++;
|
|
|
|
} while(*p);
|
|
|
|
*b++='.';
|
|
|
|
|
|
|
|
p=fast_strings[*ad];
|
|
|
|
do {
|
|
|
|
*b++=*p;
|
|
|
|
p++;
|
|
|
|
} while(*p);
|
|
|
|
*b=0;
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
gchar *
|
2015-01-08 00:24:52 +00:00
|
|
|
guid_to_str(wmem_allocator_t *scope, const e_guid_t *guid)
|
2014-09-04 01:39:04 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
gchar *buf;
|
2005-07-04 13:04:53 +00:00
|
|
|
|
2015-01-08 00:24:52 +00:00
|
|
|
buf=(gchar *)wmem_alloc(scope, GUID_STR_LEN);
|
2013-12-19 15:27:44 +00:00
|
|
|
return guid_to_str_buf(guid, buf, GUID_STR_LEN);
|
2005-07-04 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
gchar *
|
|
|
|
guid_to_str_buf(const e_guid_t *guid, gchar *buf, int buf_len)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
char *tempptr = buf;
|
|
|
|
|
|
|
|
if (buf_len < GUID_STR_LEN) {
|
|
|
|
g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 37 bytes */
|
|
|
|
tempptr = dword_to_hex(tempptr, guid->data1); /* 8 bytes */
|
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
2014-07-30 02:46:32 +00:00
|
|
|
tempptr = word_to_hex(tempptr, guid->data2); /* 4 bytes */
|
2013-12-19 15:27:44 +00:00
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
2014-07-30 02:46:32 +00:00
|
|
|
tempptr = word_to_hex(tempptr, guid->data3); /* 4 bytes */
|
2013-12-19 15:27:44 +00:00
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
|
|
|
tempptr = bytes_to_hexstr(tempptr, &guid->data4[0], 2); /* 4 bytes */
|
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
|
|
|
tempptr = bytes_to_hexstr(tempptr, &guid->data4[2], 6); /* 12 bytes */
|
|
|
|
|
|
|
|
*tempptr = '\0';
|
|
|
|
return buf;
|
2005-07-04 13:04:53 +00:00
|
|
|
}
|
2013-06-17 21:54:21 +00:00
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
const gchar *
|
|
|
|
port_type_to_str (port_type type)
|
|
|
|
{
|
2013-06-17 21:54:21 +00:00
|
|
|
switch (type) {
|
|
|
|
case PT_NONE: return "NONE";
|
|
|
|
case PT_SCTP: return "SCTP";
|
|
|
|
case PT_TCP: return "TCP";
|
|
|
|
case PT_UDP: return "UDP";
|
|
|
|
case PT_DCCP: return "DCCP";
|
|
|
|
case PT_IPX: return "IPX";
|
|
|
|
case PT_NCP: return "NCP";
|
|
|
|
case PT_EXCHG: return "FC EXCHG";
|
|
|
|
case PT_DDP: return "DDP";
|
|
|
|
case PT_SBCCS: return "FICON SBCCS";
|
|
|
|
case PT_IDP: return "IDP";
|
|
|
|
case PT_TIPC: return "TIPC";
|
|
|
|
case PT_USB: return "USB";
|
|
|
|
case PT_I2C: return "I2C";
|
|
|
|
case PT_IBQP: return "IBQP";
|
|
|
|
case PT_BLUETOOTH: return "BLUETOOTH";
|
2014-07-30 02:46:32 +00:00
|
|
|
default: return "[Unknown]";
|
2013-06-17 21:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 19:34:48 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
oct_to_str_back(char *ptr, guint32 value)
|
|
|
|
{
|
|
|
|
while (value) {
|
|
|
|
*(--ptr) = '0' + (value & 0x7);
|
|
|
|
value >>= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(--ptr) = '0';
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2014-04-30 21:46:18 +00:00
|
|
|
hex_to_str_back(char *ptr, int len, guint32 value)
|
2013-06-28 19:34:48 +00:00
|
|
|
{
|
|
|
|
do {
|
2013-06-28 22:52:24 +00:00
|
|
|
*(--ptr) = low_nibble_of_octet_to_hex(value);
|
2013-06-28 19:34:48 +00:00
|
|
|
value >>= 4;
|
2014-04-30 21:46:18 +00:00
|
|
|
len--;
|
2013-06-28 19:34:48 +00:00
|
|
|
} while (value);
|
|
|
|
|
|
|
|
/* pad */
|
2014-04-30 21:46:18 +00:00
|
|
|
while (len > 0) {
|
2013-06-28 19:34:48 +00:00
|
|
|
*(--ptr) = '0';
|
2014-04-30 21:46:18 +00:00
|
|
|
len--;
|
2013-06-28 19:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*(--ptr) = 'x';
|
|
|
|
*(--ptr) = '0';
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
uint_to_str_back(char *ptr, guint32 value)
|
|
|
|
{
|
|
|
|
char const *p;
|
|
|
|
|
|
|
|
/* special case */
|
|
|
|
if (value == 0)
|
|
|
|
*(--ptr) = '0';
|
|
|
|
|
|
|
|
while (value >= 10) {
|
|
|
|
p = fast_strings[100 + (value % 100)];
|
|
|
|
|
|
|
|
value /= 100;
|
|
|
|
|
|
|
|
*(--ptr) = p[2];
|
|
|
|
*(--ptr) = p[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
*(--ptr) = (value) | '0';
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-04-30 19:39:54 +00:00
|
|
|
char *
|
2014-04-30 21:46:18 +00:00
|
|
|
uint_to_str_back_len(char *ptr, guint32 value, int len)
|
2014-04-30 19:39:54 +00:00
|
|
|
{
|
|
|
|
char *new_ptr;
|
|
|
|
|
|
|
|
new_ptr = uint_to_str_back(ptr, value);
|
|
|
|
|
|
|
|
/* substract from len number of generated characters */
|
2014-05-01 09:10:22 +00:00
|
|
|
len -= (int)(ptr - new_ptr);
|
2014-04-30 19:39:54 +00:00
|
|
|
|
|
|
|
/* pad remaining with '0' */
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
*(--new_ptr) = '0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
2013-06-28 19:34:48 +00:00
|
|
|
char *
|
|
|
|
int_to_str_back(char *ptr, gint32 value)
|
|
|
|
{
|
|
|
|
if (value < 0) {
|
|
|
|
ptr = uint_to_str_back(ptr, -value);
|
|
|
|
*(--ptr) = '-';
|
|
|
|
} else
|
|
|
|
ptr = uint_to_str_back(ptr, value);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|