crossbind
GitHub
← すべてのライブラリ

Expat

ストリーミング XML パーサー。

インストール

ブラウザーで動きます。Node.js とエッジの実行環境も同じパッケージを使います。ビルド対象:wasm32、シングルスレッドとマルチスレッド。

shell
npm install @crossbind/port-expat-wasm@beta
crossbind.config.js
import expatWasm from '@crossbind/port-expat-wasm/crossbind.config.js';
 
export default {
dependencies: [expatWasm],
paths: { config: import.meta.url },
};
main.js
import { initNative, XML_ExpatVersionInfo } from '@crossbind/port-expat/expat.h';
 
await initNative();

crossbind 本体はバンドラーのプラグインと一緒に入ります。Vite、Webpack、Rspack、Rsbuild、Rollup、esbuild、Bun、Next.js についてはバンドラープラグインを参照してください。

ブラウザーで動く Expat:DOMParser では扱えない XML に

ブラウザーは DOMParser で XML を解析しますが、DOMParser は文書全体を 1 つの文字列として必要とし、Web Worker には存在しません。これらのアプリは、crossbind でコンパイルしたアップストリームの Expat C ライブラリをワーカーの中で実行します。1 GB のデータをメモリ使用量を一定に保ったままストリーミングし、billion laughs 攻撃を行と列の位置で止め、GPS のトラックを名前空間で読みます。 初回の実行時に 0.6 MB の WebAssembly を一度だけダウンロードします。このページのすべてのアプリがそれを共有し、何もアップロードされません。

アプリ 01

1 GB の XML をメモリに保持せずに解析

Expat は XML をストリームとして読みます。断片が入るとコールバックが呼ばれ、何も溜まりません。ここでは、生成しながら渡す最大 1.07 GB の OpenStreetMap 形式の文書を、モジュールが動く Web Worker の中で解析し、メモリの大きさは最初から最後まで変わりません。DOMParser は文書全体を 1 つの文字列として必要としますが、Chrome では文字列の長さが 536,870,888 文字までに制限されており、Worker には DOMParser 自体がありません。

文書が丸ごと存在することはありません。1 MB ごとに生成、解析、破棄します。選んだファイルも同じように約 1 MB ずつ読みます。ファイルはこのタブの中にとどまり、アップロードされることはありません。

文書を生成するか、任意のサイズの XML ファイルを選ぶと、それが Expat をストリーミングで通る様子を確認できます。
コードを表示
src/native/xml_firehose.h
// src/native/xml_firehose.h (excerpt)
std::string step(double bytes) {
const double until = parsed + std::max(bytes, 1.0);
while (!finished && parsed < until) {
chunk.clear();
const bool more = generator->fill(chunk, PIECE); // the next 1 MiB of XML
parse(chunk.data(), chunk.size(), !more);
}
return stats(); // counts so far, as JSON
}
 
void parse(const char* data, size_t length, bool last) {
parsed += static_cast<double>(length);
settle(XML_Parse(parser.get(), data, static_cast<int>(length), last));
}
main.js
const m = await initNative();
const hose = await new m.XmlFirehose();
await hose.generate(12000000); // 1.07 GB, made as it is parsed
let stats;
do {
stats = JSON.parse(await hose.step(32e6)); // the next 32 MB
show(stats.bytes, stats.elements, stats.memory);
} while (!stats.done);
 
// A file the visitor picks, as raw bytes, about 1 MB at a time:
await hose.begin(file.size);
// for each piece of file.stream(): await hose.feed(bytes, last)
アプリ 02

billion laughs 攻撃を行と列の位置で阻止

XML パーサーに対する 4 つの古典的な攻撃を、このタブの Expat に対して実行します。エンティティ爆弾は Expat の増幅の上限に引っかかります。上限はここで緩めたり厳しくしたりできます。外部エンティティはコールバックとしてコードに届くだけで、取得されることはありません。Expat には独自の入れ子の上限がないので、このラボでは開始ハンドラーで上限を加えます。

攻撃用の文書は、実行したときにモジュールの中で作られます。ページには悪意のあるものは何も含まれていません。

