Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #ifndef __BASE_PROTO_IMPL_H__
7 : #define __BASE_PROTO_IMPL_H__
8 :
9 : #include <vector>
10 :
11 : namespace detail {
12 :
13 : extern bool debug_;
14 : #define PROTO_DEBUG(args...) if (detail::debug_) LOG(DEBUG, ##args)
15 :
16 : template <typename T>
17 : struct ApplySetter {
18 : template <typename U>
19 85173 : void operator()(const uint8_t *data, int element_size, U *obj) {
20 85173 : int value = get_value(data, element_size);
21 85175 : T::set(obj, value);
22 85175 : }
23 : };
24 :
25 : template <>
26 : struct ApplySetter<void> {
27 40961 : void operator()(const uint8_t *data, int element_size, void *obj) {
28 40961 : }
29 : };
30 :
31 : template <typename T>
32 : struct ApplyGetter {
33 : template <typename U>
34 95534 : void operator()(uint8_t *data, int element_size, U *obj) {
35 95534 : uint64_t value = T::get(obj);
36 95562 : put_value(data, element_size, value);
37 95565 : }
38 : };
39 : template <>
40 : struct ApplyGetter<void> {
41 : template <typename U>
42 27509 : void operator()(uint8_t *data, int element_size, U *obj) {
43 27509 : memset(data, 0, element_size);
44 27509 : }
45 : };
46 :
47 : template <typename Accessor, typename T>
48 : struct VariableLengthWriter {
49 : template <typename Iterator>
50 36663 : int Copy(Iterator begin, Iterator end, uint8_t *data) {
51 36663 : int count = 0;
52 499360 : for (Iterator iter = begin; iter != end; ++iter) {
53 462652 : put_value(data, sizeof(*iter), *iter);
54 462694 : data += sizeof(*iter);
55 462694 : count+= sizeof(*iter);
56 : }
57 36678 : return count;
58 : }
59 36660 : int operator()(uint8_t *data, int element_size, T *obj) {
60 36660 : int count = Copy(Accessor::begin(obj), Accessor::end(obj), data);
61 36678 : return count;
62 : }
63 : };
64 : template <typename T>
65 : struct VariableLengthWriter<void, T> {
66 35919 : int operator()(uint8_t *data, int element_size, T *obj) {
67 35919 : return 0;
68 : }
69 : };
70 :
71 : template <typename P, typename C>
72 : struct ContextPush {
73 61914 : C * operator()(ParseContext *context, P *obj) {
74 61914 : C *child_obj = new C;
75 61913 : context->Push(child_obj);
76 61912 : return child_obj;
77 : }
78 : };
79 : template <typename P>
80 : struct ContextPush<P, void> {
81 17386 : P *operator()(ParseContext *context, P *obj) {
82 17386 : context->ReleaseData();
83 17386 : context->Push(obj);
84 17387 : return obj;
85 : }
86 : };
87 :
88 : template <>
89 : struct ContextPush<void, void> {
90 9841 : void *operator()(ParseContext *context, void *obj) {
91 9841 : return obj;
92 : }
93 : };
94 : template <typename T>
95 : struct NoContextPush {
96 32854 : T *operator()(ParseContext *context, T *obj) {
97 32854 : return obj;
98 : }
99 : };
100 :
101 : template<typename T, typename ChildContextType>
102 : struct StoreContext {
103 : template <typename C>
104 52073 : void operator()(C *obj, ParseObject *context_obj) {
105 52073 : ChildContextType *child_obj =
106 52073 : dynamic_cast<ChildContextType *>(context_obj);
107 52073 : assert(child_obj);
108 52073 : T::insert(obj, child_obj);
109 52074 : }
110 : };
111 :
112 : template <typename ChildContextType>
113 : struct StoreContext<void, ChildContextType> {
114 9839 : void operator()(void *obj, ParseObject *child_obj) {
115 9839 : delete child_obj;
116 9839 : }
117 : };
118 :
119 : template <typename Parent, typename T, typename ChildContextType>
120 : struct ContextPop {
121 61914 : void operator()(ParseContext *context, T *obj) {
122 : StoreContext<typename Parent::ContextStorer, ChildContextType> storer;
123 61914 : storer(obj, context->Pop());
124 61913 : }
125 : };
126 :
127 : template<typename Parent, typename T>
128 : struct ContextPop<Parent, T, void> {
129 17386 : void operator()(ParseContext *context, void *obj) {
130 17386 : context->SwapData(context->Pop());
131 17382 : }
132 : };
133 :
134 : template<typename Parent>
135 : struct ContextPop<Parent, void, void> {
136 371667 : void operator()(ParseContext *context, void *obj) {
137 371667 : }
138 : };
139 :
140 : // If the child's ContextType is void the obj is copied to child_obj.
141 : template <typename Child, typename T, typename opt_ctx_t>
142 : struct DescendentContextPush {
143 9841 : opt_ctx_t *operator()(ParseContext *context, T *obj) {
144 : ContextPush<T, opt_ctx_t> pushfn;
145 19681 : return pushfn(context, obj);
146 : }
147 : };
148 :
149 : template <typename Child, typename T>
150 : struct DescendentContextPush<Child, T, void>
151 : {
152 : typedef T ctx_t;
153 : typedef void pctx_t;
154 296129 : T *operator()(ParseContext *context, T *obj) {
155 296129 : return obj;
156 : }
157 : };
158 : template <typename Child>
159 : struct DescendentContextSwap {
160 : typedef typename Child::ContextType ctx_t;
161 : template <typename U>
162 32855 : ctx_t *operator()(ParseContext *context, U *obj) {
163 : typedef typename Child::ContextSwap swap_t;
164 32855 : ctx_t *nobj = swap_t()(obj);
165 : if (nobj == NULL) {
166 : // TODO:
167 : }
168 32855 : if (nobj != obj) {
169 32855 : context->SwapData(nobj);
170 : }
171 32855 : return nobj;
172 : }
173 : };
174 :
175 : template <class Parent, typename Child>
176 : struct DescendentParser {
177 : template <typename T>
178 338825 : static int Parse(const uint8_t *data, size_t size, ParseContext *context,
179 : T *obj) {
180 : // If the child defines ContextType, this changes the type of the
181 : // top of stack element and causes us to push a new context into the
182 : // stack.
183 : // Context push and pop is only relevant if the child doesn't
184 : // define a context swap. If it does, the swap operation will
185 : // replace the existing stack frame data object.
186 : typedef typename Child::ContextType opt_ctx_t;
187 :
188 : typedef typename boost::mpl::if_<
189 : boost::is_same<opt_ctx_t, void>,
190 : T, opt_ctx_t>::type ctx_t;
191 :
192 : typedef typename boost::mpl::if_<
193 : boost::is_same<typename Child::ContextSwap, void>,
194 : DescendentContextPush<Child, T, typename Child::ContextType>,
195 : DescendentContextSwap<Child> >::type context_op_t;
196 :
197 338825 : ctx_t *child_obj = context_op_t()(context, obj);
198 :
199 338824 : int result = Child::Parse(data, size, context, child_obj);
200 338818 : if (result < 0) return result;
201 : typedef typename boost::mpl::if_<
202 : boost::is_same<typename Child::ContextSwap, void>,
203 : opt_ctx_t,
204 : void>::type push_ctx_t;
205 : typedef typename boost::mpl::if_<
206 : boost::is_same<push_ctx_t, void>,
207 : void, T>::type pctx_t;
208 : ContextPop<Parent, pctx_t, push_ctx_t> popfn;
209 338818 : popfn(context, obj);
210 338817 : return result;
211 : }
212 : };
213 :
214 : template <typename T, typename Context>
215 : struct SequenceLengthSetter {
216 49261 : int operator()(ParseContext *context, const uint8_t *data, size_t size,
217 : size_t msgsize, Context *obj) {
218 : T t;
219 49261 : int value = t(obj, data, size);
220 49261 : context->set_lensize(size);
221 49263 : if ((size_t) value > msgsize) {
222 0 : PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
223 : << value << " > " << msgsize);
224 0 : return -1;
225 : }
226 49263 : context->set_size(value);
227 49263 : return 0;
228 : }
229 : };
230 :
231 : template <typename Context>
232 : struct SequenceLengthSetter<int, Context> {
233 9553 : int operator()(ParseContext *context, const uint8_t *data, size_t size,
234 : size_t msgsize, Context *obj) {
235 9553 : int value = get_value(data, size);
236 9553 : context->set_lensize(size);
237 9553 : if ((size_t) value > msgsize) {
238 0 : PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
239 : << value << " > " << msgsize);
240 0 : return -1;
241 : }
242 9553 : context->set_size(value);
243 9553 : return 0;
244 : }
245 : };
246 :
247 : template <typename Context>
248 : struct SequenceLengthSetter<void, Context> {
249 132293 : int operator()(ParseContext *context, const uint8_t *data, size_t size,
250 : size_t msgsize, Context *obj) {
251 132293 : return 0;
252 : }
253 : };
254 :
255 : template<typename T>
256 : struct SequenceLengthAddCallback {
257 52227 : void operator()(EncodeContext::CallbackType cb, EncodeContext *context,
258 : uint8_t *data, int arg) {
259 52227 : context->AddCallback(cb, data, arg);
260 52120 : }
261 : };
262 : template<>
263 : struct SequenceLengthAddCallback<void> {
264 162019 : void operator()(EncodeContext::CallbackType cb, EncodeContext *context,
265 : uint8_t *data, int arg) {
266 162019 : }
267 : };
268 :
269 : template<typename Accessor, typename T>
270 : struct VariableLengthSetter {
271 32122 : int operator()(const uint8_t *data, size_t size, ParseContext *context,
272 : T *obj) {
273 32122 : int element_size = context->size();
274 : // If element size is unknown, read till the end of buffer
275 32122 : if (element_size < 0) element_size = size;
276 31998 : else if (size < (size_t) element_size) {
277 0 : PROTO_DEBUG(TYPE_NAME(T) << " Variable Length Setter failed "
278 : << size << " < " << element_size);
279 0 : return -1;
280 : }
281 32122 : Accessor::set(obj, data, element_size);
282 32119 : return element_size;
283 : }
284 : };
285 : template<typename T>
286 : struct VariableLengthSetter<void, T> {
287 : int operator()(const uint8_t *data, size_t size, ParseContext *context,
288 : T *obj) {
289 : return 0;
290 : }
291 : };
292 :
293 : template<typename SizeSetter, typename T>
294 : struct LengthSizeSetter {
295 32855 : int operator()(const uint8_t *data, size_t size, ParseContext *context,
296 : T *obj) {
297 32855 : return SizeSetter::get(obj);
298 : }
299 : };
300 :
301 : template<typename Derived, typename T>
302 : struct FixedLengthSetter {
303 126134 : int operator()(const uint8_t *data, size_t size, ParseContext *context,
304 : T *obj) {
305 : typedef typename Derived::Setter setter_t;
306 : detail::ApplySetter<setter_t> setter;
307 126134 : setter(data, Derived::kSize, obj);
308 126136 : return Derived::kSize;
309 : }
310 : };
311 :
312 : template<typename Derived, typename T>
313 : struct NopSetter {
314 0 : int operator()(const uint8_t *data, size_t size, ParseContext *context,
315 : T *obj) {
316 0 : return Derived::kSize;
317 : }
318 : };
319 :
320 : template <typename Derived>
321 : struct SizeComparer {
322 126134 : bool operator()(int size) const {
323 126134 : return size >= Derived::kSize;
324 : }
325 : };
326 :
327 : struct NopComparer {
328 64972 : bool operator()(int size) const {
329 64972 : return true;
330 : }
331 : };
332 :
333 : template <typename Setter, typename T>
334 : struct VarLengthSizeValue {
335 36654 : static int get(const T *msg) {
336 36654 : return Setter::size(msg);
337 : }
338 : };
339 : template <typename T>
340 : struct VarLengthSizeValue<void, T> {
341 : static int get(const T *msg) {
342 : return 0;
343 : }
344 : };
345 : template <typename Derived>
346 : struct FixedLengthSizeValue {
347 141925 : static int get(const void *msg) {
348 141925 : return Derived::kSize;
349 : }
350 : };
351 :
352 : template <typename T>
353 : struct AddCallback {
354 18684 : void operator()(EncodeContext *context, uint8_t *data, int arg) {
355 18684 : context->AddCallback(&T::Callback, data, arg);
356 18694 : }
357 : };
358 : template <>
359 : struct AddCallback<void>{
360 195210 : void operator()(EncodeContext *context, uint8_t *data, int arg) {
361 195210 : }
362 : };
363 :
364 : template <typename ContextAccessor>
365 : struct ContextElementType {
366 : typedef typename ContextAccessor::ValueType ValueType;
367 : };
368 : template <>
369 : struct ContextElementType<void> {
370 : typedef void ValueType;
371 : };
372 :
373 : template <typename Accessor>
374 : struct ContextIterator {
375 : typedef typename Accessor::ValueType ValueType;
376 : template <typename Obj>
377 54242 : ContextIterator(const Obj *obj)
378 54242 : : iter(accessor.begin(obj)) {
379 :
380 54248 : }
381 :
382 58612 : ValueType * Next() {
383 58612 : ValueType *obj = *iter;
384 58611 : ++iter;
385 58609 : return obj;
386 : }
387 :
388 : template <typename Obj>
389 112778 : bool HasNext(Obj *obj) const {
390 112778 : return (iter != accessor.end(obj));
391 : }
392 :
393 : private:
394 : Accessor accessor;
395 : typename Accessor::CollectionType::const_iterator iter;
396 : };
397 : template <>
398 : struct ContextIterator<void> {
399 : ContextIterator(void *obj) {
400 : }
401 : void * Next() { return NULL; }
402 : bool HasNext(void *obj) const { return false; }
403 : };
404 :
405 : template <typename T>
406 : struct SaveOffset {
407 32612 : void operator()(EncodeContext *ctx) {
408 32612 : ctx->SaveOffset(T()());
409 32680 : }
410 : };
411 :
412 : template <>
413 : struct SaveOffset<void> {
414 301741 : void operator()(EncodeContext *ctx) { }
415 : };
416 :
417 : } // detail
418 :
419 :
420 : #endif
|