Replace hotfix with upstream patch, tdf#127887 follow-up

This attempt tries to avoid affecting other .wk1 files as well.

The old fix was even applied to the wrong patch section, not done for
MacOSX or Windows. Geez..

Change-Id: Ia79383f33ff760cba9bfb44f166e27f3ef212c57
Reviewed-on: https://gerrit.libreoffice.org/83431
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
diff --git a/external/libwps/UnpackedTarball_libwps.mk b/external/libwps/UnpackedTarball_libwps.mk
index b0d5dff..4fd5667 100644
--- a/external/libwps/UnpackedTarball_libwps.mk
+++ b/external/libwps/UnpackedTarball_libwps.mk
@@ -21,13 +21,13 @@ $(eval $(call gb_UnpackedTarball_add_patches,libwps,\
	$(if $(SYSTEM_REVENGE),,external/libwps/rpath.patch.0) \
	external/libwps/libtool.patch.0 \
	external/libwps/0001-Make-comparison-operator-member-functions-const.patch \
	external/libwps/libwps-lotus-version-tdf127887-upstream.patch \
))

ifneq ($(OS),MACOSX)
ifneq ($(OS),WNT)
$(eval $(call gb_UnpackedTarball_add_patches,libwps,\
	external/libwps/libwps-bundled-soname.patch.0 \
	external/libwps/libwps-lotus-version-tdf127887.patch.1 \
))
endif
endif
diff --git a/external/libwps/libwps-lotus-version-tdf127887-upstream.patch b/external/libwps/libwps-lotus-version-tdf127887-upstream.patch
new file mode 100644
index 0000000..012a8a0
--- /dev/null
+++ b/external/libwps/libwps-lotus-version-tdf127887-upstream.patch
@@ -0,0 +1,111 @@
This is a combination of upstream patches.

commit d74790b4bd55a3a11b56dbbd670c3baa1331e6dd
CommitDate: Thu Oct 31 11:26:08 2019 +0100

    lotus[wb1]: try to correct the retrieving of row references in formula...


commit 39be60f9045a758178d90526ae66b0e3d24f348e
CommitDate: Thu Oct 31 13:28:54 2019 +0100

    wks,wk1,...: try to read correctly the row cell's references...


commit 188933f0d66c77a19ccb0b5f5151a4c9f7203fbc
CommitDate: Thu Oct 31 14:30:26 2019 +0100

    wks,wk1: do not change the maximum number of columns...


commit c1ba33b7505cff91f57d5aa7e8c6ff8706b63a21
CommitDate: Thu Oct 31 15:09:19 2019 +0100

    WKS4Spreadsheet.cpp: simplify the code...


diff --git a/src/lib/WKS4.cpp b/src/lib/WKS4.cpp
index 2f222d3..a92eace 100644
--- a/src/lib/WKS4.cpp
+++ b/src/lib/WKS4.cpp
@@ -836,7 +836,7 @@ bool WKS4Parser::readZone()
 			val=int(libwps::readU8(input));
 			f << "Entries(ItCount):dos";
 			if (val!=1) f << "=" << val << ",";
-			if (m_state->m_version==2)
+			else if (m_state->m_version==2)
 				m_state->m_version=1;
 			isParsed = needWriteInAscii = true;
 			break;
diff --git a/src/lib/WKS4Spreadsheet.cpp b/src/lib/WKS4Spreadsheet.cpp
index caaf4e1..582f377 100644
--- a/src/lib/WKS4Spreadsheet.cpp
+++ b/src/lib/WKS4Spreadsheet.cpp
@@ -1798,10 +1798,14 @@ bool WKS4Spreadsheet::readCell
 	for (int dim = 0; dim < 2; dim++)
 	{
 		auto val = int(libwps::readU16(m_input));
-		if ((val & 0xF000) == 0); // absolue value ?
-		else if ((val & 0xc000) == 0x8000)   // relative ?
+		if ((val & 0x8000) == 0); // absolue value ?
+		else
 		{
-			if (version()==1)
+			// relative
+			// wb1: maximum row=0x2000, maximum col=0x100
+			// wks dos (v3) maximum row=0x4000, maximum col=0x100
+			// wdb maximum number of data ?
+			if (version()==1 && dim==0)
 			{
 				val &= 0xFF;
 				if ((val & 0x80) && val+actPos[dim] >= 0x100)
@@ -1810,27 +1814,17 @@ bool WKS4Spreadsheet::readCell
 			}
 			else
 			{
-				val &= 0x3FFF;
-				if (val & 0x2000) val = val - 0x4000;
+				// 0x400 for old file(unsure), ie. find many problematic files on
+				//   the web, so maybe 0x4000 is ok and these files are
+				//   problematic
+				int const maxVal= (dim==1 || m_mainParser.creator()==libwps::WPS_LOTUS) ? 0x2000 : version()==1 ? 0x400 : 0x4000;
+				val &= (2*maxVal-1);
+				if (val & maxVal) val = val - 2*maxVal;
+				if (val+actPos[dim]>=maxVal) val-=maxVal;
 			}
 			val += actPos[dim];
 			absolute[dim] = false;
 		}
-		else if (val==0xFFFF)
-		{
-			static bool first=true;
-			if (first)   // in general associated with a nan value, so maybe be normal
-			{
-				WPS_DEBUG_MSG(("WKS4Spreadsheet::readCell: find some ffff cell\n"));
-				first=false;
-			}
-			ok = false;
-		}
-		else
-		{
-			WPS_DEBUG_MSG(("WKS4Spreadsheet::readCell: can not read cell %x\n", unsigned(val)));
-			ok = false;
-		}
 		pos[dim] = val;
 	}
 
diff --git a/src/lib/WPSDocument.cpp b/src/lib/WPSDocument.cpp
index 7e974b6..5b18eb8 100644
--- a/src/lib/WPSDocument.cpp
+++ b/src/lib/WPSDocument.cpp
@@ -155,6 +155,10 @@ WPSLIB WPSConfidence WPSDocument::isFileFormatSupported(librevenge::RVNGInputStr
 			WKS4Parser parser(header->getInput(), header);
 			if (!parser.checkHeader(header.get(), true))
 				return WPS_CONFIDENCE_NONE;
+			// checkHeader() may set new kind and creator values,
+			// pass them up to caller.
+			kind = header->getKind();
+			creator = header->getCreator();
 			needEncoding=header->getNeedEncoding();
 			return header->getIsEncrypted() ? WPS_CONFIDENCE_SUPPORTED_ENCRYPTION : WPS_CONFIDENCE_EXCELLENT;
 		}
diff --git a/external/libwps/libwps-lotus-version-tdf127887.patch.1 b/external/libwps/libwps-lotus-version-tdf127887.patch.1
deleted file mode 100644
index 3427ef1..0000000
--- a/external/libwps/libwps-lotus-version-tdf127887.patch.1
+++ /dev/null
@@ -1,44 +0,0 @@
diff -ur libwps.org/src/lib/WKS4.cpp libwps/src/lib/WKS4.cpp
--- libwps.org/src/lib/WKS4.cpp	2018-08-06 10:51:12.000000000 +0200
+++ libwps/src/lib/WKS4.cpp	2019-10-01 17:28:59.497500545 +0200
@@ -477,7 +477,7 @@
 			}
 			else if (val==0x406)
 			{
-				m_state->m_version=1;
+				m_state->m_version=2;
 				f << "lotus,";
 				creatorId=libwps::WPS_LOTUS;
 			}
@@ -830,14 +830,15 @@
 			isParsed = true;
 			break;
 		case 0x2f: // iteration count: only in dos file Wk1, Wks(dos), Wq[12] ?
+			// Maybe, but that does not mean it would not be used
+			// in Lotus version 2 files (that are also .WK1), so do
+			// not reset an already detected version here.
 			if (sz!=1) break;
 			input->seek(pos+4, librevenge::RVNG_SEEK_SET);
 			f.str("");
 			val=int(libwps::readU8(input));
 			f << "Entries(ItCount):dos";
 			if (val!=1) f << "=" << val << ",";
-			if (m_state->m_version==2)
-				m_state->m_version=1;
 			isParsed = needWriteInAscii = true;
 			break;
 		case 0x41: // graph record name
diff -ur libwps.org/src/lib/WPSDocument.cpp libwps/src/lib/WPSDocument.cpp
--- libwps.org/src/lib/WPSDocument.cpp	2018-08-02 12:12:26.000000000 +0200
+++ libwps/src/lib/WPSDocument.cpp	2019-10-01 16:00:41.172596186 +0200
@@ -155,6 +155,10 @@
 			WKS4Parser parser(header->getInput(), header);
 			if (!parser.checkHeader(header.get(), true))
 				return WPS_CONFIDENCE_NONE;
+			// checkHeader() may set new kind and creator values,
+			// pass them up to caller.
+			kind = header->getKind();
+			creator = header->getCreator();
 			needEncoding=header->getNeedEncoding();
 			return header->getIsEncrypted() ? WPS_CONFIDENCE_SUPPORTED_ENCRYPTION : WPS_CONFIDENCE_EXCELLENT;
 		}