aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/ccnx_TlvDictionary.c
blob: 3c768529d350c478093c1c0b73d3fd3ddfb66619 (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
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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *
 */

#include <config.h>
#include <stdlib.h>
#include <sys/time.h>
#include <inttypes.h>
#include <stdio.h>

#include <ccnx/common/ccnx_Name.h>

#include <ccnx/common/internal/ccnx_TlvDictionary.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_TlvDictionary.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_DisplayIndented.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Object.h>
#include <parc/algol/parc_JSON.h>

#define DEBUG_ALLOCS 0

struct ccnx_tlv_dictionary_entry;
typedef struct ccnx_tlv_list_entry _CCNxTlvDictionaryListEntry;

typedef enum {
    CCNxTlvDictionaryType_Unknown,
    CCNxTlvDictionaryType_Interest,
    CCNxTlvDictionaryType_ContentObject,
    CCNxTlvDictionaryType_Control,
    CCNxTlvDictionaryType_InterestReturn,
    CCNxTlvDictionaryType_Manifest
} _CCNxTlvDictionaryType;

// These form a singly linked list
struct ccnx_tlv_list_entry {
    _CCNxTlvDictionaryListEntry *next;
    PARCBuffer *buffer;
    uint16_t key;
};

#define ENTRY_UNSET   ((int) 0)
#define ENTRY_BUFFER  ((int) 1)
#define ENTRY_NAME    ((int) 2)
#define ENTRY_INTEGER ((int) 3)
#define ENTRY_IOVEC   ((int) 4)
#define ENTRY_JSON    ((int) 5)
#define ENTRY_OBJECT  ((int) 6)

static struct dictionary_type_string {
    _CCNxTlvDictionaryType type;
    const char *string;
} ccnxTlvDictionaryTypeStrings [] = {
    { .type = CCNxTlvDictionaryType_Unknown,        .string = "Invalid"        },
    { .type = CCNxTlvDictionaryType_Interest,       .string = "Interest"       },
    { .type = CCNxTlvDictionaryType_ContentObject,  .string = "Content Object" },
    { .type = CCNxTlvDictionaryType_Control,        .string = "Control"        },
    { .type = CCNxTlvDictionaryType_InterestReturn, .string = "InterestReturn" },
    { .type = CCNxTlvDictionaryType_Manifest,       .string = "Manifest"       },
    { .type = UINT32_MAX,                           .string = NULL             },
};

static struct dictionary_entry_type_string {
    int type;
    const char *string;
} ccnxTlvDictionaryEntryTypeStrings [] = {
    { .type = ENTRY_UNSET,   .string = "Unset"   },
    { .type = ENTRY_BUFFER,  .string = "Buffer"  },
    { .type = ENTRY_NAME,    .string = "Name"    },
    { .type = ENTRY_INTEGER, .string = "Integer" },
    { .type = ENTRY_IOVEC,   .string = "IoVec"   },
    { .type = ENTRY_JSON,    .string = "JSON"    },
    { .type = ENTRY_OBJECT,  .string = "Object"  },
    { .type = UINT32_MAX,    .string = NULL      },
};

static const char *ccnxTlvDictionaryTypeUnknown = "Unknown";

static const char *
_ccnxTlvDictionaryEntryTypeToString(int entryType)
{
    for (int i = 0; ccnxTlvDictionaryEntryTypeStrings[i].string != NULL; i++) {
        if (ccnxTlvDictionaryEntryTypeStrings[i].type == entryType) {
            return ccnxTlvDictionaryEntryTypeStrings[i].string;
        }
    }
    return ccnxTlvDictionaryTypeUnknown;
}

static const char *
_ccnxTlvDictionaryTypeToString(_CCNxTlvDictionaryType dictionaryType)
{
    for (int i = 0; ccnxTlvDictionaryTypeStrings[i].string != NULL; i++) {
        if (ccnxTlvDictionaryTypeStrings[i].type == dictionaryType) {
            return ccnxTlvDictionaryTypeStrings[i].string;
        }
    }
    return ccnxTlvDictionaryTypeUnknown;
}


typedef struct ccnx_tlv_dictionary_entry {
    int entryType;
    union u_entry {
        PARCBuffer *buffer;
        uint64_t integer;
        CCNxName   *name;
        CCNxCodecNetworkBufferIoVec *vec;
        PARCJSON   *json;
        PARCObject *object;
    } _entry;
} _CCNxTlvDictionaryEntry;

struct ccnx_tlv_dictionary {
#define FIXED_LIST_LENGTH 8
    // These are linked lists where we put unknown TLV types.  This one static allocation should
    // be enough for all the current packet formats.
    _CCNxTlvDictionaryListEntry *fixedListHeads[FIXED_LIST_LENGTH];

    // if we need to allocate beyond FIXED_LIST_LENGTH, put them here
    _CCNxTlvDictionaryListEntry **extraListHeads;

    size_t fastArraySize;
    size_t listSize;

    _CCNxTlvDictionaryType dictionaryType;
    CCNxTlvDictionary_SchemaVersion schemaVersion;

    // Detects changes in the dictionary that were not caused by us
    uint32_t generation;

    struct timeval creationTime;

    void (*infoFreeFunction)(void **infoPtr);
    void *info;

    // A pointer to the implementation functions for the type contained by this dictionary.
    // It's a runtime static, and is not encoded. Thus, when a dictionary is received over
    // the wire, it will need to be initialized based on the dictionaryType and schemaVersion.
    CCNxMessageInterface *messageInterface;

    // will be allocated as part of the ccnx_tlv_dictionary
    _CCNxTlvDictionaryEntry directArray[CCNxCodecSchemaV1TlvDictionary_MessageFastArray_END];
};

static _CCNxTlvDictionaryListEntry *
_ccnxTlvDictionaryListEntry_Create(uint32_t key, const PARCBuffer *buffer)
{
    _CCNxTlvDictionaryListEntry *entry = parcMemory_AllocateAndClear(sizeof(_CCNxTlvDictionaryListEntry));
    assertNotNull(entry, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(_CCNxTlvDictionaryListEntry));
    entry->key = key;
    entry->buffer = parcBuffer_Acquire(buffer);

    return entry;
}

static void
_ccnxTlvDictionaryListEntry_Release(_CCNxTlvDictionaryListEntry **entryPtr)
{
    _CCNxTlvDictionaryListEntry *entry = *entryPtr;
    parcBuffer_Release(&entry->buffer);
    parcMemory_Deallocate((void **) &entry);
    *entryPtr = NULL;
}

static void
_ccnxTlvDictionaryEntry_ListRelease(_CCNxTlvDictionaryListEntry **listHeadPtr)
{
    _CCNxTlvDictionaryListEntry *listHead = *listHeadPtr;
    while (listHead) {
        _CCNxTlvDictionaryListEntry *next = listHead->next;
        _ccnxTlvDictionaryListEntry_Release(&listHead);
        listHead = next;
    }
    *listHeadPtr = NULL;
}

static void
_ccnxTlvDictionary_FinalRelease(CCNxTlvDictionary **dictionaryPtr)
{
    CCNxTlvDictionary *dictionary = *dictionaryPtr;

    // release any entries stored in the fast array
    for (int i = 0; i < dictionary->fastArraySize; i++) {
        switch (dictionary->directArray[i].entryType) {
            case ENTRY_BUFFER:
                parcBuffer_Release(&dictionary->directArray[i]._entry.buffer);
                break;
            case ENTRY_NAME:
                ccnxName_Release(&dictionary->directArray[i]._entry.name);
                break;
            case ENTRY_IOVEC:
                ccnxCodecNetworkBufferIoVec_Release(&dictionary->directArray[i]._entry.vec);
                break;
            case ENTRY_JSON:
                parcJSON_Release(&dictionary->directArray[i]._entry.json);
                break;
            case ENTRY_OBJECT:
                parcObject_Release(&dictionary->directArray[i]._entry.object);
                break;
            default:
                // other types are direct storage
                break;
        }
    }

    for (int i = 0; i < FIXED_LIST_LENGTH; i++) {
        if (dictionary->fixedListHeads[i]) {
            _ccnxTlvDictionaryEntry_ListRelease(&dictionary->fixedListHeads[i]);
        }
    }

    if (dictionary->extraListHeads) {
        for (int i = FIXED_LIST_LENGTH; i < dictionary->listSize; i++) {
            if (dictionary->extraListHeads[i - FIXED_LIST_LENGTH]) {
                _ccnxTlvDictionaryEntry_ListRelease(&dictionary->extraListHeads[i - FIXED_LIST_LENGTH]);
            }
        }
        parcMemory_Deallocate((void **) &(dictionary->extraListHeads));
    }

    if (dictionary->infoFreeFunction) {
        dictionary->infoFreeFunction(&dictionary->info);
    }

#if DEBUG_ALLOCS
    printf("finalize dictionary %p (final)\n", dictionary);
#endif
}

parcObject_ExtendPARCObject(CCNxTlvDictionary, _ccnxTlvDictionary_FinalRelease,
                            NULL, NULL, ccnxTlvDictionary_Equals, NULL, NULL, NULL);

parcObject_ImplementAcquire(ccnxTlvDictionary, CCNxTlvDictionary);

parcObject_ImplementRelease(ccnxTlvDictionary, CCNxTlvDictionary);

static void
_ccnxTlvDictionary_GetTimeOfDay(struct timeval *outputTime)
{
#ifdef DEBUG
    // if in debug mode, time messages
    gettimeofday(outputTime, NULL);
#else
    *outputTime = (struct timeval) { 0, 0 };
#endif
}


CCNxTlvDictionary *
ccnxTlvDictionary_Create(size_t bufferCount, size_t listCount)
{
    CCNxTlvDictionary *dictionary = (CCNxTlvDictionary *) parcObject_CreateAndClearInstance(CCNxTlvDictionary);

    if (dictionary != NULL) {
        _ccnxTlvDictionary_GetTimeOfDay(&dictionary->creationTime);

        dictionary->dictionaryType = CCNxTlvDictionaryType_Unknown;
        dictionary->fastArraySize = bufferCount;
        dictionary->listSize = listCount;

        dictionary->infoFreeFunction = NULL;
        dictionary->info = NULL;

        dictionary->extraListHeads = NULL;
        // dictionary->directArray is allocated as part of parcObject
    }

#if DEBUG_ALLOCS
    printf("allocate dictionary %p\n", dictionary);
#endif

    return dictionary;
}

CCNxTlvDictionary *
ccnxTlvDictionary_ShallowCopy(const CCNxTlvDictionary *source)
{
    size_t bufferCount = source->fastArraySize;
    size_t listCount = source->listSize;
    CCNxTlvDictionary  *newDictionary = ccnxTlvDictionary_Create(bufferCount, listCount);

    if (newDictionary != NULL) {
        newDictionary->dictionaryType = source->dictionaryType;
        newDictionary->schemaVersion = source->schemaVersion;
        newDictionary->generation = source->generation;
        newDictionary->creationTime = source->creationTime;
        newDictionary->messageInterface = source->messageInterface;
        newDictionary->info = source->info;
        newDictionary->infoFreeFunction = source->infoFreeFunction;

        // Update listHeads
        for (uint32_t key = 0; key < source->listSize; ++key) {
            size_t listSize = ccnxTlvDictionary_ListSize(source, key);
            for (size_t i = 0; i < listSize; ++i) {
                PARCBuffer *buffer;
                uint32_t bKey;
                ccnxTlvDictionary_ListGetByPosition(source, key, i, &buffer, &bKey);
                parcBuffer_Acquire(buffer);
                ccnxTlvDictionary_PutListBuffer(newDictionary, key, bKey, buffer);
                parcBuffer_Release(&buffer);
            }
        }

        // Update directArray
        for (uint32_t key = 0; key < source->fastArraySize; ++key) {
            switch (source->directArray[key].entryType) {
                case ENTRY_BUFFER:
                    ccnxTlvDictionary_PutBuffer(newDictionary, key, ccnxTlvDictionary_GetBuffer(source, key));
                    break;
                case ENTRY_NAME:
                    ccnxTlvDictionary_PutName(newDictionary, key, ccnxTlvDictionary_GetName(source, key));
                    break;
                case ENTRY_IOVEC:
                    ccnxTlvDictionary_PutIoVec(newDictionary, key, ccnxTlvDictionary_GetIoVec(source, key));
                    break;
                case ENTRY_JSON:
                    ccnxTlvDictionary_PutJson(newDictionary, key, ccnxTlvDictionary_GetJson(source, key));
                    break;
                case ENTRY_INTEGER:
                    ccnxTlvDictionary_PutInteger(newDictionary, key, ccnxTlvDictionary_GetInteger(source, key));
                    break;
                case ENTRY_OBJECT:
                    ccnxTlvDictionary_PutObject(newDictionary, key, ccnxTlvDictionary_GetObject(source, key));
                    break;
                default:
                    break;
            }
        }
    }

    return newDictionary;
}

bool
ccnxTlvDictionary_PutBuffer(CCNxTlvDictionary *dictionary, uint32_t key, const PARCBuffer *buffer)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(buffer, "Parameter buffer must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET) {
        dictionary->directArray[key].entryType = ENTRY_BUFFER;
        dictionary->directArray[key]._entry.buffer = parcBuffer_Acquire(buffer);
        return true;
    }
    return false;
}

