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: 124 144 86.1 %
Date: 2026-11-16 02:06:09 Functions: 27 29 93.1 %
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       79179 :     StackFrame() : offset(0), lensize(0), size(-1), total_size(-1) {
      19       79177 :     }
      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        9841 : ParseContext::ParseContext()
      28        9841 :     : offset_(0) {
      29        9841 : }
      30             : 
      31             : struct deleter {
      32             :     template <typename T>
      33        9840 :     void operator()(T *ptr) {
      34        9840 :         delete ptr;
      35        9841 :     }
      36             : };
      37             : 
      38        9840 : ParseContext::~ParseContext() {
      39        9840 :     for_each(stack_.begin(), stack_.end(), deleter());
      40        9840 :     stack_.clear();
      41        9840 : }
      42             : 
      43        9840 : ParseObject *ParseContext::release() {
      44        9840 :     if (stack_.empty()) {
      45           0 :         return NULL;
      46             :     }
      47        9840 :     StackFrame *current = stack_.back();
      48        9840 :     return current->data.release();
      49             : }
      50             : 
      51       79178 : void ParseContext::Push(ParseObject *data) {
      52       79178 :     StackFrame *frame = new StackFrame();
      53       79177 :     frame->data.reset(data);
      54       79173 :     stack_.push_back(frame);
      55       79171 : }
      56             : 
      57       79173 : ParseObject *ParseContext::Pop() {
      58       79173 :     if (stack_.size() <= 1) {
      59        9839 :         return NULL;
      60             :     }
      61       69333 :     StackFrame *frame = stack_.back();
      62       69334 :     ParseObject *obj = frame->data.release();
      63       69333 :     stack_.pop_back();
      64       69334 :     delete frame;
      65       69333 :     return obj;
      66             : }
      67             : 
      68       17265 : void ParseContext::ReleaseData() {
      69       17265 :     if (!stack_.empty()) {
      70       17265 :         StackFrame *frame = stack_.back();
      71       17265 :         frame->data.release();
      72             :     }
      73       17265 : }
      74       50116 : void ParseContext::SwapData(ParseObject *obj) {
      75       50116 :     if (!stack_.empty() && obj) {
      76       50114 :         StackFrame *frame = stack_.back();
      77       50114 :         frame->data.reset(obj);
      78             :     } else {
      79           0 :         delete obj;
      80             :     }
      81       50113 : }
      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      356963 : void ParseContext::advance(int delta) {
      92      356963 :     if (!stack_.empty()) {
      93      327415 :         stack_.back()->offset += delta;
      94             :     }
      95      356912 :     offset_ += delta;
      96      356912 : }
      97             : 
      98       58812 : void ParseContext::set_lensize(int length) {
      99       58812 :     if (!stack_.empty()) {
     100       58812 :         StackFrame *current = stack_.back();
     101       58812 :         current->lensize = length;
     102             :     }
     103       58812 : }
     104             : 
     105      112022 : int ParseContext::lensize() const {
     106      112022 :     if (stack_.empty()) {
     107           0 :         return -1;
     108             :     }
     109      112020 :     StackFrame *current = stack_.back();
     110      112019 :     return current->lensize;
     111             : }
     112             : 
     113      252488 : void ParseContext::set_size(size_t length) {
     114      252488 :     if (!stack_.empty()) {
     115      232800 :         StackFrame *current = stack_.back();
     116      232797 :         current->size = length;
     117             :     }
     118      252466 : }
     119             : 
     120      813210 : size_t ParseContext::size() const {
     121      813210 :     if (stack_.empty()) {
     122       49204 :         return -1;
     123             :     }
     124      763873 :     StackFrame *current = stack_.back();
     125      763807 :     return current->size;
     126             : }
     127             : 
     128       91662 : void ParseContext::set_total_size() {
     129       91662 :     if (!stack_.empty()) {
     130       91660 :         StackFrame *current = stack_.back();
     131       91658 :         current->total_size = current->size;
     132             :     }
     133       91658 : }
     134             : 
     135        3604 : size_t ParseContext::total_size() const {
     136        3604 :     if (stack_.empty()) {
     137           0 :         return -1;
     138             :     }
     139        3604 :     StackFrame *current = stack_.back();
     140        3604 :     return current->total_size >= 0 ? current->total_size : current->size;
     141             : }
     142             : 
     143           0 : void ParseContext::SetError(int error, int subcode, std::string type,
     144             :                             const uint8_t *data, int data_size) {
     145           0 :     if (error_context_.error_code) {
     146             :         // Error already set. Ignore this one.
     147           0 :         return;
     148             :     }
     149           0 :     error_context_.error_code = error;
     150           0 :     error_context_.error_subcode = subcode;
     151           0 :     error_context_.type_name = type;
     152           0 :     error_context_.data = data;
     153           0 :     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      179656 :     StackFrame() : offset(0) {
     160      179597 :     }
     161             :     int offset;
     162             :     std::vector<CallbackFn> callbacks;
     163             : };
     164             : 
     165       14623 : EncodeContext::EncodeContext() {
     166       14622 : }
     167             : 
     168       14632 : EncodeContext::~EncodeContext() {
     169       14632 : }
     170             : 
     171      179405 : void EncodeContext::Push() {
     172      179405 :     StackFrame *frame = new StackFrame();
     173      179583 :     stack_.push_back(frame);
     174      178786 : }
     175             : 
     176      179584 : void EncodeContext::Pop(bool callback) {
     177      179584 :     StackFrame *frame = &stack_.back();
     178      178945 :     if (callback) {
     179      178887 :         for (StackFrame::CallbackIterator iter =
     180      450325 :              frame->callbacks.begin(); iter != frame->callbacks.end(); ++iter) {
     181       92434 :             StackFrame::CallbackFn cb = *iter;
     182       92505 :             (cb)(this);
     183       92365 :         }
     184             :     }
     185             : 
     186      179184 :     int p_offset = frame->offset;
     187      179184 :     stack_.pop_back();                  // deletes frame
     188      179332 :     if (!stack_.empty()) {
     189      164487 :         StackFrame *top = &(stack_.back());
     190      163792 :         top->offset += p_offset;
     191             :     }
     192      178305 : }
     193             : 
     194      281724 : void EncodeContext::advance(int delta) {
     195      281724 :     if (stack_.empty()) {
     196           0 :         return;
     197             :     }
     198      280792 :     StackFrame *frame = &stack_.back();
     199      278773 :     frame->offset += delta;
     200             : }
     201             : 
     202       84965 : int EncodeContext::length() const {
     203       84965 :     const StackFrame *frame = &stack_.back();
     204       84847 :     return frame->offset;
     205             : };
     206             : 
     207       92633 : void EncodeContext::AddCallback(CallbackType cb, uint8_t *data,
     208             :                                 int element_size) {
     209       92633 :     StackFrame *top = &(stack_.back());
     210       92479 :     top->callbacks.push_back(
     211      184692 :         boost::bind(cb, _1, data, top->offset, element_size));
     212       92256 : }
     213             : 
     214       32619 : void EncodeOffsets::SaveOffset(std::string key, int offset) {
     215             :     std::pair<std::map<std::string, int>::iterator, bool > result =
     216       32619 :             offsets_.insert(make_pair(key, offset));
     217       32680 :     if (!result.second) {
     218           0 :         PROTO_DEBUG("Offset for " << key << " already set");
     219             :     }
     220       32680 : }
     221             : 
     222       32624 : void EncodeContext::SaveOffset(std::string key) {
     223       32624 :     int length = 0;
     224       32624 :     for (boost::ptr_vector<StackFrame>::iterator it = stack_.begin();
     225      129513 :          it != stack_.end(); ++it) {
     226       96901 :         length += it->offset;
     227             :     }
     228       32531 :     PROTO_DEBUG("Saving Offset for " << key << " at " << length);
     229       32531 :     offsets_.SaveOffset(key, length);
     230       32680 : }
     231             : 
     232       10357 : int EncodeOffsets::FindOffset(const char *key) {
     233       10357 :     std::map<std::string, int>::iterator it = offsets_.find(std::string(key));
     234       10336 :     if (it == offsets_.end())
     235           0 :         return -1;
     236       10335 :     return it->second;
     237             : }

Generated by: LCOV version 1.14