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->freshnessPeriod = -1;
96  ndn_NameComponent_initialize(&self->finalBlockId, 0, 0);
97 }
98 
102 static __inline int ndn_MetaInfo_getFreshnessSeconds(const struct ndn_MetaInfo *self)
103 {
104  return self->freshnessPeriod < 0 ? -1 : (int)round(self->freshnessPeriod / 1000.0);
105 }
106 
110 static __inline void ndn_MetaInfo_setFreshnessSeconds(struct ndn_MetaInfo *self, int freshnessSeconds)
111 {
112  self->freshnessPeriod = freshnessSeconds < 0 ? -1.0 : (double)freshnessSeconds * 1000.0;
113 }
114 
122 static __inline ndn_Error
123 ndn_MetaInfo_setFromMetaInfo
124  (struct ndn_MetaInfo *self, const struct ndn_MetaInfo *other)
125 {
126  if (other == self)
127  // Setting to itself. Do nothing.
128  return NDN_ERROR_success;
129 
130  self->timestampMilliseconds = other->timestampMilliseconds;
131  self->type = other->type;
132  self->freshnessPeriod = other->freshnessPeriod;
133  ndn_NameComponent_setFromNameComponent(&self->finalBlockId, &other->finalBlockId);
134 
135  return NDN_ERROR_success;
136 }
137 
147 static __inline void ndn_Data_initialize
148  (struct ndn_Data *self, struct ndn_NameComponent *nameComponents, size_t maxNameComponents,
149  struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents)
150 {
151  ndn_Signature_initialize(&self->signature, keyNameComponents, maxKeyNameComponents);
152  ndn_Name_initialize(&self->name, nameComponents, maxNameComponents);
153  ndn_MetaInfo_initialize(&self->metaInfo);
154  ndn_Blob_initialize(&self->content, 0, 0);
155 }
156 
164 static __inline ndn_Error
165 ndn_Data_setFromData(struct ndn_Data *self, const struct ndn_Data *other)
166 {
167  ndn_Error error;
168 
169  if (other == self)
170  // Setting to itself. Do nothing.
171  return NDN_ERROR_success;
172 
173  if ((error = ndn_Signature_setFromSignature
174  (&self->signature, &other->signature)))
175  return error;
176  if ((error = ndn_Name_setFromName(&self->name, &other->name)))
177  return error;
178  if ((error = ndn_MetaInfo_setFromMetaInfo
179  (&self->metaInfo, &other->metaInfo)))
180  return error;
181  ndn_Blob_setFromBlob(&self->content, &other->content);
182 
183  return NDN_ERROR_success;
184 }
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif
ndn_MillisecondsSince1970 timestampMilliseconds
milliseconds since 1/1/1970.
Definition: data-types.h:72
int genericTypeCode
used with Generic.
Definition: data-types.h:63
ndn_ContentType type
default is ndn_ContentType_DATA.
Definition: data-types.h:73
Definition: data-types.h:78
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:82
ndn_SignatureType type
-1 for unspecified
Definition: data-types.h:60
struct ndn_NameComponent finalBlockId
has a pointer to a pre-allocated buffer.
Definition: data-types.h:75
struct ndn_KeyLocator keyLocator
used with Sha256WithRsaSignature, Sha256WithEcdsaSignature, HmacWithSha256Signature ...
Definition: data-types.h:64
Copyright (C) 2015-2016 Regents of the University of California.
Definition: name-types.h:33
An ndn_MetaInfo struct holds the meta info which is signed inside the data packet.
Definition: data-types.h:71
ndn_Milliseconds freshnessPeriod
-1 for none
Definition: data-types.h:74
An ndn_Signature struct holds the signature bits and other info representing the signature in a data ...
Definition: data-types.h:59
struct ndn_Blob signatureInfoEncoding
used with Generic
Definition: data-types.h:62