We build a free browser viewer for engineering drawings, and one rule shaped every decision: the drawing never leaves the user's computer. Company drawings are often confidential, so opening, converting and printing all happen inside the browser tab. Nothing is uploaded unless the user chooses to send a drawing that would not open, so we can look into it.

For DWG files that means running a DWG reader in the browser. We use LibreDWG compiled to WebAssembly through libredwg-web (v0.7.15), convert the DWG to DXF in memory, and render the DXF.

In the browser build, DWG files whose object handles were longer than 4 bytes could not be read. The reader logged:

Invalid handle-reference, longer than 8 bytes 

But handles of 5 to 8 bytes are valid — the message itself says the limit should be 8.

The cause: sizeof (pointer) on wasm32

In src/bits.c, bit_read_H reads an object handle and rejects anything too long:

// size must not exceed 8 if (handle->size > sizeof (BITCODE_RC *) || handle->code > 14) 

The comment says 8, and on a normal 64-bit build sizeof (BITCODE_RC *) is 8. But WebAssembly in the browser is wasm32: pointers are 4 bytes. So in the browser build the limit silently became 4, and every handle of 5–8 bytes was treated as invalid.

The fix is to say what the comment means:

if (handle->size > 8 || handle->code > 14) 

(the same check appears once more for pre-R13 files and got the same change).

A second limit in the object map

bit_read_UMC (modular char) read at most 8 bytes. A 64-bit value written 7 bits per byte can need up to 10, so we raised MAX_BYTE_UMC from 8 to 10 so that 64-bit handle offsets in the object map parse.

The third and last change only marks the build: the DXF 999 comment now says LibreDWG 0.13.3.7825.246_0c9ab+an1, so a DXF made by our modified engine can be told apart from upstream.

That is the whole patch — three small edits.

Building it

The build is the upstream libredwg-web recipe with Emscripten: autogen.sh, emconfigure ../configure ... --disable-write --disable-json ..., emmake make, then link the embind bindings with em++ (-s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=4GB -s EXPORT_ES6=1 -s MODULARIZE=1). The only difference from upstream at the link step is emcc → em++. Exact commands are in BUILD.txt in the repository below.

What it is used for

The engine powers the DWG step of AUTONEST MultiViewer:

  • open a DWG or DXF in the browser — no install, no sign-up, no upload
  • layers and measuring on the drawing
  • title-block batch plot: it finds every title block on the sheet and plots them into one PDF, one page per title block (monochrome or with your own CTB plot style) — all in the browser
  • several drawings in tabs or a split screen, markups, and meeting minutes written on the drawing in the same workspace

Limits we are honest about: the DWG is converted to DXF to be displayed, so some entities may be missing or look different, and saving back to DWG is not supported.

Source and license

LibreDWG is GPL-3.0-or-later, so the modified engine is too. The patch and build notes are public:

If you ship LibreDWG to the browser yourself, check for sizeof of a pointer used as a size limit — on wasm32 it is 4, not 8.

— AUTONEST