Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions DesktopEditor/common/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,25 +1591,31 @@ namespace NSFile
std::wstring CFileBinary::CreateTempFileWithUniqueName(const std::wstring& strFolderPathRoot, const std::wstring& Prefix)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
wchar_t pBuffer[MAX_PATH + 1];
::GetTempFileNameW(strFolderPathRoot.c_str(), Prefix.c_str(), 0, pBuffer);
std::wstring sRet(pBuffer);
return sRet;
wchar_t pBuffer[MAX_PATH + 1] = {0};
if (0 == ::GetTempFileNameW(strFolderPathRoot.c_str(), Prefix.c_str(), 0, pBuffer))
return std::wstring();
return std::wstring(pBuffer);
#else
char pcRes[MAX_PATH];
BYTE* pData = (BYTE*)pcRes;

std::wstring sPrefix = strFolderPathRoot + L"/" + Prefix + L"_XXXXXX";

LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sPrefix.c_str(), (LONG)sPrefix.length(), pData, lLen);
pcRes[lLen] = '\0';
BYTE* pcRes = NULL;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sPrefix.c_str(), (LONG)sPrefix.length(), pcRes, lLen);

int res = mkstemp(pcRes);
if (lLen <= 0)
{
delete[] pcRes;
return std::wstring();
}

int res = mkstemp((char*)pcRes);
if (-1 != res)
close(res);

std::string sRes = pcRes;
return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), sRes.length());
std::wstring sResult = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pcRes, (LONG)lLen);

delete[] pcRes;
return sResult;
#endif
}
bool CFileBinary::OpenTempFile(std::wstring *pwsName, FILE **ppFile, wchar_t *wsMode, wchar_t *wsExt, wchar_t *wsFolder, wchar_t* /*wsName*/)
Expand Down
2 changes: 1 addition & 1 deletion MsBinaryFile/XlsFile/Converter/XlsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ void XlsConverter::convert(XLS::Obj * obj)
std::wstring objectId_bin = xlsx_context->get_mediaitems().add_control_activeX(target_bin);

