aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c
blob: e419d18e62f856daff9fe1cd127077d3edb16484 (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
/*
 * 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 <stdio.h>
#include <stdlib.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_JSON.h>

#include <ccnx/common/ccnx_Manifest.h>
#include <ccnx/common/internal/ccnx_ManifestInterface.h>

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

static size_t _ccnxManifestFacadeV1_GetNumberOfHashGroups(const CCNxTlvDictionary *dict);

static CCNxTlvDictionary *
_ccnxManifestFacadeV1_Create(const CCNxName *name)
{
    CCNxTlvDictionary *dictionary = ccnxCodecSchemaV1TlvDictionary_CreateManifest();

    if (dictionary != NULL) {
        if (name != NULL) {
            ccnxTlvDictionary_PutName(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME, name);
        }
    } else {
        trapOutOfMemory("Could not allocate an Manifest");
    }

    return dictionary;
}

static const CCNxName *
_ccnxManifestFacadeV1_GetName(const CCNxTlvDictionary *dict)
{
    return ccnxTlvDictionary_GetName(dict, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME);
}

static void
_ccnxManifestFacadeV1_AddHashGroup(CCNxTlvDictionary *dict, const CCNxManifestHashGroup *group)
{
    PARCJSON *json = ccnxManifestHashGroup_ToJson(group);
    size_t numGroups = _ccnxManifestFacadeV1_GetNumberOfHashGroups(dict);

    char *jsonString = parcJSON_ToString(json);
    PARCBuffer *buffer = parcBuffer_AllocateCString(jsonString);
    parcMemory_Deallocate(&jsonString);
    parcJSON_Release(&json);

    ccnxTlvDictionary_PutListBuffer(dict, CCNxCodecSchemaV1TlvDictionary_Lists_HASH_GROUP_LIST, (uint32_t) numGroups, buffer);
    parcBuffer_Release(&buffer);
}

static CCNxManifestHashGroup *
_ccnxManifestFacadeV1_GetHashGroup(const CCNxTlvDictionary *dict, size_t index)
{
    PARCBuffer *buffer = NULL;
    uint32_t key;
    ccnxTlvDictionary_ListGetByPosition(dict, CCNxCodecSchemaV1TlvDictionary_Lists_HASH_GROUP_LIST, index, &buffer, &key);

    char *jsonString = parcBuffer_ToString(buffer);
    PARCJSON *json = parcJSON_ParseString(jsonString);
    parcMemory_Deallocate(&jsonString);

    CCNxManifestHashGroup *group = ccnxManifestHashGroup_CreateFromJson(json);
    parcJSON_Release(&json);

    return group;
}

static size_t
_ccnxManifestFacadeV1_GetNumberOfHashGroups(const CCNxTlvDictionary *dict)
{
    size_t numHashGroups = ccnxTlvDictionary_ListSize(dict, CCNxCodecSchemaV1TlvDictionary_Lists_HASH_GROUP_LIST);
    return numHashGroups;
}

static bool
_ccnxManifestFacadeV1_Equals(const CCNxTlvDictionary *dictA, const CCNxTlvDictionary *dictB)
{
    if (dictA == dictB) {
        return true;
    }
    if (dictA == NULL || dictB == NULL) {
        return false;
    }

    if (ccnxName_Equals(_ccnxManifestFacadeV1_GetName(dictA), _ccnxManifestFacadeV1_GetName(dictB))) {
        if (_ccnxManifestFacadeV1_GetNumberOfHashGroups(dictA) == _ccnxManifestFacadeV1_GetNumberOfHashGroups(dictB)) {
            for (size_t i = 0; i < _ccnxManifestFacadeV1_GetNumberOfHashGroups(dictA); i++) {
                CCNxManifestHashGroup *groupA = _ccnxManifestFacadeV1_GetHashGroup(dictA, i);
                CCNxManifestHashGroup *groupB = _ccnxManifestFacadeV1_GetHashGroup(dictB, i);

                if (!ccnxManifestHashGroup_Equals(groupA, groupB)) {
                    ccnxManifestHashGroup_Release(&groupA);
                    ccnxManifestHashGroup_Release(&groupB);
                    return false;
                }
            }
            return true;
        }
    }
    return false;
};

CCNxManifestInterface CCNxManifestFacadeV1_Interface = {
    .description           = "CCNxManifestFacadeV1_Implementation",
    .create                = &_ccnxManifestFacadeV1_Create,
    .getName               = &_ccnxManifestFacadeV1_GetName,
    .addHashGroup          = &_ccnxManifestFacadeV1_AddHashGroup,
    .getHashGroup          = &_ccnxManifestFacadeV1_GetHashGroup,
    .getNumberOfHashGroups = &_ccnxManifestFacadeV1_GetNumberOfHashGroups,
    .equals                = &_ccnxManifestFacadeV1_Equals,
};