2 * Implementation of VERSION.DLL - Resource Access routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(ver)
21 /***********************************************************************
22 * read_xx_header [internal]
24 static int read_xx_header( HFILE lzfd )
29 LZSeek( lzfd, 0, SEEK_SET );
30 if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
32 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
35 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
36 if ( 2 != LZRead( lzfd, magic, 2 ) )
39 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
41 if ( magic[0] == 'N' && magic[1] == 'E' )
42 return IMAGE_OS2_SIGNATURE;
43 if ( magic[0] == 'P' && magic[1] == 'E' )
44 return IMAGE_NT_SIGNATURE;
47 WARN("Can't handle %s files.\n", magic );
51 /***********************************************************************
52 * load_ne_resource [internal]
54 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
55 DWORD *resLen, DWORD *resOff )
57 IMAGE_OS2_HEADER nehd;
58 NE_TYPEINFO *typeInfo;
59 NE_NAMEINFO *nameInfo;
64 /* Read in NE header */
65 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
66 if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
68 resTabSize = nehd.rname_tab_offset - nehd.resource_tab_offset;
71 TRACE("No resources in NE dll\n" );
75 /* Read in resource table */
76 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
77 if ( !resTab ) return FALSE;
79 LZSeek( lzfd, nehd.resource_tab_offset + nehdoffset, SEEK_SET );
80 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
82 HeapFree( GetProcessHeap(), 0, resTab );
87 typeInfo = (NE_TYPEINFO *)(resTab + 2);
88 typeInfo = NE_FindTypeSection( resTab, typeInfo, typeid );
91 TRACE("No typeid entry found for %p\n", typeid );
92 HeapFree( GetProcessHeap(), 0, resTab );
95 nameInfo = NE_FindResourceFromType( resTab, typeInfo, resid );
98 TRACE("No resid entry found for %p\n", typeid );
99 HeapFree( GetProcessHeap(), 0, resTab );
103 /* Return resource data */
104 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
105 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
107 HeapFree( GetProcessHeap(), 0, resTab );
111 /***********************************************************************
112 * load_pe_resource [internal]
114 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
115 DWORD *resLen, DWORD *resOff )
117 IMAGE_NT_HEADERS pehd;
119 PIMAGE_DATA_DIRECTORY resDataDir;
120 PIMAGE_SECTION_HEADER sections;
122 DWORD resSectionSize;
124 PIMAGE_RESOURCE_DIRECTORY resPtr;
125 PIMAGE_RESOURCE_DATA_ENTRY resData;
129 /* Read in PE header */
130 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
131 if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
133 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
134 if ( !resDataDir->Size )
136 TRACE("No resources in PE dll\n" );
140 /* Read in section table */
141 nSections = pehd.FileHeader.NumberOfSections;
142 sections = HeapAlloc( GetProcessHeap(), 0,
143 nSections * sizeof(IMAGE_SECTION_HEADER) );
144 if ( !sections ) return FALSE;
146 LZSeek( lzfd, pehdoffset +
147 sizeof(DWORD) + /* Signature */
148 sizeof(IMAGE_FILE_HEADER) +
149 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
151 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
152 LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
154 HeapFree( GetProcessHeap(), 0, sections );
158 /* Find resource section */
159 for ( i = 0; i < nSections; i++ )
160 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
161 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
162 sections[i].SizeOfRawData )
165 if ( i == nSections )
167 HeapFree( GetProcessHeap(), 0, sections );
168 TRACE("Couldn't find resource section\n" );
172 /* Read in resource section */
173 resSectionSize = sections[i].SizeOfRawData;
174 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
177 HeapFree( GetProcessHeap(), 0, sections );
181 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
182 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) )
184 HeapFree( GetProcessHeap(), 0, resSection );
185 HeapFree( GetProcessHeap(), 0, sections );
190 resDir = (DWORD)resSection +
191 (resDataDir->VirtualAddress - sections[i].VirtualAddress);
193 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
194 resPtr = GetResDirEntryA( resPtr, typeid, resDir, FALSE );
197 TRACE("No typeid entry found for %p\n", typeid );
198 HeapFree( GetProcessHeap(), 0, resSection );
199 HeapFree( GetProcessHeap(), 0, sections );
202 resPtr = GetResDirEntryA( resPtr, resid, resDir, FALSE );
205 TRACE("No resid entry found for %p\n", resid );
206 HeapFree( GetProcessHeap(), 0, resSection );
207 HeapFree( GetProcessHeap(), 0, sections );
210 resPtr = GetResDirEntryA( resPtr, 0, resDir, TRUE );
213 TRACE("No default language entry found for %p\n", resid );
214 HeapFree( GetProcessHeap(), 0, resSection );
215 HeapFree( GetProcessHeap(), 0, sections );
219 /* Find resource data section */
220 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
221 for ( i = 0; i < nSections; i++ )
222 if ( resData->OffsetToData >= sections[i].VirtualAddress
223 && resData->OffsetToData < sections[i].VirtualAddress +
224 sections[i].SizeOfRawData )
227 if ( i == nSections )
229 TRACE("Couldn't find resource data section\n" );
230 HeapFree( GetProcessHeap(), 0, resSection );
231 HeapFree( GetProcessHeap(), 0, sections );
235 /* Return resource data */
236 if ( resLen ) *resLen = resData->Size;
237 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
238 + sections[i].PointerToRawData;
240 HeapFree( GetProcessHeap(), 0, resSection );
241 HeapFree( GetProcessHeap(), 0, sections );
245 /***********************************************************************
246 * GetFileResourceSize32 [internal]
248 DWORD WINAPI GetFileResourceSize( LPCSTR lpszFileName,
249 LPCSTR lpszResType, LPCSTR lpszResId,
250 LPDWORD lpdwFileOffset )
257 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
258 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
261 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
262 if ( !lzfd ) return 0;
264 switch ( read_xx_header( lzfd ) )
266 case IMAGE_OS2_SIGNATURE:
267 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
268 &reslen, lpdwFileOffset );
271 case IMAGE_NT_SIGNATURE:
272 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
273 &reslen, lpdwFileOffset );
278 return retv? reslen : 0;
281 /***********************************************************************
282 * GetFileResource32 [internal]
284 DWORD WINAPI GetFileResource( LPCSTR lpszFileName,
285 LPCSTR lpszResType, LPCSTR lpszResId,
287 DWORD dwResLen, LPVOID lpvData )
292 DWORD reslen = dwResLen;
294 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
295 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
296 dwFileOffset, dwResLen, lpvData );
298 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
299 if ( lzfd == 0 ) return 0;
303 switch ( read_xx_header( lzfd ) )
305 case IMAGE_OS2_SIGNATURE:
306 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
307 &reslen, &dwFileOffset );
310 case IMAGE_NT_SIGNATURE:
311 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
312 &reslen, &dwFileOffset );
323 LZSeek( lzfd, dwFileOffset, SEEK_SET );
324 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );