]> git.sesse.net Git - xml-template/blob - c++11/xml-template.cpp
Fix some use-after-frees in the C++11 version.
[xml-template] / c++11 / xml-template.cpp
1 #include "xml-template.h"
2
3 #include <assert.h>
4 #include <string.h>
5 #include <libxml/parser.h>
6
7 using namespace std;
8
9 namespace {
10
11 void clean_node(xmlNode *node)
12 {
13         if (node->type != XML_ELEMENT_NODE) {
14                 return;
15         }
16         if (node->ns != NULL &&
17             strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
18                 while (node->children != NULL) {
19                         xmlAddPrevSibling(node, node->children);
20                 }
21
22                 xmlUnlinkNode(node);
23                 xmlFreeNode(node);
24         }
25 }
26
27 // Does A begin with B?
28 bool begins_with(const string &a, const string &b)
29 {
30         return (a.compare(0, b.size(), b) == 0);
31 }
32
33 }  // namespace
34
35 Directive::~Directive() {}
36         
37 string Directive::get_contents() { return ""; }
38
39 Replace::Replace(const string &str)
40         : str(str) {}
41
42 void Replace::process(xmlNode *node, bool clean) {
43         xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
44         if (clean) {
45                 clean_node(node);
46         }
47 }
48
49 string Replace::get_contents() { return str; }
50
51 ReplaceInclude::ReplaceInclude(xmlNodePtr included_node)
52         : included_node(included_node),
53           included_doc(NULL) {}
54         
55 ReplaceInclude::ReplaceInclude(xmlDocPtr included_doc)
56         : included_node(xmlDocGetRootElement(included_doc)),
57           included_doc(included_doc) {}
58
59 ReplaceInclude::~ReplaceInclude()
60 {
61         if (included_doc != NULL) {
62                 xmlFreeDoc(included_doc);
63         } else {
64                 xmlFreeNode(included_node);
65         }
66 }
67
68 void ReplaceInclude::process(xmlNode *node, bool clean) {
69         xmlFreeNodeList(node->children);
70         node->content = NULL;
71         node->children = node->last = NULL; 
72
73         xmlNode *new_node;
74         xmlDOMWrapCloneNode(NULL, NULL, included_node, &new_node, node->doc, node, 1, 0);
75         xmlAddChild(node, new_node);
76
77         if (clean) {
78                 Substitute dummy_subs {};
79                 dummy_subs.process(new_node, clean);
80                 clean_node(node);
81         }
82 }
83
84 Clone::Clone(const vector<Directive *> &subdirectives)
85         : subdirectives(subdirectives) {}
86
87 Clone::Clone(const vector<Substitute *> &subdirectives_subs)
88 {
89         for (auto it : subdirectives_subs) {
90                 subdirectives.push_back(static_cast<Directive *>(it));
91         }
92 }
93
94 Clone::Clone(initializer_list<Directive *> subdirectives)
95         : subdirectives(subdirectives) {}
96
97 Clone::~Clone()
98 {
99         for (auto it : subdirectives) {
100                 delete it;
101         }
102 }
103
104 void Clone::process(xmlNode *node, bool clean)
105 {
106         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
107         // how to clone elements.
108         vector<xmlNode *> new_nodes;
109
110         for (auto it : subdirectives) {
111                 if (it == NULL) {
112                         continue;
113                 }
114                 xmlNode *new_node;
115                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
116                 it->process(new_node, clean);
117                 while (new_node->children != NULL) {
118                         xmlNode *child = new_node->children;
119                         xmlUnlinkNode(child);
120                         new_nodes.push_back(child);
121                 }
122                 xmlFreeNode(new_node);
123         }
124
125         xmlFreeNodeList(node->children);
126         node->content = NULL;
127         node->children = node->last = NULL;
128
129         for (auto child : new_nodes) {
130                 xmlAddChild(node, child);
131         }       
132         if (clean) {
133                 clean_node(node);
134         }
135 }
136         
137 Alternate::Alternate(const string &attribute,
138                      const vector<Substitute *> &subdirectives_subs,
139                      const vector<string> &alternatives)
140     : Clone(subdirectives_subs)
141 {
142         unsigned jx = 0;
143         for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
144                 if (subdirectives_subs[ix] == NULL) {
145                         continue;
146                 }
147                 string value = alternatives[jx++ % alternatives.size()];
148                 Replace *r = new Replace { value };
149                 subdirectives_subs[ix]->substitution_map.insert(make_pair(
150                         attribute,
151                         new Replace { value }));
152         } 
153 }
154
155 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
156         : substitution_map(substitution_map) {}
157         
158 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
159         : substitution_map(substitution_map) {}
160
161 Substitute::~Substitute()
162 {
163         for (auto it : substitution_map) {
164                 delete it.second;
165         }
166 }
167
168 void Substitute::process(xmlNode *node, bool clean)
169 {
170         xmlNode *next_child;
171         for (xmlNode *child = node->children; child != NULL; child = next_child) {
172                 next_child = child->next;
173                 Directive *next_processor = this;
174
175                 if (child->type == XML_ELEMENT_NODE) {
176                         // Find the ID, if any.
177                         string id;
178                         xmlAttr *id_attr = NULL;
179                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
180                                 if (attr->ns != NULL &&
181                                     strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
182                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
183                                         xmlChar *id_buf = xmlNodeGetContent(attr->children);
184                                         id = reinterpret_cast<const char *>(id_buf);
185                                         xmlFree(id_buf);
186                                         id_attr = attr;
187                                 }
188                         }
189                         if (clean && id_attr != NULL) {
190                                 xmlRemoveProp(id_attr);
191                         }
192
193                         // Check all substitutions to see if we found anything appropriate.
194                         string tag = reinterpret_cast<const char *>(child->name);
195                         for (auto it : substitution_map) {
196                                 assert(it.second != nullptr);
197                                 // Attribute substitution.
198                                 if (begins_with(it.first, tag + "/")) {
199                                         string contents = it.second->get_contents();
200                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
201                                                 it.first.c_str() + tag.size() + 1);
202                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
203                                                 contents.c_str());
204                                         xmlSetProp(child, attr_key, attr_value);
205                                 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
206                                         string contents = it.second->get_contents();
207                                         const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
208                                                 it.first.c_str() + id.size() + 2);
209                                         const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
210                                                 contents.c_str());
211                                         xmlSetProp(child, attr_key, attr_value);
212                                 }
213
214                                 // Regular substitution. (Don't call process() immediately, because
215                                 // that might delete the element, which would cause problems.)
216                                 if (it.first == tag ||
217                                     (!id.empty() && it.first == ("#" + id))) {
218                                         next_processor = it.second;
219                                 }
220                         }
221                 }
222
223                 next_processor->process(child, clean);
224         }
225         if (clean) {
226                 clean_node(node);
227         }
228 }
229         
230 xmlDocPtr process_file(const string &input_filename,
231                        Directive *root_directive,
232                        bool clean)
233 {
234         LIBXML_TEST_VERSION
235
236         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
237         root_directive->process(xmlDocGetRootElement(doc), clean);
238         xmlCleanupParser();
239         xmlMemoryDump();
240         return doc;
241 }
242
243 void output_to_fd_and_free(xmlDocPtr doc, int fd)
244 {
245         xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
246         xmlSaveFileTo(buf, doc, NULL);
247         xmlFreeDoc(doc);
248 }