MSI: When loading table data, split it up into rows.

Mike McCormack mike at codeweavers.com
Wed Mar 17 21:31:38 CST 2004


ChangeLog:
* When loading table data, split it up into rows.
-------------- next part --------------
Index: dlls/msi/msi.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/msi.c,v
retrieving revision 1.12
diff -u -r1.12 msi.c
--- dlls/msi/msi.c	17 Mar 2004 20:49:59 -0000	1.12
+++ dlls/msi/msi.c	18 Mar 2004 02:42:28 -0000
@@ -500,13 +500,13 @@
 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
 {
     FIXME("%s\n", debugstr_a(szProduct));
-    return 0;
+    return INSTALLSTATE_UNKNOWN;
 }
 
 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
 {
     FIXME("%s\n", debugstr_w(szProduct));
-    return 0;
+    return INSTALLSTATE_UNKNOWN;
 }
 
 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
Index: dlls/msi/msipriv.h
===================================================================
RCS file: /home/wine/wine/dlls/msi/msipriv.h,v
retrieving revision 1.6
diff -u -r1.6 msipriv.h
--- dlls/msi/msipriv.h	17 Mar 2004 20:49:59 -0000	1.6
+++ dlls/msi/msipriv.h	18 Mar 2004 02:42:29 -0000
@@ -153,14 +153,20 @@
 extern UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **table);
 extern UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **table);
 extern UINT load_string_table( MSIDATABASE *db );
+extern UINT MSI_CommitTables( MSIDATABASE *db );
+
 
 /* string table functions */
 extern BOOL msi_addstring( string_table *st, UINT string_no, CHAR *data, UINT len, UINT refcount );
-extern UINT msi_id2string( string_table *st, UINT string_no, LPWSTR buffer, UINT *sz );
+extern UINT msi_id2stringW( string_table *st, UINT string_no, LPWSTR buffer, UINT *sz );
+extern UINT msi_id2stringA( string_table *st, UINT string_no, LPSTR buffer, UINT *sz );
 extern LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid);
 extern UINT msi_string2id( string_table *st, LPCWSTR buffer, UINT *id );
 extern string_table *msi_init_stringtable( int entries );
 extern VOID msi_destroy_stringtable( string_table *st );
+extern UINT msi_string_count( string_table *st );
+extern UINT msi_id_refcount( string_table *st, UINT i );
+extern UINT msi_string_totalsize( string_table *st );
 
 UINT VIEW_find_column( MSIVIEW *view, LPWSTR name, UINT *n );
 
Index: dlls/msi/msiquery.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/msiquery.c,v
retrieving revision 1.5
diff -u -r1.5 msiquery.c
--- dlls/msi/msiquery.c	30 Oct 2003 22:47:42 -0000	1.5
+++ dlls/msi/msiquery.c	18 Mar 2004 02:42:29 -0000
@@ -361,8 +361,22 @@
 
 UINT WINAPI MsiDatabaseCommit( MSIHANDLE hdb )
 {
-    FIXME("%ld\n", hdb);
-    return ERROR_SUCCESS;
+    MSIDATABASE *db;
+    UINT r;
+
+    TRACE("%ld\n", hdb);
+
+    db = msihandle2msiinfo( hdb, MSIHANDLETYPE_DATABASE );
+    if( !db )
+        return ERROR_INVALID_HANDLE;
+
+    /* FIXME: lock the database */
+
+    r = MSI_CommitTables( db );
+
+    /* FIXME: unlock the database */
+
+    return r;
 }
 
 UINT WINAPI MsiDatabaseGetPrimaryKeysA(MSIHANDLE hdb, 
Index: dlls/msi/string.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/string.c,v
retrieving revision 1.1
diff -u -r1.1 string.c
--- dlls/msi/string.c	17 Mar 2004 20:49:59 -0000	1.1
+++ dlls/msi/string.c	18 Mar 2004 02:42:29 -0000
@@ -123,7 +123,7 @@
 {
     int n;
 
-    TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) );
+    /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
 
     n = st_find_free_entry( st );
     if( n < 0 )
@@ -145,7 +145,7 @@
     return n;
 }
 
-UINT msi_id2string( string_table *st, UINT string_no, LPWSTR buffer, UINT *sz )
+UINT msi_id2stringW( string_table *st, UINT string_no, LPWSTR buffer, UINT *sz )
 {
     UINT len;
     LPSTR str;
@@ -173,6 +173,34 @@
     return ERROR_SUCCESS;
 }
 
