Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "block-helpers.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace encoding { |
| 26 | |
| 27 | template<Tag TAG> |
| 28 | size_t |
| 29 | prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value) |
| 30 | { |
| 31 | size_t valueLength = encoder.prependNonNegativeInteger(value); |
| 32 | size_t totalLength = valueLength; |
| 33 | totalLength += encoder.prependVarNumber(valueLength); |
| 34 | totalLength += encoder.prependVarNumber(type); |
| 35 | |
| 36 | return totalLength; |
| 37 | } |
| 38 | |
| 39 | template size_t |
| 40 | prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder, |
| 41 | uint32_t type, uint64_t value); |
| 42 | |
| 43 | template size_t |
| 44 | prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder, |
| 45 | uint32_t type, uint64_t value); |
| 46 | |
| 47 | |
| 48 | Block |
| 49 | makeNonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 50 | { |
| 51 | EncodingEstimator estimator; |
| 52 | size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value); |
| 53 | |
| 54 | EncodingBuffer encoder(totalLength, 0); |
| 55 | prependNonNegativeIntegerBlock(encoder, type, value); |
| 56 | |
| 57 | return encoder.block(); |
| 58 | } |
| 59 | |
| 60 | uint64_t |
| 61 | readNonNegativeInteger(const Block& block) |
| 62 | { |
| 63 | Buffer::const_iterator begin = block.value_begin(); |
| 64 | return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
| 65 | } |
| 66 | |
| 67 | //////// |
| 68 | |
| 69 | template<Tag TAG> |
| 70 | size_t |
| 71 | prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type) |
| 72 | { |
| 73 | size_t totalLength = encoder.prependVarNumber(0); |
| 74 | totalLength += encoder.prependVarNumber(type); |
| 75 | |
| 76 | return totalLength; |
| 77 | } |
| 78 | |
| 79 | template size_t |
| 80 | prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder, uint32_t type); |
| 81 | |
| 82 | template size_t |
| 83 | prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder, uint32_t type); |
| 84 | |
| 85 | |
| 86 | Block |
| 87 | makeEmptyBlock(uint32_t type) |
| 88 | { |
| 89 | EncodingEstimator estimator; |
| 90 | size_t totalLength = prependEmptyBlock(estimator, type); |
| 91 | |
| 92 | EncodingBuffer encoder(totalLength, 0); |
| 93 | prependEmptyBlock(encoder, type); |
| 94 | |
| 95 | return encoder.block(); |
| 96 | } |
| 97 | |
| 98 | //////// |
| 99 | |
| 100 | template<Tag TAG> |
| 101 | size_t |
| 102 | prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value) |
| 103 | { |
| 104 | size_t valueLength = encoder.prependByteArray(reinterpret_cast<const uint8_t*>(value.data()), |
| 105 | value.size()); |
| 106 | size_t totalLength = valueLength; |
| 107 | totalLength += encoder.prependVarNumber(valueLength); |
| 108 | totalLength += encoder.prependVarNumber(type); |
| 109 | |
| 110 | return totalLength; |
| 111 | } |
| 112 | |
| 113 | template size_t |
| 114 | prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>& encoder, |
| 115 | uint32_t type, const std::string& value); |
| 116 | |
| 117 | template size_t |
| 118 | prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>& encoder, |
| 119 | uint32_t type, const std::string& value); |
| 120 | |
| 121 | |
| 122 | Block |
| 123 | makeStringBlock(uint32_t type, const std::string& value) |
| 124 | { |
| 125 | EncodingEstimator estimator; |
| 126 | size_t totalLength = prependStringBlock(estimator, type, value); |
| 127 | |
| 128 | EncodingBuffer encoder(totalLength, 0); |
| 129 | prependStringBlock(encoder, type, value); |
| 130 | |
| 131 | return encoder.block(); |
| 132 | } |
| 133 | |
| 134 | std::string |
| 135 | readString(const Block& block) |
| 136 | { |
| 137 | return std::string(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 138 | } |
| 139 | |
| 140 | //////// |
| 141 | |
| 142 | Block |
| 143 | makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length) |
| 144 | { |
| 145 | EncodingEstimator estimator; |
| 146 | size_t totalLength = estimator.prependByteArrayBlock(type, value, length); |
| 147 | |
| 148 | EncodingBuffer encoder(totalLength, 0); |
| 149 | encoder.prependByteArrayBlock(type, value, length); |
| 150 | |
| 151 | return encoder.block(); |
| 152 | } |
| 153 | |
| 154 | Block |
| 155 | makeBinaryBlock(uint32_t type, const char* value, size_t length) |
| 156 | { |
| 157 | return makeBinaryBlock(type, reinterpret_cast<const uint8_t*>(value), length); |
| 158 | } |
| 159 | |
| 160 | } // namespace encoding |
| 161 | } // namespace ndn |