File: mid.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,700 kB
  • ctags: 10,983
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (1731 lines) | stat: -rw-r--r-- 47,796 bytes parent folder | download | duplicates (2)
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
/******************************************************************************
 **                           COPYRIGHT NOTICE
 **      (c) 2012 The Johns Hopkins University Applied Physics Laboratory
 **                         All rights reserved.
 **
 **     This material may only be used, modified, or reproduced by or for the
 **       U.S. Government pursuant to the license rights granted under
 **          FAR clause 52.227-14 or DFARS clauses 252.227-7013/7014
 **
 **     For any other permissions, please contact the Legal Office at JHU/APL.
 ******************************************************************************/

/*****************************************************************************
 **
 ** \file mid.c
 **
 ** Subsystem:
 **          Network Management Utilities
 **
 ** Description: This file contains the definitions, prototypes, constants, and
 **              other information necessary for the identification and
 **              processing of DTNMP Managed Identifiers (MIDs).
 **
 ** Notes:
 ** \todo 1. Need to make sure that all serialize and deserialize methods
 **          do regular bounds checking whenever writing and often when
 **          reading.
 **
 ** Assumptions:
 **      1. We limit the size of an OID in the system to reduce the amount
 **         of pre-allocated memory in this embedded system. Non-embedded
 **         implementations may wish to dynamically allocate MIDs as they are
 **         received.
 **
 **
 ** Modification History:
 **  MM/DD/YY  AUTHOR         DESCRIPTION
 **  --------  ------------   ---------------------------------------------
 **  10/21/11  E. Birrane     Code comments and functional updates.
 **  10/22/12  E. Birrane     Update to latest version of DTNMP. Cleanup.
 **  06/25/13  E. Birrane     New spec. rev. Remove priority from MIDs
 *****************************************************************************/

#include "platform.h"

#include "shared/utils/utils.h"

#include "shared/primitives/mid.h"


/******************************************************************************
 *
 * \par Function Name: mid_add_param
 *
 * \par Adds a parameter to the parameterized OID within a MID.
 *
 * \retval 0 Failure
 *        !0 Success
 *
 * \param[in,out] mid    The MID whose OID is getting a new param.
 * \param[in]     value  The value of the new parameter
 * \param[in]     len    The length, in bytes, of the new parameter.
 *
 * \par Notes:
 *		1. The new parameter is allocated into the new OID and, upon exit,
 *		   the passed-in value may be released if necessary.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/25/12  E. Birrane     Initial implementation,
 *****************************************************************************/
int mid_add_param(mid_t *mid, uint8_t *value, uint32_t len)
{
	int result = 0;

	DTNMP_DEBUG_ENTRY("mid_add_param","(%#llx, %#llx, %ld)", mid, value, len);

	/* Step 0: Sanity Check.*/
	if((mid == NULL) || (value == NULL) || (len == 0))
	{
		DTNMP_DEBUG_ERR("mid_add_param","Bad Args", NULL);
		DTNMP_DEBUG_EXIT("mid_add_param","->0.",NULL);
		return 0;
	}

	/* Step 1: Add to the OID. */
	result = oid_add_param(mid->oid, value, len);

	DTNMP_DEBUG_EXIT("mid_add_param","->%d.",result);
	return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_clear
 *
 * \par Resets the values associated with a MID. Basically, a structure-aware
 *      bzero.
 *
 * \retval void
 *
 * \param[in,out] mid  The MID being cleared.
 *
 * \par Notes:
 *		1. Clearing a MID is different than destroying a MID. This just clears
 *		   allocated members. The MID itself may be re-used.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *****************************************************************************/
void mid_clear(mid_t *mid)
{

    DTNMP_DEBUG_ENTRY("mid_clear","(%#llx)", (unsigned long) mid);

    if(mid == NULL)
    {
        DTNMP_DEBUG_ERR("mid_clear","Clearing NULL MID.", NULL);
        DTNMP_DEBUG_EXIT("mid_clear","->NULL.",NULL);
        return;
    }

    if(mid->raw != NULL)
    {
        MRELEASE(mid->raw);
        mid->raw = NULL;
    }

    if(mid->oid != NULL)
    {
    	oid_release(mid->oid);
    	mid->oid = NULL;
    }

    memset(mid, 0, sizeof(mid_t));
    DTNMP_DEBUG_EXIT("mid_clear","", NULL);
}



/******************************************************************************
 *
 * \par Function Name: mid_compare
 *
 * \par Determines equivalence of two MIDs.
 *
 * \retval -1,<0: Error or mid1 < mid2
 * 			0   : mid1 == mid2
 * 			>0  : mid1 > mid2
 *
 * \param[in] mid1      First MID being compared.
 * \param[in] mid2      Second MID being compared.
 * \param[in] use_parms Whether to compare OID paramaters as well.
 *
 * \par Notes:
 *		1. This function should only check for equivalence (== 0), not order
 *         since we do not differentiate between mid1 < mid2 and error.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *  06/25/13  E. Birrane     Removed references to priority field.
 *****************************************************************************/

int mid_compare(mid_t *mid1, mid_t *mid2, uint8_t use_parms)
{
	int result = 1;

    DTNMP_DEBUG_ENTRY("mid_compare","(%#llx, %#llx)",
    		         (unsigned long) mid1,
    		         (unsigned long) mid2);

    if((mid1 == NULL) || (mid2 == NULL))
    {
        return -1;
    }

    /* \todo: Cleanup */
    if(use_parms != 0)
    {
    if(mid1->raw_size != mid2->raw_size)
    {
        return -1;
    }
    }
    
    /* For now, we can just compare the raw version of the mid */
    /*result = memcmp(mid1->raw, mid2->raw, mid1->raw_size);*/
    /* Step 3: Check if flag and MID length bytes match. */
    if((mid1->flags == mid2->flags) &&
       (mid1->issuer == mid2->issuer) &&
       (mid1->tag == mid2->tag))
    {
    	result = oid_compare(mid1->oid, mid2->oid, use_parms);
    }

    DTNMP_DEBUG_EXIT("mid_compare","->%d.", result);
    return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_construct
 *
 * \par Constructs a MID object from arguments.
 *
 * \retval NULL - Failure
 *         !NULL - The constructed MID
 *
 * \param[in] type     The type of MID
 * \param[in] cat      The MID category
 * \param[in] issuer   The MID issuer, or NULL for no issuer.
 * \param[in] tag      The MID tag, or NULL for no tag.
 * \param[in] oid      The OID encapsulated in the MID.
 *
 * \par Notes:
 *		1. The returned MID is allocated and must be freed when no longer needed.
 *		2. The passed-in OID is *deep copied* and may be released before releasing
 *		   the MID.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *  06/17/13  E. Birrane     Updated to ION 3.1.3, switched to uvast
 *  06/25/13  E. Birrane     Removed references to priority field.
 *****************************************************************************/

mid_t *mid_construct(uint8_t type, uint8_t cat,
		             uvast *issuer, uvast *tag, oid_t *oid)
{
	mid_t *mid = NULL;
	DTNMP_DEBUG_ENTRY("mid_construct","(%#llx, %#llx, %#llx, %#llx, %#llx",
			         type, cat,
			         (unsigned long) issuer, (unsigned long) tag,
			         (unsigned long) oid);


	/* Step 0: Sanity Check */
	if(oid == NULL)
	{
		DTNMP_DEBUG_ERR("mid_construct","Bad Args.", NULL);
		DTNMP_DEBUG_EXIT("mid_construct","->NULL",NULL);
		return NULL;
	}

	/* Step 1: Allocate the MID. */
	if((mid = (mid_t *)MTAKE(sizeof(mid_t))) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_construct","Can't allocate %d bytes.",
				        sizeof(mid_t));
		DTNMP_DEBUG_EXIT("mid_construct","->NULL",NULL);
		return NULL;
	}

	/* Step 2: Populate the MID. */

	/* Flag */
	mid->flags =  (oid->type & 0x03) << 6;
	mid->flags |= (tag != NULL) ? 0x20 : 0x00;
	mid->flags |= (issuer != NULL) ? 0x10 : 0x00;
	mid->flags |= (cat & 0x03) << 2;
	mid->flags |= (type & 0x03);

	/* Shallow copies */
	mid->type     = type;
	mid->category = cat;
	mid->issuer   = (issuer != NULL) ? *issuer : 0;
	mid->tag      = (tag != NULL) ? *tag : 0;

	if((mid->oid = oid_copy(oid)) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_construct","Failed to copy OID.",NULL);

		MRELEASE(mid);
		DTNMP_DEBUG_EXIT("mid_construct","->NULL",NULL);
		return NULL;
	}

	/* Step 3: Build the serialized MID. */
	if (mid_internal_serialize(mid) == 0)
	{
		DTNMP_DEBUG_ERR("mid_construct","Failed to serialize MID.",NULL);

		mid_release(mid);

		DTNMP_DEBUG_EXIT("mid_construct","->NULL",NULL);
		return NULL;
	}


	DTNMP_DEBUG_EXIT("mid_construct","->0x%x",(unsigned long)mid);
	return mid;
}



/******************************************************************************
 *
 * \par Function Name: mid_copy
 *
 * \par Duplicates a MID object.
 *
 * \retval NULL - Failure
 *         !NULL - The copied MID
 *
 * \param[in] src_mid The MID being copied.
 *
 * \par Notes:
 *		1. The returned MID is allocated and must be freed when no longer needed.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *****************************************************************************/

mid_t *mid_copy(mid_t *src_mid)
{
    mid_t *result = 0;
    
    DTNMP_DEBUG_ENTRY("mid_copy","(%#llx)",
    		          (unsigned long) src_mid);
    
    /* Step 0: Sanity Check */
    if(src_mid == NULL)
    {
        DTNMP_DEBUG_ERR("mid_copy","Cannot copy from NULL source MID.", NULL);
        DTNMP_DEBUG_EXIT("mid_copy","->NULL",NULL);
        return NULL;
    }

    /* Step 1: Allocate the new MID. */
    if((result = (mid_t *)MTAKE(sizeof(mid_t))) == NULL)
    {
        DTNMP_DEBUG_ERR("mid_copy","Can't allocate %d bytes", sizeof(mid_t));
        DTNMP_DEBUG_EXIT("mid_copy","->NULL",NULL);
        return NULL;
    }

    /* Step 2: Start with a shallow copy. */
    memcpy(result, src_mid, sizeof(mid_t));

    /* Step 3: Now, deep copy the pointers. */
    result->oid = oid_copy(src_mid->oid);

    if((result->raw = (uint8_t *)MTAKE(src_mid->raw_size)) == NULL)
    {
        DTNMP_DEBUG_ERR("mid_copy","Can't allocate %d bytes",
        		        src_mid->raw_size);

        MRELEASE(result->oid);
        MRELEASE(result);

        DTNMP_DEBUG_EXIT("mid_copy","->NULL",NULL);
        return NULL;
    }

    memcpy(result->raw, src_mid->raw, src_mid->raw_size);

    DTNMP_DEBUG_EXIT("mid_copy","->%d", result);
    return result;
}


/******************************************************************************
 *
 * \par Function Name: mid_deserialize
 *
 * \par Extracts a MID from a byte buffer.
 *
 * \retval NULL - Failure
 *         !NULL - The created/deserialized MID.
 *
 * \param[in]  buffer       The byte buffer holding the MID data
 * \param[in]  buffer_size  The # bytes available in the buffer
 * \param[out] bytes_used   The # of bytes consumed in the deserialization.
 *
 * \par Notes:
 * \todo: Allow a NULL bytes_used for cases where we are not deserializing
 *        from a larger stream.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *  06/25/13  E. Birrane     Removed references to priority field.
 *****************************************************************************/
mid_t *mid_deserialize(unsigned char *buffer,
		               uint32_t  buffer_size,
		               uint32_t *bytes_used)
{
    mid_t *result = NULL;
    unsigned char *cursor = NULL;
    int sdnv_len = 0;
    uint32_t cur_bytes = 0;
    uint32_t bytes_left=0;

    DTNMP_DEBUG_ENTRY("mid_deserialize","(%#llx, %d, %#llx)",
                     (unsigned long) buffer,
                     (unsigned long) buffer_size,
                     (unsigned long) bytes_used);

    *bytes_used = 0;

    /* Step 1: Sanity checks. */
    if((buffer == NULL) || (buffer_size == 0) || (bytes_used == NULL))
    {
        DTNMP_DEBUG_ERR("mid_deserialize","Bad params.",NULL);
        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }
    else
    {
    	*bytes_used = 0;
    	cursor = buffer;
    	bytes_left = buffer_size;
    }

    /* Step 2: Allocate/Zero the MID. */
    if((result = (mid_t *) MTAKE(sizeof(mid_t))) == NULL)
    {
        DTNMP_DEBUG_ERR("mid_deserialize","Cannot allocate MID of size %d",
        		        sizeof(mid_t));
        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }
    else
    {
        memset(result,0,sizeof(mid_t));
    }

    /* Step 3: Grab the MID flag */
    if(utils_grab_byte(cursor, bytes_left, &(result->flags)) != 1)
    {
    	DTNMP_DEBUG_ERR("mid_deserialize","Can't grab flag byte.", NULL);
    	mid_release(result);

        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }
    else
    {
    	cursor += 1;
    	bytes_left -= 1;
    	*bytes_used += 1;
    }

    /* Step 4: Break out flag...*/
    result->type = MID_GET_FLAG_TYPE(result->flags);
    result->category = MID_GET_FLAG_CAT(result->flags);

    /* Step 5: Grab issuer, if present. Issuers MUST exist for non-atomic.*/
    if(MID_GET_FLAG_ISS(result->flags))
    {
    	cur_bytes = utils_grab_sdnv(cursor, bytes_left, &(result->issuer));
        if( cur_bytes == 0)
        {
        	DTNMP_DEBUG_ERR("mid_deserialize","Can't grab issuer.", NULL);
        	mid_release(result);

            DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
            return NULL;
        }
        else
        {
        	cursor += cur_bytes;
        	bytes_left -= cur_bytes;
        	*bytes_used += cur_bytes;
        }
    }

    /* Step 6: Grab the OID. */
    switch(MID_GET_FLAG_OID(result->flags))
    {
    	case OID_TYPE_FULL:
    		result->oid = oid_deserialize_full(cursor, bytes_left, &cur_bytes);
    		break;
    	case OID_TYPE_PARAM:
    		result->oid = oid_deserialize_param(cursor, bytes_left, &cur_bytes);
    		break;
    	case OID_TYPE_COMP_FULL:
    		result->oid = oid_deserialize_comp(cursor, bytes_left, &cur_bytes);
    		break;
    	case OID_TYPE_COMP_PARAM:
    		result->oid = oid_deserialize_comp_param(cursor, bytes_left, &cur_bytes);
    		break;
    	default:
    		result->oid = NULL;
    		cur_bytes = 0;
    		break;
    }

    if(result->oid == NULL)
    {
    	DTNMP_DEBUG_ERR("mid_deserialize","Can't grab oid.", NULL);
    	mid_release(result);

        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }
    else
    {
    	cursor += cur_bytes;
    	bytes_left -= cur_bytes;
    	*bytes_used += cur_bytes;
    	result->oid->type = MID_GET_FLAG_OID(result->flags);
    }


    /* Step 7: Populate Tag if preset. */
    if(MID_GET_FLAG_TAG(result->flags))
    {
    	cur_bytes = utils_grab_sdnv(cursor, bytes_left, &(result->tag));
        if( cur_bytes == 0)
        {
        	DTNMP_DEBUG_ERR("mid_deserialize","Can't grab tag.", NULL);
        	mid_release(result);

            DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
            return NULL;
        }
        else
        {
        	cursor += cur_bytes;
        	bytes_left -= cur_bytes;
        	*bytes_used += cur_bytes;
        }
    }

    /* Step 8: Is this a sane MID? */
    if(mid_sanity_check(result) == 0)
    {
    	DTNMP_DEBUG_ERR("mid_deserialize","Flags Sanity Check Failed.", NULL);
    	mid_release(result);

        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }

    /* Step 9: Copy MID into the raw value. */
    if((result->raw = (unsigned char *) MTAKE(*bytes_used)) == NULL)
    {
        DTNMP_DEBUG_ERR("mid_deserialize","Can't TAKE raw mid of size (%d)",
        		        *bytes_used);
        mid_release(result);

        DTNMP_DEBUG_EXIT("mid_deserialize","-> NULL", NULL);
        return NULL;
    }

    memcpy(result->raw, buffer, *bytes_used);
    result->raw_size = *bytes_used;

    DTNMP_DEBUG_EXIT("mid_deserialize","-> %#llx", (unsigned long) result);
    return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_internal_serialize
 *
 * \par populates the raw value of a MID with it's serialized representation.
 *
 * \par Since MIDs are used heavily in the agent, and since they are received in
 * a serialized form, it is efficient to keep the serialized version of the mid
 * to avoid having to re-calculate it.  However, when the MID changes, or when
 * software generates the MID initially, the serialized representation must
 * be created.
 *
 * \par
 *  +--------+--------+--------+--------+
 *  | Flags  | Issuer |   OID  |   Tag  |
 *  | [BYTE] | [SDNV] | [SDNV] | [SDNV] |
 *  |        | (opt.) |        | (opt)  |
 *  +--------+--------+--------+--------+
 *
 * \retval 0 - Failure
 *         !0 - Success serializing the MID.
 *
 * \param[in,out] mid The MID being serialized
 *
 * \par Notes:
 *      1. The serialized version of the MID exists on the memory pool and must be
 *         released when finished.
 *      2. Note that the issuer and tag are capped in this implementation.
 *      3. On error, the state of the mid->raw content is unknown.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  10/22/12  E. Birrane     Initial implementation,
 *  06/25/13  E. Birrane     Removed references to priority field.
 *****************************************************************************/

int mid_internal_serialize(mid_t *mid)
{
	Sdnv iss;
	Sdnv tag;
	uint32_t oid_size = 0;
	uint8_t *oid_val = NULL;
	uint8_t *cursor = NULL;

	DTNMP_DEBUG_ENTRY("mid_internal_serialize","(%#llx)",(unsigned long) mid);

	/* Step 0: Sanity Check. */
	if(mid == NULL)
	{
		DTNMP_DEBUG_ERR("mid_internal_serialize","Bad args.",NULL);
		DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL);
		return 0;
	}

	/* Step 1: Serialize the OID for sizing. */
	if((oid_val = oid_serialize(mid->oid, &oid_size)) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_internal_serialize","Can't serialize OID.",NULL);
		DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL);
		return 0;
	}

	/* Step 2: If there is a serialized version of the MID already, wipe it.*/
	if(mid->raw != NULL)
	{
		MRELEASE(mid->raw);
		mid->raw = NULL;
	}
	mid->raw_size = 0;

	/* Step 3: Build the SDNVs. */
	encodeSdnv(&iss, mid->issuer);
	encodeSdnv(&tag, mid->tag);


	/* Step 4: Figure out the size of the serialized MID. */
	mid->raw_size = 1 + oid_size;

	/* Add issuer if present. */
	if(MID_GET_FLAG_ISS(mid->flags))
	{
		mid->raw_size += iss.length;
	}

	/* Add tag if present. */
	if(MID_GET_FLAG_TAG(mid->flags))
	{
		mid->raw_size += tag.length;
	}


	/* Step 5: Allocate space for the serialized MID. */
	if((mid->raw = (uint8_t *)MTAKE(mid->raw_size)) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_internal_serialize","Can't alloc %d bytes.",
				        mid->raw_size);
		mid->raw_size = 0;
		MRELEASE(oid_val);

		DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL);
		return 0;
	}

	/* Step 6: Populate the serialized MID. */
	cursor = mid->raw;

	*cursor = mid->flags;
	cursor++;

	/* Add issuer if present. */
	if(MID_GET_FLAG_ISS(mid->flags))
	{
		memcpy(cursor, iss.text, iss.length);
		cursor += iss.length;
	}

	/* Copy the OID. */
	memcpy(cursor,oid_val, oid_size);
	cursor += oid_size;

	/* Add tag if present. */
	if(MID_GET_FLAG_TAG(mid->flags))
	{
		memcpy(cursor, tag.text, tag.length);
		cursor += tag.length;
	}

	/* Step 7: Final sanity check. */
	if((cursor - mid->raw) != mid->raw_size)
	{
		DTNMP_DEBUG_ERR("mid_internal_serialize","Copied %d bytes, expected %d bytes.",
				        (cursor - mid->raw), mid->raw_size);
		mid->raw_size = 0;
		MRELEASE(mid->raw);
		mid->raw = NULL;
		MRELEASE(oid_val);

		DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL);
		return 0;

	}

	DTNMP_DEBUG_EXIT("mid_internal_serialize","->1",NULL);
	return 1;
}