攻撃を選んで実行すると、Expat がどこでそれを止め、それまでにどれだけ通過したかがわかります。
コードを表示
src/native/attack_lab.h
// src/native/attack_lab.h (excerpt)
XML_SetBillionLaughsAttackProtectionMaximumAmplification(parser, maxAmplification);
XML_SetBillionLaughsAttackProtectionActivationThreshold(parser, activationBytes);
XML_SetExternalEntityRefHandler(parser, onExternal); // logs, loads nothing
 
static void XMLCALL onStart(void* data, const XML_Char*, const XML_Char**) {
Run& run = *static_cast<Run*>(data);
run.maxDepth = std::max(run.maxDepth, ++run.depth);
if (run.depthLimit > 0 && run.depth > run.depthLimit) {
XML_StopParser(run.parser.get(), XML_FALSE); // Expat has no depth limit
}
}
main.js
const m = await initNative();
const xml = await m.AttackLab.build('laughs', 9); // 784 bytes
const result = JSON.parse(await m.AttackLab.parse(xml, 100, 8 * 1024 * 1024, 0));
// result.error 'limit on input amplification factor (from DTD and entities) breached'
// result.line 14, result.column 6
// result.textBytes 0: nothing got through before the stop
アプリ 03

心拍数も含めて GPS のトラックを読む

GPX ファイルは、心拍数とケイデンスを拡張の名前空間に、書き出したアプリが選んだ任意の接頭辞で保存します。Expat の名前空間モードは、ハンドラーが名前を受け取る前にすべての接頭辞を URI に解決するので、このリーダーはどんな表記でも心拍数を見つけられます。ファイルを 64 KiB ずつ読みながらストリーミングし、トラックを描き、獲得標高を合計して、GeoJSON を書き出します。

サンプルは、1 秒ごとに記録した 64 km の周回コースで、モジュールの中で生成します。手元のファイルは、このタブにあるモジュールのメモリ上のファイルシステムにコピーされ、アップロードされることはありません。GPX 1.0 と 1.1 のファイルのトラックを読みます。ルートとウェイポイントは読みません。

サンプルのライドか、時計やアプリの .gpx を開くと、その中のトラックを表示します。
コードを表示
src/support/gpx.h
// src/support/gpx.h (excerpt): names arrive as "<namespace URI>|<local name>"
Reader() : parser(XML_ParserCreateNS(nullptr, '|'), XML_ParserFree) { ... }
 
static bool isHeartRate(const char* name) {
const size_t length = std::strlen(TRACKPOINT_V1); // Garmin's v1 and v2 URIs
return (std::strncmp(name, TRACKPOINT_V1, length) == 0 ||
std::strncmp(name, TRACKPOINT_V2, length) == 0) &&
std::strcmp(name + length, "|hr") == 0; // gpxtpx:hr, ns3:hr, any prefix
}
main.js
const m = await initNative();
const [path] = await m.autoMountFiles([file], await m.getRandomPath('/memfs')); // the .gpx you picked
const track = JSON.parse(await m.GpxTrack.summary(path));
// track.lengthKm, track.gain, track.avgHr, track.namespaces,
// track.line: [[lon, lat], ...] to draw
const geojson = await m.GpxTrack.geojson(path); // one LineString per segment

サンプル

それぞれ、小さな C++ のヘッダーファイルと、それを呼び出す JavaScript、そしてサイトのビルド時に出力された内容です。JavaScript のみのタブは、C++ ファイルなしで Expat 自身のヘッダーファイルを使って同じことをします。最後の 1 つは、WASI 向けにビルドした完全な C++ プログラムです。

XML を JavaScript のオブジェクトに変換する

Expat を使うコードの多くが行う呼び出し:XML_ParserCreate、要素ハンドラーと文字データハンドラー、そして XML_Parse。不正な文書では、Expat が止まった行と列を含む例外が投げられます。

src/native/xml_tree.h
#pragma once
 
#include <expat.h>
 
#include <stdexcept>
#include <string>
#include <vector>
 
// XML in, JSON out. Each element becomes {"name","attributes","children","text"}: `children` holds
// its child elements and `text` its own character data, left out when it is only whitespace.
// Malformed XML throws Expat's message with the line and column where parsing stopped.
class XmlTree {
public:
static std::string version() {
const XML_Expat_Version v = XML_ExpatVersionInfo();
return std::to_string(v.major) + "." + std::to_string(v.minor) + "." + std::to_string(v.micro);
}
 
static std::string parse(const std::string& xml) {
Builder builder;
XML_Parser parser = XML_ParserCreate(nullptr);
if (!parser) throw std::runtime_error("out of memory");
XML_SetUserData(parser, &builder);
XML_SetElementHandler(parser, onStart, onEnd);
XML_SetCharacterDataHandler(parser, onText);
const bool ok = XML_Parse(parser, xml.data(), static_cast<int>(xml.size()), XML_TRUE) == XML_STATUS_OK;
const std::string error = ok ? "" : std::string(XML_ErrorString(XML_GetErrorCode(parser))) + " at line " +
std::to_string(XML_GetCurrentLineNumber(parser)) + ", column " +
std::to_string(XML_GetCurrentColumnNumber(parser));
XML_ParserFree(parser);
if (!ok) throw std::runtime_error(error);
return builder.json;
}
 
private:
struct Builder {
std::string json;
std::vector<std::string> text; // character data of each open element
std::vector<bool> hasChildren;
};
 
// Expat delivers UTF-8; JSON only needs quotes, backslashes and control characters escaped.
static std::string quote(const std::string& value) {
std::string out = "\"";
for (const char c : value) {
if (c == '"' || c == '\\') {
out += '\\';
out += c;
} else if (c == '\n') {
out += "\\n";
} else if (c == '\t') {
out += "\\t";
} else if (c == '\r') {
out += "\\r";
} else {
out += c;
}
}
return out + "\"";
}
 
static void XMLCALL onStart(void* data, const XML_Char* name, const XML_Char** attributes) {
Builder& builder = *static_cast<Builder*>(data);
if (!builder.hasChildren.empty()) {
if (builder.hasChildren.back()) builder.json += ",";
builder.hasChildren.back() = true;
}
builder.json += "{\"name\":" + quote(name) + ",\"attributes\":{";
for (int i = 0; attributes[i]; i += 2) builder.json += (i ? "," : "") + quote(attributes[i]) + ":" + quote(attributes[i + 1]);
builder.json += "},\"children\":[";
builder.text.emplace_back();
builder.hasChildren.push_back(false);
}
 
static void XMLCALL onText(void* data, const XML_Char* text, int length) {
static_cast<Builder*>(data)->text.back().append(text, static_cast<size_t>(length));
}
 
static void XMLCALL onEnd(void* data, const XML_Char*) {
Builder& builder = *static_cast<Builder*>(data);
const std::string& text = builder.text.back();
builder.json += "]";
if (text.find_first_not_of(" \t\r\n") != std::string::npos) builder.json += ",\"text\":" + quote(text);
builder.json += "}";
builder.text.pop_back();
builder.hasChildren.pop_back();
}
};
main.js
import { initNative, XmlTree } from './native/xml_tree.h';
 