+UINT msi_id2stringA( string_table *st, UINT string_no, LPSTR buffer, UINT *sz )
+{
+    UINT len;
+    LPSTR str;
+
+    TRACE("Finding string %d of %d\n", string_no, st->count);
+    if( string_no >= st->count )
+        return ERROR_FUNCTION_FAILED;
+
+    if( !st->strings[string_no].refcount )
+        return ERROR_FUNCTION_FAILED;
+
+    str = st->strings[string_no].str;
+    len = strlen( str );
+
+    if( !buffer )
+    {
+        *sz = len;
+        return ERROR_SUCCESS;
+    }
+
+    memcpy( buffer, str, *sz-1 ); 
+    buffer[len] = 0;
+    *sz = len+1;
+
+    return ERROR_SUCCESS;
+}
+
 UINT msi_string2id( string_table *st, LPCWSTR buffer, UINT *id )
 {
     DWORD sz;
@@ -206,4 +234,28 @@
         HeapFree( GetProcessHeap(), 0, str );
 
     return r;
+}
+
+UINT msi_string_count( string_table *st )
+{
+    return st->count;
+}
+
+UINT msi_id_refcount( string_table *st, UINT i )
+{
+    if( i >= st->count )
+        return 0;
+    return st->strings[i].refcount;
+}
+
+UINT msi_string_totalsize( string_table *st )
+{
+    UINT size = 0, i;
+
+    for( i=0; i<st->count; i++)
+    {
+        if( st->strings[i].str )
+            size += strlen( st->strings[i].str );
+    }
+    return size;
 }
Index: dlls/msi/table.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/table.c,v
retrieving revision 1.7
diff -u -r1.7 table.c
--- dlls/msi/table.c	17 Mar 2004 20:49:59 -0000	1.7
+++ dlls/msi/table.c	18 Mar 2004 02:42:29 -0000
@@ -46,16 +46,30 @@
 
 struct tagMSITABLE
 {
-    USHORT *data;
-    UINT size;
+    USHORT **data;
     UINT ref_count;
+    UINT row_count;
     struct tagMSITABLE *next;
     struct tagMSITABLE *prev;
     WCHAR name[1];
-} ;
+};
 
 #define MAX_STREAM_NAME 0x1f
 
