Ghidra Decompiler Analysis Engine
xml.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 #ifndef __CPUI_XML__
19 #define __CPUI_XML__
20 
21 #include "types.h"
22 #include <fstream>
23 #include <iomanip>
24 #include <string>
25 #include <vector>
26 #include <map>
27 
28 using namespace std;
29 
36 class Attributes {
37  static string bogus_uri;
38 // static string prefix;
39  string *elementname;
40  vector<string *> name;
41  vector<string *> value;
42 public:
43  Attributes(string *el) { elementname = el; }
44  ~Attributes(void) {
45  for(uint4 i=0;i<name.size();++i) { delete name[i]; delete value[i]; }
46  delete elementname;
47  }
48  const string &getelemURI(void) const { return bogus_uri; }
49  const string &getelemName(void) const { return *elementname; }
50  void add_attribute(string *nm,string *vl) { name.push_back(nm); value.push_back(vl); }
51  // The official SAX interface
52  int4 getLength(void) const { return name.size(); }
53  const string &getURI(int4 i) const { return bogus_uri; }
54  const string &getLocalName(int4 i) const { return *name[i]; }
55  const string &getQName(int4 i) const { return *name[i]; }
56  // int4 getIndex(const string &uri,const string &localName) const;
57  // int4 getIndex(const string &qualifiedName) const;
58  // const string &getType(int4 index) const;
59  // const string &getType(const string &uri,const string &localName) const;
60  // const string &getType(const string &qualifiedName) const;
61  const string &getValue(int4 i) const { return *value[i]; }
62  //const string &getValue(const string &uri,const string &localName) const;
64  const string &getValue(const string &qualifiedName) const {
65  for(uint4 i=0;i<name.size();++i)
66  if (*name[i] == qualifiedName) return *value[i];
67  return bogus_uri;
68  }
69 };
70 
71 typedef void *Locator;
72 
78 public:
79  virtual ~ContentHandler(void) {}
80  virtual void setDocumentLocator(Locator locator)=0;
81  virtual void startDocument(void)=0;
82  virtual void endDocument(void)=0;
83  virtual void startPrefixMapping(const string &prefix,const string &uri)=0;
84  virtual void endPrefixMapping(const string &prefix)=0;
85 
92  virtual void startElement(const string &namespaceURI,const string &localName,
93  const string &qualifiedName,const Attributes &atts)=0;
94 
100  virtual void endElement(const string &namespaceURI,const string &localName,
101  const string &qualifiedName)=0;
102 
108  virtual void characters(const char *text,int4 start,int4 length)=0;
109 
115  virtual void ignorableWhitespace(const char *text,int4 start,int4 length)=0;
116 
120  virtual void setVersion(const string &version)=0;
121 
125  virtual void setEncoding(const string &encoding)=0;
126 
131  virtual void processingInstruction(const string &target,const string &data)=0;
132 
136  virtual void skippedEntity(const string &name)=0;
137 
141  virtual void setError(const string &errmsg)=0;
142 };
143 
144 class Element;
145 typedef vector<Element *> List;
146 
150 class Element {
151  string name;
152  string content;
153  vector<string> attr;
154  vector<string> value;
155 protected:
158 public:
159  Element(Element *par) { parent = par; }
160  ~Element(void);
161  void setName(const string &nm) { name = nm; }
162 
168  void addContent(const char *str,int4 start,int4 length) {
169  // for(int4 i=0;i<length;++i) content += str[start+i]; }
170  content.append(str+start,length); }
171 
175  void addChild(Element *child) { children.push_back(child); }
176 
181  void addAttribute(const string &nm,const string &vl) {
182  attr.push_back(nm); value.push_back(vl); }
183 
184  Element *getParent(void) const { return parent; }
185  const string &getName(void) const { return name; }
186  const List &getChildren(void) const { return children; }
187  const string &getContent(void) const { return content; }
188 
195  const string &getAttributeValue(const string &nm) const;
196 
197  int4 getNumAttributes(void) const { return attr.size(); }
198  const string &getAttributeName(int4 i) const { return attr[i]; }
199  const string &getAttributeValue(int4 i) const { return value[i]; }
200 };
201 
206 class Document : public Element {
207 public:
208  Document(void) : Element((Element *)0) {}
209  Element *getRoot(void) const { return *children.begin(); }
210 };
211 
217 class TreeHandler : public ContentHandler {
218  Element *root;
219  Element *cur;
220  string error;
221 public:
222  TreeHandler(Element *rt) { root = rt; cur = root; }
223  virtual ~TreeHandler(void) {}
224  virtual void setDocumentLocator(Locator locator) {}
225  virtual void startDocument(void) {}
226  virtual void endDocument(void) {}
227  virtual void startPrefixMapping(const string &prefix,const string &uri) {}
228  virtual void endPrefixMapping(const string &prefix) {}
229  virtual void startElement(const string &namespaceURI,const string &localName,
230  const string &qualifiedName,const Attributes &atts);
231  virtual void endElement(const string &namespaceURI,const string &localName,
232  const string &qualifiedName);
233  virtual void characters(const char *text,int4 start,int4 length);
234  virtual void ignorableWhitespace(const char *text,int4 start,int4 length) {}
235  virtual void processingInstruction(const string &target,const string &data) {}
236  virtual void setVersion(const string &val) {}
237  virtual void setEncoding(const string &val) {}
238  virtual void skippedEntity(const string &name) {}
239  virtual void setError(const string &errmsg) { error = errmsg; }
240  const string &getError(void) const { return error; }
241 };
242 
250  vector<Document *> doclist;
251  map<string,const Element *> tagmap;
252 public:
253  ~DocumentStorage(void);
254 
261  Document *parseDocument(istream &s);
262 
269  Document *openDocument(const string &filename);
270 
275  void registerTag(const Element *el);
276 
281  const Element *getTag(const string &nm) const;
282 };
283 
288 struct XmlError {
289  string explain;
290  XmlError(const string &s) { explain = s; }
291 };
292 
300 extern int4 xml_parse(istream &i,ContentHandler *hand,int4 dbg=0);
301 
308 extern Document *xml_tree(istream &i);
309 
321 extern void xml_escape(ostream &s,const char *str);
322 
323 // Some helper functions for writing XML documents directly to a stream
324 
330 inline void a_v(ostream &s,const string &attr,const string &val)
331 
332 {
333  s << ' ' << attr << "=\"";
334  xml_escape(s,val.c_str());
335  s << "\"";
336 }
337 
343 inline void a_v_i(ostream &s,const string &attr,intb val)
344 
345 {
346  s << ' ' << attr << "=\"" << dec << val << "\"";
347 }
348 
354 inline void a_v_u(ostream &s,const string &attr,uintb val)
355 
356 {
357  s << ' ' << attr << "=\"0x" << hex << val << "\"";
358 }
359 
365 inline void a_v_b(ostream &s,const string &attr,bool val)
366 
367 {
368  s << ' ' << attr << "=\"";
369  if (val)
370  s << "true";
371  else
372  s << "false";
373  s << "\"";
374 }
375 
382 inline bool xml_readbool(const string &attr)
383 
384 {
385  if (attr.size()==0) return false;
386  char firstc = attr[0];
387  if (firstc=='t') return true;
388  if (firstc=='1') return true;
389  if (firstc=='y') return true; // For backward compatibility
390  return false;
391 }
392 #endif
xml_escape
void xml_escape(ostream &s, const char *str)
Send the given character array to a stream, escaping characters with special XML meaning.
Definition: xml.cc:2350
TreeHandler::endPrefixMapping
virtual void endPrefixMapping(const string &prefix)
Finish the current prefix.
Definition: xml.hh:228
ContentHandler
The SAX interface for parsing XML documents.
Definition: xml.hh:77
Element::addChild
void addChild(Element *child)
Add a new child Element to the model, with this as the parent.
Definition: xml.hh:175
Element::addContent
void addContent(const char *str, int4 start, int4 length)
Append new character content to this element.
Definition: xml.hh:168
TreeHandler::ignorableWhitespace
virtual void ignorableWhitespace(const char *text, int4 start, int4 length)
Callback with whitespace character data for the current XML element.
Definition: xml.hh:234
XmlError::explain
string explain
Explanatory string.
Definition: xml.hh:289
a_v_b
void a_v_b(ostream &s, const string &attr, bool val)
Output the given boolean value as an XML attribute.
Definition: xml.hh:365
List
vector< Element * > List
A list of XML elements.
Definition: xml.hh:144
Element::parent
Element * parent
The parent Element (or null)
Definition: xml.hh:156
Document
A complete in-memory XML document.
Definition: xml.hh:206
Locator
void * Locator
Placeholder for a document locator object.
Definition: xml.hh:71
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
a_v_u
void a_v_u(ostream &s, const string &attr, uintb val)
Output the given unsigned integer as an XML attribute value.
Definition: xml.hh:354
TreeHandler::startPrefixMapping
virtual void startPrefixMapping(const string &prefix, const string &uri)
Start a new prefix to namespace URI mapping.
Definition: xml.hh:227
Attributes::getValue
const string & getValue(const string &qualifiedName) const
Get the value of the attribute with the given qualified name.
Definition: xml.hh:64
a_v
void a_v(ostream &s, const string &attr, const string &val)
Output an XML attribute name/value pair to stream.
Definition: xml.hh:330
TreeHandler
A SAX interface implementation for constructing an in-memory DOM model.
Definition: xml.hh:217
TreeHandler::setEncoding
virtual void setEncoding(const string &val)
Set the character encoding as specified by the current document.
Definition: xml.hh:237
TreeHandler::setDocumentLocator
virtual void setDocumentLocator(Locator locator)
Set the Locator object for documents.
Definition: xml.hh:224
xml_tree
Document * xml_tree(istream &i)
Parse the given XML stream into an in-memory document.
Definition: xml.cc:2338
TreeHandler::setError
virtual void setError(const string &errmsg)
Callback for handling an error condition during XML parsing.
Definition: xml.hh:239
xml_readbool
bool xml_readbool(const string &attr)
Read an XML attribute value as a boolean.
Definition: xml.hh:382
XmlError
An exception thrown by the XML parser.
Definition: xml.hh:288
TreeHandler::skippedEntity
virtual void skippedEntity(const string &name)
Callback for an XML entity skipped by the parser.
Definition: xml.hh:238
Element::addAttribute
void addAttribute(const string &nm, const string &vl)
Add a new name/value attribute pair to this element.
Definition: xml.hh:181
TreeHandler::startDocument
virtual void startDocument(void)
Start processing a new XML document.
Definition: xml.hh:225
DocumentStorage
A container for parsed XML documents.
Definition: xml.hh:249
a_v_i
void a_v_i(ostream &s, const string &attr, intb val)
Output the given signed integer as an XML attribute value.
Definition: xml.hh:343
TreeHandler::endDocument
virtual void endDocument(void)
End processing for the current XML document.
Definition: xml.hh:226
xml_parse
int4 xml_parse(istream &i, ContentHandler *hand, int4 dbg=0)
Start-up the XML parser given a stream and a handler.
Definition: xml.cc:2236
TreeHandler::processingInstruction
virtual void processingInstruction(const string &target, const string &data)
Callback for a formal processing instruction seen in the current document.
Definition: xml.hh:235
Attributes
The attributes for a single XML element.
Definition: xml.hh:36
TreeHandler::setVersion
virtual void setVersion(const string &val)
Set the XML version as specified by the current document.
Definition: xml.hh:236
Element::children
List children
A list of child Element objects.
Definition: xml.hh:157