bool
ccnxTlvDictionary_PutObject(CCNxTlvDictionary *dictionary, uint32_t key, const PARCObject *object)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(object, "Parameter object must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key %ud must be less than %zu", key, dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET) {
        dictionary->directArray[key].entryType = ENTRY_OBJECT;
        dictionary->directArray[key]._entry.object = parcObject_Acquire(object);
        return true;
    }
    return false;
}

bool
ccnxTlvDictionary_PutName(CCNxTlvDictionary *dictionary, uint32_t key, const CCNxName *name)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(name, "Parameter buffer must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET) {
        dictionary->directArray[key].entryType = ENTRY_NAME;
        dictionary->directArray[key]._entry.name = ccnxName_Acquire(name);
        return true;
    }
    return false;
}

bool
ccnxTlvDictionary_PutInteger(CCNxTlvDictionary *dictionary, uint32_t key, const uint64_t value)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET || dictionary->directArray[key].entryType == ENTRY_INTEGER) {
        dictionary->directArray[key].entryType = ENTRY_INTEGER;
        dictionary->directArray[key]._entry.integer = value;
        return true;
    }
    return false;
}

bool
ccnxTlvDictionary_PutIoVec(CCNxTlvDictionary *dictionary, uint32_t key, const CCNxCodecNetworkBufferIoVec *vec)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(vec, "Parameter buffer must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET) {
        dictionary->directArray[key].entryType = ENTRY_IOVEC;
        dictionary->directArray[key]._entry.vec = ccnxCodecNetworkBufferIoVec_Acquire((CCNxCodecNetworkBufferIoVec *) vec);
        return true;
    }
    return false;
}

