[51770] | 1 | /* $Id: SUPR3HardenedNoCrt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VirtualBox Support Library - Hardened main() no-crt routines.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[51770] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[51770] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[51770] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[51770] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[51770] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[51770] | 41 | #if RT_OS_WINDOWS
|
---|
[62677] | 42 | # include <iprt/win/windows.h>
|
---|
[51770] | 43 | #endif
|
---|
| 44 |
|
---|
| 45 | #include <VBox/sup.h>
|
---|
| 46 |
|
---|
| 47 | #include "SUPLibInternal.h"
|
---|
| 48 |
|
---|
| 49 |
|
---|
[52160] | 50 | #ifdef SUP_HARDENED_NEED_CRT_FUNCTIONS /** @todo this crap is obsolete. */
|
---|
[51770] | 51 |
|
---|
| 52 | /** memcmp */
|
---|
[85127] | 53 | DECLHIDDEN(int) suplibHardenedMemComp(void const *pvDst, const void *pvSrc, size_t cbToComp)
|
---|
[51770] | 54 | {
|
---|
| 55 | size_t const *puDst = (size_t const *)pvDst;
|
---|
| 56 | size_t const *puSrc = (size_t const *)pvSrc;
|
---|
| 57 | while (cbToComp >= sizeof(size_t))
|
---|
| 58 | {
|
---|
| 59 | if (*puDst != *puSrc)
|
---|
| 60 | break;
|
---|
| 61 | puDst++;
|
---|
| 62 | puSrc++;
|
---|
| 63 | cbToComp -= sizeof(size_t);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | uint8_t const *pbDst = (uint8_t const *)puDst;
|
---|
| 67 | uint8_t const *pbSrc = (uint8_t const *)puSrc;
|
---|
| 68 | while (cbToComp > 0)
|
---|
| 69 | {
|
---|
| 70 | if (*pbDst != *pbSrc)
|
---|
| 71 | {
|
---|
| 72 | if (*pbDst < *pbSrc)
|
---|
| 73 | return -1;
|
---|
| 74 | return 1;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | pbDst++;
|
---|
| 78 | pbSrc++;
|
---|
| 79 | cbToComp--;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | return 0;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | /** memcpy */
|
---|
[85127] | 87 | DECLHIDDEN(void *) suplibHardenedMemCopy(void *pvDst, const void *pvSrc, size_t cbToCopy)
|
---|
[51770] | 88 | {
|
---|
| 89 | size_t *puDst = (size_t *)pvDst;
|
---|
| 90 | size_t const *puSrc = (size_t const *)pvSrc;
|
---|
| 91 | while (cbToCopy >= sizeof(size_t))
|
---|
| 92 | {
|
---|
| 93 | *puDst++ = *puSrc++;
|
---|
| 94 | cbToCopy -= sizeof(size_t);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | uint8_t *pbDst = (uint8_t *)puDst;
|
---|
| 98 | uint8_t const *pbSrc = (uint8_t const *)puSrc;
|
---|
| 99 | while (cbToCopy > 0)
|
---|
| 100 | {
|
---|
| 101 | *pbDst++ = *pbSrc++;
|
---|
| 102 | cbToCopy--;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | return pvDst;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | /** memset */
|
---|
[85127] | 110 | DECLHIDDEN(void *) suplibHardenedMemSet(void *pvDst, int ch, size_t cbToSet)
|
---|
[51770] | 111 | {
|
---|
| 112 | uint8_t *pbDst = (uint8_t *)pvDst;
|
---|
| 113 | while (cbToSet > 0)
|
---|
| 114 | {
|
---|
| 115 | *pbDst++ = (uint8_t)ch;
|
---|
| 116 | cbToSet--;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return pvDst;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /** strcpy */
|
---|
[85127] | 124 | DECLHIDDEN(char *) suplibHardenedStrCopy(char *pszDst, const char *pszSrc)
|
---|
[51770] | 125 | {
|
---|
| 126 | char *pszRet = pszDst;
|
---|
| 127 | char ch;
|
---|
| 128 | do
|
---|
| 129 | {
|
---|
| 130 | ch = *pszSrc++;
|
---|
| 131 | *pszDst++ = ch;
|
---|
| 132 | } while (ch);
|
---|
| 133 | return pszRet;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | /** strlen */
|
---|
[85127] | 138 | DECLHIDDEN(size_t) suplibHardenedStrLen(const char *psz)
|
---|
[51770] | 139 | {
|
---|
| 140 | const char *pszStart = psz;
|
---|
| 141 | while (*psz)
|
---|
| 142 | psz++;
|
---|
| 143 | return psz - pszStart;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | /** strcat */
|
---|
[85127] | 148 | DECLHIDDEN(char *) suplibHardenedStrCat(char *pszDst, const char *pszSrc)
|
---|
[51770] | 149 | {
|
---|
| 150 | char *pszRet = pszDst;
|
---|
| 151 | while (*pszDst)
|
---|
| 152 | pszDst++;
|
---|
| 153 | suplibHardenedStrCopy(pszDst, pszSrc);
|
---|
| 154 | return pszRet;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /** strcmp */
|
---|
[85127] | 159 | DECLHIDDEN(int) suplibHardenedStrCmp(const char *psz1, const char *psz2)
|
---|
[51770] | 160 | {
|
---|
| 161 | for (;;)
|
---|
| 162 | {
|
---|
| 163 | char ch1 = *psz1++;
|
---|
| 164 | char ch2 = *psz2++;
|
---|
| 165 | if (ch1 != ch2)
|
---|
| 166 | return ch1 < ch2 ? -1 : 1;
|
---|
| 167 | if (ch1 == 0)
|
---|
| 168 | return 0;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | /** strncmp */
|
---|
[85127] | 174 | DECLHIDDEN(int) suplibHardenedStrNCmp(const char *psz1, const char *psz2, size_t cchMax)
|
---|
[51770] | 175 | {
|
---|
| 176 | while (cchMax-- > 0)
|
---|
| 177 | {
|
---|
| 178 | char ch1 = *psz1++;
|
---|
| 179 | char ch2 = *psz2++;
|
---|
| 180 | if (ch1 != ch2)
|
---|
| 181 | return ch1 < ch2 ? -1 : 1;
|
---|
| 182 | if (ch1 == 0)
|
---|
| 183 | break;
|
---|
| 184 | }
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | #endif /* SUP_HARDENED_NEED_CRT_FUNCTIONS */
|
---|
| 189 |
|
---|