summaryrefslogtreecommitdiffstats
path: root/tinyBFCP/src/tbfcp_attr.c
blob: cc8e2a3545bde965d4c0470f488928ecb846fb3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/* Copyright (C) 2014 Mamadou DIOP.
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
#include "tinybfcp/tbfcp_attr.h"

#include "tnet_endianness.h"

#include "tsk_memory.h"
#include "tsk_debug.h"

#define kWithoutPadding		tsk_false
#define kWithPadding		tsk_true

#define ALIGN_ON_32BITS(size_in_octes) if (((size_in_octes) & 3)) (size_in_octes) += (4 - ((size_in_octes) & 3));
#define ALIGN_ON_32BITS_AND_SET_PADDING_ZEROS(p_buffer, size_in_octes) \
	if (((size_in_octes) & 3)) { \
		int c = (4 - ((size_in_octes) & 3)); \
		memset(p_buffer, 0, c); \
		(size_in_octes) += c; \
	}


static enum tbfcp_attribute_format_e _tbfcp_attr_get_format(enum tbfcp_attribute_type_e type)
{
    switch (type) {
    case tbfcp_attribute_type_BENEFICIARY_ID:
        case tbfcp_attribute_type_FLOOR_ID:
            case tbfcp_attribute_type_FLOOR_REQUEST_ID:
                    return tbfcp_attribute_format_Unsigned16;
    case tbfcp_attribute_type_PRIORITY:
    case tbfcp_attribute_type_REQUEST_STATUS:
        return tbfcp_attribute_format_OctetString16;
    case tbfcp_attribute_type_ERROR_CODE:
    case tbfcp_attribute_type_ERROR_INFO:
    case tbfcp_attribute_type_PARTICIPANT_PROVIDED_INFO:
    case tbfcp_attribute_type_STATUS_INFO:
    case tbfcp_attribute_type_SUPPORTED_ATTRIBUTES:
    case tbfcp_attribute_type_SUPPORTED_PRIMITIVES:
    case tbfcp_attribute_type_USER_DISPLAY_NAME:
    case tbfcp_attribute_type_USER_URI:
        return tbfcp_attribute_format_OctetString;
    case tbfcp_attribute_type_BENEFICIARY_INFORMATION:
    case tbfcp_attribute_type_FLOOR_REQUEST_INFORMATION:
    case tbfcp_attribute_type_REQUESTED_BY_INFORMATION:
    case tbfcp_attribute_type_FLOOR_REQUEST_STATUS:
    case tbfcp_attribute_type_OVERALL_REQUEST_STATUS:
        return tbfcp_attribute_format_Grouped;
    default:
        return tbfcp_attribute_format_Unknown;

    }
}

static int _tbfcp_attr_get_size_in_octetunits(const tbfcp_attr_t* pc_self, tsk_bool_t with_padding, tsk_size_t* p_size)
{
    if (!pc_self || !p_size) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    switch (pc_self->format) {
    case tbfcp_attribute_format_Unsigned16:
    case tbfcp_attribute_format_OctetString16: {
        *p_size = (TBFCP_ATTR_HDR_SIZE_IN_OCTETS + 2);
        if (with_padding) {
            ALIGN_ON_32BITS(*p_size);
        }
        return 0;
    }
    case tbfcp_attribute_format_OctetString: {
        *p_size = (TBFCP_ATTR_HDR_SIZE_IN_OCTETS + ((const tbfcp_attr_octetstring_t*)pc_self)->OctetStringLength);
        if (with_padding) {
            ALIGN_ON_32BITS(*p_size);
        }
        return 0;
    }
    case tbfcp_attribute_format_Grouped: {
        int ret;
        tsk_size_t n_size;
        const tbfcp_attr_grouped_t* _pc_self = (const tbfcp_attr_grouped_t*)pc_self;
        const tsk_list_item_t* pc_item;
        const tbfcp_attr_t* pc_attr;
        *p_size = TBFCP_ATTR_HDR_SIZE_IN_OCTETS + _pc_self->extra_hdr_size_in_octets;
        tsk_list_foreach(pc_item, _pc_self->p_list_attrs) {
            if ((pc_attr = (const tbfcp_attr_t*)pc_item->data)) {
                if ((ret = tbfcp_attr_get_size_in_octetunits_without_padding(pc_attr, &n_size))) {
                    return ret;
                }
                *p_size += n_size;
            }
        }
        if (with_padding) {
            ALIGN_ON_32BITS(*p_size);
        }
        return 0;
    }
    default: {
        TSK_DEBUG_WARN("Attribute format=%d is unknown. Don't be surprised if something goes wrong.", pc_self->format);
        *p_size = (TBFCP_ATTR_HDR_SIZE_IN_OCTETS + pc_self->hdr.length);
        if (with_padding) {
            ALIGN_ON_32BITS(*p_size);
        }
        return 0;
    }
    }
}

int tbfcp_attr_get_size_in_octetunits_without_padding(const tbfcp_attr_t* pc_self, tsk_size_t* p_size)
{
    return _tbfcp_attr_get_size_in_octetunits(pc_self, kWithoutPadding, p_size);
}

int tbfcp_attr_get_size_in_octetunits_with_padding(const tbfcp_attr_t* pc_self, tsk_size_t* p_size)
{
    return _tbfcp_attr_get_size_in_octetunits(pc_self, kWithPadding, p_size);
}

static int _tbfcp_attr_write(const tbfcp_attr_t* pc_self, uint8_t* p_buff_ptr, tsk_size_t n_buff_size, tsk_bool_t with_padding, tsk_size_t *p_written)
{
    tsk_size_t n_min_req_size;
    int ret;
    if (!pc_self || !p_buff_ptr || !n_buff_size || !p_written) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if ((ret = _tbfcp_attr_get_size_in_octetunits(pc_self, with_padding, &n_min_req_size))) {
        return ret;
    }
    if (n_min_req_size > n_buff_size) {
        TSK_DEBUG_ERROR("Buffer too short %u<%u", n_buff_size, n_min_req_size);
        return -2;
    }

    p_buff_ptr[0] = ((uint8_t)pc_self->hdr.type) << 1;
    p_buff_ptr[0] |= (pc_self->hdr.M & 0x01);

    switch (pc_self->format) {
    case tbfcp_attribute_format_Unsigned16:
    case tbfcp_attribute_format_OctetString16: {
        *p_written = TBFCP_ATTR_HDR_SIZE_IN_OCTETS + 2;
        p_buff_ptr[1] = (uint8_t)*p_written;
        if (pc_self->format == tbfcp_attribute_format_Unsigned16) {
            *((uint16_t*)&p_buff_ptr[2]) = tnet_htons(((const tbfcp_attr_unsigned16_t*)pc_self)->Unsigned16);
        }
        else {
            p_buff_ptr[2] = ((const tbfcp_attr_octetstring16_t*)pc_self)->OctetString16[0];
            p_buff_ptr[3] = ((const tbfcp_attr_octetstring16_t*)pc_self)->OctetString16[1];
        }
        if (with_padding) {
            ALIGN_ON_32BITS_AND_SET_PADDING_ZEROS(&p_buff_ptr[*p_written], *p_written);
        }
        return 0;
    }
    case tbfcp_attribute_format_OctetString: {
        const tbfcp_attr_octetstring_t* _pc_self = (const tbfcp_attr_octetstring_t*)pc_self;
        *p_written = TBFCP_ATTR_HDR_SIZE_IN_OCTETS + ((_pc_self->OctetStringLength && _pc_self->OctetString) ? _pc_self->OctetStringLength : 0);
        p_buff_ptr[1] = (uint8_t)*p_written;
        if (_pc_self->OctetStringLength && _pc_self->OctetString) {
            memcpy(&p_buff_ptr[2], _pc_self->OctetString, _pc_self->OctetStringLength);
        }
        if (with_padding) {
            ALIGN_ON_32BITS_AND_SET_PADDING_ZEROS(&p_buff_ptr[*p_written], *p_written);
        }
        return 0;
    }
    case tbfcp_attribute_format_Grouped: {
        int ret;
        tsk_size_t n_written;
        const tbfcp_attr_grouped_t* _pc_self = (const tbfcp_attr_grouped_t*)pc_self;
        const tsk_list_item_t* pc_item;
        const tbfcp_attr_t* pc_attr;
        if (_pc_self->extra_hdr_size_in_octets && _pc_self->extra_hdr_size_in_octets != 2) {
            TSK_DEBUG_ERROR("extra_hdr_size_in_octets=%u not valid", _pc_self->extra_hdr_size_in_octets); // for now only 2byte extra values is supported
            return -2;
        }
        if ((ret = tbfcp_attr_get_size_in_octetunits_without_padding(pc_self, p_written))) {
            return ret;
        }
        p_buff_ptr[1] = (uint8_t)*p_written;
        p_buff_ptr += 2;
        n_buff_size -= 2;
        if (_pc_self->extra_hdr_size_in_octets) {
            *((uint16_t*)p_buff_ptr) = tnet_htons_2(&_pc_self->extra_hdr);
            p_buff_ptr += _pc_self->extra_hdr_size_in_octets;
            n_buff_size -= _pc_self->extra_hdr_size_in_octets;
        }
        tsk_list_foreach(pc_item, _pc_self->p_list_attrs) {
            if ((pc_attr = (const tbfcp_attr_t*)pc_item->data)) {
                if ((ret = _tbfcp_attr_write(pc_attr, p_buff_ptr, n_buff_size, kWithoutPadding, &n_written))) {
                    return ret;
                }
                p_buff_ptr += n_written;
                n_buff_size -= n_written;
            }
        }
        if (with_padding) {
            ALIGN_ON_32BITS_AND_SET_PADDING_ZEROS(&p_buff_ptr[*p_written], *p_written);
        }
        return 0;
    }
    default: {
        TSK_DEBUG_ERROR("Attribute format=%d is unknown.", pc_self->format);
        return -2;
    }
    }
}

int tbfcp_attr_write_without_padding(const tbfcp_attr_t* pc_self, uint8_t* p_buff_ptr, tsk_size_t n_buff_size, tsk_size_t *p_written)
{
    return _tbfcp_attr_write(pc_self, p_buff_ptr, n_buff_size, kWithoutPadding, p_written);
}

int tbfcp_attr_write_with_padding(const tbfcp_attr_t* pc_self, uint8_t* p_buff_ptr, tsk_size_t n_buff_size, tsk_size_t *p_written)
{
    return _tbfcp_attr_write(pc_self, p_buff_ptr, n_buff_size, kWithPadding, p_written);
}

int tbfcp_attr_read(const uint8_t* pc_buff_ptr, tsk_size_t n_buff_size, tsk_size_t *p_consumed_octets, tbfcp_attr_t** pp_attr)
{
    uint8_t M, Length, PadLength;
    tbfcp_attribute_type_t Type;
    tbfcp_attribute_format_t Format;
    int ret;
    if (!pc_buff_ptr || !n_buff_size || !pp_attr || !p_consumed_octets) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (n_buff_size < TBFCP_ATTR_HDR_SIZE_IN_OCTETS) {
        TSK_DEBUG_ERROR("Buffer too short(%u)", n_buff_size);
        return -2;
    }

    Length = pc_buff_ptr[1];
    if (Length > n_buff_size) {
        TSK_DEBUG_ERROR("Buffer too short(%u). Length=%u", n_buff_size, Length);
        return -3;
    }

    PadLength = (Length & 0x03) ? (4 - (Length & 0x03)) : 0;

    *pp_attr = tsk_null;
    *p_consumed_octets = Length + PadLength;

    Type = (pc_buff_ptr[0] >> 1) & 0x7F;
    M = (pc_buff_ptr[0] & 0x01);
    Format = _tbfcp_attr_get_format(Type);
    if (Format == tbfcp_attribute_format_Unknown) {
        return 0;
    }

    if (Format == tbfcp_attribute_format_Unsigned16) {
        uint16_t Unsigned16 = tnet_ntohs_2(&pc_buff_ptr[2]);
        if ((ret = tbfcp_attr_unsigned16_create(Type, M, Unsigned16, (tbfcp_attr_unsigned16_t**)pp_attr))) {
            return ret;
        }
    }
    else if (Format == tbfcp_attribute_format_OctetString16) {
        uint8_t OctetString16[2];
        OctetString16[0] = pc_buff_ptr[2];
        OctetString16[1] = pc_buff_ptr[3];
        if ((ret = tbfcp_attr_octetstring16_create(Type, M, OctetString16, (tbfcp_attr_octetstring16_t**)pp_attr))) {
            return ret;
        }
    }
    else if (Format == tbfcp_attribute_format_OctetString) {
        const uint8_t *OctetString = &pc_buff_ptr[TBFCP_ATTR_HDR_SIZE_IN_OCTETS];
        uint8_t OctetStringLength = (Length - TBFCP_ATTR_HDR_SIZE_IN_OCTETS);
        if ((ret = tbfcp_attr_octetstring_create(Type, M, OctetString, OctetStringLength, (tbfcp_attr_octetstring_t**)pp_attr))) {
            return ret;
        }
    }
    else if (Format == tbfcp_attribute_format_Grouped) {
        tbfcp_attr_grouped_t* p_attr;
        if ((ret = tbfcp_attr_grouped_create(Type, M, &p_attr))) {
            return ret;
        }
        *pp_attr = (tbfcp_attr_t*)p_attr;
        switch (Type) {
        case tbfcp_attribute_type_BENEFICIARY_INFORMATION: {
            p_attr->extra_hdr_size_in_octets = 2;
            p_attr->extra_hdr.BeneficiaryID = tnet_ntohs_2(&pc_buff_ptr[2]);
            break;
        }
        case tbfcp_attribute_type_FLOOR_REQUEST_INFORMATION: {
            p_attr->extra_hdr_size_in_octets = 2;
            p_attr->extra_hdr.FloorRequestID = tnet_ntohs_2(&pc_buff_ptr[2]);
            break;
        }
        case tbfcp_attribute_type_REQUESTED_BY_INFORMATION: {
            p_attr->extra_hdr_size_in_octets = 2;
            p_attr->extra_hdr.RequestedbyID = tnet_ntohs_2(&pc_buff_ptr[2]);
            break;
        }
        case tbfcp_attribute_type_FLOOR_REQUEST_STATUS: {
            p_attr->extra_hdr_size_in_octets = 2;
            p_attr->extra_hdr.FloorID = tnet_ntohs_2(&pc_buff_ptr[2]);
            break;
        }
        case tbfcp_attribute_type_OVERALL_REQUEST_STATUS: {
            p_attr->extra_hdr_size_in_octets = 2;
            p_attr->extra_hdr.FloorRequestID = tnet_ntohs_2(&pc_buff_ptr[2]);
            break;
        }
        default: {
            return 0;
        }
        }
        if ((TBFCP_ATTR_HDR_SIZE_IN_OCTETS + p_attr->extra_hdr_size_in_octets) < Length) {
            tsk_size_t n_consumed_octets, PayloadLength = Length;
            PayloadLength -= TBFCP_ATTR_HDR_SIZE_IN_OCTETS + p_attr->extra_hdr_size_in_octets;
            pc_buff_ptr += TBFCP_ATTR_HDR_SIZE_IN_OCTETS + p_attr->extra_hdr_size_in_octets;
            if (PayloadLength >= TBFCP_ATTR_HDR_SIZE_IN_OCTETS) {
                do {
                    if ((ret = tbfcp_attr_read(pc_buff_ptr, PayloadLength, &n_consumed_octets, (tbfcp_attr_t**)&p_attr))) {
                        break;
                    }
                    if ((ret = tbfcp_attr_grouped_add_attr((tbfcp_attr_grouped_t*)(*pp_attr), (tbfcp_attr_t**)&p_attr))) {
                        TSK_OBJECT_SAFE_FREE(p_attr);
                        break;
                    }
                    pc_buff_ptr += n_consumed_octets;
                    PayloadLength -= n_consumed_octets;
                }
                while (PayloadLength >= TBFCP_ATTR_HDR_SIZE_IN_OCTETS);
            }
        }
    }
    else {
        TSK_DEBUG_ERROR("%d not valid attribute format", Format);
        return -4;
    }
    return 0;
}


static int _tbfcp_attr_init(tbfcp_attr_t* p_self, tbfcp_attribute_type_t type, unsigned M, uint8_t length)
{
    if (!p_self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!p_self->pc_base) {
        p_self->pc_base = p_self;
    }
    p_self->hdr.type = type;
    p_self->hdr.M = M;
    p_self->hdr.length = length;
    if ((p_self->format = _tbfcp_attr_get_format(type)) == tbfcp_attribute_format_Unknown) {
        TSK_DEBUG_WARN("Attribute type=%d is unknown...setting its format to UNKNOWN. Don't be surprised if something goes wrong.", type);
    }

    return 0;
}


/*************** tbfcp_attr_unsigned16 *******************/
int tbfcp_attr_unsigned16_create(tbfcp_attribute_type_t type, unsigned M, uint16_t Unsigned16, tbfcp_attr_unsigned16_t** pp_self)
{
    extern const tsk_object_def_t *tbfcp_attr_unsigned16_def_t;
    tbfcp_attr_unsigned16_t* p_self;
    int ret;
    if (!pp_self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!(p_self = tsk_object_new(tbfcp_attr_unsigned16_def_t))) {
        TSK_DEBUG_ERROR("Failed to create 'tbfcp_attr_unsigned16_def_t' object");
        return -2;
    }
    if ((ret = _tbfcp_attr_init(TBFCP_ATTR(p_self), type, M, TBFCP_ATTR_HDR_SIZE_IN_OCTETS + 2))) {
        TSK_OBJECT_SAFE_FREE(p_self);
        return -3;
    }
    if (TBFCP_ATTR(p_self)->format != tbfcp_attribute_format_Unsigned16) {
        TSK_DEBUG_ERROR("Format mismatch");
        TSK_OBJECT_SAFE_FREE(p_self);
        return -4;
    }
    p_self->Unsigned16 = Unsigned16;
    *pp_self = p_self;
    return 0;
}

