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 | * |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "wire-ccnb.h" |
| 22 | |
| 23 | #include <sstream> |
| 24 | #include <boost/foreach.hpp> |
| 25 | #include "ccnb-parser/common.h" |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 26 | #include "ccnb-parser/visitors/name-visitor.h" |
| 27 | #include "ccnb-parser/syntax-tree/block.h" |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 28 | |
| 29 | NDN_NAMESPACE_BEGIN |
| 30 | |
| 31 | namespace wire { |
| 32 | |
| 33 | ////////////////////////////////////////////////////////////////////// |
| 34 | ////////////////////////////////////////////////////////////////////// |
| 35 | ////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | #define CCN_TT_BITS 3 |
| 38 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 39 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 40 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 41 | |
| 42 | size_t |
| 43 | Ccnb::AppendBlockHeader (Buffer::Iterator &start, size_t val, uint32_t tt) |
| 44 | { |
| 45 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 46 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 47 | size_t n = 1; |
| 48 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 49 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 50 | (CCN_TT_MASK & tt); |
| 51 | val >>= (7-CCN_TT_BITS); |
| 52 | while (val != 0) { |
| 53 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 54 | n++; |
| 55 | val >>= 7; |
| 56 | } |
| 57 | start.Write (p,n); |
| 58 | return n; |
| 59 | } |
| 60 | |
| 61 | size_t |
| 62 | Ccnb::EstimateBlockHeader (size_t value) |
| 63 | { |
| 64 | value >>= (7-CCN_TT_BITS); |
| 65 | size_t n = 1; |
| 66 | while (value>0) |
| 67 | { |
| 68 | value >>= 7; |
| 69 | n++; |
| 70 | } |
| 71 | return n; |
| 72 | } |
| 73 | |
| 74 | size_t |
| 75 | Ccnb::AppendNumber (Buffer::Iterator &start, uint32_t number) |
| 76 | { |
| 77 | std::ostringstream os; |
| 78 | os << number; |
| 79 | |
| 80 | size_t written = 0; |
| 81 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 82 | written += os.str().size(); |
| 83 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 84 | |
| 85 | return written; |
| 86 | } |
| 87 | |
| 88 | size_t |
| 89 | Ccnb::EstimateNumber (uint32_t number) |
| 90 | { |
| 91 | std::ostringstream os; |
| 92 | os << number; |
| 93 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 94 | } |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 96 | size_t |
| 97 | Ccnb::AppendCloser (Buffer::Iterator &start) |
| 98 | { |
| 99 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 104 | Ccnb::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
| 105 | { |
| 106 | // the original function implements Markers... thought not sure what are these markers for... |
| 107 | |
| 108 | // Determine miminal number of bytes required to store the timestamp |
| 109 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 110 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 111 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 112 | required_bytes++; |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 114 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 115 | |
| 116 | // write part with seconds |
| 117 | ts = time.ToInteger (Time::S) >> 4; |
| 118 | for (int i = 0; i < required_bytes - 2; i++) |
| 119 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 120 | |
| 121 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 122 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 123 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 124 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 125 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 127 | return len + required_bytes; |
| 128 | } |
| 129 | |
| 130 | size_t |
| 131 | Ccnb::EstimateTimestampBlob (const Time &time) |
| 132 | { |
| 133 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 134 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 135 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 136 | required_bytes++; |
| 137 | |
| 138 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 139 | } |
| 140 | |
| 141 | size_t |
| 142 | Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, |
| 143 | const uint8_t *data, size_t size) |
| 144 | { |
| 145 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 146 | /* 2 */ |
| 147 | if (size>0) |
| 148 | { |
| 149 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 150 | start.Write (data, size); |
| 151 | written += size; |
| 152 | /* size */ |
| 153 | } |
| 154 | written += AppendCloser (start); |
| 155 | /* 1 */ |
| 156 | |
| 157 | return written; |
| 158 | } |
| 159 | |
| 160 | size_t |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 161 | Ccnb::AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, |
| 162 | uint32_t length, |
| 163 | const uint8_t *data, size_t size) |
| 164 | { |
| 165 | if (size > length) |
| 166 | { |
| 167 | // no padding required |
| 168 | return AppendTaggedBlob (start, dtag, data, size); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 173 | |
| 174 | /* 2 */ |
| 175 | if (length>0) |
| 176 | { |
| 177 | written += AppendBlockHeader (start, length, CcnbParser::CCN_BLOB); |
| 178 | start.Write (data, size); |
| 179 | start.WriteU8 (0, length - size); |
| 180 | written += length; |
| 181 | /* size */ |
| 182 | } |
| 183 | written += AppendCloser (start); |
| 184 | /* 1 */ |
| 185 | |
| 186 | return written; |
| 187 | } |
| 188 | |
| 189 | size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 190 | Ccnb::EstimateTaggedBlob (uint32_t dtag, size_t size) |
| 191 | { |
| 192 | if (size>0) |
| 193 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 194 | else |
| 195 | return EstimateBlockHeader (dtag) + 1; |
| 196 | } |
| 197 | |
| 198 | size_t |
| 199 | Ccnb::AppendString (Buffer::Iterator &start, uint32_t dtag, |
| 200 | const std::string &string) |
| 201 | { |
| 202 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 203 | { |
| 204 | written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA); |
| 205 | start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ()); |
| 206 | written += string.size (); |
| 207 | } |
| 208 | written += AppendCloser (start); |
| 209 | |
| 210 | return written; |
| 211 | } |
| 212 | |
| 213 | size_t |
| 214 | Ccnb::EstimateString (uint32_t dtag, const std::string &string) |
| 215 | { |
| 216 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1; |
| 217 | } |
| 218 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 219 | size_t |
| 220 | Ccnb::SerializeName (Buffer::Iterator &start, const Name &name) |
| 221 | { |
| 222 | size_t written = 0; |
Alexander Afanasyev | dfd5249 | 2013-09-03 12:48:41 -0700 | [diff] [blame] | 223 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 224 | BOOST_FOREACH (const name::Component &component, name) |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 225 | { |
| 226 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 227 | reinterpret_cast<const uint8_t*>(component.buf ()), component.size()); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 228 | } |
Alexander Afanasyev | dfd5249 | 2013-09-03 12:48:41 -0700 | [diff] [blame] | 229 | written += AppendCloser (start); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 230 | return written; |
| 231 | } |
| 232 | |
| 233 | size_t |
| 234 | Ccnb::SerializedSizeName (const Name &name) |
| 235 | { |
| 236 | size_t written = 0; |
Alexander Afanasyev | dfd5249 | 2013-09-03 12:48:41 -0700 | [diff] [blame] | 237 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 238 | BOOST_FOREACH (const name::Component &component, name) |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 239 | { |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 240 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size ()); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 241 | } |
Alexander Afanasyev | dfd5249 | 2013-09-03 12:48:41 -0700 | [diff] [blame] | 242 | written += 1; |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 243 | return written; |
| 244 | } |
| 245 | |
| 246 | Ptr<Name> |
| 247 | Ccnb::DeserializeName (Buffer::Iterator &i) |
| 248 | { |
| 249 | Ptr<Name> name = Create<Name> (); |
| 250 | CcnbParser::NameVisitor nameVisitor; |
| 251 | |
| 252 | Ptr<CcnbParser::Block> root = CcnbParser::Block::ParseBlock (i); |
| 253 | root->accept (nameVisitor, GetPointer (name)); |
| 254 | |
| 255 | return name; |
| 256 | } |
| 257 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 258 | } // wire |
| 259 | |
| 260 | NDN_NAMESPACE_END |