blob: 8c2fd3fefb963651678a0d149b0af5c54ae638f7 [file] [log] [blame]
Zhenkai Zhu43eb2732012-12-28 00:48:26 -08001#include "ccnx-pco.h"
2
3namespace Ccnx {
4
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -08005void
6ParsedContentObject::init(const unsigned char *data, size_t len)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -08007{
Zhenkai Zhu90611802013-01-04 21:30:24 -08008 m_comps = ccn_indexbuf_create();
9 int res = ccn_parse_ContentObject(data, len, &m_pco, m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080010 if (res < 0)
11 {
12 boost::throw_exception(MisformedContentObjectException());
13 }
Zhenkai Zhu90611802013-01-04 21:30:24 -080014 readRaw(m_bytes, data, len);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080015}
16
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080017ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len)
18 : m_comps(NULL)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080019{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080020 init(data, len);
21}
22
23ParsedContentObject::ParsedContentObject(const Bytes &bytes)
24 : m_comps(NULL)
25{
26 init(head(bytes), bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080027}
28
29ParsedContentObject::ParsedContentObject(const ParsedContentObject &other)
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080030 : m_comps(NULL)
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080031{
Zhenkai Zhu9f2ef6f2013-01-04 21:46:08 -080032 init(head(other.m_bytes), other.m_bytes.size());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080033}
34
35ParsedContentObject::~ParsedContentObject()
36{
Zhenkai Zhu90611802013-01-04 21:30:24 -080037 ccn_indexbuf_destroy(&m_comps);
38 m_comps = NULL;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080039}
40
41Bytes
Zhenkai Zhubad089c2012-12-28 10:28:27 -080042ParsedContentObject::content() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080043{
Zhenkai Zhu90611802013-01-04 21:30:24 -080044 const unsigned char *content;
45 size_t len;
46 Bytes bytes;
47 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
48 if (res < 0)
49 {
50 boost::throw_exception(MisformedContentObjectException());
51 }
52
53 readRaw(bytes, content, len);
54 return bytes;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080055}
56
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080057BytesPtr
58ParsedContentObject::contentPtr() const
59{
60 const unsigned char *content;
61 size_t len;
62 int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
63 if (res < 0)
64 {
65 boost::throw_exception(MisformedContentObjectException());
66 }
67
68 return readRawPtr (content, len);
69}
70
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080071Name
Zhenkai Zhubad089c2012-12-28 10:28:27 -080072ParsedContentObject::name() const
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080073{
Zhenkai Zhu90611802013-01-04 21:30:24 -080074 return Name(head(m_bytes), m_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080075}
76
77}