static tsk_object_t* tbfcp_attr_unsigned16_ctor(tsk_object_t * self, va_list * app)
{
    tbfcp_attr_unsigned16_t *p_u16 = (tbfcp_attr_unsigned16_t *)self;
    if (p_u16) {
    }
    return self;
}
static tsk_object_t* tbfcp_attr_unsigned16_dtor(tsk_object_t * self)
{
    tbfcp_attr_unsigned16_t *p_u16 = (tbfcp_attr_unsigned16_t *)self;
    if (p_u16) {
        TSK_DEBUG_INFO("*** BFCP Attribute(Unsigned16) destroyed ***");

    }
    return self;
}
static int tbfcp_attr_unsigned16_cmp(const tsk_object_t *_att1, const tsk_object_t *_att2)
{
    const tbfcp_attr_unsigned16_t *pc_att1 = (const tbfcp_attr_unsigned16_t *)_att1;
    const tbfcp_attr_unsigned16_t *pc_att2 = (const tbfcp_attr_unsigned16_t *)_att2;

    return (int)(pc_att1-pc_att2);
}
static const tsk_object_def_t tbfcp_attr_unsigned16_def_s = {
    sizeof(tbfcp_attr_unsigned16_t),
    tbfcp_attr_unsigned16_ctor,
    tbfcp_attr_unsigned16_dtor,
    tbfcp_attr_unsigned16_cmp,
};
const tsk_object_def_t *tbfcp_attr_unsigned16_def_t = &tbfcp_attr_unsigned16_def_s;




