xberg-libwpd 1.2.8

WordPerfect (.wpd) text extraction for Xberg via libwpd + librevenge, built from vendored source (MPL-2.0 arm)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/* Flat C shim over libwpd + librevenge for Xberg.
 *
 * libwpd exposes no `extract()` call: it drives librevenge's SAX-like
 * RVNGTextInterface, calling back into a concrete implementation passed to
 * WPDocument::parse. `DocumentBuilder` below is that implementation; it
 * records a flat, format-agnostic `std::vector<Node>` as libwpd walks the
 * document, exposed to Rust as an owned binary blob (see `serialize` and
 * `src/dto.rs`) the Rust side frees and decodes into a typed document model.
 * This shim does no text/Markdown rendering itself — that would throw away
 * structure (table cell spans, list nesting, note numbering) the flat node
 * vector still carries; rendering, if wanted, belongs above this layer.
 *
 * Every entry point catches all C++ exceptions: libwpd throws on malformed
 * input, and an exception must never unwind across the FFI boundary. ~keep */
#include <librevenge-stream/librevenge-stream.h>
#include <librevenge/librevenge.h>
#include <libwpd/libwpd.h>

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>
#include <vector>

namespace {
using librevenge::RVNGPropertyList;
using librevenge::RVNGString;

/* One recorded event from the libwpd/librevenge callback walk. `text`/`text2`
 * and `level`/`counter`/`counter2` are reused across kinds (a link's href, a
 * field's placeholder, a metadata key/value, a table cell's column/span)
 * rather than giving every kind its own fields; each kind's comment below
 * says what it puts there. `serialize` below is the only place that knows
 * about the wire encoding. ~keep */
enum class NodeKind {
  Text,
  Tab,
  Space,
  LineBreak,
  ParagraphEnd,
  ListItemEnd,
  Heading,
  BoldStart,
  BoldEnd,
  ItalicStart,
  ItalicEnd,
  UnderlineStart,
  UnderlineEnd,
  StrikethroughStart,
  StrikethroughEnd,
  SuperscriptStart,
  SuperscriptEnd,
  SubscriptStart,
  SubscriptEnd,
  ListItemStart,
  TableStart,
  TableRowStart,
  TableCellStart,
  CoveredTableCell,
  TableCellEnd,
  TableRowEnd,
  TableEnd,
  HeaderStart,
  HeaderEnd,
  FooterStart,
  FooterEnd,
  NoteStart,
  NoteEnd,
  EndnoteStart,
  EndnoteEnd,
  AsideStart,
  AsideEnd,
  LinkStart,
  LinkEnd,
  FieldInsert,
  MetaData,
};

struct Node {
  NodeKind kind;
  std::string text;  // literal text; link href; field placeholder; metadata key
  std::string text2; // metadata value
  int level = 0;     // heading level; list nesting level; table cell column
  int counter = 0;   // ordered-list counter; table cell column span
  int counter2 = 0;  // table cell row span
  bool ordered = false; // list ordered flag; table row "is header row" flag
};

/* Records the document as a flat, format-agnostic `std::vector<Node>` while
 * libwpd walks it. Carries no notion of any output encoding — that is
 * `serialize`'s job, run once, after the walk is complete, over the recorded
 * nodes. ~keep */
class DocumentBuilder : public librevenge::RVNGTextInterface {
public:
  std::vector<Node> nodes;

  void insertText(const RVNGString &s) override {
    // `size()` (bytes) avoids `len()`'s multibyte truncation/NUL-stop. ~keep
    if (s.cstr())
      nodes.push_back({NodeKind::Text, std::string(s.cstr(), s.size())});
  }
  void insertTab() override { nodes.push_back({NodeKind::Tab}); }
  void insertSpace() override { nodes.push_back({NodeKind::Space}); }
  void insertLineBreak() override { nodes.push_back({NodeKind::LineBreak}); }
  void closeParagraph() override { nodes.push_back({NodeKind::ParagraphEnd}); }
  void closeListElement() override { nodes.push_back({NodeKind::ListItemEnd}); }
  void closeTableCell() override { nodes.push_back({NodeKind::TableCellEnd}); }
  void closeTableRow() override { nodes.push_back({NodeKind::TableRowEnd}); }
  void closeTable() override { nodes.push_back({NodeKind::TableEnd}); }

  void openParagraph(const RVNGPropertyList &props) override {
    const librevenge::RVNGProperty *outline = props["text:outline-level"];
    if (outline) {
      int level = outline->getInt();
      if (level >= 1 && level <= 6) {
        Node n{NodeKind::Heading};
        n.level = level;
        nodes.push_back(n);
      }
    }
  }