await initNative();
console.log('Expat', await XmlTree.version());
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="1" lang="en"><title>Dune</title><price currency="EUR">9.99</price></book>
<book id="2" lang="fr"><title>L'Étranger</title><price currency="EUR">7.50</price></book>
<book id="3" lang="en"><title>Pride &amp; Prejudice</title><price currency="GBP">5.25</price></book>
</catalog>`;
const catalog = JSON.parse(await XmlTree.parse(xml));
for (const book of catalog.children) {
const [title, price] = book.children;
console.log(book.attributes.id, book.attributes.lang, title.text, price.text, price.attributes.currency);
}
 
try {
await XmlTree.parse('<catalog>\n <book id="4"><title>Emma</book>\n</catalog>');
} catch (error) {
console.log(error.cppMessage ?? error.message);
}
出力初回は 0.6 MB をダウンロード
Expat 2.11.0
1 en Dune 9.99 EUR
2 fr L'Étranger 7.50 EUR
3 en Pride & Prejudice 5.25 GBP
mismatched tag at line 2, column 30

ファイルを Expat にストリーミングする

1 つの文字列に収めるべきでない文書向けです。XML_GetBuffer が Expat 自身のバッファーを渡し、fread がそれを満たし、XML_ParseBuffer が解析します。一度に 64 KiB ずつ処理します。

src/native/xml_stream.h
#pragma once
 
#include <expat.h>
 
#include <cstdio>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
 
// Counts the elements of an XML file by name. The file is read in fixed-size pieces straight into
// Expat's own buffer (XML_GetBuffer, then XML_ParseBuffer), so memory stays the same at any size.
class XmlStream {
public:
// JSON: {"bytes":N,"reads":R,"elements":{"name":count,...}}
static std::string countElements(const std::string& path, int chunkBytes) {
if (chunkBytes < 1024) throw std::invalid_argument("read at least 1 KiB at a time");
std::unique_ptr<FILE, int (*)(FILE*)> file(std::fopen(path.c_str(), "rb"), std::fclose);
if (!file) throw std::runtime_error("cannot open " + path);
std::unique_ptr<XML_ParserStruct, void (*)(XML_Parser)> parser(XML_ParserCreate(nullptr), XML_ParserFree);
if (!parser) throw std::runtime_error("out of memory");
 
std::map<std::string, double> counts;
XML_SetUserData(parser.get(), &counts);
XML_SetStartElementHandler(parser.get(), [](void* data, const XML_Char* name, const XML_Char**) {
(*static_cast<std::map<std::string, double>*>(data))[name] += 1;
});
 
double bytes = 0;
int reads = 0;
for (bool last = false; !last;) {
void* buffer = XML_GetBuffer(parser.get(), chunkBytes);
if (!buffer) throw std::runtime_error("out of memory");
const size_t got = std::fread(buffer, 1, static_cast<size_t>(chunkBytes), file.get());
if (std::ferror(file.get())) throw std::runtime_error("cannot read " + path);
last = got == 0;
bytes += static_cast<double>(got);
reads += last ? 0 : 1;
if (XML_ParseBuffer(parser.get(), static_cast<int>(got), last) != XML_STATUS_OK) {
throw std::runtime_error(std::string(XML_ErrorString(XML_GetErrorCode(parser.get()))) + " at line " +
std::to_string(XML_GetCurrentLineNumber(parser.get())) + ", column " +
std::to_string(XML_GetCurrentColumnNumber(parser.get())));
}
}
 
std::string elements;
for (const auto& [name, count] : counts) elements += (elements.empty() ? "\"" : ",\"") + name + "\":" + std::to_string(static_cast<long long>(count));
return "{\"bytes\":" + std::to_string(static_cast<long long>(bytes)) + ",\"reads\":" + std::to_string(reads) + ",\"elements\":{" + elements + "}}";
}
};
main.js
import { initNative } from './native/xml_stream.h';
 
const m = await initNative();
const { XmlStream } = m;
// A sitemap with 50,000 URLs, the most the sitemap protocol allows in one file.
const urls = Array.from({ length: 50000 }, (_, i) => ` <url><loc>https://example.com/products/${i + 1}</loc><lastmod>2026-09-${String((i % 28) + 1).padStart(2, '0')}</lastmod></url>`);
// m.FS.writeFile adds to a file that already exists, so every run gets a fresh directory.
const dir = await m.getRandomPath('/memfs');
await m.FS.writeFile(`${dir}/sitemap.xml`, `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls.join('\n')}\n</urlset>\n`);
 
const stats = JSON.parse(await XmlStream.countElements(`${dir}/sitemap.xml`, 65536));
console.log(`${stats.bytes} B in ${stats.reads} reads of 64 KiB`);
for (const [name, count] of Object.entries(stats.elements)) console.log(name, count);
出力初回は 0.6 MB をダウンロード
4389004 B in 67 reads of 64 KiB
lastmod 50000
loc 50000
url 50000
urlset 1

接頭辞に関係なく、名前空間付きの XML を読む

XML_ParserCreateNS は、ハンドラーが名前を受け取る前に、すべての接頭辞を名前空間 URI に解決します。XML_SetStartNamespaceDeclHandler は宣言そのものを報告します。

src/native/xml_names.h
#pragma once
 
#include <expat.h>
 
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
 
