The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
intframe.c
Go to the documentation of this file.
1 
23 #include <string.h>
24 #include <projectcommon.h>
25 #include <frameset.h>
26 #include <intframe.h>
27 #include <frametypes.h>
28 #include <generic_tlv_min.h>
29 #include <tlvhelper.h>
76 FSTATIC void _intframe_setint(IntFrame * self, guint64 value);
77 FSTATIC guint64 _intframe_getint(IntFrame * self);
78 FSTATIC void _intframe_updatedata(Frame*, gpointer, gconstpointer, FrameSet*);
79 FSTATIC gboolean _intframe_isvalid(const Frame*, gconstpointer, gconstpointer);
80 FSTATIC gchar* _intframe_toString(gconstpointer obj);
81 
86 
88 FSTATIC int
90 {
91  return self->baseclass.length;
92 }
94 FSTATIC void
95 _intframe_setint(IntFrame * self, guint64 value)
96 {
97  guint64 limit = 0;
98  switch (self->baseclass.length) {
99  case 1: limit = G_MAXUINT8;
100  break;
101  case 2: limit = G_MAXUINT16;
102  break;
103  case 3: limit = 0x00ffffffL;
104  break;
105  case 4: limit = G_MAXUINT32;
106  break;
107  case 8: limit = G_MAXUINT64;
108  break;
109  }
110  g_return_if_fail(value <= limit);
111  self->_value = value;
112 }
113 
115 FSTATIC guint64
117 {
118  return self->_value;
119 }
120 
121 FSTATIC void
123  gpointer tlvptr,
124  gconstpointer pktend,
125  FrameSet* fs)
126 {
127  IntFrame* self = CASTTOCLASS(IntFrame, fself);
128  // NOTE - this gets rid of the "const" coming out of get_generic_tlv_value...
130  void * pktpos = GINT_TO_POINTER(GPOINTER_TO_INT(get_generic_tlv_value(tlvptr, pktend)));
131  (void)fs;
132  g_return_if_fail(NULL != pktpos);
133 
134  switch (self->baseclass.length) {
135  case 1: tlv_set_guint8(pktpos, (guint8)self->_value, pktend);
136  break;
137  case 2: tlv_set_guint16(pktpos, (guint16)self->_value, pktend);
138  break;
139  case 3: tlv_set_guint24(pktpos, (guint32)self->_value, pktend);
140  break;
141  case 4: tlv_set_guint32(pktpos, (guint32)self->_value, pktend);
142  break;
143  case 8: tlv_set_guint64(pktpos, (guint64)self->_value, pktend);
144  break;
145  default:g_return_if_reached();
146  break;
147  }
148 }
149 
151 FSTATIC gboolean
153  gconstpointer tlvptr,
154  gconstpointer pktend)
155 {
156  int length = self->length;
157  if (tlvptr != NULL) {
158  int tlvlen = get_generic_tlv_len(tlvptr, pktend);
159  if (length != tlvlen) {
160  return FALSE;
161  }
162  }
163  switch (self->length) {
164  case 1: case 2: case 3: case 4: case 8:
165  return TRUE;
166  }
167  return FALSE;
168 }
169 
171 FSTATIC gchar*
172 _intframe_toString(gconstpointer obj)
173 {
174  const IntFrame* self = CASTTOCONSTCLASS(IntFrame, obj);
175  return g_strdup_printf("IntFrame(%d, %d, "FMT_64BIT"d)"
176  , self->baseclass.type, self->baseclass.length, self->_value);
177 }
178 
180 IntFrame*
181 intframe_new(guint16 frametype,
182  int intbytes)
183 {
184  Frame* frame;
185  IntFrame* iframe;
186  if (intbytes != 1 && intbytes != 2 && intbytes != 3 && intbytes != 4 && intbytes != 8) {
187  return NULL;
188  }
189  frame = frame_new(frametype, sizeof(IntFrame));
190  proj_class_register_subclassed(frame, "IntFrame");
191  iframe = CASTTOCLASS(IntFrame, frame);
192  iframe->baseclass.length = intbytes;
193  iframe->intlength = _intframe_intlength;
194  iframe->setint = _intframe_setint;
195  iframe->getint = _intframe_getint;
198  iframe->baseclass.value = NULL;
199  iframe->baseclass.setvalue = NULL;
200  iframe->baseclass.valuefinalize = NULL;
201  iframe->baseclass.length = intbytes;
203  return iframe;
204 }
207 Frame*
208 intframe_tlvconstructor(gconstpointer tlvstart,
209  gconstpointer pktend)
210 {
211  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
212  guint16 framelength = get_generic_tlv_len(tlvstart, pktend);
213  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
214  IntFrame * ret = intframe_new(frametype, framelength);
215  guint64 intvalue = G_MAXUINT64;
216  g_return_val_if_fail(ret != NULL, NULL);
217 
218  ret->baseclass.length = framelength;
219  switch (ret->baseclass.length) {
220  case 1:
221  intvalue = tlv_get_guint8(framevalue, pktend);
222  break;
223  case 2:
224  intvalue = tlv_get_guint16(framevalue, pktend);
225  break;
226  case 3:
227  intvalue = tlv_get_guint24(framevalue, pktend);
228  break;
229  case 4:
230  intvalue = tlv_get_guint32(framevalue, pktend);
231  break;
232  case 8:
233  intvalue = tlv_get_guint64(framevalue, pktend);
234  break;
235  }
236  ret->setint(ret, intvalue);
237  return CASTTOCLASS(Frame, ret);
238 }
239 
240