LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - parse_object.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 33 34 97.1 %
Date: 2026-11-16 02:06:09 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          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    10720598 : static inline uint32_t get_short(const uint8_t *data) {
      25    10720598 :     uint32_t value = *data++;
      26    10720598 :     value <<= 8;
      27    10720598 :     value += *data;
      28    10720598 :     return value;
      29             : }
      30             : 
      31        1280 : static inline uint64_t get_value_unaligned(const uint8_t *data, int size) {
      32        1280 :     uint64_t value = 0;
      33        6400 :     for (int i = 0; i < size; ++i) {
      34        5120 :         if (i) value <<= 8;
      35        5120 :         value += *data++;
      36             :     }
      37        1280 :     return value;
      38             : }
      39             : 
      40    19333393 : static inline uint64_t get_value(const uint8_t *data, int size) {
      41    19333393 :     uint64_t value = -1;
      42    19333393 :     if (size == 1) {
      43      609615 :         value = data[0];
      44    18723778 :     } else if (size == 2) {
      45    10710719 :         value = get_short(data);
      46     8013178 :     } else if (size <= 8) {
      47     8013619 :         value = 0;
      48    41025182 :         for (int i = 0; i < size; ++i) {
      49    33011563 :             if (i) value <<= 8;
      50    33011563 :             value += *data++;
      51             :         }
      52             :     }
      53    19333480 :     return value;
      54             : }
      55             : 
      56     5106965 : static inline void put_value(uint8_t *data, int size, uint64_t value) {
      57    19415081 :     for (int i = 0; i < size; i++) {
      58    14308116 :         int offset = (size - (i + 1)) * 8;
      59    14308116 :         if (offset >= (int)sizeof(value)*8) {
      60           0 :             *data++ = 0;
      61             :         } else {
      62    14308116 :             *data++ = ((value >> offset) & 0xff);
      63             :         }
      64             :     }
      65     5106965 : }
      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       39364 :     ParseErrorContext() : error_code(0), error_subcode(0), data(NULL),
      86       19682 :             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       10378 :     void ClearOffsets() { offsets_.clear(); }
      99             : private:
     100             :     std::map<std::string, int> offsets_;
     101             : };
     102             : 
     103             : class ParseObject {
     104             : public:
     105     7150155 :     virtual ~ParseObject() { }
     106             : };
     107             : 
     108             : #endif

Generated by: LCOV version 1.14