/*************** tbfcp_attr_octetstring16 *******************/
int tbfcp_attr_octetstring16_create(tbfcp_attribute_type_t type, unsigned M, uint8_t OctetString16[2], tbfcp_attr_octetstring16_t** pp_self)
{
    extern const tsk_object_def_t *tbfcp_attr_octetstring16_def_t;
    tbfcp_attr_octetstring16_t* p_self;
    int ret;
    if (!pp_self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!(p_self = tsk_object_new(tbfcp_attr_octetstring16_def_t))) {
        TSK_DEBUG_ERROR("Failed to create 'tbfcp_attr_octetstring16_def_t' object");
        return -2;
    }
    if ((ret = _tbfcp_attr_init(TBFCP_ATTR(p_self), type, M, TBFCP_ATTR_HDR_SIZE_IN_OCTETS + 2))) {
        TSK_OBJECT_SAFE_FREE(p_self);
        return -3;
    }
    if (TBFCP_ATTR(p_self)->format != tbfcp_attribute_format_OctetString16) {
        TSK_DEBUG_ERROR("Format mismatch");
        TSK_OBJECT_SAFE_FREE(p_self);
        return -4;
    }
    p_self->OctetString16[0] = OctetString16[0];
    p_self->OctetString16[1] = OctetString16[1];
    *pp_self = p_self;
    return 0;
}