+static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
+       MSICOLUMNINFO **pcols, UINT *pcount );
+static UINT get_tablecolumns( MSIDATABASE *db, 
+       LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
+
+static inline UINT bytes_per_column( MSICOLUMNINFO *col )
+{
+    if( col->type & MSITYPE_STRING )
+        return 2;
+    if( (col->type & 0xff) > 4 )
+        ERR("Invalid column size!\n");
+    return col->type & 0xff;
+}
+
 static int utf2mime(int x)
 {
     if( (x>='0') && (x<='9') )
@@ -150,7 +164,7 @@
 }
 #endif
 
-static BOOL read_stream_data( IStorage *stg, LPWSTR stname,
+static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
                               USHORT **pdata, UINT *psz )
 {
     HRESULT r;
@@ -159,8 +173,13 @@
     ULONG sz, count;
     IStream *stm = NULL;
     STATSTG stat;
+    WCHAR encname[0x20];
+
+    encode_streamname(TRUE, stname, encname);
+
+    TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
 
-    r = IStorage_OpenStream(stg, stname, NULL, 
+    r = IStorage_OpenStream(stg, encname, NULL, 
             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
     if( FAILED( r ) )
     {
@@ -208,12 +227,65 @@
     return ret;
 }
 
-UINT read_table_from_storage(IStorage *stg, LPCWSTR name, MSITABLE **ptable)
+static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
+                               LPVOID data, UINT sz )
+{
+    HRESULT r;
+    UINT ret = ERROR_FUNCTION_FAILED;
+    ULONG count;
+    IStream *stm = NULL;
+    ULARGE_INTEGER size;
+    LARGE_INTEGER pos;
+
+    r = IStorage_OpenStream( stg, stname, NULL, 
+            STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
+    if( FAILED( r ) )
+    {
+        WARN("open stream failed r = %08lx - empty table?\n",r);
+        return ret;
+    }
+
+    size.LowPart = sz;
+    size.HighPart = 0;
+    r = IStream_SetSize( stm, size );
+    if( FAILED( r ) || ( count != sz ) )
+    {
+        ERR("Failed to SetSize\n");
+        goto end;
+    }
+
+    pos.LowPart = 0;
+    pos.HighPart = 0;
+    r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
+    if( FAILED( r ) || ( count != sz ) )
+    {
+        ERR("Failed to Seek\n");
+        goto end;
+    }
+
+    r = IStream_Write(stm, data, sz, &count );
+    if( FAILED( r ) || ( count != sz ) )
+    {
+        ERR("Failed to Write\n");
+        goto end;
+    }
+
+    ret = ERROR_SUCCESS;
+
+end:
+    IStream_Release( stm );
+
+    return ret;
+}
+
+UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
 {
-    WCHAR buffer[0x20];
     MSITABLE *t;
+    USHORT *rawdata = NULL;
+    UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
+    MSICOLUMNINFO *cols, *last_col;
 
-    TRACE("%s -> %s\n",debugstr_w(name),debugstr_w(buffer));
+    TRACE("%s\n",debugstr_w(name));
 
     /* non-existing tables should be interpretted as empty tables */
     t = HeapAlloc( GetProcessHeap(), 0, 
@@ -221,15 +293,69 @@
     if( !t )
         return ERROR_NOT_ENOUGH_MEMORY;
 
-    t->size = 0;
+    r = table_get_column_info( db, name, &cols, &num_cols );
+    if( r != ERROR_SUCCESS )
+    {
+        HeapFree( GetProcessHeap(), 0, t );
+        return r;
+    }
+    last_col = &cols[num_cols-1];
+    row_size = last_col->offset + bytes_per_column( last_col );
+
     t->data = NULL;
     lstrcpyW( t->name, name );
     t->ref_count = 1;
     *ptable = t;
 
     /* if we can't read the table, just assume that it's empty */
-    encode_streamname(TRUE, name, buffer);
-    read_stream_data( stg, buffer, &t->data, &t->size );
+    read_stream_data( db->storage, name, &rawdata, &rawsize );
+    if( !rawdata )
+        return ERROR_SUCCESS;
+
+    TRACE("Read %d bytes\n", rawsize );
+
+    if( rawsize % row_size )
+    {
+        ERR("Table size is invalid %d/%d\n", rawsize, row_size );
+        return ERROR_FUNCTION_FAILED;
+    }
+
+    t->row_count = rawsize / row_size;
+    t->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
+                         t->row_count * sizeof (USHORT*) );
+    if( !t->data )
+        return ERROR_NOT_ENOUGH_MEMORY;  /* FIXME: memory leak */
+
+    /* transpose all the data */
+    TRACE("Transposing data from %d columns\n", t->row_count );
+    for( i=0; i<t->row_count; i++ )
+    {
+        t->data[i] = HeapAlloc( GetProcessHeap(), 0, row_size );
+        if( !t->data[i] )
+            return ERROR_NOT_ENOUGH_MEMORY;  /* FIXME: memory leak */
+        for( j=0; j<num_cols; j++ )
+        {
+            UINT ofs = cols[j].offset/2;
+            UINT n = bytes_per_column( &cols[j] );
+
+            switch( n )
+            {
+            case 2:
+                t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
+                break;
+            case 4:
+                t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
+                t->data[i][ofs+1] = rawdata[ofs*t->row_count + i + 1];
+                break;
+            default:
+                ERR("oops - unknown column width %d\n", n);
+                return ERROR_FUNCTION_FAILED;
+            }
+        }
+    }
+
+    HeapFree( GetProcessHeap(), 0, cols );
+    HeapFree( GetProcessHeap(), 0, rawdata );
 
     return ERROR_SUCCESS;
 }
@@ -305,6 +431,40 @@
     return ERROR_FUNCTION_FAILED;
 }
 
+static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
+{
+    UINT r, column_count;
+    MSICOLUMNINFO *columns;
+
+    /* get the number of columns in this table */
+    column_count = 0;
+    r = get_tablecolumns( db, name, NULL, &column_count );
+    if( r != ERROR_SUCCESS )
+        return r;
+
+    /* if there's no columns, there's no table */
+    if( column_count == 0 )
+        return ERROR_INVALID_PARAMETER;
+
+    TRACE("Table %s found\n", debugstr_w(name) );
+
+    columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
+    if( !columns )
+        return ERROR_FUNCTION_FAILED;
+
+    r = get_tablecolumns( db, name, columns, &column_count );
+    if( r != ERROR_SUCCESS )
+    {
+        HeapFree( GetProcessHeap(), 0, columns );
+        return ERROR_FUNCTION_FAILED;
+    }
+
+    *pcols = columns;
+    *pcount = column_count;
+
+    return r;
+}
+
 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
 {
     UINT r;
@@ -319,7 +479,7 @@
         return r;
     }
 
-    r = read_table_from_storage( db->storage, name, ptable );
+    r = read_table_from_storage( db, name, ptable );
     if( r != ERROR_SUCCESS )
         return r;
 
@@ -330,10 +490,17 @@
     return ERROR_SUCCESS;
 }
 
+UINT save_table( MSIDATABASE *db, MSITABLE *ptable )
+{
+    
+    return ERROR_SUCCESS;
+}
+
 UINT load_string_table( MSIDATABASE *db )
 {
-    MSITABLE *pool = NULL, *info = NULL;
-    UINT r, ret = ERROR_FUNCTION_FAILED;
+    CHAR *data;
+    USHORT *pool;
+    UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0;
     DWORD i, count, offset, len;
     const WCHAR szStringData[] = { 
         '_','S','t','r','i','n','g','D','a','t','a',0 };
@@ -346,21 +513,21 @@
         db->strings = NULL;
     }
 
-    r = get_table( db, szStringPool, &pool );
+    r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
     if( r != ERROR_SUCCESS)
         goto end;
-    r = get_table( db, szStringData, &info );
+    r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
     if( r != ERROR_SUCCESS)
         goto end;
 
-    count = pool->size/4;
+    count = poolsize/4;
     db->strings = msi_init_stringtable( count );
 
     offset = 0;
     for( i=0; i<count; i++ )
     {
-        len = pool->data[i*2];
-        msi_addstring( db->strings, i, (LPSTR)info->data+offset, len, pool->data[i*2+1] );
+        len = pool[i*2];
+        msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
         offset += len;
     }
 
@@ -369,10 +536,75 @@
     ret = ERROR_SUCCESS;
 
 end:
-    if( info )
-        release_table( db, info );
     if( pool )
-        release_table( db, pool );
+        HeapFree( GetProcessHeap(), 0, pool );
+    if( data )
+        HeapFree( GetProcessHeap(), 0, data );
+
+    return ret;
+}
+
+UINT save_string_table( MSIDATABASE *db )
+{
+    UINT i, count, datasize, poolsize, sz, remaining, r;
+    UINT ret = ERROR_FUNCTION_FAILED;
+    const WCHAR szStringData[] = { 
+        '_','S','t','r','i','n','g','D','a','t','a',0 };
+    const WCHAR szStringPool[] = { 
+        '_','S','t','r','i','n','g','P','o','o','l',0 };
+    CHAR *data = NULL;
+    USHORT *pool = NULL;
+
+    /* construct the new table in memory first */
+    count = msi_string_count( db->strings );
+    poolsize = count*2*sizeof(USHORT);
+    datasize = msi_string_totalsize( db->strings );
+
+    pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
+    if( ! pool )
+        goto err;
+    data = HeapAlloc( GetProcessHeap(), 0, datasize );
+    if( ! data )
+        goto err;
+
+    remaining = datasize;
+    for( i=0; i<count; i++ )
+    {
+        sz = remaining;
+        r = msi_id2stringA( db->strings, i, data+remaining, &sz );
+        if( r != ERROR_SUCCESS )
+            goto err;
+        pool[ i*2 ] = sz;
+        pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
+        remaining -= sz;
+        if( remaining < 0 )
+        {
+            ERR("oops remaining %d < 0\n", remaining);
+            goto err;
+        }
+    }
+
+    if( remaining != 0 )
+    {
+        ERR("oops remaining %d != 0\n", remaining);
+        goto err;
+    }
+
+    /* write the streams */
+    r = write_stream_data( db->storage, szStringData, data, datasize );
+    if( r )
+        goto err;
+    r = write_stream_data( db->storage, szStringPool, pool, poolsize );
+    if( r )
+        goto err;
+
+    ret = ERROR_SUCCESS;
+
+err:
+    if( data )
+        HeapFree( GetProcessHeap(), 0, data );
+    if( pool )
+        HeapFree( GetProcessHeap(), 0, pool );
 
     return ret;
 }
@@ -386,15 +618,6 @@
     return ret;
 }
 
