Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 22 | #include "ccnx-name.h" |
| 23 | #include <boost/lexical_cast.hpp> |
| 24 | #include <ctype.h> |
| 25 | #include <boost/algorithm/string/join.hpp> |
| 26 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 27 | using namespace std; |
| 28 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 29 | namespace Ccnx{ |
| 30 | |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 31 | void |
| 32 | CcnxCharbuf::init(ccn_charbuf *buf) |
| 33 | { |
| 34 | if (buf != NULL) |
| 35 | { |
| 36 | m_buf = ccn_charbuf_create(); |
| 37 | ccn_charbuf_reserve(m_buf, buf->length); |
| 38 | memcpy(m_buf->buf, buf->buf, buf->length); |
| 39 | m_buf->length = buf->length; |
| 40 | } |
| 41 | } |
| 42 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 43 | CcnxCharbuf::CcnxCharbuf() |
| 44 | : m_buf(NULL) |
| 45 | { |
| 46 | m_buf = ccn_charbuf_create(); |
| 47 | } |
| 48 | |
| 49 | CcnxCharbuf::CcnxCharbuf(ccn_charbuf *buf) |
| 50 | : m_buf(NULL) |
| 51 | { |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 52 | init(buf); |
| 53 | } |
| 54 | |
| 55 | CcnxCharbuf::CcnxCharbuf(const CcnxCharbuf &other) |
| 56 | : m_buf (NULL) |
| 57 | { |
| 58 | init(other.m_buf); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 61 | CcnxCharbuf::CcnxCharbuf(const void *buf, size_t length) |
| 62 | { |
| 63 | m_buf = ccn_charbuf_create (); |
| 64 | ccn_charbuf_reserve (m_buf, length); |
| 65 | memcpy (m_buf->buf, buf, length); |
| 66 | m_buf->length = length; |
| 67 | } |
| 68 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 69 | CcnxCharbuf::~CcnxCharbuf() |
| 70 | { |
| 71 | ccn_charbuf_destroy(&m_buf); |
| 72 | } |
| 73 | |
| 74 | Name::Name() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | Name::Name(const string &name) |
| 79 | { |
| 80 | stringstream ss(name); |
| 81 | string compStr; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 82 | bool first = true; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 83 | while(getline(ss, compStr, '/')) |
| 84 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 85 | // discard the first empty comp before the first '/' |
| 86 | if (first) |
| 87 | { |
| 88 | first = false; |
| 89 | continue; |
| 90 | } |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 91 | Bytes comp(compStr.begin(), compStr.end()); |
| 92 | m_comps.push_back(comp); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Name::Name(const vector<Bytes> &comps) |
| 97 | { |
| 98 | m_comps = comps; |
| 99 | } |
| 100 | |
| 101 | Name::Name(const Name &other) |
| 102 | { |
| 103 | m_comps = other.m_comps; |
| 104 | } |
| 105 | |
| 106 | Name::Name(const unsigned char *data, const ccn_indexbuf *comps) |
| 107 | { |
| 108 | for (int i = 0; i < comps->n - 1; i++) |
| 109 | { |
| 110 | const unsigned char *compPtr; |
| 111 | size_t size; |
| 112 | ccn_name_comp_get(data, comps, i, &compPtr, &size); |
| 113 | Bytes comp; |
| 114 | readRaw(comp, compPtr, size); |
| 115 | m_comps.push_back(comp); |
| 116 | } |
| 117 | } |
| 118 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 119 | Name::Name (const unsigned char *buf, const size_t length) |
| 120 | { |
| 121 | ccn_indexbuf *idx = ccn_indexbuf_create(); |
| 122 | const ccn_charbuf namebuf = { length, length, const_cast<unsigned char *> (buf) }; |
| 123 | ccn_name_split (&namebuf, idx); |
| 124 | |
| 125 | const unsigned char *compPtr = NULL; |
| 126 | size_t size = 0; |
| 127 | int i = 0; |
| 128 | while (ccn_name_comp_get(namebuf.buf, idx, i, &compPtr, &size) == 0) |
| 129 | { |
| 130 | Bytes comp; |
| 131 | readRaw (comp, compPtr, size); |
| 132 | m_comps.push_back(comp); |
| 133 | i++; |
| 134 | } |
| 135 | ccn_indexbuf_destroy(&idx); |
| 136 | } |
| 137 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 138 | Name::Name (const CcnxCharbuf &buf) |
| 139 | { |
| 140 | ccn_indexbuf *idx = ccn_indexbuf_create(); |
| 141 | ccn_name_split (buf.getBuf (), idx); |
| 142 | |
| 143 | const unsigned char *compPtr = NULL; |
| 144 | size_t size = 0; |
| 145 | int i = 0; |
| 146 | while (ccn_name_comp_get(buf.getBuf ()->buf, idx, i, &compPtr, &size) == 0) |
| 147 | { |
| 148 | Bytes comp; |
| 149 | readRaw (comp, compPtr, size); |
| 150 | m_comps.push_back(comp); |
| 151 | i++; |
| 152 | } |
| 153 | ccn_indexbuf_destroy(&idx); |
| 154 | } |
| 155 | |
| 156 | Name::Name (const ccn_charbuf *buf) |
| 157 | { |
| 158 | ccn_indexbuf *idx = ccn_indexbuf_create(); |
| 159 | ccn_name_split (buf, idx); |
| 160 | |
| 161 | const unsigned char *compPtr = NULL; |
| 162 | size_t size = 0; |
| 163 | int i = 0; |
| 164 | while (ccn_name_comp_get(buf->buf, idx, i, &compPtr, &size) == 0) |
| 165 | { |
| 166 | Bytes comp; |
| 167 | readRaw (comp, compPtr, size); |
| 168 | m_comps.push_back(comp); |
| 169 | i++; |
| 170 | } |
| 171 | ccn_indexbuf_destroy(&idx); |
| 172 | } |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 173 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 174 | Name & |
| 175 | Name::operator=(const Name &other) |
| 176 | { |
| 177 | m_comps = other.m_comps; |
| 178 | return *this; |
| 179 | } |
| 180 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 181 | Name::operator==(const string &str) const |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 182 | { |
| 183 | return this->toString() == str; |
| 184 | } |
| 185 | |
| 186 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 187 | Name::operator!=(const string &str) const |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 188 | { |
| 189 | return !(*this == str); |
| 190 | } |
| 191 | |
| 192 | Name |
| 193 | operator+(const Name &n1, const Name &n2) |
| 194 | { |
| 195 | vector<Bytes> comps = n1.m_comps; |
| 196 | copy(n2.m_comps.begin(), n2.m_comps.end(), back_inserter(comps)); |
| 197 | return Name(comps); |
| 198 | } |
| 199 | |
| 200 | string |
| 201 | Name::toString() const |
| 202 | { |
| 203 | stringstream ss(stringstream::out); |
| 204 | ss << *this; |
| 205 | return ss.str(); |
| 206 | } |
| 207 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 208 | CcnxCharbufPtr |
| 209 | Name::toCcnxCharbuf() const |
| 210 | { |
| 211 | CcnxCharbufPtr ptr(new CcnxCharbuf()); |
| 212 | ccn_charbuf *cbuf = ptr->getBuf(); |
| 213 | ccn_name_init(cbuf); |
| 214 | int size = m_comps.size(); |
| 215 | for (int i = 0; i < size; i++) |
| 216 | { |
| 217 | ccn_name_append(cbuf, head(m_comps[i]), m_comps[i].size()); |
| 218 | } |
| 219 | return ptr; |
| 220 | } |
| 221 | |
| 222 | Name & |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 223 | Name::appendComp(const Name &comp) |
| 224 | { |
| 225 | m_comps.insert (m_comps.end (), |
| 226 | comp.m_comps.begin (), comp.m_comps.end ()); |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | Name & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 231 | Name::appendComp(const Bytes &comp) |
| 232 | { |
| 233 | m_comps.push_back(comp); |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | Name & |
| 238 | Name::appendComp(const string &compStr) |
| 239 | { |
| 240 | Bytes comp(compStr.begin(), compStr.end()); |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 241 | return appendComp(comp); |
| 242 | } |
| 243 | |
| 244 | Name & |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 245 | Name::appendComp (const void *buf, size_t size) |
| 246 | { |
| 247 | Bytes comp (reinterpret_cast<const unsigned char*> (buf), reinterpret_cast<const unsigned char*> (buf) + size); |
| 248 | return appendComp(comp); |
| 249 | } |
| 250 | |
| 251 | Name & |
| 252 | Name::appendComp(uint64_t number) |
| 253 | { |
| 254 | Bytes comp; |
| 255 | comp.push_back (0); |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 256 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 257 | while (number > 0) |
| 258 | { |
| 259 | comp.push_back (static_cast<unsigned char> (number & 0xFF)); |
| 260 | number >>= 8; |
| 261 | } |
| 262 | return appendComp (comp); |
| 263 | } |
| 264 | |
| 265 | uint64_t |
| 266 | Name::getCompAsInt (int index) const |
| 267 | { |
| 268 | Bytes comp = getComp(index); |
| 269 | if (comp.size () < 1 || |
| 270 | comp[0] != 0) |
| 271 | { |
| 272 | boost::throw_exception(NameException() |
| 273 | << error_info_str("Non integer component: " + getCompAsString(index))); |
| 274 | } |
| 275 | uint64_t ret = 0; |
| 276 | for (int i = comp.size () - 1; i >= 1; i--) |
| 277 | { |
| 278 | ret <<= 8; |
| 279 | ret |= comp [i]; |
| 280 | } |
| 281 | return ret; |
| 282 | } |
| 283 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 284 | const Bytes & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 285 | Name::getComp(int index) const |
| 286 | { |
| 287 | if (index >= m_comps.size()) |
| 288 | { |
| 289 | boost::throw_exception(NameException() << error_info_str("Index out of range: " + boost::lexical_cast<string>(index))); |
| 290 | } |
| 291 | return m_comps[index]; |
| 292 | } |
| 293 | |
| 294 | string |
| 295 | Name::getCompAsString(int index) const |
| 296 | { |
| 297 | Bytes comp = getComp(index); |
| 298 | stringstream ss(stringstream::out); |
| 299 | int size = comp.size(); |
| 300 | for (int i = 0; i < size; i++) |
| 301 | { |
| 302 | unsigned char ch = comp[i]; |
| 303 | if (isprint(ch)) |
| 304 | { |
| 305 | ss << (char) ch; |
| 306 | } |
| 307 | else |
| 308 | { |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 309 | ss << "%" << hex << setfill('0') << setw(2) << (unsigned int)ch; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | return ss.str(); |
| 314 | } |
| 315 | |
| 316 | Name |
| 317 | Name::getPartialName(int start, int n) const |
| 318 | { |
| 319 | int size = m_comps.size(); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 320 | if (start < 0 || start >= size || (n > 0 && start + n > size)) |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 321 | { |
| 322 | stringstream ss(stringstream::out); |
| 323 | ss << "getPartialName() parameter out of range! "; |
| 324 | ss << "start = " << start; |
| 325 | ss << "n = " << n; |
| 326 | ss << "size = " << size; |
| 327 | boost::throw_exception(NameException() << error_info_str(ss.str())); |
| 328 | } |
| 329 | |
| 330 | vector<Bytes> comps; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 331 | int end; |
| 332 | if (n > 0) |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 333 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 334 | end = start + n; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 335 | } |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 336 | else |
| 337 | { |
| 338 | end = size; |
| 339 | } |
| 340 | |
| 341 | for (int i = start; i < end; i++) |
| 342 | { |
| 343 | comps.push_back(m_comps[i]); |
| 344 | } |
| 345 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 346 | return Name(comps); |
| 347 | } |
| 348 | |
| 349 | ostream & |
| 350 | operator <<(ostream &os, const Name &name) |
| 351 | { |
| 352 | int size = name.size(); |
| 353 | vector<string> strComps; |
| 354 | for (int i = 0; i < size; i++) |
| 355 | { |
| 356 | strComps.push_back(name.getCompAsString(i)); |
| 357 | } |
| 358 | string joined = boost::algorithm::join(strComps, "/"); |
| 359 | os << "/" << joined; |
| 360 | return os; |
| 361 | } |
| 362 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 363 | bool |
| 364 | operator ==(const Name &n1, const Name &n2) |
| 365 | { |
| 366 | stringstream ss1(stringstream::out); |
| 367 | stringstream ss2(stringstream::out); |
| 368 | ss1 << n1; |
| 369 | ss2 << n2; |
| 370 | return ss1.str() == ss2.str(); |
| 371 | } |
| 372 | |
| 373 | bool |
| 374 | operator !=(const Name &n1, const Name &n2) |
| 375 | { |
| 376 | return !(n1 == n2); |
| 377 | } |
| 378 | |
| 379 | bool |
| 380 | operator <(const Name &n1, const Name &n2) |
| 381 | { |
| 382 | stringstream ss1(stringstream::out); |
| 383 | stringstream ss2(stringstream::out); |
| 384 | ss1 << n1; |
| 385 | ss2 << n2; |
| 386 | return ss1.str() < ss2.str(); |
| 387 | } |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 388 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 389 | |
| 390 | } // Ccnx |