#ifndef lint
static const char rcsid[] _U_ =
- "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.90 2003-12-29 11:07:17 hannes Exp $ (LBL)";
+ "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.95.2.6 2006-02-08 01:40:09 hannes Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
#include "interface.h"
/*
- * Print out a filename (or other ascii string).
+ * Print out a null-terminated filename (or other ascii string).
* If ep is NULL, assume no truncation check is needed.
* Return true if truncated.
*/
return (n == 0) ? 0 : 1;
}
+/*
+ * Print out a null-padded filename (or other ascii string).
+ * If ep is NULL, assume no truncation check is needed.
+ * Return true if truncated.
+ */
+int
+fn_printzp(register const u_char *s, register u_int n,
+ register const u_char *ep)
+{
+ register int ret;
+ register u_char c;
+
+ ret = 1; /* assume truncated */
+ while (n > 0 && (ep == NULL || s < ep)) {
+ n--;
+ c = *s++;
+ if (c == '\0') {
+ ret = 0;
+ break;
+ }
+ if (!isascii(c)) {
+ c = toascii(c);
+ putchar('M');
+ putchar('-');
+ }
+ if (!isprint(c)) {
+ c ^= 0x40; /* DEL to ?, others to alpha */
+ putchar('^');
+ }
+ putchar(c);
+ }
+ return (n == 0) ? 0 : ret;
+}
+
/*
* Print the timestamp
*/
static unsigned b_sec;
static unsigned b_usec;
- switch(tflag) {
- case 1: /* Default */
+ switch (tflag) {
+
+ case 0: /* Default */
s = (tvp->tv_sec + thiszone) % 86400;
(void)printf("%02d:%02d:%02d.%06u ",
s / 3600, (s % 3600) / 60, s % 60,
(unsigned)tvp->tv_usec);
break;
- case -1: /* Unix timeval style */
+
+ case 1: /* No time stamp */
+ break;
+
+ case 2: /* Unix timeval style */
(void)printf("%u.%06u ",
(unsigned)tvp->tv_sec,
(unsigned)tvp->tv_usec);
break;
- case -2:
+
+ case 3: /* Microseconds since previous packet */
if (b_sec == 0) {
printf("000000 ");
} else {
b_sec = tvp->tv_sec;
b_usec = tvp->tv_usec;
break;
- case -3: /* Default + Date*/
+
+ case 4: /* Default + Date*/
s = (tvp->tv_sec + thiszone) % 86400;
Time = (tvp->tv_sec + thiszone) - s;
tm = gmtime (&Time);
int
print_unknown_data(const u_char *cp,const char *ident,int len)
{
+ if (len < 0) {
+ printf("%sDissector error: print_unknown_data called with negative length",
+ ident);
+ return(0);
+ }
+ if (snapend - cp < len)
+ len = snapend - cp;
+ if (len < 0) {
+ printf("%sDissector error: print_unknown_data called with pointer past end of packet",
+ ident);
+ return(0);
+ }
hex_print(ident,cp,len);
return(1); /* everything is ok */
}
* Convert a token value to a string; use "fmt" if not found.
*/
const char *
-tok2str(register const struct tok *lp, register const char *fmt,
- register int v)
+tok2strbuf(register const struct tok *lp, register const char *fmt,
+ register int v, char *buf, size_t bufsize)
{
- static char buf[128];
-
- while (lp->s != NULL) {
- if (lp->v == v)
- return (lp->s);
- ++lp;
+ if (lp != NULL) {
+ while (lp->s != NULL) {
+ if (lp->v == v)
+ return (lp->s);
+ ++lp;
+ }
}
if (fmt == NULL)
fmt = "#%d";
- (void)snprintf(buf, sizeof(buf), fmt, v);
- return (buf);
+
+ (void)snprintf(buf, bufsize, fmt, v);
+ return (const char *)buf;
+}
+
+/*
+ * Convert a token value to a string; use "fmt" if not found.
+ */
+const char *
+tok2str(register const struct tok *lp, register const char *fmt,
+ register int v)
+{
+ static char buf[4][128];
+ static int idx = 0;
+ char *ret;
+
+ ret = buf[idx];
+ idx = (idx+1) & 3;
+ return tok2strbuf(lp, fmt, v, ret, sizeof(buf[0]));
}
/*
*/
char *
bittok2str(register const struct tok *lp, register const char *fmt,
- register int v)
+ register int v)
{
static char buf[256]; /* our stringbuffer */
int buflen=0;
register int rotbit; /* this is the bit we rotate through all bitpositions */
register int tokval;
- while (lp->s != NULL) {
+ while (lp->s != NULL && lp != NULL) {
tokval=lp->v; /* load our first value */
rotbit=1;
while (rotbit != 0) {
}
void
-safeputs(const char *s)
+safeputs(const char *s, int maxlen)
{
- while (*s) {
+ int idx = 0;
+ while (*s && idx < maxlen) {
safeputchar(*s);
+ idx++;
s++;
}
}