static tsk_object_t* tbfcp_attr_octetstring16_ctor(tsk_object_t * self, va_list * app)
{
    tbfcp_attr_octetstring16_t *p_os16 = (tbfcp_attr_octetstring16_t *)self;
    if (p_os16) {
    }
    return self;
}
static tsk_object_t* tbfcp_attr_octetstring16_dtor(tsk_object_t * self)
{
    tbfcp_attr_octetstring16_t *p_os16 = (tbfcp_attr_octetstring16_t *)self;
    if (p_os16) {
        TSK_DEBUG_INFO("*** BFCP Attribute(OctetString16) destroyed ***");

    }
    return self;
}
static int tbfcp_attr_octetstring16_cmp(const tsk_object_t *_att1, const tsk_object_t *_att2)
{
    const tbfcp_attr_octetstring16_t *pc_att1 = (const tbfcp_attr_octetstring16_t *)_att1;
    const tbfcp_attr_octetstring16_t *pc_att2 = (const tbfcp_attr_octetstring16_t *)_att2;

    return (int)(pc_att1-pc_att2);
}
static const tsk_object_def_t tbfcp_attr_octetstring16_def_s = {
    sizeof(tbfcp_attr_octetstring16_t),
    tbfcp_attr_octetstring16_ctor,
    tbfcp_attr_octetstring16_dtor,
    tbfcp_attr_octetstring16_cmp,
};
const tsk_object_def_t *tbfcp_attr_octetstring16_def_t = &tbfcp_attr_octetstring16_def_s;