  void openSpan(const RVNGPropertyList &props) override {
    const librevenge::RVNGProperty *weight = props["fo:font-weight"];
    const librevenge::RVNGProperty *style = props["fo:font-style"];
    const librevenge::RVNGProperty *underline =
        props["style:text-underline-style"];
    const librevenge::RVNGProperty *lineThrough =
        props["fo:text-line-through-style"];
    const librevenge::RVNGProperty *position = props["style:text-position"];
    SpanFlags flags{};
    flags.bold = weight && weight->getStr() == "bold";
    flags.italic = style && style->getStr() == "italic";
    // libwpd emits "solid" for both single and double underline. ~keep
    flags.underline = underline && underline->getStr() != "none";
    flags.strikethrough = lineThrough && lineThrough->getStr() != "none";
    // libwpd emits "super <pct>%" / "sub <pct>%" (WPXContentListener). ~keep
    if (position) {
      std::string pos =
          position->getStr().cstr() ? position->getStr().cstr() : "";
      flags.superscript = pos.rfind("super", 0) == 0;
      flags.subscript = pos.rfind("sub", 0) == 0;
    }
    if (flags.bold)
      nodes.push_back({NodeKind::BoldStart});
    if (flags.italic)
      nodes.push_back({NodeKind::ItalicStart});
    if (flags.underline)
      nodes.push_back({NodeKind::UnderlineStart});
    if (flags.strikethrough)
      nodes.push_back({NodeKind::StrikethroughStart});
    if (flags.superscript)
      nodes.push_back({NodeKind::SuperscriptStart});
    if (flags.subscript)
      nodes.push_back({NodeKind::SubscriptStart});
    spanStack_.push_back(flags);
  }
  void closeSpan() override {
    if (spanStack_.empty())
      return;
    SpanFlags flags = spanStack_.back();
    spanStack_.pop_back();
    if (flags.subscript)
      nodes.push_back({NodeKind::SubscriptEnd});
    if (flags.superscript)
      nodes.push_back({NodeKind::SuperscriptEnd});
    if (flags.strikethrough)
      nodes.push_back({NodeKind::StrikethroughEnd});
    if (flags.underline)
      nodes.push_back({NodeKind::UnderlineEnd});
    if (flags.italic)
      nodes.push_back({NodeKind::ItalicEnd});
    if (flags.bold)
      nodes.push_back({NodeKind::BoldEnd});
  }

  void openOrderedListLevel(const RVNGPropertyList &) override {
    listStack_.push_back({true, 0});
  }
  void openUnorderedListLevel(const RVNGPropertyList &) override {
    listStack_.push_back({false, 0});
  }
  void closeOrderedListLevel() override {
    if (!listStack_.empty())
      listStack_.pop_back();
  }
  void closeUnorderedListLevel() override {
    if (!listStack_.empty())
      listStack_.pop_back();
  }
  void openListElement(const RVNGPropertyList &) override {
    if (listStack_.empty())
      return;
    ListLevel &level = listStack_.back();
    Node n{NodeKind::ListItemStart};
    // Clamp before the narrowing cast to u8 so deep nesting can't wrap. ~keep
    size_t depth = listStack_.size();
    n.level = static_cast<int>(depth > 255 ? 255 : depth);
    n.ordered = level.ordered;
    if (level.ordered) {
      level.counter += 1;
      n.counter = level.counter;
    }
    nodes.push_back(n);
  }

