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
|
/*
* 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 <LongBow/runtime.h>
#include <stdio.h>
#include <string.h>
#include <ccnx/common/ccnx_Link.h>
#include <parc/algol/parc_Object.h>
struct ccnx_link {
const CCNxName *name;
PARCBuffer *keyId;
PARCBuffer *contentHash;
};
static void
_destroy(CCNxLink **linkP)
{
ccnxName_Release((CCNxName **) &(*linkP)->name);
if ((*linkP)->keyId != NULL) {
parcBuffer_Release(&(*linkP)->keyId);
}
if ((*linkP)->contentHash != NULL) {
parcBuffer_Release(&(*linkP)->contentHash);
}
}
parcObject_ExtendPARCObject(CCNxLink, _destroy, NULL, ccnxLink_ToString, ccnxLink_Equals, NULL, NULL, NULL);
parcObject_ImplementAcquire(ccnxLink, CCNxLink);
parcObject_ImplementRelease(ccnxLink, CCNxLink);
CCNxLink *
ccnxLink_Create(const CCNxName *name, PARCBuffer *keyId, PARCBuffer *contentObjectHash)
{
CCNxLink *link = parcObject_CreateInstance(CCNxLink);
if (link != NULL) {
link->name = ccnxName_Acquire(name);
link->keyId = (keyId == NULL) ? NULL : parcBuffer_Acquire(keyId);
link->contentHash = (contentObjectHash == NULL) ? NULL : parcBuffer_Acquire(contentObjectHash);
}
return link;
}
CCNxLink *
ccnxLink_Copy(const CCNxLink *original)
{
CCNxLink *link = parcObject_CreateInstance(CCNxLink);
if (link != NULL) {
link->name = ccnxName_Copy(original->name);
link->keyId = (original->keyId == NULL) ? NULL : parcBuffer_Copy(original->keyId);
link->contentHash = (original->contentHash == NULL) ? NULL : parcBuffer_Copy(original->contentHash);
}
return link;
}
const CCNxName *
ccnxLink_GetName(const CCNxLink *link)
{
return link->name;
}
PARCBuffer *
ccnxLink_GetKeyID(const CCNxLink *link)
{
return link->keyId;
}
PARCBuffer *
ccnxLink_GetContentObjectHash(const CCNxLink *link)
{
return link->contentHash;
}
bool
ccnxLink_Equals(const CCNxLink *objectA, const CCNxLink *objectB)
{
bool result = false;
if (objectA == objectB) {
result = true;
} else if (objectA != NULL && objectB != NULL) {
if (ccnxName_Equals(objectA->name, objectB->name)) {
if (parcBuffer_Equals(objectA->keyId, objectB->keyId)) {
if (parcBuffer_Equals(objectA->contentHash, objectB->contentHash)) {
return true;
}
}
}
}
return result;
}
char *
ccnxLink_ToString(const CCNxLink *link)
{
char *nameString = ccnxName_ToString(link->name);
char *keyIdString = "NULL";
bool nullKeyIdString = true;
if (link->keyId != NULL) {
keyIdString = parcBuffer_ToString(link->keyId);
nullKeyIdString = false;
}
char *contentObjectHashString = NULL;
bool nullContentObjectHashString = true;
if (link->contentHash != NULL) {
contentObjectHashString = parcBuffer_ToString(link->contentHash);
nullContentObjectHashString = false;
}
char *string;
int failure = asprintf(&string, "CCNxLink { .name=\"%s\", .KeyID=\"%s\", .ContentObjectHash=\"%s\" }",
nameString,
keyIdString,
contentObjectHashString);
assertTrue(failure > -1, "Error asprintf");
parcMemory_Deallocate((void **) &nameString);
if (!nullKeyIdString) {
parcMemory_Deallocate((void **) &keyIdString);
}
if (!nullContentObjectHashString) {
parcMemory_Deallocate((void **) &contentObjectHashString);
}
char *result = parcMemory_StringDuplicate(string, strlen(string));
free(string);
return result;
}
bool
ccnxLink_IsValid(const CCNxLink *link)
{
bool result = false;
if (link != NULL) {
if (ccnxName_IsValid(link->name)) {
if (link->keyId == NULL || parcBuffer_IsValid(link->keyId)) {
if (link->contentHash == NULL || parcBuffer_IsValid(link->contentHash)) {
result = true;
}
}
}
}
return result;
}
void
ccnxLink_AssertValid(const CCNxLink *link)
{
assertTrue(ccnxLink_IsValid(link), "CCNxLink instance is not valid.");
}
|