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