  // Headers/footers recur every page; bracketing events let a consumer
  // place them wherever, instead of the shim splicing them inline. ~keep
  void openHeader(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::HeaderStart});
  }
  void closeHeader() override { nodes.push_back({NodeKind::HeaderEnd}); }
  void openFooter(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::FooterStart});
  }
  void closeFooter() override { nodes.push_back({NodeKind::FooterEnd}); }

  // Footnote/endnote are distinct kinds (not one merged "note" kind) so a
  // consumer can number/label them as two sequences, not one counter. ~keep
  void openFootnote(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::NoteStart});
  }
  void closeFootnote() override { nodes.push_back({NodeKind::NoteEnd}); }
  void openEndnote(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::EndnoteStart});
  }
  void closeEndnote() override { nodes.push_back({NodeKind::EndnoteEnd}); }
  void openComment(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::AsideStart, "comment"});
  }
  void closeComment() override { nodes.push_back({NodeKind::AsideEnd}); }
  void openTextBox(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::AsideStart, "box"});
  }
  void closeTextBox() override { nodes.push_back({NodeKind::AsideEnd}); }

  void openLink(const RVNGPropertyList &props) override {
    const librevenge::RVNGProperty *href = props["xlink:href"];
    Node n{NodeKind::LinkStart};
    // (ptr, size), like insertText: avoids truncating at an embedded NUL. ~keep
    if (href && href->getStr().cstr())
      n.text = std::string(href->getStr().cstr(), href->getStr().size());
    nodes.push_back(n);
  }
  void closeLink() override { nodes.push_back({NodeKind::LinkEnd}); }

  void insertField(const RVNGPropertyList &props) override {
    const librevenge::RVNGProperty *type = props["librevenge:field-type"];
    std::string fieldType =
        type && type->getStr().cstr() ? type->getStr().cstr() : "";
    Node n{NodeKind::FieldInsert};
    // A dropped field silently loses info a reader can't recover; render an
    // explicit placeholder so the field's presence at least survives. ~keep
    if (fieldType == "text:page-number")
      n.text = "page";
    else if (fieldType == "text:page-count")
      n.text = "pages";
    else if (fieldType.rfind("text:date", 0) == 0)
      n.text = "date";
    else if (fieldType.rfind("text:time", 0) == 0)
      n.text = "time";
    else if (!fieldType.empty())
      n.text = fieldType;
    else
      n.text = "field";
    nodes.push_back(n);
  }

  // libwpd emits "Author" as `meta:initial-creator`; `dc:creator` is
  // WordPerfect's separate "Typist" field — the Rust side maps only
  // `meta:initial-creator` to the document author. ~keep
  void setDocumentMetaData(const RVNGPropertyList &props) override {
    static const char *const kKeys[] = {
        "dc:title", "meta:initial-creator", "dc:creator",   "dc:subject",
        "dc:type",  "dc:language",          "meta:keyword",
    };
    for (const char *key : kKeys) {
      const librevenge::RVNGProperty *value = props[key];
      if (!value || !value->getStr().cstr())
        continue;
      Node n{NodeKind::MetaData};
      n.text = key;
      // (ptr, size): avoids truncating a value with an embedded NUL. ~keep
      n.text2 = std::string(value->getStr().cstr(), value->getStr().size());
      nodes.push_back(n);
    }
  }
  void startDocument(const RVNGPropertyList &) override {}
  void endDocument() override {}
  void definePageStyle(const RVNGPropertyList &) override {}
  void defineEmbeddedFont(const RVNGPropertyList &) override {}
  void openPageSpan(const RVNGPropertyList &) override {}
  void closePageSpan() override {}
  void defineParagraphStyle(const RVNGPropertyList &) override {}
  void defineCharacterStyle(const RVNGPropertyList &) override {}
  void defineSectionStyle(const RVNGPropertyList &) override {}
  void openSection(const RVNGPropertyList &) override {}
  void closeSection() override {}

  // Recorded fully (open events too) so a consumer can lay cells out on a
  // real grid: column, column span, row span, header-row flag. ~keep
  void openTable(const RVNGPropertyList &) override {
    nodes.push_back({NodeKind::TableStart});
  }
  void openTableRow(const RVNGPropertyList &props) override {
    const librevenge::RVNGProperty *header = props["librevenge:is-header-row"];
    Node n{NodeKind::TableRowStart};
    n.ordered = header && header->getInt() != 0;
    nodes.push_back(n);
  }
  void openTableCell(const RVNGPropertyList &props) override {
    Node n{NodeKind::TableCellStart};
    n.level = getIntOr(props, "librevenge:column", -1);
    n.counter = std::max(1, getIntOr(props, "table:number-columns-spanned", 1));
    n.counter2 = std::max(1, getIntOr(props, "table:number-rows-spanned", 1));
    nodes.push_back(n);
  }
  void insertCoveredTableCell(const RVNGPropertyList &props) override {
    Node n{NodeKind::CoveredTableCell};
    n.level = getIntOr(props, "librevenge:column", -1);
    nodes.push_back(n);
  }

  void openFrame(const RVNGPropertyList &) override {}
  void closeFrame() override {}
  void insertBinaryObject(const RVNGPropertyList &) override {}
  void insertEquation(const RVNGPropertyList &) override {}
  void openGroup(const RVNGPropertyList &) override {}
  void closeGroup() override {}
  void defineGraphicStyle(const RVNGPropertyList &) override {}
  void drawRectangle(const RVNGPropertyList &) override {}
  void drawEllipse(const RVNGPropertyList &) override {}
  void drawPolygon(const RVNGPropertyList &) override {}
  void drawPolyline(const RVNGPropertyList &) override {}
  void drawPath(const RVNGPropertyList &) override {}
  void drawConnector(const RVNGPropertyList &) override {}

private:
  struct SpanFlags {
    bool bold;
    bool italic;
    bool underline;
    bool strikethrough;
    bool superscript;
    bool subscript;
  };
  struct ListLevel {
    bool ordered;
    int counter;
  };

  static int getIntOr(const RVNGPropertyList &props, const char *key,
                      int fallback) {
    const librevenge::RVNGProperty *p = props[key];
    return p ? p->getInt() : fallback;
  }

  std::vector<SpanFlags> spanStack_;
  std::vector<ListLevel> listStack_;
};

