2004-03-17 21:49:59 +01:00
|
|
|
/*
|
2007-04-23 09:18:27 +02:00
|
|
|
* String Table Functions
|
2004-03-17 21:49:59 +01:00
|
|
|
*
|
2004-03-19 02:16:36 +01:00
|
|
|
* Copyright 2002-2004, Mike McCormack for CodeWeavers
|
2007-04-23 09:18:27 +02:00
|
|
|
* Copyright 2007 Robert Shearman for CodeWeavers
|
2004-03-17 21:49:59 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-03-17 21:49:59 +01:00
|
|
|
*/
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
#define COBJMACROS
|
|
|
|
|
2004-03-17 21:49:59 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "wine/debug.h"
|
2004-06-30 20:18:27 +02:00
|
|
|
#include "wine/unicode.h"
|
2004-03-17 21:49:59 +01:00
|
|
|
#include "msi.h"
|
|
|
|
#include "msiquery.h"
|
|
|
|
#include "objbase.h"
|
|
|
|
#include "objidl.h"
|
|
|
|
#include "msipriv.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
|
|
|
|
#include "query.h"
|
|
|
|
|
2005-11-02 15:24:21 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2006-09-08 09:20:56 +02:00
|
|
|
#define HASH_SIZE 0x101
|
2007-06-08 01:41:51 +02:00
|
|
|
#define LONG_STR_BYTES 3
|
2006-04-03 11:48:52 +02:00
|
|
|
|
2004-03-17 21:49:59 +01:00
|
|
|
typedef struct _msistring
|
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
int hash_next;
|
2007-04-23 09:17:00 +02:00
|
|
|
UINT persistent_refcount;
|
|
|
|
UINT nonpersistent_refcount;
|
2004-06-30 20:18:27 +02:00
|
|
|
LPWSTR str;
|
2004-03-17 21:49:59 +01:00
|
|
|
} msistring;
|
|
|
|
|
|
|
|
struct string_table
|
|
|
|
{
|
2004-06-30 20:24:33 +02:00
|
|
|
UINT maxcount; /* the number of strings */
|
2004-03-17 21:49:59 +01:00
|
|
|
UINT freeslot;
|
2004-06-26 02:18:36 +02:00
|
|
|
UINT codepage;
|
2006-04-03 11:48:52 +02:00
|
|
|
int hash[HASH_SIZE];
|
2004-03-17 21:49:59 +01:00
|
|
|
msistring *strings; /* an array of strings (in the tree) */
|
|
|
|
};
|
|
|
|
|
2004-08-06 19:30:20 +02:00
|
|
|
static UINT msistring_makehash( const WCHAR *str )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2004-08-06 19:30:20 +02:00
|
|
|
UINT hash = 0;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2004-07-30 01:59:15 +02:00
|
|
|
if (str==NULL)
|
|
|
|
return hash;
|
|
|
|
|
2004-03-17 21:49:59 +01:00
|
|
|
while( *str )
|
|
|
|
{
|
|
|
|
hash ^= *str++;
|
|
|
|
hash *= 53;
|
2004-07-30 01:59:15 +02:00
|
|
|
hash = (hash<<5) | (hash>>27);
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
2006-04-03 11:48:52 +02:00
|
|
|
return hash % HASH_SIZE;
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:18:27 +02:00
|
|
|
static string_table *init_stringtable( int entries, UINT codepage )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
|
|
|
string_table *st;
|
2006-04-03 11:48:52 +02:00
|
|
|
int i;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2007-06-26 23:24:05 +02:00
|
|
|
if (codepage != CP_ACP && !IsValidCodePage(codepage))
|
|
|
|
{
|
|
|
|
ERR("invalid codepage %d\n", codepage);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:57:19 +02:00
|
|
|
st = msi_alloc( sizeof (string_table) );
|
2004-03-17 21:49:59 +01:00
|
|
|
if( !st )
|
|
|
|
return NULL;
|
2005-09-26 11:54:56 +02:00
|
|
|
if( entries < 1 )
|
|
|
|
entries = 1;
|
2005-09-20 13:57:19 +02:00
|
|
|
st->strings = msi_alloc_zero( sizeof (msistring) * entries );
|
2006-04-07 06:30:40 +02:00
|
|
|
if( !st->strings )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2005-09-20 13:57:19 +02:00
|
|
|
msi_free( st );
|
2004-03-17 21:49:59 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-30 20:24:33 +02:00
|
|
|
st->maxcount = entries;
|
2004-03-19 02:16:36 +01:00
|
|
|
st->freeslot = 1;
|
2004-06-26 02:18:36 +02:00
|
|
|
st->codepage = codepage;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2006-04-03 11:48:52 +02:00
|
|
|
for( i=0; i<HASH_SIZE; i++ )
|
|
|
|
st->hash[i] = -1;
|
|
|
|
|
2004-03-17 21:49:59 +01:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID msi_destroy_stringtable( string_table *st )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
for( i=0; i<st->maxcount; i++ )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if( st->strings[i].persistent_refcount ||
|
|
|
|
st->strings[i].nonpersistent_refcount )
|
2005-09-20 13:57:19 +02:00
|
|
|
msi_free( st->strings[i].str );
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
2005-09-20 13:57:19 +02:00
|
|
|
msi_free( st->strings );
|
|
|
|
msi_free( st );
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int st_find_free_entry( string_table *st )
|
|
|
|
{
|
2004-08-06 19:30:20 +02:00
|
|
|
UINT i, sz;
|
2004-03-19 02:16:36 +01:00
|
|
|
msistring *p;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
TRACE("%p\n", st);
|
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
if( st->freeslot )
|
|
|
|
{
|
|
|
|
for( i = st->freeslot; i < st->maxcount; i++ )
|
2007-04-23 09:17:00 +02:00
|
|
|
if( !st->strings[i].persistent_refcount &&
|
|
|
|
!st->strings[i].nonpersistent_refcount )
|
2004-06-30 20:24:33 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
for( i = 1; i < st->maxcount; i++ )
|
2007-04-23 09:17:00 +02:00
|
|
|
if( !st->strings[i].persistent_refcount &&
|
|
|
|
!st->strings[i].nonpersistent_refcount )
|
2004-03-17 21:49:59 +01:00
|
|
|
return i;
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
/* dynamically resize */
|
2004-06-30 20:24:33 +02:00
|
|
|
sz = st->maxcount + 1 + st->maxcount/2;
|
2005-09-20 13:57:19 +02:00
|
|
|
p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !p )
|
|
|
|
return -1;
|
|
|
|
st->strings = p;
|
2004-06-30 20:24:33 +02:00
|
|
|
st->freeslot = st->maxcount;
|
|
|
|
st->maxcount = sz;
|
2007-04-23 09:17:00 +02:00
|
|
|
if( st->strings[st->freeslot].persistent_refcount ||
|
|
|
|
st->strings[st->freeslot].nonpersistent_refcount )
|
2004-03-19 02:16:36 +01:00
|
|
|
ERR("oops. expected freeslot to be free...\n");
|
|
|
|
return st->freeslot;
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
static void set_st_entry( string_table *st, UINT n, LPWSTR str, UINT refcount, enum StringPersistence persistence )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
UINT hash = msistring_makehash( str );
|
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
if (persistence == StringPersistent)
|
|
|
|
{
|
|
|
|
st->strings[n].persistent_refcount = refcount;
|
|
|
|
st->strings[n].nonpersistent_refcount = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st->strings[n].persistent_refcount = 0;
|
|
|
|
st->strings[n].nonpersistent_refcount = refcount;
|
|
|
|
}
|
|
|
|
|
2006-04-03 11:48:52 +02:00
|
|
|
st->strings[n].str = str;
|
|
|
|
|
|
|
|
st->strings[n].hash_next = st->hash[hash];
|
|
|
|
st->hash[hash] = n;
|
|
|
|
|
|
|
|
if( n < st->maxcount )
|
|
|
|
st->freeslot = n + 1;
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:18:27 +02:00
|
|
|
static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
LPWSTR str;
|
2004-06-30 20:18:27 +02:00
|
|
|
int sz;
|
|
|
|
|
2004-06-30 20:42:02 +02:00
|
|
|
if( !data )
|
|
|
|
return 0;
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !data[0] )
|
|
|
|
return 0;
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if( st->strings[n].persistent_refcount ||
|
|
|
|
st->strings[n].nonpersistent_refcount )
|
2004-03-19 02:16:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
|
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if (persistence == StringPersistent)
|
|
|
|
st->strings[n].persistent_refcount += refcount;
|
|
|
|
else
|
|
|
|
st->strings[n].nonpersistent_refcount += refcount;
|
2004-03-19 02:16:36 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n = st_find_free_entry( st );
|
|
|
|
if( n < 0 )
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
if( n < 1 )
|
|
|
|
{
|
|
|
|
ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-17 21:49:59 +01:00
|
|
|
/* allocate a new string */
|
|
|
|
if( len < 0 )
|
2004-06-30 20:18:27 +02:00
|
|
|
len = strlen(data);
|
|
|
|
sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
|
2006-04-03 11:48:52 +02:00
|
|
|
str = msi_alloc( (sz+1)*sizeof(WCHAR) );
|
|
|
|
if( !str )
|
2004-03-17 21:49:59 +01:00
|
|
|
return -1;
|
2006-04-03 11:48:52 +02:00
|
|
|
MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
|
|
|
|
str[sz] = 0;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
set_st_entry( st, n, str, refcount, persistence );
|
2004-03-17 21:49:59 +01:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
LPWSTR str;
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
/* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
|
|
|
|
|
2004-06-30 20:42:02 +02:00
|
|
|
if( !data )
|
|
|
|
return 0;
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !data[0] )
|
|
|
|
return 0;
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if( st->strings[n].persistent_refcount ||
|
|
|
|
st->strings[n].nonpersistent_refcount )
|
2004-03-19 02:16:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-06-30 20:18:27 +02:00
|
|
|
if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if (persistence == StringPersistent)
|
|
|
|
st->strings[n].persistent_refcount += refcount;
|
|
|
|
else
|
|
|
|
st->strings[n].nonpersistent_refcount += refcount;
|
2004-03-19 02:16:36 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n = st_find_free_entry( st );
|
|
|
|
if( n < 0 )
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
if( n < 1 )
|
|
|
|
{
|
|
|
|
ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
/* allocate a new string */
|
2004-06-30 20:18:27 +02:00
|
|
|
if(len<0)
|
|
|
|
len = strlenW(data);
|
|
|
|
TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
|
|
|
|
|
2006-04-03 11:48:52 +02:00
|
|
|
str = msi_alloc( (len+1)*sizeof(WCHAR) );
|
|
|
|
if( !str )
|
2004-03-19 02:16:36 +01:00
|
|
|
return -1;
|
2006-04-03 11:48:52 +02:00
|
|
|
memcpy( str, data, len*sizeof(WCHAR) );
|
|
|
|
str[len] = 0;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
set_st_entry( st, n, str, refcount, persistence );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2004-03-19 20:14:12 +01:00
|
|
|
/* find the string identified by an id - return null if there's none */
|
2007-06-12 22:51:36 +02:00
|
|
|
const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
|
2004-03-19 20:14:12 +01:00
|
|
|
{
|
2004-06-30 20:18:27 +02:00
|
|
|
static const WCHAR zero[] = { 0 };
|
2004-03-19 20:14:12 +01:00
|
|
|
if( id == 0 )
|
2004-06-30 20:18:27 +02:00
|
|
|
return zero;
|
2004-03-19 20:14:12 +01:00
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
if( id >= st->maxcount )
|
2004-03-19 20:14:12 +01:00
|
|
|
return NULL;
|
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
|
2004-03-19 20:14:12 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return st->strings[id].str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* msi_id2stringW
|
|
|
|
*
|
|
|
|
* [in] st - pointer to the string table
|
2004-06-15 22:26:45 +02:00
|
|
|
* [in] id - id of the string to retrieve
|
2004-03-19 20:14:12 +01:00
|
|
|
* [out] buffer - destination of the string
|
|
|
|
* [in/out] sz - number of bytes available in the buffer on input
|
|
|
|
* number of bytes used on output
|
|
|
|
*
|
|
|
|
* The size includes the terminating nul character. Short buffers
|
|
|
|
* will be filled, but not nul terminated.
|
|
|
|
*/
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_id2stringW( const string_table *st, UINT id, LPWSTR buffer, UINT *sz )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
|
|
|
UINT len;
|
2004-06-30 20:18:27 +02:00
|
|
|
const WCHAR *str;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
TRACE("Finding string %d of %d\n", id, st->maxcount);
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2004-06-26 02:11:08 +02:00
|
|
|
str = msi_string_lookup_id( st, id );
|
2004-03-19 20:14:12 +01:00
|
|
|
if( !str )
|
2004-03-18 05:04:08 +01:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
len = strlenW( str ) + 1;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
if( !buffer )
|
|
|
|
{
|
2004-03-19 20:14:12 +01:00
|
|
|
*sz = len;
|
2004-03-18 05:04:08 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
if( *sz < len )
|
|
|
|
*sz = len;
|
|
|
|
memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
|
|
|
|
*sz = len;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-03-19 20:14:12 +01:00
|
|
|
/*
|
|
|
|
* msi_id2stringA
|
|
|
|
*
|
|
|
|
* [in] st - pointer to the string table
|
2004-06-15 22:26:45 +02:00
|
|
|
* [in] id - id of the string to retrieve
|
2004-03-19 20:14:12 +01:00
|
|
|
* [out] buffer - destination of the UTF8 string
|
|
|
|
* [in/out] sz - number of bytes available in the buffer on input
|
|
|
|
* number of bytes used on output
|
|
|
|
*
|
|
|
|
* The size includes the terminating nul character. Short buffers
|
|
|
|
* will be filled, but not nul terminated.
|
|
|
|
*/
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
|
|
|
UINT len;
|
2004-06-30 20:18:27 +02:00
|
|
|
const WCHAR *str;
|
2004-06-30 20:24:33 +02:00
|
|
|
int n;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
TRACE("Finding string %d of %d\n", id, st->maxcount);
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2004-06-26 02:11:08 +02:00
|
|
|
str = msi_string_lookup_id( st, id );
|
2004-03-19 20:14:12 +01:00
|
|
|
if( !str )
|
2004-03-17 21:49:59 +01:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
|
2004-03-17 21:49:59 +01:00
|
|
|
|
|
|
|
if( !buffer )
|
|
|
|
{
|
|
|
|
*sz = len;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-06-30 20:24:33 +02:00
|
|
|
if( len > *sz )
|
|
|
|
{
|
|
|
|
n = strlenW( str ) + 1;
|
|
|
|
while( n && (len > *sz) )
|
|
|
|
len = WideCharToMultiByte( st->codepage, 0,
|
|
|
|
str, --n, NULL, 0, NULL, NULL );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
n = -1;
|
|
|
|
|
|
|
|
*sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
|
2004-03-17 21:49:59 +01:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-03-19 20:14:12 +01:00
|
|
|
/*
|
2004-06-30 20:18:27 +02:00
|
|
|
* msi_string2idW
|
2004-03-19 20:14:12 +01:00
|
|
|
*
|
|
|
|
* [in] st - pointer to the string table
|
2004-06-30 20:18:27 +02:00
|
|
|
* [in] str - string to find in the string table
|
2004-03-19 20:14:12 +01:00
|
|
|
* [out] id - id of the string, if found
|
|
|
|
*/
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
UINT n, hash = msistring_makehash( str );
|
|
|
|
msistring *se = st->strings;
|
2004-03-17 21:49:59 +01:00
|
|
|
|
2006-04-03 11:48:52 +02:00
|
|
|
for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
|
2004-03-17 21:49:59 +01:00
|
|
|
{
|
2006-04-03 11:48:52 +02:00
|
|
|
*id = n;
|
|
|
|
return ERROR_SUCCESS;
|
2004-03-17 21:49:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-03 11:48:52 +02:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
2004-03-19 02:16:36 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
|
|
|
DWORD sz;
|
|
|
|
UINT r = ERROR_INVALID_PARAMETER;
|
2004-06-30 20:18:27 +02:00
|
|
|
LPWSTR str;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2004-03-19 20:14:12 +01:00
|
|
|
if( buffer[0] == 0 )
|
|
|
|
{
|
|
|
|
*id = 0;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( sz <= 0 )
|
|
|
|
return r;
|
2005-09-20 13:57:19 +02:00
|
|
|
str = msi_alloc( sz*sizeof(WCHAR) );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !str )
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
2004-06-30 20:18:27 +02:00
|
|
|
MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
r = msi_string2idW( st, str, id );
|
2005-09-20 13:57:19 +02:00
|
|
|
msi_free( str );
|
2004-03-17 21:49:59 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_strcmp( const string_table *st, UINT lval, UINT rval, UINT *res )
|
2004-06-26 02:11:08 +02:00
|
|
|
{
|
2004-06-30 20:18:27 +02:00
|
|
|
const WCHAR *l_str, *r_str;
|
2004-06-26 02:11:08 +02:00
|
|
|
|
|
|
|
l_str = msi_string_lookup_id( st, lval );
|
|
|
|
if( !l_str )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
r_str = msi_string_lookup_id( st, rval );
|
|
|
|
if( !r_str )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* does this do the right thing for all UTF-8 strings? */
|
2004-06-30 20:18:27 +02:00
|
|
|
*res = strcmpW( l_str, r_str );
|
2004-06-26 02:11:08 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-06-12 22:51:36 +02:00
|
|
|
static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-10-18 05:57:18 +02:00
|
|
|
UINT i, len, holesize;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-04-23 09:17:00 +02:00
|
|
|
if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
|
2004-06-30 20:24:33 +02:00
|
|
|
ERR("oops. element 0 has a string\n");
|
2006-08-28 09:51:41 +02:00
|
|
|
|
|
|
|
*poolsize = 4;
|
|
|
|
*datasize = 0;
|
|
|
|
holesize = 0;
|
2004-06-30 20:24:33 +02:00
|
|
|
for( i=1; i<st->maxcount; i++ )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-04-23 09:17:00 +02:00
|
|
|
if( !st->strings[i].persistent_refcount )
|
2007-10-18 05:57:18 +02:00
|
|
|
{
|
|
|
|
TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
|
|
|
|
(*poolsize) += 4;
|
|
|
|
}
|
|
|
|
else if( st->strings[i].str )
|
2004-06-30 20:18:27 +02:00
|
|
|
{
|
2004-06-30 20:24:33 +02:00
|
|
|
TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
|
2004-06-30 20:18:27 +02:00
|
|
|
len = WideCharToMultiByte( st->codepage, 0,
|
|
|
|
st->strings[i].str, -1, NULL, 0, NULL, NULL);
|
|
|
|
if( len )
|
|
|
|
len--;
|
2006-08-28 09:51:41 +02:00
|
|
|
(*datasize) += len;
|
|
|
|
if (len>0xffff)
|
|
|
|
(*poolsize) += 4;
|
|
|
|
(*poolsize) += holesize + 4;
|
|
|
|
holesize = 0;
|
2004-06-30 20:18:27 +02:00
|
|
|
}
|
2006-08-28 09:51:41 +02:00
|
|
|
else
|
|
|
|
holesize += 4;
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
2006-08-28 09:51:41 +02:00
|
|
|
TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
|
2004-06-30 20:24:33 +02:00
|
|
|
}
|
2007-04-23 09:17:40 +02:00
|
|
|
|
|
|
|
static const WCHAR szStringData[] = {
|
|
|
|
'_','S','t','r','i','n','g','D','a','t','a',0 };
|
|
|
|
static const WCHAR szStringPool[] = {
|
|
|
|
'_','S','t','r','i','n','g','P','o','o','l',0 };
|
|
|
|
|
|
|
|
HRESULT msi_init_string_table( IStorage *stg )
|
|
|
|
{
|
|
|
|
USHORT zero[2] = { 0, 0 };
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
/* create the StringPool stream... add the zero string to it*/
|
2007-04-25 12:22:01 +02:00
|
|
|
ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
|
2007-04-23 09:17:40 +02:00
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
/* create the StringData stream... make it zero length */
|
2007-04-25 12:22:01 +02:00
|
|
|
ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
|
2007-04-23 09:17:40 +02:00
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:41:51 +02:00
|
|
|
string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
|
2007-04-23 09:17:40 +02:00
|
|
|
{
|
|
|
|
string_table *st = NULL;
|
|
|
|
CHAR *data = NULL;
|
|
|
|
USHORT *pool = NULL;
|
|
|
|
UINT r, datasize = 0, poolsize = 0, codepage;
|
|
|
|
DWORD i, count, offset, len, n, refs;
|
|
|
|
|
2007-07-16 23:45:29 +02:00
|
|
|
r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
|
2007-04-23 09:17:40 +02:00
|
|
|
if( r != ERROR_SUCCESS)
|
|
|
|
goto end;
|
2007-07-16 23:45:29 +02:00
|
|
|
r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
|
2007-04-23 09:17:40 +02:00
|
|
|
if( r != ERROR_SUCCESS)
|
|
|
|
goto end;
|
|
|
|
|
2007-06-26 23:24:05 +02:00
|
|
|
if ( (poolsize > 4) && (pool[1] & 0x8000) )
|
2007-06-08 01:41:51 +02:00
|
|
|
*bytes_per_strref = LONG_STR_BYTES;
|
|
|
|
else
|
|
|
|
*bytes_per_strref = sizeof(USHORT);
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
count = poolsize/4;
|
2007-06-26 23:24:05 +02:00
|
|
|
if( poolsize > 4 )
|
|
|
|
codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
|
2007-04-23 09:17:40 +02:00
|
|
|
else
|
|
|
|
codepage = CP_ACP;
|
2007-04-23 09:18:27 +02:00
|
|
|
st = init_stringtable( count, codepage );
|
2007-06-26 23:24:05 +02:00
|
|
|
if (!st)
|
|
|
|
goto end;
|
2007-04-23 09:17:40 +02:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
n = 1;
|
|
|
|
i = 1;
|
|
|
|
while( i<count )
|
|
|
|
{
|
|
|
|
/* the string reference count is always the second word */
|
|
|
|
refs = pool[i*2+1];
|
|
|
|
|
|
|
|
/* empty entries have two zeros, still have a string id */
|
|
|
|
if (pool[i*2] == 0 && refs == 0)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a string is over 64k, the previous string entry is made null
|
|
|
|
* and its the high word of the length is inserted in the null string's
|
|
|
|
* reference count field.
|
|
|
|
*/
|
|
|
|
if( pool[i*2] == 0)
|
|
|
|
{
|
|
|
|
len = (pool[i*2+3] << 16) + pool[i*2+2];
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = pool[i*2];
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (offset + len) > datasize )
|
|
|
|
{
|
|
|
|
ERR("string table corrupt?\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
|
|
|
|
if( r != n )
|
|
|
|
ERR("Failed to add string %d\n", n );
|
|
|
|
n++;
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( datasize != offset )
|
|
|
|
ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
|
|
|
|
|
|
|
|
TRACE("Loaded %d strings\n", count);
|
|
|
|
|
|
|
|
end:
|
|
|
|
msi_free( pool );
|
|
|
|
msi_free( data );
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2007-06-12 22:51:36 +02:00
|
|
|
UINT msi_save_string_table( const string_table *st, IStorage *storage )
|
2007-04-23 09:17:40 +02:00
|
|
|
{
|
2007-04-23 09:18:27 +02:00
|
|
|
UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
|
2007-04-23 09:17:40 +02:00
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
|
|
|
CHAR *data = NULL;
|
|
|
|
USHORT *pool = NULL;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
/* construct the new table in memory first */
|
2007-04-23 09:18:27 +02:00
|
|
|
string_totalsize( st, &datasize, &poolsize );
|
2007-04-23 09:17:40 +02:00
|
|
|
|
2007-04-23 09:18:27 +02:00
|
|
|
TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
|
2007-04-23 09:17:40 +02:00
|
|
|
|
|
|
|
pool = msi_alloc( poolsize );
|
|
|
|
if( ! pool )
|
|
|
|
{
|
|
|
|
WARN("Failed to alloc pool %d bytes\n", poolsize );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
data = msi_alloc( datasize );
|
|
|
|
if( ! data )
|
|
|
|
{
|
|
|
|
WARN("Failed to alloc data %d bytes\n", poolsize );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
used = 0;
|
2007-04-23 09:18:27 +02:00
|
|
|
codepage = st->codepage;
|
2007-04-23 09:17:40 +02:00
|
|
|
pool[0]=codepage&0xffff;
|
|
|
|
pool[1]=(codepage>>16);
|
|
|
|
n = 1;
|
2007-04-23 09:18:27 +02:00
|
|
|
for( i=1; i<st->maxcount; i++ )
|
2007-04-23 09:17:40 +02:00
|
|
|
{
|
2007-04-23 09:18:27 +02:00
|
|
|
if( !st->strings[i].persistent_refcount )
|
2007-10-18 05:57:18 +02:00
|
|
|
{
|
|
|
|
pool[ n*2 ] = 0;
|
|
|
|
pool[ n*2 + 1] = 0;
|
|
|
|
n++;
|
2007-04-23 09:17:40 +02:00
|
|
|
continue;
|
2007-10-18 05:57:18 +02:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
sz = datasize - used;
|
|
|
|
r = msi_id2stringA( st, i, data+used, &sz );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
ERR("failed to fetch string\n");
|
|
|
|
sz = 0;
|
|
|
|
}
|
|
|
|
if( sz && (sz < (datasize - used ) ) )
|
|
|
|
sz--;
|
|
|
|
|
|
|
|
if (sz)
|
2007-04-23 09:18:27 +02:00
|
|
|
pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
|
2007-04-23 09:17:40 +02:00
|
|
|
else
|
|
|
|
pool[ n*2 + 1 ] = 0;
|
|
|
|
if (sz < 0x10000)
|
|
|
|
{
|
|
|
|
pool[ n*2 ] = sz;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pool[ n*2 ] = 0;
|
|
|
|
pool[ n*2 + 2 ] = sz&0xffff;
|
|
|
|
pool[ n*2 + 3 ] = (sz>>16);
|
|
|
|
n += 2;
|
|
|
|
}
|
|
|
|
used += sz;
|
|
|
|
if( used > datasize )
|
|
|
|
{
|
|
|
|
ERR("oops overran %d >= %d\n", used, datasize);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( used != datasize )
|
|
|
|
{
|
|
|
|
ERR("oops used %d != datasize %d\n", used, datasize);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the streams */
|
2007-04-25 12:22:01 +02:00
|
|
|
r = write_stream_data( storage, szStringData, data, datasize, TRUE );
|
2007-04-23 09:17:40 +02:00
|
|
|
TRACE("Wrote StringData r=%08x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
2007-04-25 12:22:01 +02:00
|
|
|
r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
|
2007-04-23 09:17:40 +02:00
|
|
|
TRACE("Wrote StringPool r=%08x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
msi_free( data );
|
|
|
|
msi_free( pool );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|