blob: 34036110aebdcdca34e0f3b5e56d31148c9de176 [file] [log] [blame]
Alexander Afanasyev1043c702013-07-15 16:21:09 -07001/* -*- 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 Afanasyev0aa47bf2013-07-30 10:55:36 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev1043c702013-07-15 16:21:09 -070019 */
20
21#include "wire-ccnb.h"
22
23#include <sstream>
24#include <boost/foreach.hpp>
25#include "ccnb-parser/common.h"
Alexander Afanasyeva89bc102013-07-16 10:17:31 -070026#include "ccnb-parser/visitors/name-visitor.h"
27#include "ccnb-parser/syntax-tree/block.h"
Alexander Afanasyev1043c702013-07-15 16:21:09 -070028
29NDN_NAMESPACE_BEGIN
30
31namespace 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
42size_t
43Ccnb::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
61size_t
62Ccnb::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
74size_t
75Ccnb::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
88size_t
89Ccnb::EstimateNumber (uint32_t number)
90{
91 std::ostringstream os;
92 os << number;
93 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
94}
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -070095
Alexander Afanasyev1043c702013-07-15 16:21:09 -070096size_t
97Ccnb::AppendCloser (Buffer::Iterator &start)
98{
99 start.WriteU8 (CcnbParser::CCN_CLOSE);
100 return 1;
101}
102
103size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700104Ccnb::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 Afanasyev0aa47bf2013-07-30 10:55:36 -0700113
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700114 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 Afanasyev0aa47bf2013-07-30 10:55:36 -0700126
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700127 return len + required_bytes;
128}
129
130size_t
131Ccnb::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
141size_t
142Ccnb::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
160size_t
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700161Ccnb::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
189size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700190Ccnb::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
198size_t
199Ccnb::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
213size_t
214Ccnb::EstimateString (uint32_t dtag, const std::string &string)
215{
216 return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1;
217}
218
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700219size_t
220Ccnb::SerializeName (Buffer::Iterator &start, const Name &name)
221{
222 size_t written = 0;
Alexander Afanasyevdfd52492013-09-03 12:48:41 -0700223 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG);
Alexander Afanasyev92136012013-07-16 20:36:30 -0700224 BOOST_FOREACH (const name::Component &component, name)
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700225 {
226 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
Alexander Afanasyev92136012013-07-16 20:36:30 -0700227 reinterpret_cast<const uint8_t*>(component.buf ()), component.size());
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700228 }
Alexander Afanasyevdfd52492013-09-03 12:48:41 -0700229 written += AppendCloser (start);
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700230 return written;
231}
232
233size_t
234Ccnb::SerializedSizeName (const Name &name)
235{
236 size_t written = 0;
Alexander Afanasyevdfd52492013-09-03 12:48:41 -0700237 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name);
Alexander Afanasyev92136012013-07-16 20:36:30 -0700238 BOOST_FOREACH (const name::Component &component, name)
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700239 {
Alexander Afanasyev92136012013-07-16 20:36:30 -0700240 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size ());
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700241 }
Alexander Afanasyevdfd52492013-09-03 12:48:41 -0700242 written += 1;
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700243 return written;
244}
245
246Ptr<Name>
247Ccnb::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 Afanasyev1043c702013-07-15 16:21:09 -0700258} // wire
259
260NDN_NAMESPACE_END