Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "base/proto.h" 6 : 7 : #include <boost/bind/bind.hpp> 8 : 9 : using namespace std; 10 : using namespace boost::placeholders; 11 : 12 : namespace detail { 13 : bool debug_ = false; 14 : } 15 : 16 : struct ParseContext::StackFrame { 17 156255 : StackFrame() : offset(0), lensize(0), size(-1), total_size(-1) { 18 156249 : } 19 : int offset; // offset of the data pointer at present 20 : int lensize; //size of the length of the current element being parsed 21 : size_t size; 22 : size_t total_size; 23 : std::unique_ptr<ParseObject> data; 24 : }; 25 : 26 21021 : ParseContext::ParseContext() 27 21021 : : offset_(0) { 28 21021 : } 29 : 30 : struct deleter { 31 : template <typename T> 32 21017 : void operator()(T *ptr) { 33 21017 : delete ptr; 34 21017 : } 35 : }; 36 : 37 21017 : ParseContext::~ParseContext() { 38 21017 : for_each(stack_.begin(), stack_.end(), deleter()); 39 21017 : stack_.clear(); 40 21018 : } 41 : 42 21019 : ParseObject *ParseContext::release() { 43 21019 : if (stack_.empty()) { 44 0 : return NULL; 45 : } 46 21019 : StackFrame *current = stack_.back(); 47 21017 : return current->data.release(); 48 : } 49 : 50 156242 : void ParseContext::Push(ParseObject *data) { 51 156242 : StackFrame *frame = new StackFrame(); 52 156250 : frame->data.reset(data); 53 156232 : stack_.push_back(frame); 54 156242 : } 55 : 56 156241 : ParseObject *ParseContext::Pop() { 57 156241 : if (stack_.size() <= 1) { 58 21019 : return NULL; 59 : } 60 135224 : StackFrame *frame = stack_.back(); 61 135225 : ParseObject *obj = frame->data.release(); 62 135220 : stack_.pop_back(); 63 135223 : delete frame; 64 135238 : return obj; 65 : } 66 : 67 36065 : void ParseContext::ReleaseData() { 68 36065 : if (!stack_.empty()) { 69 36060 : StackFrame *frame = stack_.back(); 70 36057 : frame->data.release(); 71 : } 72 36059 : } 73 98131 : void ParseContext::SwapData(ParseObject *obj) { 74 98131 : if (!stack_.empty() && obj) { 75 98130 : StackFrame *frame = stack_.back(); 76 98128 : frame->data.reset(obj); 77 : } else { 78 0 : delete obj; 79 : } 80 98128 : } 81 : 82 0 : ParseObject *ParseContext::data() { 83 0 : if (stack_.empty()) { 84 0 : return NULL; 85 : } 86 0 : StackFrame *current = stack_.back(); 87 0 : return current->data.get(); 88 : } 89 : 90 693907 : void ParseContext::advance(int delta) { 91 693907 : if (!stack_.empty()) { 92 630798 : stack_.back()->offset += delta; 93 : } 94 693727 : offset_ += delta; 95 693727 : } 96 : 97 111035 : void ParseContext::set_lensize(int length) { 98 111035 : if (!stack_.empty()) { 99 111034 : StackFrame *current = stack_.back(); 100 111032 : current->lensize = length; 101 : } 102 111032 : } 103 : 104 218290 : int ParseContext::lensize() const { 105 218290 : if (stack_.empty()) { 106 0 : return -1; 107 : } 108 218284 : StackFrame *current = stack_.back(); 109 218273 : return current->lensize; 110 : } 111 : 112 493280 : void ParseContext::set_size(size_t length) { 113 493280 : if (!stack_.empty()) { 114 451269 : StackFrame *current = stack_.back(); 115 451249 : current->size = length; 116 : } 117 493197 : } 118 : 119 1576552 : size_t ParseContext::size() const { 120 1576552 : if (stack_.empty()) { 121 105068 : return -1; 122 : } 123 1471001 : StackFrame *current = stack_.back(); 124 1470911 : return current->size; 125 : } 126 : 127 173081 : void ParseContext::set_total_size() { 128 173081 : if (!stack_.empty()) { 129 173080 : StackFrame *current = stack_.back(); 130 173078 : current->total_size = current->size; 131 : } 132 173078 : } 133 : 134 6920 : size_t ParseContext::total_size() const { 135 6920 : if (stack_.empty()) { 136 0 : return -1; 137 : } 138 6920 : StackFrame *current = stack_.back(); 139 6920 : return current->total_size >= 0 ? current->total_size : current->size; 140 : } 141 : 142 0 : void ParseContext::SetError(int error, int subcode, std::string type, 143 : const uint8_t *data, int data_size) { 144 0 : if (error_context_.error_code) { 145 : // Error already set. Ignore this one. 146 0 : return; 147 : } 148 0 : error_context_.error_code = error; 149 0 : error_context_.error_subcode = subcode; 150 0 : error_context_.type_name = type; 151 0 : error_context_.data = data; 152 0 : error_context_.data_size = data_size; 153 : } 154 : 155 : struct EncodeContext::StackFrame { 156 : typedef boost::function<void(EncodeContext *)> CallbackFn; 157 : typedef std::vector<CallbackFn>::iterator CallbackIterator; 158 331154 : StackFrame() : offset(0) { 159 331103 : } 160 : int offset; 161 : std::vector<CallbackFn> callbacks; 162 : }; 163 : 164 28046 : EncodeContext::EncodeContext() { 165 28045 : } 166 : 167 28060 : EncodeContext::~EncodeContext() { 168 28060 : } 169 : 170 330963 : void EncodeContext::Push() { 171 330963 : StackFrame *frame = new StackFrame(); 172 331096 : stack_.push_back(frame); 173 330402 : } 174 : 175 331055 : void EncodeContext::Pop(bool callback) { 176 331055 : StackFrame *frame = &stack_.back(); 177 330571 : if (callback) { 178 330517 : for (StackFrame::CallbackIterator iter = 179 833762 : frame->callbacks.begin(); iter != frame->callbacks.end(); ++iter) { 180 172668 : StackFrame::CallbackFn cb = *iter; 181 172729 : (cb)(this); 182 172582 : } 183 : } 184 : 185 330712 : int p_offset = frame->offset; 186 330712 : stack_.pop_back(); // deletes frame 187 330856 : if (!stack_.empty()) { 188 302633 : StackFrame *top = &(stack_.back()); 189 302122 : top->offset += p_offset; 190 : } 191 330072 : } 192 : 193 525676 : void EncodeContext::advance(int delta) { 194 525676 : if (stack_.empty()) { 195 0 : return; 196 : } 197 524858 : StackFrame *frame = &stack_.back(); 198 523219 : frame->offset += delta; 199 : } 200 : 201 159567 : int EncodeContext::length() const { 202 159567 : const StackFrame *frame = &stack_.back(); 203 159455 : return frame->offset; 204 : }; 205 : 206 172791 : void EncodeContext::AddCallback(CallbackType cb, uint8_t *data, 207 : int element_size) { 208 172791 : StackFrame *top = &(stack_.back()); 209 172670 : top->callbacks.push_back( 210 345139 : boost::bind(cb, _1, data, top->offset, element_size)); 211 172478 : } 212 : 213 60890 : void EncodeOffsets::SaveOffset(std::string key, int offset) { 214 : std::pair<std::map<std::string, int>::iterator, bool > result = 215 60890 : offsets_.insert(make_pair(key, offset)); 216 60962 : if (!result.second) { 217 0 : PROTO_DEBUG("Offset for " << key << " already set"); 218 : } 219 60962 : } 220 : 221 60893 : void EncodeContext::SaveOffset(std::string key) { 222 60893 : int length = 0; 223 60893 : for (boost::ptr_vector<StackFrame>::iterator it = stack_.begin(); 224 234615 : it != stack_.end(); ++it) { 225 173723 : length += it->offset; 226 : } 227 60826 : PROTO_DEBUG("Saving Offset for " << key << " at " << length); 228 60826 : offsets_.SaveOffset(key, length); 229 60962 : } 230 : 231 14309 : int EncodeOffsets::FindOffset(const char *key) { 232 14309 : std::map<std::string, int>::iterator it = offsets_.find(std::string(key)); 233 14308 : if (it == offsets_.end()) 234 0 : return -1; 235 14308 : return it->second; 236 : }