blob: e3a9ef13302251154ec47bc10747f294f4e362d1 [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 *
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"
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}
95
96size_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++;
113
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)) );
126
127 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
161Ccnb::EstimateTaggedBlob (uint32_t dtag, size_t size)
162{
163 if (size>0)
164 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
165 else
166 return EstimateBlockHeader (dtag) + 1;
167}
168
169size_t
170Ccnb::AppendString (Buffer::Iterator &start, uint32_t dtag,
171 const std::string &string)
172{
173 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
174 {
175 written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA);
176 start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ());
177 written += string.size ();
178 }
179 written += AppendCloser (start);
180
181 return written;
182}
183
184size_t
185Ccnb::EstimateString (uint32_t dtag, const std::string &string)
186{
187 return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1;
188}
189
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700190size_t
191Ccnb::SerializeName (Buffer::Iterator &start, const Name &name)
192{
193 size_t written = 0;
Alexander Afanasyev92136012013-07-16 20:36:30 -0700194 BOOST_FOREACH (const name::Component &component, name)
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700195 {
196 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
Alexander Afanasyev92136012013-07-16 20:36:30 -0700197 reinterpret_cast<const uint8_t*>(component.buf ()), component.size());
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700198 }
199 return written;
200}
201
202size_t
203Ccnb::SerializedSizeName (const Name &name)
204{
205 size_t written = 0;
Alexander Afanasyev92136012013-07-16 20:36:30 -0700206 BOOST_FOREACH (const name::Component &component, name)
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700207 {
Alexander Afanasyev92136012013-07-16 20:36:30 -0700208 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size ());
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700209 }
210 return written;
211}
212
213Ptr<Name>
214Ccnb::DeserializeName (Buffer::Iterator &i)
215{
216 Ptr<Name> name = Create<Name> ();
217 CcnbParser::NameVisitor nameVisitor;
218
219 Ptr<CcnbParser::Block> root = CcnbParser::Block::ParseBlock (i);
220 root->accept (nameVisitor, GetPointer (name));
221
222 return name;
223}
224
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700225} // wire
226
227NDN_NAMESPACE_END