Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "wire-ccnb.h" |
| 22 | |
| 23 | #include <sstream> |
| 24 | #include <boost/foreach.hpp> |
| 25 | #include "ccnb-parser/common.h" |
| 26 | |
| 27 | NDN_NAMESPACE_BEGIN |
| 28 | |
| 29 | namespace wire { |
| 30 | |
| 31 | ////////////////////////////////////////////////////////////////////// |
| 32 | ////////////////////////////////////////////////////////////////////// |
| 33 | ////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | #define CCN_TT_BITS 3 |
| 36 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 37 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 38 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 39 | |
| 40 | size_t |
| 41 | Ccnb::AppendBlockHeader (Buffer::Iterator &start, size_t val, uint32_t tt) |
| 42 | { |
| 43 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 44 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 45 | size_t n = 1; |
| 46 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 47 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 48 | (CCN_TT_MASK & tt); |
| 49 | val >>= (7-CCN_TT_BITS); |
| 50 | while (val != 0) { |
| 51 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 52 | n++; |
| 53 | val >>= 7; |
| 54 | } |
| 55 | start.Write (p,n); |
| 56 | return n; |
| 57 | } |
| 58 | |
| 59 | size_t |
| 60 | Ccnb::EstimateBlockHeader (size_t value) |
| 61 | { |
| 62 | value >>= (7-CCN_TT_BITS); |
| 63 | size_t n = 1; |
| 64 | while (value>0) |
| 65 | { |
| 66 | value >>= 7; |
| 67 | n++; |
| 68 | } |
| 69 | return n; |
| 70 | } |
| 71 | |
| 72 | size_t |
| 73 | Ccnb::AppendNumber (Buffer::Iterator &start, uint32_t number) |
| 74 | { |
| 75 | std::ostringstream os; |
| 76 | os << number; |
| 77 | |
| 78 | size_t written = 0; |
| 79 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 80 | written += os.str().size(); |
| 81 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 82 | |
| 83 | return written; |
| 84 | } |
| 85 | |
| 86 | size_t |
| 87 | Ccnb::EstimateNumber (uint32_t number) |
| 88 | { |
| 89 | std::ostringstream os; |
| 90 | os << number; |
| 91 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 92 | } |
| 93 | |
| 94 | size_t |
| 95 | Ccnb::AppendCloser (Buffer::Iterator &start) |
| 96 | { |
| 97 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | size_t |
| 102 | Ccnb::AppendName (Buffer::Iterator &start, const Name &name) |
| 103 | { |
| 104 | size_t written = 0; |
| 105 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 106 | { |
| 107 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
| 108 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 109 | } |
| 110 | return written; |
| 111 | } |
| 112 | |
| 113 | size_t |
| 114 | Ccnb::EstimateName (const Name &name) |
| 115 | { |
| 116 | size_t written = 0; |
| 117 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 118 | { |
| 119 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size()); |
| 120 | } |
| 121 | return written; |
| 122 | } |
| 123 | |
| 124 | size_t |
| 125 | Ccnb::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
| 126 | { |
| 127 | // the original function implements Markers... thought not sure what are these markers for... |
| 128 | |
| 129 | // Determine miminal number of bytes required to store the timestamp |
| 130 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 131 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 132 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 133 | required_bytes++; |
| 134 | |
| 135 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 136 | |
| 137 | // write part with seconds |
| 138 | ts = time.ToInteger (Time::S) >> 4; |
| 139 | for (int i = 0; i < required_bytes - 2; i++) |
| 140 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 141 | |
| 142 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 143 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 144 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 145 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 146 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 147 | |
| 148 | return len + required_bytes; |
| 149 | } |
| 150 | |
| 151 | size_t |
| 152 | Ccnb::EstimateTimestampBlob (const Time &time) |
| 153 | { |
| 154 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 155 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 156 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 157 | required_bytes++; |
| 158 | |
| 159 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 160 | } |
| 161 | |
| 162 | size_t |
| 163 | Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, |
| 164 | const uint8_t *data, size_t size) |
| 165 | { |
| 166 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 167 | /* 2 */ |
| 168 | if (size>0) |
| 169 | { |
| 170 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 171 | start.Write (data, size); |
| 172 | written += size; |
| 173 | /* size */ |
| 174 | } |
| 175 | written += AppendCloser (start); |
| 176 | /* 1 */ |
| 177 | |
| 178 | return written; |
| 179 | } |
| 180 | |
| 181 | size_t |
| 182 | Ccnb::EstimateTaggedBlob (uint32_t dtag, size_t size) |
| 183 | { |
| 184 | if (size>0) |
| 185 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 186 | else |
| 187 | return EstimateBlockHeader (dtag) + 1; |
| 188 | } |
| 189 | |
| 190 | size_t |
| 191 | Ccnb::AppendString (Buffer::Iterator &start, uint32_t dtag, |
| 192 | const std::string &string) |
| 193 | { |
| 194 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 195 | { |
| 196 | written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA); |
| 197 | start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ()); |
| 198 | written += string.size (); |
| 199 | } |
| 200 | written += AppendCloser (start); |
| 201 | |
| 202 | return written; |
| 203 | } |
| 204 | |
| 205 | size_t |
| 206 | Ccnb::EstimateString (uint32_t dtag, const std::string &string) |
| 207 | { |
| 208 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1; |
| 209 | } |
| 210 | |
| 211 | } // wire |
| 212 | |
| 213 | NDN_NAMESPACE_END |