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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
|
/*
* Copyright 2026 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lua_common.h"
#include "lua_xml_scanner.hxx"
#include "libserver/url.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
/***
* @module rspamd_ooxml
* Native bounded parsing for the small OOXML schemas used by lua_content.
*/
namespace {
constexpr std::string_view content_types_namespace =
"http://schemas.openxmlformats.org/package/2006/content-types";
constexpr std::string_view strict_content_types_namespace =
"http://purl.oclc.org/ooxml/package/content-types";
constexpr std::string_view relationships_namespace =
"http://schemas.openxmlformats.org/package/2006/relationships";
constexpr std::string_view strict_relationships_namespace =
"http://purl.oclc.org/ooxml/package/relationships";
constexpr std::string_view word_namespace =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
constexpr std::string_view strict_word_namespace =
"http://purl.oclc.org/ooxml/wordprocessingml/main";
constexpr std::string_view drawing_namespace =
"http://schemas.openxmlformats.org/drawingml/2006/main";
constexpr std::string_view strict_drawing_namespace =
"http://purl.oclc.org/ooxml/drawingml/main";
constexpr std::string_view spreadsheet_namespace =
"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
constexpr std::string_view strict_spreadsheet_namespace =
"http://purl.oclc.org/ooxml/spreadsheetml/main";
constexpr std::string_view document_relationship_namespace =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships";
constexpr std::string_view strict_document_relationship_namespace =
"http://purl.oclc.org/ooxml/officeDocument/relationships";
constexpr std::string_view hyperlink_relationship =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
constexpr std::string_view strict_hyperlink_relationship =
"http://purl.oclc.org/ooxml/officeDocument/relationships/hyperlink";
using xml_limits = rspamd::xml::limits;
struct ooxml_limits {
xml_limits xml;
std::size_t max_relationships = 4096;
std::size_t max_content_types = 4096;
std::size_t max_target_length = 16U * 1024U;
std::size_t max_text = 2U * 1024U * 1024U;
std::size_t max_urls = 1024;
};
template<typename T>
using ooxml_result = rspamd::xml::result<T>;
using ooxml_status = rspamd::xml::status;
struct ooxml_namespaces {
enum class id : std::uint8_t {
unbound,
none,
xml,
xmlns,
content_types,
relationships,
word,
drawing,
spreadsheet,
document_relationships,
other,
};
static auto classify(std::string_view uri) -> id
{
if (uri.empty()) return id::unbound;
if (uri == rspamd::xml::xml_namespace) return id::xml;
if (uri == rspamd::xml::xmlns_namespace) return id::xmlns;
if (uri == content_types_namespace || uri == strict_content_types_namespace) {
return id::content_types;
}
if (uri == relationships_namespace || uri == strict_relationships_namespace) {
return id::relationships;
}
if (uri == word_namespace || uri == strict_word_namespace) return id::word;
if (uri == drawing_namespace || uri == strict_drawing_namespace) return id::drawing;
if (uri == spreadsheet_namespace || uri == strict_spreadsheet_namespace) {
return id::spreadsheet;
}
if (uri == document_relationship_namespace ||
uri == strict_document_relationship_namespace) {
return id::document_relationships;
}
return id::other;
}
};
using namespace_id = ooxml_namespaces::id;
using xml_attribute = rspamd::xml::attribute<ooxml_namespaces>;
using rspamd::xml::get_attribute;
using rspamd::xml::has_ascii_control;
using rspamd::xml::has_uri_scheme;
auto validate_segment(std::string_view segment) -> bool
{
if (segment.empty()) return false;
for (std::size_t i = 0; i < segment.size(); i++) {
if (segment[i] == '%') {
if (i + 2 >= segment.size()) return false;
if (!g_ascii_isxdigit(segment[i + 1]) || !g_ascii_isxdigit(segment[i + 2])) {
return false;
}
i += 2;
}
}
std::string decoded{segment};
decoded.resize(rspamd_url_decode(decoded.data(), segment.data(), segment.size()));
if (decoded == "." || decoded == "..") return false;
return std::none_of(decoded.begin(), decoded.end(), [](char ch) {
return g_ascii_iscntrl(static_cast<unsigned char>(ch)) || ch == '\\' || ch == '/';
});
}
auto resolve_part_name(std::string_view source_part, std::string_view target)
-> ooxml_result<std::string>
{
if (target.empty()) return tl::make_unexpected("empty relationship target");
if (target.front() == '/') {
/* A part name: it starts at the root of the package, not at the source */
target.remove_prefix(1);
source_part = {};
if (target.empty()) return tl::make_unexpected("empty relationship target");
}
if (target.front() == '/' || has_uri_scheme(target) ||
std::any_of(target.begin(), target.end(), [](char ch) {
return g_ascii_iscntrl(static_cast<unsigned char>(ch)) || ch == '\\';
})) {
return tl::make_unexpected("absolute or invalid internal relationship target");
}
auto fragment = target.find('#');
auto path = target.substr(0, fragment);
if (path.empty() || path.find('?') != std::string_view::npos ||
path.find("//") != std::string_view::npos || path.back() == '/') {
return tl::make_unexpected("invalid internal relationship target");
}
/*
* Build the part name from the segments validated here: only "." and ".."
* are dot segments, anything else ("...", "..x", ".x", "x.") is a name.
* A URL path normaliser does not follow these rules.
*/
std::vector<std::string_view> segments;
auto source_directory_end = source_part.rfind('/');
if (source_directory_end != std::string_view::npos) {
auto directory = source_part.substr(0, source_directory_end);
for (std::size_t pos = 0; pos < directory.size();) {
auto slash = directory.find('/', pos);
if (slash == std::string_view::npos) slash = directory.size();
if (slash > pos) segments.push_back(directory.substr(pos, slash - pos));
pos = slash + 1;
}
}
std::size_t start = 0;
std::string_view segment;
while (start < path.size()) {
auto slash = path.find('/', start);
if (slash == std::string_view::npos) slash = path.size();
segment = path.substr(start, slash - start);
if (segment == "..") {
if (segments.empty()) {
return tl::make_unexpected("relationship target escapes package root");
}
segments.pop_back();
}
else if (segment != ".") {
if (!validate_segment(segment)) {
return tl::make_unexpected("invalid relationship path segment");
}
segments.push_back(segment);
}
start = slash + 1;
}
/* A last "." or ".." names a directory, as a trailing "/" does */
if (segments.empty() || segment == "." || segment == "..") {
return tl::make_unexpected("relationship target does not name a part");
}
std::string result;
for (auto name: segments) {
if (!result.empty()) result.push_back('/');
result.append(name);
}
return result;
}
struct content_types_result {
ankerl::unordered_dense::map<std::string, std::string> defaults;
ankerl::unordered_dense::map<std::string, std::string> overrides;
};
class content_types_handler {
public:
using namespaces = ooxml_namespaces;
explicit content_types_handler(const ooxml_limits &limits)
: limits_{limits}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
depth_++;
if (depth_ == 1) {
if (name != "Types" || ns != namespace_id::content_types) {
return tl::make_unexpected("invalid OOXML content types root");
}
root_seen_ = true;
}
else if (depth_ == 2 && ns == namespace_id::content_types) {
if ((name == "Default" || name == "Override") &&
result_.defaults.size() + result_.overrides.size() >= limits_.max_content_types) {
return tl::make_unexpected("OOXML content type limit exceeded");
}
if (name == "Default") {
auto *extension = get_attribute(attributes, "Extension");
auto *content_type = get_attribute(attributes, "ContentType");
if (extension == nullptr || extension->empty() || content_type == nullptr ||
content_type->empty()) {
return tl::make_unexpected("invalid OOXML default content type");
}
auto key = *extension;
rspamd_str_lc(key.data(), key.size());
if (!result_.defaults.emplace(std::move(key), *content_type).second) {
return tl::make_unexpected("duplicate OOXML default content type");
}
}
else if (name == "Override") {
auto *part_name = get_attribute(attributes, "PartName");
auto *content_type = get_attribute(attributes, "ContentType");
if (part_name == nullptr || part_name->empty() || part_name->front() != '/' ||
content_type == nullptr || content_type->empty()) {
return tl::make_unexpected("invalid OOXML content type override");
}
auto normalized = resolve_part_name({}, std::string_view{*part_name}.substr(1));
if (!normalized) return tl::make_unexpected(std::move(normalized.error()));
if (!result_.overrides.emplace(std::move(*normalized), *content_type).second) {
return tl::make_unexpected("duplicate OOXML content type override");
}
}
}
return {};
}
auto end_element(namespace_id, std::string_view) -> ooxml_status
{
depth_--;
return {};
}
auto text(std::string_view) -> ooxml_status
{
return {};
}
auto finish() -> ooxml_result<content_types_result>
{
if (!root_seen_) return tl::make_unexpected("missing OOXML content types root");
return std::move(result_);
}
private:
ooxml_limits limits_;
content_types_result result_;
std::size_t depth_ = 0;
bool root_seen_ = false;
};
struct relationship {
std::string id;
std::string type;
std::string target;
std::string part_name;
bool external = false;
};
class relationships_handler {
public:
using namespaces = ooxml_namespaces;
relationships_handler(std::string_view source_part, const ooxml_limits &limits)
: source_part_{source_part}, limits_{limits}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
depth_++;
if (depth_ == 1) {
if (name != "Relationships" || ns != namespace_id::relationships) {
return tl::make_unexpected("invalid OOXML relationships root");
}
root_seen_ = true;
}
else if (depth_ == 2 && name == "Relationship" && ns == namespace_id::relationships) {
auto *id = get_attribute(attributes, "Id");
auto *type = get_attribute(attributes, "Type");
auto *target = get_attribute(attributes, "Target");
auto *mode = get_attribute(attributes, "TargetMode");
if (id == nullptr || id->empty() || type == nullptr || type->empty() ||
target == nullptr || target->empty()) {
return tl::make_unexpected("invalid OOXML relationship");
}
if (target->size() > limits_.max_target_length) {
return tl::make_unexpected("OOXML relationship target limit exceeded");
}
if (has_ascii_control(*target)) {
return tl::make_unexpected("control character in OOXML relationship target");
}
if (ids_.contains(*id)) {
return tl::make_unexpected("duplicate OOXML relationship id");
}
if (relationships_.size() >= limits_.max_relationships) {
return tl::make_unexpected("OOXML relationship limit exceeded");
}
bool external = false;
if (mode != nullptr) {
auto normalized_mode = *mode;
rspamd_str_lc(normalized_mode.data(), normalized_mode.size());
if (normalized_mode != "external" && normalized_mode != "internal") {
return tl::make_unexpected("invalid OOXML relationship target mode");
}
external = normalized_mode == "external";
}
relationship rel{*id, *type, *target, {}, external};
if (!external) {
auto part_name = resolve_part_name(source_part_, *target);
if (!part_name) return tl::make_unexpected(std::move(part_name.error()));
rel.part_name = std::move(*part_name);
}
ids_.emplace(rel.id);
relationships_.push_back(std::move(rel));
}
return {};
}
auto end_element(namespace_id, std::string_view) -> ooxml_status
{
depth_--;
return {};
}
auto text(std::string_view) -> ooxml_status
{
return {};
}
auto finish() -> ooxml_result<std::vector<relationship>>
{
if (!root_seen_) return tl::make_unexpected("missing OOXML relationships root");
return std::move(relationships_);
}
private:
std::string source_part_;
ooxml_limits limits_;
std::vector<relationship> relationships_;
ankerl::unordered_dense::set<std::string> ids_;
std::size_t depth_ = 0;
bool root_seen_ = false;
};
struct gstring_deleter {
void operator()(GString *str) const
{
if (str != nullptr) g_string_free(str, TRUE);
}
};
constexpr std::size_t max_auto_exec_names = 16;
struct extraction_result {
std::unique_ptr<GString, gstring_deleter> text{g_string_sized_new(1024)};
std::vector<std::string> urls;
ankerl::unordered_dense::set<std::string> url_seen;
std::vector<std::string> auto_exec_names;
std::size_t max_text;
std::size_t max_urls;
std::size_t sheets = 0;
std::size_t hidden_sheets = 0;
std::size_t very_hidden_sheets = 0;
bool last_chunk_is_newline = false;
extraction_result(std::size_t max_text, std::size_t max_urls)
: max_text{max_text}, max_urls{max_urls}
{
}
auto add_text(std::string_view value) -> ooxml_status
{
if (text->len + value.size() > max_text) {
return tl::make_unexpected("OOXML text limit exceeded");
}
g_string_append_len(text.get(), value.data(), value.size());
last_chunk_is_newline = value == "\n";
return {};
}
auto add_url(std::string value) -> ooxml_status
{
if (value.empty() || url_seen.contains(value)) return {};
if (urls.size() >= max_urls) {
return tl::make_unexpected("OOXML URL limit exceeded");
}
url_seen.emplace(value);
urls.push_back(std::move(value));
return {};
}
};
auto is_hyperlink_relationship(std::string_view type) -> bool
{
return type == hyperlink_relationship || type == strict_hyperlink_relationship;
}
auto hyperlink_from_instruction(std::string_view instruction) -> std::optional<std::string>
{
auto start_offset = rspamd_substring_search_caseless(instruction.data(), instruction.size(),
"hyperlink", sizeof("hyperlink") - 1);
if (start_offset == -1) return std::nullopt;
auto start = static_cast<std::size_t>(start_offset);
auto is_word = [](char ch) {
return g_ascii_isalnum(static_cast<unsigned char>(ch)) || ch == '_';
};
if ((start > 0 && is_word(instruction[start - 1])) ||
(start + 9 < instruction.size() && is_word(instruction[start + 9]))) {
return std::nullopt;
}
auto pos = start + 9;
while (pos < instruction.size() && g_ascii_isspace(instruction[pos])) pos++;
auto end = instruction.size();
while (end > pos && g_ascii_isspace(instruction[end - 1])) end--;
if (pos == end || instruction[pos] == '\\') return std::nullopt;
std::string_view target;
if (instruction[pos] == '"' || instruction[pos] == '\'') {
auto quote = instruction[pos++];
auto close = instruction.find(quote, pos);
if (close == std::string_view::npos) return std::nullopt;
target = instruction.substr(pos, close - pos);
}
else {
auto close = pos;
while (close < end && !g_ascii_isspace(instruction[close])) close++;
target = instruction.substr(pos, close - pos);
}
if (target.empty()) return std::nullopt;
std::string unescaped;
unescaped.reserve(target.size());
for (std::size_t i = 0; i < target.size(); i++) {
if (target[i] == '\\' && i + 1 < target.size()) i++;
unescaped.push_back(target[i]);
}
return unescaped;
}
using hyperlink_map = ankerl::unordered_dense::map<std::string, std::string>;
auto relationship_id(const std::vector<xml_attribute> &attributes) -> const std::string *
{
for (const auto &attribute: attributes) {
if (attribute.name == "id" &&
attribute.namespace_value == namespace_id::document_relationships) {
return &attribute.value;
}
}
return nullptr;
}
auto add_relationship_url(const hyperlink_map &relationships, const std::string *id,
extraction_result &result) -> ooxml_status
{
if (id == nullptr) return {};
auto found = relationships.find(*id);
if (found == relationships.end()) return {};
return result.add_url(found->second);
}
class word_story_handler {
public:
using namespaces = ooxml_namespaces;
word_story_handler(const hyperlink_map &relationships, extraction_result &result)
: relationships_{relationships}, result_{result}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
depth_++;
if (ns == namespace_id::word) {
if ((name == "del" || name == "moveFrom") && !excluded_depth_) {
excluded_depth_ = depth_;
}
else if (name == "t" && !excluded_depth_) {
text_depth_ = depth_;
}
else if (name == "tab" && !excluded_depth_) {
if (auto ret = result_.add_text("\t"); !ret) return ret;
}
else if ((name == "br" || name == "cr") && !excluded_depth_) {
if (auto ret = result_.add_text("\n"); !ret) return ret;
}
else if (name == "hyperlink") {
if (auto ret = add_relationship_url(relationships_, relationship_id(attributes),
result_);
!ret) {
return ret;
}
}
else if (name == "fldSimple") {
for (const auto &attribute: attributes) {
if (attribute.name == "instr" && attribute.namespace_value == namespace_id::word) {
auto url = hyperlink_from_instruction(attribute.value);
if (url) {
if (auto ret = result_.add_url(std::move(*url)); !ret) return ret;
}
break;
}
}
}
else if (name == "fldChar") {
const std::string *field_type = nullptr;
for (const auto &attribute: attributes) {
if (attribute.name == "fldCharType" &&
attribute.namespace_value == namespace_id::word) {
field_type = &attribute.value;
break;
}
}
if (field_type != nullptr && *field_type == "begin") {
fields_.push_back({});
}
else if (field_type != nullptr && *field_type == "separate") {
if (auto ret = finish_field(); !ret) return ret;
}
else if (field_type != nullptr && *field_type == "end" && !fields_.empty()) {
if (auto ret = finish_field(); !ret) return ret;
fields_.pop_back();
}
}
else if (name == "instrText" && !fields_.empty()) {
instruction_depth_ = depth_;
}
}
else if (ns == namespace_id::drawing) {
if (name == "t" && !excluded_depth_) {
text_depth_ = depth_;
}
else if (name == "hlinkClick" || name == "hlinkHover") {
return add_relationship_url(relationships_, relationship_id(attributes), result_);
}
}
return {};
}
auto end_element(namespace_id ns, std::string_view name) -> ooxml_status
{
if (ns == namespace_id::word && name == "p" && !excluded_depth_) {
if (auto ret = result_.add_text("\n"); !ret) return ret;
}
if (text_depth_ == depth_) text_depth_.reset();
if (instruction_depth_ == depth_) instruction_depth_.reset();
if (excluded_depth_ == depth_) excluded_depth_.reset();
depth_--;
return {};
}
auto text(std::string_view value) -> ooxml_status
{
if (instruction_depth_ && !fields_.empty()) {
fields_.back().instruction.append(value);
}
else if (text_depth_ && !excluded_depth_) {
return result_.add_text(value);
}
return {};
}
private:
struct field_state {
std::string instruction;
bool parsed = false;
};
auto finish_field() -> ooxml_status
{
if (!fields_.empty() && !fields_.back().parsed) {
auto url = hyperlink_from_instruction(fields_.back().instruction);
if (url) {
if (auto ret = result_.add_url(std::move(*url)); !ret) return ret;
}
fields_.back().parsed = true;
}
return {};
}
const hyperlink_map &relationships_;
extraction_result &result_;
std::vector<field_state> fields_;
std::size_t depth_ = 0;
std::optional<std::size_t> excluded_depth_;
std::optional<std::size_t> text_depth_;
std::optional<std::size_t> instruction_depth_;
};
/* DrawingML text bodies: standalone drawings, slides and notes slides */
class drawing_handler {
public:
using namespaces = ooxml_namespaces;
drawing_handler(const hyperlink_map &relationships, extraction_result &result)
: relationships_{relationships}, result_{result}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
depth_++;
if (ns != namespace_id::drawing) return {};
if (name == "t") {
text_depth_ = depth_;
}
else if (name == "br") {
return result_.add_text("\n");
}
else if (name == "tab") {
return result_.add_text("\t");
}
else if (name == "hlinkClick" || name == "hlinkHover") {
return add_relationship_url(relationships_, relationship_id(attributes), result_);
}
return {};
}
auto end_element(namespace_id ns, std::string_view name) -> ooxml_status
{
if (ns == namespace_id::drawing && name == "p") {
if (auto ret = result_.add_text("\n"); !ret) return ret;
}
if (text_depth_ == depth_) text_depth_.reset();
depth_--;
return {};
}
auto text(std::string_view value) -> ooxml_status
{
if (text_depth_) return result_.add_text(value);
return {};
}
private:
const hyperlink_map &relationships_;
extraction_result &result_;
std::size_t depth_ = 0;
std::optional<std::size_t> text_depth_;
};
/* xl/sharedStrings.xml: every string item becomes one text line */
class shared_strings_handler {
public:
using namespaces = ooxml_namespaces;
explicit shared_strings_handler(extraction_result &result)
: result_{result}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &) -> ooxml_status
{
depth_++;
if (ns != namespace_id::spreadsheet) return {};
if (name == "si") {
item_depth_ = depth_;
item_has_text_ = false;
}
else if (name == "rPh" && !excluded_depth_) {
excluded_depth_ = depth_;
}
else if (name == "t" && item_depth_ && !excluded_depth_) {
text_depth_ = depth_;
}
return {};
}
auto end_element(namespace_id ns, std::string_view name) -> ooxml_status
{
if (ns == namespace_id::spreadsheet && name == "si" && item_depth_ == depth_) {
item_depth_.reset();
if (item_has_text_) {
if (auto ret = result_.add_text("\n"); !ret) return ret;
}
}
if (text_depth_ == depth_) text_depth_.reset();
if (excluded_depth_ == depth_) excluded_depth_.reset();
depth_--;
return {};
}
auto text(std::string_view value) -> ooxml_status
{
if (text_depth_ && !excluded_depth_) {
item_has_text_ = true;
return result_.add_text(value);
}
return {};
}
private:
extraction_result &result_;
std::size_t depth_ = 0;
bool item_has_text_ = false;
std::optional<std::size_t> item_depth_;
std::optional<std::size_t> excluded_depth_;
std::optional<std::size_t> text_depth_;
};
auto looks_like_url(std::string_view value) -> bool
{
if (value.size() >= 4 && rspamd_lc_cmp(value.data(), "www.", 4) == 0) return true;
if (value.size() >= 7 && rspamd_lc_cmp(value.data(), "mailto:", 7) == 0) return true;
if (!has_uri_scheme(value)) return false;
return value.find("://") != std::string_view::npos;
}
/*
* Collect URL-like string literals from a cell formula. The first literal
* argument of HYPERLINK() is always taken, other literals only when they look
* like URLs on their own (WEBSERVICE, concatenations and so on).
*/
auto formula_urls(std::string_view formula, extraction_result &result) -> ooxml_status
{
std::size_t i = 0;
bool hyperlink_target = false;
while (i < formula.size()) {
auto ch = formula[i];
if (ch == '"') {
std::string literal;
i++;
while (i < formula.size()) {
if (formula[i] == '"') {
if (i + 1 < formula.size() && formula[i + 1] == '"') {
literal.push_back('"');
i += 2;
continue;
}
i++;
break;
}
literal.push_back(formula[i++]);
}
if (!literal.empty() && (hyperlink_target || looks_like_url(literal))) {
if (auto ret = result.add_url(std::move(literal)); !ret) return ret;
}
hyperlink_target = false;
}
else if (g_ascii_isalpha(ch) || ch == '_') {
auto start = i;
while (i < formula.size() &&
(g_ascii_isalnum(formula[i]) || formula[i] == '_' || formula[i] == '.')) {
i++;
}
auto word = formula.substr(start, i - start);
if (word.size() > 6 && rspamd_lc_cmp(word.data(), "_xlfn.", 6) == 0) {
word.remove_prefix(6);
}
auto next = i;
while (next < formula.size() && g_ascii_isspace(formula[next])) next++;
if (next < formula.size() && formula[next] == '(' && word.size() == 9 &&
rspamd_lc_cmp(word.data(), "hyperlink", 9) == 0) {
next++;
while (next < formula.size() && g_ascii_isspace(formula[next])) next++;
hyperlink_target = next < formula.size() && formula[next] == '"';
i = next;
}
}
else {
i++;
}
}
return {};
}
/* xl/worksheets/sheetN.xml and xl/macrosheets/sheetN.xml */
class worksheet_handler {
public:
using namespaces = ooxml_namespaces;
worksheet_handler(const hyperlink_map &relationships, extraction_result &result)
: relationships_{relationships}, result_{result}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
depth_++;
if (ns != namespace_id::spreadsheet) return {};
if (name == "row") {
row_has_text_ = false;
}
else if (name == "c") {
cell_has_text_ = false;
formula_string_cell_ = false;
auto *type = get_attribute(attributes, "t");
if (type != nullptr && *type == "str") formula_string_cell_ = true;
}
else if (name == "v" && formula_string_cell_) {
text_depth_ = depth_;
}
else if (name == "is") {
inline_depth_ = depth_;
}
else if (name == "t" && inline_depth_ && !excluded_depth_) {
text_depth_ = depth_;
}
else if (name == "rPh" && !excluded_depth_) {
excluded_depth_ = depth_;
}
else if (name == "f") {
formula_depth_ = depth_;
formula_.clear();
}
else if (name == "hyperlink") {
return add_relationship_url(relationships_, relationship_id(attributes), result_);
}
return {};
}
auto end_element(namespace_id ns, std::string_view name) -> ooxml_status
{
if (ns == namespace_id::spreadsheet) {
if (name == "row" && row_has_text_) {
row_has_text_ = false;
if (auto ret = result_.add_text("\n"); !ret) return ret;
}
else if (name == "f" && formula_depth_ == depth_) {
formula_depth_.reset();
auto ret = formula_urls(formula_, result_);
formula_.clear();
if (!ret) return ret;
}
}
if (text_depth_ == depth_) text_depth_.reset();
if (inline_depth_ == depth_) inline_depth_.reset();
if (excluded_depth_ == depth_) excluded_depth_.reset();
depth_--;
return {};
}
auto text(std::string_view value) -> ooxml_status
{
if (formula_depth_) {
formula_.append(value);
}
else if (text_depth_ && !excluded_depth_) {
if (!cell_has_text_ && row_has_text_) {
if (auto ret = result_.add_text("\t"); !ret) return ret;
}
cell_has_text_ = true;
row_has_text_ = true;
return result_.add_text(value);
}
return {};
}
private:
const hyperlink_map &relationships_;
extraction_result &result_;
std::string formula_;
std::size_t depth_ = 0;
bool row_has_text_ = false;
bool cell_has_text_ = false;
bool formula_string_cell_ = false;
std::optional<std::size_t> text_depth_;
std::optional<std::size_t> inline_depth_;
std::optional<std::size_t> excluded_depth_;
std::optional<std::size_t> formula_depth_;
};
auto is_auto_exec_name(std::string_view name) -> bool
{
constexpr std::string_view prefix = "_xlnm.";
if (name.size() > prefix.size() &&
rspamd_lc_cmp(name.data(), prefix.data(), prefix.size()) == 0) {
name.remove_prefix(prefix.size());
}
for (std::string_view candidate: {"auto_open", "auto_close", "auto_activate", "auto_deactivate"}) {
if (name.size() == candidate.size() &&
rspamd_lc_cmp(name.data(), candidate.data(), candidate.size()) == 0) {
return true;
}
}
return false;
}
/* xl/workbook.xml: sheet visibility and auto-executing defined names */
class workbook_handler {
public:
using namespaces = ooxml_namespaces;
explicit workbook_handler(extraction_result &result)
: result_{result}
{
}
auto start_element(namespace_id ns, std::string_view name,
const std::vector<xml_attribute> &attributes) -> ooxml_status
{
if (ns != namespace_id::spreadsheet) return {};
if (name == "sheet") {
result_.sheets++;
auto *state = get_attribute(attributes, "state");
if (state != nullptr) {
if (*state == "hidden") {
result_.hidden_sheets++;
}
else if (*state == "veryHidden") {
result_.very_hidden_sheets++;
}
}
}
else if (name == "definedName") {
auto *defined_name = get_attribute(attributes, "name");
if (defined_name != nullptr && is_auto_exec_name(*defined_name) &&
result_.auto_exec_names.size() < max_auto_exec_names) {
result_.auto_exec_names.push_back(*defined_name);
}
}
return {};
}
auto end_element(namespace_id, std::string_view) -> ooxml_status
{
return {};
}
auto text(std::string_view) -> ooxml_status
{
return {};
}
private:
extraction_result &result_;
};
enum class story_kind : std::uint8_t {
word,
drawing,
shared_strings,
worksheet,
workbook,
};
auto parse_story_kind(std::string_view kind) -> std::optional<story_kind>
{
if (kind == "word") return story_kind::word;
if (kind == "drawing" || kind == "slide") return story_kind::drawing;
if (kind == "shared_strings") return story_kind::shared_strings;
if (kind == "worksheet") return story_kind::worksheet;
if (kind == "workbook") return story_kind::workbook;
return std::nullopt;
}
template<typename Handler>
auto run_story(std::string_view input, const xml_limits &limits, Handler &handler,
std::size_t &total_tokens) -> ooxml_status
{
rspamd::xml::scanner scanner{input, limits, handler};
auto parsed = scanner.parse();
total_tokens += scanner.tokens();
return parsed;
}
auto read_limits(lua_State *L, int table_index) -> ooxml_result<ooxml_limits>
{
ooxml_limits result;
if (!lua_istable(L, table_index)) return result;
table_index = lua_absindex(L, table_index);
int64_t max_relationships = result.max_relationships;
int64_t max_content_types = result.max_content_types;
int64_t max_target_length = result.max_target_length;
int64_t max_text = result.max_text;
int64_t max_urls = result.max_urls;
GError *error = nullptr;
if (!rspamd_lua_parse_table_arguments(L, table_index, &error,
RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING,
"max_relationships=I;max_content_types=I;max_target_length=I;"
"max_text=I;max_urls=I",
&max_relationships, &max_content_types, &max_target_length,
&max_text, &max_urls)) {
auto message = error != nullptr ? std::string{error->message} : "invalid OOXML limits";
if (error != nullptr) g_error_free(error);
return tl::make_unexpected(std::move(message));
}
if (max_relationships >= 0) result.max_relationships = max_relationships;
if (max_content_types >= 0) result.max_content_types = max_content_types;
if (max_target_length >= 0) result.max_target_length = max_target_length;
if (max_text >= 0) result.max_text = max_text;
if (max_urls >= 0) result.max_urls = max_urls;
auto xml = rspamd::xml::read_limits(L, table_index, result.xml);
if (!xml) return tl::make_unexpected(std::move(xml.error()));
result.xml = *xml;
return result;
}
auto check_string(lua_State *L, int pos) -> std::optional<std::string_view>
{
if (lua_type(L, pos) != LUA_TSTRING) return std::nullopt;
size_t len;
auto *value = lua_tolstring(L, pos, &len);
return std::string_view{value, len};
}
auto push_error(lua_State *L, std::string_view error) -> int
{
lua_pushnil(L);
lua_pushlstring(L, error.data(), error.size());
return 2;
}
auto push_error_with_tokens(lua_State *L, std::string_view error, std::size_t tokens) -> int
{
push_error(L, error);
lua_pushinteger(L, tokens);
return 3;
}
void push_relationship(lua_State *L, const relationship &rel)
{
lua_createtable(L, 0, 5);
lua_pushlstring(L, rel.id.data(), rel.id.size());
lua_setfield(L, -2, "id");
lua_pushlstring(L, rel.type.data(), rel.type.size());
lua_setfield(L, -2, "type");
lua_pushlstring(L, rel.target.data(), rel.target.size());
lua_setfield(L, -2, "target");
lua_pushboolean(L, rel.external);
lua_setfield(L, -2, "external");
if (!rel.part_name.empty()) {
lua_pushlstring(L, rel.part_name.data(), rel.part_name.size());
lua_setfield(L, -2, "part_name");
}
}
auto read_hyperlinks(lua_State *L, int relationships_index) -> hyperlink_map
{
hyperlink_map result;
if (!lua_istable(L, relationships_index)) return result;
relationships_index = lua_absindex(L, relationships_index);
lua_getfield(L, relationships_index, "list");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return result;
}
auto list_index = lua_absindex(L, -1);
auto count = rspamd_lua_table_size(L, list_index);
for (int i = 1; i <= count; i++) {
lua_rawgeti(L, list_index, i);
if (lua_istable(L, -1)) {
auto rel_index = lua_absindex(L, -1);
const char *id = nullptr;
const char *type = nullptr;
const char *target = nullptr;
gboolean external = false;
GError *error = nullptr;
if (rspamd_lua_parse_table_arguments(L, rel_index, &error,
RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT,
"id=S;type=S;target=S;external=B",
&id, &type, &target, &external) &&
external && id != nullptr && type != nullptr && target != nullptr &&
is_hyperlink_relationship(type)) {
result.emplace(id, target);
}
if (error != nullptr) g_error_free(error);
}
lua_pop(L, 1);
}
lua_pop(L, 1);
return result;
}
void collapse_newlines(GString *text)
{
std::size_t read = 0;
std::size_t write = 0;
std::size_t newlines = 0;
while (read < text->len) {
auto ch = text->str[read++];
if (ch == '\n') {
newlines++;
if (newlines > 2) continue;
}
else {
newlines = 0;
}
text->str[write++] = ch;
}
g_string_truncate(text, write);
}
static int lua_ooxml_resolve_part_name(lua_State *L)
{
auto source = check_string(L, 1);
auto target = check_string(L, 2);
if (!source || !target) return push_error(L, "source and target strings expected");
auto result = resolve_part_name(*source, *target);
if (!result) return push_error(L, result.error());
lua_pushlstring(L, result->data(), result->size());
return 1;
}
static int lua_ooxml_hyperlink_from_instruction(lua_State *L)
{
auto instruction = check_string(L, 1);
if (!instruction) {
lua_pushnil(L);
return 1;
}
auto result = hyperlink_from_instruction(*instruction);
if (result) {
lua_pushlstring(L, result->data(), result->size());
}
else {
lua_pushnil(L);
}
return 1;
}
static int lua_ooxml_parse_content_types(lua_State *L)
{
auto *input = lua_check_text_or_string(L, 1);
if (input == nullptr) return push_error(L, "string or rspamd_text expected");
auto limits = read_limits(L, 2);
if (!limits) return push_error(L, limits.error());
content_types_handler handler{*limits};
rspamd::xml::scanner scanner{std::string_view{input->start, input->len},
rspamd::xml::effective_limits(limits->xml), handler};
if (auto parsed = scanner.parse(); !parsed) {
return push_error_with_tokens(L, parsed.error(), scanner.tokens());
}
auto result = handler.finish();
if (!result) return push_error_with_tokens(L, result.error(), scanner.tokens());
lua_createtable(L, 0, 2);
lua_createtable(L, 0, result->defaults.size());
for (const auto &[key, value]: result->defaults) {
lua_pushlstring(L, value.data(), value.size());
lua_setfield(L, -2, key.c_str());
}
lua_setfield(L, -2, "defaults");
lua_createtable(L, 0, result->overrides.size());
for (const auto &[key, value]: result->overrides) {
lua_pushlstring(L, value.data(), value.size());
lua_setfield(L, -2, key.c_str());
}
lua_setfield(L, -2, "overrides");
lua_pushnil(L);
lua_pushinteger(L, scanner.tokens());
return 3;
}
static int lua_ooxml_parse_relationships(lua_State *L)
{
auto *input = lua_check_text_or_string(L, 1);
if (input == nullptr) return push_error(L, "string or rspamd_text expected");
auto source = check_string(L, 2);
if (!source) return push_error(L, "source part string expected");
auto limits = read_limits(L, 3);
if (!limits) return push_error(L, limits.error());
relationships_handler handler{*source, *limits};
rspamd::xml::scanner scanner{std::string_view{input->start, input->len},
rspamd::xml::effective_limits(limits->xml), handler};
if (auto parsed = scanner.parse(); !parsed) {
return push_error_with_tokens(L, parsed.error(), scanner.tokens());
}
auto relationships = handler.finish();
if (!relationships) {
return push_error_with_tokens(L, relationships.error(), scanner.tokens());
}
lua_createtable(L, 0, 2);
auto result_index = lua_absindex(L, -1);
lua_createtable(L, relationships->size(), 0);
auto list_index = lua_absindex(L, -1);
lua_createtable(L, 0, relationships->size());
auto by_id_index = lua_absindex(L, -1);
for (std::size_t i = 0; i < relationships->size(); i++) {
push_relationship(L, (*relationships)[i]);
lua_pushvalue(L, -1);
lua_rawseti(L, list_index, i + 1);
lua_setfield(L, by_id_index, (*relationships)[i].id.c_str());
}
lua_setfield(L, result_index, "by_id");
lua_setfield(L, result_index, "list");
lua_pushnil(L);
lua_pushinteger(L, scanner.tokens());
return 3;
}
static int lua_ooxml_extract(lua_State *L)
{
if (!lua_istable(L, 1)) return push_error(L, "stories table expected");
auto limits = read_limits(L, 2);
if (!limits) return push_error(L, limits.error());
extraction_result result{limits->max_text, limits->max_urls};
std::size_t total_tokens = 0;
/*
* A story that cannot be read is left out and counted, with what was
* read of it kept: one broken part, or one made so, must not take away
* the text and the links of all the others
*/
int failed_stories = 0;
std::string first_error;
auto stories_index = lua_absindex(L, 1);
auto count = rspamd_lua_table_size(L, stories_index);
for (int i = 1; i <= count; i++) {
lua_rawgeti(L, stories_index, i);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return push_error_with_tokens(L, "invalid OOXML story entry", total_tokens);
}
auto story_index = lua_absindex(L, -1);
lua_getfield(L, story_index, "content");
auto *input = lua_check_text_or_string(L, -1);
if (input == nullptr) {
lua_pop(L, 2);
return push_error_with_tokens(L, "invalid OOXML story content", total_tokens);
}
lua_getfield(L, story_index, "kind");
auto kind = parse_story_kind(check_string(L, -1).value_or("word"));
lua_pop(L, 1);
if (!kind) {
lua_pop(L, 2);
return push_error_with_tokens(L, "invalid OOXML story kind", total_tokens);
}
lua_getfield(L, story_index, "relationships");
auto hyperlinks = read_hyperlinks(L, -1);
lua_pop(L, 1);
std::string_view content{input->start, input->len};
auto xml_limits_value = rspamd::xml::effective_limits(limits->xml);
ooxml_status parsed;
switch (*kind) {
case story_kind::word: {
word_story_handler handler{hyperlinks, result};
parsed = run_story(content, xml_limits_value, handler, total_tokens);
break;
}
case story_kind::drawing: {
drawing_handler handler{hyperlinks, result};
parsed = run_story(content, xml_limits_value, handler, total_tokens);
break;
}
case story_kind::shared_strings: {
shared_strings_handler handler{result};
parsed = run_story(content, xml_limits_value, handler, total_tokens);
break;
}
case story_kind::worksheet: {
worksheet_handler handler{hyperlinks, result};
parsed = run_story(content, xml_limits_value, handler, total_tokens);
break;
}
case story_kind::workbook: {
workbook_handler handler{result};
parsed = run_story(content, xml_limits_value, handler, total_tokens);
break;
}
}
if (!parsed) {
if (failed_stories++ == 0) {
first_error = parsed.error();
}
}
lua_pop(L, 1); /* content */
if (!result.last_chunk_is_newline) {
/* Fails only when the text is full already */
(void) result.add_text("\n");
}
lua_pop(L, 1); /* story */
}
if (failed_stories > 0 && failed_stories == count) {
/* Nothing of the document could be read */
return push_error_with_tokens(L, first_error, total_tokens);
}
collapse_newlines(result.text.get());
lua_createtable(L, 0, 6);
lua_pushstring(L, "text");
auto text_len = result.text->len;
auto *text_data = g_string_free(result.text.release(), FALSE);
auto *text = lua_new_text(L, text_data, text_len, FALSE);
text->flags |= RSPAMD_TEXT_FLAG_OWN;
lua_settable(L, -3);
lua_createtable(L, result.urls.size(), 0);
for (std::size_t i = 0; i < result.urls.size(); i++) {
lua_pushlstring(L, result.urls[i].data(), result.urls[i].size());
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "urls");
lua_pushinteger(L, result.sheets);
lua_setfield(L, -2, "sheets");
lua_pushinteger(L, result.hidden_sheets);
lua_setfield(L, -2, "hidden_sheets");
lua_pushinteger(L, result.very_hidden_sheets);
lua_setfield(L, -2, "very_hidden_sheets");
lua_createtable(L, result.auto_exec_names.size(), 0);
for (std::size_t i = 0; i < result.auto_exec_names.size(); i++) {
lua_pushlstring(L, result.auto_exec_names[i].data(), result.auto_exec_names[i].size());
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "auto_exec_names");
if (failed_stories > 0) {
lua_pushinteger(L, failed_stories);
lua_setfield(L, -2, "failed_stories");
lua_pushlstring(L, first_error.data(), first_error.size());
lua_setfield(L, -2, "error");
}
lua_pushnil(L);
lua_pushinteger(L, total_tokens);
return 3;
}
static const struct luaL_reg ooxml_lib[] = {
{"resolve_part_name", lua_ooxml_resolve_part_name},
{"hyperlink_from_instruction", lua_ooxml_hyperlink_from_instruction},
{"parse_content_types", lua_ooxml_parse_content_types},
{"parse_relationships", lua_ooxml_parse_relationships},
{"extract", lua_ooxml_extract},
{"extract_docx", lua_ooxml_extract},
{nullptr, nullptr},
};
}// namespace
void luaopen_ooxml(lua_State *L)
{
rspamd_lua_add_preload(L, "rspamd_ooxml", [](lua_State *LL) -> int {
luaL_register(LL, "rspamd_ooxml", ooxml_lib);
return 1;
});
}
|