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