Include files from the "epan" directory and subdirectories thereof with
[obnox/wireshark/wip.git] / epan / ftypes / ftype-tvbuff.c
1 /*
2  * $Id: ftype-tvbuff.c,v 1.6 2002/01/21 07:37:39 guy Exp $
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@ethereal.com>
6  * Copyright 2001 Gerald Combs
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <ftypes-int.h>
28 #include <epan/gdebug.h>
29
30 static void
31 value_new(fvalue_t *fv)
32 {
33         fv->value.tvb = NULL;
34 }
35
36
37 static void
38 value_set(fvalue_t *fv, gpointer value, gboolean already_copied)
39 {
40         g_assert(already_copied);
41         fv->value.tvb = value;
42 }
43
44 static gpointer
45 value_get(fvalue_t *fv)
46 {
47         return fv->value.tvb;
48 }
49
50 static guint
51 len(fvalue_t *fv)
52 {
53         if (fv->value.tvb)
54                 return tvb_length(fv->value.tvb);
55         else
56                 return 0;
57 }
58
59 static void
60 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
61 {
62         const guint8* data;
63
64         if (fv->value.tvb) {
65                 TRY {
66                         data = tvb_get_ptr(fv->value.tvb, offset, length);
67                         g_byte_array_append(bytes, data, length);
68                 }
69                 CATCH_ALL {
70                         /* nothing */
71                 }
72                 ENDTRY;
73
74         }
75 }
76
77 void
78 ftype_register_tvbuff(void)
79 {
80
81         static ftype_t protocol_type = {
82                 "FT_PROTOCOL",
83                 "protocol",
84                 0,
85                 value_new,
86                 NULL,
87                 NULL,
88
89                 value_set,
90                 NULL,
91                 NULL,
92
93                 value_get,
94                 NULL,
95                 NULL,
96
97                 NULL,
98                 NULL,
99                 NULL,
100                 NULL,
101                 NULL,
102                 NULL,
103
104                 len,
105                 slice,
106
107         };
108
109
110         ftype_register(FT_PROTOCOL, &protocol_type);
111 }