2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2004,2005 Aric Stewart for CodeWeavers
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msi);
32 typedef struct _tagTT_OFFSET_TABLE {
37 USHORT uEntrySelector;
41 typedef struct _tagTT_TABLE_DIRECTORY {
42 char szTag[4]; /* table name */
43 ULONG uCheckSum; /* Check sum */
44 ULONG uOffset; /* Offset from beginning of file */
45 ULONG uLength; /* length of the table in bytes */
48 typedef struct _tagTT_NAME_TABLE_HEADER {
49 USHORT uFSelector; /* format selector. Always 0 */
50 USHORT uNRCount; /* Name Records count */
51 USHORT uStorageOffset; /* Offset for strings storage,
52 * from start of the table */
53 } TT_NAME_TABLE_HEADER;
55 #define NAME_ID_FULL_FONT_NAME 4
56 #define NAME_ID_VERSION 5
58 typedef struct _tagTT_NAME_RECORD {
64 USHORT uStringOffset; /* from start of storage area */
67 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
68 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
70 static const WCHAR regfont1[] =
71 {'S','o','f','t','w','a','r','e','\\',
72 'M','i','c','r','o','s','o','f','t','\\',
73 'W','i','n','d','o','w','s',' ','N','T','\\',
74 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
75 'F','o','n','t','s',0};
76 static const WCHAR regfont2[] =
77 {'S','o','f','t','w','a','r','e','\\',
78 'M','i','c','r','o','s','o','f','t','\\',
79 'W','i','n','d','o','w','s','\\',
80 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
81 'F','o','n','t','s',0};
84 * Code based off of code located here
85 * http://www.codeproject.com/gdi/fontnamefromfile.asp
87 static WCHAR *load_ttf_name_id( const WCHAR *filename, DWORD id )
89 TT_TABLE_DIRECTORY tblDir;
91 TT_OFFSET_TABLE ttOffsetTable;
92 TT_NAME_TABLE_HEADER ttNTHeader;
93 TT_NAME_RECORD ttRecord;
99 handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
100 FILE_ATTRIBUTE_NORMAL, 0 );
101 if (handle == INVALID_HANDLE_VALUE)
103 ERR("Unable to open font file %s\n", debugstr_w(filename));
107 if (!ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),&dwRead,NULL))
110 ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
111 ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
112 ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
114 if ((ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0) &&
115 (ttOffsetTable.uMajorVersion != 0x4f54 || ttOffsetTable.uMinorVersion != 0x544f))
118 for (i=0; i< ttOffsetTable.uNumOfTables; i++)
120 if (!ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),&dwRead,NULL))
122 if (memcmp(tblDir.szTag,"name",4)==0)
125 tblDir.uLength = SWAPLONG(tblDir.uLength);
126 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
134 SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
135 if (!ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER), &dwRead,NULL))
138 ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
139 ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
140 for(i=0; i<ttNTHeader.uNRCount; i++)
142 if (!ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),&dwRead,NULL))
145 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
146 if (ttRecord.uNameID == id)
151 ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
152 ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
153 nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
154 SetFilePointer(handle, tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset,
156 buf = msi_alloc_zero( ttRecord.uStringLength + 1 );
157 ReadFile(handle, buf, ttRecord.uStringLength, &dwRead, NULL);
160 ret = strdupAtoW(buf);
165 SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
171 TRACE("Returning %s\n", debugstr_w(ret));
175 static WCHAR *font_name_from_file( const WCHAR *filename )
177 static const WCHAR truetypeW[] = {' ','(','T','r','u','e','T','y','p','e',')',0};
178 WCHAR *name, *ret = NULL;
180 if ((name = load_ttf_name_id( filename, NAME_ID_FULL_FONT_NAME )))
182 ret = msi_alloc( (strlenW( name ) + strlenW( truetypeW ) + 1 ) * sizeof(WCHAR) );
183 strcpyW( ret, name );
184 strcatW( ret, truetypeW );
190 WCHAR *msi_font_version_from_file( const WCHAR *filename )
192 static const WCHAR dotzerodotzeroW[] = {'.','0','.','0',0};
193 WCHAR *version, *p, *ret = NULL;
196 if ((version = load_ttf_name_id( filename, NAME_ID_VERSION )))
198 if ((p = strchrW( version, ';' ))) *p = 0;
200 while (*p && !isdigitW( *p )) p++;
201 len = strlenW( p ) + strlenW(dotzerodotzeroW) + 1;
202 ret = msi_alloc( len * sizeof(WCHAR) );
204 if ((p = strchrW( p, '.' )) && !(p = strchrW( p + 1, '.' )))
206 strcatW( ret, dotzerodotzeroW );
213 static UINT ITERATE_RegisterFonts(MSIRECORD *row, LPVOID param)
215 MSIPACKAGE *package = param;
224 filename = MSI_RecordGetString( row, 1 );
225 file = msi_get_loaded_file( package, filename );
228 WARN("unable to find file %s\n", debugstr_w(filename));
229 return ERROR_SUCCESS;
231 comp = msi_get_loaded_component( package, file->Component->Component );
234 WARN("unable to find component %s\n", debugstr_w(file->Component->Component));
235 return ERROR_SUCCESS;
237 comp->Action = msi_get_component_action( package, comp );
238 if (comp->Action != INSTALLSTATE_LOCAL)
240 TRACE("component not scheduled for installation %s\n", debugstr_w(comp->Component));
241 return ERROR_SUCCESS;
244 RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
245 RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
247 if (MSI_RecordIsNull(row,2))
248 name = font_name_from_file( file->TargetPath );
250 name = msi_dup_record_field(row,2);
254 msi_reg_set_val_str( hkey1, name, file->TargetPath);
255 msi_reg_set_val_str( hkey2, name, file->TargetPath);
263 uirow = MSI_CreateRecord( 1 );
264 uipath = strdupW( file->TargetPath );
265 p = strrchrW(uipath,'\\');
268 MSI_RecordSetStringW( uirow, 1, p );
269 msi_ui_actiondata( package, szRegisterFonts, uirow );
270 msiobj_release( &uirow->hdr );
272 /* FIXME: call msi_ui_progress? */
274 return ERROR_SUCCESS;
277 UINT ACTION_RegisterFonts(MSIPACKAGE *package)
279 static const WCHAR query[] = {
280 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','F','o','n','t','`',0};
284 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
285 if (rc != ERROR_SUCCESS)
286 return ERROR_SUCCESS;
288 rc = MSI_IterateRecords(view, NULL, ITERATE_RegisterFonts, package);
289 msiobj_release(&view->hdr);
293 static UINT ITERATE_UnregisterFonts( MSIRECORD *row, LPVOID param )
295 MSIPACKAGE *package = param;
304 filename = MSI_RecordGetString( row, 1 );
305 file = msi_get_loaded_file( package, filename );
308 WARN("unable to find file %s\n", debugstr_w(filename));
309 return ERROR_SUCCESS;
311 comp = msi_get_loaded_component( package, file->Component->Component );
314 WARN("unable to find component %s\n", debugstr_w(file->Component->Component));
315 return ERROR_SUCCESS;
317 comp->Action = msi_get_component_action( package, comp );
318 if (comp->Action != INSTALLSTATE_ABSENT)
320 TRACE("component not scheduled for removal %s\n", debugstr_w(comp->Component));
321 return ERROR_SUCCESS;
324 RegCreateKeyW( HKEY_LOCAL_MACHINE, regfont1, &hkey1 );
325 RegCreateKeyW( HKEY_LOCAL_MACHINE, regfont2, &hkey2 );
327 if (MSI_RecordIsNull( row, 2 ))
328 name = font_name_from_file( file->TargetPath );
330 name = msi_dup_record_field( row, 2 );
334 RegDeleteValueW( hkey1, name );
335 RegDeleteValueW( hkey2, name );
339 RegCloseKey( hkey1 );
340 RegCloseKey( hkey2 );
343 uirow = MSI_CreateRecord( 1 );
344 uipath = strdupW( file->TargetPath );
345 p = strrchrW( uipath,'\\' );
348 MSI_RecordSetStringW( uirow, 1, p );
349 msi_ui_actiondata( package, szUnregisterFonts, uirow );
350 msiobj_release( &uirow->hdr );
352 /* FIXME: call msi_ui_progress? */
354 return ERROR_SUCCESS;
357 UINT ACTION_UnregisterFonts( MSIPACKAGE *package )
359 static const WCHAR query[] = {
360 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','F','o','n','t','`',0};
364 r = MSI_DatabaseOpenViewW( package->db, query, &view );
365 if (r != ERROR_SUCCESS)
366 return ERROR_SUCCESS;
368 r = MSI_IterateRecords( view, NULL, ITERATE_UnregisterFonts, package );
369 msiobj_release( &view->hdr );