All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
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 #include "security/validity-period.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
40 static __inline void ndn_Signature_initialize(struct ndn_Signature *self, struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents) {
41  self->type = (ndn_SignatureType)-1;
42  ndn_Blob_initialize(&self->signature, 0, 0);
43  ndn_Blob_initialize(&self->signatureInfoEncoding, 0, 0);
44  self->genericTypeCode = -1;
45  ndn_KeyLocator_initialize(&self->keyLocator, keyNameComponents, maxKeyNameComponents);
46  ndn_ValidityPeriod_initialize(&self->validityPeriod);
47 }
48 
54 static __inline void ndn_Signature_clear(struct ndn_Signature *self)
55 {
56  ndn_Signature_initialize
57  (self, self->keyLocator.keyName.components,
58  self->keyLocator.keyName.maxComponents);
59 }
60 
68 static __inline ndn_Error
69 ndn_Signature_setFromSignature
70  (struct ndn_Signature *self, const struct ndn_Signature *other)
71 {
72  ndn_Error error;
73 
74  if (other == self)
75  // Setting to itself. Do nothing.
76  return NDN_ERROR_success;
77 
78  self->type = other->type;
79  ndn_Blob_setFromBlob(&self->signature, &other->signature);
80  ndn_Blob_setFromBlob(&self->signatureInfoEncoding, &other->signatureInfoEncoding);
81  self->genericTypeCode = other->genericTypeCode;
82  if ((error = ndn_KeyLocator_setFromKeyLocator
83  (&self->keyLocator, &other->keyLocator)))
84  return error;
85  self->validityPeriod = other->validityPeriod;
86 
87  return NDN_ERROR_success;
88 }
89 
94 static __inline void ndn_MetaInfo_initialize(struct ndn_MetaInfo *self)
95 {
96  self->timestampMilliseconds = -1;
97  self->type = ndn_ContentType_BLOB;
98  self->otherTypeCode = -1;
99  self->freshnessPeriod = -1;
100  ndn_NameComponent_initialize(&self->finalBlockId, 0, 0);
101 }
102 
106 static __inline int ndn_MetaInfo_getFreshnessSeconds(const struct ndn_MetaInfo *self)
107 {
108  return self->freshnessPeriod < 0 ? -1 : (int)round(self->freshnessPeriod / 1000.0);
109 }
110 
114 static __inline void ndn_MetaInfo_setFreshnessSeconds(struct ndn_MetaInfo *self, int freshnessSeconds)
115 {
116  self->freshnessPeriod = freshnessSeconds < 0 ? -1.0 : (double)freshnessSeconds * 1000.0;
117 }
118 
126 static __inline ndn_Error
127 ndn_MetaInfo_setFromMetaInfo
128  (struct ndn_MetaInfo *self, const struct ndn_MetaInfo *other)
129 {
130  if (other == self)
131  // Setting to itself. Do nothing.
132  return NDN_ERROR_success;
133 
134  self->timestampMilliseconds = other->timestampMilliseconds;
135  self->type = other->type;
136  self->freshnessPeriod = other->freshnessPeriod;
137  ndn_NameComponent_setFromNameComponent(&self->finalBlockId, &other->finalBlockId);
138 
139  return NDN_ERROR_success;
140 }
141 
151 static __inline void ndn_Data_initialize
152  (struct ndn_Data *self, struct ndn_NameComponent *nameComponents, size_t maxNameComponents,
153  struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents)
154 {
155  ndn_Signature_initialize(&self->signature, keyNameComponents, maxKeyNameComponents);
156  ndn_Name_initialize(&self->name, nameComponents, maxNameComponents);
157  ndn_MetaInfo_initialize(&self->metaInfo);
158  ndn_Blob_initialize(&self->content, 0, 0);
159 }
160 
168 static __inline ndn_Error
169 ndn_Data_setFromData(struct ndn_Data *self, const struct ndn_Data *other)
170 {
171  ndn_Error error;
172 
173  if (other == self)
174  // Setting to itself. Do nothing.
175  return NDN_ERROR_success;
176 
177  if ((error = ndn_Signature_setFromSignature
178  (&self->signature, &other->signature)))
179  return error;
180  if ((error = ndn_Name_setFromName(&self->name, &other->name)))
181  return error;
182  if ((error = ndn_MetaInfo_setFromMetaInfo
183  (&self->metaInfo, &other->metaInfo)))
184  return error;
185  ndn_Blob_setFromBlob(&self->content, &other->content);
186 
187  return NDN_ERROR_success;
188 }
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif
struct ndn_ValidityPeriod validityPeriod
used with Sha256WithRsaSignature, Sha256WithEcdsaSignature
Definition: data-types.h:73
ndn_MillisecondsSince1970 timestampMilliseconds
milliseconds since 1/1/1970.
Definition: data-types.h:81
int genericTypeCode
used with Generic.
Definition: data-types.h:70
ndn_ContentType type
default is ndn_ContentType_BLOB.
Definition: data-types.h:82
Definition: data-types.h:88
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:92
ndn_SignatureType type
-1 for unspecified
Definition: data-types.h:67
struct ndn_NameComponent finalBlockId
has a pointer to a pre-allocated buffer.
Definition: data-types.h:85
struct ndn_KeyLocator keyLocator
used with Sha256WithRsaSignature, Sha256WithEcdsaSignature, HmacWithSha256Signature ...
Definition: data-types.h:71
An ndn_NameComponent holds a pointer to the component value.
Definition: name-types.h:47
An ndn_MetaInfo struct holds the meta info which is signed inside the data packet.
Definition: data-types.h:80
ndn_Milliseconds freshnessPeriod
-1 for none
Definition: data-types.h:84
An ndn_Signature struct holds the signature bits and other info representing the signature in a data ...
Definition: data-types.h:66
struct ndn_Blob signatureInfoEncoding
used with Generic
Definition: data-types.h:69