The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
base.c
Go to the documentation of this file.
1 #include <freeradius-devel/util/cbor.h>
2 #include <freeradius-devel/io/test_point.h>
3 
4 static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
5  uint8_t const *data, size_t data_len, UNUSED void *decode_ctx)
6 {
7  fr_dbuff_t dbuff;
8  uint8_t field = 0;
9  ssize_t slen;
10 
11  fr_dbuff_init(&dbuff, data, data_len);
12 
13  FR_DBUFF_OUT_RETURN(&field, &dbuff);
14  if (field != 0x9f) {
15  fr_strerror_printf("Invalid cbor header - expected indefinite array 9f, got %02x",
16  field);
17  return -1;
18  }
19 
20  do {
21  if (fr_dbuff_extend_lowat(NULL, &dbuff, 1) == 0) {
22  fr_strerror_printf("Invalid cbor header - unexpected end of data");
23  return -fr_dbuff_used(&dbuff);
24  }
25 
26  field = *fr_dbuff_current(&dbuff);
27  if (field == 0xff) {
28  fr_dbuff_advance(&dbuff, 1);
29  break;
30  }
31 
32  slen = fr_cbor_decode_pair(ctx, out, &dbuff, parent, false);
33  if (slen <= 0) return slen - fr_dbuff_used(&dbuff);
34  } while (true);
35 
36  return fr_dbuff_used(&dbuff);
37 }
38 
40 {
41  fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
42  fr_pair_t *vp;
43 
44  FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) 0x9f); /* indefinite array */
45 
46  for (vp = fr_dcursor_current(cursor);
47  vp != NULL;
48  vp = fr_dcursor_next(cursor)) {
49  ssize_t slen;
50 
51  slen = fr_cbor_encode_pair(&work_dbuff, vp);
52  if (slen <= 0) return slen - fr_dbuff_used(&work_dbuff);
53  }
54 
55  FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) 0xff); /* end of indefinite array */
56 
57  return fr_dbuff_set(dbuff, &work_dbuff);
58 }
59 
60 /*
61  * Test points
62  */
65  .func = encode_pair,
66 };
67 
70  .func = decode_pair
71 };
#define UNUSED
Definition: build.h:313
ssize_t fr_cbor_encode_pair(fr_dbuff_t *dbuff, fr_pair_t *vp)
Encode a pair.
Definition: cbor.c:1436
ssize_t fr_cbor_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dbuff_t *dbuff, fr_dict_attr_t const *parent, bool tainted)
Definition: cbor.c:1629
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition: dbuff.h:1072
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition: dbuff.h:767
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition: dbuff.h:911
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition: dbuff.h:354
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition: dbuff.h:660
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition: dbuff.h:222
#define FR_DBUFF_OUT_RETURN(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data.
Definition: dbuff.h:1818
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition: dbuff.h:1472
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition: dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition: dcursor.h:337
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static fr_internal_encode_ctx_t encode_ctx
fr_test_point_pair_encode_t cbor_tp_encode_pair
Definition: base.c:64
fr_test_point_pair_decode_t cbor_tp_decode_pair
Definition: base.c:69
static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, UNUSED void *decode_ctx)
Definition: base.c:4
static ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition: base.c:39
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
fr_pair_decode_t func
Decoder for pairs.
Definition: test_point.h:105
fr_pair_encode_t func
Encoder for pairs.
Definition: test_point.h:113
Entry point for pair decoders.
Definition: test_point.h:103
Entry point for pair encoders.
Definition: test_point.h:111
static fr_slen_t parent
Definition: pair.h:851
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition: strerror.h:64
return fr_dbuff_set(dbuff, &our_dbuff)
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997