lupdate / lrelease ui/qt/Qtshark.pro
[metze/wireshark/wip.git] / frame_tvbuff.c
index 20635fd9589e89cf6e442708cec8aba793dce2d5..7682f10d2a608161a1940dae92120f2c27cc9639 100644 (file)
@@ -56,7 +56,10 @@ frame_read(struct tvb_frame *frame_tvb, struct wtap_pkthdr *phdr, Buffer *buf)
        if (cfile.wth != frame_tvb->wth)
                return FALSE;
 
-       if (!wtap_seek_read(frame_tvb->wth, frame_tvb->file_off, phdr, buf, frame_tvb->tvb.length, &err, &err_info)) {
+       /* XXX, what if phdr->caplen isn't equal to
+        * frame_tvb->tvb.length + frame_tvb->offset?
+        */
+       if (!wtap_seek_read(frame_tvb->wth, frame_tvb->file_off, phdr, buf, &err, &err_info)) {
                switch (err) {
                        case WTAP_ERR_UNSUPPORTED_ENCAP:
                        case WTAP_ERR_BAD_FILE:
@@ -68,9 +71,8 @@ frame_read(struct tvb_frame *frame_tvb, struct wtap_pkthdr *phdr, Buffer *buf)
        return TRUE;
 }
 
-
 static void
-frame_invalidate(struct tvb_frame *frame_tvb)
+frame_cache(struct tvb_frame *frame_tvb)
 {
        struct wtap_pkthdr phdr; /* Packet header */
 
@@ -78,13 +80,13 @@ frame_invalidate(struct tvb_frame *frame_tvb)
                frame_tvb->buf = (struct Buffer *) g_malloc(sizeof(struct Buffer));
 
                /* XXX, register frame_tvb to some list which frees from time to time not used buffers :] */
-               buffer_init(frame_tvb->buf, frame_tvb->tvb.length);
+               buffer_init(frame_tvb->buf, frame_tvb->tvb.length + frame_tvb->offset);
 
                if (!frame_read(frame_tvb, &phdr, frame_tvb->buf))
                        { /* TODO: THROW(???); */ }
        }
 
-       frame_tvb->tvb.real_data = buffer_start_ptr(frame_tvb->buf);
+       frame_tvb->tvb.real_data = buffer_start_ptr(frame_tvb->buf) + frame_tvb->offset;
 }
 
 static void
@@ -104,7 +106,7 @@ frame_get_ptr(tvbuff_t *tvb, guint abs_offset, guint abs_length _U_)
 {
        struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
 
-       frame_invalidate(frame_tvb);
+       frame_cache(frame_tvb);
 
        return tvb->real_data + abs_offset;
 }
@@ -114,7 +116,7 @@ frame_memcpy(tvbuff_t *tvb, void *target, guint abs_offset, guint abs_length)
 {
        struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
 
-       frame_invalidate(frame_tvb);
+       frame_cache(frame_tvb);
 
        return memcpy(target, tvb->real_data + abs_offset, abs_length);
 }
@@ -125,7 +127,7 @@ frame_find_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
        struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
        const guint8 *result;
 
-       frame_invalidate(frame_tvb);
+       frame_cache(frame_tvb);
 
        result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
        if (result)
@@ -139,17 +141,11 @@ frame_pbrk_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *ne
 {
        struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
 
-       frame_invalidate(frame_tvb);
+       frame_cache(frame_tvb);
 
        return tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
 }
 
-static gsize
-frame_sizeof(void)
-{ 
-       return sizeof(struct tvb_frame); 
-}
-
 static guint
 frame_offset(const tvbuff_t *tvb _U_, const guint counter)
 {
@@ -160,7 +156,8 @@ frame_offset(const tvbuff_t *tvb _U_, const guint counter)
 static tvbuff_t *frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length);
 
 static const struct tvb_ops tvb_frame_ops = {
-       frame_sizeof,         /* size */
+       sizeof(struct tvb_frame), /* size */
+
        frame_free,           /* free */
        frame_offset,         /* offset */
        frame_get_ptr,        /* get_ptr */
@@ -197,7 +194,7 @@ frame_tvbuff_new(const frame_data *fd, const guint8 *buf)
         *
         * For now, we clip the reported length at G_MAXINT
         *
-        * (XXX, is this still a problem?) There was an exception when we call 
+        * (XXX, is this still a problem?) There was an exception when we call
         * tvb_new_real_data() now there's no one
         */
 
@@ -214,11 +211,12 @@ frame_tvbuff_new(const frame_data *fd, const guint8 *buf)
 
        frame_tvb = (struct tvb_frame *) tvb;
 
-       /* XXX, how to handle fd->file_off == -1 (edited packet) ?? */
-       /* don't care, reassemble code was doing whole copy of data, so it'll work the same */
-
        /* XXX, wtap_can_seek() */
-       if (fd && cfile.wth && cfile.wth->random_fh) {
+       if (cfile.wth && cfile.wth->random_fh 
+#ifdef WANT_PACKET_EDITOR
+               && fd->file_off != -1 /* generic clone for modified packets */
+#endif
+       ) {
                frame_tvb->wth = cfile.wth;
                frame_tvb->file_off = fd->file_off;
                frame_tvb->offset = 0;
@@ -272,3 +270,71 @@ frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length)
 
        return cloned_tvb;
 }
+
+
+/* based on tvb_new_real_data() */
+tvbuff_t *
+file_tvbuff_new(const frame_data *fd, const guint8 *buf)
+{
+       struct tvb_frame *frame_tvb;
+       tvbuff_t *tvb;
+
+       tvb = tvb_new(&tvb_frame_ops);
+
+       /*
+        * XXX - currently, the length arguments in
+        * tvbuff structure are signed, but the captured
+        * and reported length values are unsigned; this means
+        * that length values > 2^31 - 1 will appear as
+        * negative lengths
+        *
+        * Captured length values that large will already
+        * have been filtered out by the Wiretap modules
+        * (the file will be reported as corrupted), to
+        * avoid trying to allocate large chunks of data.
+        *
+        * Reported length values will not have been
+        * filtered out, and should not be filtered out,
+        * as those lengths are not necessarily invalid.
+        *
+        * For now, we clip the reported length at G_MAXINT
+        *
+        * (XXX, is this still a problem?) There was an exception when we call
+        * tvb_new_real_data() now there's no one
+        */
+
+       tvb->real_data       = buf;
+       tvb->length          = fd->cap_len;
+       tvb->reported_length = fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len;
+       tvb->initialized     = TRUE;
+
+       /*
+        * This is the top-level real tvbuff for this data source,
+        * so its data source tvbuff is itself.
+        */
+       tvb->ds_tvb = tvb;
+
+       frame_tvb = (struct tvb_frame *) tvb;
+
+       /* XXX, wtap_can_seek() */
+       if (cfile.wth && cfile.wth->random_fh 
+#ifdef WANT_PACKET_EDITOR
+               && fd->file_off != -1 /* generic clone for modified packets */
+#endif
+       ) {
+               frame_tvb->wth = cfile.wth;
+               frame_tvb->file_off = fd->file_off;
+               frame_tvb->offset = 0;
+       } else
+               frame_tvb->wth = NULL;
+
+       frame_tvb->buf = NULL;
+
+       return tvb;
+}
+
+tvbuff_t *
+file_tvbuff_new_buffer(const frame_data *fd, Buffer *buf)
+{
+       return frame_tvbuff_new(fd, buffer_start_ptr(buf));
+}