diff options
author | Anders Broman <anders.broman@ericsson.com> | 2007-10-26 05:42:12 +0000 |
---|---|---|
committer | Anders Broman <anders.broman@ericsson.com> | 2007-10-26 05:42:12 +0000 |
commit | 3a16fb20a3ef22213bac070b871c91a141cbba65 (patch) | |
tree | 4d67f48f3730a2b0a859650113866f141a786480 /epan | |
parent | 300503cdcb355a2415ee44df7350e79113bb3ee4 (diff) |
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
Diffstat (limited to 'epan')
-rw-r--r-- | epan/addr_resolv.c | 44 | ||||
-rw-r--r-- | epan/column-utils.c | 32 | ||||
-rw-r--r-- | epan/strutil.c | 70 | ||||
-rw-r--r-- | epan/strutil.h | 1 |
4 files changed, 90 insertions, 57 deletions
diff --git a/epan/addr_resolv.c b/epan/addr_resolv.c index b19ed043f7..4b2c52ec91 100644 --- a/epan/addr_resolv.c +++ b/epan/addr_resolv.c @@ -120,6 +120,8 @@ #include "ipv6-utils.h" #include "addr_resolv.h" #include "filesystem.h" + +#include <epan/strutil.h> #include <wiretap/file_util.h> #include <epan/prefs.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->next = NULL; - strncpy(tp->name, service_name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, service_name, MAXNAMELEN); } @@ -524,8 +525,7 @@ static gchar *serv_name_lookup(guint port, port_type proto) /* unknown port */ g_snprintf(tp->name, MAXNAMELEN, "%d", port); } else { - strncpy(tp->name, servp->s_name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, servp->s_name, MAXNAMELEN); } return (tp->name); @@ -625,8 +625,7 @@ static gchar *host_name_lookup(guint addr, gboolean *found) # endif /* AVOID_DNS_TIMEOUT */ if (hostp != NULL) { - strncpy(tp->name, hostp->h_name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN); tp->is_dummy_entry = FALSE; return tp->name; } @@ -699,8 +698,7 @@ static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found) # endif /* AVOID_DNS_TIMEOUT */ if (hostp != NULL) { - strncpy(tp->name, hostp->h_name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN); tp->is_dummy_entry = FALSE; 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) return -1; - strncpy(eth->name, cp, MAXNAMELEN); - eth->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(eth->name, cp, MAXNAMELEN); 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)); - strncpy(tp->name, name, MAXMANUFLEN); - tp->name[MAXMANUFLEN-1] = '\0'; + g_strlcpy(tp->name, name, MAXMANUFLEN); tp->next = NULL; return; } @@ -1099,8 +1095,7 @@ static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name) } memcpy(etp->addr, addr, sizeof(etp->addr)); - strncpy(etp->name, name, MAXNAMELEN); - etp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(etp->name, name, MAXNAMELEN); etp->next = NULL; 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); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, name, MAXNAMELEN); if (new_one) { memcpy(tp->addr, addr, sizeof(tp->addr)); tp->next = NULL; @@ -1400,8 +1394,7 @@ static gchar *eth_name_lookup(const guint8 *addr) tp->is_dummy_entry = TRUE; } else { - strncpy(tp->name, eth->name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, eth->name, MAXNAMELEN); 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; } - strncpy(ipxnet->name, cp, MAXNAMELEN); - ipxnet->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(ipxnet->name, cp, MAXNAMELEN); return 0; @@ -1623,8 +1615,7 @@ static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name) } tp->addr = addr; - strncpy(tp->name, name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, name, MAXNAMELEN); tp->next = NULL; return tp; @@ -1667,8 +1658,7 @@ static gchar *ipxnet_name_lookup(const guint addr) g_snprintf(tp->name, MAXNAMELEN, "%X", addr); } else { - strncpy(tp->name, ipxnet->name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN); } return (tp->name); @@ -1998,8 +1988,7 @@ extern void add_ipv4_name(guint addr, const gchar *name) } } - strncpy(tp->name, name, MAXNAMELEN); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, name, MAXNAMELEN); if (new_one) { tp->addr = addr; 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); - tp->name[MAXNAMELEN-1] = '\0'; + g_strlcpy(tp->name, name, MAXNAMELEN); if (new_one) { tp->addr = *addrp; tp->next = NULL; diff --git a/epan/column-utils.c b/epan/column-utils.c index 81aa9951dd..9bddd1e2f6 100644 --- a/epan/column-utils.c +++ b/epan/column-utils.c @@ -41,6 +41,8 @@ #include "osi-utils.h" #include "value_string.h" +#include <epan/strutil.h> + /* Allocate all the data structures for constructing column data, given the number of columns. */ void @@ -179,8 +181,7 @@ col_clear(column_info *cinfo, gint el) if (cinfo->col_data[i] != cinfo->col_buf[i]) { \ /* This was set with "col_set_str()"; copy the string they \ set it to into the buffer, so we can append to it. */ \ - strncpy(cinfo->col_buf[i], cinfo->col_data[i], max_len); \ - cinfo->col_buf[i][max_len - 1] = '\0'; \ + g_strlcpy(cinfo->col_buf[i], cinfo->col_data[i], max_len); \ 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); - strncpy(&cinfo->col_buf[i][fence], str, max_len - fence); - cinfo->col_buf[i][max_len - 1] = 0; + g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence); } else { /* * 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 */ orig = cinfo->col_data[i]; } else { - strncpy(orig_buf, cinfo->col_buf[i], max_len); - orig_buf[max_len - 1] = '\0'; + g_strlcpy(orig_buf, cinfo->col_buf[i], max_len); orig = orig_buf; } 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 */ orig = cinfo->col_data[i]; } else { - strncpy(orig_buf, cinfo->col_buf[i], max_len); - orig_buf[max_len - 1] = '\0'; + g_strlcpy(orig_buf, cinfo->col_buf[i], max_len); orig = orig_buf; } 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]; } - strncpy(&cinfo->col_buf[i][fence], str, max_len - fence); - cinfo->col_buf[i][max_len - 1] = 0; + g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence); } } } @@ -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"); else strcpy(pinfo->cinfo->col_expr[col], "eth.dst"); - strncpy(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'; + g_strlcpy(pinfo->cinfo->col_expr_val[col], ether_to_str(addr->data), COL_MAX_LEN); break; 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"); else strcpy(pinfo->cinfo->col_expr[col], "ip.dst"); - strncpy(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'; + g_strlcpy(pinfo->cinfo->col_expr_val[col], ip_to_str(addr->data), COL_MAX_LEN); break; 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"); else strcpy(pinfo->cinfo->col_expr[col], "ipv6.dst"); - strncpy(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'; + g_strlcpy(pinfo->cinfo->col_expr_val[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN); break; case AT_ATALK: @@ -1051,14 +1045,14 @@ col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src) case PT_SCTP: 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 g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); break; case PT_TCP: 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 g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); if (is_src) @@ -1071,7 +1065,7 @@ col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src) case PT_UDP: 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 g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port); if (is_src) diff --git a/epan/strutil.c b/epan/strutil.c index 3f1b04db86..05ab72bf94 100644 --- a/epan/strutil.c +++ b/epan/strutil.c @@ -952,19 +952,69 @@ convert_string_case(const char *string, gboolean case_insensitive) 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 gsize -g_strlcat(gchar *dst, gchar *src, gsize size) +g_strlcat(gchar *dest, gchar *src, gsize dest_size) { - gsize strl; - int strs; - strl=strlen(dst); - strs=strlen(src); - if(strl<size) - g_snprintf(dst+strl, size-strl, "%s", src); - dst[size-1]=0; - return strl+strs; + gchar *d = dest; + const gchar *s = src; + gsize bytes_left = dest_size; + gsize dlength; /* Logically, MIN (strlen (d), dest_size) */ + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (*d != 0 && bytes_left-- != 0) + 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 diff --git a/epan/strutil.h b/epan/strutil.h index 5e5eac1df4..7c2dae483f 100644 --- a/epan/strutil.h +++ b/epan/strutil.h @@ -218,6 +218,7 @@ char * epan_strcasestr(const char *haystack, const char *needle); /* g_strlcat() does not exist in GLib 1.2[.x] */ #if GLIB_MAJOR_VERSION < 2 gsize g_strlcat(gchar *dst, gchar *src, gsize size); +gsize g_strlcpy(gchar *dest, const gchar *src, gsize dest_size); #endif #if GLIB_MAJOR_VERSION < 2 |