/******************************************************************************
 *
 * \par Function Name: mid_pretty_print
 *
 * \par Purpose: Builds a string representation of the MID suitable for
 *               diagnostic viewing.
 *
 * \retval  NULL: Failure to construct a string representation of the MID.
 *         !NULL: The string representation of the MID string.
 *
 * \param[in]  mid  The MID whose string representation is being created.
 *
 * \par Notes:
 *		1. This is a slow, wasteful function and should not be called in
 *		   embedded systems.  For something to dump in a log, try the function
 *		   mid_to_string instead.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *  06/25/13  E. Birrane     Removed references to priority field.
 *****************************************************************************/

char *mid_pretty_print(mid_t *mid)
{
	int size = 0;
	int oid_size = 0;
	int raw_size = 0;
	char *oid_str = NULL;
	char *raw_str;
	char *result = NULL;
	char *cursor = NULL;

	DTNMP_DEBUG_ENTRY("mid_pretty_print","(%#llx)", (unsigned long) mid);

	/* Step 0: Sanity Check. */
	if(mid == NULL)
	{
		DTNMP_DEBUG_ERR("mid_pretty_print","NULL MID.",NULL);
		DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL.",NULL);
		return NULL;
	}


	/* Step 1: Grab the pretty-print of the encapsulated OID. We will need to
	 *         do this anyway and it will help with the sizing.
	 */
	if(mid->oid != NULL)
	{
		oid_str = oid_pretty_print(mid->oid);
		oid_size = strlen((char *)oid_str) + 1;
	}

	if(oid_str == NULL)
	{
		oid_size = strlen("NULL_OID") + 1;
		if((oid_str = (char *) MTAKE(oid_size)) != NULL)
		{
			memset(oid_str,0,oid_size);
			strncpy((char *)oid_str,"NULL OID",oid_size);
		}
		else
		{
			DTNMP_DEBUG_ERR("mid_pretty_print","Can't alloc %d bytes for OID.",
						    oid_size);

			DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL.",NULL);
			return NULL;
		}
	}

	/* Step 2: Grab the raw version of the MID string. */
	if((raw_str = mid_to_string(mid)) == NULL)
	{
		raw_size = strlen("NO RAW!") + 1;
		if((raw_str = (char *) MTAKE(raw_size)) != NULL)
		{
			memset(raw_str, 0, raw_size);
			strncpy(raw_str,"NO RAW!", raw_size);
		}
		else
		{
			DTNMP_DEBUG_ERR("mid_pretty_print","Can't alloc %d bytes for RAW.",
						    raw_size);
			MRELEASE(oid_str);

			DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL.",NULL);
			return NULL;
		}
	}
	else
	{
		raw_size = strlen((char*)raw_str) + 1;
	}

	/* Step 3: Guestimate size. This is, at best, a dark art and prone to
	 *         exaggeration. Numerics are assumed to take 5 bytes, unless I
	 *         think they will take 3 instead (indices vs. values). Other
	 *         values are based on constant strings in the function. One must
	 *         update this calculation if one changes string constants, else
	 *         one will be sorry.
	 */
	size = 28 +             /* BANNER------ */
		   14 +   			/* Flag : <...> */
		   18 +   			/* Type : <...> */
		   17 +   			/* Cat : <...>  */
		   17 +        		/* ISS : <...>  */
		   7 + oid_size +   /* OID : <...>  */
		   17 +             /* Tag : <...>  */
		   7 + raw_size +   /* Raw : <...>  */
		   28;				/* BANNER ----- */

	/* Step 4: Allocate the string. */
	if((result = (char*)MTAKE(size)) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_pretty_print", "Can't alloc %d bytes.", size);

		MRELEASE(oid_str);
		MRELEASE(raw_str);

		DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL",NULL);
		return NULL;
	}
	else
	{
		memset(result,0,size);
	}

	/* Step 5: Populate the allocated string. Keep an eye on the size. */
	cursor = result;
	cursor += sprintf(cursor,"MID:\n---------------------\nFlag: %#x",mid->flags);

	cursor += sprintf(cursor,"\nType : ");
	switch(mid->type)
	{
	case 0: cursor += sprintf(cursor,"DATA\n"); break;
	case 1: cursor += sprintf(cursor,"CONTROL\n"); break;
	case 2: cursor += sprintf(cursor,"LITERAL\n"); break;
	case 3: cursor += sprintf(cursor,"OPERATOR\n"); break;
	default: cursor += sprintf(cursor,"UNKNOWN\n"); break;
	}

	cursor += sprintf(cursor,"Cat: ");
	switch(mid->category)
	{
	case 0: cursor += sprintf(cursor,"ATOMIC\n"); break;
	case 1: cursor += sprintf(cursor,"COMPUTED\n"); break;
	case 2: cursor += sprintf(cursor,"COLLECTION\n"); break;
	default: cursor += sprintf(cursor,"UNKNOWN\n"); break;
	}

	if(MID_GET_FLAG_ISS(mid->flags))
	{
		cursor += sprintf(cursor,UVAST_FIELDSPEC"\n",mid->issuer);
	}
	else
	{
		cursor += sprintf(cursor,"None.\n");
	}

	cursor += sprintf(cursor,"OID : %s", oid_str);
	MRELEASE(oid_str);

	if(MID_GET_FLAG_TAG(mid->flags))
	{
		cursor += sprintf(cursor,UVAST_FIELDSPEC"\n",mid->tag);
	}
	else
	{
		cursor += sprintf(cursor,"None.\n");
	}

	cursor += sprintf(cursor,"RAW : %s", raw_str);
	MRELEASE(raw_str);

	/* Step 6: Sanity check. */
	if((cursor - result) > size)
	{
		DTNMP_DEBUG_ERR("mid_pretty_print", "OVERWROTE! Alloc %d, wrote %llu.",
				        size, (cursor-result));
		MRELEASE(result);
		DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL",NULL);
		return NULL;
	}

	DTNMP_DEBUG_INFO("mid_pretty_print","Wrote %llu into %d string.",
			         (cursor -result), size);

	DTNMP_DEBUG_EXIT("mid_pretty_print","->%#llx",result);

	return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_release
 *
 * \par Purpose: Frees resources associated with a MID
 *
 * \param[in,out] mid  The MID being released.
 *
 * \par Notes:
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/

void mid_release(mid_t *mid)
{
    DTNMP_DEBUG_ENTRY("mid_release", "(%#llx)", (unsigned long) mid);

    if(mid != NULL)
    {
        mid_clear(mid);
        MRELEASE(mid);
        mid = NULL;
    }

    DTNMP_DEBUG_EXIT("mid_release", "-> NULL", NULL);
}



/******************************************************************************
 *
 * \par Function Name: mid_sanity_check
 *
 * \par Purpose: Evaluates the consistency of MID member values.
 *
 * \retval  0: The MID failed its check.
 *         !0: The MID passed its check.
 *
 * \param[in] mid  The MID being released.
 *
 * \par Notes:
 *		1. This function keeps going after first sanity failure to collect
 *		   all MID problems in the error log.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *  06/25/13  E. Birrane     Removed references to priority field. Removed
 *                           type/cat checks as spec allows many new combos.
 *****************************************************************************/

int mid_sanity_check(mid_t *mid)
{
	int result = 1;
    DTNMP_DEBUG_ENTRY("mid_sanity_check","(%#llx)", (unsigned long) mid);

	/* NULL Checks */
	if(mid == NULL)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","NULL mid.", NULL);
        return 0;
	}

	/* Range Checks */
	if(mid->type > 3)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","Type out of range (%d)",
        		        mid->type);
        result = 0;
	}

	if(mid->category > 2)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","Cat. out of range (%d)",
        		        mid->category);
        result = 0;
	}

	if(mid->oid == NULL)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","NULL OID.",NULL);
        result = 0;
	}
	else if(mid->oid->type > 3)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","OID type out of range (%d)",
        		        mid->oid->type);
        result = 0;
	}


	/* Type/Category Checks */
	if (
		((mid->type == MID_TYPE_CONTROL) && (mid->category == MID_CAT_COMPUTED)) ||
		((mid->type == MID_TYPE_LITERAL) && (mid->category != MID_CAT_ATOMIC)) ||
		((mid->type == MID_TYPE_OPERATOR) && (mid->category != MID_CAT_ATOMIC))
		)
	{
        DTNMP_DEBUG_ERR("mid_sanity_check","Bad type(%d)/cat.(%d) combo.",
        		        mid->type, mid->category);
        result = 0;
	}

    DTNMP_DEBUG_EXIT("mid_sanity_check","-> %d", result);
    return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_serialize
 *
 * \par Purpose: Returns a serialized version of the MID.
 *
 * \retval  NULL - Failure
 *         !NULL - The serialized MID.
 *
 * \param[in,out] mid   The MID being serialized
 * \param[out]    size  The # bytes of the serialized MID.
 *
 * \par Notes:
 *		1. The serialized version of the MID exists on the memory pool and must be
 *         released when finished.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/

