From 019446a474253b79eb75790c4fed85f383bb9c37 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 30 Nov 2015 00:31:12 -0800 Subject: Handle Date: headers differently from other headers. Handle Date: outside the "loop over magic strings" loop; that way, we don't have to compare against "Date:" twice. Don't use strlen() on constant strings - sizeof string - 1 lets us calculate the length at compile time. Change-Id: I7e4ff2f9762f96749a39609efa76430e5915d4f4 Reviewed-on: https://code.wireshark.org/review/12302 Reviewed-by: Guy Harris --- wiretap/ascendtext.c | 124 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 43 deletions(-) (limited to 'wiretap') diff --git a/wiretap/ascendtext.c b/wiretap/ascendtext.c index 7d9d27f04e..7697c45e40 100644 --- a/wiretap/ascendtext.c +++ b/wiretap/ascendtext.c @@ -50,27 +50,30 @@ typedef struct _ascend_magic_string { guint type; - const gchar *strptr; + const gchar *strptr; + size_t strlength; } ascend_magic_string; -#define ASCEND_MAGIC_STRINGS 11 -#define ASCEND_DATE "Date:" - /* these magic strings signify the headers of a supported debug commands */ +#define ASCEND_MAGIC_ENTRY(type, string) \ + { type, string, sizeof string - 1 } /* strlen of a constant string */ static const ascend_magic_string ascend_magic[] = { - { ASCEND_PFX_ISDN_X, "PRI-XMIT-" }, - { ASCEND_PFX_ISDN_R, "PRI-RCV-" }, - { ASCEND_PFX_WDS_X, "XMIT-" }, - { ASCEND_PFX_WDS_R, "RECV-" }, - { ASCEND_PFX_WDS_X, "XMIT:" }, - { ASCEND_PFX_WDS_R, "RECV:" }, - { ASCEND_PFX_WDS_X, "PPP-OUT" }, - { ASCEND_PFX_WDS_R, "PPP-IN" }, - { ASCEND_PFX_WDD, ASCEND_DATE }, - { ASCEND_PFX_WDD, "WD_DIALOUT_DISP:" }, - { ASCEND_PFX_ETHER, "ETHER" }, + ASCEND_MAGIC_ENTRY(ASCEND_PFX_ISDN_X, "PRI-XMIT-"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_ISDN_R, "PRI-RCV-"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_X, "XMIT-"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_R, "RECV-"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_X, "XMIT:"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_R, "RECV:"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_X, "PPP-OUT"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDS_R, "PPP-IN"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_WDD, "WD_DIALOUT_DISP:"), + ASCEND_MAGIC_ENTRY(ASCEND_PFX_ETHER, "ETHER"), }; +#define ASCEND_MAGIC_STRINGS G_N_ELEMENTS(ascend_magic) + +#define ASCEND_DATE "Date:" + static gboolean ascend_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset); static gboolean ascend_seek_read(wtap *wth, gint64 seek_off, @@ -86,9 +89,13 @@ static gint64 ascend_seek(wtap *wth, int *err, gchar **err_info) gint64 date_off = -1, cur_off, packet_off; size_t string_level[ASCEND_MAGIC_STRINGS]; guint string_i = 0, type = 0; + static const gchar ascend_date[] = ASCEND_DATE; + size_t ascend_date_len = sizeof ascend_date - 1; /* strlen of a constant string */ + size_t ascend_date_string_level; guint excessive_read_count = 262144; memset(&string_level, 0, sizeof(string_level)); + ascend_date_string_level = 0; while (((byte = file_getc(wth->fh)) != EOF)) { excessive_read_count--; @@ -104,7 +111,7 @@ static gint64 ascend_seek(wtap *wth, int *err, gchar **err_info) */ for (string_i = 0; string_i < ASCEND_MAGIC_STRINGS; string_i++) { const gchar *strptr = ascend_magic[string_i].strptr; - size_t len = strlen(strptr); + size_t len = ascend_magic[string_i].strlength; if (byte == *(strptr + string_level[string_i])) { /* @@ -127,40 +134,71 @@ static gint64 ascend_seek(wtap *wth, int *err, gchar **err_info) return -1; } - /* Date: header is a special case. Remember the offset, - but keep looking for other headers. */ - if (strcmp(strptr, ASCEND_DATE) == 0) { - /* We matched a Date: header. - Reset the amount of Date: header that we've matched, - so that we start the process of matching a Date: - header all over again. - - XXX - what if we match multiple Date: headers before - matching some other header? */ - date_off = cur_off - len; - string_level[string_i] = 0; + /* We matched some other type of header. */ + if (date_off == -1) { + /* We haven't yet seen a date header, so this packet + doesn't have one. + Back up over the header we just read; that's where a read + of this packet should start. */ + packet_off = cur_off - len; } else { - /* We matched some other type of header. */ - if (date_off == -1) { - /* We haven't yet seen a date header, so this packet - doesn't have one. - Back up over the header we just read; that's where a read - of this packet should start. */ - packet_off = cur_off - len; - } else { - /* This packet has a date/time header; a read of it should - start at the beginning of *that* header. */ - packet_off = date_off; - } - - type = ascend_magic[string_i].type; - goto found; + /* This packet has a date/time header; a read of it should + start at the beginning of *that* header. */ + packet_off = date_off; } + + type = ascend_magic[string_i].type; + goto found; } } else { + /* + * Not a match for this string, so reset the match process. + */ string_level[string_i] = 0; } } + + /* + * See whether this is the date_string_level'th character of + * ASCEND_DATE. + */ + if (byte == *(ascend_date + ascend_date_string_level)) { + /* + * Yes, it is, so we need to check for the next character of + * that string. + */ + ascend_date_string_level++; + + /* + * Have we matched the entire string? + */ + if (ascend_date_string_level >= ascend_date_len) { + /* We matched a Date: header. It's a special case; + remember the offset, but keep looking for other + headers. + + Reset the amount of Date: header that we've matched, + so that we start the process of matching a Date: + header all over again. + + XXX - what if we match multiple Date: headers before + matching some other header? */ + cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh, err_info); + return -1; + } + + date_off = cur_off - ascend_date_len; + ascend_date_string_level = 0; + } + } else { + /* + * Not a match for the Date: string, so reset the match process. + */ + ascend_date_string_level = 0; + } } *err = file_error(wth->fh, err_info); -- cgit v1.2.3