peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Peizhen Guo <patrick.guopz@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "sub-tree.hpp" |
| 23 | #include "Auditor.hpp" |
| 24 | #include <math.h> |
| 25 | |
| 26 | |
| 27 | namespace nsl { |
| 28 | |
| 29 | void |
| 30 | SubTree::addNode(ndn::ConstBufferPtr hash_ptr) |
| 31 | { |
| 32 | if(m_remainPosition > 0) |
| 33 | { |
| 34 | uint8_t seqNo = 127 - m_remainPosition; |
| 35 | m_nodeHashes[seqNo] = ndn::make_shared<ndn::Buffer>(*hash_ptr); |
| 36 | m_remainPosition -= 1; |
| 37 | updateHash(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | SubTree::updateHash() |
| 43 | { |
| 44 | uint8_t i_ = 6; |
| 45 | uint8_t lastNo = 126 - m_remainPosition; |
| 46 | for (; i_ > 0; i_--) |
| 47 | { |
| 48 | for (int i = int(pow(2, i_)) - 1; i <= lastNo; i+= 2) |
| 49 | { |
| 50 | if ((i + 1) <= lastNo) |
| 51 | { |
| 52 | uint8_t up_idx = (i-1) / 2; |
| 53 | Auditor hasher; |
| 54 | ndn::ConstBufferPtr buf = hasher.computeHash(m_nodeHashes[i], |
| 55 | m_nodeHashes[i + 1]); |
| 56 | m_nodeHashes[up_idx] = ndn::make_shared<ndn::Buffer>(*buf); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | uint8_t up_idx = (i-1) / 2; |
| 61 | Auditor hasher; |
| 62 | ndn::ConstBufferPtr buf = hasher.computeHashOneSide(m_nodeHashes[i]); |
| 63 | m_nodeHashes[up_idx] = ndn::make_shared<ndn::Buffer>(*buf); |
| 64 | } |
| 65 | } |
| 66 | lastNo = (lastNo - 1) / 2; |
| 67 | } |
| 68 | m_callBackUpdate(m_remainPosition, m_nodeHashes[0]); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | void |
| 75 | SubTree::updateLeafHash(Index subRootIndex, ndn::ConstBufferPtr hash) |
| 76 | { |
| 77 | uint8_t lastNo = 126 - m_remainPosition; |
| 78 | uint64_t sequenceNo = subRootIndex.number; |
| 79 | uint64_t level = subRootIndex.level; |
| 80 | uint8_t indexBase = int(pow(2, m_root.level - level) - 1); |
| 81 | uint8_t indexOffset = (sequenceNo - m_root.number) / int(pow(2, level)); |
| 82 | m_nodeHashes[indexBase + indexOffset] = ndn::make_shared<ndn::Buffer>(*hash); |
| 83 | if (lastNo < indexBase + indexOffset) // update value ? add new value |
| 84 | { |
| 85 | m_remainPosition -= 1; |
| 86 | } |
| 87 | updateHash(); |
| 88 | } |
| 89 | |
| 90 | ndn::ConstBufferPtr |
| 91 | SubTree::getHash(Index nodeIndex) |
| 92 | { |
| 93 | uint64_t sequenceNo = nodeIndex.number; |
| 94 | uint64_t level = nodeIndex.level; |
| 95 | uint8_t indexBase = int(pow(2, m_root.level - level) - 1); |
| 96 | uint8_t indexOffset = (sequenceNo - m_root.number) / int(pow(2, level)); |
| 97 | return m_nodeHashes[indexBase + indexOffset]; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | Index |
| 104 | SubTree::getRootIndex() |
| 105 | { |
| 106 | return m_root; |
| 107 | } |
| 108 | |
| 109 | uint8_t |
| 110 | SubTree::getRemainPosition() |
| 111 | { |
| 112 | return m_remainPosition; |
| 113 | } |
| 114 | |
| 115 | Index |
| 116 | SubTree::getParentRootIndex() |
| 117 | { |
| 118 | Index parentIndex; |
| 119 | parentIndex.number = m_root.number; |
| 120 | parentIndex.level = m_root.level; |
| 121 | for (int i = 0; i < 6; i++) |
| 122 | { |
| 123 | parentIndex.number -= parentIndex.number%int(pow(2, parentIndex.level + 1)); |
| 124 | parentIndex.level += 1; |
| 125 | } |
| 126 | return parentIndex; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | std::string |
| 132 | SubTree::encoding() |
| 133 | { |
| 134 | std::string subTreeInfo = ""; |
| 135 | uint64_t seq = m_root.number; |
| 136 | uint64_t lev = m_root.level; |
| 137 | unsigned char div_seq[8]; |
| 138 | unsigned char div_lev[8]; |
| 139 | for (int i = 0; i < 8; i++) |
| 140 | { |
| 141 | div_seq[i] = (seq >> (8*i)) & 0xFF; |
| 142 | div_lev[i] = (lev >> (8*i)) & 0xFF; |
| 143 | } |
| 144 | for (int i = 0; i < 8; i++) |
| 145 | { |
| 146 | subTreeInfo += div_seq[i]; |
| 147 | } |
| 148 | for (int i = 0; i < 8; i++) |
| 149 | { |
| 150 | subTreeInfo += div_lev[i]; |
| 151 | } |
| 152 | subTreeInfo += m_remainPosition; |
| 153 | for (int i = 0; i < 127; i++) |
| 154 | { |
| 155 | for (int j = 0; j < m_nodeHashes[i]->size(); j++) |
| 156 | { |
| 157 | subTreeInfo += (*m_nodeHashes[i])[j]; |
| 158 | } |
| 159 | uint8_t flag = 0; |
| 160 | for (int j = m_nodeHashes[i]->size(); j < 32; j++) |
| 161 | { |
| 162 | subTreeInfo += flag; |
| 163 | } |
| 164 | } |
| 165 | return subTreeInfo; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | SubTree::resumeFromString(uint8_t remain, std::vector<ndn::BufferPtr> hashes) |
| 170 | { |
| 171 | m_remainPosition = remain; |
| 172 | if (remain == 0) |
| 173 | { |
| 174 | for (int i = 0; i < hashes.size(); i++) |
| 175 | { |
| 176 | m_nodeHashes[i] = hashes[i]; |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | for (int i = 0; i < hashes.size(); i++) |
| 182 | { |
| 183 | m_nodeHashes[63 + i] = hashes[i]; |
| 184 | } |
| 185 | uint8_t i_ = 6; |
| 186 | uint8_t lastNo = 126 - m_remainPosition; |
| 187 | for (; i_ > 0; i_--) |
| 188 | { |
| 189 | for (int i = int(pow(2, i_)) - 1; i <= lastNo; i+= 2) |
| 190 | { |
| 191 | if ((i + 1) <= lastNo) |
| 192 | { |
| 193 | uint8_t up_idx = (i-1) / 2; |
| 194 | Auditor hasher; |
| 195 | ndn::ConstBufferPtr buf = hasher.computeHash(m_nodeHashes[i], |
| 196 | m_nodeHashes[i + 1]); |
| 197 | m_nodeHashes[up_idx] = ndn::make_shared<ndn::Buffer>(*buf); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | uint8_t up_idx = (i-1) / 2; |
| 202 | Auditor hasher; |
| 203 | ndn::ConstBufferPtr buf = hasher.computeHashOneSide(m_nodeHashes[i]); |
| 204 | m_nodeHashes[up_idx] = ndn::make_shared<ndn::Buffer>(*buf); |
| 205 | } |
| 206 | } |
| 207 | lastNo = (lastNo - 1) / 2; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | } // namespace nsl |