uint8_t *mid_serialize(mid_t *mid, uint32_t *size)
{
	uint8_t *result = NULL;

	DTNMP_DEBUG_ENTRY("mid_serialize","(%#llx, %#llx)",
			          (unsigned long) mid, (unsigned long) size);

	/* Step 0: Sanity Check. */
	if((mid == NULL) || (size == NULL))
	{
		DTNMP_DEBUG_ERR("mid_serialize","Bad args.", NULL);
		DTNMP_DEBUG_EXIT("mid_serialize","->NULL", NULL);
		return NULL;
	}

	/* Step 1: Make sure the MID is serialized. */
	if((mid->raw == NULL) || (mid->raw_size == 0))
	{
		if(mid_internal_serialize(mid) == 0)
		{
			DTNMP_DEBUG_ERR("mid_serialize","Can't serialize mid.", NULL);
			*size = 0;
			DTNMP_DEBUG_EXIT("mid_serialize","->NULL", NULL);
			return NULL;
		}
	}

	*size = mid->raw_size;

	/* Step 2: Allocate result. */
	if((result = (uint8_t*) MTAKE(*size)) == NULL)
	{
		DTNMP_DEBUG_ERR("mid_serialize","Can't alloc %d bytes.", *size);
		*size = 0;
		DTNMP_DEBUG_EXIT("mid_serialize","->NULL", NULL);
		return NULL;
	}

	/* Step 3: Copy in the result. */
	memcpy(result, mid->raw, mid->raw_size);

	DTNMP_DEBUG_EXIT("mid_serialize","->%#llx", (unsigned long)result);
	return result;
}



