All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
der-node.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
24 #ifndef NDN_DER_NODE_HPP
25 #define NDN_DER_NODE_HPP
26 
27 #include <ndn-cpp/util/blob.hpp>
28 #include <ndn-cpp/encoding/oid.hpp>
29 #include "../../util/dynamic-uint8-vector.hpp"
30 #include "der-node-type.hpp"
31 
32 namespace ndn {
33 
38 class DerNode {
39 public:
40  virtual size_t
41  getSize();
42 
47  virtual Blob
48  encode();
49 
58  static ptr_lib::shared_ptr<DerNode>
59  parse(const uint8_t* inputBuf, size_t inputBufLength, size_t startIdx = 0);
60 
61  static ptr_lib::shared_ptr<DerNode>
62  parse(const Blob& input, size_t startIdx = 0)
63  {
64  return parse(input.buf(), input.size(), startIdx);
65  }
66 
67  static ptr_lib::shared_ptr<DerNode>
68  parse(const std::vector<uint8_t>& input, size_t startIdx = 0)
69  {
70  return parse(&input[0], input.size(), startIdx);
71  }
72 
78  virtual Blob
79  toVal();
80 
85  Blob
86  getPayload() { return Blob(&payload_[0], payloadPosition_); }
87 
95  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
96  getChildren();
97 
98  class DerStructure;
99 
101  // Now for all the node types...
102  // Class definitions are after the definition of DerNode.
104 
105  class DerByteString;
106  class DerBoolean;
107  class DerInteger;
108  class DerBitString;
109  class DerOctetString;
110  class DerNull;
111  class DerOid;
112  class DerSequence;
113  class DerPrintableString;
114  class DerGeneralizedTime;
115 
126  static DerNode::DerSequence&
128  (const std::vector<ptr_lib::shared_ptr<DerNode> >&children, size_t index);
129 
130 protected:
137  : nodeType_(nodeType),
138  parent_(0),
139  payload_(0),
140  payloadPosition_(0)
141  {
142  }
143 
148  void
149  encodeHeader(size_t size);
150 
157  size_t
158  decodeHeader(const uint8_t* inputBuf, size_t inputBufLength, size_t startIdx);
159 
165  virtual void
166  decode(const uint8_t* inputBuf, size_t inputBufLength, size_t startIdx);
167 
174  void
175  payloadAppend(const uint8_t *value, size_t valueLength)
176  {
177  payloadPosition_ = payload_.copy(value, valueLength, payloadPosition_);
178  }
179 
180  DerStructure* parent_;
181  DerNodeType nodeType_;
182  std::vector<uint8_t> header_;
183  DynamicUInt8Vector payload_;
184  size_t payloadPosition_;
185 };
186 
191 public:
196  virtual size_t
197  getSize();
198 
203  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
204  getChildren();
205 
206  void
207  addChild(const ptr_lib::shared_ptr<DerNode>& node, bool notifyParent = false)
208  {
209  node->parent_ = this;
210  nodeList_.push_back(node);
211 
212  if (notifyParent) {
213  if (parent_)
214  parent_->setChildChanged();
215  }
216 
217  childChanged_ = true;
218  }
219 
225  virtual Blob
226  encode();
227 
228 protected:
235  : DerNode(nodeType),
236  childChanged_(false),
237  size_(0)
238  {
239  }
240 
247  virtual void
248  decode(const uint8_t* inputBuf, size_t inputBufLength, size_t startIdx);
249 
250 private:
251  void
252  updateSize();
253 
257  void
258  setChildChanged()
259  {
260  if (parent_)
261  parent_->setChildChanged();
262  childChanged_ = true;
263  }
264 
265  bool childChanged_;
266  std::vector<ptr_lib::shared_ptr<DerNode> > nodeList_;
267  size_t size_;
268 };
269 
274 public:
279  virtual Blob
280  toVal();
281 
282 protected:
292  (const uint8_t* inputData, size_t inputDataLength, DerNodeType nodeType)
293  : DerNode(nodeType)
294  {
295  if (inputData) {
296  payloadAppend(inputData, inputDataLength);
297  encodeHeader(inputDataLength);
298  }
299  }
300 };
301 
307 class DerNode::DerBoolean : public DerNode {
308 public:
313  DerBoolean(bool value)
314  : DerNode(DerNodeType_Boolean)
315  {
316  uint8_t val = value ? 0xff : 0x00;
317  payloadAppend(&val, 1);
318  encodeHeader(1);
319  }
320 
321  DerBoolean()
322  : DerNode(DerNodeType_Boolean)
323  {
324  }
325 };
326 
330 class DerNode::DerInteger : public DerNode {
331 public:
336  DerInteger(int integer);
337 
338  DerInteger();
339 
345  int
346  toIntegerVal() const;
347 };
348 
353 public:
361  DerBitString(const uint8_t* inputBuf, size_t inputBufLength, int paddingLength)
362  : DerNode(DerNodeType_BitString)
363  {
364  uint8_t pad = paddingLength & 0xff;
365  payloadAppend(&pad, 1);
366  payloadAppend(inputBuf, inputBufLength);
367  encodeHeader(payloadPosition_);
368  }
369 
370  DerBitString()
371  : DerNode(DerNodeType_BitString)
372  {
373  }
374 };
375 
382 public:
383  DerOctetString(const uint8_t* inputData, size_t inputDataLength)
384  : DerByteString(inputData, inputDataLength, DerNodeType_OctetString)
385  {
386  }
387 
389  : DerByteString(0, 0, DerNodeType_OctetString)
390  {
391  }
392 };
393 
397 class DerNode::DerNull : public DerNode {
398 public:
403  : DerNode(DerNodeType_Null)
404  {
405  encodeHeader(0);
406  }
407 };
408 
412 class DerNode::DerOid : public DerNode {
413 public:
419  DerOid(const std::string& oidStr)
420  : DerNode(DerNodeType_ObjectIdentifier)
421  {
422  // Use OID to construct the integer list.
423  OID tempOid(oidStr);
424  prepareEncoding(tempOid.getIntegerList());
425  }
426 
432  DerOid(const OID& oid)
433  : DerNode(DerNodeType_ObjectIdentifier)
434  {
435  prepareEncoding(oid.getIntegerList());
436  }
437 
438  DerOid()
439  : DerNode(DerNodeType_ObjectIdentifier)
440  {
441  }
442 
447  virtual Blob
448  toVal();
449 
450 private:
455  void
456  prepareEncoding(const std::vector<int>& value);
457 
464  static std::vector<uint8_t>
465  encode128(int value);
466 
473  int
474  decode128(size_t offset, size_t& skip);
475 };
476 
478 public:
483  : DerStructure(DerNodeType_Sequence)
484  {
485  }
486 };
487 
495 public:
496  DerPrintableString(const uint8_t* inputData, size_t inputDataLength)
497  : DerByteString(inputData, inputDataLength, DerNodeType_PrintableString)
498  {
499  }
500 
502  : DerByteString(0, 0, DerNodeType_PrintableString)
503  {
504  }
505 };
506 
512 public:
518  : DerNode(DerNodeType_GeneralizedTime)
519  {
520  std::string derTime = toDerTimeString(msSince1970);
521  payloadAppend((const uint8_t*)&derTime[0], derTime.size());
522  encodeHeader(payloadPosition_);
523  }
524 
526  : DerNode(DerNodeType_GeneralizedTime)
527  {
528  }
529 
537 
543  static std::string
544  toIsoString(const MillisecondsSince1970& time);
545 
551  static MillisecondsSince1970
552  fromIsoString(const std::string& isoString);
553 
554 private:
560  static std::string
561  toDerTimeString(MillisecondsSince1970 msSince1970);
562 };
563 
564 }
565 
566 #endif
Blob getPayload()
Get a copy of the payload bytes.
Definition: der-node.hpp:86
DerOid(const OID &oid)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:432
DerOctetString extends DerByteString to encode a string of bytes.
Definition: der-node.hpp:381
size_t decodeHeader(const uint8_t *inputBuf, size_t inputBufLength, size_t startIdx)
Extract the header from an input buffer and return the size.
Definition: der-node.cpp:149
static ptr_lib::shared_ptr< DerNode > parse(const uint8_t *inputBuf, size_t inputBufLength, size_t startIdx=0)
Parse the data from the input buffer recursively and return the root as an object of a subclass of De...
Definition: der-node.cpp:53
DerSequence()
Create a DerSequence.
Definition: der-node.hpp:482
A DerBitString extends DerNode to handle a bit string.
Definition: der-node.hpp:352
virtual Blob encode()
Get the raw data encoding for this node.
Definition: der-node.cpp:40
virtual size_t getSize()
Override to get the total length of the encoding, including children.
Definition: der-node.cpp:201
static DerNode::DerSequence & getSequence(const std::vector< ptr_lib::shared_ptr< DerNode > > &children, size_t index)
Check that index is in bounds for the children list, cast children[index] to DerSequence and return i...
Definition: der-node.cpp:100
int toIntegerVal() const
Parse the payload as an integer and return the value.
Definition: der-node.cpp:316
void encodeHeader(size_t size)
Encode the given size and update the header.
Definition: der-node.cpp:114
DerNull()
Create a DerNull.
Definition: der-node.hpp:402
virtual Blob toVal()
Convert the encoded data to a standard representation.
Definition: der-node.cpp:87
DerNodeType
The DerNodeType enum defines the known DER node types.
Definition: der-node-type.hpp:32
Definition: der-node.hpp:477
DerInteger extends DerNode to encode an integer value.
Definition: der-node.hpp:330
MillisecondsSince1970 toMillisecondsSince1970()
Interpret the result of toVal() as a time string and return the milliseconds since 1970...
Definition: der-node.cpp:432
static MillisecondsSince1970 fromIsoString(const std::string &isoString)
Convert from the ISO string representation to the internal time format.
Definition: der-node.cpp:462
virtual Blob encode()
Override the base encode to return raw data encoding for this node and its children.
Definition: der-node.cpp:219
A DerPrintableString extends DerByteString to handle a a printable string.
Definition: der-node.hpp:494
DerByteString(const uint8_t *inputData, size_t inputDataLength, DerNodeType nodeType)
Create a DerByteString with the given inputData and nodeType.
Definition: der-node.hpp:292
DerOid(const std::string &oidStr)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:419
virtual Blob toVal()
Override to return the string representation of the OID.
Definition: der-node.cpp:332
A DerStructure extends DerNode to hold other DerNodes.
Definition: der-node.hpp:190
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
A DerOid extends DerNode to represent an object identifier.
Definition: der-node.hpp:412
const uint8_t * buf() const
Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null...
Definition: blob.hpp:159
DerBoolean extends DerNode to encode a boolean value.
Definition: der-node.hpp:307
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:147
virtual Blob toVal()
Override to return just the byte string.
Definition: der-node.cpp:272
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:119
A DerNull extends DerNode to encode a null value.
Definition: der-node.hpp:397
DerNode implements the DER node types used in encoding/decoding DER-formatted data.
Definition: der-node.hpp:38
DerStructure(DerNodeType nodeType)
Create a DerStructure with the given nodeType.
Definition: der-node.hpp:234
DerNode(DerNodeType nodeType)
Create a generic DER node with the given nodeType.
Definition: der-node.hpp:136
virtual void decode(const uint8_t *inputBuf, size_t inputBufLength, size_t startIdx)
Override the base decode to decode and store the data from an input buffer.
Definition: der-node.cpp:240
Definition: oid.hpp:31
size_t copy(const uint8_t *value, size_t valueLength, size_t offset)
Copy value into the vector at offset, using ensureLength to make sure the vector has enough size...
Definition: dynamic-uint8-vector.hpp:68
static std::string toIsoString(const MillisecondsSince1970 &time)
Convert to the ISO string representation of the time.
Definition: der-node.cpp:451
DerBoolean(bool value)
Create a new DerBoolean for the value.
Definition: der-node.hpp:313
virtual const std::vector< ptr_lib::shared_ptr< DerNode > > & getChildren()
Get the children of this node.
Definition: der-node.cpp:213
virtual const std::vector< ptr_lib::shared_ptr< DerNode > > & getChildren()
If this object is a DerSequence, get the children of this node.
Definition: der-node.cpp:93
A DerGeneralizedTime extends DerNode to represent a date and time, with millisecond accuracy...
Definition: der-node.hpp:511
virtual void decode(const uint8_t *inputBuf, size_t inputBufLength, size_t startIdx)
Decode and store the data from an input buffer.
Definition: der-node.cpp:189
A DynamicUInt8Vector extends ndn_DynamicUInt8Array to hold a shared_ptr<vector<uint8_t> > for use wit...
Definition: dynamic-uint8-vector.hpp:37
DerBitString(const uint8_t *inputBuf, size_t inputBufLength, int paddingLength)
Create a DerBitString with the given padding and inputBuf.
Definition: der-node.hpp:361
DerGeneralizedTime(MillisecondsSince1970 msSince1970)
Create a DerGeneralizedTime with the given milliseconds since 1970.
Definition: der-node.hpp:517
void payloadAppend(const uint8_t *value, size_t valueLength)
Call payload_.copy to copy value into payload_ at payloadPosition_.
Definition: der-node.hpp:175
A DerByteString extends DerNode to handle byte strings.
Definition: der-node.hpp:273