blob: dd94ae022c3c05afb00e0629cc4d51efa420c65b [file] [log] [blame]
Jeff Thompsond56753e2013-09-13 16:46:17 -07001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
4 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_SIGNED_BLOB_HPP
10#define NDN_SIGNED_BLOB_HPP
11
12#include "blob.hpp"
13
14namespace ndn {
15
16/**
17 * A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet).
18 */
19class SignedBlob : public Blob {
20public:
21 /**
Jeff Thompsonaedee942013-09-16 17:57:38 -070022 * Create a new SignedBlob with a null pointer.
23 */
24 SignedBlob()
25 : signedPortionBeginOffset_(0), signedPortionEndOffset_(0)
26 {
27 }
28
29 /**
Jeff Thompsond56753e2013-09-13 16:46:17 -070030 * Create a new SignedBlob with an immutable copy of the given array.
31 * @param value A pointer to the byte array which is copied.
32 * @param valueLength The length of value.
33 * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion.
34 * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion.
35 */
36 SignedBlob
Jeff Thompson97223af2013-09-24 17:01:27 -070037 (const uint8_t* value, size_t valueLength, size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070038 : Blob(value, valueLength), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
39 {
40 }
41
42 /**
43 * Create a new SignedBlob with an immutable copy of the array in the given vector.
44 * If you want to transfer the array without copying, the the vector has to start as a
Jeff Thompson10ad12a2013-09-24 16:19:11 -070045 * ptr_lib::shared_ptr<std::vector<uint8_t> > and you can use the SignedBlob constructor with this type.
Jeff Thompsond56753e2013-09-13 16:46:17 -070046 * @param value A reference to a vector which is copied.
47 * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion.
48 * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion.
49 */
50 SignedBlob
Jeff Thompson97223af2013-09-24 17:01:27 -070051 (const std::vector<uint8_t> &value, size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070052 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
53 {
54 }
55
56 /**
57 * Create a new SignedBlob to point to an existing byte array. IMPORTANT: After calling this constructor,
58 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
59 * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
60 * @param signedPortionBeginOffset The offset in the array of the beginning of the signed portion.
61 * @param signedPortionEndOffset The offset in the array of the end of the signed portion.
62 */
63 SignedBlob
Jeff Thompson10ad12a2013-09-24 16:19:11 -070064 (const ptr_lib::shared_ptr<std::vector<uint8_t> > &value,
Jeff Thompson97223af2013-09-24 17:01:27 -070065 size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070066 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
67 {
68 }
69 SignedBlob
Jeff Thompson10ad12a2013-09-24 16:19:11 -070070 (const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value,
Jeff Thompson97223af2013-09-24 17:01:27 -070071 size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070072 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
73 {
74 }
75
76 /**
77 * Return the length of the signed portion of the immutable byte array, or 0 of the pointer to the array is null.
78 */
Jeff Thompson97223af2013-09-24 17:01:27 -070079 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070080 signedSize() const
Jeff Thompsond56753e2013-09-13 16:46:17 -070081 {
82 if (*this)
83 return signedPortionEndOffset_ - signedPortionBeginOffset_;
84 else
85 return 0;
86 }
87
88 /**
89 * Return a const pointer to the first byte of the signed portion of the immutable byte array, or 0 if the
90 * pointer to the array is null.
91 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070092 const uint8_t*
Jeff Thompson0050abe2013-09-17 12:50:25 -070093 signedBuf() const
Jeff Thompsond56753e2013-09-13 16:46:17 -070094 {
95 if (*this)
96 return &(*this)->front() + signedPortionBeginOffset_;
97 else
98 return 0;
99 }
100
101 /**
102 * Return the offset in the array of the beginning of the signed portion.
103 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700104 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105 getSignedPortionBeginOffset() { return signedPortionBeginOffset_; }
Jeff Thompsond56753e2013-09-13 16:46:17 -0700106
107 /**
108 * Return the offset in the array of the end of the signed portion.
109 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700110 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111 getSignedPortionEndOffset() { return signedPortionEndOffset_; }
Jeff Thompsond56753e2013-09-13 16:46:17 -0700112
113private:
Jeff Thompson97223af2013-09-24 17:01:27 -0700114 size_t signedPortionBeginOffset_;
115 size_t signedPortionEndOffset_;
Jeff Thompsond56753e2013-09-13 16:46:17 -0700116};
117
118}
119
120#endif