/******************************************************************************
 *
 * \par Function Name: mid_to_string
 *
 * \par Purpose: Create a string representation of the MID.
 *
 * \retval  NULL - Failure
 *         !NULL - The string representation of the raw MID.
 *
 * \param[in] mid The MID whose string representation is being calculated.
 *
 * \par Notes:
 *		1. The string is dynamically allocated from the memory pool and must
 *		   be deallocated when no longer needed.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/

char *mid_to_string(mid_t *mid)
{
    char *result = NULL;

    DTNMP_DEBUG_ENTRY("mid_to_string","(%#llx)",(unsigned long) mid);

    /* For now, we just show the raw MID. */
	result = utils_hex_to_string(mid->raw, mid->raw_size);

    DTNMP_DEBUG_EXIT("mid_to_string","->%s.", result);

	return result;
}



/******************************************************************************
 *
 * \par Function Name: midcol_copy
 *
 * \par Purpose: Copies a MID collection
 *
 * \retval  NULL - Failure
 *         !NULL - The copied MID collection
 *
 * \param[in]  mids  MID collection to be copied.
 *
 * \par Notes:
 *		1. This is a deep copy.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/
Lyst midcol_copy(Lyst mids)
{
	Lyst result = NULL;
	LystElt elt;
	mid_t *cur_mid = NULL;
	mid_t *new_mid = NULL;

	DTNMP_DEBUG_ENTRY("midcol_copy","(%#llx)",(unsigned long) mids);

	/* Step 0: Sanity Check. */
	if(mids == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_copy","Bad Args.",NULL);
		DTNMP_DEBUG_EXIT("midcol_copy","->NULL.",NULL);
		return NULL;
	}

	/* Build the Lyst. */
	if((result = lyst_create()) == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_copy","Unable to create lyst.",NULL);
		DTNMP_DEBUG_EXIT("midcol_copy","->NULL.",NULL);
		return NULL;
	}

	/* Walking copy. */
	int success = 1;
	for(elt = lyst_first(mids); elt; elt = lyst_next(elt))
	{
		cur_mid = (mid_t *) lyst_data(elt);
		if((new_mid = mid_copy(cur_mid)) == NULL)
		{
			DTNMP_DEBUG_ERR("midcol_copy","Fsiled to copy MID.",NULL);
			midcol_destroy(&result);

			DTNMP_DEBUG_EXIT("midcol_copy","->NULL.",NULL);
			return NULL;
		}
		else
		{
			lyst_insert_last(result,mid_copy(cur_mid));
		}
	}

	DTNMP_DEBUG_EXIT("midcol_copy","->%#llx.",(unsigned long) result);
	return result;
}