bool
ccnxTlvDictionary_PutJson(CCNxTlvDictionary *dictionary, uint32_t key, const PARCJSON *json)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(json, "Parameter json must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_UNSET) {
        dictionary->directArray[key].entryType = ENTRY_JSON;
        dictionary->directArray[key]._entry.json = parcJSON_Acquire(json);
        return true;
    }
    return false;
}

CCNxCodecNetworkBufferIoVec *
ccnxTlvDictionary_GetIoVec(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_IOVEC) {
        return dictionary->directArray[key]._entry.vec;
    }
    return NULL;
}

// If you need to change the list head, use this
static _CCNxTlvDictionaryListEntry **
_getListHeadReference(CCNxTlvDictionary *dictionary, uint32_t listKey)
{
    if (listKey < FIXED_LIST_LENGTH) {
        return &dictionary->fixedListHeads[listKey];
    } else {
        if (dictionary->extraListHeads == NULL) {
            dictionary->extraListHeads = parcMemory_AllocateAndClear(sizeof(_CCNxTlvDictionaryListEntry *) * (dictionary->listSize - FIXED_LIST_LENGTH));
        }

        return &dictionary->extraListHeads[listKey - FIXED_LIST_LENGTH];
    }
}

// If not going to modify the list, use this
static _CCNxTlvDictionaryListEntry *
_getListHead(const CCNxTlvDictionary *dictionary, uint32_t listKey)
{
    _CCNxTlvDictionaryListEntry **head = _getListHeadReference((CCNxTlvDictionary *) dictionary, listKey);
    return *head;
}

bool
ccnxTlvDictionary_PutListBuffer(CCNxTlvDictionary *dictionary, uint32_t listKey, uint32_t key, const PARCBuffer *buffer)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(buffer, "Parameter buffer must be non-null");
    assertTrue(listKey < dictionary->listSize, "Parameter key must be less than %zu", dictionary->listSize);

    _CCNxTlvDictionaryListEntry *entry = _ccnxTlvDictionaryListEntry_Create(key, buffer);

    _CCNxTlvDictionaryListEntry **head = _getListHeadReference(dictionary, listKey);
    if (*head) {
        // insert new value at list head
        entry->next = *head;
        *head = entry;
    } else {
        // new value is the list head
        *head = entry;
    }
    return true;
}

bool
ccnxTlvDictionary_IsValueBuffer(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_BUFFER);
}

bool
ccnxTlvDictionary_IsValueObject(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_OBJECT);
}

bool
ccnxTlvDictionary_IsValueInteger(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_INTEGER);
}

bool
ccnxTlvDictionary_IsValueName(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_NAME);
}

bool
ccnxTlvDictionary_IsValueIoVec(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_IOVEC);
}

bool
ccnxTlvDictionary_IsValueJson(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);
    return (dictionary->directArray[key].entryType == ENTRY_JSON);
}

