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