blob: 700e1e5f82927c80e4e4da57329556abad34839a [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 Zhudd1f14d2013-03-13 12:04:28 -070040ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool integrityChecked, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080041 : m_comps(NULL)
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070042 , m_integrityChecked(integrityChecked)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080043 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080044{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080045 init(data, len);
46}
47
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070048ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool integrityChecked, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080049 : m_comps(NULL)
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070050 , m_integrityChecked(integrityChecked)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080051 , m_verified(verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080052{
53 init(head(bytes), bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080054}
55
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070056ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool integrityChecked, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080057 : m_comps(NULL)
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070058 , m_integrityChecked(integrityChecked)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080059 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080060{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080061 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080062}
63
64ParsedContentObject::~ParsedContentObject()
65{
Zhenkai Zhu90611802013-01-04 21:30:24 -080066 ccn_indexbuf_destroy(&m_comps);
67 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080068}
69
70Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080071ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080072{
Zhenkai Zhu90611802013-01-04 21:30:24 -080073 const unsigned char *content;
74 size_t len;
Zhenkai Zhu90611802013-01-04 21:30:24 -080075 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
76 if (res < 0)
77 {
78 boost::throw_exception(MisformedContentObjectException());
79 }
80
Alexander Afanasyevf278db32013-01-21 14:41:01 -080081 Bytes bytes;
Zhenkai Zhu90611802013-01-04 21:30:24 -080082 readRaw(bytes, content, len);
83 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080084}
85
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080086BytesPtr
87ParsedContentObject::contentPtr() const
88{
89 const unsigned char *content;
90 size_t len;
91 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
92 if (res < 0)
93 {
94 boost::throw_exception(MisformedContentObjectException());
95 }
96
97 return readRawPtr (content, len);
98}
99
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800100Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800101ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800102{
Zhenkai Zhu90611802013-01-04 21:30:24 -0800103 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800104}
105
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700106Name
107ParsedContentObject::keyName() const
108{
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700109 if (m_pco.offset[CCN_PCO_E_KeyName_Name] > m_pco.offset[CCN_PCO_B_KeyName_Name])
110 {
111 CcnxCharbufPtr ptr = boost::make_shared<CcnxCharbuf>();
112 ccn_charbuf_append(ptr->getBuf(), head(m_bytes) + m_pco.offset[CCN_PCO_B_KeyName_Name], m_pco.offset[CCN_PCO_E_KeyName_Name] - m_pco.offset[CCN_PCO_B_KeyName_Name]);
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700113
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700114 return Name(*ptr);
115 }
116 else
117 {
118 return Name();
119 }
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700120}
121
122HashPtr
123ParsedContentObject::publisherPublicKeyDigest() const
124{
125 const unsigned char *buf = NULL;
126 size_t size = 0;
127 ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest, head(m_bytes), m_pco.offset[CCN_PCO_B_PublisherPublicKeyDigest], m_pco.offset[CCN_PCO_E_PublisherPublicKeyDigest], &buf, &size);
128
129 return boost::make_shared<Hash>(buf, size);
130}
131
132ParsedContentObject::Type
133ParsedContentObject::type() const
134{
135 switch (m_pco.type)
136 {
137 case CCN_CONTENT_DATA: return DATA;
138 case CCN_CONTENT_KEY: return KEY;
139 default: break;
140 }
141 return OTHER;
142}
143
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800144}