OpenSDN source code
parse_object.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef ctrlplane_parse_object_h
7 #define ctrlplane_parse_object_h
8 #include <stdint.h>
9 #include <string>
10 #include <string.h>
11 #include <map>
12 #if defined(__linux__)
13 #include <byteswap.h>
14 #endif
15 
16 #ifdef __LITTLE_ENDIAN__
17 #define be64_to_host(data) bswap_64(data)
18 #define host_to_be64(data) bswap_64(data)
19 #else
20 #define be64_to_host(data) (data)
21 #define host_to_be64(data) (data)
22 #endif
23 
24 static inline uint32_t get_short(const uint8_t *data) {
25  uint32_t value = *data++;
26  value <<= 8;
27  value += *data;
28  return value;
29 }
30 
31 static inline uint64_t get_value_unaligned(const uint8_t *data, int size) {
32  uint64_t value = 0;
33  for (int i = 0; i < size; ++i) {
34  if (i) value <<= 8;
35  value += *data++;
36  }
37  return value;
38 }
39 
40 static inline uint64_t get_value(const uint8_t *data, int size) {
41  uint64_t value = -1;
42  if (size == 1) {
43  value = data[0];
44  } else if (size == 2) {
45  value = get_short(data);
46  } else if (size <= 8) {
47  value = 0;
48  for (int i = 0; i < size; ++i) {
49  if (i) value <<= 8;
50  value += *data++;
51  }
52  }
53  return value;
54 }
55 
56 static inline void put_value(uint8_t *data, int size, uint64_t value) {
57  for (int i = 0; i < size; i++) {
58  int offset = (size - (i + 1)) * 8;
59  if (offset >= (int)sizeof(value)*8) {
60  *data++ = 0;
61  } else {
62  *data++ = ((value >> offset) & 0xff);
63  }
64  }
65 }
66 
67 static inline double get_double(const uint8_t *data) {
68  uint64_t *pp = (uint64_t *)data;
69  uint64_t re = be64_to_host(*pp);
70  double *dp = (double *)&re;
71  return *dp;
72 }
73 
74 static inline void put_double(uint8_t *data, double value) {
75  uint64_t *pp = (uint64_t *)data;
76  union {
77  uint64_t u64;
78  double d;
79  } x;
80  x.d = value;
81  *pp = host_to_be64(x.u64);
82 }
83 
86  data_size(0) {}
89  std::string type_name;
90  const uint8_t *data;
91  int data_size;
92 };
93 
95 public:
96  void SaveOffset(std::string, int);
97  int FindOffset(const char *);
98  void ClearOffsets() { offsets_.clear(); }
99 private:
100  std::map<std::string, int> offsets_;
101 };
102 
103 class ParseObject {
104 public:
105  virtual ~ParseObject() { }
106 };
107 
108 #endif
void ClearOffsets()
Definition: parse_object.h:98
void SaveOffset(std::string, int)
std::map< std::string, int > offsets_
Definition: parse_object.h:100
virtual ~ParseObject()
Definition: parse_object.h:105
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
#define host_to_be64(data)
Definition: parse_object.h:21
static void put_double(uint8_t *data, double value)
Definition: parse_object.h:74
#define be64_to_host(data)
Definition: parse_object.h:20
static uint64_t get_value_unaligned(const uint8_t *data, int size)
Definition: parse_object.h:31
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
static double get_double(const uint8_t *data)
Definition: parse_object.h:67
static uint32_t get_short(const uint8_t *data)
Definition: parse_object.h:24
const uint8_t * data
Definition: parse_object.h:90
std::string type_name
Definition: parse_object.h:89