summaryrefslogtreecommitdiffstats
path: root/tools/source/misc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /tools/source/misc
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/source/misc')
-rw-r--r--tools/source/misc/cpuid.cxx140
-rw-r--r--tools/source/misc/extendapplicationenvironment.cxx88
-rw-r--r--tools/source/misc/fix16.cxx172
-rw-r--r--tools/source/misc/json_writer.cxx482
-rw-r--r--tools/source/misc/pathutils.cxx109
5 files changed, 991 insertions, 0 deletions
diff --git a/tools/source/misc/cpuid.cxx b/tools/source/misc/cpuid.cxx
new file mode 100644
index 000000000..855b87e6d
--- /dev/null
+++ b/tools/source/misc/cpuid.cxx
@@ -0,0 +1,140 @@
+/* -*- 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/.
+ *
+ */
+
+#include <tools/cpuid.hxx>
+#include <cstdint>
+
+namespace cpuid
+{
+namespace
+{
+#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
+#include <intrin.h>
+void getCpuId(uint32_t array[4], uint32_t nInfoType)
+{
+ __cpuid(reinterpret_cast<int*>(array), nInfoType);
+}
+#elif (defined(__i386__) || defined(__x86_64__))
+#include <cpuid.h>
+void getCpuId(uint32_t array[4], uint32_t nInfoType)
+{
+ __cpuid_count(nInfoType, 0, *(array + 0), *(array + 1), *(array + 2), *(array + 3));
+}
+#else
+void getCpuId(uint32_t array[4], uint32_t /*nInfoType*/)
+{
+ array[0] = array[1] = array[2] = array[3] = 0;
+}
+#endif
+
+// For AVX we need to check if OS has support for ymm registers
+bool checkAVXSupportInOS()
+{
+ uint32_t xcr0 = 0;
+#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
+ xcr0 = uint32_t(_xgetbv(0));
+#elif (defined(__i386__) || defined(__x86_64__))
+ __asm__("xgetbv" : "=a"(xcr0) : "c"(0) : "%edx");
+#endif
+ return ((xcr0 & 6) == 6); /* checking if xmm and ymm state are enabled in XCR0 */
+}
+
+} // end anonymous namespace
+
+#define HYPER_bit (1 << 28)
+#define SSE2_bit (1 << 26)
+#define SSSE3_bit (1 << 9)
+#define SSE41_bit (1 << 19)
+#define SSE42_bit (1 << 20)
+#define XSAVE_bit (1 << 27)
+#define AVX_bit (1 << 28)
+#define AVX2_bit (1 << 5)
+#define AVX512F_bit (1 << 16)
+
+InstructionSetFlags getCpuInstructionSetFlags()
+{
+ InstructionSetFlags eInstructions = InstructionSetFlags::NONE;
+
+ uint32_t info[] = { 0, 0, 0, 0 };
+ getCpuId(info, 0);
+ int nLevel = info[0];
+
+ if (nLevel >= 1)
+ {
+ uint32_t aCpuInfoArray[] = { 0, 0, 0, 0 };
+ getCpuId(aCpuInfoArray, 1);
+
+ if ((aCpuInfoArray[3] & HYPER_bit) != 0)
+ eInstructions |= InstructionSetFlags::HYPER;
+
+ if ((aCpuInfoArray[3] & SSE2_bit) != 0)
+ eInstructions |= InstructionSetFlags::SSE2;
+
+ if ((aCpuInfoArray[2] & SSSE3_bit) != 0)
+ eInstructions |= InstructionSetFlags::SSSE3;
+
+ if ((aCpuInfoArray[2] & SSE41_bit) != 0)
+ eInstructions |= InstructionSetFlags::SSE41;
+
+ if ((aCpuInfoArray[2] & SSE42_bit) != 0)
+ eInstructions |= InstructionSetFlags::SSE42;
+
+ if (((aCpuInfoArray[2] & AVX_bit) != 0) && ((aCpuInfoArray[2] & XSAVE_bit) != 0))
+ {
+ if (checkAVXSupportInOS())
+ {
+ eInstructions |= InstructionSetFlags::AVX;
+
+ if (nLevel >= 7)
+ {
+ uint32_t aExtendedInfo[] = { 0, 0, 0, 0 };
+ getCpuId(aExtendedInfo, 7);
+
+ if ((aExtendedInfo[1] & AVX2_bit) != 0)
+ eInstructions |= InstructionSetFlags::AVX2;
+ if ((aExtendedInfo[1] & AVX512F_bit) != 0)
+ eInstructions |= InstructionSetFlags::AVX512F;
+ }
+ }
+ }
+ }
+
+ return eInstructions;
+}
+
+bool isCpuInstructionSetSupported(InstructionSetFlags eInstructions)
+{
+ static InstructionSetFlags eCPUFlags = getCpuInstructionSetFlags();
+ return (eCPUFlags & eInstructions) == eInstructions;
+}
+
+OUString instructionSetSupportedString()
+{
+ OUString aString;
+ if (isCpuInstructionSetSupported(InstructionSetFlags::SSE2))
+ aString += "SSE2 ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::SSSE3))
+ aString += "SSSE3 ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::SSE41))
+ aString += "SSE4.1 ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::SSE42))
+ aString += "SSE4.2 ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::AVX))
+ aString += "AVX ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::AVX2))
+ aString += "AVX2 ";
+ if (isCpuInstructionSetSupported(InstructionSetFlags::AVX512F))
+ aString += "AVX512F ";
+ return aString;
+}
+
+} // end cpuid
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/misc/extendapplicationenvironment.cxx b/tools/source/misc/extendapplicationenvironment.cxx
new file mode 100644
index 000000000..ce2237a88
--- /dev/null
+++ b/tools/source/misc/extendapplicationenvironment.cxx
@@ -0,0 +1,88 @@
+/* -*- 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 <config_folders.h>
+
+#include <sal/config.h>
+
+#include <stdlib.h>
+
+#if defined UNX
+#include <sys/resource.h>
+#endif
+
+#include <osl/process.h>
+#include <rtl/bootstrap.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.hxx>
+#include <sal/types.h>
+#include <tools/extendapplicationenvironment.hxx>
+
+namespace tools
+{
+void extendApplicationEnvironment()
+{
+#if defined UNX
+ // Try to set RLIMIT_NOFILE as large as possible (failure is harmless):
+ rlimit lim;
+ if (getrlimit(RLIMIT_NOFILE, &lim) == 0)
+ {
+ lim.rlim_cur = lim.rlim_max;
+ setrlimit(RLIMIT_NOFILE, &lim);
+ }
+#endif
+
+ // Make sure URE_BOOTSTRAP environment variable is set (failure is fatal):
+ OUStringBuffer env(512);
+ OUString envVar("URE_BOOTSTRAP");
+ OUString uri;
+ if (rtl::Bootstrap::get(envVar, uri))
+ {
+ if (!uri.matchIgnoreAsciiCase("vnd.sun.star.pathname:"))
+ {
+ uri = rtl::Bootstrap::encode(uri);
+ }
+ env.append(uri);
+ }
+ else
+ {
+ if (osl_getExecutableFile(&uri.pData) != osl_Process_E_None)
+ {
+ abort();
+ }
+ sal_Int32 lastDirSeparatorPos = uri.lastIndexOf('/');
+ if (lastDirSeparatorPos >= 0)
+ {
+ uri = uri.copy(0, lastDirSeparatorPos + 1);
+ }
+ env.append(rtl::Bootstrap::encode(uri));
+#ifdef MACOSX
+ env.append("../" LIBO_SHARE_FOLDER "/");
+#endif
+ env.append(SAL_CONFIGFILE("fundamental"));
+ }
+ OUString envValue(env.makeStringAndClear());
+ if (osl_setEnvironment(envVar.pData, envValue.pData) != osl_Process_E_None)
+ {
+ abort();
+ }
+}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/misc/fix16.cxx b/tools/source/misc/fix16.cxx
new file mode 100644
index 000000000..978f77291
--- /dev/null
+++ b/tools/source/misc/fix16.cxx
@@ -0,0 +1,172 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * libfixmath is Copyright (c) 2011-2021 Flatmush <Flatmush@gmail.com>,
+ * Petteri Aimonen <Petteri.Aimonen@gmail.com>, & libfixmath AUTHORS
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <tools/fix16.hxx>
+
+const fix16_t fix16_minimum = 0x80000000; /*!< the minimum value of fix16_t */
+const fix16_t fix16_overflow = 0x80000000; /*!< the value used to indicate overflows */
+
+static inline uint32_t fix_abs(fix16_t in)
+{
+ if (in == fix16_minimum)
+ {
+ // minimum negative number has same representation as
+ // its absolute value in unsigned
+ return 0x80000000;
+ }
+ else
+ {
+ return (in >= 0) ? in : -in;
+ }
+}
+
+/* 64-bit implementation for fix16_mul. Fastest version for e.g. ARM Cortex M3.
+ * Performs a 32*32 -> 64bit multiplication. The middle 32 bits are the result,
+ * bottom 16 bits are used for rounding, and upper 16 bits are used for overflow
+ * detection.
+ */
+
+fix16_t fix16_mul(fix16_t inArg0, fix16_t inArg1)
+{
+ int64_t product = static_cast<int64_t>(inArg0) * inArg1;
+
+ // The upper 17 bits should all be the same (the sign).
+ uint32_t upper = (product >> 47);
+
+ if (product < 0)
+ {
+ if (~upper)
+ return fix16_overflow;
+
+ // This adjustment is required in order to round -1/2 correctly
+ product--;
+ }
+ else
+ {
+ if (upper)
+ return fix16_overflow;
+ }
+
+ fix16_t result = product >> 16;
+ result += (product & 0x8000) >> 15;
+
+ return result;
+}
+
+/* 32-bit implementation of fix16_div. Fastest version for e.g. ARM Cortex M3.
+ * Performs 32-bit divisions repeatedly to reduce the remainder. For this to
+ * be efficient, the processor has to have 32-bit hardware division.
+ */
+#ifdef __GNUC__
+// Count leading zeros, using processor-specific instruction if available.
+#define clz(x) (__builtin_clzl(x) - (8 * sizeof(long) - 32))
+#else
+static uint8_t clz(uint32_t x)
+{
+ uint8_t result = 0;
+ if (x == 0)
+ return 32;
+ while (!(x & 0xF0000000))
+ {
+ result += 4;
+ x <<= 4;
+ }
+ while (!(x & 0x80000000))
+ {
+ result += 1;
+ x <<= 1;
+ }
+ return result;
+}
+#endif
+
+fix16_t fix16_div(fix16_t a, fix16_t b)
+{
+ // This uses a hardware 32/32 bit division multiple times, until we have
+ // computed all the bits in (a<<17)/b. Usually this takes 1-3 iterations.
+
+ if (b == 0)
+ return fix16_minimum;
+
+ uint32_t remainder = fix_abs(a);
+ uint32_t divider = fix_abs(b);
+ uint64_t quotient = 0;
+ int bit_pos = 17;
+
+ // Kick-start the division a bit.
+ // This improves speed in the worst-case scenarios where N and D are large
+ // It gets a lower estimate for the result by N/(D >> 17 + 1).
+ if (divider & 0xFFF00000)
+ {
+ uint32_t shifted_div = (divider >> 17) + 1;
+ quotient = remainder / shifted_div;
+ uint64_t tmp = (quotient * static_cast<uint64_t>(divider)) >> 17;
+ remainder -= static_cast<uint32_t>(tmp);
+ }
+
+ // If the divider is divisible by 2^n, take advantage of it.
+ while (!(divider & 0xF) && bit_pos >= 4)
+ {
+ divider >>= 4;
+ bit_pos -= 4;
+ }
+
+ while (remainder && bit_pos >= 0)
+ {
+ // Shift remainder as much as we can without overflowing
+ int shift = clz(remainder);
+ if (shift > bit_pos)
+ shift = bit_pos;
+ remainder <<= shift;
+ bit_pos -= shift;
+
+ uint32_t div = remainder / divider;
+ remainder = remainder % divider;
+ quotient += static_cast<uint64_t>(div) << bit_pos;
+
+ if (div & ~(0xFFFFFFFF >> bit_pos))
+ return fix16_overflow;
+
+ remainder <<= 1;
+ bit_pos--;
+ }
+
+ // Quotient is always positive so rounding is easy
+ quotient++;
+
+ fix16_t result = quotient >> 1;
+
+ // Figure out the sign of the result
+ if ((a ^ b) & 0x80000000)
+ {
+ if (result == fix16_minimum)
+ return fix16_overflow;
+
+ result = -result;
+ }
+
+ return result;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
new file mode 100644
index 000000000..3d78f82e0
--- /dev/null
+++ b/tools/source/misc/json_writer.cxx
@@ -0,0 +1,482 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * 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/.
+ */
+
+#include <tools/json_writer.hxx>
+#include <stdio.h>
+#include <cstring>
+#include <rtl/math.hxx>
+
+namespace tools
+{
+/** These buffers are short-lived, so rather waste some space and avoid the cost of
+ * repeated calls into the allocator */
+constexpr int DEFAULT_BUFFER_SIZE = 2048;
+
+JsonWriter::JsonWriter()
+ : mpBuffer(static_cast<char*>(malloc(DEFAULT_BUFFER_SIZE)))
+ , mPos(mpBuffer)
+ , mSpaceAllocated(DEFAULT_BUFFER_SIZE)
+ , mStartNodeCount(0)
+ , mbFirstFieldInNode(true)
+{
+ *mPos = '{';
+ ++mPos;
+ *mPos = ' ';
+ ++mPos;
+
+ addValidationMark();
+}
+
+JsonWriter::~JsonWriter()
+{
+ assert(!mpBuffer && "forgot to extract data?");
+ free(mpBuffer);
+}
+
+ScopedJsonWriterNode JsonWriter::startNode(const char* pNodeName)
+{
+ auto len = strlen(pNodeName);
+ ensureSpace(len + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pNodeName, len);
+ mPos += len;
+ memcpy(mPos, "\": { ", 5);
+ mPos += 5;
+ mStartNodeCount++;
+ mbFirstFieldInNode = true;
+
+ validate();
+
+ return ScopedJsonWriterNode(*this);
+}
+
+void JsonWriter::endNode()
+{
+ assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
+ --mStartNodeCount;
+ ensureSpace(1);
+ *mPos = '}';
+ ++mPos;
+ mbFirstFieldInNode = false;
+
+ validate();
+}
+
+ScopedJsonWriterArray JsonWriter::startArray(const char* pNodeName)
+{
+ auto len = strlen(pNodeName);
+ ensureSpace(len + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pNodeName, len);
+ mPos += len;
+ memcpy(mPos, "\": [ ", 5);
+ mPos += 5;
+ mStartNodeCount++;
+ mbFirstFieldInNode = true;
+
+ validate();
+
+ return ScopedJsonWriterArray(*this);
+}
+
+void JsonWriter::endArray()
+{
+ assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
+ --mStartNodeCount;
+ ensureSpace(1);
+ *mPos = ']';
+ ++mPos;
+ mbFirstFieldInNode = false;
+
+ validate();
+}
+
+ScopedJsonWriterStruct JsonWriter::startStruct()
+{
+ ensureSpace(6);
+
+ addCommaBeforeField();
+
+ *mPos = '{';
+ ++mPos;
+ *mPos = ' ';
+ ++mPos;
+ mStartNodeCount++;
+ mbFirstFieldInNode = true;
+
+ validate();
+
+ return ScopedJsonWriterStruct(*this);
+}
+
+void JsonWriter::endStruct()
+{
+ assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
+ --mStartNodeCount;
+ ensureSpace(1);
+ *mPos = '}';
+ ++mPos;
+ mbFirstFieldInNode = false;
+
+ validate();
+}
+
+static char getEscapementChar(char ch)
+{
+ switch (ch)
+ {
+ case '\b':
+ return 'b';
+ case '\t':
+ return 't';
+ case '\n':
+ return 'n';
+ case '\f':
+ return 'f';
+ case '\r':
+ return 'r';
+ default:
+ return ch;
+ }
+}
+
+static bool writeEscapedSequence(sal_uInt32 ch, char*& pos)
+{
+ switch (ch)
+ {
+ case '\b':
+ case '\t':
+ case '\n':
+ case '\f':
+ case '\r':
+ case '"':
+ case '/':
+ case '\\':
+ *pos++ = '\\';
+ *pos++ = getEscapementChar(ch);
+ return true;
+ // Special processing of U+2028 and U+2029, which are valid JSON, but invalid JavaScript
+ // Write them in escaped '\u2028' or '\u2029' form
+ case 0x2028:
+ case 0x2029:
+ *pos++ = '\\';
+ *pos++ = 'u';
+ *pos++ = '2';
+ *pos++ = '0';
+ *pos++ = '2';
+ *pos++ = ch == 0x2028 ? '8' : '9';
+ return true;
+ default:
+ return false;
+ }
+}
+
+void JsonWriter::writeEscapedOUString(const OUString& rPropVal)
+{
+ // Convert from UTF-16 to UTF-8 and perform escaping
+ sal_Int32 i = 0;
+ while (i < rPropVal.getLength())
+ {
+ sal_uInt32 ch = rPropVal.iterateCodePoints(&i);
+ if (writeEscapedSequence(ch, mPos))
+ continue;
+ if (ch <= 0x7F)
+ {
+ *mPos = static_cast<char>(ch);
+ ++mPos;
+ }
+ else if (ch <= 0x7FF)
+ {
+ *mPos = 0xC0 | (ch >> 6); /* 110xxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
+ else if (ch <= 0xFFFF)
+ {
+ *mPos = 0xE0 | (ch >> 12); /* 1110xxxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 6) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
+ else
+ {
+ *mPos = 0xF0 | (ch >> 18); /* 11110xxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 12) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 6) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
+ }
+
+ validate();
+}
+
+void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
+{
+ auto nPropNameLength = strlen(pPropName);
+ // But values can be any UTF-8,
+ // if the string only contains of 0x2028, it will be expanded 6 times (see writeEscapedSequence)
+ auto nWorstCasePropValLength = rPropVal.getLength() * 6;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ memcpy(mPos, "\": \"", 4);
+ mPos += 4;
+
+ writeEscapedOUString(rPropVal);
+
+ *mPos = '"';
+ ++mPos;
+
+ validate();
+}
+
+void JsonWriter::put(const char* pPropName, std::string_view rPropVal)
+{
+ // we assume property names are ascii
+ auto nPropNameLength = strlen(pPropName);
+ // escaping can double the length
+ auto nWorstCasePropValLength = rPropVal.size() * 2;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ memcpy(mPos, "\": \"", 4);
+ mPos += 4;
+
+ // copy and perform escaping
+ for (size_t i = 0; i < rPropVal.size(); ++i)
+ {
+ char ch = rPropVal[i];
+ switch (ch)
+ {
+ case '\b':
+ case '\t':
+ case '\n':
+ case '\f':
+ case '\r':
+ case '"':
+ case '/':
+ case '\\':
+ writeEscapedSequence(ch, mPos);
+ break;
+ case '\xE2': // Special processing of U+2028 and U+2029
+ if (i + 2 < rPropVal.size() && rPropVal[i + 1] == '\x80'
+ && (rPropVal[i + 2] == '\xA8' || rPropVal[i + 2] == '\xA9'))
+ {
+ writeEscapedSequence(rPropVal[i + 2] == '\xA8' ? 0x2028 : 0x2029, mPos);
+ i += 2;
+ break;
+ }
+ [[fallthrough]];
+ default:
+ *mPos = ch;
+ ++mPos;
+ break;
+ }
+ }
+
+ *mPos = '"';
+ ++mPos;
+
+ validate();
+}
+
+void JsonWriter::put(const char* pPropName, sal_Int64 nPropVal)
+{
+ auto nPropNameLength = strlen(pPropName);
+ auto nWorstCasePropValLength = 32;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ memcpy(mPos, "\": ", 3);
+ mPos += 3;
+
+ // clang-format off
+ SAL_WNODEPRECATED_DECLARATIONS_PUSH // sprintf (macOS 13 SDK)
+ mPos += sprintf(mPos, "%" SAL_PRIdINT64, nPropVal);
+ SAL_WNODEPRECATED_DECLARATIONS_POP
+ // clang-format on
+
+ validate();
+}
+
+void JsonWriter::put(const char* pPropName, double fPropVal)
+{
+ OString sPropVal = rtl::math::doubleToString(fPropVal, rtl_math_StringFormat_F, 12, '.');
+ auto nPropNameLength = strlen(pPropName);
+ ensureSpace(nPropNameLength + sPropVal.getLength() + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ memcpy(mPos, "\": ", 3);
+ mPos += 3;
+
+ memcpy(mPos, sPropVal.getStr(), sPropVal.getLength());
+ mPos += sPropVal.getLength();
+
+ validate();
+}
+
+void JsonWriter::put(const char* pPropName, bool nPropVal)
+{
+ auto nPropNameLength = strlen(pPropName);
+ ensureSpace(nPropNameLength + 5 + 8);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ memcpy(mPos, "\": ", 3);
+ mPos += 3;
+
+ const char* pVal;
+ if (nPropVal)
+ pVal = "true";
+ else
+ pVal = "false";
+ memcpy(mPos, pVal, strlen(pVal));
+ mPos += strlen(pVal);
+
+ validate();
+}
+
+void JsonWriter::putSimpleValue(const OUString& rPropVal)
+{
+ auto nWorstCasePropValLength = rPropVal.getLength() * 3;
+ ensureSpace(nWorstCasePropValLength + 4);
+
+ addCommaBeforeField();
+
+ *mPos = '"';
+ ++mPos;
+
+ writeEscapedOUString(rPropVal);
+
+ *mPos = '"';
+ ++mPos;
+
+ validate();
+}
+
+void JsonWriter::putRaw(std::string_view rRawBuf)
+{
+ ensureSpace(rRawBuf.size() + 2);
+
+ addCommaBeforeField();
+
+ memcpy(mPos, rRawBuf.data(), rRawBuf.size());
+ mPos += rRawBuf.size();
+
+ validate();
+}
+
+void JsonWriter::addCommaBeforeField()
+{
+ if (mbFirstFieldInNode)
+ mbFirstFieldInNode = false;
+ else
+ {
+ *mPos = ',';
+ ++mPos;
+ *mPos = ' ';
+ ++mPos;
+ }
+}
+
+void JsonWriter::ensureSpace(int noMoreBytesRequired)
+{
+ assert(mpBuffer && "already extracted data");
+ int currentUsed = mPos - mpBuffer;
+ if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
+ {
+ auto newSize = (currentUsed + noMoreBytesRequired) * 2;
+ mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize));
+ mPos = mpBuffer + currentUsed;
+ mSpaceAllocated = newSize;
+
+ addValidationMark();
+ }
+}
+
+/** Hands ownership of the underlying storage buffer to the caller,
+ * after this no more document modifications may be written. */
+std::pair<char*, int> JsonWriter::extractDataImpl()
+{
+ assert(mStartNodeCount == 0 && "did not close all nodes");
+ assert(mpBuffer && "data already extracted");
+ ensureSpace(2);
+ // add closing brace
+ *mPos = '}';
+ ++mPos;
+ // null-terminate
+ *mPos = 0;
+ const int sz = mPos - mpBuffer;
+ mPos = nullptr;
+ return { std::exchange(mpBuffer, nullptr), sz };
+}
+
+OString JsonWriter::extractAsOString()
+{
+ auto[pChar, sz] = extractDataImpl();
+ OString ret(pChar, sz);
+ free(pChar);
+ return ret;
+}
+
+std::string JsonWriter::extractAsStdString()
+{
+ auto[pChar, sz] = extractDataImpl();
+ std::string ret(pChar, sz);
+ free(pChar);
+ return ret;
+}
+
+bool JsonWriter::isDataEquals(const std::string& s) const
+{
+ return s.length() == static_cast<size_t>(mPos - mpBuffer)
+ && memcmp(s.data(), mpBuffer, s.length()) == 0;
+}
+
+} // namespace tools
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/tools/source/misc/pathutils.cxx b/tools/source/misc/pathutils.cxx
new file mode 100644
index 000000000..706740a32
--- /dev/null
+++ b/tools/source/misc/pathutils.cxx
@@ -0,0 +1,109 @@
+/* -*- 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 <sal/config.h>
+
+#if defined(_WIN32)
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#include <o3tl/safeint.hxx>
+#include <sal/types.h>
+#include <tools/pathutils.hxx>
+
+namespace tools {
+
+WCHAR * filename(WCHAR * path) {
+ WCHAR * f = path;
+ for (WCHAR * p = path;;) {
+ switch (*p++) {
+ case L'\0':
+ return f;
+ case L'\\':
+ f = p;
+ break;
+ }
+ }
+}
+
+WCHAR * buildPath(
+ WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
+ WCHAR const * backBegin, std::size_t backLength)
+{
+ // Remove leading ".." segments in the second path together with matching
+ // segments in the first path that are neither empty nor "." nor ".." nor
+ // end in ":" (which is not foolproof, as it can erroneously erase the start
+ // of a UNC path, but only if the input is bad data):
+ while (backLength >= 2 && backBegin[0] == L'.' && backBegin[1] == L'.' &&
+ (backLength == 2 || backBegin[2] == L'\\'))
+ {
+ if (frontEnd - frontBegin < 2 || frontEnd[-1] != L'\\' ||
+ frontEnd[-2] == L'\\' || frontEnd[-2] == L':' ||
+ (frontEnd[-2] == L'.' &&
+ (frontEnd - frontBegin < 3 || frontEnd[-3] == L'\\' ||
+ (frontEnd[-3] == L'.' &&
+ (frontEnd - frontBegin < 4 || frontEnd[-4] == L'\\')))))
+ {
+ break;
+ }
+ WCHAR const * p = frontEnd - 1;
+ while (p != frontBegin && p[-1] != L'\\') {
+ --p;
+ }
+ if (p == frontBegin) {
+ break;
+ }
+ frontEnd = p;
+ if (backLength == 2) {
+ backBegin += 2;
+ backLength -= 2;
+ } else {
+ backBegin += 3;
+ backLength -= 3;
+ }
+ }
+ if (backLength <
+ o3tl::make_unsigned(MAX_PATH - (frontEnd - frontBegin)))
+ {
+ WCHAR * p;
+ if (frontBegin == path) {
+ p = const_cast< WCHAR * >(frontEnd);
+ } else {
+ p = path;
+ while (frontBegin != frontEnd) {
+ *p++ = *frontBegin++;
+ }
+ }
+ for (; backLength > 0; --backLength) {
+ *p++ = *backBegin++;
+ }
+ *p = L'\0';
+ return p;
+ } else {
+ SetLastError(ERROR_FILENAME_EXCED_RANGE);
+ return nullptr;
+ }
+}
+
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */