Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | /** |
| 17 | * A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet). |
| 18 | */ |
| 19 | class SignedBlob : public Blob { |
| 20 | public: |
| 21 | /** |
Jeff Thompson | aedee94 | 2013-09-16 17:57:38 -0700 | [diff] [blame] | 22 | * Create a new SignedBlob with a null pointer. |
| 23 | */ |
| 24 | SignedBlob() |
| 25 | : signedPortionBeginOffset_(0), signedPortionEndOffset_(0) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | /** |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 30 | * 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 37 | (const uint8_t* value, size_t valueLength, size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 45 | * ptr_lib::shared_ptr<std::vector<uint8_t> > and you can use the SignedBlob constructor with this type. |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 51 | (const std::vector<uint8_t> &value, size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 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 |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 64 | (const ptr_lib::shared_ptr<std::vector<uint8_t> > &value, |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 65 | size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 66 | : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 67 | { |
| 68 | } |
| 69 | SignedBlob |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 70 | (const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value, |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 71 | size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 79 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 80 | signedSize() const |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 81 | { |
| 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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 92 | const uint8_t* |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 93 | signedBuf() const |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 94 | { |
| 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 104 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 105 | getSignedPortionBeginOffset() { return signedPortionBeginOffset_; } |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Return the offset in the array of the end of the signed portion. |
| 109 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 110 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 111 | getSignedPortionEndOffset() { return signedPortionEndOffset_; } |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 114 | size_t signedPortionBeginOffset_; |
| 115 | size_t signedPortionEndOffset_; |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #endif |