diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /tools/source/misc | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/source/misc')
-rw-r--r-- | tools/source/misc/cpuid.cxx | 140 | ||||
-rw-r--r-- | tools/source/misc/extendapplicationenvironment.cxx | 88 | ||||
-rw-r--r-- | tools/source/misc/fix16.cxx | 172 | ||||
-rw-r--r-- | tools/source/misc/json_writer.cxx | 405 | ||||
-rw-r--r-- | tools/source/misc/pathutils.cxx | 109 |
5 files changed, 914 insertions, 0 deletions
diff --git a/tools/source/misc/cpuid.cxx b/tools/source/misc/cpuid.cxx new file mode 100644 index 0000000000..855b87e6da --- /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 0000000000..07f9779ccc --- /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 && !defined EMSCRIPTEN + // 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 0000000000..978f77291d --- /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 0000000000..e3e27bf756 --- /dev/null +++ b/tools/source/misc/json_writer.cxx @@ -0,0 +1,405 @@ +/* -*- 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) + , mbClosed(false) +{ + *mPos = '{'; + ++mPos; + *mPos = ' '; + ++mPos; + + addValidationMark(); +} + +JsonWriter::~JsonWriter() +{ + assert(mbClosed && "forgot to extract data?"); + free(mpBuffer); +} + +ScopedJsonWriterNode JsonWriter::startNode(std::string_view pNodeName) +{ + putLiteral(pNodeName, "{ "); + + mStartNodeCount++; + mbFirstFieldInNode = true; + + return ScopedJsonWriterNode(*this); +} + +void JsonWriter::endNode() +{ + assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere"); + --mStartNodeCount; + ensureSpace(1); + *mPos = '}'; + ++mPos; + mbFirstFieldInNode = false; + + validate(); +} + +ScopedJsonWriterArray JsonWriter::startArray(std::string_view pNodeName) +{ + putLiteral(pNodeName, "[ "); + + mStartNodeCount++; + mbFirstFieldInNode = true; + + 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) +{ + // control characters + if (ch <= 0x1f) + { + int written = snprintf(pos, 7, "\\u%.4x", static_cast<unsigned int>(ch)); + if (written > 0) + pos += written; + return true; + } + + switch (ch) + { + 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) +{ + *mPos = '"'; + ++mPos; + + // 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; + } + } + + *mPos = '"'; + ++mPos; + + validate(); +} + +void JsonWriter::put(std::u16string_view pPropName, const OUString& rPropVal) +{ + auto nPropNameLength = pPropName.length(); + // 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(); + + writeEscapedOUString(OUString(pPropName)); + + memcpy(mPos, ": ", 2); + mPos += 2; + + writeEscapedOUString(rPropVal); + + validate(); +} + +void JsonWriter::put(std::string_view pPropName, const OUString& rPropVal) +{ + // 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 + 2; + ensureSpaceAndWriteNameColon(pPropName, nWorstCasePropValLength); + + writeEscapedOUString(rPropVal); +} + +void JsonWriter::put(std::string_view pPropName, std::string_view rPropVal) +{ + // escaping can double the length, plus quotes + auto nWorstCasePropValLength = rPropVal.size() * 2 + 2; + ensureSpaceAndWriteNameColon(pPropName, nWorstCasePropValLength); + + *mPos = '"'; + ++mPos; + + // copy and perform escaping + bool bReachedEnd = false; + for (size_t i = 0; i < rPropVal.size() && !bReachedEnd; ++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 0: + bReachedEnd = true; + 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(std::string_view pPropName, bool nPropVal) +{ + putLiteral(pPropName, nPropVal ? std::string_view("true") : std::string_view("false")); +} + +void JsonWriter::putSimpleValue(const OUString& rPropVal) +{ + auto nWorstCasePropValLength = rPropVal.getLength() * 6; + ensureSpace(nWorstCasePropValLength + 4); + + addCommaBeforeField(); + + writeEscapedOUString(rPropVal); +} + +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(!mbClosed && "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(); + } +} + +void JsonWriter::ensureSpaceAndWriteNameColon(std::string_view name, int valSize) +{ + // we assume property names are ascii + ensureSpace(name.size() + valSize + 6); + + addCommaBeforeField(); + + *mPos = '"'; + ++mPos; + memcpy(mPos, name.data(), name.size()); + mPos += name.size(); + memcpy(mPos, "\": ", 3); + mPos += 3; +} + +void JsonWriter::putLiteral(std::string_view propName, std::string_view propValue) +{ + ensureSpaceAndWriteNameColon(propName, propValue.size()); + memcpy(mPos, propValue.data(), propValue.size()); + mPos += propValue.size(); + + validate(); +} + +OString JsonWriter::finishAndGetAsOString() +{ + assert(mStartNodeCount == 0 && "did not close all nodes"); + assert(!mbClosed && "data already extracted"); + ensureSpace(2); + // add closing brace + *mPos = '}'; + ++mPos; + // null-terminate + *mPos = 0; + mbClosed = true; + + OString ret(mpBuffer, mPos - mpBuffer); + return ret; +} + +bool JsonWriter::isDataEquals(std::string_view s) const +{ + return std::string_view(mpBuffer, static_cast<size_t>(mPos - mpBuffer)) == s; +} + +} // 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 0000000000..706740a320 --- /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: */ |