blob: 2c4e351df811dd52b78f394985d2e035b5a148bb [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 Zhu43eb2732012-12-28 00:48:26 -080037}
38
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080039ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len)
40 : m_comps(NULL)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080041{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080042 init(data, len);
43}
44
45ParsedContentObject::ParsedContentObject(const Bytes &bytes)
46 : m_comps(NULL)
47{
48 init(head(bytes), bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080049}
50
51ParsedContentObject::ParsedContentObject(const ParsedContentObject &other)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080052 : m_comps(NULL)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080053{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080054 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080055}
56
57ParsedContentObject::~ParsedContentObject()
58{
Zhenkai Zhu90611802013-01-04 21:30:24 -080059 ccn_indexbuf_destroy(&m_comps);
60 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080061}
62
63Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080064ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080065{
Zhenkai Zhu90611802013-01-04 21:30:24 -080066 const unsigned char *content;
67 size_t len;
Zhenkai Zhu90611802013-01-04 21:30:24 -080068 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 Afanasyevf278db32013-01-21 14:41:01 -080074 Bytes bytes;
Zhenkai Zhu90611802013-01-04 21:30:24 -080075 readRaw(bytes, content, len);
76 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080077}
78
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080079BytesPtr
80ParsedContentObject::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 Zhucb2d0dd2013-01-03 14:10:48 -080093Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -080094ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080095{
Zhenkai Zhu90611802013-01-04 21:30:24 -080096 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080097}
98
99}