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 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 15081326 : static inline uint32_t get_short(const uint8_t *data) { 25 15081326 : uint32_t value = *data++; 26 15081326 : value <<= 8; 27 15081326 : value += *data; 28 15081326 : return value; 29 : } 30 : 31 260146 : static inline uint64_t get_value_unaligned(const uint8_t *data, int size) { 32 260146 : uint64_t value = 0; 33 1300730 : for (int i = 0; i < size; ++i) { 34 1040584 : if (i) value <<= 8; 35 1040584 : value += *data++; 36 : } 37 260146 : return value; 38 : } 39 : 40 45689768 : static inline uint64_t get_value(const uint8_t *data, int size) { 41 45689768 : uint64_t value = -1; 42 45689768 : if (size == 1) { 43 18396139 : value = data[0]; 44 27293629 : } else if (size == 2) { 45 14885832 : value = get_short(data); 46 12408249 : } else if (size <= 8) { 47 12409232 : value = 0; 48 70560217 : for (int i = 0; i < size; ++i) { 49 58150985 : if (i) value <<= 8; 50 58150985 : value += *data++; 51 : } 52 : } 53 45689821 : return value; 54 : } 55 : 56 27656882 : static inline void put_value(uint8_t *data, int size, uint64_t value) { 57 85322823 : for (int i = 0; i < size; i++) { 58 57665941 : int offset = (size - (i + 1)) * 8; 59 57665941 : if (offset >= (int)sizeof(value)*8) { 60 0 : *data++ = 0; 61 : } else { 62 57665941 : *data++ = ((value >> offset) & 0xff); 63 : } 64 : } 65 27656882 : } 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 : 84 : struct ParseErrorContext { 85 765906 : ParseErrorContext() : error_code(0), error_subcode(0), data(NULL), 86 382950 : data_size(0) {} 87 : int error_code; 88 : int error_subcode; 89 : std::string type_name; 90 : const uint8_t *data; 91 : int data_size; 92 : }; 93 : 94 : class EncodeOffsets { 95 : public: 96 : void SaveOffset(std::string, int); 97 : int FindOffset(const char *); 98 155291 : void ClearOffsets() { offsets_.clear(); } 99 : private: 100 : std::map<std::string, int> offsets_; 101 : }; 102 : 103 : class ParseObject { 104 : public: 105 8419962 : virtual ~ParseObject() { } 106 : }; 107 : 108 : #endif