// Namespace-aware parsing. XML_ParserCreateNS hands every name over as "<namespace URI>|<local name>",
// so what a document calls its prefixes no longer matters; names outside any namespace stay bare.
class XmlNames {
public:
// The text inside every element named `local` in the namespace `uri`, as a JSON array of strings.
static std::string textOf(const std::string& xml, const std::string& uri, const std::string& local) {
Search search;
search.name = uri.empty() ? local : uri + "|" + local;
Parser parser = create(&search);
XML_SetElementHandler(parser.get(), onStart, onEnd);
XML_SetCharacterDataHandler(parser.get(), onText);
parse(parser.get(), xml);
std::string json = "[";
for (const std::string& text : search.found) json += (json.size() > 1 ? "," : "") + quote(text);
return json + "]";
}
 
// Every prefix the document declares, with its URI, as JSON; "" is the default namespace.
static std::string declarations(const std::string& xml) {
std::map<std::string, std::string> declared;
Parser parser = create(&declared);
XML_SetStartNamespaceDeclHandler(parser.get(), [](void* data, const XML_Char* prefix, const XML_Char* uri) {
(*static_cast<std::map<std::string, std::string>*>(data))[prefix ? prefix : ""] = uri ? uri : "";
});
parse(parser.get(), xml);
std::string json = "{";
for (const auto& [prefix, uri] : declared) json += (json.size() > 1 ? "," : "") + quote(prefix) + ":" + quote(uri);
return json + "}";
}
 
private:
using Parser = std::unique_ptr<XML_ParserStruct, void (*)(XML_Parser)>;
 
struct Search {
std::string name;
std::vector<std::string> open; // the text of each open element with that name
std::vector<std::string> found;
};
 
static Parser create(void* data) {
Parser parser(XML_ParserCreateNS(nullptr, '|'), XML_ParserFree);
if (!parser) throw std::runtime_error("out of memory");
XML_SetUserData(parser.get(), data);
return parser;
}
 
static void parse(XML_Parser parser, const std::string& xml) {
if (XML_Parse(parser, xml.data(), static_cast<int>(xml.size()), XML_TRUE) != XML_STATUS_OK) {
throw std::runtime_error(std::string(XML_ErrorString(XML_GetErrorCode(parser))) + " at line " +
std::to_string(XML_GetCurrentLineNumber(parser)) + ", column " +
std::to_string(XML_GetCurrentColumnNumber(parser)));
}
}
 
static void XMLCALL onStart(void* data, const XML_Char* name, const XML_Char**) {
Search& search = *static_cast<Search*>(data);
if (search.name == name) search.open.emplace_back();
}
 
static void XMLCALL onText(void* data, const XML_Char* text, int length) {
Search& search = *static_cast<Search*>(data);
if (!search.open.empty()) search.open.back().append(text, static_cast<size_t>(length));
}
 
static void XMLCALL onEnd(void* data, const XML_Char* name) {
Search& search = *static_cast<Search*>(data);
if (search.name != name) return;
search.found.push_back(search.open.back());
search.open.pop_back();
}
 
static std::string quote(const std::string& value) {
std::string out = "\"";
for (const char c : value) {
if (c == '"' || c == '\\') {
out += '\\';
out += c;
} else if (c == '\n') {
out += "\\n";
} else if (c == '\t') {
out += "\\t";
} else if (c == '\r') {
out += "\\r";
} else {
out += c;
}
}
return out + "\"";
}
};
main.js
import { initNative, XmlNames } from './native/xml_names.h';
 
await initNative();
const GPX = 'http://www.topografix.com/GPX/1/1';
const HR = 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1';
// The same heart rates, written by two exporters that picked different prefixes for Garmin's extension.
const track = (prefix) => `<gpx version="1.1" creator="example" xmlns="${GPX}" xmlns:${prefix}="${HR}">
<trk><trkseg>
<trkpt lat="46.5190" lon="6.5668"><extensions><${prefix}:TrackPointExtension><${prefix}:hr>128</${prefix}:hr></${prefix}:TrackPointExtension></extensions></trkpt>
<trkpt lat="46.5192" lon="6.5671"><extensions><${prefix}:TrackPointExtension><${prefix}:hr>131</${prefix}:hr></${prefix}:TrackPointExtension></extensions></trkpt>
</trkseg></trk>
</gpx>`;
for (const prefix of ['gpxtpx', 'ns3']) {
const rates = JSON.parse(await XmlNames.textOf(track(prefix), HR, 'hr'));
console.log(`${prefix}:hr`, rates.join(' '));
}
console.log('hr in the GPX namespace:', JSON.parse(await XmlNames.textOf(track('gpxtpx'), GPX, 'hr')).length);
console.log(await XmlNames.declarations(track('ns3')));
出力初回は 0.6 MB をダウンロード
gpxtpx:hr 128 131
ns3:hr 128 131
hr in the GPX namespace: 0
{"":"http://www.topografix.com/GPX/1/1","ns3":"http://www.garmin.com/xmlschemas/TrackPointExtension/v1"}