/******************************************************************************
 *
 * \par Function Name: midcol_destroy
 *
 * \par Purpose: Destroy MID collection in a Lyst.
 *
 * \retval  NULL - Failure
 *         !NULL - The copied MID collection
 *
 * \param[in,out] mids The lyst being destroyed.
 *
 * \par Notes:
 *		1. We pass a double pointer for the honor of making sure the lyst is set
 *         to NULL when finished.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/
void midcol_destroy(Lyst *mids)
{
	LystElt elt;
	mid_t *cur_mid = NULL;

	DTNMP_DEBUG_ENTRY("midcol_destroy","(%#llx)", (unsigned long) mids);

	/*
	 * Step 0: Make sure we even have a lyst. Not an error if not, since we
	 * are destroying anyway.
	 */
	if((mids == NULL) || (*mids == NULL))
	{
		DTNMP_DEBUG_ERR("midcol_destroy","Bad Args.",NULL);
		DTNMP_DEBUG_EXIT("midcol_destroy","->.", NULL);
		return;
	}

	/* Step 1: Walk through the MIDs releasing as you go. */
    for(elt = lyst_first(*mids); elt; elt = lyst_next(elt))
    {
    	cur_mid = (mid_t *) lyst_data(elt);

    	if(cur_mid != NULL)
    	{
    		mid_release(cur_mid);
    	}
    }

    /* Step 2: Destroy and zero out the lyst. */
    lyst_destroy(*mids);
    *mids = NULL;

    DTNMP_DEBUG_EXIT("midcol_destroy","->.", NULL);
}



/******************************************************************************
 *
 * \par Function Name: midcol_deserialize
 *
 * \par Purpose: Deserialize a MID collection into a Lyst.
 *
 * \retval NULL - Failure
 *        !NULL - The created Lyst.
 *
 * \param[in]  buffer      The buffer holding the MC.
 * \param[in]  buffer_size The size of the buffer, in bytes.
 * \param[out] bytes_used  The # bytes consumed in the deserialization.
 *
 * \par Notes:
 *		1. The created Lyst must be freed when done.
 *		2. Reminder: A Lyst is a pointer to a LystStruct.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *  06/17/13  E. Birrane     Updated to ION 3.1.3, moved to uvast
 *****************************************************************************/
Lyst midcol_deserialize(unsigned char *buffer,
		                uint32_t buffer_size,
		                uint32_t *bytes_used)
{
	unsigned char *cursor = NULL;
	Lyst result = NULL;
	uint32_t bytes = 0;
	uvast num = 0;
	mid_t *cur_mid = NULL;
	uint32_t i = 0;

	DTNMP_DEBUG_ENTRY("midcol_deserialize","(%#llx,%d,%#llx)",
			          (unsigned long) buffer, buffer_size,
			          (unsigned long) bytes_used);

	/* Step 0: Sanity Check. */
	if((buffer == NULL) || (buffer_size == 0) || (bytes_used == NULL))
	{
		DTNMP_DEBUG_ERR("mid_deserialize_mc","Bad Args", NULL);
		DTNMP_DEBUG_EXIT("mid_deserialize_mc","->NULL",NULL);
		return NULL;
	}

	*bytes_used = 0;
	cursor = buffer;

	/* Step 1: Create the Lyst. */
	if((result = lyst_create()) == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_deserialize","Can't create lyst.", NULL);
		DTNMP_DEBUG_EXIT("midcol_deserialize","->NULL",NULL);
		return NULL;
	}

	/* Step 2: Grab # MIDs in the collection. */
	if((bytes = utils_grab_sdnv(cursor, buffer_size, &num)) == 0)
	{
		DTNMP_DEBUG_ERR("midcol_deserialize","Can't parse SDNV.", NULL);
		lyst_destroy(result);

		DTNMP_DEBUG_EXIT("midcol_deserialize","->NULL",NULL);
		return NULL;
	}
	else
	{
		cursor += bytes;
		buffer_size -= bytes;
		*bytes_used += bytes;
	}

	/* Step 3: Grab Mids. */
	for(i = 0; i < num; i++)
	{
		/* Deserialize ith MID. */
		if((cur_mid = mid_deserialize(cursor, buffer_size, &bytes)) == NULL)
		{
			DTNMP_DEBUG_ERR("mid_deserialize_mc","Can't grab MID #%d.", i);
			midcol_destroy(&result);

			DTNMP_DEBUG_EXIT("mid_deserialize_mc","->NULL",NULL);
			return NULL;
		}
		else
		{
			cursor += bytes;
			buffer_size -= bytes;
			*bytes_used += bytes;
		}

		/* Drop it in the lyst in order. */
		lyst_insert_last(result, cur_mid);
	}

	DTNMP_DEBUG_EXIT("midcol_deserialize","->%#llx",(unsigned long)result);
	return result;
}





