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 | /** |
| 22 | * Create a new SignedBlob with an immutable copy of the given array. |
| 23 | * @param value A pointer to the byte array which is copied. |
| 24 | * @param valueLength The length of value. |
| 25 | * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion. |
| 26 | * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion. |
| 27 | */ |
| 28 | SignedBlob |
| 29 | (const unsigned char* value, unsigned int valueLength, unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset) |
| 30 | : Blob(value, valueLength), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Create a new SignedBlob with an immutable copy of the array in the given vector. |
| 36 | * If you want to transfer the array without copying, the the vector has to start as a |
| 37 | * ptr_lib::shared_ptr<std::vector<unsigned char> > and you can use the SignedBlob constructor with this type. |
| 38 | * @param value A reference to a vector which is copied. |
| 39 | * @param signedPortionBeginOffset The offset in the encoding of the beginning of the signed portion. |
| 40 | * @param signedPortionEndOffset The offset in the encoding of the end of the signed portion. |
| 41 | */ |
| 42 | SignedBlob |
| 43 | (const std::vector<unsigned char> &value, unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset) |
| 44 | : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create a new SignedBlob to point to an existing byte array. IMPORTANT: After calling this constructor, |
| 50 | * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it. |
| 51 | * @param value A pointer to a vector with the byte array. This takes another reference and does not copy the bytes. |
| 52 | * @param signedPortionBeginOffset The offset in the array of the beginning of the signed portion. |
| 53 | * @param signedPortionEndOffset The offset in the array of the end of the signed portion. |
| 54 | */ |
| 55 | SignedBlob |
| 56 | (const ptr_lib::shared_ptr<std::vector<unsigned char> > &value, |
| 57 | unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset) |
| 58 | : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 59 | { |
| 60 | } |
| 61 | SignedBlob |
| 62 | (const ptr_lib::shared_ptr<const std::vector<unsigned char> > &value, |
| 63 | unsigned int signedPortionBeginOffset, unsigned int signedPortionEndOffset) |
| 64 | : Blob(value), signedPortionBeginOffset_(signedPortionBeginOffset), signedPortionEndOffset_(signedPortionEndOffset) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Return the length of the signed portion of the immutable byte array, or 0 of the pointer to the array is null. |
| 70 | */ |
| 71 | unsigned int signedSize() const |
| 72 | { |
| 73 | if (*this) |
| 74 | return signedPortionEndOffset_ - signedPortionBeginOffset_; |
| 75 | else |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Return a const pointer to the first byte of the signed portion of the immutable byte array, or 0 if the |
| 81 | * pointer to the array is null. |
| 82 | */ |
| 83 | const unsigned char* signedBuf() const |
| 84 | { |
| 85 | if (*this) |
| 86 | return &(*this)->front() + signedPortionBeginOffset_; |
| 87 | else |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return the offset in the array of the beginning of the signed portion. |
| 93 | */ |
| 94 | unsigned int getSignedPortionBeginOffset() { return signedPortionBeginOffset_; } |
| 95 | |
| 96 | /** |
| 97 | * Return the offset in the array of the end of the signed portion. |
| 98 | */ |
| 99 | unsigned int getSignedPortionEndOffset() { return signedPortionEndOffset_; } |
| 100 | |
| 101 | private: |
| 102 | unsigned int signedPortionBeginOffset_; |
| 103 | unsigned int signedPortionEndOffset_; |
| 104 | }; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | #endif |