The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tlvhelper.c
Go to the documentation of this file.
1 
25 #include <string.h>
26 #include <glib.h>
27 #include <tlvhelper.h>
30 guint8
31 tlv_get_guint8(const void * vitem,
32  const void* bufend)
33 {
34  const guint8* itemptr = (const guint8*)vitem;
35  guint8 item;
36  g_return_val_if_fail(vitem != NULL && vitem < bufend, TLV_BAD8);
37  item = *itemptr;
38  return item;
39 }
41 void
42 tlv_set_guint8(void * vitem,
43  guint8 item,
44  const void* bufend)
45 {
46  guint8* itemptr = (guint8*)vitem;
47  g_return_if_fail(vitem < bufend);
48  *itemptr = item;
49 }
50 
54 guint16
55 tlv_get_guint16(const void * vitem,
56  const void* bufend)
57 {
58  const guint16* itemptr = (const guint16*)vitem;
59  guint16 item;
60  g_return_val_if_fail(vitem != NULL && ((itemptr+1) <= (const guint16*)bufend), TLV_BAD16);
61  memcpy(&item, vitem, sizeof(item));
62  return g_ntohs(item);
63 }
66 void
67 tlv_set_guint16(void * vitem,
68  guint16 item,
69  const void* bufend)
70 {
71  guint16* itemptr = (guint16*)vitem;
72  g_return_if_fail(vitem != NULL && ((void*)(itemptr+1)) <= bufend);
73  item = g_htons(item);
74  memcpy(vitem, &item, sizeof(item));
75 }
76 
80 guint32
81 tlv_get_guint32(const void * vitem,
82  const void* bufend)
83 {
84  const guint32* itemptr = (const guint32*)vitem;
85  guint32 item;
86  g_return_val_if_fail(vitem != NULL && ((const void*)(itemptr+1) <= bufend), TLV_BAD32);
87  memcpy(&item, vitem, sizeof(item));
88  return g_ntohl(item);
89 }
92 void
93 tlv_set_guint32(void * vitem,
94  guint32 item,
95  const void* bufend)
96 {
97  guint32* itemptr = (guint32*)vitem;
98  g_return_if_fail(vitem != NULL && ((void*)(itemptr+1) <= bufend));
99  item = g_htonl(item);
100  memcpy(vitem, &item, sizeof(item));
101 }
102 
103 
107 guint64
108 tlv_get_guint64(const void * vitem,
109  const void* bufend)
110 {
111  const guint64* itemptr = (const guint64*)vitem;
112  guint64 item;
113  g_return_val_if_fail(vitem != NULL && ((const void*)(itemptr+1) <= bufend), TLV_BAD24);
114  memcpy(&item, vitem, sizeof(item));
115  return GINT64_FROM_BE(item);
116 }
119 void
120 tlv_set_guint64(void * vitem,
121  guint64 item,
122  const void* bufend)
123 {
124  guint64* itemptr = (guint64*)vitem;
125  g_return_if_fail(vitem != NULL && ((void*)(itemptr+1) <= bufend));
126  item = GINT64_TO_BE(item);
127  memcpy(vitem, &item, sizeof(item));
128 }
132 guint32
133 tlv_get_guint24(const void * vitem,
134  const void* bufend)
135 {
136  guint32 item;
137  guint8 firstbyte;
138  g_return_val_if_fail(vitem != NULL && ((const void*)((const guint8*)vitem+3)) <= bufend, TLV_BAD24);
139  firstbyte = *((const guint8*)vitem);
141  item = tlv_get_guint16((((const guint8 *)vitem+1)), bufend);
142  item += ((((guint32)firstbyte)<<16)&0xff0000);
143  return item;
144 }
147 void
148 tlv_set_guint24(void * vitem,
149  guint32 item,
150  const void* bufend)
151 {
152  guint8 firstbyte = ((item>>16)&0xff);
153  guint16 item16;
154  g_return_if_fail(vitem != NULL && (void*)(((guint8*)vitem)+3) <= bufend);
155  item16 = (guint16) (item & 0xffff);
157  *((guint8 *)vitem) = firstbyte;
158  tlv_set_guint16(((guint8 *)vitem+1), item16, bufend);
159 }
guint8 tlv_get_guint8(const void *vitem, const void *bufend)
Retrieve an unsigned 8 bit integer from the given location with error checking.
Definition: tlvhelper.c:31
guint64 tlv_get_guint64(const void *vitem, const void *bufend)
Retrieve an unsigned 64 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:108
#define TLV_BAD16
Definition: tlvhelper.h:30
#define TLV_BAD8
Definition: tlvhelper.h:29
void tlv_set_guint24(void *vitem, guint32 item, const void *bufend)
Set an unsigned 24 bit (3-byte) integer to the given location with error checking and without caring ...
Definition: tlvhelper.c:148
void tlv_set_guint16(void *vitem, guint16 item, const void *bufend)
Set an unsigned 16 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:67
guint32 tlv_get_guint32(const void *vitem, const void *bufend)
Retrieve an unsigned 32 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:81
guint32 tlv_get_guint24(const void *vitem, const void *bufend)
Retrieve an unsigned 24 bit (3-byte) integer from the given location with error checking and without ...
Definition: tlvhelper.c:133
TLV helper interfaces definitions.
void tlv_set_guint8(void *vitem, guint8 item, const void *bufend)
Set an unsigned 8 bit integer to the given location with error checking.
Definition: tlvhelper.c:42
void tlv_set_guint32(void *vitem, guint32 item, const void *bufend)
Set an unsigned 32 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:93
#define TLV_BAD32
Definition: tlvhelper.h:32
guint16 tlv_get_guint16(const void *vitem, const void *bufend)
Retrieve an unsigned 16 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:55
void tlv_set_guint64(void *vitem, guint64 item, const void *bufend)
Set an unsigned 64 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:120
#define TLV_BAD24
Definition: tlvhelper.h:31