wine/dlls/msi/string.c

466 lines
11 KiB
C
Raw Normal View History

/*
* Implementation of the Microsoft Installer (msi.dll)
*
2004-03-19 01:16:36 +00:00
* Copyright 2002-2004, Mike McCormack for CodeWeavers
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include <assert.h>
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wine/debug.h"
2004-06-30 18:18:27 +00:00
#include "wine/unicode.h"
#include "msi.h"
#include "msiquery.h"
#include "objbase.h"
#include "objidl.h"
#include "msipriv.h"
#include "winnls.h"
#include "query.h"
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
2006-04-03 09:48:52 +00:00
#define HASH_SIZE 67
typedef struct _msistring
{
2006-04-03 09:48:52 +00:00
int hash_next;
UINT refcount;
2004-06-30 18:18:27 +00:00
LPWSTR str;
} msistring;
struct string_table
{
2004-06-30 18:24:33 +00:00
UINT maxcount; /* the number of strings */
UINT freeslot;
UINT codepage;
2006-04-03 09:48:52 +00:00
int hash[HASH_SIZE];
msistring *strings; /* an array of strings (in the tree) */
};
static UINT msistring_makehash( const WCHAR *str )
{
UINT hash = 0;
if (str==NULL)
return hash;
while( *str )
{
hash ^= *str++;
hash *= 53;
hash = (hash<<5) | (hash>>27);
}
2006-04-03 09:48:52 +00:00
return hash % HASH_SIZE;
}
string_table *msi_init_stringtable( int entries, UINT codepage )
{
string_table *st;
2006-04-03 09:48:52 +00:00
int i;
st = msi_alloc( sizeof (string_table) );
if( !st )
return NULL;
if( entries < 1 )
entries = 1;
st->strings = msi_alloc_zero( sizeof (msistring) * entries );
if( !st->strings )
{
msi_free( st );
return NULL;
}
2004-06-30 18:24:33 +00:00
st->maxcount = entries;
2004-03-19 01:16:36 +00:00
st->freeslot = 1;
st->codepage = codepage;
2006-04-03 09:48:52 +00:00
for( i=0; i<HASH_SIZE; i++ )
st->hash[i] = -1;
return st;
}
VOID msi_destroy_stringtable( string_table *st )
{
UINT i;
2004-06-30 18:24:33 +00:00
for( i=0; i<st->maxcount; i++ )
{
if( st->strings[i].refcount )
msi_free( st->strings[i].str );
}
msi_free( st->strings );
msi_free( st );
}
static int st_find_free_entry( string_table *st )
{
UINT i, sz;
2004-03-19 01:16:36 +00:00
msistring *p;
2004-06-30 18:18:27 +00:00
TRACE("%p\n", st);
2004-06-30 18:24:33 +00:00
if( st->freeslot )
{
for( i = st->freeslot; i < st->maxcount; i++ )
if( !st->strings[i].refcount )
return i;
}
for( i = 1; i < st->maxcount; i++ )
if( !st->strings[i].refcount )
return i;
2004-03-19 01:16:36 +00:00
/* dynamically resize */
2004-06-30 18:24:33 +00:00
sz = st->maxcount + 1 + st->maxcount/2;
p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
2004-03-19 01:16:36 +00:00
if( !p )
return -1;
st->strings = p;
2004-06-30 18:24:33 +00:00
st->freeslot = st->maxcount;
st->maxcount = sz;
2004-03-19 01:16:36 +00:00
if( st->strings[st->freeslot].refcount )
ERR("oops. expected freeslot to be free...\n");
return st->freeslot;
}
2006-04-03 09:48:52 +00:00
static void set_st_entry( string_table *st, UINT n, LPWSTR str )
{
2006-04-03 09:48:52 +00:00
UINT hash = msistring_makehash( str );
st->strings[n].refcount = 1;
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;
}
2005-07-06 10:33:30 +00:00
int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount )
{
2006-04-03 09:48:52 +00:00
LPWSTR str;
2004-06-30 18:18:27 +00:00
int sz;
if( !data )
return 0;
2004-03-19 01:16:36 +00:00
if( !data[0] )
return 0;
if( n > 0 )
{
if( st->strings[n].refcount )
return -1;
}
else
{
if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
{
st->strings[n].refcount++;
return n;
}
n = st_find_free_entry( st );
if( n < 0 )
return -1;
}
2004-06-30 18:24:33 +00:00
if( n < 1 )
{
ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
return -1;
}
/* allocate a new string */
if( len < 0 )
2004-06-30 18:18:27 +00:00
len = strlen(data);
sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
2006-04-03 09:48:52 +00:00
str = msi_alloc( (sz+1)*sizeof(WCHAR) );
if( !str )
return -1;
2006-04-03 09:48:52 +00:00
MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
str[sz] = 0;
2006-04-03 09:48:52 +00:00
set_st_entry( st, n, str );
return n;
}
2005-07-06 10:33:30 +00:00
int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount )
2004-03-19 01:16:36 +00:00
{
2006-04-03 09:48:52 +00:00
LPWSTR str;
2004-03-19 01:16:36 +00:00
/* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
if( !data )
return 0;
2004-03-19 01:16:36 +00:00
if( !data[0] )
return 0;
if( n > 0 )
{
if( st->strings[n].refcount )
return -1;
}
else
{
2004-06-30 18:18:27 +00:00
if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
2004-03-19 01:16:36 +00:00
{
st->strings[n].refcount++;
return n;
}
n = st_find_free_entry( st );
if( n < 0 )
return -1;
}
2004-06-30 18:24:33 +00:00
if( n < 1 )
{
ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
return -1;
}
2004-03-19 01:16:36 +00:00
/* allocate a new string */
2004-06-30 18:18:27 +00:00
if(len<0)
len = strlenW(data);
TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
2006-04-03 09:48:52 +00:00
str = msi_alloc( (len+1)*sizeof(WCHAR) );
if( !str )
2004-03-19 01:16:36 +00:00
return -1;
2004-06-30 18:18:27 +00:00
TRACE("%d\n",__LINE__);
2006-04-03 09:48:52 +00:00
memcpy( str, data, len*sizeof(WCHAR) );
str[len] = 0;
2004-03-19 01:16:36 +00:00
2006-04-03 09:48:52 +00:00
set_st_entry( st, n, str );
2004-03-19 01:16:36 +00:00
return n;
}
/* find the string identified by an id - return null if there's none */
2004-06-30 18:18:27 +00:00
const WCHAR *msi_string_lookup_id( string_table *st, UINT id )
{
2004-06-30 18:18:27 +00:00
static const WCHAR zero[] = { 0 };
if( id == 0 )
2004-06-30 18:18:27 +00:00
return zero;
2004-06-30 18:24:33 +00:00
if( id >= st->maxcount )
return NULL;
if( id && !st->strings[id].refcount )
return NULL;
return st->strings[id].str;
}
/*
* msi_id2stringW
*
* [in] st - pointer to the string table
2004-06-15 20:26:45 +00:00
* [in] id - id of the string to retrieve
* [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.
*/
UINT msi_id2stringW( string_table *st, UINT id, LPWSTR buffer, UINT *sz )
{
UINT len;
2004-06-30 18:18:27 +00:00
const WCHAR *str;
2004-06-30 18:24:33 +00:00
TRACE("Finding string %d of %d\n", id, st->maxcount);
2004-06-26 00:11:08 +00:00
str = msi_string_lookup_id( st, id );
if( !str )
return ERROR_FUNCTION_FAILED;
2004-06-30 18:18:27 +00:00
len = strlenW( str ) + 1;
if( !buffer )
{
*sz = len;
return ERROR_SUCCESS;
}
2004-06-30 18:18:27 +00:00
if( *sz < len )
*sz = len;
memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
*sz = len;
return ERROR_SUCCESS;
}
/*
* msi_id2stringA
*
* [in] st - pointer to the string table
2004-06-15 20:26:45 +00:00
* [in] id - id of the string to retrieve
* [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.
*/
UINT msi_id2stringA( string_table *st, UINT id, LPSTR buffer, UINT *sz )
{
UINT len;
2004-06-30 18:18:27 +00:00
const WCHAR *str;
2004-06-30 18:24:33 +00:00
int n;
2004-06-30 18:24:33 +00:00
TRACE("Finding string %d of %d\n", id, st->maxcount);
2004-06-26 00:11:08 +00:00
str = msi_string_lookup_id( st, id );
if( !str )
return ERROR_FUNCTION_FAILED;
2004-06-30 18:18:27 +00:00
len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
if( !buffer )
{
*sz = len;
return ERROR_SUCCESS;
}
2004-06-30 18:24:33 +00: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 );
return ERROR_SUCCESS;
}
/*
2004-06-30 18:18:27 +00:00
* msi_string2idW
*
* [in] st - pointer to the string table
2004-06-30 18:18:27 +00:00
* [in] str - string to find in the string table
* [out] id - id of the string, if found
*/
2004-06-30 18:18:27 +00:00
UINT msi_string2idW( string_table *st, LPCWSTR str, UINT *id )
{
2006-04-03 09:48:52 +00:00
UINT n, hash = msistring_makehash( str );
msistring *se = st->strings;
2006-04-03 09:48:52 +00:00
for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
{
2006-04-03 09:48:52 +00:00
if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
{
2006-04-03 09:48:52 +00:00
*id = n;
return ERROR_SUCCESS;
}
}
2006-04-03 09:48:52 +00:00
return ERROR_INVALID_PARAMETER;
2004-03-19 01:16:36 +00:00
}
2004-06-30 18:18:27 +00:00
UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
2004-03-19 01:16:36 +00:00
{
DWORD sz;
UINT r = ERROR_INVALID_PARAMETER;
2004-06-30 18:18:27 +00:00
LPWSTR str;
2004-03-19 01:16:36 +00:00
2004-06-30 18:18:27 +00:00
TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
2004-03-19 01:16:36 +00:00
if( buffer[0] == 0 )
{
*id = 0;
return ERROR_SUCCESS;
}
2004-06-30 18:18:27 +00:00
sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
2004-03-19 01:16:36 +00:00
if( sz <= 0 )
return r;
str = msi_alloc( sz*sizeof(WCHAR) );
2004-03-19 01:16:36 +00:00
if( !str )
return ERROR_NOT_ENOUGH_MEMORY;
2004-06-30 18:18:27 +00:00
MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
2004-03-19 01:16:36 +00:00
2004-06-30 18:18:27 +00:00
r = msi_string2idW( st, str, id );
msi_free( str );
return r;
}
2004-06-26 00:11:08 +00:00
UINT msi_strcmp( string_table *st, UINT lval, UINT rval, UINT *res )
{
2004-06-30 18:18:27 +00:00
const WCHAR *l_str, *r_str;
2004-06-26 00:11:08 +00: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 18:18:27 +00:00
*res = strcmpW( l_str, r_str );
2004-06-26 00:11:08 +00:00
return ERROR_SUCCESS;
}
2004-03-19 01:16:36 +00:00
UINT msi_string_count( string_table *st )
{
2004-06-30 18:24:33 +00:00
return st->maxcount;
}
UINT msi_id_refcount( string_table *st, UINT i )
{
2004-06-30 18:24:33 +00:00
if( i >= st->maxcount )
return 0;
return st->strings[i].refcount;
}
2004-06-30 18:24:33 +00:00
UINT msi_string_totalsize( string_table *st, UINT *total )
{
2004-06-30 18:18:27 +00:00
UINT size = 0, i, len;
2004-06-30 18:24:33 +00:00
if( st->strings[0].str || st->strings[0].refcount )
ERR("oops. element 0 has a string\n");
*total = 0;
for( i=1; i<st->maxcount; i++ )
{
if( st->strings[i].str )
2004-06-30 18:18:27 +00:00
{
2004-06-30 18:24:33 +00:00
TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
2004-06-30 18:18:27 +00:00
len = WideCharToMultiByte( st->codepage, 0,
st->strings[i].str, -1, NULL, 0, NULL, NULL);
if( len )
len--;
size += len;
2004-06-30 18:24:33 +00:00
*total = (i+1);
2004-06-30 18:18:27 +00:00
}
}
2004-06-30 18:24:33 +00:00
TRACE("%u/%u strings %u bytes codepage %x\n", *total, st->maxcount, size, st->codepage );
return size;
}
2004-06-30 18:24:33 +00:00
UINT msi_string_get_codepage( string_table *st )
{
return st->codepage;
}