blob: 591f1d314d325a0192b6aa59897b4f515619cad9 [file] [log] [blame]
Alexander Afanasyevf278db32013-01-21 14:41:01 -08001/* -*- 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 Zhu43eb2732012-12-28 00:48:26 -080022#include "ccnx-pco.h"
23
24namespace Ccnx {
25
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080026void
27ParsedContentObject::init(const unsigned char *data, size_t len)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080028{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080029 readRaw(m_bytes, data, len);
30
Zhenkai Zhu90611802013-01-04 21:30:24 -080031 m_comps = ccn_indexbuf_create();
Alexander Afanasyevf278db32013-01-21 14:41:01 -080032 int res = ccn_parse_ContentObject(head (m_bytes), len, &m_pco, m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080033 if (res < 0)
34 {
35 boost::throw_exception(MisformedContentObjectException());
36 }
Zhenkai Zhu79264a42013-02-07 21:49:42 -080037
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080038}
39
Zhenkai Zhu79264a42013-02-07 21:49:42 -080040ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080041 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080042 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080043{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080044 init(data, len);
45}
46
Zhenkai Zhu79264a42013-02-07 21:49:42 -080047ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080048 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080049 , m_verified(verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080050{
51 init(head(bytes), bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080052}
53
Zhenkai Zhu79264a42013-02-07 21:49:42 -080054ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080055 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080056 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080057{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080058 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080059}
60
61ParsedContentObject::~ParsedContentObject()
62{
Zhenkai Zhu90611802013-01-04 21:30:24 -080063 ccn_indexbuf_destroy(&m_comps);
64 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080065}
66
67Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080068ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080069{
Zhenkai Zhu90611802013-01-04 21:30:24 -080070 const unsigned char *content;
71 size_t len;
Zhenkai Zhu90611802013-01-04 21:30:24 -080072 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 Afanasyevf278db32013-01-21 14:41:01 -080078 Bytes bytes;
Zhenkai Zhu90611802013-01-04 21:30:24 -080079 readRaw(bytes, content, len);
80 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080081}
82
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080083BytesPtr
84ParsedContentObject::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 Zhucb2d0dd2013-01-03 14:10:48 -080097Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -080098ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080099{
Zhenkai Zhu90611802013-01-04 21:30:24 -0800100 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800101}
102
103}