/*************** tbfcp_attr_octetstring *******************/
int tbfcp_attr_octetstring_create(tbfcp_attribute_type_t type, unsigned M, const uint8_t *OctetString, uint8_t OctetStringLength, tbfcp_attr_octetstring_t** pp_self)
{
    extern const tsk_object_def_t *tbfcp_attr_octetstring_def_t;
    tbfcp_attr_octetstring_t* p_self;
    int ret;
    if (!pp_self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!(p_self = tsk_object_new(tbfcp_attr_octetstring_def_t))) {
        TSK_DEBUG_ERROR("Failed to create 'tbfcp_attr_octetstring_def_t' object");
        return -2;
    }
    if ((ret = _tbfcp_attr_init(TBFCP_ATTR(p_self), type, M, (TBFCP_ATTR_HDR_SIZE_IN_OCTETS + OctetStringLength)))) {
        TSK_OBJECT_SAFE_FREE(p_self);
        return -3;
    }
    if (TBFCP_ATTR(p_self)->format != tbfcp_attribute_format_OctetString) {
        TSK_DEBUG_ERROR("Format mismatch");
        TSK_OBJECT_SAFE_FREE(p_self);
        return -4;
    }
    if (OctetStringLength) {
        if (!(p_self->OctetString = tsk_malloc(OctetStringLength))) {
            TSK_DEBUG_ERROR("Failed to alloc %u octets", OctetStringLength);
            TSK_OBJECT_SAFE_FREE(p_self);
            return -5;
        }
        if (OctetString) {
            memcpy(p_self->OctetString, OctetString, OctetStringLength);
        }
        p_self->OctetStringLength = OctetStringLength;
    }
    else {
        TBFCP_ATTR(p_self)->hdr.length = TBFCP_ATTR_HDR_SIZE_IN_OCTETS;
        p_self->OctetStringLength = 0;
    }
    *pp_self = p_self;
    return 0;
}

static tsk_object_t* tbfcp_attr_octetstring_ctor(tsk_object_t * self, va_list * app)
{
    tbfcp_attr_octetstring_t *p_os = (tbfcp_attr_octetstring_t *)self;
    if (p_os) {
    }
    return self;
}
static tsk_object_t* tbfcp_attr_octetstring_dtor(tsk_object_t * self)
{
    tbfcp_attr_octetstring_t *p_os = (tbfcp_attr_octetstring_t *)self;
    if (p_os) {
        TSK_DEBUG_INFO("*** BFCP Attribute(OctetString) destroyed ***");
        TSK_SAFE_FREE(p_os->OctetString);
    }
    return self;
}
static int tbfcp_attr_octetstring_cmp(const tsk_object_t *_att1, const tsk_object_t *_att2)
{
    const tbfcp_attr_octetstring_t *pc_att1 = (const tbfcp_attr_octetstring_t *)_att1;
    const tbfcp_attr_octetstring_t *pc_att2 = (const tbfcp_attr_octetstring_t *)_att2;

    return (int)(pc_att1-pc_att2);
}
static const tsk_object_def_t tbfcp_attr_octetstring_def_s = {
    sizeof(tbfcp_attr_octetstring_t),
    tbfcp_attr_octetstring_ctor,
    tbfcp_attr_octetstring_dtor,
    tbfcp_attr_octetstring_cmp,
};
const tsk_object_def_t *tbfcp_attr_octetstring_def_t = &tbfcp_attr_octetstring_def_s;



