kernel32: Avoid calling MultiByteToWideChar with invalid parameters.
[wine] / dlls / kernel32 / kernel_main.c
1 /*
2  * Kernel initialization code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <signal.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wincon.h"
33 #include "winternl.h"
34
35 #include "wine/library.h"
36 #include "kernel_private.h"
37 #include "console_private.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(process);
41
42 extern int CDECL __wine_set_signal_handler(unsigned, int (*)(unsigned));
43
44 static ULONGLONG server_start_time;
45
46 /***********************************************************************
47  *           set_entry_point
48  */
49 static void set_entry_point( HMODULE module, const char *name, DWORD rva )
50 {
51     IMAGE_EXPORT_DIRECTORY *exports;
52     DWORD exp_size;
53
54     if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
55                                                   IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
56     {
57         DWORD *functions = (DWORD *)((char *)module + exports->AddressOfFunctions);
58         const WORD *ordinals = (const WORD *)((const char *)module + exports->AddressOfNameOrdinals);
59         const DWORD *names = (const DWORD *)((const char *)module +  exports->AddressOfNames);
60         int min = 0, max = exports->NumberOfNames - 1;
61
62         while (min <= max)
63         {
64             int res, pos = (min + max) / 2;
65             const char *ename = (const char *)module + names[pos];
66             if (!(res = strcmp( ename, name )))
67             {
68                 WORD ordinal = ordinals[pos];
69                 assert( ordinal < exports->NumberOfFunctions );
70                 TRACE( "setting %s at %p to %08x\n", name, &functions[ordinal], rva );
71                 functions[ordinal] = rva;
72                 return;
73             }
74             if (res > 0) max = pos - 1;
75             else min = pos + 1;
76         }
77     }
78 }
79
80
81 /***********************************************************************
82  *           KERNEL process initialisation routine
83  */
84 static BOOL process_attach( HMODULE module )
85 {
86     SYSTEM_TIMEOFDAY_INFORMATION ti;
87     RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
88
89     NtQuerySystemInformation( SystemTimeOfDayInformation, &ti, sizeof(ti), NULL );
90     server_start_time = ti.liKeBootTime.QuadPart;
91
92     /* Setup registry locale information */
93     LOCALE_InitRegistry();
94
95     /* Setup computer name */
96     COMPUTERNAME_Init();
97
98     CONSOLE_Init(params);
99
100     /* copy process information from ntdll */
101     ENV_CopyStartupInformation();
102
103     if (!(GetVersion() & 0x80000000))
104     {
105         /* Securom checks for this one when version is NT */
106         set_entry_point( module, "FT_Thunk", 0 );
107     }
108     else LoadLibraryA( "krnl386.exe16" );
109
110     /* finish the process initialisation for console bits, if needed */
111     __wine_set_signal_handler(SIGINT, CONSOLE_HandleCtrlC);
112
113     if (params->ConsoleHandle == KERNEL32_CONSOLE_ALLOC)
114     {
115         HMODULE mod = GetModuleHandleA(0);
116         if (RtlImageNtHeader(mod)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
117             AllocConsole();
118     }
119     /* else TODO for DETACHED_PROCESS:
120      * 1/ inherit console + handles
121      * 2/ create std handles, if handles are not inherited
122      * TBD when not using wineserver handles for console handles
123      */
124
125     return TRUE;
126 }
127
128 /***********************************************************************
129  *           KERNEL initialisation routine
130  */
131 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
132 {
133     switch(reason)
134     {
135     case DLL_PROCESS_ATTACH:
136         DisableThreadLibraryCalls( hinst );
137         return process_attach( hinst );
138     case DLL_PROCESS_DETACH:
139         WritePrivateProfileSectionW( NULL, NULL, NULL );
140         CONSOLE_Exit();
141         break;
142     }
143     return TRUE;
144 }
145
146 /***********************************************************************
147  *           MulDiv   (KERNEL32.@)
148  * RETURNS
149  *      Result of multiplication and division
150  *      -1: Overflow occurred or Divisor was 0
151  */
152 INT WINAPI MulDiv( INT nMultiplicand, INT nMultiplier, INT nDivisor)
153 {
154     LONGLONG ret;
155
156     if (!nDivisor) return -1;
157
158     /* We want to deal with a positive divisor to simplify the logic. */
159     if (nDivisor < 0)
160     {
161       nMultiplicand = - nMultiplicand;
162       nDivisor = -nDivisor;
163     }
164
165     /* If the result is positive, we "add" to round. else, we subtract to round. */
166     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
167          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
168       ret = (((LONGLONG)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
169     else
170       ret = (((LONGLONG)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
171
172     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
173     return ret;
174 }
175
176
177 /******************************************************************************
178  *           GetTickCount64       (KERNEL32.@)
179  */
180 ULONGLONG WINAPI GetTickCount64(void)
181 {
182     LARGE_INTEGER now;
183
184     NtQuerySystemTime( &now );
185     return (now.QuadPart - server_start_time) / 10000;
186 }
187
188
189 /***********************************************************************
190  *           GetTickCount       (KERNEL32.@)
191  *
192  * Get the number of milliseconds the system has been running.
193  *
194  * PARAMS
195  *  None.
196  *
197  * RETURNS
198  *  The current tick count.
199  *
200  * NOTES
201  *  The value returned will wrap around every 2^32 milliseconds.
202  *  Under Windows, tick 0 is the moment at which the system is rebooted.
203  *  Under Wine, tick 0 begins at the moment the wineserver process is started.
204  */
205 DWORD WINAPI GetTickCount(void)
206 {
207     return GetTickCount64();
208 }
209
210 /******************************************************************************
211  *           GetSystemRegistryQuota       (KERNEL32.@)
212  */
213 BOOL WINAPI GetSystemRegistryQuota(PDWORD pdwQuotaAllowed, PDWORD pdwQuotaUsed)
214 {
215     FIXME("(%p, %p) faking reported quota values\n", pdwQuotaAllowed, pdwQuotaUsed);
216
217     if (pdwQuotaAllowed)
218         *pdwQuotaAllowed = 2 * 1000 * 1000 * 1000; /* 2 GB */
219
220     if (pdwQuotaUsed)
221         *pdwQuotaUsed = 100 * 1000 * 1000; /* 100 MB */
222
223     return TRUE;
224 }