epan: Use realloc when extending the uncompressed buffer

tvb_uncompress initially allocates an output buffer of twice the
input size. It is typical to have a compression ratio of 2:1 or
5:1, but in the extreme case (lots of all identical bytes), 1030:1
is possible.

When extending the output buffer, instead of always malloc'ing
a new buffer and memcpy'ing the old buffer into it, call realloc,
which at least some (most?) of the time will extend the current
buffer in place instead. This should reduce the time to unzip
from always O(N^2) (where N is the compression ratio) to something
average case more like O(N) or O(N log N), depending on how often
it actually copies the data. It only really affects pathological
cases.

Related to #13779.
This commit is contained in:
John Thacker 2022-12-02 23:28:42 -05:00
parent 868313956f
commit bd1f2cc996
1 changed files with 2 additions and 7 deletions

View File

@ -114,13 +114,8 @@ tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen)
g_memdup2(strmbuf, bytes_pass) :
g_strdup(""));
} else {
guint8 *new_data = (guint8 *)g_malloc0(bytes_out + bytes_pass);
memcpy(new_data, uncompr, bytes_out);
memcpy(new_data + bytes_out, strmbuf, bytes_pass);
g_free(uncompr);
uncompr = new_data;
uncompr = (guint8 *)g_realloc(uncompr, bytes_out + bytes_pass);
memcpy(uncompr + bytes_out, strmbuf, bytes_pass);
}
bytes_out += bytes_pass;