/******************************************************************************
 *
 * \par Function Name: midcol_pretty_print
 *
 * \par Purpose: Builds a string representation of a MID collection suitable for
 *               diagnostic viewing.
 *
 * \retval NULL: Failure to construct a string representation of the MID coll.
 *         !NULL: The string representation of the MID coll string.
 *
 * \param[in]  mc  The MID collection whose string rep is being created.
 *
 * \par Notes:
 *		1. This is a slow, wasteful function and should not be called in
 *		   embedded systems.  For something to dump in a log, try the function
 *		   midcol_to_string instead.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/14/12  E. Birrane     Initial implementation,
 *****************************************************************************/

char *midcol_pretty_print(Lyst mc)
{
	LystElt elt;
	char *result = NULL;
	char *cursor = NULL;
	int num_items = 0;
	int i = 0;

	char **mid_strs = NULL;
	int tot_size = 0;

	DTNMP_DEBUG_ENTRY("midcol_pretty_print","(%#llx)", (unsigned long) mc);

	/* Step 0: Sanity Check. */
	if(mc == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_pretty_print","Bad Args.", NULL);
		DTNMP_DEBUG_EXIT("midcol_pretty_print","->NULL", NULL);
		return NULL;
	}

	/* Step 1: Grab the number of MIDs in the collection, and pre-allocate
	 *         space to store their printed information.
	 */
	num_items = (int) lyst_length(mc);

	mid_strs = (char**) MTAKE(num_items * sizeof(char*));
	if(mid_strs == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_pretty_print","Can't alloc %d bytes.",
				        num_items * sizeof(char *));
		DTNMP_DEBUG_EXIT("midcol_pretty_print","->NULL",NULL);
		return NULL;
	}

	/* Step 2: Grab the pretty-print of individual MIDs. We need this anyway
	 *         and it will help us get the sizing right.
	 */
	for(elt = lyst_first(mc); (elt && (i < num_items)); elt=lyst_next(elt))
	{
		mid_t *cur_mid = (mid_t*) lyst_data(elt);
		mid_strs[i] = mid_pretty_print(cur_mid);
		tot_size += strlen(mid_strs[i]);
		i++;
	}

	/* Step 3: Calculate size of the MID collection print and allocate it. */
	tot_size += 28 +       /* HEADER and ---'s */
			    28 +       /* Trailer ---'s */
			    num_items; /* newline per MID. */

	if((result = (char *) MTAKE(tot_size)) == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_pretty_print","Can't alloc %d bytes.",
				        tot_size);
		for(i = 0; i < num_items; i++)
		{
			MRELEASE(mid_strs[i]);
		}
		MRELEASE(mid_strs);

		DTNMP_DEBUG_EXIT("midcol_pretty_print","->NULL",NULL);
		return NULL;
	}
	else
	{
		cursor = result;
	}

	/* Step 4: Fill in the MID collection string. */
	cursor += sprintf(cursor,"MID COLLECTION:\n-------------\n");
	for(i = 0; i < num_items; i++)
	{
		cursor += sprintf(cursor,"%s\n",mid_strs[i]);
		MRELEASE(mid_strs[i]);
	}
	cursor += sprintf(cursor,"--------------\n");
	MRELEASE(mid_strs);

	/* Step 5: Sanity check. */
	if((cursor - result) > tot_size)
	{
		DTNMP_DEBUG_ERR("midcol_pretty_print", "OVERWROTE! Alloc %d, wrote %llu.",
				tot_size, (cursor-result));
		MRELEASE(result);
		DTNMP_DEBUG_EXIT("mid_pretty_print","->NULL",NULL);
		return NULL;
	}

	DTNMP_DEBUG_INFO("midcol_pretty_print","Wrote %llu into %d string.",
					(cursor -result), tot_size);

	DTNMP_DEBUG_EXIT("midcol_pretty_print","->%#llx",result);

	return result;
}



/******************************************************************************
 *
 * \par Function Name: midcol_to_string
 *
 * \par Purpose: Create a string representation of a MID collection.
 *
 * \retval  NULL - Failure
 *         !NULL - The string representation of the MID collection.
 *
 * \param[in] mc The collection whose string representation is being calculated.
 *
 * \par Notes:
 *		1. The string is dynamically allocated from the memory pool and must
 *		   be deallocated when no longer needed.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/23/12  E. Birrane     Initial implementation,
 *****************************************************************************/

