blob: 065cfd63be4a7a9601208e8e44fd9c5be5552daa [file] [log] [blame]
Jeff Thompson958bf9b2013-10-12 17:20:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_DER_HPP
10#define NDN_DER_HPP
11
12#include <vector>
13#include <string>
14#include <istream>
15#include <ostream>
16#include <sstream>
17
18#include <ndn-cpp/common.hpp>
19#include <ndn-cpp/encoding/oid.hpp>
20
21#include "visitor/visitor.hpp"
22#include "visitor/void-visitor.hpp"
23#include "visitor/no-arguments-visitor.hpp"
24#include "visitor/void-no-arguments-visitor.hpp"
25
26namespace ndn {
27
Jeff Thompson958bf9b2013-10-12 17:20:51 -070028namespace der {
29
30enum DerType {
31 DER_EOC = 0,
32 DER_BOOLEAN = 1,
33 DER_INTEGER = 2,
34 DER_BIT_STRING = 3,
35 DER_OCTET_STRING = 4,
36 DER_NULL = 5,
37 DER_OBJECT_IDENTIFIER = 6,
38 DER_OBJECT_DESCRIPTOR = 7,
39 DER_EXTERNAL = 40,
40 DER_REAL = 9,
41 DER_ENUMERATED = 10,
42 DER_EMBEDDED_PDV = 43,
43 DER_UTF8_STRING = 12,
44 DER_RELATIVE_OID = 13,
45 DER_SEQUENCE = 48,
46 DER_SET = 49,
47 DER_NUMERIC_STRING = 18,
48 DER_PRINTABLE_STRING = 19,
49 DER_T61_STRING = 20,
50 DER_VIDEOTEX_STRING = 21,
51 DER_IA5_STRING = 22,
52 DER_UTC_TIME = 23,
53 DER_GENERALIZED_TIME = 24,
54 DER_GRAPHIC_STRING = 25,
55 DER_VISIBLE_STRING = 26,
56 DER_GENERAL_STRING = 27,
57 DER_UNIVERSAL_STRING = 28,
58 DER_CHARACTER_STRING = 29,
59 DER_BMP_STRING = 30,
60};
61
62class DerComplex;
63
64class DerNode
65{
66public:
67 DerNode();
68
69 DerNode(DerType type);
70
71 DerNode(std::istream& start);
72
73 virtual
74 ~DerNode();
75
76 virtual int
77 getSize() { return m_header.size() + m_payload.size(); }
78
79 virtual void
80 encode(std::ostream& start);
81
82 void
83 setParent(DerComplex * parent) { m_parent = parent; }
84
85 static ptr_lib::shared_ptr<DerNode>
86 parse(std::istream& start);
87
88 const std::vector<uint8_t>&
89 getHeader() const { return m_header; }
90
91 std::vector<uint8_t>&
92 getHeader() { return m_header; }
93
94 const std::vector<uint8_t>&
95 getPayload() const { return m_payload; }
96
97 std::vector<uint8_t>&
98 getPayload() { return m_payload; }
99
100 const DerType&
101 getType() { return m_type; }
102
103 virtual ptr_lib::shared_ptr<std::vector<uint8_t> >
104 getRaw()
105 {
106 ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>());
107 blob->insert(blob->end(), m_header.begin(), m_header.end());
108 blob->insert(blob->end(), m_payload.begin(), m_payload.end());
109
110 return blob;
111 }
112
113 virtual void accept(VoidNoArgumentsVisitor& visitor) = 0;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700114 virtual void accept(VoidVisitor& visitor, ndnboost::any param) = 0;
115 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) = 0;
116 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) = 0;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700117
118protected:
119 void
120 decode(std::istream& start);
121
122 void
123 encodeHeader(int size);
124
125 int
126 decodeHeader(std::istream& start);
127
128protected:
129 DerType m_type;
130 std::vector<uint8_t> m_header;
131 std::vector<uint8_t> m_payload;
132 DerComplex * m_parent;
133};
134
135
136typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList;
137
138class DerComplex : public DerNode
139{
140public:
141 DerComplex();
142
143 DerComplex(DerType type);
144
145 DerComplex(std::istream& start);
146
147 virtual
148 ~DerComplex();
149
150 virtual int
151 getSize();
152
153 void
154 addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true);
155
156 virtual void
157 encode(std::ostream& start);
158
159 const DerNodePtrList&
160 getChildren() const { return m_nodeList; }
161
162 DerNodePtrList&
163 getChildren() { return m_nodeList; }
164
165 virtual ptr_lib::shared_ptr<std::vector<uint8_t> >
166 getRaw();
167
168private:
169 void
170 updateSize();
171
172 void
173 setChildChanged();
174
175private:
176 bool m_childChanged;
177 int m_size;
178 DerNodePtrList m_nodeList;
179};
180
181
182
183class DerByteString : public DerNode
184{
185public:
186 DerByteString(const std::string& str, DerType type);
187
188 DerByteString(const std::vector<uint8_t>& blob, DerType type);
189
190 DerByteString(std::istream& start);
191
192 virtual
193 ~DerByteString();
194};
195
196
197//0x01
198class DerBool : public DerNode
199{
200public:
201 DerBool(bool value);
202
203 DerBool(std::istream& start);
204
205 virtual
206 ~DerBool();
207
208 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700209 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
210 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
211 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700212
213};
214
215//0x02
216class DerInteger : public DerNode
217{
218public:
219 DerInteger(const std::vector<uint8_t>& blob);
220
221 DerInteger(std::istream& start);
222
223 virtual
224 ~DerInteger();
225
226 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700227 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
228 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
229 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700230
231};
232
233//0x03
234class DerBitString : public DerNode
235{
236public:
237 DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen);
238
239 DerBitString(std::istream& start);
240
241 virtual
242 ~DerBitString();
243
244 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700245 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
246 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
247 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700248
249};
250
251//0x04
252class DerOctetString : public DerByteString
253{
254public:
255 DerOctetString(const std::string& str);
256
257 DerOctetString(const std::vector<uint8_t>& blob);
258
259 DerOctetString(std::istream& start);
260
261 virtual
262 ~DerOctetString();
263
264 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700265 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
266 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
267 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700268
269};
270
271
272//0x05
273class DerNull : public DerNode
274{
275public:
276 DerNull();
277
278 DerNull(std::istream& start);
279
280 virtual
281 ~DerNull();
282
283 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700284 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
285 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
286 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700287
288};
289
290
291//0x06
292class DerOid : public DerNode
293{
294public:
295 DerOid(const OID& oid);
296
297 DerOid(const std::string& oidStr);
298
299 DerOid(const std::vector<int>& value);
300
301 DerOid(std::istream& start);
302
303 virtual
304 ~DerOid();
305
306 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700307 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
308 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
309 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700310
311 int
312 decode128(int& offset);
313
314private:
315 void
316 prepareEncoding(const std::vector<int>& value);
317
318 void
319 encode128(int value, std::ostringstream& os);
320};
321
322
323//0x10
324class DerSequence : public DerComplex
325{
326public:
327 DerSequence();
328
329 DerSequence(std::istream& start);
330
331 virtual
332 ~DerSequence();
333
334 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700335 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
336 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
337 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700338
339};
340
341//0x13
342class DerPrintableString : public DerByteString
343{
344public:
345 DerPrintableString(const std::string& str);
346
347 DerPrintableString(const std::vector<uint8_t>& blob);
348
349 DerPrintableString(std::istream& start);
350
351 virtual
352 ~DerPrintableString();
353
354 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700355 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
356 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
357 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700358};
359
360//0x1b
361class DerGtime : public DerNode
362{
363public:
364 DerGtime(const Time& time);
365
366 DerGtime(std::istream& start);
367
368 virtual
369 ~DerGtime();
370
371 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700372 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
373 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
374 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700375
376};
377
378} // der
379
380}
381
382#endif