billion laughs 攻撃を防いでエンティティを展開する

Expat は文書が宣言するエンティティを展開し、爆発的に膨らむものは止めます。どこまで許すかは XML_SetBillionLaughsAttackProtectionMaximumAmplification と …ActivationThreshold で設定します。

src/native/xml_guard.h
#pragma once
 
// expat.h declares the amplification limits only when XML_GE is 1; this build compiles them in.
#ifndef XML_GE
#define XML_GE 1
#endif
#include <expat.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// Untrusted XML with entities expanded under Expat's limits. Once `activationBytes` of input and
// entity text have been processed, the expansion may be at most `maxAmplification` times the input;
// Expat's defaults are 100 and 8 MiB. A breach stops the parse like any other error.
class XmlGuard {
public:
// The document's character data, entities expanded.
static std::string text(const std::string& xml, double maxAmplification, double activationBytes) {
std::unique_ptr<XML_ParserStruct, void (*)(XML_Parser)> parser(XML_ParserCreate(nullptr), XML_ParserFree);
if (!parser) throw std::runtime_error("out of memory");
if (!XML_SetBillionLaughsAttackProtectionMaximumAmplification(parser.get(), static_cast<float>(maxAmplification))) {
throw std::invalid_argument("the maximum amplification must be at least 1");
}
if (activationBytes < 0 || !XML_SetBillionLaughsAttackProtectionActivationThreshold(parser.get(), static_cast<unsigned long long>(activationBytes))) {
throw std::invalid_argument("the activation threshold must be a byte count");
}
std::string text;
XML_SetUserData(parser.get(), &text);
XML_SetCharacterDataHandler(parser.get(), [](void* data, const XML_Char* chunk, int length) {
static_cast<std::string*>(data)->append(chunk, static_cast<size_t>(length));
});
if (XML_Parse(parser.get(), xml.data(), static_cast<int>(xml.size()), XML_TRUE) != XML_STATUS_OK) {
throw std::runtime_error(std::string(XML_ErrorString(XML_GetErrorCode(parser.get()))) + " at line " +
std::to_string(XML_GetCurrentLineNumber(parser.get())) + ", column " +
std::to_string(XML_GetCurrentColumnNumber(parser.get())));
}
return text;
}
};
main.js
import { initNative, XmlGuard } from './native/xml_guard.h';
 
await initNative();
const MAX_AMPLIFICATION = 100; // Expat's defaults
const THRESHOLD = 8 * 1024 * 1024;
console.log(await XmlGuard.text('<!DOCTYPE note [<!ENTITY product "crossbind">]><note>&product; parses &product;</note>', MAX_AMPLIFICATION, THRESHOLD));
 
// Each level repeats the one below ten times: 9 levels would expand to 3 GB.
const laughs = (levels) => {
const entities = ['<!ENTITY lol0 "lol">'];
for (let level = 1; level <= levels; level += 1) entities.push(`<!ENTITY lol${level} "${`&lol${level - 1};`.repeat(10)}">`);
return `<?xml version="1.0"?>\n<!DOCTYPE lolz [\n${entities.join('\n')}\n]>\n<lolz>&lol${levels};</lolz>`;
};
console.log('5 levels:', (await XmlGuard.text(laughs(5), MAX_AMPLIFICATION, THRESHOLD)).length, 'characters');
try {
await XmlGuard.text(laughs(9), MAX_AMPLIFICATION, THRESHOLD);
} catch (error) {
console.log('9 levels:', error.cppMessage ?? error.message);
}
try {
await XmlGuard.text(laughs(5), MAX_AMPLIFICATION, 64 * 1024);
} catch (error) {
console.log('5 levels, 64 KiB threshold:', error.cppMessage ?? error.message);
}
出力初回は 0.6 MB をダウンロード
crossbind parses crossbind
5 levels: 300000 characters
9 levels: limit on input amplification factor (from DTD and entities) breached at line 14, column 6
5 levels, 64 KiB threshold: limit on input amplification factor (from DTD and entities) breached at line 10, column 6

XML の要約コマンド

WASI には JavaScript のバインディングはありません。プログラムはライブラリをリンクする main() で、1 つの .wasm にビルドして wasmtime で実行します。これは各ファイルを 64 KiB ずつ読みながら Expat に流し、中身の概要か、最初のエラーを行と列とともに出力します。

