LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - proto.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 133 144 92.4 %
Date: 2026-11-30 02:00:48 Functions: 28 29 96.6 %
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             : #include "base/proto.h"
       7             : 
       8             : #include <boost/bind/bind.hpp>
       9             : 
      10             : using namespace std;
      11             : using namespace boost::placeholders;
      12             : 
      13             : namespace detail {
      14             :     bool debug_ = false;
      15             : }
      16             : 
      17             : struct ParseContext::StackFrame {
      18     2656103 :     StackFrame() : offset(0), lensize(0), size(-1), total_size(-1) {
      19     2656056 :     }
      20             :     int offset; // offset of the data pointer at present
      21             :     int lensize; //size of the length of the current element being parsed
      22             :     size_t size;
      23             :     size_t total_size;
      24             :     std::unique_ptr<ParseObject> data;
      25             : };
      26             : 
      27      198936 : ParseContext::ParseContext()
      28      198936 :     : offset_(0) {
      29      198935 : }
      30             : 
      31             : struct deleter {
      32             :     template <typename T>
      33      199290 :     void operator()(T *ptr) {
      34      199290 :         delete ptr;
      35      199304 :     }
      36             : };
      37             : 
      38      198905 : ParseContext::~ParseContext() {
      39      198905 :     for_each(stack_.begin(), stack_.end(), deleter());
      40      198902 :     stack_.clear();
      41      198903 : }
      42             : 
      43      191370 : ParseObject *ParseContext::release() {
      44      191370 :     if (stack_.empty()) {
      45           0 :         return NULL;
      46             :     }
      47      191370 :     StackFrame *current = stack_.back();
      48      191368 :     return current->data.release();
      49             : }
      50             : 
      51     2656045 : void ParseContext::Push(ParseObject *data) {
      52     2656045 :     StackFrame *frame = new StackFrame();
      53     2656064 :     frame->data.reset(data);
      54     2655950 :     stack_.push_back(frame);
      55     2655942 : }
      56             : 
      57     2647888 : ParseObject *ParseContext::Pop() {
      58     2647888 :     if (stack_.size() <= 1) {
      59      191357 :         return NULL;
      60             :     }
      61     2456523 :     StackFrame *frame = stack_.back();
      62     2456523 :     ParseObject *obj = frame->data.release();
      63     2456482 :     stack_.pop_back();
      64     2456490 :     delete frame;
      65     2456575 :     return obj;
      66             : }
      67             : 
      68      661804 : void ParseContext::ReleaseData() {
      69      661804 :     if (!stack_.empty()) {
      70      661785 :         StackFrame *frame = stack_.back();
      71      661773 :         frame->data.release();
      72             :     }
      73      661769 : }
      74     1292023 : void ParseContext::SwapData(ParseObject *obj) {
      75     1292023 :     if (!stack_.empty() && obj) {
      76     1291994 :         StackFrame *frame = stack_.back();
      77     1291982 :         frame->data.reset(obj);
      78             :     } else {
      79           0 :         delete obj;
      80             :     }
      81     1291985 : }
      82             : 
      83           0 : ParseObject *ParseContext::data() {
      84           0 :     if (stack_.empty()) {
      85           0 :         return NULL;
      86             :     }
      87           0 :     StackFrame *current = stack_.back();
      88           0 :     return current->data.get();
      89             : }
      90             : 
      91     9265373 : void ParseContext::advance(int delta) {
      92     9265373 :     if (!stack_.empty()) {
      93     8676868 :         stack_.back()->offset += delta;
      94             :     }
      95     9264005 :     offset_ += delta;
      96     9264005 : }
      97             : 
      98     2157000 : void ParseContext::set_lensize(int length) {
      99     2157000 :     if (!stack_.empty()) {
     100     2156965 :         StackFrame *current = stack_.back();
     101     2156936 :         current->lensize = length;
     102             :     }
     103     2156936 : }
     104             : 
     105     3281324 : int ParseContext::lensize() const {
     106     3281324 :     if (stack_.empty()) {
     107           0 :         return -1;
     108             :     }
     109     3281237 :     StackFrame *current = stack_.back();
     110     3281197 :     return current->lensize;
     111             : }
     112             : 
     113     7269281 : void ParseContext::set_size(size_t length) {
     114     7269281 :     if (!stack_.empty()) {
     115     6876250 :         StackFrame *current = stack_.back();
     116     6876005 :         current->size = length;
     117             :     }
     118     7268553 : }
     119             : 
     120    23582370 : size_t ParseContext::size() const {
     121    23582370 :     if (stack_.empty()) {
     122      984832 :         return -1;
     123             :     }
     124    22593857 :     StackFrame *current = stack_.back();
     125    22592505 :     return current->size;
     126             : }
     127             : 
     128     2790027 : void ParseContext::set_total_size() {
     129     2790027 :     if (!stack_.empty()) {
     130     2790019 :         StackFrame *current = stack_.back();
     131     2790001 :         current->total_size = current->size;
     132             :     }
     133     2790001 : }
     134             : 
     135       87566 : size_t ParseContext::total_size() const {
     136       87566 :     if (stack_.empty()) {
     137           0 :         return -1;
     138             :     }
     139       87566 :     StackFrame *current = stack_.back();
     140       87565 :     return current->total_size >= 0 ? current->total_size : current->size;
     141             : }
     142             : 
     143        7840 : void ParseContext::SetError(int error, int subcode, std::string type,
     144             :                             const uint8_t *data, int data_size) {
     145        7840 :     if (error_context_.error_code) {
     146             :         // Error already set. Ignore this one.
     147         300 :         return;
     148             :     }
     149        7540 :     error_context_.error_code = error;
     150        7540 :     error_context_.error_subcode = subcode;
     151        7540 :     error_context_.type_name = type;
     152        7540 :     error_context_.data = data;
     153        7540 :     error_context_.data_size = data_size;
     154             : }
     155             : 
     156             : struct EncodeContext::StackFrame {
     157             :     typedef boost::function<void(EncodeContext *)> CallbackFn;
     158             :     typedef std::vector<CallbackFn>::iterator CallbackIterator;
     159     5091036 :     StackFrame() : offset(0) {
     160     5090751 :     }
     161             :     int offset;
     162             :     std::vector<CallbackFn> callbacks;
     163             : };
     164             : 
     165      275612 : EncodeContext::EncodeContext() {
     166      275579 : }
     167             : 
     168      275605 : EncodeContext::~EncodeContext() {
     169      275605 : }
     170             : 
     171     5090299 : void EncodeContext::Push() {
     172     5090299 :     StackFrame *frame = new StackFrame();
     173     5090691 :     stack_.push_back(frame);
     174     5088493 : }
     175             : 
     176     5089572 : void EncodeContext::Pop(bool callback) {
     177     5089572 :     StackFrame *frame = &stack_.back();
     178     5088206 :     if (callback) {
     179     5087955 :         for (StackFrame::CallbackIterator iter =
     180    12618855 :              frame->callbacks.begin(); iter != frame->callbacks.end(); ++iter) {
     181     2442722 :             StackFrame::CallbackFn cb = *iter;
     182     2442826 :             (cb)(this);
     183     2442525 :         }
     184             :     }
     185             : 
     186     5088539 :     int p_offset = frame->offset;
     187     5088539 :     stack_.pop_back();                  // deletes frame
     188     5088808 :     if (!stack_.empty()) {
     189     4813006 :         StackFrame *top = &(stack_.back());
     190     4811871 :         top->offset += p_offset;
     191             :     }
     192     5086978 : }
     193             : 
     194     8611157 : void EncodeContext::advance(int delta) {
     195     8611157 :     if (stack_.empty()) {
     196           0 :         return;
     197             :     }
     198     8608791 :     StackFrame *frame = &stack_.back();
     199     8604028 :     frame->offset += delta;
     200             : }
     201             : 
     202     2206744 : int EncodeContext::length() const {
     203     2206744 :     const StackFrame *frame = &stack_.back();
     204     2206475 :     return frame->offset;
     205             : };
     206             : 
     207     2443684 : void EncodeContext::AddCallback(CallbackType cb, uint8_t *data,
     208             :                                 int element_size) {
     209     2443684 :     StackFrame *top = &(stack_.back());
     210     2443245 :     top->callbacks.push_back(
     211     4885789 :         boost::bind(cb, _1, data, top->offset, element_size));
     212     2442428 : }
     213             : 
     214      583109 : void EncodeOffsets::SaveOffset(std::string key, int offset) {
     215             :     std::pair<std::map<std::string, int>::iterator, bool > result =
     216      583109 :             offsets_.insert(make_pair(key, offset));
     217      583400 :     if (!result.second) {
     218           4 :         PROTO_DEBUG("Offset for " << key << " already set");
     219             :     }
     220      583400 : }
     221             : 
     222      583133 : void EncodeContext::SaveOffset(std::string key) {
     223      583133 :     int length = 0;
     224      583133 :     for (boost::ptr_vector<StackFrame>::iterator it = stack_.begin();
     225     2251535 :          it != stack_.end(); ++it) {
     226     1668433 :         length += it->offset;
     227             :     }
     228      582899 :     PROTO_DEBUG("Saving Offset for " << key << " at " << length);
     229      582899 :     offsets_.SaveOffset(key, length);
     230      583398 : }
     231             : 
     232      178959 : int EncodeOffsets::FindOffset(const char *key) {
     233      178959 :     std::map<std::string, int>::iterator it = offsets_.find(std::string(key));
     234      178949 :     if (it == offsets_.end())
     235           0 :         return -1;
     236      178948 :     return it->second;
     237             : }

Generated by: LCOV version 1.14