Flutter Engine
The Flutter Engine
SkXMLParser.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
13#include "src/xml/SkXMLParser.h"
14
15#include <expat.h>
16
17#include <vector>
18
19static char const* const gErrorStrings[] = {
20 "empty or missing file ",
21 "unknown element ",
22 "unknown attribute name ",
23 "error in attribute value ",
24 "duplicate ID ",
25 "unknown error "
26};
27
28SkXMLParserError::SkXMLParserError() : fCode(kNoError), fLineNumber(-1),
29 fNativeCode(-1)
30{
31 reset();
32}
33
35{
36 // need a virtual destructor for our subclasses
37}
38
40{
41 SkASSERT(str);
42 SkString temp;
43 if (fCode != kNoError) {
44 if ((unsigned)fCode < std::size(gErrorStrings))
45 temp.set(gErrorStrings[fCode - 1]);
46 temp.append(fNoun);
47 } else
48 SkXMLParser::GetNativeErrorString(fNativeCode, &temp);
49 str->append(temp);
50}
51
54 fLineNumber = -1;
55 fNativeCode = -1;
56}
57
58////////////////
59
60namespace {
61
62constexpr const void* kHashSeed = &kHashSeed;
63
64const XML_Memory_Handling_Suite sk_XML_alloc = {
68};
69
70struct ParsingContext {
71 ParsingContext(SkXMLParser* parser)
72 : fParser(parser)
73 , fXMLParser(XML_ParserCreate_MM(nullptr, &sk_XML_alloc, nullptr)) { }
74
75 void flushText() {
76 if (!fBufferedText.empty()) {
77 fParser->text(fBufferedText.data(), SkTo<int>(fBufferedText.size()));
78 fBufferedText.clear();
79 }
80 }
81
82 void appendText(const char* txt, size_t len) {
83 fBufferedText.insert(fBufferedText.end(), txt, &txt[len]);
84 }
85
86 SkXMLParser* fParser;
88
89private:
90 std::vector<char> fBufferedText;
91};
92
93#define HANDLER_CONTEXT(arg, name) ParsingContext* name = static_cast<ParsingContext*>(arg)
94
95void XMLCALL start_element_handler(void *data, const char* tag, const char** attributes) {
97 ctx->flushText();
98
99 ctx->fParser->startElement(tag);
100
101 for (size_t i = 0; attributes[i]; i += 2) {
102 ctx->fParser->addAttribute(attributes[i], attributes[i + 1]);
103 }
104}
105
106void XMLCALL end_element_handler(void* data, const char* tag) {
107 HANDLER_CONTEXT(data, ctx);
108 ctx->flushText();
109
110 ctx->fParser->endElement(tag);
111}
112
113void XMLCALL text_handler(void *data, const char* txt, int len) {
114 HANDLER_CONTEXT(data, ctx);
115
116 ctx->appendText(txt, SkTo<size_t>(len));
117}
118
119void XMLCALL entity_decl_handler(void *data,
120 const XML_Char *entityName,
121 int is_parameter_entity,
122 const XML_Char *value,
123 int value_length,
124 const XML_Char *base,
125 const XML_Char *systemId,
126 const XML_Char *publicId,
127 const XML_Char *notationName) {
128 HANDLER_CONTEXT(data, ctx);
129
130 SkDEBUGF("'%s' entity declaration found, stopping processing", entityName);
131 XML_StopParser(ctx->fXMLParser, XML_FALSE);
132}
133
134} // anonymous namespace
135
136SkXMLParser::SkXMLParser(SkXMLParserError* parserError) : fParser(nullptr), fError(parserError)
137{
138}
139
141{
142}
143
145{
146 ParsingContext ctx(this);
147 if (!ctx.fXMLParser) {
148 SkDEBUGF("could not create XML parser\n");
149 return false;
150 }
151
152 // Avoid calls to rand_s if this is not set. This seed helps prevent DOS
153 // with a known hash sequence so an address is sufficient. The provided
154 // seed should not be zero as that results in a call to rand_s.
155 unsigned long seed = static_cast<unsigned long>(
156 reinterpret_cast<size_t>(kHashSeed) & 0xFFFFFFFF);
157 XML_SetHashSalt(ctx.fXMLParser, seed ? seed : 1);
158
159 XML_SetUserData(ctx.fXMLParser, &ctx);
160 XML_SetElementHandler(ctx.fXMLParser, start_element_handler, end_element_handler);
161 XML_SetCharacterDataHandler(ctx.fXMLParser, text_handler);
162
163 // Disable entity processing, to inhibit internal entity expansion. See expat CVE-2013-0340.
164 XML_SetEntityDeclHandler(ctx.fXMLParser, entity_decl_handler);
165
166 XML_Status status = XML_STATUS_OK;
167 if (docStream.getMemoryBase() && docStream.hasLength()) {
168 const char* base = reinterpret_cast<const char*>(docStream.getMemoryBase());
169 status = XML_Parse(ctx.fXMLParser,
170 base + docStream.getPosition(),
171 docStream.getLength() - docStream.getPosition(),
172 true);
173 } else {
174 static constexpr int kBufferSize = 4096;
175 bool done = false;
176 do {
177 void* buffer = XML_GetBuffer(ctx.fXMLParser, kBufferSize);
178 if (!buffer) {
179 SkDEBUGF("could not buffer enough to continue\n");
180 return false;
181 }
182
183 size_t len = docStream.read(buffer, kBufferSize);
184 done = docStream.isAtEnd();
185 status = XML_ParseBuffer(ctx.fXMLParser, SkToS32(len), done);
186 if (XML_STATUS_ERROR == status) {
187 break;
188 }
189 } while (!done);
190 }
191 if (XML_STATUS_ERROR == status) {
192#if defined(SK_DEBUG)
193 XML_Error error = XML_GetErrorCode(ctx.fXMLParser);
194 int line = XML_GetCurrentLineNumber(ctx.fXMLParser);
195 int column = XML_GetCurrentColumnNumber(ctx.fXMLParser);
196 const XML_LChar* errorString = XML_ErrorString(error);
197 SkDEBUGF("parse error @%d:%d: %d (%s).\n", line, column, error, errorString);
198#endif
199 return false;
200 }
201
202 return true;
203}
204
205bool SkXMLParser::parse(const char doc[], size_t len)
206{
207 SkMemoryStream docStream(doc, len);
208 return this->parse(docStream);
209}
210
212{
213
214}
215
216bool SkXMLParser::startElement(const char elem[])
217{
218 return this->onStartElement(elem);
219}
220
221bool SkXMLParser::addAttribute(const char name[], const char value[])
222{
223 return this->onAddAttribute(name, value);
224}
225
226bool SkXMLParser::endElement(const char elem[])
227{
228 return this->onEndElement(elem);
229}
230
231bool SkXMLParser::text(const char text[], int len)
232{
233 return this->onText(text, len);
234}
235
236////////////////////////////////////////////////////////////////////////////////
237
238bool SkXMLParser::onStartElement(const char elem[]) {return false; }
239bool SkXMLParser::onAddAttribute(const char name[], const char value[]) {return false; }
240bool SkXMLParser::onEndElement(const char elem[]) { return false; }
241bool SkXMLParser::onText(const char text[], int len) {return false; }
static void done(const char *config, const char *src, const char *srcOptions, const char *name)
Definition: DM.cpp:263
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkDEBUGF(...)
Definition: SkDebug.h:24
static void XMLCALL start_element_handler(void *data, const char *tag, const char **attributes)
static void XMLCALL end_element_handler(void *data, const char *tag)
static const XML_Memory_Handling_Suite sk_XML_alloc
SK_API void sk_free(void *)
static void * sk_malloc_throw(size_t size)
Definition: SkMalloc.h:67
SK_API void * sk_realloc_throw(void *buffer, size_t size)
static const size_t kBufferSize
Definition: SkString.cpp:27
constexpr int32_t SkToS32(S x)
Definition: SkTo.h:25
#define HANDLER_CONTEXT(arg, name)
Definition: SkXMLParser.cpp:93
static char const *const gErrorStrings[]
Definition: SkXMLParser.cpp:19
virtual size_t getPosition() const
Definition: SkStream.h:119
virtual bool isAtEnd() const =0
virtual size_t getLength() const
Definition: SkStream.h:137
virtual const void * getMemoryBase()
Definition: SkStream.h:141
virtual bool hasLength() const
Definition: SkStream.h:135
virtual size_t read(void *buffer, size_t size)=0
void set(const SkString &src)
Definition: SkString.h:186
void append(const char text[])
Definition: SkString.h:203
ErrorCode fCode
Definition: SkXMLParser.h:46
virtual ~SkXMLParserError()
Definition: SkXMLParser.cpp:34
virtual void getErrorString(SkString *str) const
Definition: SkXMLParser.cpp:39
bool startElement(const char elem[])
bool endElement(const char elem[])
virtual ~SkXMLParser()
virtual bool onEndElement(const char elem[])
static void GetNativeErrorString(int nativeErrorCode, SkString *str)
virtual bool onAddAttribute(const char name[], const char value[])
virtual bool onText(const char text[], int len)
virtual bool onStartElement(const char elem[])
bool addAttribute(const char name[], const char value[])
bool text(const char text[], int len)
bool parse(const char doc[], size_t len)
SkXMLParser(SkXMLParserError *parserError=nullptr)
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
std::u16string text
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
parser
Definition: zip.py:78
const int kNoError
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63