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 "publisher-public-key-digest.h"
28 #include "key-locator.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->digestAlgorithm, 0, 0);
43  ndn_Blob_initialize(&self->witness, 0, 0);
44  ndn_Blob_initialize(&self->signature, 0, 0);
45  ndn_PublisherPublicKeyDigest_initialize(&self->publisherPublicKeyDigest);
46  ndn_KeyLocator_initialize(&self->keyLocator, keyNameComponents, maxKeyNameComponents);
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  ndn_Blob_setFromBlob(&self->digestAlgorithm, &other->digestAlgorithm);
79  ndn_Blob_setFromBlob(&self->witness, &other->witness);
80  ndn_Blob_setFromBlob(&self->signature, &other->signature);
81  self->publisherPublicKeyDigest = other->publisherPublicKeyDigest;
82  if ((error = ndn_KeyLocator_setFromKeyLocator
83  (&self->keyLocator, &other->keyLocator)))
84  return error;
85 
86  return NDN_ERROR_success;
87 }
88 
93 static __inline void ndn_MetaInfo_initialize(struct ndn_MetaInfo *self)
94 {
95  self->timestampMilliseconds = -1;
96  self->type = ndn_ContentType_BLOB;
97  self->freshnessPeriod = -1;
98  ndn_NameComponent_initialize(&self->finalBlockId, 0, 0);
99 }
100 
104 static __inline int ndn_MetaInfo_getFreshnessSeconds(const struct ndn_MetaInfo *self)
105 {
106  return self->freshnessPeriod < 0 ? -1 : (int)round(self->freshnessPeriod / 1000.0);
107 }
108 
112 static __inline void ndn_MetaInfo_setFreshnessSeconds(struct ndn_MetaInfo *self, int freshnessSeconds)
113 {
114  self->freshnessPeriod = freshnessSeconds < 0 ? -1.0 : (double)freshnessSeconds * 1000.0;
115 }
116 
124 static __inline ndn_Error
125 ndn_MetaInfo_setFromMetaInfo
126  (struct ndn_MetaInfo *self, const struct ndn_MetaInfo *other)
127 {
128  if (other == self)
129  // Setting to itself. Do nothing.
130  return NDN_ERROR_success;
131 
132  self->timestampMilliseconds = other->timestampMilliseconds;
133  self->type = other->type;
134  self->freshnessPeriod = other->freshnessPeriod;
135  ndn_NameComponent_setFromNameComponent(&self->finalBlockId, &other->finalBlockId);
136 
137  return NDN_ERROR_success;
138 }
139 
149 static __inline void ndn_Data_initialize
150  (struct ndn_Data *self, struct ndn_NameComponent *nameComponents, size_t maxNameComponents,
151  struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents)
152 {
153  ndn_Signature_initialize(&self->signature, keyNameComponents, maxKeyNameComponents);
154  ndn_Name_initialize(&self->name, nameComponents, maxNameComponents);
155  ndn_MetaInfo_initialize(&self->metaInfo);
156  ndn_Blob_initialize(&self->content, 0, 0);
157 }
158 
166 static __inline ndn_Error
167 ndn_Data_setFromData(struct ndn_Data *self, const struct ndn_Data *other)
168 {
169  ndn_Error error;
170 
171  if (other == self)
172  // Setting to itself. Do nothing.
173  return NDN_ERROR_success;
174 
175  if ((error = ndn_Signature_setFromSignature
176  (&self->signature, &other->signature)))
177  return error;
178  if ((error = ndn_Name_setFromName(&self->name, &other->name)))
179  return error;
180  if ((error = ndn_MetaInfo_setFromMetaInfo
181  (&self->metaInfo, &other->metaInfo)))
182  return error;
183  ndn_Blob_setFromBlob(&self->content, &other->content);
184 
185  return NDN_ERROR_success;
186 }
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #endif
struct ndn_PublisherPublicKeyDigest publisherPublicKeyDigest
Definition: data-types.h:72
ndn_MillisecondsSince1970 timestampMilliseconds
milliseconds since 1/1/1970.
Definition: data-types.h:80
ndn_ContentType type
default is ndn_ContentType_DATA.
Definition: data-types.h:81
Definition: data-types.h:86
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:90
struct ndn_Blob witness
A Blob whose value is a pointer to pre-allocated buffer.
Definition: data-types.h:68
struct ndn_NameComponent finalBlockId
has a pointer to a pre-allocated buffer.
Definition: data-types.h:83
Copyright (C) 2015 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:79
ndn_Milliseconds freshnessPeriod
-1 for none
Definition: data-types.h:82
An ndn_Signature struct holds the signature bits and other info representing the signature in a data ...
Definition: data-types.h:64
struct ndn_Blob digestAlgorithm
A Blob whose value is a pointer to a pre-allocated buffer.
Definition: data-types.h:66