Line data Source code
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 15091734 : static inline uint32_t get_short(const uint8_t *data) { 24 15091734 : uint32_t value = *data++; 25 15091734 : value <<= 8; 26 15091734 : value += *data; 27 15091734 : return value; 28 : } 29 : 30 260140 : static inline uint64_t get_value_unaligned(const uint8_t *data, int size) { 31 260140 : uint64_t value = 0; 32 1300708 : for (int i = 0; i < size; ++i) { 33 1040568 : if (i) value <<= 8; 34 1040568 : value += *data++; 35 : } 36 260140 : return value; 37 : } 38 : 39 45776344 : static inline uint64_t get_value(const uint8_t *data, int size) { 40 45776344 : uint64_t value = -1; 41 45776344 : if (size == 1) { 42 18483027 : value = data[0]; 43 27293317 : } else if (size == 2) { 44 14892964 : value = get_short(data); 45 12400650 : } else if (size <= 8) { 46 12401258 : value = 0; 47 70529596 : for (int i = 0; i < size; ++i) { 48 58128338 : if (i) value <<= 8; 49 58128338 : value += *data++; 50 : } 51 : } 52 45776372 : return value; 53 : } 54 : 55 27827696 : static inline void put_value(uint8_t *data, int size, uint64_t value) { 56 85727785 : for (int i = 0; i < size; i++) { 57 57900089 : int offset = (size - (i + 1)) * 8; 58 57900089 : if (offset >= (int)sizeof(value)*8) { 59 0 : *data++ = 0; 60 : } else { 61 57900089 : *data++ = ((value >> offset) & 0xff); 62 : } 63 : } 64 27827696 : } 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 : 83 : struct ParseErrorContext { 84 779090 : ParseErrorContext() : error_code(0), error_subcode(0), data(NULL), 85 389545 : data_size(0) {} 86 : int error_code; 87 : int error_subcode; 88 : std::string type_name; 89 : const uint8_t *data; 90 : int data_size; 91 : }; 92 : 93 : class EncodeOffsets { 94 : public: 95 : void SaveOffset(std::string, int); 96 : int FindOffset(const char *); 97 156794 : void ClearOffsets() { offsets_.clear(); } 98 : private: 99 : std::map<std::string, int> offsets_; 100 : }; 101 : 102 : class ParseObject { 103 : public: 104 8449945 : virtual ~ParseObject() { } 105 : }; 106 : 107 : #endif