PARCBuffer *
ccnxTlvDictionary_GetBuffer(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    // For now return NULL for backward compatability with prior code, case 1011
    if (dictionary->directArray[key].entryType == ENTRY_BUFFER) {
        return dictionary->directArray[key]._entry.buffer;
    }
    return NULL;
}

CCNxName *
ccnxTlvDictionary_GetName(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_NAME) {
        return dictionary->directArray[key]._entry.name;
    }
    return NULL;
}

uint64_t
ccnxTlvDictionary_GetInteger(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    trapIllegalValueIf(dictionary->directArray[key].entryType != ENTRY_INTEGER,
                       "Key %u is of type %d",
                       key, dictionary->directArray[key].entryType)
    {
        ccnxTlvDictionary_Display(dictionary, 3);
    }

    return dictionary->directArray[key]._entry.integer;
}


PARCJSON *
ccnxTlvDictionary_GetJson(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_JSON) {
        return dictionary->directArray[key]._entry.json;
    }
    return NULL;
}

PARCObject *
ccnxTlvDictionary_GetObject(const CCNxTlvDictionary *dictionary, uint32_t key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(key < dictionary->fastArraySize, "Parameter key must be less than %zu", dictionary->fastArraySize);

    if (dictionary->directArray[key].entryType == ENTRY_OBJECT) {
        return dictionary->directArray[key]._entry.object;
    }

    return NULL;
}

bool
ccnxTlvDictionary_ListGetByPosition(const CCNxTlvDictionary *dictionary, uint32_t listKey, size_t listPosition, PARCBuffer **bufferPtr, uint32_t *keyPtr)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertNotNull(bufferPtr, "Parameter bufferPtr must be non-null");
    assertNotNull(keyPtr, "Parameter keyPtr must be non-null");
    assertTrue(listKey < dictionary->listSize, "Parameter key must be less than %zu", dictionary->listSize);

    _CCNxTlvDictionaryListEntry *entry = _getListHead(dictionary, listKey);
    while (entry) {
        if (listPosition == 0) {
            *bufferPtr = entry->buffer;
            *keyPtr = entry->key;
            return true;
        }
        entry = entry->next;
        --listPosition;
    }

    return false;
}


