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
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you 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 .
*/
#include <com/sun/star/sheet/TableValidationVisibility.hpp>
#include <xicontent.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/docfile.hxx>
#include <tools/urlobj.hxx>
#include <sfx2/linkmgr.hxx>
#include <svl/itemset.hxx>
#include <scitems.hxx>
#include <editeng/eeitem.hxx>
#include <svl/intitem.hxx>
#include <svl/stritem.hxx>
#include <editeng/flditem.hxx>
#include <editeng/editobj.hxx>
#include <unotools/charclass.hxx>
#include <unotools/configmgr.hxx>
#include <stringutil.hxx>
#include <cellform.hxx>
#include <cellvalue.hxx>
#include <document.hxx>
#include <editutil.hxx>
#include <validat.hxx>
#include <patattr.hxx>
#include <docpool.hxx>
#include <rangenam.hxx>
#include <arealink.hxx>
#include <stlsheet.hxx>
#include <xlcontent.hxx>
#include <xlformula.hxx>
#include <xltracer.hxx>
#include <xistream.hxx>
#include <xihelper.hxx>
#include <xistyle.hxx>
#include <xiescher.hxx>
#include <xiname.hxx>
#include <excform.hxx>
#include <tabprotection.hxx>
#include <documentimport.hxx>
#include <memory>
#include <utility>
#include <oox/helper/helper.hxx>
#include <sal/log.hxx>
using ::com::sun::star::uno::Sequence;
using ::std::unique_ptr;
// Shared string table ========================================================
XclImpSst::XclImpSst( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpSst::ReadSst( XclImpStream& rStrm )
{
rStrm.Ignore( 4 );
sal_uInt32 nStrCount = rStrm.ReaduInt32();
auto nBytesAvailable = rStrm.GetRecLeft();
if (nStrCount > nBytesAvailable)
{
SAL_WARN("sc.filter", "xls claimed to have " << nStrCount << " strings, but only " << nBytesAvailable << " bytes available, truncating");
nStrCount = nBytesAvailable;
}
maStrings.clear();
maStrings.reserve(nStrCount);
while( (nStrCount > 0) && rStrm.IsValid() )
{
XclImpString aString;
aString.Read( rStrm );
maStrings.push_back( aString );
--nStrCount;
}
}
const XclImpString* XclImpSst::GetString( sal_uInt32 nSstIndex ) const
{
return (nSstIndex < maStrings.size()) ? &maStrings[ nSstIndex ] : nullptr;
}
// Hyperlinks =================================================================
namespace {
/** Reads character array and stores it into rString.
@param nChars Number of following characters (not byte count!).
@param b16Bit true = 16-bit characters, false = 8-bit characters. */
void lclAppendString32( OUString& rString, XclImpStream& rStrm, sal_uInt32 nChars, bool b16Bit )
{
sal_uInt16 nReadChars = ulimit_cast< sal_uInt16 >( nChars );
rString += rStrm.ReadRawUniString( nReadChars, b16Bit );
// ignore remaining chars
std::size_t nIgnore = nChars - nReadChars;
if( b16Bit )
nIgnore *= 2;
rStrm.Ignore( nIgnore );
}
/** Reads 32-bit string length and the character array and stores it into rString.
@param b16Bit true = 16-bit characters, false = 8-bit characters. */
void lclAppendString32( OUString& rString, XclImpStream& rStrm, bool b16Bit )
{
lclAppendString32( rString, rStrm, rStrm.ReaduInt32(), b16Bit );
}
/** Reads 32-bit string length and ignores following 16-bit character array. */
void lclIgnoreString32( XclImpStream& rStrm )
{
sal_uInt32 nChars = rStrm.ReaduInt32();
nChars *= 2;
rStrm.Ignore( nChars );
}
/** Converts a path to an absolute path.
@param rPath The source path. The resulting path is returned here.
@param nLevel Number of parent directories to add in front of the path. */
void lclGetAbsPath( OUString& rPath, sal_uInt16 nLevel, const SfxObjectShell* pDocShell )
{
OUStringBuffer aTmpStr;
while( nLevel )
{
aTmpStr.append( "../" );
--nLevel;
}
aTmpStr.append( rPath );
if( pDocShell )
{
bool bWasAbs = false;
rPath = pDocShell->GetMedium()->GetURLObject().smartRel2Abs( aTmpStr.makeStringAndClear(), bWasAbs ).GetMainURL( INetURLObject::DecodeMechanism::NONE );
// full path as stored in SvxURLField must be encoded
}
else
rPath = aTmpStr.makeStringAndClear();
}
/** Inserts the URL into a text cell. Does not modify value or formula cells. */
void lclInsertUrl( XclImpRoot& rRoot, const OUString& rUrl, SCCOL nScCol, SCROW nScRow, SCTAB nScTab )
{
ScDocumentImport& rDoc = rRoot.GetDocImport();
ScAddress aScPos( nScCol, nScRow, nScTab );
ScRefCellValue aCell(rDoc.getDoc(), aScPos);
switch( aCell.meType )
{
// #i54261# hyperlinks in string cells
case CELLTYPE_STRING:
case CELLTYPE_EDIT:
{
sal_uInt32 nNumFmt = rDoc.getDoc().GetNumberFormat(rDoc.getDoc().GetNonThreadedContext(), aScPos);
SvNumberFormatter* pFormatter = rDoc.getDoc().GetFormatTable();
const Color* pColor;
OUString aDisplText = ScCellFormat::GetString(aCell, nNumFmt, &pColor, *pFormatter, rDoc.getDoc());
if (aDisplText.isEmpty())
aDisplText = rUrl;
ScEditEngineDefaulter& rEE = rRoot.GetEditEngine();
SvxURLField aUrlField( rUrl, aDisplText, SvxURLFormat::AppDefault );
if( aCell.meType == CELLTYPE_EDIT )
{
const EditTextObject* pEditObj = aCell.mpEditText;
rEE.SetTextCurrentDefaults( *pEditObj );
rEE.QuickInsertField( SvxFieldItem( aUrlField, EE_FEATURE_FIELD ), ESelection( 0, 0, EE_PARA_ALL, 0 ) );
}
else
{
rEE.SetTextCurrentDefaults( OUString() );
rEE.QuickInsertField( SvxFieldItem( aUrlField, EE_FEATURE_FIELD ), ESelection() );
if( const ScPatternAttr* pPattern = rDoc.getDoc().GetPattern( aScPos.Col(), aScPos.Row(), nScTab ) )
{
SfxItemSet aItemSet( rEE.GetEmptyItemSet() );
pPattern->FillEditItemSet( &aItemSet );
rEE.QuickSetAttribs( aItemSet, ESelection( 0, 0, EE_PARA_ALL, 0 ) );
}
}
// The cell will own the text object instance.
rDoc.setEditCell(aScPos, rEE.CreateTextObject());
}
break;
default:
// Handle other cell types e.g. formulas ( and ? ) that have associated
// hyperlinks.
// Ideally all hyperlinks should be treated as below. For the moment,
// given the current absence of ods support lets just handle what we
// previously didn't handle the new way.
// Unfortunately we won't be able to preserve such hyperlinks when
// saving to ods. Note: when we are able to save such hyperlinks to ods
// we should handle *all* imported hyperlinks as below ( e.g. as cell
// attribute ) for better interoperability.
{
SfxStringItem aItem( ATTR_HYPERLINK, rUrl );
rDoc.getDoc().ApplyAttr(nScCol, nScRow, nScTab, aItem);
break;
}
}
}
} // namespace
void XclImpHyperlink::ReadHlink( XclImpStream& rStrm )
{
XclRange aXclRange( ScAddress::UNINITIALIZED );
rStrm >> aXclRange;
// #i80006# Excel silently ignores invalid hi-byte of column index (TODO: everywhere?)
aXclRange.maFirst.mnCol &= 0xFF;
aXclRange.maLast.mnCol &= 0xFF;
OUString aString = ReadEmbeddedData( rStrm );
if ( !aString.isEmpty() )
rStrm.GetRoot().GetXFRangeBuffer().SetHyperlink( aXclRange, aString );
}
OUString XclImpHyperlink::ReadEmbeddedData( XclImpStream& rStrm )
{
const XclImpRoot& rRoot = rStrm.GetRoot();
SfxObjectShell* pDocShell = rRoot.GetDocShell();
OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
XclGuid aGuid;
rStrm >> aGuid;
rStrm.Ignore( 4 );
sal_uInt32 nFlags = rStrm.ReaduInt32();
OSL_ENSURE( aGuid == XclTools::maGuidStdLink, "XclImpHyperlink::ReadEmbeddedData - unknown header GUID" );
::std::unique_ptr< OUString > xLongName; // link / file name
::std::unique_ptr< OUString > xShortName; // 8.3-representation of file name
::std::unique_ptr< OUString > xTextMark; // text mark
// description (ignore)
if( ::get_flag( nFlags, EXC_HLINK_DESCR ) )
lclIgnoreString32( rStrm );
// target frame (ignore) !! DESCR/FRAME - is this the right order? (never seen them together)
if( ::get_flag( nFlags, EXC_HLINK_FRAME ) )
lclIgnoreString32( rStrm );
// URL fields are zero-terminated - do not let the stream replace them
// in the lclAppendString32() with the '?' character.
rStrm.SetNulSubstChar( '\0' );
// UNC path
if( ::get_flag( nFlags, EXC_HLINK_UNC ) )
{
xLongName.reset( new OUString );
lclAppendString32( *xLongName, rStrm, true );
lclGetAbsPath( *xLongName, 0, pDocShell );
}
// file link or URL
else if( ::get_flag( nFlags, EXC_HLINK_BODY ) )
{
rStrm >> aGuid;
if( aGuid == XclTools::maGuidFileMoniker )
{
sal_uInt16 nLevel = rStrm.ReaduInt16(); // counter for level to climb down in path
xShortName.reset( new OUString );
lclAppendString32( *xShortName, rStrm, false );
rStrm.Ignore( 24 );
sal_uInt32 nStrLen = rStrm.ReaduInt32();
if( nStrLen )
{
nStrLen = rStrm.ReaduInt32();
nStrLen /= 2; // it's byte count here...
rStrm.Ignore( 2 );
xLongName.reset( new OUString );
lclAppendString32( *xLongName, rStrm, nStrLen, true );
lclGetAbsPath( *xLongName, nLevel, pDocShell );
}
else
lclGetAbsPath( *xShortName, nLevel, pDocShell );
}
else if( aGuid == XclTools::maGuidUrlMoniker )
{
sal_uInt32 nStrLen = rStrm.ReaduInt32();
nStrLen /= 2; // it's byte count here...
xLongName.reset( new OUString );
lclAppendString32( *xLongName, rStrm, nStrLen, true );
if( !::get_flag( nFlags, EXC_HLINK_ABS ) )
lclGetAbsPath( *xLongName, 0, pDocShell );
}
else
{
OSL_FAIL( "XclImpHyperlink::ReadEmbeddedData - unknown content GUID" );
}
}
// text mark
if( ::get_flag( nFlags, EXC_HLINK_MARK ) )
{
xTextMark.reset( new OUString );
lclAppendString32( *xTextMark, rStrm, true );
}
rStrm.SetNulSubstChar(); // back to default
OSL_ENSURE( rStrm.GetRecLeft() == 0, "XclImpHyperlink::ReadEmbeddedData - record size mismatch" );
if (!xLongName && xShortName)
xLongName = std::move(xShortName);
else if (!xLongName && xTextMark)
xLongName.reset( new OUString );
if (xLongName)
{
if (xTextMark)
{
if( xLongName->isEmpty() )
{
sal_Int32 nSepPos = xTextMark->lastIndexOf( '!' );
if( nSepPos > 0 )
{
// Do not attempt to blindly convert '#SheetName!A1' to
// '#SheetName.A1', it can be #SheetName!R1C1 as well.
// Hyperlink handler has to handle all, but prefer
// '#SheetName.A1' if possible.
if (nSepPos < xTextMark->getLength() - 1)
{
ScDocument& rDoc = rRoot.GetDoc();
ScRange aRange;
if ((aRange.ParseAny( xTextMark->copy( nSepPos + 1 ), rDoc, formula::FormulaGrammar::CONV_XL_R1C1)
& ScRefFlags::VALID) == ScRefFlags::ZERO)
xTextMark.reset( new OUString( xTextMark->replaceAt( nSepPos, 1, rtl::OUStringChar( '.' ))));
}
}
}
xLongName.reset( new OUString( *xLongName + "#" + *xTextMark ) );
}
return( *xLongName );
}
return( OUString() );
}
void XclImpHyperlink::ConvertToValidTabName(OUString& rUrl)
{
sal_Int32 n = rUrl.getLength();
if (n < 4)
// Needs at least 4 characters.
return;
if (rUrl[0] != '#')
// the 1st character must be '#'.
return;
OUStringBuffer aNewUrl("#");
OUStringBuffer aTabName;
bool bInQuote = false;
bool bQuoteTabName = false;
for( sal_Int32 i = 1; i < n; ++i )
{
sal_Unicode c = rUrl[i];
if (c == '\'')
{
if (bInQuote && i+1 < n && rUrl[i+1] == '\'')
{
// Two consecutive single quotes ('') signify a single literal
// quite. When this occurs, the whole table name needs to be
// quoted.
bQuoteTabName = true;
aTabName.append(c).append(c);
++i;
continue;
}
bInQuote = !bInQuote;
if (!bInQuote && !aTabName.isEmpty())
{
if (bQuoteTabName)
aNewUrl.append("'");
aNewUrl.append(aTabName);
if (bQuoteTabName)
aNewUrl.append("'");
}
}
else if (bInQuote)
aTabName.append(c);
else
aNewUrl.append(c);
}
if (bInQuote)
// It should be outside the quotes!
return;
// All is good. Pass the new URL.
rUrl = aNewUrl.makeStringAndClear();
}
void XclImpHyperlink::InsertUrl( XclImpRoot& rRoot, const XclRange& rXclRange, const OUString& rUrl )
{
OUString aUrl(rUrl);
ConvertToValidTabName(aUrl);
SCTAB nScTab = rRoot.GetCurrScTab();
ScRange aScRange( ScAddress::UNINITIALIZED );
if( rRoot.GetAddressConverter().ConvertRange( aScRange, rXclRange, nScTab, nScTab, true ) )
{
SCCOL nScCol1, nScCol2;
SCROW nScRow1, nScRow2;
aScRange.GetVars( nScCol1, nScRow1, nScTab, nScCol2, nScRow2, nScTab );
if (utl::ConfigManager::IsFuzzing())
{
SCROW nRows = nScRow2 - nScRow1;
if (nRows > 1024)
{
SAL_WARN("sc.filter", "for fuzzing performance, clamped hyperlink apply range end row from " << nScRow2 << " to " << nScRow1 + 1024);
nScRow2 = nScRow1 + 1024;
}
}
for( SCCOL nScCol = nScCol1; nScCol <= nScCol2; ++nScCol )
for( SCROW nScRow = nScRow1; nScRow <= nScRow2; ++nScRow )
lclInsertUrl( rRoot, aUrl, nScCol, nScRow, nScTab );
}
}
// Label ranges ===============================================================
void XclImpLabelranges::ReadLabelranges( XclImpStream& rStrm )
{
const XclImpRoot& rRoot = rStrm.GetRoot();
OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
ScDocument& rDoc = rRoot.GetDoc();
SCTAB nScTab = rRoot.GetCurrScTab();
XclImpAddressConverter& rAddrConv = rRoot.GetAddressConverter();
ScRangePairListRef xLabelRangesRef;
XclRangeList aRowXclRanges, aColXclRanges;
rStrm >> aRowXclRanges >> aColXclRanges;
// row label ranges
ScRangeList aRowScRanges;
rAddrConv.ConvertRangeList( aRowScRanges, aRowXclRanges, nScTab, false );
xLabelRangesRef = rDoc.GetRowNameRangesRef();
for ( size_t i = 0, nRanges = aRowScRanges.size(); i < nRanges; ++i )
{
const ScRange & rScRange = aRowScRanges[ i ];
ScRange aDataRange( rScRange );
if( aDataRange.aEnd.Col() < rDoc.MaxCol() )
{
aDataRange.aStart.SetCol( aDataRange.aEnd.Col() + 1 );
aDataRange.aEnd.SetCol( rDoc.MaxCol() );
}
else if( aDataRange.aStart.Col() > 0 )
{
aDataRange.aEnd.SetCol( aDataRange.aStart.Col() - 1 );
aDataRange.aStart.SetCol( 0 );
}
xLabelRangesRef->Append( ScRangePair( rScRange, aDataRange ) );
}
// column label ranges
ScRangeList aColScRanges;
rAddrConv.ConvertRangeList( aColScRanges, aColXclRanges, nScTab, false );
xLabelRangesRef = rDoc.GetColNameRangesRef();
for ( size_t i = 0, nRanges = aColScRanges.size(); i < nRanges; ++i )
{
const ScRange & rScRange = aColScRanges[ i ];
ScRange aDataRange( rScRange );
if( aDataRange.aEnd.Row() < rDoc.MaxRow() )
{
aDataRange.aStart.SetRow( aDataRange.aEnd.Row() + 1 );
aDataRange.aEnd.SetRow( rDoc.MaxRow() );
}
else if( aDataRange.aStart.Row() > 0 )
{
aDataRange.aEnd.SetRow( aDataRange.aStart.Row() - 1 );
aDataRange.aStart.SetRow( 0 );
}
xLabelRangesRef->Append( ScRangePair( rScRange, aDataRange ) );
}
}
// Conditional formatting =====================================================
XclImpCondFormat::XclImpCondFormat( const XclImpRoot& rRoot, sal_uInt32 nFormatIndex ) :
XclImpRoot( rRoot ),
mnFormatIndex( nFormatIndex ),
mnCondCount( 0 ),
mnCondIndex( 0 )
{
}
XclImpCondFormat::~XclImpCondFormat()
{
}
void XclImpCondFormat::ReadCondfmt( XclImpStream& rStrm )
{
OSL_ENSURE( !mnCondCount, "XclImpCondFormat::ReadCondfmt - already initialized" );
XclRangeList aXclRanges;
mnCondCount = rStrm.ReaduInt16();
rStrm.Ignore( 10 );
rStrm >> aXclRanges;
GetAddressConverter().ConvertRangeList( maRanges, aXclRanges, GetCurrScTab(), true );
}
void XclImpCondFormat::ReadCF( XclImpStream& rStrm )
{
if( mnCondIndex >= mnCondCount )
{
OSL_FAIL( "XclImpCondFormat::ReadCF - CF without leading CONDFMT" );
return;
}
// entire conditional format outside of valid range?
if( maRanges.empty() )
return;
sal_uInt8 nType = rStrm.ReaduInt8();
sal_uInt8 nOperator = rStrm.ReaduInt8();
sal_uInt16 nFmlaSize1 = rStrm.ReaduInt16();
sal_uInt16 nFmlaSize2 = rStrm.ReaduInt16();
sal_uInt32 nFlags = rStrm.ReaduInt32();
rStrm.Ignore( 2 ); //nFlagsExtended
// *** mode and comparison operator ***
ScConditionMode eMode = ScConditionMode::NONE;
switch( nType )
{
case EXC_CF_TYPE_CELL:
{
switch( nOperator )
{
case EXC_CF_CMP_BETWEEN: eMode = ScConditionMode::Between; break;
case EXC_CF_CMP_NOT_BETWEEN: eMode = ScConditionMode::NotBetween; break;
case EXC_CF_CMP_EQUAL: eMode = ScConditionMode::Equal; break;
case EXC_CF_CMP_NOT_EQUAL: eMode = ScConditionMode::NotEqual; break;
case EXC_CF_CMP_GREATER: eMode = ScConditionMode::Greater; break;
case EXC_CF_CMP_LESS: eMode = ScConditionMode::Less; break;
case EXC_CF_CMP_GREATER_EQUAL: eMode = ScConditionMode::EqGreater; break;
case EXC_CF_CMP_LESS_EQUAL: eMode = ScConditionMode::EqLess; break;
default:
SAL_INFO(
"sc.filter", "unknown CF comparison " << nOperator);
}
}
break;
case EXC_CF_TYPE_FMLA:
eMode = ScConditionMode::Direct;
break;
default:
SAL_INFO("sc.filter", "unknown CF mode " << nType);
return;
}
// *** create style sheet ***
OUString aStyleName( XclTools::GetCondFormatStyleName( GetCurrScTab(), mnFormatIndex, mnCondIndex ) );
SfxItemSet& rStyleItemSet = ScfTools::MakeCellStyleSheet( GetStyleSheetPool(), aStyleName, true ).GetItemSet();
const XclImpPalette& rPalette = GetPalette();
// number format
if( get_flag( nFlags, EXC_CF_BLOCK_NUMFMT ) )
{
XclImpNumFmtBuffer& rNumFmtBuffer = GetRoot().GetNumFmtBuffer();
bool bIFmt = get_flag( nFlags, EXC_CF_IFMT_USER );
sal_uInt16 nFormat = rNumFmtBuffer.ReadCFFormat( rStrm, bIFmt );
rNumFmtBuffer.FillToItemSet( rStyleItemSet, nFormat );
}
// *** font block ***
if( ::get_flag( nFlags, EXC_CF_BLOCK_FONT ) )
{
XclImpFont aFont( GetRoot() );
aFont.ReadCFFontBlock( rStrm );
aFont.FillToItemSet( rStyleItemSet, XclFontItemType::Cell );
}
// alignment
if( get_flag( nFlags, EXC_CF_BLOCK_ALIGNMENT ) )
{
XclImpCellAlign aAlign;
sal_uInt16 nAlign(0);
sal_uInt16 nAlignMisc(0);
nAlign = rStrm.ReaduInt16();
nAlignMisc = rStrm.ReaduInt16();
aAlign.FillFromCF( nAlign, nAlignMisc );
aAlign.FillToItemSet( rStyleItemSet, nullptr );
rStrm.Ignore(4);
}
// *** border block ***
if( ::get_flag( nFlags, EXC_CF_BLOCK_BORDER ) )
{
sal_uInt16 nLineStyle(0);
sal_uInt32 nLineColor(0);
nLineStyle = rStrm.ReaduInt16();
nLineColor = rStrm.ReaduInt32();
rStrm.Ignore( 2 );
XclImpCellBorder aBorder;
aBorder.FillFromCF8( nLineStyle, nLineColor, nFlags );
aBorder.FillToItemSet( rStyleItemSet, rPalette );
}
// *** pattern block ***
if( ::get_flag( nFlags, EXC_CF_BLOCK_AREA ) )
{
sal_uInt16 nPattern(0), nColor(0);
nPattern = rStrm.ReaduInt16();
nColor = rStrm.ReaduInt16();
XclImpCellArea aArea;
aArea.FillFromCF8( nPattern, nColor, nFlags );
aArea.FillToItemSet( rStyleItemSet, rPalette );
}
if( get_flag( nFlags, EXC_CF_BLOCK_PROTECTION ) )
{
sal_uInt16 nCellProt;
nCellProt = rStrm.ReaduInt16();
XclImpCellProt aCellProt;
aCellProt.FillFromXF3(nCellProt);
aCellProt.FillToItemSet( rStyleItemSet );
}
// *** formulas ***
const ScAddress& rPos = maRanges.front().aStart; // assured above that maRanges is not empty
ExcelToSc& rFmlaConv = GetOldFmlaConverter();
::std::unique_ptr< ScTokenArray > xTokArr1;
if( nFmlaSize1 > 0 )
{
std::unique_ptr<ScTokenArray> pTokArr;
rFmlaConv.Reset( rPos );
rFmlaConv.Convert( pTokArr, rStrm, nFmlaSize1, false, FT_CondFormat );
// formula converter owns pTokArr -> create a copy of the token array
if( pTokArr )
{
xTokArr1 = std::move( pTokArr );
GetDoc().CheckLinkFormulaNeedingCheck( *xTokArr1);
}
}
::std::unique_ptr< ScTokenArray > xTokArr2;
if( nFmlaSize2 > 0 )
{
std::unique_ptr<ScTokenArray> pTokArr;
rFmlaConv.Reset( rPos );
rFmlaConv.Convert( pTokArr, rStrm, nFmlaSize2, false, FT_CondFormat );
// formula converter owns pTokArr -> create a copy of the token array
if( pTokArr )
{
xTokArr2 = std::move( pTokArr );
GetDoc().CheckLinkFormulaNeedingCheck( *xTokArr2);
}
}
// *** create the Calc conditional formatting ***
const ScAddress aPos(rPos); //in case maRanges.Join invalidates it
if( !mxScCondFmt )
{
mxScCondFmt.reset( new ScConditionalFormat( 0/*nKey*/, &GetDoc() ) );
if(maRanges.size() > 1)
maRanges.Join(maRanges[0], true);
mxScCondFmt->SetRange(maRanges);
}
ScCondFormatEntry* pEntry = new ScCondFormatEntry(eMode, xTokArr1.get(), xTokArr2.get(), GetDoc(), aPos, aStyleName);
mxScCondFmt->AddEntry( pEntry );
++mnCondIndex;
}
void XclImpCondFormat::Apply()
{
if( mxScCondFmt )
{
ScDocument& rDoc = GetDoc();
SCTAB nTab = maRanges.front().aStart.Tab();
sal_uLong nKey = rDoc.AddCondFormat( mxScCondFmt->Clone(), nTab );
rDoc.AddCondFormatData( maRanges, nTab, nKey );
}
}
XclImpCondFormatManager::XclImpCondFormatManager( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpCondFormatManager::ReadCondfmt( XclImpStream& rStrm )
{
XclImpCondFormat* pFmt = new XclImpCondFormat( GetRoot(), maCondFmtList.size() );
pFmt->ReadCondfmt( rStrm );
maCondFmtList.push_back( std::unique_ptr<XclImpCondFormat>(pFmt) );
}
void XclImpCondFormatManager::ReadCF( XclImpStream& rStrm )
{
OSL_ENSURE( !maCondFmtList.empty(), "XclImpCondFormatManager::ReadCF - CF without leading CONDFMT" );
if( !maCondFmtList.empty() )
maCondFmtList.back()->ReadCF( rStrm );
}
void XclImpCondFormatManager::Apply()
{
for( auto& rxFmt : maCondFmtList )
rxFmt->Apply();
maCondFmtList.clear();
}
// Data Validation ============================================================
XclImpValidationManager::DVItem::DVItem( const ScRangeList& rRanges, const ScValidationData& rValidData ) :
maRanges(rRanges), maValidData(rValidData) {}
XclImpValidationManager::XclImpValidationManager( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpValidationManager::ReadDval( XclImpStream& rStrm )
{
const XclImpRoot& rRoot = rStrm.GetRoot();
OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
sal_uInt32 nObjId(0);
rStrm.Ignore( 10 );
nObjId = rStrm.ReaduInt32();
if( nObjId != EXC_DVAL_NOOBJ )
{
OSL_ENSURE( nObjId <= 0xFFFF, "XclImpValidation::ReadDval - invalid object ID" );
rRoot.GetCurrSheetDrawing().SetSkipObj( static_cast< sal_uInt16 >( nObjId ) );
}
}
void XclImpValidationManager::ReadDV( XclImpStream& rStrm )
{
const XclImpRoot& rRoot = rStrm.GetRoot();
OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
ScDocument& rDoc = rRoot.GetDoc();
SCTAB nScTab = rRoot.GetCurrScTab();
ExcelToSc& rFmlaConv = rRoot.GetOldFmlaConverter();
// flags
sal_uInt32 nFlags = rStrm.ReaduInt32();
// message strings
/* Empty strings are single NUL characters in Excel (string length is 1).
-> Do not let the stream replace them with '?' characters. */
rStrm.SetNulSubstChar( '\0' );
OUString aPromptTitle( rStrm.ReadUniString() );
OUString aErrorTitle( rStrm.ReadUniString() );
OUString aPromptMessage( rStrm.ReadUniString() );
OUString aErrorMessage( rStrm.ReadUniString() );
rStrm.SetNulSubstChar(); // back to default
// formula(s)
if ( rStrm.GetRecLeft() <= 8 )
// Not enough bytes left in the record. Bail out.
return;
// first formula
// string list is single tStr token with NUL separators -> replace them with LF
rStrm.SetNulSubstChar( '\n' );
::std::unique_ptr< ScTokenArray > xTokArr1;
// We can't import the formula directly because we need the range
sal_uInt16 nLenFormula1 = rStrm.ReaduInt16();
rStrm.Ignore( 2 );
XclImpStreamPos aPosFormula1;
rStrm.StorePosition(aPosFormula1);
rStrm.Ignore(nLenFormula1);
// second formula
::std::unique_ptr< ScTokenArray > xTokArr2;
sal_uInt16 nLenFormula2 = rStrm.ReaduInt16();
rStrm.Ignore( 2 );
XclImpStreamPos aPosFormula2;
rStrm.StorePosition(aPosFormula2);
rStrm.Ignore(nLenFormula2);
// read all cell ranges
XclRangeList aXclRanges;
rStrm >> aXclRanges;
// convert to Calc range list
ScRangeList aScRanges;
rRoot.GetAddressConverter().ConvertRangeList( aScRanges, aXclRanges, nScTab, true );
// only continue if there are valid ranges
if ( aScRanges.empty() )
return;
ScRange aCombinedRange = aScRanges.Combine();
XclImpStreamPos aCurrentPos;
rStrm.StorePosition(aCurrentPos);
rStrm.RestorePosition(aPosFormula1);
if( nLenFormula1 > 0 )
{
std::unique_ptr<ScTokenArray> pTokArr;
rFmlaConv.Reset(aCombinedRange.aStart);
rFmlaConv.Convert( pTokArr, rStrm, nLenFormula1, false, FT_CondFormat );
// formula converter owns pTokArr -> create a copy of the token array
if( pTokArr )
xTokArr1 = std::move( pTokArr );
}
rStrm.SetNulSubstChar(); // back to default
if (nLenFormula2 > 0)
{
rStrm.RestorePosition(aPosFormula2);
std::unique_ptr<ScTokenArray> pTokArr;
rFmlaConv.Reset(aCombinedRange.aStart);
rFmlaConv.Convert( pTokArr, rStrm, nLenFormula2, false, FT_CondFormat );
// formula converter owns pTokArr -> create a copy of the token array
if( pTokArr )
xTokArr2 = std::move( pTokArr );
}
rStrm.RestorePosition(aCurrentPos);
bool bIsValid = true; // valid settings in flags field
ScValidationMode eValMode = SC_VALID_ANY;
switch( nFlags & EXC_DV_MODE_MASK )
{
case EXC_DV_MODE_ANY: eValMode = SC_VALID_ANY; break;
case EXC_DV_MODE_WHOLE: eValMode = SC_VALID_WHOLE; break;
case EXC_DV_MODE_DECIMAL: eValMode = SC_VALID_DECIMAL; break;
case EXC_DV_MODE_LIST: eValMode = SC_VALID_LIST; break;
case EXC_DV_MODE_DATE: eValMode = SC_VALID_DATE; break;
case EXC_DV_MODE_TIME: eValMode = SC_VALID_TIME; break;
case EXC_DV_MODE_TEXTLEN: eValMode = SC_VALID_TEXTLEN; break;
case EXC_DV_MODE_CUSTOM: eValMode = SC_VALID_CUSTOM; break;
default: bIsValid = false;
}
rRoot.GetTracer().TraceDVType(eValMode == SC_VALID_CUSTOM);
ScConditionMode eCondMode = ScConditionMode::Between;
switch( nFlags & EXC_DV_COND_MASK )
{
case EXC_DV_COND_BETWEEN: eCondMode = ScConditionMode::Between; break;
case EXC_DV_COND_NOTBETWEEN:eCondMode = ScConditionMode::NotBetween; break;
case EXC_DV_COND_EQUAL: eCondMode = ScConditionMode::Equal; break;
case EXC_DV_COND_NOTEQUAL: eCondMode = ScConditionMode::NotEqual; break;
case EXC_DV_COND_GREATER: eCondMode = ScConditionMode::Greater; break;
case EXC_DV_COND_LESS: eCondMode = ScConditionMode::Less; break;
case EXC_DV_COND_EQGREATER: eCondMode = ScConditionMode::EqGreater; break;
case EXC_DV_COND_EQLESS: eCondMode = ScConditionMode::EqLess; break;
default: bIsValid = false;
}
if ( !bIsValid )
// No valid validation found. Bail out.
return;
// The default value for comparison is _BETWEEN. However, custom
// rules are a formula, and thus the comparator should be ignored
// and only a true or false from the formula is evaluated. In Calc,
// formulas use comparison SC_COND_DIRECT.
if( eValMode == SC_VALID_CUSTOM )
{
eCondMode = ScConditionMode::Direct;
}
// first range for base address for relative references
const ScRange& rScRange = aScRanges.front(); // aScRanges is not empty
// process string list of a list validity (convert to list of string tokens)
if( xTokArr1 && (eValMode == SC_VALID_LIST) && ::get_flag( nFlags, EXC_DV_STRINGLIST ) )
XclTokenArrayHelper::ConvertStringToList(*xTokArr1, rDoc.GetSharedStringPool(), '\n');
maDVItems.push_back(
std::make_unique<DVItem>(aScRanges, ScValidationData(eValMode, eCondMode, xTokArr1.get(), xTokArr2.get(), rDoc, rScRange.aStart)));
DVItem& rItem = *maDVItems.back();
rItem.maValidData.SetIgnoreBlank( ::get_flag( nFlags, EXC_DV_IGNOREBLANK ) );
rItem.maValidData.SetListType( ::get_flagvalue( nFlags, EXC_DV_SUPPRESSDROPDOWN, css::sheet::TableValidationVisibility::INVISIBLE, css::sheet::TableValidationVisibility::UNSORTED ) );
// *** prompt box ***
if( !aPromptTitle.isEmpty() || !aPromptMessage.isEmpty() )
{
// set any text stored in the record
rItem.maValidData.SetInput( aPromptTitle, aPromptMessage );
if( !::get_flag( nFlags, EXC_DV_SHOWPROMPT ) )
rItem.maValidData.ResetInput();
}
// *** error box ***
ScValidErrorStyle eErrStyle = SC_VALERR_STOP;
switch( nFlags & EXC_DV_ERROR_MASK )
{
case EXC_DV_ERROR_WARNING: eErrStyle = SC_VALERR_WARNING; break;
case EXC_DV_ERROR_INFO: eErrStyle = SC_VALERR_INFO; break;
}
// set texts and error style
rItem.maValidData.SetError( aErrorTitle, aErrorMessage, eErrStyle );
if( !::get_flag( nFlags, EXC_DV_SHOWERROR ) )
rItem.maValidData.ResetError();
}
void XclImpValidationManager::Apply()
{
ScDocument& rDoc = GetRoot().GetDoc();
for (const auto& rxDVItem : maDVItems)
{
DVItem& rItem = *rxDVItem;
// set the handle ID
sal_uLong nHandle = rDoc.AddValidationEntry( rItem.maValidData );
ScPatternAttr aPattern( rDoc.GetPool() );
aPattern.GetItemSet().Put( SfxUInt32Item( ATTR_VALIDDATA, nHandle ) );
// apply all ranges
for ( size_t i = 0, nRanges = rItem.maRanges.size(); i < nRanges; ++i )
{
const ScRange & rScRange = rItem.maRanges[ i ];
rDoc.ApplyPatternAreaTab( rScRange.aStart.Col(), rScRange.aStart.Row(),
rScRange.aEnd.Col(), rScRange.aEnd.Row(), rScRange.aStart.Tab(), aPattern );
}
}
maDVItems.clear();
}
// Web queries ================================================================
XclImpWebQuery::XclImpWebQuery( const ScRange& rDestRange ) :
maDestRange( rDestRange ),
meMode( xlWQUnknown ),
mnRefresh( 0 )
{
}
void XclImpWebQuery::ReadParamqry( XclImpStream& rStrm )
{
sal_uInt16 nFlags = rStrm.ReaduInt16();
sal_uInt16 nType = ::extract_value< sal_uInt16 >( nFlags, 0, 3 );
if( !((nType == EXC_PQRYTYPE_WEBQUERY) && ::get_flag( nFlags, EXC_PQRY_WEBQUERY )) )
return;
if( ::get_flag( nFlags, EXC_PQRY_TABLES ) )
{
meMode = xlWQAllTables;
maTables = ScfTools::GetHTMLTablesName();
}
else
{
meMode = xlWQDocument;
maTables = ScfTools::GetHTMLDocName();
}
}
void XclImpWebQuery::ReadWqstring( XclImpStream& rStrm )
{
maURL = rStrm.ReadUniString();
}
void XclImpWebQuery::ReadWqsettings( XclImpStream& rStrm )
{
rStrm.Ignore( 10 );
sal_uInt16 nFlags = rStrm.ReaduInt16();
rStrm.Ignore( 10 );
mnRefresh = rStrm.ReaduInt16();
if( ::get_flag( nFlags, EXC_WQSETT_SPECTABLES ) && (meMode == xlWQAllTables) )
meMode = xlWQSpecTables;
}
void XclImpWebQuery::ReadWqtables( XclImpStream& rStrm )
{
if( meMode != xlWQSpecTables )
return;
rStrm.Ignore( 4 );
OUString aTables( rStrm.ReadUniString() );
const sal_Unicode cSep = ';';
static const OUStringLiteral aQuotedPairs( u"\"\"" );
maTables.clear();
for ( sal_Int32 nStringIx {aTables.isEmpty() ? -1 : 0}; nStringIx>=0; )
{
OUString aToken( ScStringUtil::GetQuotedToken( aTables, 0, aQuotedPairs, ',', nStringIx ) );
sal_Int32 nTabNum = CharClass::isAsciiNumeric( aToken ) ? aToken.toInt32() : 0;
if( nTabNum > 0 )
maTables = ScGlobal::addToken( maTables, ScfTools::GetNameFromHTMLIndex( static_cast< sal_uInt32 >( nTabNum ) ), cSep );
else
{
ScGlobal::EraseQuotes( aToken, '"', false );
if( !aToken.isEmpty() )
maTables = ScGlobal::addToken( maTables, ScfTools::GetNameFromHTMLName( aToken ), cSep );
}
}
}
void XclImpWebQuery::Apply( ScDocument& rDoc, const OUString& rFilterName )
{
if( !maURL.isEmpty() && (meMode != xlWQUnknown) && rDoc.GetDocumentShell() )
{
ScAreaLink* pLink = new ScAreaLink( rDoc.GetDocumentShell(),
maURL, rFilterName, OUString(), maTables, maDestRange, mnRefresh * 60UL );
rDoc.GetLinkManager()->InsertFileLink( *pLink, sfx2::SvBaseLinkObjectType::ClientFile,
maURL, &rFilterName, &maTables );
}
}
XclImpWebQueryBuffer::XclImpWebQueryBuffer( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpWebQueryBuffer::ReadQsi( XclImpStream& rStrm )
{
if( GetBiff() == EXC_BIFF8 )
{
rStrm.Ignore( 10 );
OUString aXclName( rStrm.ReadUniString() );
// #i64794# Excel replaces spaces with underscores
aXclName = aXclName.replaceAll( " ", "_" );
// find the defined name used in Calc
if( const XclImpName* pName = GetNameManager().FindName( aXclName, GetCurrScTab() ) )
{
if( const ScRangeData* pRangeData = pName->GetScRangeData() )
{
ScRange aRange;
if( pRangeData->IsReference( aRange ) )
maWQList.emplace_back( aRange );
}
}
}
else
{
DBG_ERROR_BIFF();
}
}
void XclImpWebQueryBuffer::ReadParamqry( XclImpStream& rStrm )
{
if (!maWQList.empty())
maWQList.back().ReadParamqry( rStrm );
}
void XclImpWebQueryBuffer::ReadWqstring( XclImpStream& rStrm )
{
if (!maWQList.empty())
maWQList.back().ReadWqstring( rStrm );
}
void XclImpWebQueryBuffer::ReadWqsettings( XclImpStream& rStrm )
{
if (!maWQList.empty())
maWQList.back().ReadWqsettings( rStrm );
}
void XclImpWebQueryBuffer::ReadWqtables( XclImpStream& rStrm )
{
if (!maWQList.empty())
maWQList.back().ReadWqtables( rStrm );
}
void XclImpWebQueryBuffer::Apply()
{
ScDocument& rDoc = GetDoc();
for( auto& rQuery : maWQList )
rQuery.Apply( rDoc, EXC_WEBQRY_FILTER );
}
// Decryption =================================================================
namespace {
XclImpDecrypterRef lclReadFilepass5( XclImpStream& rStrm )
{
XclImpDecrypterRef xDecr;
OSL_ENSURE( rStrm.GetRecLeft() == 4, "lclReadFilepass5 - wrong record size" );
if( rStrm.GetRecLeft() == 4 )
{
sal_uInt16 nKey(0), nHash(0);
nKey = rStrm.ReaduInt16();
nHash = rStrm.ReaduInt16();
xDecr = std::make_shared<XclImpBiff5Decrypter>( nKey, nHash );
}
return xDecr;
}
XclImpDecrypterRef lclReadFilepass8_Standard( XclImpStream& rStrm )
{
XclImpDecrypterRef xDecr;
OSL_ENSURE( rStrm.GetRecLeft() == 48, "lclReadFilepass8 - wrong record size" );
if( rStrm.GetRecLeft() == 48 )
{
std::vector<sal_uInt8> aSalt(16);
std::vector<sal_uInt8> aVerifier(16);
std::vector<sal_uInt8> aVerifierHash(16);
rStrm.Read(aSalt.data(), 16);
rStrm.Read(aVerifier.data(), 16);
rStrm.Read(aVerifierHash.data(), 16);
xDecr = std::make_shared<XclImpBiff8StdDecrypter>(std::move(aSalt), std::move(aVerifier), std::move(aVerifierHash));
}
return xDecr;
}
XclImpDecrypterRef lclReadFilepass8_Strong(XclImpStream& rStream)
{
//It is possible there are other variants in existence but these
//are the defaults I get with Excel 2013
XclImpDecrypterRef xDecr;
msfilter::RC4EncryptionInfo info;
info.header.flags = rStream.ReaduInt32();
if (oox::getFlag( info.header.flags, msfilter::ENCRYPTINFO_EXTERNAL))
return xDecr;
sal_uInt32 nHeaderSize = rStream.ReaduInt32();
sal_uInt32 actualHeaderSize = sizeof(info.header);
if( nHeaderSize < actualHeaderSize )
return xDecr;
info.header.flags = rStream.ReaduInt32();
info.header.sizeExtra = rStream.ReaduInt32();
info.header.algId = rStream.ReaduInt32();
info.header.algIdHash = rStream.ReaduInt32();
info.header.keyBits = rStream.ReaduInt32();
info.header.providedType = rStream.ReaduInt32();
info.header.reserved1 = rStream.ReaduInt32();
info.header.reserved2 = rStream.ReaduInt32();
rStream.Ignore(nHeaderSize - actualHeaderSize);
info.verifier.saltSize = rStream.ReaduInt32();
if (info.verifier.saltSize != msfilter::SALT_LENGTH)
return xDecr;
rStream.Read(&info.verifier.salt, sizeof(info.verifier.salt));
rStream.Read(&info.verifier.encryptedVerifier, sizeof(info.verifier.encryptedVerifier));
info.verifier.encryptedVerifierHashSize = rStream.ReaduInt32();
if (info.verifier.encryptedVerifierHashSize != RTL_DIGEST_LENGTH_SHA1)
return xDecr;
rStream.Read(&info.verifier.encryptedVerifierHash, info.verifier.encryptedVerifierHashSize);
// check flags and algorithm IDs, required are AES128 and SHA-1
if (!oox::getFlag(info.header.flags, msfilter::ENCRYPTINFO_CRYPTOAPI))
return xDecr;
if (oox::getFlag(info.header.flags, msfilter::ENCRYPTINFO_AES))
return xDecr;
if (info.header.algId != msfilter::ENCRYPT_ALGO_RC4)
return xDecr;
// hash algorithm ID 0 defaults to SHA-1 too
if (info.header.algIdHash != 0 && info.header.algIdHash != msfilter::ENCRYPT_HASH_SHA1)
return xDecr;
xDecr = std::make_shared<XclImpBiff8CryptoAPIDecrypter>(
std::vector<sal_uInt8>(info.verifier.salt,
info.verifier.salt + SAL_N_ELEMENTS(info.verifier.salt)),
std::vector<sal_uInt8>(info.verifier.encryptedVerifier,
info.verifier.encryptedVerifier + SAL_N_ELEMENTS(info.verifier.encryptedVerifier)),
std::vector<sal_uInt8>(info.verifier.encryptedVerifierHash,
info.verifier.encryptedVerifierHash + SAL_N_ELEMENTS(info.verifier.encryptedVerifierHash)));
return xDecr;
}
XclImpDecrypterRef lclReadFilepass8( XclImpStream& rStrm )
{
XclImpDecrypterRef xDecr;
sal_uInt16 nMode = rStrm.ReaduInt16();
switch( nMode )
{
case EXC_FILEPASS_BIFF5:
xDecr = lclReadFilepass5( rStrm );
break;
case EXC_FILEPASS_BIFF8:
{
sal_uInt32 nVersion = rStrm.ReaduInt32();
if (nVersion == msfilter::VERSION_INFO_1997_FORMAT)
{
//A Version structure where Version.vMajor MUST be 0x0001,
//and Version.vMinor MUST be 0x0001.
xDecr = lclReadFilepass8_Standard(rStrm);
}
else if (nVersion == msfilter::VERSION_INFO_2007_FORMAT ||
nVersion == msfilter::VERSION_INFO_2007_FORMAT_SP2)
{
//Version.vMajor MUST be 0x0002, 0x0003 or 0x0004 and
//Version.vMinor MUST be 0x0002.
xDecr = lclReadFilepass8_Strong(rStrm);
}
else
OSL_FAIL("lclReadFilepass8 - unknown BIFF8 encryption sub mode");
}
break;
default:
OSL_FAIL( "lclReadFilepass8 - unknown encryption mode" );
}
return xDecr;
}
} // namespace
const ErrCode& XclImpDecryptHelper::ReadFilepass( XclImpStream& rStrm )
{
XclImpDecrypterRef xDecr;
rStrm.DisableDecryption();
// read the FILEPASS record and create a new decrypter object
switch( rStrm.GetRoot().GetBiff() )
{
case EXC_BIFF2:
case EXC_BIFF3:
case EXC_BIFF4:
case EXC_BIFF5: xDecr = lclReadFilepass5( rStrm ); break;
case EXC_BIFF8: xDecr = lclReadFilepass8( rStrm ); break;
default: DBG_ERROR_BIFF();
};
// set decrypter at import stream
rStrm.SetDecrypter( xDecr );
// request and verify a password (decrypter implements IDocPasswordVerifier)
if( xDecr )
rStrm.GetRoot().RequestEncryptionData( *xDecr );
// return error code (success, wrong password, etc.)
return xDecr ? xDecr->GetError() : EXC_ENCR_ERROR_UNSUPP_CRYPT;
}
// Document protection ========================================================
XclImpDocProtectBuffer::XclImpDocProtectBuffer( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot ),
mnPassHash(0x0000),
mbDocProtect(false),
mbWinProtect(false)
{
}
void XclImpDocProtectBuffer::ReadDocProtect( XclImpStream& rStrm )
{
mbDocProtect = rStrm.ReaduInt16() != 0;
}
void XclImpDocProtectBuffer::ReadWinProtect( XclImpStream& rStrm )
{
mbWinProtect = rStrm.ReaduInt16() != 0;
}
void XclImpDocProtectBuffer::ReadPasswordHash( XclImpStream& rStrm )
{
rStrm.EnableDecryption();
mnPassHash = rStrm.ReaduInt16();
}
void XclImpDocProtectBuffer::Apply() const
{
if (!mbDocProtect && !mbWinProtect)
// Excel requires either the structure or windows protection is set.
// If neither is set then the document is not protected at all.
return;
unique_ptr<ScDocProtection> pProtect(new ScDocProtection);
pProtect->setProtected(true);
if (mnPassHash)
{
// 16-bit password hash.
Sequence<sal_Int8> aPass{sal_Int8(mnPassHash >> 8), sal_Int8(mnPassHash & 0xFF)};
pProtect->setPasswordHash(aPass, PASSHASH_XL);
}
// document protection options
pProtect->setOption(ScDocProtection::STRUCTURE, mbDocProtect);
pProtect->setOption(ScDocProtection::WINDOWS, mbWinProtect);
GetDoc().SetDocProtection(pProtect.get());
}
// Sheet Protection ===========================================================
XclImpSheetProtectBuffer::Sheet::Sheet() :
mbProtected(false),
mnPasswordHash(0x0000),
mnOptions(0x4400)
{
}
XclImpSheetProtectBuffer::Sheet::Sheet(const Sheet& r) :
mbProtected(r.mbProtected),
mnPasswordHash(r.mnPasswordHash),
mnOptions(r.mnOptions)
{
}
XclImpSheetProtectBuffer::XclImpSheetProtectBuffer( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpSheetProtectBuffer::ReadProtect( XclImpStream& rStrm, SCTAB nTab )
{
if ( rStrm.ReaduInt16() )
{
Sheet* pSheet = GetSheetItem(nTab);
if (pSheet)
pSheet->mbProtected = true;
}
}
void XclImpSheetProtectBuffer::ReadOptions( XclImpStream& rStrm, SCTAB nTab )
{
// The flag size specifies the size of bytes that follows that stores
// feature data. If -1 it depends on the feature type imported earlier.
// For enhanced protection data, the size is always 4. For the most xls
// documents out there this value is almost always -1.
sal_Int32 nFlagSize = rStrm.ReadInt32();
if (nFlagSize != -1)
return;
// There are actually 4 bytes to read, but the upper 2 bytes currently
// don't store any bits.
sal_uInt16 nOptions = rStrm.ReaduInt16();
Sheet* pSheet = GetSheetItem(nTab);
if (pSheet)
pSheet->mnOptions = nOptions;
}
void XclImpSheetProtectBuffer::AppendEnhancedProtection( const ScEnhancedProtection & rProt, SCTAB nTab )
{
Sheet* pSheet = GetSheetItem(nTab);
if (pSheet)
pSheet->maEnhancedProtections.push_back( rProt);
}
void XclImpSheetProtectBuffer::ReadPasswordHash( XclImpStream& rStrm, SCTAB nTab )
{
sal_uInt16 nHash = rStrm.ReaduInt16();
Sheet* pSheet = GetSheetItem(nTab);
if (pSheet)
pSheet->mnPasswordHash = nHash;
}
void XclImpSheetProtectBuffer::Apply() const
{
for (const auto& [rTab, rSheet] : maProtectedSheets)
{
if (!rSheet.mbProtected)
// This sheet is (for whatever reason) not protected.
continue;
unique_ptr<ScTableProtection> pProtect(new ScTableProtection);
pProtect->setProtected(true);
// 16-bit hash password
const sal_uInt16 nHash = rSheet.mnPasswordHash;
if (nHash)
{
Sequence<sal_Int8> aPass{sal_Int8(nHash >> 8), sal_Int8(nHash & 0xFF)};
pProtect->setPasswordHash(aPass, PASSHASH_XL);
}
// sheet protection options
const sal_uInt16 nOptions = rSheet.mnOptions;
pProtect->setOption( ScTableProtection::OBJECTS, (nOptions & 0x0001) );
pProtect->setOption( ScTableProtection::SCENARIOS, (nOptions & 0x0002) );
pProtect->setOption( ScTableProtection::FORMAT_CELLS, (nOptions & 0x0004) );
pProtect->setOption( ScTableProtection::FORMAT_COLUMNS, (nOptions & 0x0008) );
pProtect->setOption( ScTableProtection::FORMAT_ROWS, (nOptions & 0x0010) );
pProtect->setOption( ScTableProtection::INSERT_COLUMNS, (nOptions & 0x0020) );
pProtect->setOption( ScTableProtection::INSERT_ROWS, (nOptions & 0x0040) );
pProtect->setOption( ScTableProtection::INSERT_HYPERLINKS, (nOptions & 0x0080) );
pProtect->setOption( ScTableProtection::DELETE_COLUMNS, (nOptions & 0x0100) );
pProtect->setOption( ScTableProtection::DELETE_ROWS, (nOptions & 0x0200) );
pProtect->setOption( ScTableProtection::SELECT_LOCKED_CELLS, (nOptions & 0x0400) );
pProtect->setOption( ScTableProtection::SORT, (nOptions & 0x0800) );
pProtect->setOption( ScTableProtection::AUTOFILTER, (nOptions & 0x1000) );
pProtect->setOption( ScTableProtection::PIVOT_TABLES, (nOptions & 0x2000) );
pProtect->setOption( ScTableProtection::SELECT_UNLOCKED_CELLS, (nOptions & 0x4000) );
// Enhanced protection containing editable ranges and permissions.
pProtect->setEnhancedProtection( std::vector(rSheet.maEnhancedProtections) );
// all done. now commit.
GetDoc().SetTabProtection(rTab, pProtect.get());
}
}
XclImpSheetProtectBuffer::Sheet* XclImpSheetProtectBuffer::GetSheetItem( SCTAB nTab )
{
ProtectedSheetMap::iterator itr = maProtectedSheets.find(nTab);
if (itr == maProtectedSheets.end())
{
// new sheet
if ( !maProtectedSheets.emplace( nTab, Sheet() ).second )
return nullptr;
itr = maProtectedSheets.find(nTab);
}
return &itr->second;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|