blob: 01c78dad99db65bbabdf17b2df2442c6cc6fc2f9 [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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070022#include "ndnx-pco.h"
23#include "ndnx-cert.h"
24#include "hash-helper.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080025
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070026namespace Ndnx {
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080027
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080028void
29ParsedContentObject::init(const unsigned char *data, size_t len)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080030{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080031 readRaw(m_bytes, data, len);
32
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070033 m_comps = ndn_indexbuf_create();
34 int res = ndn_parse_ContentObject(head (m_bytes), len, &m_pco, m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080035 if (res < 0)
36 {
37 boost::throw_exception(MisformedContentObjectException());
38 }
Zhenkai Zhu79264a42013-02-07 21:49:42 -080039
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080040}
41
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070042ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080043 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080044 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080045{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080046 init(data, len);
47}
48
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070049ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080050 : m_comps(NULL)
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 Zhud5d99be2013-03-13 19:15:56 -070056ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool verified)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080057 : m_comps(NULL)
Zhenkai Zhu79264a42013-02-07 21:49:42 -080058 , m_verified(verified)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080059{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080060 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080061}
62
63ParsedContentObject::~ParsedContentObject()
64{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070065 ndn_indexbuf_destroy(&m_comps);
Zhenkai Zhu90611802013-01-04 21:30:24 -080066 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080067}
68
69Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080070ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080071{
Zhenkai Zhu90611802013-01-04 21:30:24 -080072 const unsigned char *content;
73 size_t len;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070074 int res = ndn_content_get_value(head(m_bytes), m_pco.offset[NDN_PCO_E], &m_pco, &content, &len);
Zhenkai Zhu90611802013-01-04 21:30:24 -080075 if (res < 0)
76 {
77 boost::throw_exception(MisformedContentObjectException());
78 }
79
Alexander Afanasyevf278db32013-01-21 14:41:01 -080080 Bytes bytes;
Zhenkai Zhu90611802013-01-04 21:30:24 -080081 readRaw(bytes, content, len);
82 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080083}
84
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080085BytesPtr
86ParsedContentObject::contentPtr() const
87{
88 const unsigned char *content;
89 size_t len;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070090 int res = ndn_content_get_value(head(m_bytes), m_pco.offset[NDN_PCO_E], &m_pco, &content, &len);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080091 if (res < 0)
92 {
93 boost::throw_exception(MisformedContentObjectException());
94 }
95
96 return readRawPtr (content, len);
97}
98
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080099Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800100ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800101{
Zhenkai Zhu90611802013-01-04 21:30:24 -0800102 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800103}
104
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700105Name
106ParsedContentObject::keyName() const
107{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700108 if (m_pco.offset[NDN_PCO_E_KeyName_Name] > m_pco.offset[NDN_PCO_B_KeyName_Name])
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700109 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700110 NdnxCharbufPtr ptr = boost::make_shared<NdnxCharbuf>();
111 ndn_charbuf_append(ptr->getBuf(), head(m_bytes) + m_pco.offset[NDN_PCO_B_KeyName_Name], m_pco.offset[NDN_PCO_E_KeyName_Name] - m_pco.offset[NDN_PCO_B_KeyName_Name]);
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700112
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700113 return Name(*ptr);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700114}
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700115 else
116 {
117 return Name();
118 }
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700119}
120
121HashPtr
122ParsedContentObject::publisherPublicKeyDigest() const
123{
124 const unsigned char *buf = NULL;
125 size_t size = 0;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700126 ndn_ref_tagged_BLOB(NDN_DTAG_PublisherPublicKeyDigest, head(m_bytes), m_pco.offset[NDN_PCO_B_PublisherPublicKeyDigest], m_pco.offset[NDN_PCO_E_PublisherPublicKeyDigest], &buf, &size);
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700127
128 return boost::make_shared<Hash>(buf, size);
129}
130
131ParsedContentObject::Type
132ParsedContentObject::type() const
133{
134 switch (m_pco.type)
135 {
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700136 case NDN_CONTENT_DATA: return DATA;
137 case NDN_CONTENT_KEY: return KEY;
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -0700138 default: break;
139 }
140 return OTHER;
141}
142
Zhenkai Zhud5d99be2013-03-13 19:15:56 -0700143void
144ParsedContentObject::verifySignature(const CertPtr &cert)
145{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700146 m_verified = (ndn_verify_signature(head(m_bytes), m_pco.offset[NDN_PCO_E], &m_pco, cert->pkey()) == 1);
Zhenkai Zhud5d99be2013-03-13 19:15:56 -0700147}
148
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800149}