/* Entry points for johnmcm.com's File Opener drawings: WordPerfect Graphics * (libwpg, with libwpd for the text a WPG file keeps as WordPerfect text) and * Zoner Draw / Zoner Callisto (libzmf), with librevenge, compiled to * WebAssembly as a module of their own (draw.wasm), beside the Publisher, * CorelDRAW and Visio module (binding.cpp), whose structure and drawing * generator it shares. Their own module, so revenge.wasm (the Publisher * viewer's) does not grow. * * SPDX-License-Identifier: MPL-2.0 * * Every entry point returns a plain object { ok, ..., error } instead of * throwing, so the JS side never deals with WebAssembly.Exception. Traps * (out-of-bounds, abort, stack overflow) still surface as a JS RuntimeError; * the page then discards the instance. * * view = inputBuffer(n) Uint8Array over n bytes of wasm memory; fill it at once * isSupportedInput(fmt) fmt: 0 WordPerfect Graphics (.wpg, WPG 1 and 2), 1 Zoner Draw / Callisto (.zmf) * r = parseInput(fmt) { ok, pages, json, blobs, type?, error?, unsupported?, memory? } * type (Zoner only): libzmf's ZMFDocument::Type (1 Draw/Callisto 4-5, 2 Zebra, 3 Bitmap) * blob(i) Uint8Array view of picture i; copy it out before the next call * release() frees the blobs and any pending input */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "JSONDrawingGenerator.h" using emscripten::val; namespace { enum Format { WPG = 0, ZMF = 1 }; bool supported(int format, librevenge::RVNGInputStream *input, int *type) { switch (format) { case WPG: return libwpg::WPGraphics::isSupported(input); case ZMF: { libzmf::ZMFDocument::Type t = libzmf::ZMFDocument::TYPE_UNKNOWN; bool ok = libzmf::ZMFDocument::isSupported(input, &t); *type = int(t); return ok; } default: return false; } } bool parse(int format, librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter) { switch (format) { case WPG: return libwpg::WPGraphics::parse(input, painter); case ZMF: return libzmf::ZMFDocument::parse(input, painter); default: return false; } } val fail(const std::string &msg) { val r = val::object(); r.set("ok", false); r.set("error", msg); return r; } // The module's memory is capped (-sMAXIMUM_MEMORY): a file that needs more, // or a damaged one that asks for a huge block, ends here with a clear flag. val outOfMemory() { val r = fail("not enough memory for this file"); r.set("memory", true); return r; } /* * Set when an allocation failed during the parse. The libraries catch * exceptions of every kind themselves and report only that parsing failed, * so std::bad_alloc rarely reaches outOfMemory(); this handler notes it * first, then fails the allocation as usual. */ bool g_outOfMemory = false; void noteOutOfMemory() { g_outOfMemory = true; throw std::bad_alloc(); } /** Starts a parse: the flag cleared and the handler in place. */ void watchMemory() { g_outOfMemory = false; std::set_new_handler(noteOutOfMemory); } struct PendingState { unsigned char *input = nullptr; size_t inputSize = 0; std::vector blobs; } g_state; void freeInput() { std::free(g_state.input); g_state.input = nullptr; g_state.inputSize = 0; } void release() { freeInput(); std::vector().swap(g_state.blobs); } val inputBuffer(unsigned size) { release(); if (size == 0) return val::null(); g_state.input = static_cast(std::malloc(size)); if (!g_state.input) return val::null(); g_state.inputSize = size; return val(emscripten::typed_memory_view(size, g_state.input)); } bool isSupportedInput(int format) { if (!g_state.input) return false; try { librevenge::RVNGStringStream input(g_state.input, static_cast(g_state.inputSize)); int type = 0; return supported(format, &input, &type); } catch (...) { return false; } } val parseInput(int format) { if (!g_state.input) return fail("no input: call inputBuffer() first"); watchMemory(); try { std::unique_ptr input( new librevenge::RVNGStringStream(g_state.input, static_cast(g_state.inputSize))); freeInput(); // the stream keeps its own copy int type = 0; if (!supported(format, input.get(), &type)) { val r = fail("unsupported"); r.set("unsupported", true); return r; } input->seek(0, librevenge::RVNG_SEEK_SET); oi::JSONDrawingGenerator generator; bool parsed = parse(format, input.get(), &generator); if (!parsed && g_outOfMemory) { release(); return outOfMemory(); } input.reset(); // free the file before serializing std::string json = generator.result(); g_state.blobs = generator.takeBlobs(); val r = val::object(); r.set("ok", parsed); r.set("pages", generator.pageCount()); r.set("json", json); r.set("blobs", static_cast(g_state.blobs.size())); if (format == ZMF) r.set("type", type); if (!parsed) r.set("error", std::string("parse() returned false")); return r; } catch (const std::bad_alloc &) { release(); return outOfMemory(); } catch (const std::exception &e) { release(); return fail(std::string("C++ exception: ") + e.what()); } catch (...) { release(); return fail("unknown C++ exception"); } } val blob(unsigned index) { if (index >= g_state.blobs.size()) return val::null(); const std::vector &d = g_state.blobs[index].data; return val(emscripten::typed_memory_view(d.size(), d.data())); } double heapSize() { return static_cast(emscripten_get_heap_size()); } } // anonymous namespace EMSCRIPTEN_BINDINGS(draw) { emscripten::function("inputBuffer", &inputBuffer); emscripten::function("isSupportedInput", &isSupportedInput); emscripten::function("parseInput", &parseInput); emscripten::function("blob", &blob); emscripten::function("release", &release); emscripten::function("heapSize", &heapSize); }