blob: f039b548d3dd8119c99a37d6037396fdab5526df [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * BSD license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
13#include "pco.h"
14
15namespace ndn {
16
17void
18ParsedContentObject::init(const unsigned char *data, size_t len)
19{
20 readRaw(m_bytes, data, len);
21
22 m_comps = ccn_indexbuf_create();
23 int res = ccn_parse_ContentObject(head (m_bytes), len, &m_pco, m_comps);
24 if (res < 0)
25 {
26 boost::throw_exception(Error::MisformedContentObject());
27 }
28
29}
30
31ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool verified)
32 : m_comps(NULL)
33 , m_verified(verified)
34{
35 init(data, len);
36}
37
38ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool verified)
39 : m_comps(NULL)
40 , m_verified(verified)
41{
42 init(head(bytes), bytes.size());
43}
44
45ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool verified)
46 : m_comps(NULL)
47 , m_verified(verified)
48{
49 init(head(other.m_bytes), other.m_bytes.size());
50}
51
52ParsedContentObject::~ParsedContentObject()
53{
54 ccn_indexbuf_destroy(&m_comps);
55 m_comps = NULL;
56}
57
58Bytes
59ParsedContentObject::content() const
60{
61 const unsigned char *content;
62 size_t len;
63 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
64 if (res < 0)
65 {
66 boost::throw_exception(Error::MisformedContentObject());
67 }
68
69 Bytes bytes;
70 readRaw(bytes, content, len);
71 return bytes;
72}
73
74BytesPtr
75ParsedContentObject::contentPtr() const
76{
77 const unsigned char *content;
78 size_t len;
79 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
80 if (res < 0)
81 {
82 boost::throw_exception(Error::MisformedContentObject());
83 }
84
85 return readRawPtr (content, len);
86}
87
88Name
89ParsedContentObject::name() const
90{
91 return Name(head(m_bytes), m_comps);
92}
93
94Name
95ParsedContentObject::keyName() const
96{
97 if (m_pco.offset[CCN_PCO_E_KeyName_Name] > m_pco.offset[CCN_PCO_B_KeyName_Name])
98 {
99 CharbufPtr ptr = boost::make_shared<Charbuf>();
100 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]);
101
102 return Name(*ptr);
103 }
104 else
105 {
106 return Name();
107 }
108}
109
110HashPtr
111ParsedContentObject::publisherPublicKeyDigest() const
112{
113 const unsigned char *buf = NULL;
114 size_t size = 0;
115 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);
116
117 return boost::make_shared<Hash>(buf, size);
118}
119
120ParsedContentObject::Type
121ParsedContentObject::type() const
122{
123 switch (m_pco.type)
124 {
125 case CCN_CONTENT_DATA: return DATA;
126 case CCN_CONTENT_KEY: return KEY;
127 default: break;
128 }
129 return OTHER;
130}
131
132// void
133// ParsedContentObject::verifySignature(const CertPtr &cert)
134// {
135// m_verified = (ccn_verify_signature(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, cert->pkey()) == 1);
136// }
137
138}