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 |
| 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 | */ |
| 79 | unsigned int signedSize() const |
| 80 | { |
| 81 | if (*this) |
| 82 | return signedPortionEndOffset_ - signedPortionBeginOffset_; |
| 83 | else |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return a const pointer to the first byte of the signed portion of the immutable byte array, or 0 if the |
| 89 | * pointer to the array is null. |
| 90 | */ |
| 91 | const unsigned char* signedBuf() const |
| 92 | { |
| 93 | if (*this) |
| 94 | return &(*this)->front() + signedPortionBeginOffset_; |
| 95 | else |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Return the offset in the array of the beginning of the signed portion. |
| 101 | */ |
| 102 | unsigned int getSignedPortionBeginOffset() { return signedPortionBeginOffset_; } |
| 103 | |
| 104 | /** |
| 105 | * Return the offset in the array of the end of the signed portion. |
| 106 | */ |
| 107 | unsigned int getSignedPortionEndOffset() { return signedPortionEndOffset_; } |
| 108 | |
| 109 | private: |
| 110 | unsigned int signedPortionBeginOffset_; |
| 111 | unsigned int signedPortionEndOffset_; |
| 112 | }; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | #endif |