/*************** tbfcp_attr_grouped *******************/
int tbfcp_attr_grouped_create(tbfcp_attribute_type_t type, unsigned M, tbfcp_attr_grouped_t** pp_self)
{
    extern const tsk_object_def_t *tbfcp_attr_grouped_def_t;
    tbfcp_attr_grouped_t* p_self;
    int ret;
    if (!pp_self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!(p_self = tsk_object_new(tbfcp_attr_grouped_def_t))) {
        TSK_DEBUG_ERROR("Failed to create 'tbfcp_attr_grouped_def_t' object");
        return -2;
    }
    if ((ret = _tbfcp_attr_init(TBFCP_ATTR(p_self), type, M, TBFCP_ATTR_HDR_SIZE_IN_OCTETS + 0))) {
        TSK_OBJECT_SAFE_FREE(p_self);
        return -3;
    }
    if (TBFCP_ATTR(p_self)->format != tbfcp_attribute_format_Grouped) {
        TSK_DEBUG_ERROR("Format mismatch");
        TSK_OBJECT_SAFE_FREE(p_self);
        return -4;
    }
    if (!(p_self->p_list_attrs = tsk_list_create())) {
        TSK_DEBUG_ERROR("Failed to create empty list");
        TSK_OBJECT_SAFE_FREE(p_self);
        return -5;
    }

    *pp_self = p_self;
    return 0;
}

int tbfcp_attr_grouped_create_u16(tbfcp_attribute_type_t type, unsigned M, uint16_t extra_hdr_u16_val, tbfcp_attr_grouped_t** pp_self)
{
    int ret;
    if ((ret = tbfcp_attr_grouped_create(type, M, pp_self))) {
        return ret;
    }
    *((uint16_t*)&(*pp_self)->extra_hdr) = extra_hdr_u16_val;
    (*pp_self)->extra_hdr_size_in_octets = 2;
    return 0;
}

int tbfcp_attr_grouped_add_attr(tbfcp_attr_grouped_t* p_self, tbfcp_attr_t** p_attr)
{
    if (!p_self || !p_attr) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    tsk_list_push_back_data(p_self->p_list_attrs, (void**)p_attr);
    return 0;
}

int tbfcp_attr_grouped_find_at(const struct tbfcp_attr_grouped_s* pc_self, enum tbfcp_attribute_format_e e_format, tsk_size_t u_index, const struct tbfcp_attr_s** ppc_attr)
{
    tsk_size_t _u_index = 0;
    const tsk_list_item_t *pc_item;
    const struct tbfcp_attr_s* pc_attr;
    if (!pc_self || !ppc_attr) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    *ppc_attr = tsk_null;
    tsk_list_foreach(pc_item, pc_self->p_list_attrs) {
        pc_attr = (const struct tbfcp_attr_s*)pc_item->data;
        if (!pc_attr || pc_attr->format != e_format) {
            continue;
        }
        if (_u_index++ >= u_index) {
            *ppc_attr = pc_attr;
            break;
        }
    }
    return 0;
}

static tsk_object_t* tbfcp_attr_grouped_ctor(tsk_object_t * self, va_list * app)
{
    tbfcp_attr_grouped_t *p_gr = (tbfcp_attr_grouped_t *)self;
    if (p_gr) {
    }
    return self;
}
static tsk_object_t* tbfcp_attr_grouped_dtor(tsk_object_t * self)
{
    tbfcp_attr_grouped_t *p_gr = (tbfcp_attr_grouped_t *)self;
    if (p_gr) {
        TSK_DEBUG_INFO("*** BFCP Attribute(Grouped) destroyed ***");
        TSK_OBJECT_SAFE_FREE(p_gr->p_list_attrs);
    }
    return self;
}
static int tbfcp_attr_grouped_cmp(const tsk_object_t *_att1, const tsk_object_t *_att2)
{
    const tbfcp_attr_grouped_t *pc_att1 = (const tbfcp_attr_grouped_t *)_att1;
    const tbfcp_attr_grouped_t *pc_att2 = (const tbfcp_attr_grouped_t *)_att2;

    return (int)(pc_att1-pc_att2);
}
static const tsk_object_def_t tbfcp_attr_grouped_def_s = {
    sizeof(tbfcp_attr_grouped_t),
    tbfcp_attr_grouped_ctor,
    tbfcp_attr_grouped_dtor,
    tbfcp_attr_grouped_cmp,
};
const tsk_object_def_t *tbfcp_attr_grouped_def_t = &tbfcp_attr_grouped_def_s;
OpenPOWER on IntegriCloud