Added DebugBreak.
[wine] / dlls / version / resource.c
1 /* 
2  * Implementation of VERSION.DLL - Resource Access routines
3  * 
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  * Copyright 1999 Ulrich Weigand
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "peexe.h"
13 #include "neexe.h"
14 #include "module.h"
15 #include "winver.h"
16 #include "heap.h"
17 #include "lzexpand.h"
18 #include "peexe.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(ver)
22
23
24 /***********************************************************************
25  *           read_xx_header         [internal]
26  */
27 static int read_xx_header( HFILE lzfd )
28 {
29     IMAGE_DOS_HEADER mzh;
30     char magic[3];
31
32     LZSeek( lzfd, 0, SEEK_SET );
33     if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
34         return 0;
35     if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
36         return 0;
37
38     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
39     if ( 2 != LZRead( lzfd, magic, 2 ) )
40         return 0;
41
42     LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
43
44     if ( magic[0] == 'N' && magic[1] == 'E' )
45         return IMAGE_OS2_SIGNATURE;
46     if ( magic[0] == 'P' && magic[1] == 'E' )
47         return IMAGE_NT_SIGNATURE;
48
49     magic[2] = '\0';
50     WARN("Can't handle %s files.\n", magic );
51     return 0;
52 }
53
54 /***********************************************************************
55  *           load_ne_resource         [internal]
56  */
57 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
58                                 DWORD *resLen, DWORD *resOff )
59 {
60     IMAGE_OS2_HEADER nehd;
61     NE_TYPEINFO *typeInfo;
62     NE_NAMEINFO *nameInfo;
63     DWORD nehdoffset;
64     LPBYTE resTab;
65     DWORD resTabSize;
66
67     /* Read in NE header */ 
68     nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
69     if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
70
71     resTabSize = nehd.rname_tab_offset - nehd.resource_tab_offset; 
72     if ( !resTabSize )
73     {
74         TRACE("No resources in NE dll\n" );
75         return FALSE;
76     }
77
78     /* Read in resource table */ 
79     resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
80     if ( !resTab ) return FALSE;
81
82     LZSeek( lzfd, nehd.resource_tab_offset + nehdoffset, SEEK_SET );
83     if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
84     {
85         HeapFree( GetProcessHeap(), 0, resTab );
86         return FALSE;
87     }
88
89     /* Find resource */
90     typeInfo = (NE_TYPEINFO *)(resTab + 2);
91     typeInfo = NE_FindTypeSection( resTab, typeInfo, typeid );
92     if ( !typeInfo )
93     {
94         TRACE("No typeid entry found for %p\n", typeid );
95         HeapFree( GetProcessHeap(), 0, resTab );
96         return FALSE;
97     }
98     nameInfo = NE_FindResourceFromType( resTab, typeInfo, resid );
99     if ( !nameInfo )
100     {
101         TRACE("No resid entry found for %p\n", typeid );
102         HeapFree( GetProcessHeap(), 0, resTab );
103         return FALSE;
104     }
105
106     /* Return resource data */
107     if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
108     if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
109
110     HeapFree( GetProcessHeap(), 0, resTab );
111     return TRUE;
112 }
113
114 /***********************************************************************
115  *           load_pe_resource         [internal]
116  */
117 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
118                                 DWORD *resLen, DWORD *resOff )
119 {
120     IMAGE_NT_HEADERS pehd;
121     DWORD pehdoffset;
122     PIMAGE_DATA_DIRECTORY resDataDir;
123     PIMAGE_SECTION_HEADER sections;
124     LPBYTE resSection;
125     DWORD resSectionSize;
126     DWORD resDir;
127     PIMAGE_RESOURCE_DIRECTORY resPtr;
128     PIMAGE_RESOURCE_DATA_ENTRY resData;
129     int i, nSections;
130
131
132     /* Read in PE header */
133     pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
134     if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
135
136     resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
137     if ( !resDataDir->Size )
138     {
139         TRACE("No resources in PE dll\n" );
140         return FALSE;
141     }
142
143     /* Read in section table */
144     nSections = pehd.FileHeader.NumberOfSections; 
145     sections = HeapAlloc( GetProcessHeap(), 0, 
146                           nSections * sizeof(IMAGE_SECTION_HEADER) );
147     if ( !sections ) return FALSE;
148
149     LZSeek( lzfd, pehdoffset +
150                     sizeof(DWORD) + /* Signature */
151                     sizeof(IMAGE_FILE_HEADER) +
152                     pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
153
154     if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
155          LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
156     {
157         HeapFree( GetProcessHeap(), 0, sections );
158         return FALSE;
159     }
160
161     /* Find resource section */
162     for ( i = 0; i < nSections; i++ )
163         if (    resDataDir->VirtualAddress >= sections[i].VirtualAddress
164              && resDataDir->VirtualAddress <  sections[i].VirtualAddress +
165                                               sections[i].SizeOfRawData )
166             break;
167
168     if ( i == nSections )
169     {
170         HeapFree( GetProcessHeap(), 0, sections );
171         TRACE("Couldn't find resource section\n" );
172         return FALSE;
173     }
174
175     /* Read in resource section */
176     resSectionSize = sections[i].SizeOfRawData; 
177     resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
178     if ( !resSection ) 
179     {
180         HeapFree( GetProcessHeap(), 0, sections );
181         return FALSE;
182     }
183
184     LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
185     if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) )
186     {
187         HeapFree( GetProcessHeap(), 0, resSection );
188         HeapFree( GetProcessHeap(), 0, sections );
189         return FALSE;
190     }
191
192     /* Find resource */
193     resDir = (DWORD)resSection + 
194              (resDataDir->VirtualAddress - sections[i].VirtualAddress);
195
196     resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
197     resPtr = GetResDirEntryA( resPtr, typeid, resDir, FALSE );
198     if ( !resPtr )
199     {
200         TRACE("No typeid entry found for %p\n", typeid );
201         HeapFree( GetProcessHeap(), 0, resSection );
202         HeapFree( GetProcessHeap(), 0, sections );
203         return FALSE;
204     }
205     resPtr = GetResDirEntryA( resPtr, resid, resDir, FALSE );
206     if ( !resPtr )
207     {
208         TRACE("No resid entry found for %p\n", resid );
209         HeapFree( GetProcessHeap(), 0, resSection );
210         HeapFree( GetProcessHeap(), 0, sections );
211         return FALSE;
212     }
213     resPtr = GetResDirEntryA( resPtr, 0, resDir, TRUE );
214     if ( !resPtr )
215     {
216         TRACE("No default language entry found for %p\n", resid );
217         HeapFree( GetProcessHeap(), 0, resSection );
218         HeapFree( GetProcessHeap(), 0, sections );
219         return FALSE;
220     }
221
222     /* Find resource data section */
223     resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
224     for ( i = 0; i < nSections; i++ )
225         if (    resData->OffsetToData >= sections[i].VirtualAddress
226              && resData->OffsetToData <  sections[i].VirtualAddress +
227                                          sections[i].SizeOfRawData )
228             break;
229
230     if ( i == nSections )
231     {
232         TRACE("Couldn't find resource data section\n" );
233         HeapFree( GetProcessHeap(), 0, resSection );
234         HeapFree( GetProcessHeap(), 0, sections );
235         return FALSE;
236     }
237
238     /* Return resource data */
239     if ( resLen ) *resLen = resData->Size;
240     if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
241                             + sections[i].PointerToRawData;
242
243     HeapFree( GetProcessHeap(), 0, resSection );
244     HeapFree( GetProcessHeap(), 0, sections );
245     return TRUE;
246 }
247
248 /***********************************************************************
249  *           GetFileResourceSize32         [internal]
250  */
251 DWORD WINAPI GetFileResourceSize( LPCSTR lpszFileName,
252                                     LPCSTR lpszResType, LPCSTR lpszResId,
253                                     LPDWORD lpdwFileOffset )
254 {
255     BOOL retv = FALSE;
256     HFILE lzfd;
257     OFSTRUCT ofs;
258     DWORD reslen;
259
260     TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
261                 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId, 
262                 lpszResId );
263
264     lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
265     if ( !lzfd ) return 0;
266
267     switch ( read_xx_header( lzfd ) )
268     {
269     case IMAGE_OS2_SIGNATURE:
270         retv = find_ne_resource( lzfd, lpszResType, lpszResId, 
271                                  &reslen, lpdwFileOffset );
272         break;
273
274     case IMAGE_NT_SIGNATURE:
275         retv = find_pe_resource( lzfd, lpszResType, lpszResId, 
276                                  &reslen, lpdwFileOffset );
277         break;
278     }
279
280     LZClose( lzfd );
281     return retv? reslen : 0;
282 }
283
284 /***********************************************************************
285  *           GetFileResource32         [internal]
286  */
287 DWORD WINAPI GetFileResource( LPCSTR lpszFileName,
288                                 LPCSTR lpszResType, LPCSTR lpszResId,
289                                 DWORD dwFileOffset,
290                                 DWORD dwResLen, LPVOID lpvData )
291 {
292     BOOL retv = FALSE;
293     HFILE lzfd;
294     OFSTRUCT ofs;
295     DWORD reslen = dwResLen;
296
297     TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
298                 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId, 
299                 dwFileOffset, dwResLen, lpvData );
300
301     lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
302     if ( lzfd == 0 ) return 0;
303
304     if ( !dwFileOffset )
305     {
306         switch ( read_xx_header( lzfd ) ) 
307         {
308         case IMAGE_OS2_SIGNATURE:
309             retv = find_ne_resource( lzfd, lpszResType, lpszResId, 
310                                      &reslen, &dwFileOffset );
311             break;
312
313         case IMAGE_NT_SIGNATURE:
314             retv = find_pe_resource( lzfd, lpszResType, lpszResId, 
315                                      &reslen, &dwFileOffset );
316             break;
317         }
318
319         if ( !retv ) 
320         {
321             LZClose( lzfd );
322             return 0;
323         }
324     }
325
326     LZSeek( lzfd, dwFileOffset, SEEK_SET );
327     reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
328     LZClose( lzfd );
329
330     return reslen;
331 }
332