/* Serializes a recorded `std::vector<Node>` to the versioned binary wire
 * format Rust's `dto::decode` parses. This is the only place that knows about
 * the wire encoding — `DocumentBuilder` above records the same structure
 * regardless of how it will eventually be serialized.
 *
 * # Wire format (version 1)
 *
 * This spec MUST stay byte-for-byte identical to the one in `src/dto.rs` —
 * the two are independently hand-written mirrors of the same format, not
 * generated from a shared schema. All integers are little-endian. Strings are
 * raw UTF-8 bytes (not NUL-terminated) with an explicit `u32` byte length, so
 * embedded NULs never truncate anything.
 *
 *   document := version metadata_section event_section
 *   version  := u8                          // WIRE_VERSION, currently 1
 *   metadata_section := u32 count  count * (string key, string value)
 *   event_section     := u32 count  count * event
 *   event             := u8 tag  payload     // payload shape depends on tag
 *   string            := u32 byte_len  byte_len * u8
 *   bool (in a payload) := u8, 0 or 1
 *
 * Event tags (see the matching table in `dto.rs` for full payload shapes):
 *   0 Text(string) | 1 Tab | 2 Space | 3 LineBreak | 4 ParagraphEnd
 *   5 ListItemStart(bool ordered, u8 level, u32 counter) | 6 ListItemEnd
 *   7 HeadingStart(u8 level)
 *   8/9 BoldStart/End | 10/11 ItalicStart/End | 12/13 UnderlineStart/End
 *   14/15 StrikethroughStart/End | 16/17 SuperscriptStart/End
 *   18/19 SubscriptStart/End
 *   20 TableStart | 21 RowStart(bool header)
 *   22 CellStart(i32 column, u32 col_span, u32 row_span)
 *   23 CoveredCell(i32 column) | 24 CellEnd | 25 RowEnd | 26 TableEnd
 *   27/28 HeaderStart/End (document running header) | 29/30 FooterStart/End
 *   31 NoteStart(bool endnote) | 32 NoteEnd
 *   33 AsideStart(string kind) | 34 AsideEnd
 *   35 LinkStart(string href) | 36 LinkEnd
 *   37 Field(string text)
 *
 * `MetaData` nodes are not serialized as events: they are pulled out into the
 * metadata section up front, keyed by the same string (e.g. "dc:title") the
 * `Node` carried. This mirrors every key `setDocumentMetaData` captured, not
 * only the ones `dto::WpdMetadata` exposes as named fields, so no metadata
 * the shim captured is silently dropped from the wire. ~keep */
namespace wire {
constexpr uint8_t kWireVersion = 1;

enum class Tag : uint8_t {
  Text = 0,
  Tab = 1,
  Space = 2,
  LineBreak = 3,
  ParagraphEnd = 4,
  ListItemStart = 5,
  ListItemEnd = 6,
  HeadingStart = 7,
  BoldStart = 8,
  BoldEnd = 9,
  ItalicStart = 10,
  ItalicEnd = 11,
  UnderlineStart = 12,
  UnderlineEnd = 13,
  StrikethroughStart = 14,
  StrikethroughEnd = 15,
  SuperscriptStart = 16,
  SuperscriptEnd = 17,
  SubscriptStart = 18,
  SubscriptEnd = 19,
  TableStart = 20,
  RowStart = 21,
  CellStart = 22,
  CoveredCell = 23,
  CellEnd = 24,
  RowEnd = 25,
  TableEnd = 26,
  HeaderStart = 27,
  HeaderEnd = 28,
  FooterStart = 29,
  FooterEnd = 30,
  NoteStart = 31,
  NoteEnd = 32,
  AsideStart = 33,
  AsideEnd = 34,
  LinkStart = 35,
  LinkEnd = 36,
  Field = 37,
};

void putU8(std::string &out, uint8_t v) { out.push_back(static_cast<char>(v)); }
void putBool(std::string &out, bool v) { putU8(out, v ? 1 : 0); }
void putTag(std::string &out, Tag tag) {
  putU8(out, static_cast<uint8_t>(tag));
}

void putU32(std::string &out, uint32_t v) {
  for (int i = 0; i < 4; ++i) {
    out.push_back(static_cast<char>(v & 0xff));
    v >>= 8;
  }
}
void putI32(std::string &out, int32_t v) {
  putU32(out, static_cast<uint32_t>(v));
}
void putString(std::string &out, const std::string &s) {
  putU32(out, static_cast<uint32_t>(s.size()));
  out += s;
}

// Callers clamp counts/spans to at least 1; this only guards a negative value
// reaching the wire. ~keep
uint32_t nonNegative(int v) { return v < 0 ? 0 : static_cast<uint32_t>(v); }

// No wire payload beyond a tag; `NoteEnd`/`EndnoteEnd` both map to
// `Tag::NoteEnd` (the footnote/endnote split is on the *start* bool). ~keep
struct SimpleTagMapping {
  NodeKind kind;
  Tag tag;
};

constexpr SimpleTagMapping kSimpleTags[] = {
    {NodeKind::Tab, Tag::Tab},
    {NodeKind::Space, Tag::Space},
    {NodeKind::LineBreak, Tag::LineBreak},
    {NodeKind::ParagraphEnd, Tag::ParagraphEnd},
    {NodeKind::ListItemEnd, Tag::ListItemEnd},
    {NodeKind::BoldStart, Tag::BoldStart},
    {NodeKind::BoldEnd, Tag::BoldEnd},
    {NodeKind::ItalicStart, Tag::ItalicStart},
    {NodeKind::ItalicEnd, Tag::ItalicEnd},
    {NodeKind::UnderlineStart, Tag::UnderlineStart},
    {NodeKind::UnderlineEnd, Tag::UnderlineEnd},
    {NodeKind::StrikethroughStart, Tag::StrikethroughStart},
    {NodeKind::StrikethroughEnd, Tag::StrikethroughEnd},
    {NodeKind::SuperscriptStart, Tag::SuperscriptStart},
    {NodeKind::SuperscriptEnd, Tag::SuperscriptEnd},
    {NodeKind::SubscriptStart, Tag::SubscriptStart},
    {NodeKind::SubscriptEnd, Tag::SubscriptEnd},
    {NodeKind::TableStart, Tag::TableStart},
    {NodeKind::TableCellEnd, Tag::CellEnd},
    {NodeKind::TableRowEnd, Tag::RowEnd},
    {NodeKind::TableEnd, Tag::TableEnd},
    {NodeKind::HeaderStart, Tag::HeaderStart},
    {NodeKind::HeaderEnd, Tag::HeaderEnd},
    {NodeKind::FooterStart, Tag::FooterStart},
    {NodeKind::FooterEnd, Tag::FooterEnd},
    {NodeKind::NoteEnd, Tag::NoteEnd},
    {NodeKind::EndnoteEnd, Tag::NoteEnd},
    {NodeKind::AsideEnd, Tag::AsideEnd},
    {NodeKind::LinkEnd, Tag::LinkEnd},
};

// Writes `kind`'s tag if it is one of `kSimpleTags`. Returns whether it was.
bool putSimpleEvent(std::string &out, NodeKind kind) {
  for (const SimpleTagMapping &mapping : kSimpleTags) {
    if (mapping.kind == kind) {
      putTag(out, mapping.tag);
      return true;
    }
  }
  return false;
}

// Only reached for a kind absent from `kSimpleTags` (`MetaData` is filtered out
// earlier, by `serialize`). ~keep
void putPayloadEvent(std::string &out, const Node &n) {
  switch (n.kind) {
  case NodeKind::Text:
    putTag(out, Tag::Text);
    putString(out, n.text);
    break;
  case NodeKind::ListItemStart:
    putTag(out, Tag::ListItemStart);
    putBool(out, n.ordered);
    putU8(out, static_cast<uint8_t>(n.level));
    putU32(out, nonNegative(n.counter));
    break;
  case NodeKind::Heading:
    putTag(out, Tag::HeadingStart);
    putU8(out, static_cast<uint8_t>(n.level));
    break;
  case NodeKind::TableRowStart:
    putTag(out, Tag::RowStart);
    putBool(out, n.ordered);
    break;
  case NodeKind::TableCellStart:
    putTag(out, Tag::CellStart);
    putI32(out, n.level);
    putU32(out, nonNegative(std::max(1, n.counter)));
    putU32(out, nonNegative(std::max(1, n.counter2)));
    break;
  case NodeKind::CoveredTableCell:
    putTag(out, Tag::CoveredCell);
    putI32(out, n.level);
    break;
  case NodeKind::NoteStart:
    putTag(out, Tag::NoteStart);
    putBool(out, false);
    break;
  case NodeKind::EndnoteStart:
    putTag(out, Tag::NoteStart);
    putBool(out, true);
    break;
  case NodeKind::AsideStart:
    putTag(out, Tag::AsideStart);
    putString(out, n.text);
    break;
  case NodeKind::LinkStart:
    putTag(out, Tag::LinkStart);
    putString(out, n.text);
    break;
  case NodeKind::FieldInsert:
    putTag(out, Tag::Field);
    putString(out, n.text);
    break;
  default: // MetaData; every other kind is handled by putSimpleEvent.
    break;
  }
}

/* Encodes one event node. `MetaData` nodes must be filtered out by the caller
 * before reaching here — they have no event representation. ~keep */
void putEvent(std::string &out, const Node &n) {
  if (!putSimpleEvent(out, n.kind))
    putPayloadEvent(out, n);
}
} // namespace wire

std::string serialize(const std::vector<Node> &nodes) {
  std::vector<const Node *> metadata;
  std::vector<const Node *> events;
  events.reserve(nodes.size());
  for (const Node &n : nodes) {
    if (n.kind == NodeKind::MetaData)
      metadata.push_back(&n);
    else
      events.push_back(&n);
  }

  std::string out;
  // ~8 bytes/node; amortized doubling absorbs bigger text nodes. ~keep
  constexpr size_t kBytesPerNodeEstimate = 8;
  out.reserve(nodes.size() * kBytesPerNodeEstimate + sizeof(uint32_t) * 2 + 1);
  wire::putU8(out, wire::kWireVersion);

  wire::putU32(out, static_cast<uint32_t>(metadata.size()));
  for (const Node *m : metadata) {
    wire::putString(out, m->text);
    wire::putString(out, m->text2);
  }

  wire::putU32(out, static_cast<uint32_t>(events.size()));
  for (const Node *n : events)
    wire::putEvent(out, *n);

  return out;
}
} // namespace

