2006-02-17 00:04:10 +00:00
|
|
|
/*
|
2008-01-16 10:11:22 +00:00
|
|
|
* String Table Functions
|
2006-02-17 00:04:10 +00:00
|
|
|
*
|
|
|
|
* Copyright 2002-2004, Mike McCormack for CodeWeavers
|
2008-01-16 10:11:22 +00:00
|
|
|
* Copyright 2007 Robert Shearman for CodeWeavers
|
2010-10-22 13:18:11 +00:00
|
|
|
* Copyright 2010 Hans Leidekker for CodeWeavers
|
2006-02-17 00:04:10 +00: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-08-01 23:12:11 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-02-17 00:04:10 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-04 23:30:58 +00:00
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "msi.h"
|
|
|
|
#include "msiquery.h"
|
|
|
|
#include "objbase.h"
|
|
|
|
#include "objidl.h"
|
2006-02-17 00:04:10 +00:00
|
|
|
#include "msipriv.h"
|
2018-03-04 23:30:58 +00:00
|
|
|
#include "winnls.h"
|
|
|
|
|
|
|
|
#include "query.h"
|
2006-02-17 00:04:10 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
|
|
|
|
|
2012-01-21 17:19:12 +00:00
|
|
|
struct msistring
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2010-05-29 08:55:43 +00:00
|
|
|
USHORT persistent_refcount;
|
|
|
|
USHORT nonpersistent_refcount;
|
2012-12-09 19:43:59 +00:00
|
|
|
WCHAR *data;
|
|
|
|
int len;
|
2012-01-21 17:19:12 +00:00
|
|
|
};
|
2006-02-17 00:04:10 +00:00
|
|
|
|
|
|
|
struct string_table
|
|
|
|
{
|
|
|
|
UINT maxcount; /* the number of strings */
|
|
|
|
UINT freeslot;
|
|
|
|
UINT codepage;
|
2009-12-22 09:28:03 +00:00
|
|
|
UINT sortcount;
|
2012-01-21 17:19:12 +00:00
|
|
|
struct msistring *strings; /* an array of strings */
|
|
|
|
UINT *sorted; /* index */
|
2006-02-17 00:04:10 +00:00
|
|
|
};
|
|
|
|
|
2011-03-20 08:47:41 +00:00
|
|
|
static BOOL validate_codepage( UINT codepage )
|
|
|
|
{
|
|
|
|
if (codepage != CP_ACP && !IsValidCodePage( codepage ))
|
|
|
|
{
|
|
|
|
WARN("invalid codepage %u\n", codepage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-01-16 10:11:22 +00:00
|
|
|
static string_table *init_stringtable( int entries, UINT codepage )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
|
|
|
string_table *st;
|
|
|
|
|
2011-03-20 08:47:41 +00:00
|
|
|
if (!validate_codepage( codepage ))
|
2008-01-16 10:11:22 +00:00
|
|
|
return NULL;
|
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
st = msi_alloc( sizeof (string_table) );
|
|
|
|
if( !st )
|
2008-01-16 10:11:22 +00:00
|
|
|
return NULL;
|
2006-02-17 00:04:10 +00:00
|
|
|
if( entries < 1 )
|
|
|
|
entries = 1;
|
2009-12-22 09:28:03 +00:00
|
|
|
|
2012-01-21 17:19:12 +00:00
|
|
|
st->strings = msi_alloc_zero( sizeof(struct msistring) * entries );
|
2006-08-01 23:12:11 +00:00
|
|
|
if( !st->strings )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
|
|
|
msi_free( st );
|
2008-01-16 10:11:22 +00:00
|
|
|
return NULL;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
2009-12-22 09:28:03 +00:00
|
|
|
|
|
|
|
st->sorted = msi_alloc( sizeof (UINT) * entries );
|
|
|
|
if( !st->sorted )
|
|
|
|
{
|
|
|
|
msi_free( st->strings );
|
|
|
|
msi_free( st );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
st->maxcount = entries;
|
|
|
|
st->freeslot = 1;
|
|
|
|
st->codepage = codepage;
|
2009-12-22 09:28:03 +00:00
|
|
|
st->sortcount = 0;
|
2006-08-01 23:12:11 +00:00
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID msi_destroy_stringtable( string_table *st )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
for( i=0; i<st->maxcount; i++ )
|
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
if( st->strings[i].persistent_refcount ||
|
|
|
|
st->strings[i].nonpersistent_refcount )
|
2012-12-09 19:43:59 +00:00
|
|
|
msi_free( st->strings[i].data );
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
msi_free( st->strings );
|
2009-12-22 09:28:03 +00:00
|
|
|
msi_free( st->sorted );
|
2006-02-17 00:04:10 +00:00
|
|
|
msi_free( st );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int st_find_free_entry( string_table *st )
|
|
|
|
{
|
2009-12-22 09:28:03 +00:00
|
|
|
UINT i, sz, *s;
|
2012-01-21 17:19:12 +00:00
|
|
|
struct msistring *p;
|
2006-02-17 00:04:10 +00:00
|
|
|
|
|
|
|
TRACE("%p\n", st);
|
|
|
|
|
|
|
|
if( st->freeslot )
|
|
|
|
{
|
|
|
|
for( i = st->freeslot; i < st->maxcount; i++ )
|
2008-01-16 10:11:22 +00:00
|
|
|
if( !st->strings[i].persistent_refcount &&
|
|
|
|
!st->strings[i].nonpersistent_refcount )
|
2006-02-17 00:04:10 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
for( i = 1; i < st->maxcount; i++ )
|
2008-01-16 10:11:22 +00:00
|
|
|
if( !st->strings[i].persistent_refcount &&
|
|
|
|
!st->strings[i].nonpersistent_refcount )
|
2006-02-17 00:04:10 +00:00
|
|
|
return i;
|
|
|
|
|
|
|
|
/* dynamically resize */
|
|
|
|
sz = st->maxcount + 1 + st->maxcount/2;
|
2012-01-21 17:19:12 +00:00
|
|
|
p = msi_realloc_zero( st->strings, sz * sizeof(struct msistring) );
|
2006-02-17 00:04:10 +00:00
|
|
|
if( !p )
|
|
|
|
return -1;
|
2009-12-22 09:28:03 +00:00
|
|
|
|
|
|
|
s = msi_realloc( st->sorted, sz*sizeof(UINT) );
|
|
|
|
if( !s )
|
|
|
|
{
|
|
|
|
msi_free( p );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
st->strings = p;
|
2009-12-22 09:28:03 +00:00
|
|
|
st->sorted = s;
|
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
st->freeslot = st->maxcount;
|
|
|
|
st->maxcount = sz;
|
2008-01-16 10:11:22 +00:00
|
|
|
if( st->strings[st->freeslot].persistent_refcount ||
|
|
|
|
st->strings[st->freeslot].nonpersistent_refcount )
|
2006-02-17 00:04:10 +00:00
|
|
|
ERR("oops. expected freeslot to be free...\n");
|
|
|
|
return st->freeslot;
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
static inline int cmp_string( const WCHAR *str1, int len1, const WCHAR *str2, int len2 )
|
|
|
|
{
|
|
|
|
if (len1 < len2) return -1;
|
|
|
|
else if (len1 > len2) return 1;
|
|
|
|
while (len1)
|
|
|
|
{
|
|
|
|
if (*str1 == *str2) { str1++; str2++; }
|
|
|
|
else return *str1 - *str2;
|
|
|
|
len1--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-22 09:28:03 +00:00
|
|
|
static int find_insert_index( const string_table *st, UINT string_id )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2009-12-22 09:28:03 +00:00
|
|
|
int i, c, low = 0, high = st->sortcount - 1;
|
|
|
|
|
|
|
|
while (low <= high)
|
|
|
|
{
|
|
|
|
i = (low + high) / 2;
|
2012-12-09 19:43:59 +00:00
|
|
|
c = cmp_string( st->strings[string_id].data, st->strings[string_id].len,
|
|
|
|
st->strings[st->sorted[i]].data, st->strings[st->sorted[i]].len );
|
2009-12-22 09:28:03 +00:00
|
|
|
if (c < 0)
|
|
|
|
high = i - 1;
|
|
|
|
else if (c > 0)
|
|
|
|
low = i + 1;
|
|
|
|
else
|
|
|
|
return -1; /* already exists */
|
|
|
|
}
|
|
|
|
return high + 1;
|
|
|
|
}
|
2006-08-01 23:12:11 +00:00
|
|
|
|
2009-12-22 09:28:03 +00:00
|
|
|
static void insert_string_sorted( string_table *st, UINT string_id )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = find_insert_index( st, string_id );
|
|
|
|
if (i == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
|
|
|
|
st->sorted[i] = string_id;
|
|
|
|
st->sortcount++;
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
static void set_st_entry( string_table *st, UINT n, WCHAR *str, int len, USHORT refcount,
|
|
|
|
enum StringPersistence persistence )
|
2009-12-22 09:28:03 +00:00
|
|
|
{
|
2008-01-16 10:11:22 +00: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;
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
st->strings[n].data = str;
|
|
|
|
st->strings[n].len = len;
|
2006-08-01 23:12:11 +00:00
|
|
|
|
2009-12-22 09:28:03 +00:00
|
|
|
insert_string_sorted( st, n );
|
2006-08-01 23:12:11 +00:00
|
|
|
|
|
|
|
if( n < st->maxcount )
|
|
|
|
st->freeslot = n + 1;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 20:28:19 +00:00
|
|
|
static UINT string2id( const string_table *st, const char *buffer, UINT *id )
|
2009-01-18 12:52:01 +00:00
|
|
|
{
|
|
|
|
DWORD sz;
|
|
|
|
UINT r = ERROR_INVALID_PARAMETER;
|
|
|
|
LPWSTR str;
|
|
|
|
|
|
|
|
TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
|
|
|
|
|
|
|
|
if( buffer[0] == 0 )
|
|
|
|
{
|
|
|
|
*id = 0;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
|
|
|
|
if( sz <= 0 )
|
|
|
|
return r;
|
|
|
|
str = msi_alloc( sz*sizeof(WCHAR) );
|
|
|
|
if( !str )
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
r = msi_string2id( st, str, sz - 1, id );
|
2009-01-18 12:52:01 +00:00
|
|
|
msi_free( str );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-03-09 20:28:19 +00:00
|
|
|
static int add_string( string_table *st, UINT n, const char *data, UINT len, USHORT refcount, enum StringPersistence persistence )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2006-08-01 23:12:11 +00:00
|
|
|
LPWSTR str;
|
2006-02-17 00:04:10 +00:00
|
|
|
int sz;
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
if( !data || !len )
|
2006-02-17 00:04:10 +00:00
|
|
|
return 0;
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
if( st->strings[n].persistent_refcount ||
|
|
|
|
st->strings[n].nonpersistent_refcount )
|
2006-02-17 00:04:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-09 20:28:19 +00:00
|
|
|
if (string2id( st, data, &n ) == ERROR_SUCCESS)
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
if (persistence == StringPersistent)
|
|
|
|
st->strings[n].persistent_refcount += refcount;
|
|
|
|
else
|
|
|
|
st->strings[n].nonpersistent_refcount += refcount;
|
2006-02-17 00:04:10 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n = st_find_free_entry( st );
|
2008-01-16 10:11:22 +00:00
|
|
|
if( n == -1 )
|
2006-02-17 00:04:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( n < 1 )
|
|
|
|
{
|
|
|
|
ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a new string */
|
|
|
|
sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
|
2006-08-01 23:12:11 +00:00
|
|
|
str = msi_alloc( (sz+1)*sizeof(WCHAR) );
|
|
|
|
if( !str )
|
2006-02-17 00:04:10 +00:00
|
|
|
return -1;
|
2006-08-01 23:12:11 +00:00
|
|
|
MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
|
|
|
|
str[sz] = 0;
|
2006-02-17 00:04:10 +00:00
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
set_st_entry( st, n, str, sz, refcount, persistence );
|
2006-02-17 00:04:10 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-03-09 20:28:19 +00:00
|
|
|
int msi_add_string( string_table *st, const WCHAR *data, int len, enum StringPersistence persistence )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2010-05-29 08:55:43 +00:00
|
|
|
UINT n;
|
2006-08-01 23:12:11 +00:00
|
|
|
LPWSTR str;
|
|
|
|
|
2006-02-17 00:04:10 +00:00
|
|
|
if( !data )
|
|
|
|
return 0;
|
2012-12-09 19:43:59 +00:00
|
|
|
|
|
|
|
if (len < 0) len = strlenW( data );
|
|
|
|
|
|
|
|
if( !data[0] && !len )
|
2006-02-17 00:04:10 +00:00
|
|
|
return 0;
|
2010-05-29 08:55:43 +00:00
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
if (msi_string2id( st, data, len, &n) == ERROR_SUCCESS )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2010-05-29 08:55:43 +00:00
|
|
|
if (persistence == StringPersistent)
|
2015-03-09 20:28:19 +00:00
|
|
|
st->strings[n].persistent_refcount++;
|
2010-05-29 08:55:43 +00:00
|
|
|
else
|
2015-03-09 20:28:19 +00:00
|
|
|
st->strings[n].nonpersistent_refcount++;
|
2010-05-29 08:55:43 +00:00
|
|
|
return n;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 08:55:43 +00:00
|
|
|
n = st_find_free_entry( st );
|
|
|
|
if( n == -1 )
|
2006-02-17 00:04:10 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* allocate a new string */
|
2012-12-09 19:43:59 +00:00
|
|
|
TRACE( "%s, n = %d len = %d\n", debugstr_wn(data, len), n, len );
|
2006-02-17 00:04:10 +00:00
|
|
|
|
2006-08-01 23:12:11 +00:00
|
|
|
str = msi_alloc( (len+1)*sizeof(WCHAR) );
|
|
|
|
if( !str )
|
2006-02-17 00:04:10 +00:00
|
|
|
return -1;
|
2006-08-01 23:12:11 +00:00
|
|
|
memcpy( str, data, len*sizeof(WCHAR) );
|
|
|
|
str[len] = 0;
|
2006-02-17 00:04:10 +00:00
|
|
|
|
2015-03-09 20:28:19 +00:00
|
|
|
set_st_entry( st, n, str, len, 1, persistence );
|
2006-02-17 00:04:10 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the string identified by an id - return null if there's none */
|
2012-12-09 19:43:59 +00:00
|
|
|
const WCHAR *msi_string_lookup( const string_table *st, UINT id, int *len )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
|
|
|
if( id == 0 )
|
2012-12-09 19:43:59 +00:00
|
|
|
{
|
|
|
|
if (len) *len = 0;
|
2009-10-25 11:06:09 +00:00
|
|
|
return szEmpty;
|
2012-12-09 19:43:59 +00:00
|
|
|
}
|
2006-02-17 00:04:10 +00:00
|
|
|
if( id >= st->maxcount )
|
|
|
|
return NULL;
|
|
|
|
|
2008-01-16 10:11:22 +00:00
|
|
|
if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
|
2006-02-17 00:04:10 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
if (len) *len = st->strings[id].len;
|
|
|
|
|
|
|
|
return st->strings[id].data;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-09 20:28:19 +00:00
|
|
|
* id2string
|
2006-02-17 00:04:10 +00:00
|
|
|
*
|
|
|
|
* [in] st - pointer to the string table
|
|
|
|
* [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
|
|
|
|
*
|
2012-08-13 16:17:18 +00:00
|
|
|
* Returned string is not nul terminated.
|
2006-02-17 00:04:10 +00:00
|
|
|
*/
|
2015-03-09 20:28:19 +00:00
|
|
|
static UINT id2string( const string_table *st, UINT id, char *buffer, UINT *sz )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2012-12-09 19:43:59 +00:00
|
|
|
int len, lenW;
|
2006-02-17 00:04:10 +00:00
|
|
|
const WCHAR *str;
|
|
|
|
|
|
|
|
TRACE("Finding string %d of %d\n", id, st->maxcount);
|
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
str = msi_string_lookup( st, id, &lenW );
|
2006-02-17 00:04:10 +00:00
|
|
|
if( !str )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2012-08-13 16:17:18 +00:00
|
|
|
len = WideCharToMultiByte( st->codepage, 0, str, lenW, NULL, 0, NULL, NULL );
|
|
|
|
if( *sz < len )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
|
|
|
*sz = len;
|
2012-08-13 16:17:18 +00:00
|
|
|
return ERROR_MORE_DATA;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
2012-08-13 16:17:18 +00:00
|
|
|
*sz = WideCharToMultiByte( st->codepage, 0, str, lenW, buffer, *sz, NULL, NULL );
|
2006-02-17 00:04:10 +00:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-12-09 19:43:59 +00:00
|
|
|
* msi_string2id
|
2006-02-17 00:04:10 +00:00
|
|
|
*
|
|
|
|
* [in] st - pointer to the string table
|
|
|
|
* [in] str - string to find in the string table
|
|
|
|
* [out] id - id of the string, if found
|
|
|
|
*/
|
2012-12-09 19:43:59 +00:00
|
|
|
UINT msi_string2id( const string_table *st, const WCHAR *str, int len, UINT *id )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2009-12-22 09:28:03 +00:00
|
|
|
int i, c, low = 0, high = st->sortcount - 1;
|
2006-02-17 00:04:10 +00:00
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
if (len < 0) len = strlenW( str );
|
|
|
|
|
2009-12-22 09:28:03 +00:00
|
|
|
while (low <= high)
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2009-12-22 09:28:03 +00:00
|
|
|
i = (low + high) / 2;
|
2012-12-09 19:43:59 +00:00
|
|
|
c = cmp_string( str, len, st->strings[st->sorted[i]].data, st->strings[st->sorted[i]].len );
|
2009-12-22 09:28:03 +00:00
|
|
|
|
|
|
|
if (c < 0)
|
|
|
|
high = i - 1;
|
|
|
|
else if (c > 0)
|
|
|
|
low = i + 1;
|
|
|
|
else
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2009-12-22 09:28:03 +00:00
|
|
|
*id = st->sorted[i];
|
2006-08-01 23:12:11 +00:00
|
|
|
return ERROR_SUCCESS;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-01 23:12:11 +00:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 10:11:22 +00:00
|
|
|
static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
UINT i, len, holesize;
|
2006-02-17 00:04:10 +00:00
|
|
|
|
2012-12-09 19:43:59 +00:00
|
|
|
if( st->strings[0].data || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
|
2006-02-17 00:04:10 +00:00
|
|
|
ERR("oops. element 0 has a string\n");
|
2006-08-30 19:24:26 +00:00
|
|
|
|
|
|
|
*poolsize = 4;
|
|
|
|
*datasize = 0;
|
|
|
|
holesize = 0;
|
2006-02-17 00:04:10 +00:00
|
|
|
for( i=1; i<st->maxcount; i++ )
|
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
if( !st->strings[i].persistent_refcount )
|
|
|
|
{
|
2012-12-09 19:43:59 +00:00
|
|
|
TRACE("[%u] nonpersistent = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
|
2008-01-16 10:11:22 +00:00
|
|
|
(*poolsize) += 4;
|
|
|
|
}
|
2012-12-09 19:43:59 +00:00
|
|
|
else if( st->strings[i].data )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2012-12-09 19:43:59 +00:00
|
|
|
TRACE("[%u] = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
|
|
|
|
len = WideCharToMultiByte( st->codepage, 0, st->strings[i].data, st->strings[i].len + 1,
|
|
|
|
NULL, 0, NULL, NULL);
|
2006-02-17 00:04:10 +00:00
|
|
|
if( len )
|
|
|
|
len--;
|
2006-08-30 19:24:26 +00:00
|
|
|
(*datasize) += len;
|
|
|
|
if (len>0xffff)
|
|
|
|
(*poolsize) += 4;
|
|
|
|
(*poolsize) += holesize + 4;
|
|
|
|
holesize = 0;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
2006-08-30 19:24:26 +00:00
|
|
|
else
|
|
|
|
holesize += 4;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
2006-08-30 19:24:26 +00:00
|
|
|
TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 10:11:22 +00:00
|
|
|
HRESULT msi_init_string_table( IStorage *stg )
|
|
|
|
{
|
|
|
|
USHORT zero[2] = { 0, 0 };
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
/* create the StringPool stream... add the zero string to it*/
|
|
|
|
ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
|
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
/* create the StringData stream... make it zero length */
|
|
|
|
ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
|
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
|
|
|
|
{
|
|
|
|
string_table *st = NULL;
|
|
|
|
CHAR *data = NULL;
|
|
|
|
USHORT *pool = NULL;
|
|
|
|
UINT r, datasize = 0, poolsize = 0, codepage;
|
|
|
|
DWORD i, count, offset, len, n, refs;
|
|
|
|
|
|
|
|
r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
|
|
|
|
if( r != ERROR_SUCCESS)
|
|
|
|
goto end;
|
|
|
|
r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
|
|
|
|
if( r != ERROR_SUCCESS)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if ( (poolsize > 4) && (pool[1] & 0x8000) )
|
|
|
|
*bytes_per_strref = LONG_STR_BYTES;
|
|
|
|
else
|
|
|
|
*bytes_per_strref = sizeof(USHORT);
|
|
|
|
|
|
|
|
count = poolsize/4;
|
|
|
|
if( poolsize > 4 )
|
|
|
|
codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
|
|
|
|
else
|
|
|
|
codepage = CP_ACP;
|
|
|
|
st = init_stringtable( count, codepage );
|
|
|
|
if (!st)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-09 20:28:19 +00:00
|
|
|
r = add_string( st, n, data+offset, len, refs, StringPersistent );
|
2008-01-16 10:11:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-10-22 13:18:11 +00:00
|
|
|
UINT msi_save_string_table( const string_table *st, IStorage *storage, UINT *bytes_per_strref )
|
2006-02-17 00:04:10 +00:00
|
|
|
{
|
2008-01-16 10:11:22 +00:00
|
|
|
UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
|
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
|
|
|
CHAR *data = NULL;
|
|
|
|
USHORT *pool = NULL;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
/* construct the new table in memory first */
|
|
|
|
string_totalsize( st, &datasize, &poolsize );
|
|
|
|
|
|
|
|
TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
|
|
|
|
|
|
|
|
pool = msi_alloc( poolsize );
|
|
|
|
if( ! pool )
|
|
|
|
{
|
|
|
|
WARN("Failed to alloc pool %d bytes\n", poolsize );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
data = msi_alloc( datasize );
|
|
|
|
if( ! data )
|
|
|
|
{
|
2012-08-13 16:17:18 +00:00
|
|
|
WARN("Failed to alloc data %d bytes\n", datasize );
|
2008-01-16 10:11:22 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
used = 0;
|
|
|
|
codepage = st->codepage;
|
2010-10-22 13:18:11 +00:00
|
|
|
pool[0] = codepage & 0xffff;
|
|
|
|
pool[1] = codepage >> 16;
|
|
|
|
if (st->maxcount > 0xffff)
|
|
|
|
{
|
|
|
|
pool[1] |= 0x8000;
|
|
|
|
*bytes_per_strref = LONG_STR_BYTES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*bytes_per_strref = sizeof(USHORT);
|
|
|
|
|
2008-01-16 10:11:22 +00:00
|
|
|
n = 1;
|
|
|
|
for( i=1; i<st->maxcount; i++ )
|
|
|
|
{
|
|
|
|
if( !st->strings[i].persistent_refcount )
|
|
|
|
{
|
|
|
|
pool[ n*2 ] = 0;
|
|
|
|
pool[ n*2 + 1] = 0;
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = datasize - used;
|
2015-03-09 20:28:19 +00:00
|
|
|
r = id2string( st, i, data+used, &sz );
|
2008-01-16 10:11:22 +00:00
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
ERR("failed to fetch string\n");
|
|
|
|
sz = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sz)
|
|
|
|
pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
|
|
|
|
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 */
|
|
|
|
r = write_stream_data( storage, szStringData, data, datasize, TRUE );
|
|
|
|
TRACE("Wrote StringData r=%08x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
|
|
|
|
TRACE("Wrote StringPool r=%08x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
msi_free( data );
|
|
|
|
msi_free( pool );
|
|
|
|
|
|
|
|
return ret;
|
2006-02-17 00:04:10 +00:00
|
|
|
}
|
2011-03-20 08:47:41 +00:00
|
|
|
|
|
|
|
UINT msi_get_string_table_codepage( const string_table *st )
|
|
|
|
{
|
|
|
|
return st->codepage;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT msi_set_string_table_codepage( string_table *st, UINT codepage )
|
|
|
|
{
|
|
|
|
if (validate_codepage( codepage ))
|
|
|
|
{
|
|
|
|
st->codepage = codepage;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|