/* Entry points for johnmcm.com's old Mac document viewers (AppleWorks / * ClarisWorks, MacWrite and the other programs libmwaw reads): libmwaw and * librevenge, compiled to WebAssembly as a module of its own (mac.wasm), * beside the Publisher/CorelDRAW/Visio and QuarkXPress/PageMaker/FreeHand * modules, whose drawing generator it shares. * * SPDX-License-Identifier: MPL-2.0 * * Word processing files and spreadsheets come back as the Works Viewer's * librevenge events (recorder.h, a copy of wasm-src/wps/src/recorder.h), so * the page builds, shows and saves them with the Works Viewer's code; * drawings and paintings as the drawing generator's JSON. Every entry point * returns a plain object instead of throwing. * * view = inputBuffer(n) Uint8Array over n bytes of wasm memory; fill it at once * d = detect() { confidence, kind, type }: confidence 0 none, 1 encrypted (unsupported), * 2 encrypted (password needed), 3 readable; kind 1 text, 2 draw, 3 paint, * 4 presentation, 5 spreadsheet, 6 database; type is libmwaw's MWAWDocument::Type * r = parseInput(pw) { ok, kind, type, pages, json, blobs, error?, unsupported?, password? } * text, spreadsheet, database: json = the events. A drawing libmwaw keeps * inside a text (image/mwaw-odg) is decoded here: its pictures come as * "insertDrawingBlob" events, then "insertDrawing" { "jm:drawing": JSON }; * draw, paint: json = the drawing, its pictures as blob(i); * presentation: json = {"slides": [svg, ...]} (librevenge's SVG) * 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" #include "recorder.h" using emscripten::val; namespace { struct PendingState { unsigned char *input = nullptr; size_t inputSize = 0; std::vector blobs; } g_state; 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); } 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)); } /* A drawing libmwaw embeds in a text or a spreadsheet (image/mwaw-odg) can * only be read back by libmwaw itself: it is decoded here, into the same JSON * as a drawing file, and recorded as an event of its own. */ template class WithDrawings : public Recorder { public: void insertBinaryObject(const librevenge::RVNGPropertyList &propList) override { const librevenge::RVNGProperty *mime = propList["librevenge:mime-type"]; const librevenge::RVNGProperty *data = propList["office:binary-data"]; if (!mime || !data || std::string(mime->getStr().cstr()) != "image/mwaw-odg") { Recorder::insertBinaryObject(propList); return; } std::vector bytes; const librevenge::RVNGString b64 = data->getStr(); oi::JSONDrawingGenerator drawing; if (!oi::decodeBase64(b64.cstr(), b64.size(), bytes) || bytes.empty()) return; librevenge::RVNGBinaryData binary(bytes.data(), static_cast(bytes.size())); if (!MWAWDocument::decodeGraphic(binary, &drawing)) return; const std::string json = drawing.result(); for (const oi::Blob &b : drawing.takeBlobs()) { librevenge::RVNGPropertyList p; p.insert("librevenge:mime-type", b.mimeType.c_str()); p.insert("office:binary-data", librevenge::RVNGBinaryData(b.data.data(), static_cast(b.data.size()))); this->m_ev.event("insertDrawingBlob", p); } librevenge::RVNGPropertyList p; p.insert("jm:drawing", json.c_str()); this->m_ev.event("insertDrawing", p); } }; val detect() { val r = val::object(); r.set("confidence", 0); if (!g_state.input) return r; try { librevenge::RVNGStringStream input(g_state.input, static_cast(g_state.inputSize)); MWAWDocument::Type type = MWAWDocument::MWAW_T_UNKNOWN; MWAWDocument::Kind kind = MWAWDocument::MWAW_K_UNKNOWN; MWAWDocument::Confidence c = MWAWDocument::isFileFormatSupported(&input, type, kind); r.set("confidence", int(c)); r.set("kind", int(kind)); r.set("type", int(type)); } catch (...) { } return r; } val parseInput(std::string password) { 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 MWAWDocument::Type type = MWAWDocument::MWAW_T_UNKNOWN; MWAWDocument::Kind kind = MWAWDocument::MWAW_K_UNKNOWN; MWAWDocument::Confidence c = MWAWDocument::isFileFormatSupported(input.get(), type, kind); if (c == MWAWDocument::MWAW_C_NONE || c == MWAWDocument::MWAW_C_UNSUPPORTED_ENCRYPTION) { val r = fail(c == MWAWDocument::MWAW_C_NONE ? "unsupported" : "encrypted"); r.set("unsupported", true); r.set("type", int(type)); return r; } input->seek(0, librevenge::RVNG_SEEK_SET); const char *pw = password.empty() ? nullptr : password.c_str(); MWAWDocument::Result result = MWAWDocument::MWAW_R_UNKNOWN_ERROR; std::string json; unsigned pages = 1; if (kind == MWAWDocument::MWAW_K_TEXT) { WithDrawings rec; result = MWAWDocument::parse(input.get(), &rec, pw); json = rec.result(); } else if (kind == MWAWDocument::MWAW_K_SPREADSHEET || kind == MWAWDocument::MWAW_K_DATABASE) { WithDrawings rec; result = MWAWDocument::parse(input.get(), &rec, pw); json = rec.result(); } else if (kind == MWAWDocument::MWAW_K_PRESENTATION) { librevenge::RVNGStringVector slides; librevenge::RVNGSVGPresentationGenerator generator(slides); result = MWAWDocument::parse(input.get(), &generator, pw); oi::Json list = oi::Json::array(); for (unsigned i = 0; i < slides.size(); ++i) list.push(oi::Json::string(slides[i].cstr())); oi::Json doc = oi::Json::object(); pages = slides.size(); doc.set("slides", std::move(list)); doc.serialize(json); } else { oi::JSONDrawingGenerator generator; result = MWAWDocument::parse(input.get(), &generator, pw); json = generator.result(); g_state.blobs = generator.takeBlobs(); pages = generator.pageCount(); } input.reset(); if (result != MWAWDocument::MWAW_R_OK && g_outOfMemory) { release(); return outOfMemory(); } val r = val::object(); r.set("ok", result == MWAWDocument::MWAW_R_OK); r.set("kind", int(kind)); r.set("type", int(type)); r.set("pages", pages); r.set("json", json); r.set("blobs", static_cast(g_state.blobs.size())); if (result == MWAWDocument::MWAW_R_PASSWORD_MISSMATCH_ERROR || (c == MWAWDocument::MWAW_C_SUPPORTED_ENCRYPTION && result != MWAWDocument::MWAW_R_OK)) r.set("password", true); if (result != MWAWDocument::MWAW_R_OK) r.set("error", std::string("parse() returned ") + std::to_string(int(result))); 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(mac) { emscripten::function("inputBuffer", &inputBuffer); emscripten::function("detect", &detect); emscripten::function("parseInput", &parseInput); emscripten::function("blob", &blob); emscripten::function("release", &release); emscripten::function("heapSize", &heapSize); }