crossbind.config.js
import expatWasi from '@crossbind/port-expat-wasi/crossbind.config.js';
 
export default {
general: { name: 'xml-stats' },
dependencies: [expatWasi],
paths: { config: import.meta.url },
};
src/native/main.cpp
// A command-line XML summary for WASI:
// xml-stats <file>...
// Each file streams through Expat in 64 KiB reads (XML_GetBuffer, XML_ParseBuffer), so any size
// fits. Well-formed files get their size, element and attribute counts, depth and most common
// elements; the first error ends that file with Expat's message and position, and the exit code 1.
#include <expat.h>
 
#include <algorithm>
#include <cstdio>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
 
namespace {
 
struct Stats {
std::map<std::string, long long> elements;
long long attributes = 0;
int depth = 0;
int maxDepth = 0;
};
 
void XMLCALL onStart(void* data, const XML_Char* name, const XML_Char** atts) {
Stats& stats = *static_cast<Stats*>(data);
stats.elements[name] += 1;
for (int i = 0; atts[i]; i += 2) stats.attributes += 1;
stats.maxDepth = std::max(stats.maxDepth, ++stats.depth);
}
 
void XMLCALL onEnd(void* data, const XML_Char*) { static_cast<Stats*>(data)->depth -= 1; }
 
const char* plural(long long count) { return count == 1 ? "" : "s"; }
 
bool summarize(const char* path) {
std::unique_ptr<FILE, int (*)(FILE*)> file(std::fopen(path, "rb"), std::fclose);
if (!file) {
std::fprintf(stderr, "%s: cannot open\n", path);
return false;
}
std::unique_ptr<XML_ParserStruct, void (*)(XML_Parser)> parser(XML_ParserCreate(nullptr), XML_ParserFree);
if (!parser) {
std::fprintf(stderr, "out of memory\n");
return false;
}
Stats stats;
XML_SetUserData(parser.get(), &stats);
XML_SetElementHandler(parser.get(), onStart, onEnd);
 
long long bytes = 0;
for (bool last = false; !last;) {
void* buffer = XML_GetBuffer(parser.get(), 1 << 16);
if (!buffer) {
std::fprintf(stderr, "out of memory\n");
return false;
}
const size_t got = std::fread(buffer, 1, 1 << 16, file.get());
last = got == 0;
bytes += static_cast<long long>(got);
if (XML_ParseBuffer(parser.get(), static_cast<int>(got), last) != XML_STATUS_OK) {
std::fprintf(stderr, "%s: %s at line %lu, column %lu\n", path, XML_ErrorString(XML_GetErrorCode(parser.get())),
static_cast<unsigned long>(XML_GetCurrentLineNumber(parser.get())), static_cast<unsigned long>(XML_GetCurrentColumnNumber(parser.get())));
return false;
}
}
 
std::vector<std::pair<std::string, long long>> top(stats.elements.begin(), stats.elements.end());
std::sort(top.begin(), top.end(), [](const auto& a, const auto& b) { return a.second != b.second ? a.second > b.second : a.first < b.first; });
long long total = 0;
for (const auto& entry : top) total += entry.second;
std::printf("%s: %lld bytes of well-formed XML (%s)\n", path, bytes, XML_ExpatVersion());
std::printf("%lld element%s, %lld attribute%s, nested %d deep\n", total, plural(total), stats.attributes, plural(stats.attributes), stats.maxDepth);
for (size_t i = 0; i < top.size() && i < 5; i += 1) std::printf("%s %lld\n", top[i].first.c_str(), top[i].second);
return true;
}
 
} // namespace
 
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: xml-stats <file>...\n");
return 2;
}
bool ok = true;
for (int i = 1; i < argc; i += 1) ok = summarize(argv[i]) && ok;
return ok ? 0 : 1;
}
shell
npx crossbind build -p wasi -e wasi -b release
wasmtime run --dir=. .crossbind/build/xml-stats-wasi-wasm32-st-release.wasm sitemap.xml
output
sitemap.xml: 4389004 bytes of well-formed XML (expat_2.11.0)
150001 elements, 1 attribute, nested 3 deep
lastmod 50000
loc 50000
url 50000
urlset 1
入力すると、ガイドのすべてのページとセクションを検索します。
↑↓ 移動↵ 開くesc 閉じる