blob: f9032237e921b7543e4a652477549a6cc48855b4 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3#include <stdint.h>
4#include <string.h>
5
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01006#include <osmocom/core/msgb.h>
Harald Welte50ef7332017-05-15 12:45:59 +02007#include <osmocom/core/bit16gen.h>
8#include <osmocom/core/bit32gen.h>
Harald Welteec8b4502010-02-20 20:34:29 +01009
Harald Welte57c7d372011-08-17 17:50:55 +020010/*! \defgroup tlv GSM L3 compatible TLV parser
11 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020012 * \file tlv.h */
Harald Welte57c7d372011-08-17 17:50:55 +020013
Harald Welteec8b4502010-02-20 20:34:29 +010014/* Terminology / wording
15 tag length value (in bits)
16
17 V - - 8
18 LV - 8 N * 8
19 TLV 8 8 N * 8
20 TL16V 8 16 N * 8
21 TLV16 8 8 N * 16
22 TvLV 8 8/16 N * 8
Harald Welte2fe68472012-07-14 01:50:33 +020023 vTvLV 8/16 8/16 N * 8
Harald Welte4a29f342017-08-09 19:00:09 +020024 T16LV 16 8 N * 8
Harald Welteec8b4502010-02-20 20:34:29 +010025*/
26
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027/*! gross length of a LV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010028#define LV_GROSS_LEN(x) (x+1)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029/*! gross length of a TLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010030#define TLV_GROSS_LEN(x) (x+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020031/*! gross length of a TLV16 type field */
Harald Welteec8b4502010-02-20 20:34:29 +010032#define TLV16_GROSS_LEN(x) ((2*x)+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! gross length of a TL16V type field */
Harald Welteec8b4502010-02-20 20:34:29 +010034#define TL16V_GROSS_LEN(x) (x+3)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020035/*! gross length of a L16TV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010036#define L16TV_GROSS_LEN(x) (x+3)
Harald Welte4a29f342017-08-09 19:00:09 +020037/*! gross length of a T16LV type field */
38#define T16LV_GROSS_LEN(x) (x+3)
Harald Welteec8b4502010-02-20 20:34:29 +010039
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! maximum length of TLV of one byte length */
Harald Welteec8b4502010-02-20 20:34:29 +010041#define TVLV_MAX_ONEBYTE 0x7f
42
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043/*! gross length of a TVLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010044static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
45{
46 if (len <= TVLV_MAX_ONEBYTE)
47 return TLV_GROSS_LEN(len);
48 else
49 return TL16V_GROSS_LEN(len);
50}
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! gross length of vTvL header (tag+len) */
Harald Welte2fe68472012-07-14 01:50:33 +020053static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
54{
55 uint16_t ret = 2;
56
57 if (tag > TVLV_MAX_ONEBYTE)
58 ret++;
59
60 if (len > TVLV_MAX_ONEBYTE)
61 ret++;
62
63 return ret;
64}
65
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! gross length of vTvLV (tag+len+val) */
Harald Welte2fe68472012-07-14 01:50:33 +020067static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
68{
69 uint16_t ret;
70
71 if (len <= TVLV_MAX_ONEBYTE)
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030072 ret = TLV_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020073 else
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030074 ret = TL16V_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020075
76 if (tag > TVLV_MAX_ONEBYTE)
77 ret += 1;
78
79 return ret;
80}
81
Harald Welteec8b4502010-02-20 20:34:29 +010082/* TLV generation */
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084/*! put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010085static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
86 const uint8_t *val)
87{
88 *buf++ = len;
89 memcpy(buf, val, len);
90 return buf + len;
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! put (append) a TLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010094static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
95 const uint8_t *val)
96{
97 *buf++ = tag;
98 *buf++ = len;
99 memcpy(buf, val, len);
100 return buf + len;
101}
102
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103/*! put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +0100104static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
105 const uint16_t *val)
106{
107 *buf++ = tag;
108 *buf++ = len;
109 memcpy(buf, val, len*2);
110 return buf + len*2;
111}
112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113/*! put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100114static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
115 const uint8_t *val)
116{
117 *buf++ = tag;
118 *buf++ = len >> 8;
119 *buf++ = len & 0xff;
120 memcpy(buf, val, len);
121 return buf + len*2;
122}
123
Harald Welte4a29f342017-08-09 19:00:09 +0200124/*! put (append) a TL16V field */
125static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len,
126 const uint8_t *val)
127{
128 *buf++ = tag >> 8;
129 *buf++ = tag & 0xff;
130 *buf++ = len;
131 memcpy(buf, val, len);
132 return buf + len + 2;
133}
134
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200135/*! put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100136static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
137 const uint8_t *val)
138{
139 uint8_t *ret;
140
141 if (len <= TVLV_MAX_ONEBYTE) {
142 ret = tlv_put(buf, tag, len, val);
143 buf[1] |= 0x80;
144 } else
145 ret = tl16v_put(buf, tag, len, val);
146
147 return ret;
148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! put (append) a variable-length tag or variable-length length * */
Harald Welte2fe68472012-07-14 01:50:33 +0200151static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag)
152{
153 if (tag > TVLV_MAX_ONEBYTE) {
154 /* two-byte TAG */
155 *buf++ = 0x80 | (tag >> 8);
156 *buf++ = (tag & 0xff);
157 } else
158 *buf++ = tag;
159
160 return buf;
161}
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/* put (append) vTvL (GAN) field (tag + length)*/
Harald Welte2fe68472012-07-14 01:50:33 +0200164static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
165{
166 uint8_t *ret;
167
168 ret = vt_gan_put(buf, tag);
169 return vt_gan_put(ret, len);
170}
171
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200172/* put (append) vTvLV (GAN) field (tag + length + val) */
Harald Welte2fe68472012-07-14 01:50:33 +0200173static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len,
174 const uint8_t *val)
175{
176 uint8_t *ret;
177
178 ret = vtvl_gan_put(buf, tag, len );
179
180 memcpy(ret, val, len);
181 ret = buf + len;
182
183 return ret;
184}
185
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186/*! put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100187static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
188{
189 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
190 return tlv16_put(buf, tag, len, val);
191}
192
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193/*! put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100194static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
195 const uint8_t *val)
196{
197 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
198 return tl16v_put(buf, tag, len, val);
199}
200
Harald Welte4a29f342017-08-09 19:00:09 +0200201static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val)
202{
203 uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len));
204 return t16lv_put(buf, tag, len, val);
205}
206
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200207/*! put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100208static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
209 const uint8_t *val)
210{
211 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
212 return tvlv_put(buf, tag, len, val);
213}
214
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200215/*! put (append) a vTvLV field to \ref msgb */
Harald Welte2fe68472012-07-14 01:50:33 +0200216static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag,
217 uint16_t len, const uint8_t *val)
218{
219 uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len));
220 return vtvlv_gan_put(buf, tag, len, val);
221}
222
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223/*! put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100224static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
225 const uint8_t *val)
226{
227 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
228
229 *buf++ = len >> 8;
230 *buf++ = len & 0xff;
231 *buf++ = tag;
232 memcpy(buf, val, len);
233 return buf + len;
234}
235
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200236/*! put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100237static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
238{
239 *buf++ = val;
240 return buf;
241}
242
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200243/*! put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100244static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
245 uint8_t val)
246{
247 *buf++ = tag;
248 *buf++ = val;
249 return buf;
250}
251
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200252/*! put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100253static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
254 unsigned int len, const uint8_t *val)
255{
256 *buf++ = tag;
257 memcpy(buf, val, len);
258 return buf + len;
259}
260
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200261/*! put (append) a TV16 field
Harald Welte57c7d372011-08-17 17:50:55 +0200262 * \param[in,out] buf data buffer
263 * \param[in] tag Tag value
264 * \param[in] val Value (in host byte order!)
265 */
Harald Welteec8b4502010-02-20 20:34:29 +0100266static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
267 uint16_t val)
268{
269 *buf++ = tag;
270 *buf++ = val >> 8;
271 *buf++ = val & 0xff;
272 return buf;
273}
274
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200275/*! put (append) a LV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100276 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100277static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
278{
279 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
280 return lv_put(buf, len, val);
281}
282
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283/*! put (append) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100284 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100285static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
286{
287 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
288 return tlv_put(buf, tag, len, val);
289}
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291/*! put (append) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100292 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100293static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
294{
295 uint8_t *buf = msgb_put(msg, 2);
296 return tv_put(buf, tag, val);
297}
298
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200299/*! put (append) a TVfixed field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100300 * \returns pointer to first byte after newly-put information */
Harald Welte63196de2011-03-05 14:32:50 +0100301static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
302 unsigned int len, const uint8_t *val)
303{
304 uint8_t *buf = msgb_put(msg, 1+len);
305 return tv_fixed_put(buf, tag, len, val);
306}
307
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200308/*! put (append) a V field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100309 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100310static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
311{
312 uint8_t *buf = msgb_put(msg, 1);
313 return v_put(buf, val);
314}
315
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200316/*! put (append) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100317 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100318static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
319{
320 uint8_t *buf = msgb_put(msg, 3);
321 return tv16_put(buf, tag, val);
322}
323
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200324/*! push (prepend) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100325 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100326static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
327{
328 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100329 tlv_put(buf, tag, len, val);
330 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100331}
332
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200333/*! push (prepend) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100334 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100335static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
336{
337 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100338 tv_put(buf, tag, val);
339 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100340}
341
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200342/*! push (prepend) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100343 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100344static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
345{
346 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100347 tv16_put(buf, tag, val);
348 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100349}
350
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200351/*! push (prepend) a TvLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100352 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100353static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
354 const uint8_t *val)
355{
356 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100357 tvlv_put(buf, tag, len, val);
358 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100359}
360
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200361/* push (prepend) a vTvL header to a \ref msgb
Harald Welte2fe68472012-07-14 01:50:33 +0200362 */
363static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
364 uint16_t len)
365{
366 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
367 vtvl_gan_put(buf, tag, len);
368 return buf;
369}
370
371
372static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
373 uint16_t len, const uint8_t *val)
374{
375 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
376 vtvlv_gan_put(buf, tag, len, val);
377 return buf;
378}
379
Harald Welteec8b4502010-02-20 20:34:29 +0100380/* TLV parsing */
381
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200382/*! Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100383struct tlv_p_entry {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200384 uint16_t len; /*!< length */
385 const uint8_t *val; /*!< pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100386};
387
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200388/*! TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100389enum tlv_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200390 TLV_TYPE_NONE, /*!< no type */
391 TLV_TYPE_FIXED, /*!< fixed-length value-only */
392 TLV_TYPE_T, /*!< tag-only */
393 TLV_TYPE_TV, /*!< tag-value (8bit) */
394 TLV_TYPE_TLV, /*!< tag-length-value */
395 TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */
396 TLV_TYPE_TvLV, /*!< tag, variable length, value */
397 TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */
398 TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100399};
400
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200401/*! Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100402struct tlv_def {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200403 enum tlv_type type; /*!< TLV type */
404 uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100405};
406
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200407/*! Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100408struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200409 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100410};
411
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200412/*! result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100413struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200414 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100415};
416
417extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200418extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100419
420int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
421 const struct tlv_definition *def,
422 const uint8_t *buf, int buf_len);
423int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
424 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100425/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100426void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
427
428#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
429#define TLVP_LEN(x, y) (x)->lv[y].len
430#define TLVP_VAL(x, y) (x)->lv[y].val
431
Harald Weltecc27fa62014-08-18 15:31:04 +0200432#define TLVP_PRES_LEN(tp, tag, min_len) \
433 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
434
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200435/*! Align given TLV element with 16 bit value to an even address
Andreas Eversberg01675962012-11-06 12:02:59 +0100436 * \param[in] tp pointer to \ref tlv_parsed
437 * \param[in] pos element to return
438 * \returns aligned 16 bit value
439 */
440static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
441{
442 uint16_t res;
443 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
444 return res;
445}
446
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200447/*! Align given TLV element with 32 bit value to an address that is a multiple of 4
Andreas Eversberg01675962012-11-06 12:02:59 +0100448 * \param[in] tp pointer to \ref tlv_parsed
449 * \param[in] pos element to return
450 * \returns aligned 32 bit value
451 */
452static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
453{
454 uint32_t res;
455 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
456 return res;
457}
458
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200459/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200460 * \param[in] tp pointer to \ref tlv_parsed
461 * \param[in] pos element to return
462 * \returns aligned 16 bit value in host byte order
463 */
464static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
465{
466 return osmo_load16be(TLVP_VAL(tp, pos));
467}
468
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200469/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200470 * \param[in] tp pointer to \ref tlv_parsed
471 * \param[in] pos element to return
472 * \returns aligned 32 bit value in host byte order
473 */
474static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
475{
476 return osmo_load32be(TLVP_VAL(tp, pos));
477}
478
479
Maxdbd3a922017-01-02 14:10:30 +0100480struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
481int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
Harald Weltefbd02fa2016-04-25 15:19:35 +0200482int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
483 size_t len, uint8_t **value);
484int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
485 uint8_t tag, size_t len, uint8_t **value);
486int osmo_shift_tlv(uint8_t **data, size_t *data_len,
487 uint8_t *tag, uint8_t **value, size_t *value_len);
488int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
489 uint8_t tag, uint8_t **value, size_t *value_len);
490int osmo_shift_lv(uint8_t **data, size_t *data_len,
491 uint8_t **value, size_t *value_len);
492
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200493/*! @} */