Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / netapi32 / netapi32.c
1 /*
2  * Copyright 2001 Mike McCormack
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include <string.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/debug.h"
35 #include "winerror.h"
36 #include "nb30.h"
37 #include "lm.h"
38 #include "iphlpapi.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
41
42 HMODULE NETAPI32_hModule = 0;
43
44 static UCHAR NETBIOS_Enum(PNCB ncb)
45 {
46     int             i;
47     LANA_ENUM *lanas = (PLANA_ENUM) ncb->ncb_buffer;
48     DWORD apiReturn, size = 0;
49     PMIB_IFTABLE table;
50     UCHAR ret;
51  
52     TRACE("NCBENUM\n");
53  
54     apiReturn = GetIfTable(NULL, &size, FALSE);
55     if (apiReturn != NO_ERROR)
56       {
57         table = (PMIB_IFTABLE)malloc(size);
58         if (table)
59           {
60             apiReturn = GetIfTable(table, &size, FALSE);
61             if (apiReturn == NO_ERROR)
62               {
63                 lanas->length = 0;
64                 for (i = 0; i < table->dwNumEntries && lanas->length < MAX_LANA;
65                  i++)
66                   {
67                     if (table->table[i].dwType != MIB_IF_TYPE_LOOPBACK)
68                       {
69                         lanas->lana[lanas->length] = table->table[i].dwIndex;
70                         lanas->length++;
71                       }
72                   }
73                 ret = NRC_GOODRET;
74               }
75             else
76                 ret = NRC_SYSTEM;
77             free(table);
78           }
79         else
80             ret = NRC_NORESOURCES;
81       }
82     else
83         ret = NRC_SYSTEM;
84     return ret;
85 }
86
87
88 static UCHAR NETBIOS_Astat(PNCB ncb)
89 {
90     PADAPTER_STATUS astat = (PADAPTER_STATUS) ncb->ncb_buffer;
91     MIB_IFROW row;
92   
93     TRACE("NCBASTAT (Adapter %d)\n", ncb->ncb_lana_num);
94   
95     memset(astat, 0, sizeof astat);
96   
97     row.dwIndex = ncb->ncb_lana_num;
98     if (GetIfEntry(&row) != NO_ERROR)
99         return NRC_INVADDRESS;
100     /* doubt anyone cares, but why not.. */
101     if (row.dwType == MIB_IF_TYPE_TOKENRING)
102         astat->adapter_type = 0xff;
103     else
104         astat->adapter_type = 0xfe; /* for Ethernet */
105     return NRC_GOODRET;
106 }
107
108 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
109 {
110     TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
111
112     switch (fdwReason) {
113         case DLL_PROCESS_ATTACH:
114             DisableThreadLibraryCalls(hinstDLL);
115             NETAPI32_hModule = hinstDLL;
116             break;
117         case DLL_PROCESS_DETACH:
118             break;
119     }
120
121     return TRUE;
122 }
123
124 UCHAR WINAPI Netbios(PNCB pncb)
125 {
126     UCHAR ret = NRC_ILLCMD;
127
128     TRACE("ncb = %p\n",pncb);
129
130     if(!pncb)
131         return NRC_INVADDRESS;
132
133     switch(pncb->ncb_command&0x7f)
134     {
135     case NCBRESET:
136         FIXME("NCBRESET adapter %d\n",pncb->ncb_lana_num);
137         if(pncb->ncb_lana_num < MAX_LANA )
138           {
139             MIB_IFROW row;
140
141             row.dwIndex = pncb->ncb_lana_num;
142             if (GetIfEntry(&row) != NO_ERROR)
143                 ret = NRC_GOODRET;
144             else
145                 ret = NRC_ILLCMD; /* NetBIOS emulator not found */
146           }
147         else
148             ret = NRC_ILLCMD; /* NetBIOS emulator not found */
149         break;
150
151     case NCBADDNAME:
152         FIXME("NCBADDNAME\n");
153         break;
154
155     case NCBADDGRNAME:
156         FIXME("NCBADDGRNAME\n");
157         break;
158
159     case NCBDELNAME:
160         FIXME("NCBDELNAME\n");
161         break;
162
163     case NCBSEND:
164         FIXME("NCBSEND\n");
165         break;
166
167     case NCBRECV:
168         FIXME("NCBRECV\n");
169         break;
170
171     case NCBHANGUP:
172         FIXME("NCBHANGUP\n");
173         break;
174
175     case NCBCANCEL:
176         FIXME("NCBCANCEL\n");
177         break;
178
179     case NCBLISTEN:
180         FIXME("NCBLISTEN\n");
181         break;
182
183     case NCBASTAT:
184         ret = NETBIOS_Astat(pncb);
185         break;
186
187     case NCBENUM:
188         ret = NETBIOS_Enum(pncb);
189         break;
190
191     default:
192         FIXME("(%p): command code %02x\n", pncb, pncb->ncb_command);
193
194         ret = NRC_ILLCMD; /* NetBIOS emulator not found */
195     }
196     pncb->ncb_retcode = ret;
197     return ret;
198 }
199
200 NET_API_STATUS  WINAPI NetServerEnum(
201   LPCWSTR servername,
202   DWORD level,
203   LPBYTE* bufptr,
204   DWORD prefmaxlen,
205   LPDWORD entriesread,
206   LPDWORD totalentries,
207   DWORD servertype,
208   LPCWSTR domain,
209   LPDWORD resume_handle
210 )
211 {
212     FIXME("Stub (%p, %ld %p %ld %p %p %ld %s %p)\n",servername, level, bufptr,
213           prefmaxlen, entriesread, totalentries, servertype, debugstr_w(domain), resume_handle);
214
215     return ERROR_NO_BROWSER_SERVERS_FOUND;
216 }
217
218
219 /************************************************************
220  *                NetStatisticsGet  (NETAPI32.@)
221  */
222 NET_API_STATUS WINAPI NetStatisticsGet(LPWSTR *server, LPWSTR *service,
223                                        DWORD level, DWORD options,
224                                        LPBYTE *bufptr)
225 {
226     TRACE("(%p, %p, %ld, %ld, %p)\n", server, service, level, options, bufptr);
227     return NERR_InternalError;
228 }