NSFile::CFileBinary file;
if(xls_global_info->controls_data.second >= obj->pictFmla.lPosInCtlStm + obj->pictFmla.cbBufInCtlStm)
if (obj->pictFmla.lPosInCtlStm <= xls_global_info->controls_data.second && obj->pictFmla.cbBufInCtlStm <= xls_global_info->controls_data.second - obj->pictFmla.lPosInCtlStm)
{
if ( file.CreateFileW(xlsx_context->get_mediaitems().activeX_path() + target_bin) )
{
Expand Down
3 changes: 2 additions & 1 deletion OFDFile/OFDFile.pro
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ PWD_ROOT_DIR = $$PWD
include($$CORE_ROOT_DIR/Common/base.pri)
include($$CORE_ROOT_DIR/Common/3dParty/boost/boost.pri)

DEFINES += OFD_USE_DYNAMIC_LIBRARY
DEFINES += OFD_USE_DYNAMIC_LIBRARY \
CRYPTOPP_DISABLE_ASM

ADD_DEPENDENCY(graphics, kernel, UnicodeConverter, PdfFile)

Expand Down
200 changes: 200 additions & 0 deletions OOXML/Base/Rand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (C) Ascensio System SIA, 2009-2026
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
*
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
* No trademark rights are granted under this License.
*
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/

#ifndef SECURE_RANDOM_BYTES_H
#define SECURE_RANDOM_BYTES_H

#include <random>
#include <mutex>
#include <cstdio>
#include <cstring>
#include <cstddef>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>

#if defined(WIN_XP_OR_VISTA)

// RtlGenRandom (a.k.a. SystemFunction036 in advapi32.dll).
// Available since Windows XP, cryptographically secure,
// does not open key containers — stable under load.
extern "C" BOOLEAN NTAPI SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength);
#pragma comment(lib, "advapi32.lib")

#else

#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
#endif

#elif defined(_MAC)

#include <Security/Security.h>

#elif defined(_LINUX)

#include <unistd.h>
#include <errno.h>
#include <sys/syscall.h>

// Fallback syscall numbers for systems where SYS_getrandom is not in headers.
// getrandom() syscall added in Linux 3.17 (October 2014).

#ifndef SYS_getrandom

#if defined(__x86_64__)
#define SYS_getrandom 318
#elif defined(__i386__)
#define SYS_getrandom 355
#elif defined(__aarch64__)
#define SYS_getrandom 278
#elif defined(__arm__)
#define SYS_getrandom 384
#elif defined(__powerpc64__)
#define SYS_getrandom 359
#elif defined(__riscv)
#define SYS_getrandom 278
#endif

#endif

#endif


// Fills `buf` with `len` cryptographically secure random bytes.
// Returns true on success, false if every available source failed.
static bool SecureRandomBytes(unsigned char* buf, size_t len)
{
if (len == 0)
return true;
if (buf == nullptr)
return false;

size_t filled = 0;

#if defined(_WIN32) || defined(_WIN64)

#if defined(WIN_XP_OR_VISTA)
while (filled < len)
{
ULONG chunk = (len - filled > 0xFFFFFF00u) ? 0xFFFFFF00u : static_cast<ULONG>(len - filled);
if (!::SystemFunction036(buf + filled, chunk))
break;
filled += chunk;
}
if (filled == len)
return true;
#else
while (filled < len)
{
ULONG chunk = (len - filled > 0xFFFFFF00u) ? 0xFFFFFF00u : static_cast<ULONG>(len - filled);
NTSTATUS status = ::BCryptGenRandom(nullptr, buf + filled, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (status != 0) // STATUS_SUCCESS
break;
filled += chunk;
}
if (filled == len)
return true;
#endif

#elif defined(_MAC)
if (::SecRandomCopyBytes(kSecRandomDefault, len, buf) == errSecSuccess)
return true;

#elif defined(__ANDROID__) || defined(__linux__)
// ===== Linux / Android =====
// Direct syscall — does not depend on glibc version or headers.
// On kernels older than 3.17 syscall returns ENOSYS and we fall through.
#ifdef SYS_getrandom
while (filled < len) {
size_t chunk = (len - filled > 256) ? 256 : (len - filled);
long ret = ::syscall(SYS_getrandom, buf + filled, chunk, 0);
if (ret < 0) {
if (errno == EINTR) continue;
break; // ENOSYS or other error — fall through to /dev/urandom
}
filled += static_cast<size_t>(ret);
}
if (filled == len) return true;
#endif
#endif

// POSIX fallback: /dev/urandom
#if !defined(_WIN32) && !defined(_WIN64)
{
FILE* f = std::fopen("/dev/urandom", "rb");
if (f) {
while (filled < len) {
size_t n = std::fread(buf + filled, 1, len - filled, f);
if (n == 0) break;
filled += n;
}
std::fclose(f);
if (filled == len) return true;
}
}
#endif

// Final fallback: std::random_device
try
{
static std::mutex m;
static std::random_device rd;
std::lock_guard<std::mutex> lock(m);
while (filled < len)
{
unsigned int r = rd();
size_t to_copy = (len - filled < sizeof(r)) ? (len - filled) : sizeof(r);
std::memcpy(buf + filled, &r, to_copy);
filled += to_copy;
}
return true;
}
catch (...) {
return false;
}

return false;
}

// Returns a cryptographically secure random unsigned int.
static unsigned int RandUInt()
{
unsigned int result = 0;
SecureRandomBytes(reinterpret_cast<unsigned char*>(&result), sizeof(result));
return result;
}

#endif // SECURE_RANDOM_BYTES_H
66 changes: 28 additions & 38 deletions OOXML/Base/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "Unit.h"
#include <cwchar>


double Cm_To_Mm (const double &dValue)
{
return dValue * 10;
Expand Down Expand Up @@ -759,43 +758,6 @@ namespace XmlUtils
return sstream.str();
}

int Rand()
{
//rand returns integral value range between 0 and RAND_MAX.(RAND_MAX at least 32767.)
static bool init = false; /* ensure different random header each time */
if (!init)
{
init = true;
srand((unsigned int) time(nullptr));
}
return std::rand();
}
int GenerateInt()
{
//todo c++11 <random>
return ((Rand() & 0x7FFF) | ((Rand() & 0x7FFF) << 15) | ((Rand() & 0x3) << 30));
}

std::wstring GenerateGuid()
{
std::wstring result;
//#if defined (_WIN32) || defined(_WIN64)
// GUID guid;
// CoCreateGuid(&guid);
//
// OLECHAR* guidString;
// StringFromCLSID(guid, &guidString);
//
// result = std::wstring(guidString);
//
// CoTaskMemFree(guidString);
//#else
std::wstringstream sstream;
sstream << boost::wformat(L"%04X%04X-%04X-%04X-%04X-%04X%04X%04X") % (Rand() & 0xff) % (Rand() & 0xff) % (Rand() & 0xff) % ((Rand() & 0x0fff) | 0x4000) % ((Rand() % 0x3fff) + 0x8000) % (Rand() & 0xff) % (Rand() & 0xff) % (Rand() & 0xff);
result = sstream.str();
//#endif
return result;
}
std::wstring DoubleToString( double value, wchar_t* format )
{
if ( format == nullptr ) return L"";
Expand Down Expand Up @@ -1056,3 +1018,31 @@ namespace XmlUtils
return buffer;
}
}

#include "./Rand.h"
namespace XmlUtils
{
int GenerateInt()
{
return static_cast<int>(RandUInt());
}

std::wstring GenerateGuid()
{
std::wstring result;

std::wstringstream sstream;
sstream << boost::wformat(L"%04X%04X-%04X-%04X-%04X-%04X%04X%04X")
% (RandUInt() & 0xffff)
% (RandUInt() & 0xffff)
% (RandUInt() & 0xffff)
% ((RandUInt() & 0x0fff) | 0x4000)
% ((RandUInt() & 0x3fff) | 0x8000)
% (RandUInt() & 0xffff)
% (RandUInt() & 0xffff)
% (RandUInt() & 0xffff);
result = sstream.str();

return result;
}
}
6 changes: 5 additions & 1 deletion OOXML/Base/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/

/*
* SPDX-FileCopyrightText: Euro-Office contributors
* Modified by Euro-Office contributors, see MODIFICATIONS.txt
*/
#pragma once

#include "Base.h"
Expand Down Expand Up @@ -172,7 +177,6 @@ namespace XmlUtils
std::wstring ToString(double value, const wchar_t* format);
std::string ToString(double value, const char* format);

int Rand();
int GenerateInt();

std::wstring GenerateGuid();
Expand Down
3 changes: 2 additions & 1 deletion OOXML/Projects/Linux/DocxFormatLib/DocxFormatLib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ CONFIG += core_x2t
include(../../../../Common/base.pri)

DEFINES += UNICODE _UNICODE \
DONT_WRITE_EMBEDDED_FONTS
DONT_WRITE_EMBEDDED_FONTS \
CRYPTOPP_DISABLE_ASM

#BOOST
include($$PWD/../../../../Common/3dParty/boost/boost.pri)
Expand Down
2 changes: 1 addition & 1 deletion OOXML/XlsxFormat/Comments/ThreadedComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace OOX
writer.WriteString(L"<person");
WritingStringNullableAttrEncodeXmlString(L"displayName", displayName, *displayName);
WritingStringNullableAttrString(L"id", id, *id);
WritingStringNullableAttrString(L"userId", userId, *userId);
WritingStringNullableAttrEncodeXmlString(L"userId", userId, *userId);
WritingStringNullableAttrEncodeXmlString(L"providerId", providerId, *providerId);
writer.WriteString(L"/>");
}
Expand Down
6 changes: 6 additions & 0 deletions OdfFile/Reader/Converter/StarMath2OOXML/StarMath2OOXML.pro
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ HEADERS += \
$$PWD/typeConversion.h \
$$PWD/typeselements.h \
$$PWD/TFormulaSize.h

core_mac | core_ios {
LIBS += -framework Security
}


Loading
Loading