1 | /* $Id: SUPR3HardenedNoCrt.cpp 51770 2014-07-01 18:14:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - Hardened main() no-crt routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #if RT_OS_WINDOWS
|
---|
31 | # include <Windows.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/sup.h>
|
---|
35 |
|
---|
36 | #include "SUPLibInternal.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | #ifdef SUP_HARDENED_NEED_CRT_FUNCTIONS
|
---|
40 |
|
---|
41 | /** memcmp */
|
---|
42 | DECLHIDDEN(int) suplibHardenedMemComp(void const *pvDst, const void *pvSrc, size_t cbToComp)
|
---|
43 | {
|
---|
44 | size_t const *puDst = (size_t const *)pvDst;
|
---|
45 | size_t const *puSrc = (size_t const *)pvSrc;
|
---|
46 | while (cbToComp >= sizeof(size_t))
|
---|
47 | {
|
---|
48 | if (*puDst != *puSrc)
|
---|
49 | break;
|
---|
50 | puDst++;
|
---|
51 | puSrc++;
|
---|
52 | cbToComp -= sizeof(size_t);
|
---|
53 | }
|
---|
54 |
|
---|
55 | uint8_t const *pbDst = (uint8_t const *)puDst;
|
---|
56 | uint8_t const *pbSrc = (uint8_t const *)puSrc;
|
---|
57 | while (cbToComp > 0)
|
---|
58 | {
|
---|
59 | if (*pbDst != *pbSrc)
|
---|
60 | {
|
---|
61 | if (*pbDst < *pbSrc)
|
---|
62 | return -1;
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | pbDst++;
|
---|
67 | pbSrc++;
|
---|
68 | cbToComp--;
|
---|
69 | }
|
---|
70 |
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /** memcpy */
|
---|
76 | DECLHIDDEN(void *) suplibHardenedMemCopy(void *pvDst, const void *pvSrc, size_t cbToCopy)
|
---|
77 | {
|
---|
78 | size_t *puDst = (size_t *)pvDst;
|
---|
79 | size_t const *puSrc = (size_t const *)pvSrc;
|
---|
80 | while (cbToCopy >= sizeof(size_t))
|
---|
81 | {
|
---|
82 | *puDst++ = *puSrc++;
|
---|
83 | cbToCopy -= sizeof(size_t);
|
---|
84 | }
|
---|
85 |
|
---|
86 | uint8_t *pbDst = (uint8_t *)puDst;
|
---|
87 | uint8_t const *pbSrc = (uint8_t const *)puSrc;
|
---|
88 | while (cbToCopy > 0)
|
---|
89 | {
|
---|
90 | *pbDst++ = *pbSrc++;
|
---|
91 | cbToCopy--;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return pvDst;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /** memset */
|
---|
99 | DECLHIDDEN(void *) suplibHardenedMemSet(void *pvDst, int ch, size_t cbToSet)
|
---|
100 | {
|
---|
101 | uint8_t *pbDst = (uint8_t *)pvDst;
|
---|
102 | while (cbToSet > 0)
|
---|
103 | {
|
---|
104 | *pbDst++ = (uint8_t)ch;
|
---|
105 | cbToSet--;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return pvDst;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /** strcpy */
|
---|
113 | DECLHIDDEN(char *) suplibHardenedStrCopy(char *pszDst, const char *pszSrc)
|
---|
114 | {
|
---|
115 | char *pszRet = pszDst;
|
---|
116 | char ch;
|
---|
117 | do
|
---|
118 | {
|
---|
119 | ch = *pszSrc++;
|
---|
120 | *pszDst++ = ch;
|
---|
121 | } while (ch);
|
---|
122 | return pszRet;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /** strlen */
|
---|
127 | DECLHIDDEN(size_t) suplibHardenedStrLen(const char *psz)
|
---|
128 | {
|
---|
129 | const char *pszStart = psz;
|
---|
130 | while (*psz)
|
---|
131 | psz++;
|
---|
132 | return psz - pszStart;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /** strcat */
|
---|
137 | DECLHIDDEN(char *) suplibHardenedStrCat(char *pszDst, const char *pszSrc)
|
---|
138 | {
|
---|
139 | char *pszRet = pszDst;
|
---|
140 | while (*pszDst)
|
---|
141 | pszDst++;
|
---|
142 | suplibHardenedStrCopy(pszDst, pszSrc);
|
---|
143 | return pszRet;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | # ifdef RT_OS_WINDOWS
|
---|
148 | /** stricmp */
|
---|
149 | DECLHIDDEN(int) suplibHardenedStrICmp(const char *psz1, const char *psz2)
|
---|
150 | {
|
---|
151 | const char *pszOrg1 = psz1;
|
---|
152 | const char *pszOrg2 = psz2;
|
---|
153 |
|
---|
154 | for (;;)
|
---|
155 | {
|
---|
156 | char ch1 = *psz1++;
|
---|
157 | char ch2 = *psz2++;
|
---|
158 | if (ch1 != ch2)
|
---|
159 | {
|
---|
160 | int rc = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, pszOrg1, -1, pszOrg2, -1);
|
---|
161 | # ifdef VBOX_STRICT
|
---|
162 | if (rc == 0)
|
---|
163 | __debugbreak();
|
---|
164 | # endif
|
---|
165 | return rc - 2;
|
---|
166 | }
|
---|
167 | if (ch1 == 0)
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | # endif
|
---|
172 |
|
---|
173 |
|
---|
174 | /** strcmp */
|
---|
175 | DECLHIDDEN(int) suplibHardenedStrCmp(const char *psz1, const char *psz2)
|
---|
176 | {
|
---|
177 | for (;;)
|
---|
178 | {
|
---|
179 | char ch1 = *psz1++;
|
---|
180 | char ch2 = *psz2++;
|
---|
181 | if (ch1 != ch2)
|
---|
182 | return ch1 < ch2 ? -1 : 1;
|
---|
183 | if (ch1 == 0)
|
---|
184 | return 0;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /** strncmp */
|
---|
190 | DECLHIDDEN(int) suplibHardenedStrNCmp(const char *psz1, const char *psz2, size_t cchMax)
|
---|
191 | {
|
---|
192 | while (cchMax-- > 0)
|
---|
193 | {
|
---|
194 | char ch1 = *psz1++;
|
---|
195 | char ch2 = *psz2++;
|
---|
196 | if (ch1 != ch2)
|
---|
197 | return ch1 < ch2 ? -1 : 1;
|
---|
198 | if (ch1 == 0)
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | return 0;
|
---|
202 | }
|
---|
203 |
|
---|
204 | #endif /* SUP_HARDENED_NEED_CRT_FUNCTIONS */
|
---|
205 |
|
---|