File: DocNode.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (156 lines) | stat: -rw-r--r-- 4,885 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include "DocNode.h"
#include "Utils.h"

using std::string;
using namespace EsiLib;

const char *DocNode::type_names_[] = {"UNKNOWN", "PRE",       "INCLUDE", "COMMENT", "REMOVE", "VARS",         "CHOOSE",
                                      "WHEN",    "OTHERWISE", "TRY",     "ATTEMPT", "EXCEPT", "HTML_COMMENT", "SPECIAL_INCLUDE"};

// helper functions

inline void
packString(const char *str, int32_t str_len, string &buffer)
{
  buffer.append(reinterpret_cast<const char *>(&str_len), sizeof(str_len));
  if (str_len) {
    buffer.append(str, str_len);
  }
}

inline void
unpackString(const char *&packed_data, const char *&item, int32_t &item_len)
{
  item_len = *(reinterpret_cast<const int32_t *>(packed_data));
  packed_data += sizeof(int32_t);
  item = item_len ? packed_data : nullptr;
  packed_data += item_len;
}

template <typename T>
inline void
unpackItem(const char *&packed_data, T &item)
{
  item = *(reinterpret_cast<const T *>(packed_data));
  packed_data += sizeof(T);
}

void
DocNode::pack(string &buffer) const
{
  int32_t orig_buf_size = buffer.size();
  buffer += DOCNODE_VERSION;
  buffer.append(sizeof(int32_t), ' '); // reserve space for length
  buffer.append(reinterpret_cast<const char *>(&type), sizeof(type));
  packString(data, data_len, buffer);
  int32_t n_elements = attr_list.size();
  buffer.append(reinterpret_cast<const char *>(&n_elements), sizeof(n_elements));
  for (const auto &iter : attr_list) {
    packString(iter.name, iter.name_len, buffer);
    packString(iter.value, iter.value_len, buffer);
  }
  child_nodes.packToBuffer(buffer);
  *(reinterpret_cast<int32_t *>(&buffer[orig_buf_size + 1])) = buffer.size() - orig_buf_size;
}

bool
DocNode::unpack(const char *packed_data, int packed_data_len, int &node_len)
{
  const char *packed_data_start = packed_data;

  if (!packed_data || (packed_data_len < static_cast<int>((sizeof(char) + sizeof(int32_t))))) {
    Utils::ERROR_LOG("[%s] Invalid arguments (%p, %d)", __FUNCTION__, packed_data, packed_data_len);
    return false;
  }
  if (*packed_data != DOCNODE_VERSION) {
    Utils::ERROR_LOG("[%s] Version %d not in supported set (%d)", __FUNCTION__, static_cast<int>(*packed_data),
                     static_cast<int>(DOCNODE_VERSION));
    return false;
  }
  ++packed_data;

  int32_t node_size;
  unpackItem(packed_data, node_size);
  if (node_size > packed_data_len) {
    Utils::ERROR_LOG("[%s] Data size (%d) not sufficient to hold node of size %d", __FUNCTION__, packed_data_len, node_size);
    return false;
  }
  node_len = node_size;

  unpackItem(packed_data, type);

  unpackString(packed_data, data, data_len);

  int32_t n_elements;
  unpackItem(packed_data, n_elements);
  Attribute attr;
  attr_list.clear();
  for (int i = 0; i < n_elements; ++i) {
    unpackString(packed_data, attr.name, attr.name_len);
    unpackString(packed_data, attr.value, attr.value_len);
    attr_list.push_back(attr);
  }

  if (!child_nodes.unpack(packed_data, packed_data_len - (packed_data - packed_data_start))) {
    Utils::ERROR_LOG("[%s] Could not unpack child nodes", __FUNCTION__);
    return false;
  }
  return true;
}

void
DocNodeList::packToBuffer(string &buffer) const
{
  int32_t n_elements = size();
  buffer.append(reinterpret_cast<const char *>(&n_elements), sizeof(n_elements));
  for (const auto &iter : *this) {
    iter.pack(buffer);
  }
}

bool
DocNodeList::unpack(const char *data, int data_len)
{
  if (!data || (data_len < static_cast<int>(sizeof(int32_t)))) {
    Utils::ERROR_LOG("[%s] Invalid arguments", __FUNCTION__);
    return false;
  }
  const char *data_start = data;
  int32_t n_elements;
  unpackItem(data, n_elements);
  clear();
  int data_offset = data - data_start, node_size;
  DocNode node;
  for (int i = 0; i < n_elements; ++i) {
    if (!node.unpack(data_start + data_offset, data_len - data_offset, node_size)) {
      Utils::ERROR_LOG("[%s] Could not unpack node", __FUNCTION__);
      return false;
    }
    data_offset += node_size;
    push_back(node);
  }
  return true;
}