blob: 6dac765c858877d77fe10eb7b7f978d806e80f67 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsond56753e2013-09-13 16:46:17 -07002/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
5 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
7 * See COPYING for copyright and distribution information.
8 */
9
10#ifndef NDN_SIGNED_BLOB_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -070011#define NDN_SIGNED_BLOB_HPP
Jeff Thompsond56753e2013-09-13 16:46:17 -070012
13#include "blob.hpp"
14
15namespace ndn {
16
17/**
18 * A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet).
19 */
20class SignedBlob : public Blob {
21public:
22 /**
Jeff Thompsonaedee942013-09-16 17:57:38 -070023 * Create a new SignedBlob with a null pointer.
24 */
25 SignedBlob()
26 : signedPortionBeginOffset_(0), signedPortionEndOffset_(0)
27 {
28 }
29
30 /**
Jeff Thompsond56753e2013-09-13 16:46:17 -070031 * Create a new SignedBlob with an immutable copy of the given array.
32 * @param value A pointer to the byte array which is copied.
33 * @param valueLength The length of value.
34 * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion.
35 * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion.
36 */
37 SignedBlob
Jeff Thompson97223af2013-09-24 17:01:27 -070038 (const uint8_t* value, size_t valueLength, size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070039 : Blob(value, valueLength), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
40 {
41 }
42
43 /**
44 * Create a new SignedBlob with an immutable copy of the array in the given vector.
45 * If you want to transfer the array without copying, the the vector has to start as a
Jeff Thompson10ad12a2013-09-24 16:19:11 -070046 * 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 -070047 * @param value A reference to a vector which is copied.
48 * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion.
49 * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion.
50 */
51 SignedBlob
Jeff Thompson97223af2013-09-24 17:01:27 -070052 (const std::vector<uint8_t> &value, size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070053 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
54 {
55 }
56
57 /**
58 * Create a new SignedBlob to point to an existing byte array. IMPORTANT: After calling this constructor,
59 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
60 * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
61 * @param signedPortionBeginOffset The offset in the array of the beginning of the signed portion.
62 * @param signedPortionEndOffset The offset in the array of the end of the signed portion.
63 */
64 SignedBlob
Jeff Thompson10ad12a2013-09-24 16:19:11 -070065 (const ptr_lib::shared_ptr<std::vector<uint8_t> > &value,
Jeff Thompson97223af2013-09-24 17:01:27 -070066 size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070067 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
68 {
69 }
70 SignedBlob
Jeff Thompson10ad12a2013-09-24 16:19:11 -070071 (const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value,
Jeff Thompson97223af2013-09-24 17:01:27 -070072 size_t signedPortionBeginOffset, size_t signedPortionEndOffset)
Jeff Thompsond56753e2013-09-13 16:46:17 -070073 : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset)
74 {
75 }
76
77 /**
78 * Return the length of the signed portion of the immutable byte array, or 0 of the pointer to the array is null.
79 */
Jeff Thompson97223af2013-09-24 17:01:27 -070080 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070081 signedSize() const
Jeff Thompsond56753e2013-09-13 16:46:17 -070082 {
83 if (*this)
84 return signedPortionEndOffset_ - signedPortionBeginOffset_;
85 else
86 return 0;
87 }
88
89 /**
90 * Return a const pointer to the first byte of the signed portion of the immutable byte array, or 0 if the
91 * pointer to the array is null.
92 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070093 const uint8_t*
Jeff Thompson0050abe2013-09-17 12:50:25 -070094 signedBuf() const
Jeff Thompsond56753e2013-09-13 16:46:17 -070095 {
96 if (*this)
97 return &(*this)->front() + signedPortionBeginOffset_;
98 else
99 return 0;
100 }
101
102 /**
103 * Return the offset in the array of the beginning of the signed portion.
104 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700105 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700106 getSignedPortionBeginOffset() { return signedPortionBeginOffset_; }
Jeff Thompsond56753e2013-09-13 16:46:17 -0700107
108 /**
109 * Return the offset in the array of the end of the signed portion.
110 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700111 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700112 getSignedPortionEndOffset() { return signedPortionEndOffset_; }
Jeff Thompsond56753e2013-09-13 16:46:17 -0700113
114private:
Jeff Thompson97223af2013-09-24 17:01:27 -0700115 size_t signedPortionBeginOffset_;
116 size_t signedPortionEndOffset_;
Jeff Thompsond56753e2013-09-13 16:46:17 -0700117};
118
119}
120
121#endif