data.h
1 
21 #ifndef NDN_DATA_H
22 #define NDN_DATA_H
23 
24 #include <math.h>
25 #include <ndn-cpp/c/data-types.h>
26 #include "name.h"
27 #include "key-locator.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
39 static __inline void ndn_Signature_initialize(struct ndn_Signature *self, struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents) {
40  self->type = (ndn_SignatureType)-1;
41  ndn_Blob_initialize(&self->signature, 0, 0);
42  ndn_Blob_initialize(&self->signatureInfoEncoding, 0, 0);
43  self->genericTypeCode = -1;
44  ndn_KeyLocator_initialize(&self->keyLocator, keyNameComponents, maxKeyNameComponents);
45 }
46 
52 static __inline void ndn_Signature_clear(struct ndn_Signature *self)
53 {
54  ndn_Signature_initialize
55  (self, self->keyLocator.keyName.components,
56  self->keyLocator.keyName.maxComponents);
57 }
58 
66 static __inline ndn_Error
67 ndn_Signature_setFromSignature
68  (struct ndn_Signature *self, const struct ndn_Signature *other)
69 {
70  ndn_Error error;
71 
72  if (other == self)
73  // Setting to itself. Do nothing.
74  return NDN_ERROR_success;
75 
76  self->type = other->type;
77  ndn_Blob_setFromBlob(&self->signature, &other->signature);
78  ndn_Blob_setFromBlob(&self->signatureInfoEncoding, &other->signatureInfoEncoding);
79  self->genericTypeCode = other->genericTypeCode;
80  if ((error = ndn_KeyLocator_setFromKeyLocator
81  (&self->keyLocator, &other->keyLocator)))
82  return error;
83 
84  return NDN_ERROR_success;
85 }
86 
91 static __inline void ndn_MetaInfo_initialize(struct ndn_MetaInfo *self)
92 {
93  self->timestampMilliseconds = -1;
94  self->type = ndn_ContentType_BLOB;
95  self->otherTypeCode = -1;
96  self->freshnessPeriod = -1;
97  ndn_NameComponent_initialize(&self->finalBlockId, 0, 0);
98 }
99 
103 static __inline int ndn_MetaInfo_getFreshnessSeconds(const struct ndn_MetaInfo *self)
104 {
105  return self->freshnessPeriod < 0 ? -1 : (int)round(self->freshnessPeriod / 1000.0);
106 }
107 
111 static __inline void ndn_MetaInfo_setFreshnessSeconds(struct ndn_MetaInfo *self, int freshnessSeconds)
112 {
113  self->freshnessPeriod = freshnessSeconds < 0 ? -1.0 : (double)freshnessSeconds * 1000.0;
114 }
115 
123 static __inline ndn_Error
124 ndn_MetaInfo_setFromMetaInfo
125  (struct ndn_MetaInfo *self, const struct ndn_MetaInfo *other)
126 {
127  if (other == self)
128  // Setting to itself. Do nothing.
129  return NDN_ERROR_success;
130 
131  self->timestampMilliseconds = other->timestampMilliseconds;
132  self->type = other->type;
133  self->freshnessPeriod = other->freshnessPeriod;
134  ndn_NameComponent_setFromNameComponent(&self->finalBlockId, &other->finalBlockId);
135 
136  return NDN_ERROR_success;
137 }
138 
148 static __inline void ndn_Data_initialize
149  (struct ndn_Data *self, struct ndn_NameComponent *nameComponents, size_t maxNameComponents,
150  struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents)
151 {
152  ndn_Signature_initialize(&self->signature, keyNameComponents, maxKeyNameComponents);
153  ndn_Name_initialize(&self->name, nameComponents, maxNameComponents);
154  ndn_MetaInfo_initialize(&self->metaInfo);
155  ndn_Blob_initialize(&self->content, 0, 0);
156 }
157 
165 static __inline ndn_Error
166 ndn_Data_setFromData(struct ndn_Data *self, const struct ndn_Data *other)
167 {
168  ndn_Error error;
169 
170  if (other == self)
171  // Setting to itself. Do nothing.
172  return NDN_ERROR_success;
173 
174  if ((error = ndn_Signature_setFromSignature
175  (&self->signature, &other->signature)))
176  return error;
177  if ((error = ndn_Name_setFromName(&self->name, &other->name)))
178  return error;
179  if ((error = ndn_MetaInfo_setFromMetaInfo
180  (&self->metaInfo, &other->metaInfo)))
181  return error;
182  ndn_Blob_setFromBlob(&self->content, &other->content);
183 
184  return NDN_ERROR_success;
185 }
186 
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif
ndn_MillisecondsSince1970 timestampMilliseconds
milliseconds since 1/1/1970.
Definition: data-types.h:78
int genericTypeCode
used with Generic.
Definition: data-types.h:69
ndn_ContentType type
default is ndn_ContentType_BLOB.
Definition: data-types.h:79
Definition: data-types.h:85
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:89
ndn_SignatureType type
-1 for unspecified
Definition: data-types.h:66
struct ndn_NameComponent finalBlockId
has a pointer to a pre-allocated buffer.
Definition: data-types.h:82
struct ndn_KeyLocator keyLocator
used with Sha256WithRsaSignature, Sha256WithEcdsaSignature, HmacWithSha256Signature ...
Definition: data-types.h:70
An ndn_NameComponent holds a pointer to the component value.
Definition: name-types.h:41
An ndn_MetaInfo struct holds the meta info which is signed inside the data packet.
Definition: data-types.h:77
ndn_Milliseconds freshnessPeriod
-1 for none
Definition: data-types.h:81
An ndn_Signature struct holds the signature bits and other info representing the signature in a data ...
Definition: data-types.h:65
struct ndn_Blob signatureInfoEncoding
used with Generic
Definition: data-types.h:68