Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -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 | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 22 | #include "ccnx-pco.h" |
| 23 | |
| 24 | namespace Ccnx { |
| 25 | |
Zhenkai Zhu | 9f2ef6f | 2013-01-04 21:46:08 -0800 | [diff] [blame] | 26 | void |
| 27 | ParsedContentObject::init(const unsigned char *data, size_t len) |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 28 | { |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 29 | readRaw(m_bytes, data, len); |
| 30 | |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 31 | m_comps = ccn_indexbuf_create(); |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 32 | int res = ccn_parse_ContentObject(head (m_bytes), len, &m_pco, m_comps); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 33 | if (res < 0) |
| 34 | { |
| 35 | boost::throw_exception(MisformedContentObjectException()); |
| 36 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Zhenkai Zhu | 9f2ef6f | 2013-01-04 21:46:08 -0800 | [diff] [blame] | 39 | ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len) |
| 40 | : m_comps(NULL) |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 41 | { |
Zhenkai Zhu | 9f2ef6f | 2013-01-04 21:46:08 -0800 | [diff] [blame] | 42 | init(data, len); |
| 43 | } |
| 44 | |
| 45 | ParsedContentObject::ParsedContentObject(const Bytes &bytes) |
| 46 | : m_comps(NULL) |
| 47 | { |
| 48 | init(head(bytes), bytes.size()); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | ParsedContentObject::ParsedContentObject(const ParsedContentObject &other) |
Zhenkai Zhu | 9f2ef6f | 2013-01-04 21:46:08 -0800 | [diff] [blame] | 52 | : m_comps(NULL) |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 53 | { |
Zhenkai Zhu | 9f2ef6f | 2013-01-04 21:46:08 -0800 | [diff] [blame] | 54 | init(head(other.m_bytes), other.m_bytes.size()); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | ParsedContentObject::~ParsedContentObject() |
| 58 | { |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 59 | ccn_indexbuf_destroy(&m_comps); |
| 60 | m_comps = NULL; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Bytes |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 64 | ParsedContentObject::content() const |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 65 | { |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 66 | const unsigned char *content; |
| 67 | size_t len; |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 68 | int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len); |
| 69 | if (res < 0) |
| 70 | { |
| 71 | boost::throw_exception(MisformedContentObjectException()); |
| 72 | } |
| 73 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 74 | Bytes bytes; |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 75 | readRaw(bytes, content, len); |
| 76 | return bytes; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 79 | BytesPtr |
| 80 | ParsedContentObject::contentPtr() const |
| 81 | { |
| 82 | const unsigned char *content; |
| 83 | size_t len; |
| 84 | int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len); |
| 85 | if (res < 0) |
| 86 | { |
| 87 | boost::throw_exception(MisformedContentObjectException()); |
| 88 | } |
| 89 | |
| 90 | return readRawPtr (content, len); |
| 91 | } |
| 92 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 93 | Name |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 94 | ParsedContentObject::name() const |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 95 | { |
Zhenkai Zhu | 9061180 | 2013-01-04 21:30:24 -0800 | [diff] [blame] | 96 | return Name(head(m_bytes), m_comps); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } |