diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-05-02 14:35:23 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-05-03 19:37:13 +0200 |
commit | bb459008de9d410e6e7ea982ce30aa22f70ae849 (patch) | |
tree | d6280d2fb247f81372f4a064aa0d1e31d20c252d /vcl/source/filter | |
parent | 76803b37038f47e7c74bbf3896c2a9b16a45909e (diff) |
vcl: add DetectorTools + tests, refactor array string matching
Add DetectorTools with byte array searching and matching to a
input string (or another byte array). This refactors the existing
function in GraphicFormatDetector. It needs to go into its own
header so that the function(s) can be tested easily. Replace
the previous searchEntry implementation with refactored one in
the source code.
Change-Id: I59d30b694e13f28d6366f1a99fe2ef2ea3c1a07d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93339
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl/source/filter')
-rw-r--r-- | vcl/source/filter/GraphicFormatDetector.cxx | 53 |
1 files changed, 19 insertions, 34 deletions
diff --git a/vcl/source/filter/GraphicFormatDetector.cxx b/vcl/source/filter/GraphicFormatDetector.cxx index 56624074366e..5682d78f8e3c 100644 --- a/vcl/source/filter/GraphicFormatDetector.cxx +++ b/vcl/source/filter/GraphicFormatDetector.cxx @@ -22,6 +22,7 @@ #include <algorithm> #include <graphic/GraphicFormatDetector.hxx> +#include <graphic/DetectorTools.hxx> #include <tools/solar.h> #include <tools/zcodec.hxx> @@ -67,23 +68,6 @@ bool isPCT(SvStream& rStream, sal_uLong nStreamPos, sal_uLong nStreamLen) return false; } -sal_uInt8* searchEntry(sal_uInt8* pSource, const char* pDest, sal_uLong nComp, sal_uLong nSize) -{ - while (nComp-- >= nSize) - { - sal_uLong i; - for (i = 0; i < nSize; i++) - { - if ((pSource[i] & ~0x20) != (pDest[i] & ~0x20)) - break; - } - if (i == nSize) - return pSource; - pSource++; - } - return nullptr; -} - } // end anonymous namespace GraphicFormatDetector::GraphicFormatDetector(SvStream& rStream, OUString const& rFormatExtension) @@ -320,13 +304,15 @@ bool GraphicFormatDetector::checkPSD() bool GraphicFormatDetector::checkEPS() { + const char* pFirstBytesAsCharArray = reinterpret_cast<char*>(maFirstBytes.data()); + if (mnFirstLong == 0xC5D0D3C6) { msDetectedFormat = "EPS"; return true; } - else if (searchEntry(maFirstBytes.data(), "%!PS-Adobe", 10, 10) - && searchEntry(&maFirstBytes[15], "EPS", 3, 3)) + else if (matchArrayWithString(pFirstBytesAsCharArray, 10, "%!PS-Adobe") + && matchArrayWithString(pFirstBytesAsCharArray + 15, 3, "EPS")) { msDetectedFormat = "EPS"; return true; @@ -419,7 +405,8 @@ bool GraphicFormatDetector::checkRAS() bool GraphicFormatDetector::checkXPM() { - if (searchEntry(maFirstBytes.data(), "/* XPM */", 256, 9)) + const char* pFirstBytesAsCharArray = reinterpret_cast<char*>(maFirstBytes.data()); + if (matchArrayWithString(pFirstBytesAsCharArray, 256, "/* XPM */")) { msDetectedFormat = "XPM"; return true; @@ -434,15 +421,13 @@ bool GraphicFormatDetector::checkXBM() mrStream.Seek(mnStreamPosition); mrStream.ReadBytes(pBuffer.get(), nSize); - sal_uInt8* pPtr = searchEntry(pBuffer.get(), "#define", nSize, 7); - if (pPtr) + const char* pBufferAsCharArray = reinterpret_cast<char*>(pBuffer.get()); + + if (checkArrayForMatchingStrings(pBufferAsCharArray, nSize, { "#define", "_width" })) { - if (searchEntry(pPtr, "_width", pBuffer.get() + nSize - pPtr, 6)) - { - msDetectedFormat = "XBM"; - return true; - } + msDetectedFormat = "XBM"; + return true; } return false; } @@ -473,20 +458,20 @@ bool GraphicFormatDetector::checkSVG() bool bIsSvg(false); + const char* pCheckArrayAsCharArray = reinterpret_cast<char*>(pCheckArray); + // check for XML // #119176# SVG files which have no xml header at all have shown up this is optional // check for "xml" then "version" then "DOCTYPE" and "svg" tags - if (searchEntry(pCheckArray, "<?xml", nCheckSize, 5) - && searchEntry(pCheckArray, "version", nCheckSize, 7) - && searchEntry(pCheckArray, "DOCTYPE", nCheckSize, 7) - && searchEntry(pCheckArray, "svg", nCheckSize, 3)) + if (checkArrayForMatchingStrings(pCheckArrayAsCharArray, nCheckSize, + { "<?xml", "version", "DOCTYPE", "svg" })) { bIsSvg = true; } // check for svg element in 1st 256 bytes // search for '<svg' - if (!bIsSvg && searchEntry(pCheckArray, "<svg", nCheckSize, 4)) + if (!bIsSvg && checkArrayForMatchingStrings(pCheckArrayAsCharArray, nCheckSize, { "<svg" })) { bIsSvg = true; } @@ -499,7 +484,7 @@ bool GraphicFormatDetector::checkSVG() // with Svg files containing big comment headers or Svg as the host // language - pCheckArray = sExtendedOrDecompressedFirstBytes; + pCheckArrayAsCharArray = reinterpret_cast<char*>(sExtendedOrDecompressedFirstBytes); if (bIsGZip) { @@ -513,7 +498,7 @@ bool GraphicFormatDetector::checkSVG() } // search for '<svg' - if (searchEntry(pCheckArray, "<svg", nCheckSize, 4)) + if (checkArrayForMatchingStrings(pCheckArrayAsCharArray, nCheckSize, { "<svg" })) { bIsSvg = true; } |