PARCBuffer *
ccnxTlvDictionary_ListGetByType(const CCNxTlvDictionary *dictionary, uint32_t listKey, uint32_t type)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(listKey < dictionary->listSize, "Parameter key must be less than %zu", dictionary->listSize);

    PARCBuffer *buffer = NULL;
    _CCNxTlvDictionaryListEntry *entry = _getListHead(dictionary, listKey);
    while (entry) {
        if (entry->key == type) {
            buffer = entry->buffer;
            break;
        }
        entry = entry->next;
    }

    return buffer;
}


size_t
ccnxTlvDictionary_ListSize(const CCNxTlvDictionary *dictionary, uint32_t listKey)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-null");
    assertTrue(listKey < dictionary->listSize, "Parameter key must be less than %zu", dictionary->listSize);

    size_t size = 0;
    _CCNxTlvDictionaryListEntry *entry = _getListHead(dictionary, listKey);
    while (entry) {
        size++;
        entry = entry->next;
    }

    return size;
}

void
ccnxTlvDictionary_SetMessageType_Interest(CCNxTlvDictionary *dictionary, CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    dictionary->dictionaryType = CCNxTlvDictionaryType_Interest;
    dictionary->schemaVersion = schemaVersion;
}

void
ccnxTlvDictionary_SetMessageType_ContentObject(CCNxTlvDictionary *dictionary, CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    dictionary->dictionaryType = CCNxTlvDictionaryType_ContentObject;
    dictionary->schemaVersion = schemaVersion;
}

void
ccnxTlvDictionary_SetMessageType_Control(CCNxTlvDictionary *dictionary, CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    dictionary->dictionaryType = CCNxTlvDictionaryType_Control;
    dictionary->schemaVersion = schemaVersion;
}

void
ccnxTlvDictionary_SetMessageType_InterestReturn(CCNxTlvDictionary *dictionary, CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    dictionary->dictionaryType = CCNxTlvDictionaryType_InterestReturn;
    dictionary->schemaVersion = schemaVersion;
}

void
ccnxTlvDictionary_SetMessageType_Manifest(CCNxTlvDictionary *dictionary, CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    dictionary->dictionaryType = CCNxTlvDictionaryType_Manifest;
    dictionary->schemaVersion = schemaVersion;
}

bool
ccnxTlvDictionary_IsInterest(const CCNxTlvDictionary *dictionary)
{
    return (dictionary->dictionaryType == CCNxTlvDictionaryType_Interest);
}

bool
ccnxTlvDictionary_IsInterestReturn(const CCNxTlvDictionary *dictionary)
{
    return (dictionary->dictionaryType == CCNxTlvDictionaryType_InterestReturn);
}

bool
ccnxTlvDictionary_IsContentObject(const CCNxTlvDictionary *dictionary)
{
    return (dictionary->dictionaryType == CCNxTlvDictionaryType_ContentObject);
}

bool
ccnxTlvDictionary_IsControl(const CCNxTlvDictionary *dictionary)
{
    return (dictionary->dictionaryType == CCNxTlvDictionaryType_Control);
}

bool
ccnxTlvDictionary_IsManifest(const CCNxTlvDictionary *dictionary)
{
    return (dictionary->dictionaryType == CCNxTlvDictionaryType_Manifest);
}

CCNxTlvDictionary_SchemaVersion
ccnxTlvDictionary_GetSchemaVersion(const CCNxTlvDictionary *dictionary)
{
    return dictionary->schemaVersion;
}

void
ccnxTlvDictionary_SetMessageInterface(CCNxTlvDictionary *dictionary, const CCNxMessageInterface *implementation)
{
    dictionary->messageInterface = (CCNxMessageInterface *) implementation;
}

CCNxMessageInterface *
ccnxTlvDictionary_GetMessageInterface(const CCNxTlvDictionary *dictionary)
{
    return dictionary->messageInterface;
}

struct timeval
ccnxTlvDictionary_GetLifetime(const CCNxTlvDictionary *dictionary)
{
    struct timeval now;
    _ccnxTlvDictionary_GetTimeOfDay(&now);
    timersub(&now, &dictionary->creationTime, &now);
    return now;
}

static void
_ccnxTlvDictionary_DisplayBuffer(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s pointer %p\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), (void *) entry->_entry.buffer);
    parcBuffer_Display(entry->_entry.buffer, 6);
}