-static inline UINT bytes_per_column( MSICOLUMNINFO *col )
-{
-    if( col->type & MSITYPE_STRING )
-        return 2;
-    if( (col->type & 0xff) > 4 )
-        ERR("Invalid column size!\n");
-    return col->type & 0xff;
-}
-
 /* information for default tables */
 const WCHAR szTables[]  = { '_','T','a','b','l','e','s',0 };
 const WCHAR szTable[]  = { 'T','a','b','l','e',0 };
@@ -456,21 +679,21 @@
     UINT sz=0, r;
     LPWSTR str;
 
-    r = msi_id2string( db->strings, stringid, NULL, &sz );
+    r = msi_id2stringW( db->strings, stringid, NULL, &sz );
     if( r != ERROR_SUCCESS )
         return NULL;
     sz ++; /* space for NUL char */
     str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
     if( !str )
         return str;
-    r = msi_id2string( db->strings, stringid, str, &sz );
+    r = msi_id2stringW( db->strings, stringid, str, &sz );
     if( r == ERROR_SUCCESS )
         return str;
     HeapFree(  GetProcessHeap(), 0, str );
     return NULL;
 }
 
-UINT get_tablecolumns( MSIDATABASE *db, 
+static UINT get_tablecolumns( MSIDATABASE *db, 
        LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
 {
     UINT r, i, n=0, table_id, count, maxcount = *sz;
@@ -500,18 +723,18 @@
 
     TRACE("Table id is %d\n", table_id);
 
-    count = table->size/8;
+    count = table->row_count;
     for( i=0; i<count; i++ )
     {
-        if( table->data[ i ] != table_id )
+        if( table->data[ i ][ 0 ] != table_id )
             continue;
         if( colinfo )
         {
-            UINT id = table->data[ i + count*2 ];
+            UINT id = table->data[ i ] [ 2 ];
             colinfo[n].tablename = MSI_makestring( db, table_id );
-            colinfo[n].number = table->data[ i + count ] - (1<<15);
+            colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
             colinfo[n].colname = MSI_makestring( db, id );
-            colinfo[n].type = table->data[ i + count*3 ];
+            colinfo[n].type = table->data[ i ] [ 3 ];
             /* this assumes that columns are in order in the table */
             if( n )
                 colinfo[n].offset = colinfo[n-1].offset
@@ -567,9 +790,10 @@
         return FALSE;
     }
 
-    count = table->size/2;
+    /* count = table->size/2; */
+    count = table->row_count;
     for( i=0; i<count; i++ )
-        if( table->data[ i ] == table_id )
+        if( table->data[ i ][ 0 ] == table_id )
             break;
 
     release_table( db, table );
@@ -593,7 +817,6 @@
     WCHAR          name[1];
 } MSITABLEVIEW;
 
-
 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
@@ -606,7 +829,7 @@
         return ERROR_INVALID_PARAMETER;
 
     /* how many rows are there ? */
-    num_rows = tv->table->size / tv->row_size;
+    num_rows = tv->table->row_count;
     if( row >= num_rows )
         return ERROR_NO_MORE_ITEMS;
 
@@ -622,12 +845,12 @@
     switch( n )
     {
     case 4:
-        offset = row*2 + (tv->columns[col-1].offset/2) * num_rows;
-        *val = tv->table->data[offset] + (tv->table->data[offset + 1] << 16);
+        offset = tv->columns[col-1].offset/2;
+        *val = tv->table->data[row][offset] + (tv->table->data[row][offset + 1] << 16);
         break;
     case 2:
-        offset = row + (tv->columns[col-1].offset/2) * num_rows;
-        *val = tv->table->data[offset];
+        offset = tv->columns[col-1].offset/2;
+        *val = tv->table->data[row][offset];
         break;
     default:
         ERR("oops! what is %d bytes per column?\n", n );
@@ -683,7 +906,7 @@
     {
         if( !tv->table )
             return ERROR_INVALID_PARAMETER;
-        *rows = tv->table->size / tv->row_size;
+        *rows = tv->table->row_count;
     }
 
     return ERROR_SUCCESS;
@@ -812,6 +1035,26 @@
 
     *view = (MSIVIEW*) tv;
     lstrcpyW( tv->name, name );
+
+    return ERROR_SUCCESS;
+}
+
+UINT MSI_CommitTables( MSIDATABASE *db )
+{
+    UINT r;
+    MSITABLE *table = NULL;
+
+    save_string_table( db );
+
+    for( table = db->first_table; table; table = table->next )
+    {
+        r = save_table( db, table );
+        if( r != ERROR_SUCCESS )
+            return r;
+    }
+
+    /* force everything to reload next time */
+    free_cached_tables( db );
 
     return ERROR_SUCCESS;
 }


More information about the wine-patches mailing list