blob: 0b6cddadaac548915809ca944a5fc8c876e056c1 [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
37 (const unsigned char* value, unsigned int valueLength, unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset)
38 : 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
45 * ptr_lib::shared_ptr<std::vector<unsigned char> > and you can use the SignedBlob constructor with this type.
46 * @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
51 (const std::vector<unsigned char> &value, unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset)
52 : 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
64 (const ptr_lib::shared_ptr<std::vector<unsigned char> > &value,
65 unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset)
66 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
67 {
68 }
69 SignedBlob
70 (const ptr_lib::shared_ptr<const std::vector<unsigned char> > &value,
71 unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset)
72 : 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 Thompson0050abe2013-09-17 12:50:25 -070079 unsigned int
80 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 Thompson0050abe2013-09-17 12:50:25 -070092 const unsigned char*
93 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 Thompson0050abe2013-09-17 12:50:25 -0700104 unsigned int
105 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 Thompson0050abe2013-09-17 12:50:25 -0700110 unsigned int
111 getSignedPortionEndOffset() { return signedPortionEndOffset_; }
Jeff Thompsond56753e2013-09-13 16:46:17 -0700112
113private:
114 unsigned int signedPortionBeginOffset_;
115 unsigned int signedPortionEndOffset_;
116};
117
118}
119
120#endif