static void
_ccnxTlvDictionary_DisplayInteger(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s value 0x%" PRIX64 " (%" PRIu64 ")\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), entry->_entry.integer, entry->_entry.integer);
}

static void
_ccnxTlvDictionary_DisplayIoVec(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s pointer %p\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), (void *) entry->_entry.vec);
    ccnxCodecNetworkBufferIoVec_Display(entry->_entry.vec, 6);
}

static void
_ccnxTlvDictionary_DisplayJson(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s pointer %p\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), (void *) entry->_entry.json);
    char *string = parcJSON_ToString(entry->_entry.json);
    printf("%s\n", string);
    parcMemory_Deallocate((void **) &string);
}

static void
_ccnxTlvDictionary_DisplayName(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s pointer %p\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), (void *) entry->_entry.name);
    ccnxName_Display(entry->_entry.name, 6);
}

static void
_ccnxTlvDictionary_DisplayUnknown(const _CCNxTlvDictionaryEntry *entry, int index)
{
    printf("     Entry %3d type %8s pointer %p\n", index, _ccnxTlvDictionaryEntryTypeToString(entry->entryType), (void *) entry->_entry.buffer);
}

static void
_ccnxTlvDictionary_DisplayListEntry(const _CCNxTlvDictionaryListEntry *entry, int listIndex, int position)
{
    printf("     List %3d Position %3d key 0x%04X pointer %p\n", listIndex, position, entry->key, (void *) entry->buffer);
    parcBuffer_Display(entry->buffer, 6);
}

void
ccnxTlvDictionary_Display(const CCNxTlvDictionary *dictionary, int indent)
{
    parcDisplayIndented_PrintLine(indent, "CCNxTlvDictionary@%p fastArraySize %zu listSize %zu dictionaryType %s schemaVersion %d refcount %" PRIu64 "\n",
                                  (void *) dictionary,
                                  dictionary->fastArraySize,
                                  dictionary->listSize,
                                  _ccnxTlvDictionaryTypeToString(dictionary->dictionaryType),
                                  dictionary->schemaVersion,
                                  parcObject_GetReferenceCount((PARCObject *) dictionary));

    parcDisplayIndented_PrintLine(indent, "    createTime %0.6f generation %u Info %p InfoFreeFunc %p\n",
                                  (dictionary->creationTime.tv_sec + dictionary->creationTime.tv_usec * 1E-6),
                                  dictionary->generation,
                                  (void *) dictionary->info,
                                  dictionary->infoFreeFunction);

    for (int i = 0; i < dictionary->fastArraySize; i++) {
        if (dictionary->directArray[i].entryType != ENTRY_UNSET) {
            switch (dictionary->directArray[i].entryType) {
                case ENTRY_BUFFER:
                    _ccnxTlvDictionary_DisplayBuffer(&dictionary->directArray[i], i);
                    break;

                case ENTRY_INTEGER:
                    _ccnxTlvDictionary_DisplayInteger(&dictionary->directArray[i], i);
                    break;

                case ENTRY_IOVEC:
                    _ccnxTlvDictionary_DisplayIoVec(&dictionary->directArray[i], i);
                    break;

                case ENTRY_JSON:
                    _ccnxTlvDictionary_DisplayJson(&dictionary->directArray[i], i);
                    break;

                case ENTRY_NAME:
                    _ccnxTlvDictionary_DisplayName(&dictionary->directArray[i], i);
                    break;

                default:
                    _ccnxTlvDictionary_DisplayUnknown(&dictionary->directArray[i], i);
            }
        }
    }

    for (int i = 0; i < dictionary->listSize; i++) {
        _CCNxTlvDictionaryListEntry *entry = _getListHead(dictionary, i);
        if (entry) {
            int position = 0;
            printf("   Displaying custom entry list index %3d head %p\n", i, (void *) entry);
            while (entry) {
                _ccnxTlvDictionary_DisplayListEntry(entry, i, position);
                entry = entry->next;
            }
        }
    }
}

