Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 2 | /** |
| 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 Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 11 | #define NDN_SIGNED_BLOB_HPP |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 12 | |
| 13 | #include "blob.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | /** |
| 18 | * A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet). |
| 19 | */ |
| 20 | class SignedBlob : public Blob { |
| 21 | public: |
| 22 | /** |
Jeff Thompson | aedee94 | 2013-09-16 17:57:38 -0700 | [diff] [blame] | 23 | * Create a new SignedBlob with a null pointer. |
| 24 | */ |
| 25 | SignedBlob() |
| 26 | : signedPortionBeginOffset_(0), signedPortionEndOffset_(0) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | /** |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 31 | * 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 38 | (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] | 39 | : 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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 46 | * 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] | 47 | * @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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 52 | (const std::vector<uint8_t> &value, size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 53 | : 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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 65 | (const ptr_lib::shared_ptr<std::vector<uint8_t> > &value, |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 66 | size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 67 | : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 68 | { |
| 69 | } |
| 70 | SignedBlob |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 71 | (const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value, |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 72 | size_t signedPortionBeginOffset, size_t signedPortionEndOffset) |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 73 | : 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 80 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 81 | signedSize() const |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 82 | { |
| 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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 93 | const uint8_t* |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 94 | signedBuf() const |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 95 | { |
| 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 105 | size_t |
Jeff Thompson | 45b1ad2 | 2013-10-24 11:15:09 -0700 | [diff] [blame] | 106 | getSignedPortionBeginOffset() const { return signedPortionBeginOffset_; } |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Return the offset in the array of the end of the signed portion. |
| 110 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 111 | size_t |
Jeff Thompson | 45b1ad2 | 2013-10-24 11:15:09 -0700 | [diff] [blame] | 112 | getSignedPortionEndOffset() const { return signedPortionEndOffset_; } |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 115 | size_t signedPortionBeginOffset_; |
| 116 | size_t signedPortionEndOffset_; |
Jeff Thompson | d56753e | 2013-09-13 16:46:17 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | #endif |