Apply yet another set of the optimization patches:

Replace strncpy with g_strlcpy.
Add g_strlcat for GTK1 and don't use g_snprintf in GTK1 g_strlcpy
printf family is very slow.

svn path=/trunk/; revision=23273
This commit is contained in:
Anders Broman 2007-10-26 05:42:12 +00:00
parent 300503cdcb
commit 3a16fb20a3
4 changed files with 90 additions and 57 deletions

View File

@ -120,6 +120,8 @@
#include "ipv6-utils.h" #include "ipv6-utils.h"
#include "addr_resolv.h" #include "addr_resolv.h"
#include "filesystem.h" #include "filesystem.h"
#include <epan/strutil.h>
#include <wiretap/file_util.h> #include <wiretap/file_util.h>
#include <epan/prefs.h> #include <epan/prefs.h>
#include <epan/emem.h> #include <epan/emem.h>
@ -355,8 +357,7 @@ static void add_service_name(hashport_t **proto_table, guint port, const char *s
tp->port = port; tp->port = port;
tp->next = NULL; tp->next = NULL;
strncpy(tp->name, service_name, MAXNAMELEN); g_strlcpy(tp->name, service_name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
} }
@ -524,8 +525,7 @@ static gchar *serv_name_lookup(guint port, port_type proto)
/* unknown port */ /* unknown port */
g_snprintf(tp->name, MAXNAMELEN, "%d", port); g_snprintf(tp->name, MAXNAMELEN, "%d", port);
} else { } else {
strncpy(tp->name, servp->s_name, MAXNAMELEN); g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
} }
return (tp->name); return (tp->name);
@ -625,8 +625,7 @@ static gchar *host_name_lookup(guint addr, gboolean *found)
# endif /* AVOID_DNS_TIMEOUT */ # endif /* AVOID_DNS_TIMEOUT */
if (hostp != NULL) { if (hostp != NULL) {
strncpy(tp->name, hostp->h_name, MAXNAMELEN); g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
tp->is_dummy_entry = FALSE; tp->is_dummy_entry = FALSE;
return tp->name; return tp->name;
} }
@ -699,8 +698,7 @@ static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
# endif /* AVOID_DNS_TIMEOUT */ # endif /* AVOID_DNS_TIMEOUT */
if (hostp != NULL) { if (hostp != NULL) {
strncpy(tp->name, hostp->h_name, MAXNAMELEN); g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
tp->is_dummy_entry = FALSE; tp->is_dummy_entry = FALSE;
return tp->name; return tp->name;
} }
@ -898,8 +896,7 @@ static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
if ((cp = strtok(NULL, " \t")) == NULL) if ((cp = strtok(NULL, " \t")) == NULL)
return -1; return -1;
strncpy(eth->name, cp, MAXNAMELEN); g_strlcpy(eth->name, cp, MAXNAMELEN);
eth->name[MAXNAMELEN-1] = '\0';
return 0; return 0;
@ -1065,8 +1062,7 @@ static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
} }
memcpy(tp->addr, addr, sizeof(tp->addr)); memcpy(tp->addr, addr, sizeof(tp->addr));
strncpy(tp->name, name, MAXMANUFLEN); g_strlcpy(tp->name, name, MAXMANUFLEN);
tp->name[MAXMANUFLEN-1] = '\0';
tp->next = NULL; tp->next = NULL;
return; return;
} }
@ -1099,8 +1095,7 @@ static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
} }
memcpy(etp->addr, addr, sizeof(etp->addr)); memcpy(etp->addr, addr, sizeof(etp->addr));
strncpy(etp->name, name, MAXNAMELEN); g_strlcpy(etp->name, name, MAXNAMELEN);
etp->name[MAXNAMELEN-1] = '\0';
etp->next = NULL; etp->next = NULL;
etp->is_dummy_entry = FALSE; etp->is_dummy_entry = FALSE;
@ -1253,8 +1248,7 @@ static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
} }
} }
strncpy(tp->name, name, MAXNAMELEN); g_strlcpy(tp->name, name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
if (new_one) { if (new_one) {
memcpy(tp->addr, addr, sizeof(tp->addr)); memcpy(tp->addr, addr, sizeof(tp->addr));
tp->next = NULL; tp->next = NULL;
@ -1400,8 +1394,7 @@ static gchar *eth_name_lookup(const guint8 *addr)
tp->is_dummy_entry = TRUE; tp->is_dummy_entry = TRUE;
} else { } else {
strncpy(tp->name, eth->name, MAXNAMELEN); g_strlcpy(tp->name, eth->name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
tp->is_dummy_entry = FALSE; tp->is_dummy_entry = FALSE;
} }
@ -1485,8 +1478,7 @@ static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3; ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
} }
strncpy(ipxnet->name, cp, MAXNAMELEN); g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
ipxnet->name[MAXNAMELEN-1] = '\0';
return 0; return 0;
@ -1623,8 +1615,7 @@ static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
} }
tp->addr = addr; tp->addr = addr;
strncpy(tp->name, name, MAXNAMELEN); g_strlcpy(tp->name, name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
tp->next = NULL; tp->next = NULL;
return tp; return tp;
@ -1667,8 +1658,7 @@ static gchar *ipxnet_name_lookup(const guint addr)
g_snprintf(tp->name, MAXNAMELEN, "%X", addr); g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
} else { } else {
strncpy(tp->name, ipxnet->name, MAXNAMELEN); g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
} }
return (tp->name); return (tp->name);
@ -1998,8 +1988,7 @@ extern void add_ipv4_name(guint addr, const gchar *name)
} }
} }
strncpy(tp->name, name, MAXNAMELEN); g_strlcpy(tp->name, name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
if (new_one) { if (new_one) {
tp->addr = addr; tp->addr = addr;
tp->next = NULL; tp->next = NULL;
@ -2041,8 +2030,7 @@ extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
} }
} }
strncpy(tp->name, name, MAXNAMELEN); g_strlcpy(tp->name, name, MAXNAMELEN);
tp->name[MAXNAMELEN-1] = '\0';
if (new_one) { if (new_one) {
tp->addr = *addrp; tp->addr = *addrp;
tp->next = NULL; tp->next = NULL;

View File

@ -41,6 +41,8 @@
#include "osi-utils.h" #include "osi-utils.h"
#include "value_string.h" #include "value_string.h"
#include <epan/strutil.h>
/* Allocate all the data structures for constructing column data, given /* Allocate all the data structures for constructing column data, given
the number of columns. */ the number of columns. */
void void
@ -179,8 +181,7 @@ col_clear(column_info *cinfo, gint el)
if (cinfo->col_data[i] != cinfo->col_buf[i]) { \ if (cinfo->col_data[i] != cinfo->col_buf[i]) { \
/* This was set with "col_set_str()"; copy the string they \ /* This was set with "col_set_str()"; copy the string they \
set it to into the buffer, so we can append to it. */ \ set it to into the buffer, so we can append to it. */ \
strncpy(cinfo->col_buf[i], cinfo->col_data[i], max_len); \ g_strlcpy(cinfo->col_buf[i], cinfo->col_data[i], max_len); \
cinfo->col_buf[i][max_len - 1] = '\0'; \
cinfo->col_data[i] = cinfo->col_buf[i]; \ cinfo->col_data[i] = cinfo->col_buf[i]; \
} }
@ -216,8 +217,7 @@ col_set_str(column_info *cinfo, gint el, const gchar* str)
*/ */
COL_CHECK_APPEND(cinfo, i, max_len); COL_CHECK_APPEND(cinfo, i, max_len);
strncpy(&cinfo->col_buf[i][fence], str, max_len - fence); g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
cinfo->col_buf[i][max_len - 1] = 0;
} else { } else {
/* /*
* There's no fence, so we can just set the column to point * There's no fence, so we can just set the column to point
@ -360,8 +360,7 @@ col_prepend_fstr(column_info *cinfo, gint el, const gchar *format, ...)
/* This was set with "col_set_str()"; which is effectively const */ /* This was set with "col_set_str()"; which is effectively const */
orig = cinfo->col_data[i]; orig = cinfo->col_data[i];
} else { } else {
strncpy(orig_buf, cinfo->col_buf[i], max_len); g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
orig_buf[max_len - 1] = '\0';
orig = orig_buf; orig = orig_buf;
} }
g_vsnprintf(cinfo->col_buf[i], max_len, format, ap); g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
@ -402,8 +401,7 @@ col_prepend_fence_fstr(column_info *cinfo, gint el, const gchar *format, ...)
/* This was set with "col_set_str()"; which is effectively const */ /* This was set with "col_set_str()"; which is effectively const */
orig = cinfo->col_data[i]; orig = cinfo->col_data[i];
} else { } else {
strncpy(orig_buf, cinfo->col_buf[i], max_len); g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
orig_buf[max_len - 1] = '\0';
orig = orig_buf; orig = orig_buf;
} }
g_vsnprintf(cinfo->col_buf[i], max_len, format, ap); g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
@ -456,8 +454,7 @@ col_add_str(column_info *cinfo, gint el, const gchar* str)
*/ */
cinfo->col_data[i] = cinfo->col_buf[i]; cinfo->col_data[i] = cinfo->col_buf[i];
} }
strncpy(&cinfo->col_buf[i][fence], str, max_len - fence); g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
cinfo->col_buf[i][max_len - 1] = 0;
} }
} }
} }
@ -985,8 +982,7 @@ col_set_addr(packet_info *pinfo, int col, address *addr, gboolean is_res,
strcpy(pinfo->cinfo->col_expr[col], "eth.src"); strcpy(pinfo->cinfo->col_expr[col], "eth.src");
else else
strcpy(pinfo->cinfo->col_expr[col], "eth.dst"); strcpy(pinfo->cinfo->col_expr[col], "eth.dst");
strncpy(pinfo->cinfo->col_expr_val[col], ether_to_str(addr->data), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_expr_val[col], ether_to_str(addr->data), COL_MAX_LEN);
pinfo->cinfo->col_expr_val[col][COL_MAX_LEN - 1] = '\0';
break; break;
case AT_IPv4: case AT_IPv4:
@ -994,8 +990,7 @@ col_set_addr(packet_info *pinfo, int col, address *addr, gboolean is_res,
strcpy(pinfo->cinfo->col_expr[col], "ip.src"); strcpy(pinfo->cinfo->col_expr[col], "ip.src");
else else
strcpy(pinfo->cinfo->col_expr[col], "ip.dst"); strcpy(pinfo->cinfo->col_expr[col], "ip.dst");
strncpy(pinfo->cinfo->col_expr_val[col], ip_to_str(addr->data), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_expr_val[col], ip_to_str(addr->data), COL_MAX_LEN);
pinfo->cinfo->col_expr_val[col][COL_MAX_LEN - 1] = '\0';
break; break;
case AT_IPv6: case AT_IPv6:
@ -1003,8 +998,7 @@ col_set_addr(packet_info *pinfo, int col, address *addr, gboolean is_res,
strcpy(pinfo->cinfo->col_expr[col], "ipv6.src"); strcpy(pinfo->cinfo->col_expr[col], "ipv6.src");
else else
strcpy(pinfo->cinfo->col_expr[col], "ipv6.dst"); strcpy(pinfo->cinfo->col_expr[col], "ipv6.dst");
strncpy(pinfo->cinfo->col_expr_val[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_expr_val[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN);
pinfo->cinfo->col_expr_val[col][COL_MAX_LEN - 1] = '\0';
break; break;
case AT_ATALK: case AT_ATALK:
@ -1051,14 +1045,14 @@ col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src)
case PT_SCTP: case PT_SCTP:
if (is_res) if (is_res)
strncpy(pinfo->cinfo->col_buf[col], get_sctp_port(port), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_buf[col], get_sctp_port(port), COL_MAX_LEN);
else else
g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
break; break;
case PT_TCP: case PT_TCP:
if (is_res) if (is_res)
strncpy(pinfo->cinfo->col_buf[col], get_tcp_port(port), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_buf[col], get_tcp_port(port), COL_MAX_LEN);
else else
g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
if (is_src) if (is_src)
@ -1071,7 +1065,7 @@ col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src)
case PT_UDP: case PT_UDP:
if (is_res) if (is_res)
strncpy(pinfo->cinfo->col_buf[col], get_udp_port(port), COL_MAX_LEN); g_strlcpy(pinfo->cinfo->col_buf[col], get_udp_port(port), COL_MAX_LEN);
else else
g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
if (is_src) if (is_src)

View File

@ -952,19 +952,69 @@ convert_string_case(const char *string, gboolean case_insensitive)
return out_string; return out_string;
} }
/* g_strlcat() does not exist in GLib 1.2[.x] */ /* g_strlcat(), g_strlcpy don't exist in GLib 1.2[.x] */
#if GLIB_MAJOR_VERSION < 2 #if GLIB_MAJOR_VERSION < 2
gsize gsize
g_strlcat(gchar *dst, gchar *src, gsize size) g_strlcat(gchar *dest, gchar *src, gsize dest_size)
{ {
gsize strl; gchar *d = dest;
int strs; const gchar *s = src;
strl=strlen(dst); gsize bytes_left = dest_size;
strs=strlen(src); gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
if(strl<size)
g_snprintf(dst+strl, size-strl, "%s", src); /* Find the end of dst and adjust bytes left but don't go past end */
dst[size-1]=0; while (*d != 0 && bytes_left-- != 0)
return strl+strs; d++;
dlength = d - dest;
bytes_left = dest_size - dlength;
if (bytes_left == 0)
return dlength + strlen (s);
while (*s != 0)
{
if (bytes_left != 1)
{
*d++ = *s;
bytes_left--;
}
s++;
}
*d = 0;
return dlength + (s - src); /* count does not include NUL */
}
/* --------------------------------- */
gsize
g_strlcpy(gchar *dest, const gchar *src, gsize dest_size)
{
gchar *d = dest;
const gchar *s = src;
gsize n = dest_size;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0)
do
{
gchar c = *s++;
*d++ = c;
if (c == 0)
break;
}
while (--n != 0);
/* If not enough room in dest, add NUL and traverse rest of src */
if (n == 0)
{
if (dest_size != 0)
*d = 0;
while (*s++)
;
}
return s - src - 1; /* count does not include NUL */
} }
#endif #endif

View File

@ -218,6 +218,7 @@ char * epan_strcasestr(const char *haystack, const char *needle);
/* g_strlcat() does not exist in GLib 1.2[.x] */ /* g_strlcat() does not exist in GLib 1.2[.x] */
#if GLIB_MAJOR_VERSION < 2 #if GLIB_MAJOR_VERSION < 2
gsize g_strlcat(gchar *dst, gchar *src, gsize size); gsize g_strlcat(gchar *dst, gchar *src, gsize size);
gsize g_strlcpy(gchar *dest, const gchar *src, gsize dest_size);
#endif #endif
#if GLIB_MAJOR_VERSION < 2 #if GLIB_MAJOR_VERSION < 2