static bool
_ccnxTlvDictionaryEntry_Equals(const _CCNxTlvDictionaryEntry *a, const _CCNxTlvDictionaryEntry *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }

    if (a == NULL || b == NULL) {
        return false;
    }

    bool equals = false;
    if (a->entryType == b->entryType) {
        switch (a->entryType) {
            case ENTRY_UNSET:
                equals = true;
                break;

            case ENTRY_BUFFER:
                equals = parcBuffer_Equals(a->_entry.buffer, b->_entry.buffer);
                break;

            case ENTRY_OBJECT:
                equals = parcObject_Equals(a->_entry.object, b->_entry.object);
                break;

            case ENTRY_INTEGER:
                equals = (a->_entry.integer == b->_entry.integer);
                break;

            case ENTRY_IOVEC:
                equals = ccnxCodecNetworkBufferIoVec_Equals(a->_entry.vec, b->_entry.vec);
                break;

            case ENTRY_JSON:
                equals = parcJSON_Equals(a->_entry.json, b->_entry.json);
                break;

            case ENTRY_NAME:
                equals = ccnxName_Equals(a->_entry.name, b->_entry.name);
                break;

            default:
                trapIllegalValue(a->entryType, "Cannot compare due to unknown entry type: %d", a->entryType);
        }
    }
    return equals;
}

static bool
_ccnxTlvDictionaryListEntry_Equals(const _CCNxTlvDictionaryListEntry *a, const _CCNxTlvDictionaryListEntry *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }

    if (a == NULL || b == NULL) {
        return false;
    }

    if (a->key == b->key) {
        if (parcBuffer_Equals(a->buffer, b->buffer)) {
            return true;
        }
    }
    return false;
}

static bool
_ccnxTlvDictionary_ListEquals(const _CCNxTlvDictionaryListEntry *listHeadA, const _CCNxTlvDictionaryListEntry *listHeadB)
{
    if (listHeadA == NULL && listHeadB == NULL) {
        return true;
    }

    if (listHeadA == NULL || listHeadB == NULL) {
        return false;
    }

    // walk both linked lists in parallel
    while (listHeadA && listHeadB) {
        if (!_ccnxTlvDictionaryListEntry_Equals(listHeadA, listHeadB)) {
            return false;
        }

        listHeadA = listHeadA->next;
        listHeadB = listHeadB->next;
    }

    // they must both be NULL otherwise the lists did not end at same place
    if (listHeadA == NULL && listHeadB == NULL) {
        return true;
    }
    return false;
}

/*
 * precondition: we know they are not null and they have the same fastarray size
 */
static bool
_ccnxTlvDictionary_FastArrayEquals(const CCNxTlvDictionary *a, const CCNxTlvDictionary *b)
{
    bool equals = true;
    for (int i = 0; i < a->fastArraySize && equals; i++) {
        equals = _ccnxTlvDictionaryEntry_Equals(&a->directArray[i], &b->directArray[i]);
    }
    return equals;
}

/*
 * preconditiona: we know they are not null and they have the same list size
 */
static bool
_ccnxTlvDictionary_ListsEquals(const CCNxTlvDictionary *a, const CCNxTlvDictionary *b)
{
    bool equals = true;
    for (int i = 0; i < a->listSize && equals; i++) {
        _CCNxTlvDictionaryListEntry *entry_a = _getListHead(a, i);
        _CCNxTlvDictionaryListEntry *entry_b = _getListHead(b, i);
        equals = _ccnxTlvDictionary_ListEquals(entry_a, entry_b);
    }
    return equals;
}

bool
ccnxTlvDictionary_Equals(const CCNxTlvDictionary *a, const CCNxTlvDictionary *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }

    if (a == NULL || b == NULL) {
        return false;
    }

    // They are both non-null
    bool equals = false;
    if (a->fastArraySize == b->fastArraySize) {
        if (a->listSize == b->listSize) {
            if (a->dictionaryType == b->dictionaryType) {
                if (a->schemaVersion == b->schemaVersion) {
                    if (_ccnxTlvDictionary_FastArrayEquals(a, b)) {
                        if (_ccnxTlvDictionary_ListsEquals(a, b)) {
                            equals = true;
                        }
                    }
                }
            }
        }
    }
    return equals;
}