2 * String Table Functions
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
43 #define LONG_STR_BYTES 3
45 typedef struct _msistring
47 USHORT persistent_refcount
;
48 USHORT nonpersistent_refcount
;
54 UINT maxcount
; /* the number of strings */
58 msistring
*strings
; /* an array of strings */
59 UINT
*sorted
; /* index */
62 static string_table
*init_stringtable( int entries
, UINT codepage
)
66 if (codepage
!= CP_ACP
&& !IsValidCodePage(codepage
))
68 ERR("invalid codepage %d\n", codepage
);
72 st
= msi_alloc( sizeof (string_table
) );
78 st
->strings
= msi_alloc_zero( sizeof (msistring
) * entries
);
85 st
->sorted
= msi_alloc( sizeof (UINT
) * entries
);
88 msi_free( st
->strings
);
93 st
->maxcount
= entries
;
95 st
->codepage
= codepage
;
101 VOID
msi_destroy_stringtable( string_table
*st
)
105 for( i
=0; i
<st
->maxcount
; i
++ )
107 if( st
->strings
[i
].persistent_refcount
||
108 st
->strings
[i
].nonpersistent_refcount
)
109 msi_free( st
->strings
[i
].str
);
111 msi_free( st
->strings
);
112 msi_free( st
->sorted
);
116 static int st_find_free_entry( string_table
*st
)
125 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
126 if( !st
->strings
[i
].persistent_refcount
&&
127 !st
->strings
[i
].nonpersistent_refcount
)
130 for( i
= 1; i
< st
->maxcount
; i
++ )
131 if( !st
->strings
[i
].persistent_refcount
&&
132 !st
->strings
[i
].nonpersistent_refcount
)
135 /* dynamically resize */
136 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
137 p
= msi_realloc_zero( st
->strings
, sz
*sizeof(msistring
) );
141 s
= msi_realloc( st
->sorted
, sz
*sizeof(UINT
) );
151 st
->freeslot
= st
->maxcount
;
153 if( st
->strings
[st
->freeslot
].persistent_refcount
||
154 st
->strings
[st
->freeslot
].nonpersistent_refcount
)
155 ERR("oops. expected freeslot to be free...\n");
159 static int find_insert_index( const string_table
*st
, UINT string_id
)
161 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
165 i
= (low
+ high
) / 2;
166 c
= lstrcmpW( st
->strings
[string_id
].str
, st
->strings
[st
->sorted
[i
]].str
);
173 return -1; /* already exists */
178 static void insert_string_sorted( string_table
*st
, UINT string_id
)
182 i
= find_insert_index( st
, string_id
);
186 memmove( &st
->sorted
[i
] + 1, &st
->sorted
[i
], (st
->sortcount
- i
) * sizeof(UINT
) );
187 st
->sorted
[i
] = string_id
;
191 static void set_st_entry( string_table
*st
, UINT n
, LPWSTR str
, USHORT refcount
, enum StringPersistence persistence
)
193 if (persistence
== StringPersistent
)
195 st
->strings
[n
].persistent_refcount
= refcount
;
196 st
->strings
[n
].nonpersistent_refcount
= 0;
200 st
->strings
[n
].persistent_refcount
= 0;
201 st
->strings
[n
].nonpersistent_refcount
= refcount
;
204 st
->strings
[n
].str
= str
;
206 insert_string_sorted( st
, n
);
208 if( n
< st
->maxcount
)
209 st
->freeslot
= n
+ 1;
212 static UINT
msi_string2idA( const string_table
*st
, LPCSTR buffer
, UINT
*id
)
215 UINT r
= ERROR_INVALID_PARAMETER
;
218 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
223 return ERROR_SUCCESS
;
226 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
229 str
= msi_alloc( sz
*sizeof(WCHAR
) );
231 return ERROR_NOT_ENOUGH_MEMORY
;
232 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
234 r
= msi_string2idW( st
, str
, id
);
240 static int msi_addstring( string_table
*st
, UINT n
, const CHAR
*data
, int len
, USHORT refcount
, enum StringPersistence persistence
)
251 if( st
->strings
[n
].persistent_refcount
||
252 st
->strings
[n
].nonpersistent_refcount
)
257 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
259 if (persistence
== StringPersistent
)
260 st
->strings
[n
].persistent_refcount
+= refcount
;
262 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
265 n
= st_find_free_entry( st
);
272 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
276 /* allocate a new string */
279 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
280 str
= msi_alloc( (sz
+1)*sizeof(WCHAR
) );
283 MultiByteToWideChar( st
->codepage
, 0, data
, len
, str
, sz
);
286 set_st_entry( st
, n
, str
, refcount
, persistence
);
291 int msi_addstringW( string_table
*st
, const WCHAR
*data
, int len
, USHORT refcount
, enum StringPersistence persistence
)
301 if( msi_string2idW( st
, data
, &n
) == ERROR_SUCCESS
)
303 if (persistence
== StringPersistent
)
304 st
->strings
[n
].persistent_refcount
+= refcount
;
306 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
310 n
= st_find_free_entry( st
);
314 /* allocate a new string */
317 TRACE("%s, n = %d len = %d\n", debugstr_w(data
), n
, len
);
319 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
322 memcpy( str
, data
, len
*sizeof(WCHAR
) );
325 set_st_entry( st
, n
, str
, refcount
, persistence
);
330 /* find the string identified by an id - return null if there's none */
331 const WCHAR
*msi_string_lookup_id( const string_table
*st
, UINT id
)
336 if( id
>= st
->maxcount
)
339 if( id
&& !st
->strings
[id
].persistent_refcount
&& !st
->strings
[id
].nonpersistent_refcount
)
342 return st
->strings
[id
].str
;
348 * [in] st - pointer to the string table
349 * [in] id - id of the string to retrieve
350 * [out] buffer - destination of the UTF8 string
351 * [in/out] sz - number of bytes available in the buffer on input
352 * number of bytes used on output
354 * The size includes the terminating nul character. Short buffers
355 * will be filled, but not nul terminated.
357 static UINT
msi_id2stringA( const string_table
*st
, UINT id
, LPSTR buffer
, UINT
*sz
)
363 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
365 str
= msi_string_lookup_id( st
, id
);
367 return ERROR_FUNCTION_FAILED
;
369 len
= WideCharToMultiByte( st
->codepage
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
374 return ERROR_SUCCESS
;
379 n
= strlenW( str
) + 1;
380 while( n
&& (len
> *sz
) )
381 len
= WideCharToMultiByte( st
->codepage
, 0,
382 str
, --n
, NULL
, 0, NULL
, NULL
);
387 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, n
, buffer
, len
, NULL
, NULL
);
389 return ERROR_SUCCESS
;
395 * [in] st - pointer to the string table
396 * [in] str - string to find in the string table
397 * [out] id - id of the string, if found
399 UINT
msi_string2idW( const string_table
*st
, LPCWSTR str
, UINT
*id
)
401 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
405 i
= (low
+ high
) / 2;
406 c
= lstrcmpW( str
, st
->strings
[st
->sorted
[i
]].str
);
415 return ERROR_SUCCESS
;
419 return ERROR_INVALID_PARAMETER
;
422 static void string_totalsize( const string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
424 UINT i
, len
, holesize
;
426 if( st
->strings
[0].str
|| st
->strings
[0].persistent_refcount
|| st
->strings
[0].nonpersistent_refcount
)
427 ERR("oops. element 0 has a string\n");
432 for( i
=1; i
<st
->maxcount
; i
++ )
434 if( !st
->strings
[i
].persistent_refcount
)
436 TRACE("[%u] nonpersistent = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
439 else if( st
->strings
[i
].str
)
441 TRACE("[%u] = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
442 len
= WideCharToMultiByte( st
->codepage
, 0,
443 st
->strings
[i
].str
, -1, NULL
, 0, NULL
, NULL
);
449 (*poolsize
) += holesize
+ 4;
455 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
458 static const WCHAR szStringData
[] = {
459 '_','S','t','r','i','n','g','D','a','t','a',0 };
460 static const WCHAR szStringPool
[] = {
461 '_','S','t','r','i','n','g','P','o','o','l',0 };
463 HRESULT
msi_init_string_table( IStorage
*stg
)
465 USHORT zero
[2] = { 0, 0 };
468 /* create the StringPool stream... add the zero string to it*/
469 ret
= write_stream_data(stg
, szStringPool
, zero
, sizeof zero
, TRUE
);
470 if (ret
!= ERROR_SUCCESS
)
473 /* create the StringData stream... make it zero length */
474 ret
= write_stream_data(stg
, szStringData
, NULL
, 0, TRUE
);
475 if (ret
!= ERROR_SUCCESS
)
481 string_table
*msi_load_string_table( IStorage
*stg
, UINT
*bytes_per_strref
)
483 string_table
*st
= NULL
;
486 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
487 DWORD i
, count
, offset
, len
, n
, refs
;
489 r
= read_stream_data( stg
, szStringPool
, TRUE
, (BYTE
**)&pool
, &poolsize
);
490 if( r
!= ERROR_SUCCESS
)
492 r
= read_stream_data( stg
, szStringData
, TRUE
, (BYTE
**)&data
, &datasize
);
493 if( r
!= ERROR_SUCCESS
)
496 if ( (poolsize
> 4) && (pool
[1] & 0x8000) )
497 *bytes_per_strref
= LONG_STR_BYTES
;
499 *bytes_per_strref
= sizeof(USHORT
);
503 codepage
= pool
[0] | ( (pool
[1] & ~0x8000) << 16 );
506 st
= init_stringtable( count
, codepage
);
515 /* the string reference count is always the second word */
518 /* empty entries have two zeros, still have a string id */
519 if (pool
[i
*2] == 0 && refs
== 0)
527 * If a string is over 64k, the previous string entry is made null
528 * and its the high word of the length is inserted in the null string's
529 * reference count field.
533 len
= (pool
[i
*2+3] << 16) + pool
[i
*2+2];
542 if ( (offset
+ len
) > datasize
)
544 ERR("string table corrupt?\n");
548 r
= msi_addstring( st
, n
, data
+offset
, len
, refs
, StringPersistent
);
550 ERR("Failed to add string %d\n", n
);
555 if ( datasize
!= offset
)
556 ERR("string table load failed! (%08x != %08x), please report\n", datasize
, offset
);
558 TRACE("Loaded %d strings\n", count
);
567 UINT
msi_save_string_table( const string_table
*st
, IStorage
*storage
)
569 UINT i
, datasize
= 0, poolsize
= 0, sz
, used
, r
, codepage
, n
;
570 UINT ret
= ERROR_FUNCTION_FAILED
;
576 /* construct the new table in memory first */
577 string_totalsize( st
, &datasize
, &poolsize
);
579 TRACE("%u %u %u\n", st
->maxcount
, datasize
, poolsize
);
581 pool
= msi_alloc( poolsize
);
584 WARN("Failed to alloc pool %d bytes\n", poolsize
);
587 data
= msi_alloc( datasize
);
590 WARN("Failed to alloc data %d bytes\n", poolsize
);
595 codepage
= st
->codepage
;
596 pool
[0]=codepage
&0xffff;
597 pool
[1]=(codepage
>>16);
599 for( i
=1; i
<st
->maxcount
; i
++ )
601 if( !st
->strings
[i
].persistent_refcount
)
609 sz
= datasize
- used
;
610 r
= msi_id2stringA( st
, i
, data
+used
, &sz
);
611 if( r
!= ERROR_SUCCESS
)
613 ERR("failed to fetch string\n");
616 if( sz
&& (sz
< (datasize
- used
) ) )
620 pool
[ n
*2 + 1 ] = st
->strings
[i
].persistent_refcount
;
631 pool
[ n
*2 + 2 ] = sz
&0xffff;
632 pool
[ n
*2 + 3 ] = (sz
>>16);
636 if( used
> datasize
)
638 ERR("oops overran %d >= %d\n", used
, datasize
);
643 if( used
!= datasize
)
645 ERR("oops used %d != datasize %d\n", used
, datasize
);
649 /* write the streams */
650 r
= write_stream_data( storage
, szStringData
, data
, datasize
, TRUE
);
651 TRACE("Wrote StringData r=%08x\n", r
);
654 r
= write_stream_data( storage
, szStringPool
, pool
, poolsize
, TRUE
);
655 TRACE("Wrote StringPool r=%08x\n", r
);