- Made sure that the files that contains the declarations
[wine.git] / dlls / version / resource.c
blob126ad29bef4eecb13fd150b4c5cab294011c1782
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 */
9 #include "config.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <unistd.h>
16 #include "winbase.h"
17 #include "lzexpand.h"
19 #include "wine/unicode.h"
20 #include "wine/winbase16.h"
21 #include "wine/winuser16.h"
22 #include "winver.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(ver);
29 /**********************************************************************
30 * find_entry_by_id
32 * Find an entry by id in a resource directory
33 * Copied from loader/pe_resource.c
35 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
36 WORD id, const void *root )
38 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
39 int min, max, pos;
41 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
42 min = dir->NumberOfNamedEntries;
43 max = min + dir->NumberOfIdEntries - 1;
44 while (min <= max)
46 pos = (min + max) / 2;
47 if (entry[pos].u1.s2.Id == id)
48 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
49 if (entry[pos].u1.s2.Id > id) max = pos - 1;
50 else min = pos + 1;
52 return NULL;
56 /**********************************************************************
57 * find_entry_default
59 * Find a default entry in a resource directory
60 * Copied from loader/pe_resource.c
62 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
63 const void *root )
65 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
67 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
68 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
72 /**********************************************************************
73 * find_entry_by_name
75 * Find an entry by name in a resource directory
76 * Copied from loader/pe_resource.c
78 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
79 LPCSTR name, const void *root )
81 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
82 LPWSTR nameW;
83 DWORD namelen;
85 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
86 if (name[0] == '#')
88 return find_entry_by_id( dir, atoi(name+1), root );
91 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
92 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
94 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
95 const IMAGE_RESOURCE_DIR_STRING_U *str;
96 int min, max, res, pos;
98 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
99 namelen--; /* remove terminating null */
100 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
101 min = 0;
102 max = dir->NumberOfNamedEntries - 1;
103 while (min <= max)
105 pos = (min + max) / 2;
106 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
107 res = strncmpiW( nameW, str->NameString, str->Length );
108 if (!res && namelen == str->Length)
110 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
111 break;
113 if (res < 0) max = pos - 1;
114 else min = pos + 1;
116 HeapFree( GetProcessHeap(), 0, nameW );
118 return ret;
122 /***********************************************************************
123 * read_xx_header [internal]
125 static int read_xx_header( HFILE lzfd )
127 IMAGE_DOS_HEADER mzh;
128 char magic[3];
130 LZSeek( lzfd, 0, SEEK_SET );
131 if ( sizeof(mzh) != LZRead( lzfd, &mzh, sizeof(mzh) ) )
132 return 0;
133 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
134 return 0;
136 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
137 if ( 2 != LZRead( lzfd, magic, 2 ) )
138 return 0;
140 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
142 if ( magic[0] == 'N' && magic[1] == 'E' )
143 return IMAGE_OS2_SIGNATURE;
144 if ( magic[0] == 'P' && magic[1] == 'E' )
145 return IMAGE_NT_SIGNATURE;
147 magic[2] = '\0';
148 WARN("Can't handle %s files.\n", magic );
149 return 0;
152 /***********************************************************************
153 * load_ne_resource [internal]
155 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
156 DWORD *resLen, DWORD *resOff )
158 IMAGE_OS2_HEADER nehd;
159 NE_TYPEINFO *typeInfo;
160 NE_NAMEINFO *nameInfo;
161 DWORD nehdoffset;
162 LPBYTE resTab;
163 DWORD resTabSize;
164 int count;
166 /* Read in NE header */
167 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
168 if ( sizeof(nehd) != LZRead( lzfd, &nehd, sizeof(nehd) ) ) return 0;
170 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
171 if ( !resTabSize )
173 TRACE("No resources in NE dll\n" );
174 return FALSE;
177 /* Read in resource table */
178 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
179 if ( !resTab ) return FALSE;
181 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
182 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
184 HeapFree( GetProcessHeap(), 0, resTab );
185 return FALSE;
188 /* Find resource */
189 typeInfo = (NE_TYPEINFO *)(resTab + 2);
191 if (HIWORD(typeid) != 0) /* named type */
193 BYTE len = strlen( typeid );
194 while (typeInfo->type_id)
196 if (!(typeInfo->type_id & 0x8000))
198 BYTE *p = resTab + typeInfo->type_id;
199 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
201 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
202 typeInfo->count * sizeof(NE_NAMEINFO));
205 else /* numeric type id */
207 WORD id = LOWORD(typeid) | 0x8000;
208 while (typeInfo->type_id)
210 if (typeInfo->type_id == id) goto found_type;
211 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
212 typeInfo->count * sizeof(NE_NAMEINFO));
215 TRACE("No typeid entry found for %p\n", typeid );
216 HeapFree( GetProcessHeap(), 0, resTab );
217 return FALSE;
219 found_type:
220 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
222 if (HIWORD(resid) != 0) /* named resource */
224 BYTE len = strlen( resid );
225 for (count = typeInfo->count; count > 0; count--, nameInfo++)
227 BYTE *p = resTab + nameInfo->id;
228 if (nameInfo->id & 0x8000) continue;
229 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
232 else /* numeric resource id */
234 WORD id = LOWORD(resid) | 0x8000;
235 for (count = typeInfo->count; count > 0; count--, nameInfo++)
236 if (nameInfo->id == id) goto found_name;
238 TRACE("No resid entry found for %p\n", typeid );
239 HeapFree( GetProcessHeap(), 0, resTab );
240 return FALSE;
242 found_name:
243 /* Return resource data */
244 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
245 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
247 HeapFree( GetProcessHeap(), 0, resTab );
248 return TRUE;
251 /***********************************************************************
252 * load_pe_resource [internal]
254 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
255 DWORD *resLen, DWORD *resOff )
257 IMAGE_NT_HEADERS pehd;
258 DWORD pehdoffset;
259 PIMAGE_DATA_DIRECTORY resDataDir;
260 PIMAGE_SECTION_HEADER sections;
261 LPBYTE resSection;
262 DWORD resSectionSize;
263 const void *resDir;
264 const IMAGE_RESOURCE_DIRECTORY *resPtr;
265 const IMAGE_RESOURCE_DATA_ENTRY *resData;
266 int i, nSections;
267 BOOL ret = FALSE;
269 /* Read in PE header */
270 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
271 if ( sizeof(pehd) != LZRead( lzfd, &pehd, sizeof(pehd) ) ) return 0;
273 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
274 if ( !resDataDir->Size )
276 TRACE("No resources in PE dll\n" );
277 return FALSE;
280 /* Read in section table */
281 nSections = pehd.FileHeader.NumberOfSections;
282 sections = HeapAlloc( GetProcessHeap(), 0,
283 nSections * sizeof(IMAGE_SECTION_HEADER) );
284 if ( !sections ) return FALSE;
286 LZSeek( lzfd, pehdoffset +
287 sizeof(DWORD) + /* Signature */
288 sizeof(IMAGE_FILE_HEADER) +
289 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
291 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
292 LZRead( lzfd, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
294 HeapFree( GetProcessHeap(), 0, sections );
295 return FALSE;
298 /* Find resource section */
299 for ( i = 0; i < nSections; i++ )
300 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
301 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
302 sections[i].SizeOfRawData )
303 break;
305 if ( i == nSections )
307 HeapFree( GetProcessHeap(), 0, sections );
308 TRACE("Couldn't find resource section\n" );
309 return FALSE;
312 /* Read in resource section */
313 resSectionSize = sections[i].SizeOfRawData;
314 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
315 if ( !resSection )
317 HeapFree( GetProcessHeap(), 0, sections );
318 return FALSE;
321 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
322 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
324 /* Find resource */
325 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
327 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
328 resPtr = find_entry_by_name( resPtr, typeid, resDir );
329 if ( !resPtr )
331 TRACE("No typeid entry found for %p\n", typeid );
332 goto done;
334 resPtr = find_entry_by_name( resPtr, resid, resDir );
335 if ( !resPtr )
337 TRACE("No resid entry found for %p\n", resid );
338 goto done;
340 resPtr = find_entry_default( resPtr, resDir );
341 if ( !resPtr )
343 TRACE("No default language entry found for %p\n", resid );
344 goto done;
347 /* Find resource data section */
348 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
349 for ( i = 0; i < nSections; i++ )
350 if ( resData->OffsetToData >= sections[i].VirtualAddress
351 && resData->OffsetToData < sections[i].VirtualAddress +
352 sections[i].SizeOfRawData )
353 break;
355 if ( i == nSections )
357 TRACE("Couldn't find resource data section\n" );
358 goto done;
361 /* Return resource data */
362 if ( resLen ) *resLen = resData->Size;
363 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
364 + sections[i].PointerToRawData;
365 ret = TRUE;
367 done:
368 HeapFree( GetProcessHeap(), 0, resSection );
369 HeapFree( GetProcessHeap(), 0, sections );
370 return ret;
374 /*************************************************************************
375 * GetFileResourceSize [VER.2]
377 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
378 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
380 BOOL retv = FALSE;
381 HFILE lzfd;
382 OFSTRUCT ofs;
383 DWORD reslen;
385 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
386 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
387 lpszResId );
389 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
390 if ( lzfd < 0 ) return 0;
392 switch ( read_xx_header( lzfd ) )
394 case IMAGE_OS2_SIGNATURE:
395 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
396 &reslen, lpdwFileOffset );
397 break;
399 case IMAGE_NT_SIGNATURE:
400 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
401 &reslen, lpdwFileOffset );
402 break;
405 LZClose( lzfd );
406 return retv? reslen : 0;
410 /*************************************************************************
411 * GetFileResource [VER.3]
413 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
414 LPCSTR lpszResId, DWORD dwFileOffset,
415 DWORD dwResLen, LPVOID lpvData )
417 BOOL retv = FALSE;
418 HFILE lzfd;
419 OFSTRUCT ofs;
420 DWORD reslen = dwResLen;
422 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
423 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
424 dwFileOffset, dwResLen, lpvData );
426 lzfd = LZOpenFileA( lpszFileName, &ofs, OF_READ );
427 if ( lzfd < 0 ) return 0;
429 if ( !dwFileOffset )
431 switch ( read_xx_header( lzfd ) )
433 case IMAGE_OS2_SIGNATURE:
434 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
435 &reslen, &dwFileOffset );
436 break;
438 case IMAGE_NT_SIGNATURE:
439 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
440 &reslen, &dwFileOffset );
441 break;
444 if ( !retv )
446 LZClose( lzfd );
447 return 0;
451 LZSeek( lzfd, dwFileOffset, SEEK_SET );
452 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
453 LZClose( lzfd );
455 return reslen;