extern "C" {

/* Result codes shared with the Rust side (see error.rs). ~keep */
enum {
  XBERG_WPD_OK = 0,
  XBERG_WPD_INVALID_ARGS = 1,
  XBERG_WPD_UNSUPPORTED_FORMAT = 2,
  XBERG_WPD_PARSE_ERROR = 3,
  XBERG_WPD_OUT_OF_MEMORY = 4,
  XBERG_WPD_PANIC = 5,
  XBERG_WPD_ENCRYPTED = 6,
};

namespace {
char *dup_malloc(const char *data, size_t n) {
  char *buf = static_cast<char *>(std::malloc(n + 1));
  if (!buf)
    return nullptr;
  if (n)
    std::memcpy(buf, data, n);
  buf[n] = '\0';
  return buf;
}
} // namespace

/* Returns non-zero if the buffer looks like a WordPerfect document libwpd can
 * parse. Never throws. ~keep */
int xberg_wpd_is_supported(const unsigned char *data, unsigned long len) {
  if (!data || len == 0)
    return 0;
  if (len > (std::numeric_limits<unsigned int>::max)())
    return 0;
  try {
    librevenge::RVNGStringStream input(data, static_cast<unsigned int>(len));
    return libwpd::WPDocument::isFileFormatSupported(&input) !=
                   libwpd::WPD_CONFIDENCE_NONE
               ? 1
               : 0;
  } catch (...) {
    return 0;
  }
}

/* Extract the structured document model of an in-memory WordPerfect document
 * as a versioned binary blob (see the wire-format comment above `serialize`).
 * Parses once into an internal `std::vector<Node>` document via
 * `DocumentBuilder`, then serializes that one document — there is no
 * rendering step; the caller (Rust's `dto::decode`) reconstructs a typed
 * document model from the bytes.
 *
 * On XBERG_WPD_OK, *out_buf is a malloc'd buffer of *out_len bytes (NOT
 * NUL-terminated; the format is binary, not text) the caller frees via
 * xberg_wpd_free_string. On any other return, *out_buf is left null.
 *
 * On failure, *out_err may be set to a malloc'd, NUL-terminated diagnostic
 * message (freed the same way) describing the underlying C++ exception; it
 * is left null when no additional detail is available. ~keep */
int xberg_wpd_extract_document(const unsigned char *data, unsigned long len,
                               char **out_buf, unsigned long *out_len,
                               char **out_err) {
  if (!out_buf || !out_len)
    return XBERG_WPD_INVALID_ARGS;
  *out_buf = nullptr;
  *out_len = 0;
  if (out_err)
    *out_err = nullptr;
  if (!data || len == 0)
    return XBERG_WPD_INVALID_ARGS;
  // RVNGStringStream takes unsigned int; direct C callers reach this too. ~keep
  if (len > (std::numeric_limits<unsigned int>::max)())
    return XBERG_WPD_INVALID_ARGS;

  try {
    librevenge::RVNGStringStream input(data, static_cast<unsigned int>(len));
    if (libwpd::WPDocument::isFileFormatSupported(&input) ==
        libwpd::WPD_CONFIDENCE_NONE)
      return XBERG_WPD_UNSUPPORTED_FORMAT;

    DocumentBuilder builder;
    libwpd::WPDResult result =
        libwpd::WPDocument::parse(&input, &builder, nullptr);
    if (result != libwpd::WPD_OK) {
      // Encryption/password failures are distinguished from generic parse
      // errors so a caller can tell "this needs a password" from
      // "this file is corrupt" instead of both collapsing into the same
      // opaque error. ~keep
      if (result == libwpd::WPD_UNSUPPORTED_ENCRYPTION_ERROR ||
          result == libwpd::WPD_PASSWORD_MISSMATCH_ERROR)
        return XBERG_WPD_ENCRYPTED;
      return XBERG_WPD_PARSE_ERROR;
    }

    std::string blob = serialize(builder.nodes);
    if (blob.size() > (std::numeric_limits<unsigned long>::max)())
      return XBERG_WPD_OUT_OF_MEMORY;
    char *buf = dup_malloc(blob.data(), blob.size());
    if (!buf)
      return XBERG_WPD_OUT_OF_MEMORY;
    *out_buf = buf;
    *out_len = static_cast<unsigned long>(blob.size());
    return XBERG_WPD_OK;
  } catch (const std::exception &e) {
    // libwpd's own error types (ParseException, FileException,
    // GenericException, ...) are bare classes that do not derive from
    // std::exception, so they land in the catch-all below; this arm covers
    // allocation and standard-library failures. what() may return null. ~keep
    const char *msg = e.what();
    if (out_err && msg)
      *out_err = dup_malloc(msg, std::strlen(msg));
    return XBERG_WPD_PANIC;
  } catch (...) {
    return XBERG_WPD_PANIC;
  }
}

void xberg_wpd_free_string(char *s) { std::free(s); }

} // extern "C"

namespace {
// Self-test support: top-level so a loop's branching counts once, here. ~keep
const Node *findFirstKind(const std::vector<Node> &nodes, NodeKind kind) {
  for (const Node &node : nodes)
    if (node.kind == kind)
      return &node;
  return nullptr;
}

bool containsKind(const std::vector<Node> &nodes, NodeKind kind) {
  return findFirstKind(nodes, kind) != nullptr;
}

const Node *findMetaData(const std::vector<Node> &nodes, const char *key) {
  for (const Node &node : nodes)
    if (node.kind == NodeKind::MetaData && node.text == key)
      return &node;
  return nullptr;
}

// One expected (kind, text) pair; `text` nullptr skips the text check.
struct ExpectedNode {
  NodeKind kind;
  const char *text;
};

// One loop instead of N chained `&&`s, so complexity doesn't scale with N.
template <size_t N>
bool matchesSequence(const std::vector<Node> &nodes,
                     const ExpectedNode (&expected)[N]) {
  if (nodes.size() != N)
    return false;
  for (size_t i = 0; i < N; ++i) {
    if (nodes[i].kind != expected[i].kind)
      return false;
    if (expected[i].text && nodes[i].text != expected[i].text)
      return false;
  }
  return true;
}

// Per-feature checks for `xberg_wpd_self_test_features`, so it stays flat.
// ~keep
bool testMetadataAndLinkFeatures(const std::vector<Node> &n) {
  const Node *title = findMetaData(n, "dc:title");
  const Node *creator = findMetaData(n, "dc:creator");
  const Node *link = findFirstKind(n, NodeKind::LinkStart);
  bool ok = title && title->text2 == "Sample Report";
  ok = ok && creator && creator->text2 == "A. Writer";
  ok = ok && link && link->text == "https://example.com/report";
  ok = ok && containsKind(n, NodeKind::LinkEnd);
  ok = ok && containsKind(n, NodeKind::StrikethroughStart) &&
       containsKind(n, NodeKind::StrikethroughEnd);
  return ok;
}

bool testFieldAndNoteFeatures(const std::vector<Node> &n) {
  const Node *field = findFirstKind(n, NodeKind::FieldInsert);
  bool ok = field && field->text == "page";
  ok = ok && containsKind(n, NodeKind::NoteStart) &&
       containsKind(n, NodeKind::NoteEnd);
  ok = ok && containsKind(n, NodeKind::EndnoteStart) &&
       containsKind(n, NodeKind::EndnoteEnd);
  return ok;
}

bool testTableFeatures(const std::vector<Node> &n) {
  const Node *headerRow = findFirstKind(n, NodeKind::TableRowStart);
  bool ok = headerRow && headerRow->ordered;

  int spanCellCount = 0;
  for (const Node &node : n)
    if (node.kind == NodeKind::TableCellStart && node.counter == 2)
      spanCellCount++;
  ok = ok && spanCellCount == 1;
  ok = ok && containsKind(n, NodeKind::CoveredTableCell);

  // Embedded '|'/linebreak survive verbatim; sanitizing was rendering-only.
  // ~keep
  bool foundRawPipeText = false;
  for (const Node &node : n)
    if (node.kind == NodeKind::Text && node.text == "Jo|e")
      foundRawPipeText = true;
  return ok && foundRawPipeText;
}

// libwpd can skip a covered cell for a vertical merge yet still stamp the
// surviving cell with its true librevenge:column; row 2 omits the column-0
// covered cell but its real cell must still carry column 1, not 0. ~keep
bool testColumnReanchoring() {
  RVNGPropertyList empty;
  DocumentBuilder c;
  c.openTable(empty);
  c.openTableRow(empty);
  RVNGPropertyList r1c0;
  r1c0.insert("librevenge:column", 0);
  c.openTableCell(r1c0);
  c.insertText(RVNGString("r1c0"));
  c.closeTableCell();
  RVNGPropertyList r1c1;
  r1c1.insert("librevenge:column", 1);
  c.openTableCell(r1c1);
  c.insertText(RVNGString("r1c1"));
  c.closeTableCell();
  c.closeTableRow();
  c.openTableRow(empty);
  RVNGPropertyList r2c1;
  r2c1.insert("librevenge:column", 1);
  c.openTableCell(r2c1);
  c.insertText(RVNGString("r2c1"));
  c.closeTableCell();
  c.closeTableRow();
  c.closeTable();

  int r2CellColumn = -2;
  bool afterSecondRow = false;
  int rowEndsSeen = 0;
  for (const Node &node : c.nodes) {
    if (node.kind == NodeKind::TableRowEnd)
      rowEndsSeen++;
    if (rowEndsSeen == 1 && node.kind == NodeKind::TableCellStart)
      afterSecondRow = true;
    if (afterSecondRow && node.kind == NodeKind::TableCellStart)
      r2CellColumn = node.level;
  }
  return r2CellColumn == 1;
}

// Builds the document `xberg_wpd_self_test_features` asserts over. ~keep
void buildFeatureDocument(DocumentBuilder &b) {
  RVNGPropertyList empty;

  RVNGPropertyList meta;
  meta.insert("dc:title", "Sample Report");
  meta.insert("dc:creator", "A. Writer");
  b.setDocumentMetaData(meta);

  b.openParagraph(empty);
  RVNGPropertyList linkProps;
  linkProps.insert("xlink:href", "https://example.com/report");
  b.openLink(linkProps);
  b.insertText(RVNGString("full report"));
  b.closeLink();
  b.insertText(RVNGString(" "));

  RVNGPropertyList strikeProps;
  strikeProps.insert("fo:text-line-through-style", "solid");
  b.openSpan(strikeProps);
  b.insertText(RVNGString("obsolete"));
  b.closeSpan();
  b.insertText(RVNGString(" "));

  RVNGPropertyList pageFieldProps;
  pageFieldProps.insert("librevenge:field-type", "text:page-number");
  b.insertField(pageFieldProps);

  b.openFootnote(empty);
  b.insertText(RVNGString("A footnote."));
  b.closeFootnote();
  b.openEndnote(empty);
  b.insertText(RVNGString("An endnote."));
  b.closeEndnote();
  b.closeParagraph();

  b.openTable(empty);
  RVNGPropertyList headerRowProps;
  headerRowProps.insert("librevenge:is-header-row", true);
  b.openTableRow(headerRowProps);
  b.openTableCell(empty);
  b.insertText(RVNGString("Name"));
  b.closeTableCell();
  RVNGPropertyList spanCellProps;
  spanCellProps.insert("table:number-columns-spanned", 2);
  b.openTableCell(spanCellProps);
  b.insertText(RVNGString("Contact"));
  b.closeTableCell();
  b.closeTableRow();

  b.openTableRow(empty);
  b.openTableCell(empty);
  // Embedded '|'/newline survive unmodified: sanitization was rendering-only.
  // ~keep
  b.insertText(RVNGString("Jo|e"));
  b.insertLineBreak();
  b.insertText(RVNGString("Doe"));
  b.closeTableCell();
  b.insertCoveredTableCell(empty);
  b.openTableCell(empty);
  b.insertText(RVNGString("jo@example.com"));
  b.closeTableCell();
  b.closeTableRow();
  b.closeTable();
}
} // namespace