char *midcol_to_string(Lyst mc)
{
	LystElt elt;
	char *result = NULL;
	char *cursor = NULL;
	int num_items = 0;
	int i = 0;

	char **mid_strs = NULL;
	int tot_size = 0;

	DTNMP_DEBUG_ENTRY("midcol_to_string","(%#llx)", (unsigned long) mc);

	/* Step 0: Sanity Check. */
	if(mc == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_to_string","Bad Args.", NULL);
		DTNMP_DEBUG_EXIT("midcol_to_string","->NULL", NULL);
		return NULL;
	}

	/* Step 1: Grab the number of MIDs in the collection, and pre-allocate
	 *         space to store their printed information.
	 */
	num_items = (int) lyst_length(mc);

	mid_strs = (char**) MTAKE(num_items * sizeof(char*));
	if(mid_strs == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_to_string","Can't alloc %d bytes.",
				        num_items * sizeof(char *));
		DTNMP_DEBUG_EXIT("midcol_to_string","->NULL",NULL);
		return NULL;
	}

	/* Step 2: Grab the pretty-print of individual MIDs. We need this anyway
	 *         and it will help us get the sizing right.
	 */
	for(elt = lyst_first(mc); (elt && (i < num_items)); elt=lyst_next(elt))
	{
		mid_t *cur_mid = (mid_t*) lyst_data(elt);
		mid_strs[i] = mid_to_string(cur_mid);
		tot_size += strlen(mid_strs[i]);
		i++;
	}

	/* Step 3: Calculate size of the MID collection print and allocate it. */
	tot_size += 5 +        /* "MC : " */
				2 +        /* trailer */
			    num_items; /* space between MIDs. */

	if((result = (char *) MTAKE(tot_size)) == NULL)
	{
		DTNMP_DEBUG_ERR("midcol_to_string","Can't alloc %d bytes.",
				        tot_size);
		for(i = 0; i < num_items; i++)
		{
			MRELEASE(mid_strs[i]);
		}
		MRELEASE(mid_strs);

		DTNMP_DEBUG_EXIT("midcol_to_string","->NULL",NULL);
		return NULL;
	}
	else
	{
		cursor = result;
	}

	/* Step 4: Fill in the MID collection string. */
	cursor += sprintf(cursor,"MC : ");
	for(i = 0; i < num_items; i++)
	{
		cursor += sprintf(cursor,"%s ",mid_strs[i]);
		MRELEASE(mid_strs[i]);
	}
	cursor += sprintf(cursor,".\n");
	MRELEASE(mid_strs);

	/* Step 5: Sanity check. */
	if((cursor - result) > tot_size)
	{
		DTNMP_DEBUG_ERR("midcol_to_string", "OVERWROTE! Alloc %d, wrote %llu.",
				tot_size, (cursor-result));
		MRELEASE(result);
		DTNMP_DEBUG_EXIT("mid_to_string","->NULL",NULL);
		return NULL;
	}

	DTNMP_DEBUG_INFO("midcol_to_string","Wrote %llu into %d string.",
					(cursor -result), tot_size);

	DTNMP_DEBUG_EXIT("midcol_to_string","->%#llx",result);

	return result;
}



/******************************************************************************
 *
 * \par Function Name: midcol_serialize
 *
 * \par Purpose: Serialize a MID collection.
 *
 * \retval NULL - Failure
 *         !NULL - Serialized MC.
 *
 *  \param[in]  mids  The list of MIDs comprising the collection.
 *  \param[out] size  The number of bytes of serialized MC.
 *
 * \par Notes:
 *		1. The serialized collection is allocated on the memory pool and must
 *         be returned when no longer needed.
 *
 * Modification History:
 *  MM/DD/YY  AUTHOR         DESCRIPTION
 *  --------  ------------   ---------------------------------------------
 *  11/23/12  E. Birrane     Initial implementation,
 *****************************************************************************/

uint8_t *midcol_serialize(Lyst mids, uint32_t *size)
{
	uint8_t *result = NULL;
	Sdnv num_sdnv;
	LystElt elt;

	DTNMP_DEBUG_ENTRY("midcol_serialize","(%#llx, %#llx)",
			          (unsigned long) mids, (unsigned long) size);

	/* Step 0: Sanity Check */
	if((mids == NULL) || (size == NULL))
	{
		DTNMP_DEBUG_ERR("midcol_serialize","Bad args.", NULL);
		DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL);
		return NULL;
	}

	/* Step 1: Calculate the size. */

	/* Consider the size of the SDNV holding # MIDS.*/
	encodeSdnv(&num_sdnv, lyst_length(mids));

	*size = num_sdnv.length;

	/* Walk through each MID, make sure it is serialized, and look at size. */
    for(elt = lyst_first(mids); elt; elt = lyst_next(elt))
    {
    	mid_t *cur_mid = (mid_t *) lyst_data(elt);

    	if(cur_mid != NULL)
    	{
    		if((cur_mid->raw == NULL) || (cur_mid->raw_size == 0))
    		{
    			/* \todo check return code. */
    			mid_internal_serialize(cur_mid);
    		}
    		if((cur_mid->raw == NULL) || (cur_mid->raw_size == 0))
    		{
        		DTNMP_DEBUG_WARN("midcol_serialize","MID didn't serialize.", NULL);
    		}
    		else
    		{
    			*size += cur_mid->raw_size;
    		}
    	}
    	else
    	{
    		DTNMP_DEBUG_WARN("midcol_serialize","Found NULL MID?", NULL);
    	}
    }

    /* Step 3: Allocate the space for the serialized list. */
    if((result = (uint8_t*) MTAKE(*size)) == NULL)
    {
		DTNMP_DEBUG_ERR("midcol_serialize","Can't alloc %d bytes", *size);
		*size = 0;

		DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL);
		return NULL;
    }

    /* Step 4: Walk through list again copying as we go. */
    uint8_t *cursor = result;

    /* COpy over the number of MIDs in the collection. */
    memcpy(cursor, num_sdnv.text, num_sdnv.length);
    cursor += num_sdnv.length;

    for(elt = lyst_first(mids); elt; elt = lyst_next(elt))
    {
    	mid_t *cur_mid = (mid_t *) lyst_data(elt);

    	if(cur_mid != NULL)
    	{
    		if((cur_mid->raw != NULL) && (cur_mid->raw_size > 0))
    		{
    			memcpy(cursor,cur_mid->raw, cur_mid->raw_size);
    			cursor += cur_mid->raw_size;
    		}
    	}
    	else
    	{
    		DTNMP_DEBUG_WARN("midcol_serialize","Found NULL MID?", NULL);
    	}
    }

    /* Step 5: Final sanity check. */
    if((cursor - result) != *size)
    {
		DTNMP_DEBUG_ERR("midcol_serialize","Wrote %d bytes not %d bytes",
				        (cursor - result), *size);
		*size = 0;
		MRELEASE(result);
		DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL);
		return NULL;
    }

	DTNMP_DEBUG_EXIT("midcol_serialize","->%#llx",(unsigned long) result);
	return result;
}