blob: b66b184522d98979aabc562456dc48c87023882b [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"
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070023#include "ccnx-cert.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080024
25namespace Ccnx {
26
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080027void
28ParsedContentObject::init(const unsigned char *data, size_t len)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080029{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080030 readRaw(m_bytes, data, len);
31
Zhenkai Zhu90611802013-01-04 21:30:24 -080032 m_comps = ccn_indexbuf_create();
Alexander Afanasyevf278db32013-01-21 14:41:01 -080033 int res = ccn_parse_ContentObject(head (m_bytes), len, &m_pco, m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080034 if (res < 0)
35 {
36 boost::throw_exception(MisformedContentObjectException());
37 }
Zhenkai Zhu79264a42013-02-07 21:49:42 -080038
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080039}
40
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070041ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080042 : m_comps(NULL)
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 Zhud5d99be2013-03-13 19:15:56 -070048ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080049 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080050 , m_verified(verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080051{
52 init(head(bytes), bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080053}
54
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070055ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080056 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080057 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080058{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080059 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080060}
61
62ParsedContentObject::~ParsedContentObject()
63{
Zhenkai Zhu90611802013-01-04 21:30:24 -080064 ccn_indexbuf_destroy(&m_comps);
65 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080066}
67
68Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080069ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080070{
Zhenkai Zhu90611802013-01-04 21:30:24 -080071 const unsigned char *content;
72 size_t len;
Zhenkai Zhu90611802013-01-04 21:30:24 -080073 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
74 if (res < 0)
75 {
76 boost::throw_exception(MisformedContentObjectException());
77 }
78
Alexander Afanasyevf278db32013-01-21 14:41:01 -080079 Bytes bytes;
Zhenkai Zhu90611802013-01-04 21:30:24 -080080 readRaw(bytes, content, len);
81 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080082}
83
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080084BytesPtr
85ParsedContentObject::contentPtr() const
86{
87 const unsigned char *content;
88 size_t len;
89 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
90 if (res < 0)
91 {
92 boost::throw_exception(MisformedContentObjectException());
93 }
94
95 return readRawPtr (content, len);
96}
97
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080098Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -080099ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800100{
Zhenkai Zhu90611802013-01-04 21:30:24 -0800101 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800102}
103
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700104Name
105ParsedContentObject::keyName() const
106{
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700107 if (m_pco.offset[CCN_PCO_E_KeyName_Name] > m_pco.offset[CCN_PCO_B_KeyName_Name])
108 {
109 CcnxCharbufPtr ptr = boost::make_shared<CcnxCharbuf>();
110 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 -0700111
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700112 return Name(*ptr);
113 }
114 else
115 {
116 return Name();
117 }
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700118}
119
120HashPtr
121ParsedContentObject::publisherPublicKeyDigest() const
122{
123 const unsigned char *buf = NULL;
124 size_t size = 0;
125 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);
126
127 return boost::make_shared<Hash>(buf, size);
128}
129
130ParsedContentObject::Type
131ParsedContentObject::type() const
132{
133 switch (m_pco.type)
134 {
135 case CCN_CONTENT_DATA: return DATA;
136 case CCN_CONTENT_KEY: return KEY;
137 default: break;
138 }
139 return OTHER;
140}
141
Zhenkai Zhud5d99be2013-03-13 19:15:56 -0700142void
143ParsedContentObject::verifySignature(const CertPtr &cert)
144{
145 m_verified = (ccn_verify_signature(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, cert->pkey()) == 1);
146}
147
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800148}