From d18ae43123fcd7604d1c36a1ec8450dbe6071824 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 20:44:26 +0100 Subject: Initial commit: ccnxlibs. Change-Id: I1b376527a7dd01a6b9e083a6cb646955902f45c0 Signed-off-by: Luca Muscariello --- .../ccnx/common/internal/ccnx_ManifestFacadeV1.c | 132 +++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c (limited to 'libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c') diff --git a/libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c b/libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c new file mode 100644 index 00000000..e419d18e --- /dev/null +++ b/libccnx-common/ccnx/common/internal/ccnx_ManifestFacadeV1.c @@ -0,0 +1,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 +#include +#include + +#include + +#include + +#include +#include + +#include + +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, +}; -- cgit 1.2.3-korg