Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / setupapi / infparse.c
1 /*
2  * SetupX .inf file parsing functions
3  *
4  * Copyright 2000 Andreas Mohr for CodeWeavers
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * FIXME:
21  * - return values ???
22  * - this should be reimplemented at some point to have its own
23  *   file parsing instead of using profile functions,
24  *   as some SETUPX exports probably demand that
25  *   (IpSaveRestorePosition, IpFindNextMatchLine, ...).
26  */
27
28 #include <stdarg.h>
29 #include <string.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "winnls.h"
38 #include "setupapi.h"
39 #include "setupapi_private.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
43
44 #define MAX_HANDLES 16384
45 #define FIRST_HANDLE 32
46
47 static HINF handles[MAX_HANDLES];
48
49
50 static RETERR16 alloc_hinf16( HINF hinf, HINF16 *hinf16 )
51 {
52     int i;
53     for (i = 0; i < MAX_HANDLES; i++)
54     {
55         if (!handles[i])
56         {
57             handles[i] = hinf;
58             *hinf16 = i + FIRST_HANDLE;
59             return OK;
60         }
61     }
62     return ERR_IP_OUT_OF_HANDLES;
63 }
64
65 static HINF get_hinf( HINF16 hinf16 )
66 {
67     int idx = hinf16 - FIRST_HANDLE;
68     if (idx < 0 || idx >= MAX_HANDLES) return 0;
69     return handles[idx];
70 }
71
72
73 static HINF free_hinf16( HINF16 hinf16 )
74 {
75     HINF ret;
76     int idx = hinf16 - FIRST_HANDLE;
77
78     if (idx < 0 || idx >= MAX_HANDLES) return 0;
79     ret = handles[idx];
80     handles[idx] = 0;
81     return ret;
82 }
83
84 /* convert last error code to a RETERR16 value */
85 static RETERR16 get_last_error(void)
86 {
87     switch(GetLastError())
88     {
89     case ERROR_EXPECTED_SECTION_NAME:
90     case ERROR_BAD_SECTION_NAME_LINE:
91     case ERROR_SECTION_NAME_TOO_LONG: return ERR_IP_INVALID_SECT_NAME;
92     case ERROR_SECTION_NOT_FOUND: return ERR_IP_SECT_NOT_FOUND;
93     case ERROR_LINE_NOT_FOUND: return ERR_IP_LINE_NOT_FOUND;
94     default: return IP_ERROR;  /* FIXME */
95     }
96 }
97
98
99 /***********************************************************************
100  *              IpOpen (SETUPX.2)
101  *
102  */
103 RETERR16 WINAPI IpOpen16( LPCSTR filename, HINF16 *hinf16 )
104 {
105     HINF hinf = SetupOpenInfFileA( filename, NULL, INF_STYLE_WIN4, NULL );
106     if (hinf == (HINF)INVALID_HANDLE_VALUE) return get_last_error();
107     return alloc_hinf16( hinf, hinf16 );
108 }
109
110
111 /***********************************************************************
112  *              IpClose (SETUPX.4)
113  */
114 RETERR16 WINAPI IpClose16( HINF16 hinf16 )
115 {
116     HINF hinf = free_hinf16( hinf16 );
117     if (!hinf) return ERR_IP_INVALID_HINF;
118     SetupCloseInfFile( hinf );
119     return OK;
120 }
121
122
123 /***********************************************************************
124  *              IpGetProfileString (SETUPX.210)
125  */
126 RETERR16 WINAPI IpGetProfileString16( HINF16 hinf16, LPCSTR section, LPCSTR entry,
127                                       LPSTR buffer, WORD buflen )
128 {
129     DWORD required_size;
130     HINF hinf = get_hinf( hinf16 );
131
132     if (!hinf) return ERR_IP_INVALID_HINF;
133     if (!SetupGetLineTextA( NULL, hinf, section, entry, buffer, buflen, &required_size ))
134         return get_last_error();
135     TRACE("%p: section %s entry %s ret %s\n",
136           hinf, debugstr_a(section), debugstr_a(entry), debugstr_a(buffer) );
137     return OK;
138 }
139
140
141 /***********************************************************************
142  *              GenFormStrWithoutPlaceHolders (SETUPX.103)
143  *
144  * ought to be pretty much implemented, I guess...
145  */
146 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR dst, LPCSTR src, HINF16 hinf16 )
147 {
148     UNICODE_STRING srcW;
149     HINF hinf = get_hinf( hinf16 );
150
151     if (!hinf) return;
152
153     if (!RtlCreateUnicodeStringFromAsciiz( &srcW, src )) return;
154     PARSER_string_substA( hinf, srcW.Buffer, dst, MAX_INF_STRING_LENGTH );
155     RtlFreeUnicodeString( &srcW );
156     TRACE( "%s -> %s\n", debugstr_a(src), debugstr_a(dst) );
157 }
158
159 /***********************************************************************
160  *              GenInstall (SETUPX.101)
161  *
162  * generic installer function for .INF file sections
163  *
164  * This is not perfect - patch whenever you can !
165  *
166  * wFlags == GENINSTALL_DO_xxx
167  * e.g. NetMeeting:
168  * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
169  * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
170  */
171 RETERR16 WINAPI GenInstall16( HINF16 hinf16, LPCSTR section, WORD genflags )
172 {
173     UINT flags = 0;
174     HINF hinf = get_hinf( hinf16 );
175     RETERR16 ret = OK;
176     void *context;
177
178     if (!hinf) return ERR_IP_INVALID_HINF;
179
180     if (genflags & GENINSTALL_DO_FILES) flags |= SPINST_FILES;
181     if (genflags & GENINSTALL_DO_INI) flags |= SPINST_INIFILES;
182     if (genflags & GENINSTALL_DO_REG) flags |= SPINST_REGISTRY;
183     if (genflags & GENINSTALL_DO_INI2REG) flags |= SPINST_INI2REG;
184     if (genflags & GENINSTALL_DO_LOGCONFIG) flags |= SPINST_LOGCONFIG;
185     if (genflags & GENINSTALL_DO_REGSRCPATH) FIXME( "unsupported flag: GENINSTALL_DO_REGSRCPATH\n" );
186     if (genflags & GENINSTALL_DO_CFGAUTO) FIXME( "unsupported flag: GENINSTALL_DO_CFGAUTO\n" );
187     if (genflags & GENINSTALL_DO_PERUSER) FIXME( "unsupported flag: GENINSTALL_DO_PERUSER\n" );
188
189     context = SetupInitDefaultQueueCallback( 0 );
190     if (!SetupInstallFromInfSectionA( 0, hinf, section, flags, 0, NULL,
191                                       SP_COPY_NEWER_OR_SAME, SetupDefaultQueueCallbackA,
192                                       context, 0, 0 ))
193         ret = get_last_error();
194
195     SetupTermDefaultQueueCallback( context );
196     return ret;
197 }