extern "C" {

// Real evidence footnote/header nodes are distinct events. Internal. ~keep
int xberg_wpd_self_test_separation(void) try {
  DocumentBuilder b;

  RVNGPropertyList empty;
  b.openHeader(empty);
  b.insertText(RVNGString("Confidential Draft"));
  b.closeHeader();

  b.openParagraph(empty);
  b.insertText(RVNGString("Body start."));
  b.openFootnote(empty);
  b.insertText(RVNGString("See appendix A."));
  b.closeFootnote();
  b.insertText(RVNGString("Body continues."));
  b.closeParagraph();

  b.openFooter(empty);
  b.insertText(RVNGString("Page 1 of 1"));
  b.closeFooter();

  static const ExpectedNode kExpected[] = {
      {NodeKind::HeaderStart, nullptr},  {NodeKind::Text, "Confidential Draft"},
      {NodeKind::HeaderEnd, nullptr},    {NodeKind::Text, "Body start."},
      {NodeKind::NoteStart, nullptr},    {NodeKind::Text, "See appendix A."},
      {NodeKind::NoteEnd, nullptr},      {NodeKind::Text, "Body continues."},
      {NodeKind::ParagraphEnd, nullptr}, {NodeKind::FooterStart, nullptr},
      {NodeKind::Text, "Page 1 of 1"},   {NodeKind::FooterEnd, nullptr},
  };
  bool ok = matchesSequence(b.nodes, kExpected);

  // Round-trips through the wire tags a Rust decoder would see. ~keep
  std::string blob = serialize(b.nodes);
  ok = ok && blob.size() > 0 &&
       static_cast<uint8_t>(blob[0]) == wire::kWireVersion;

  return ok ? 1 : 0;
} catch (...) {
  return 0;
}

// Covers links, fields, strikethrough, notes, table structure, metadata. ~keep
int xberg_wpd_self_test_features(void) try {
  DocumentBuilder b;
  buildFeatureDocument(b);

  const std::vector<Node> &n = b.nodes;
  bool ok = testMetadataAndLinkFeatures(n) && testFieldAndNoteFeatures(n) &&
            testTableFeatures(n) && testColumnReanchoring();

  // Smoke check that `serialize` ran over this richer document; the wire
  // format itself is `dto::decode`'s job, exercised from Rust. ~keep
  std::string blob = serialize(n);
  ok = ok && blob.size() > 0 &&
       static_cast<uint8_t>(blob[0]) == wire::kWireVersion;

  return ok ? 1 : 0;
} catch (...) {
  return 0;
}
}