2 * String Table Functions
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
6 * Copyright 2010 Hans Leidekker for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
46 USHORT persistent_refcount
;
47 USHORT nonpersistent_refcount
;
53 UINT maxcount
; /* the number of strings */
57 struct msistring
*strings
; /* an array of strings */
58 UINT
*sorted
; /* index */
61 static BOOL
validate_codepage( UINT codepage
)
63 if (codepage
!= CP_ACP
&& !IsValidCodePage( codepage
))
65 WARN("invalid codepage %u\n", codepage
);
71 static string_table
*init_stringtable( int entries
, UINT codepage
)
75 if (!validate_codepage( codepage
))
78 st
= msi_alloc( sizeof (string_table
) );
84 st
->strings
= msi_alloc_zero( sizeof(struct msistring
) * entries
);
91 st
->sorted
= msi_alloc( sizeof (UINT
) * entries
);
94 msi_free( st
->strings
);
99 st
->maxcount
= entries
;
101 st
->codepage
= codepage
;
107 VOID
msi_destroy_stringtable( string_table
*st
)
111 for( i
=0; i
<st
->maxcount
; i
++ )
113 if( st
->strings
[i
].persistent_refcount
||
114 st
->strings
[i
].nonpersistent_refcount
)
115 msi_free( st
->strings
[i
].str
);
117 msi_free( st
->strings
);
118 msi_free( st
->sorted
);
122 static int st_find_free_entry( string_table
*st
)
131 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
132 if( !st
->strings
[i
].persistent_refcount
&&
133 !st
->strings
[i
].nonpersistent_refcount
)
136 for( i
= 1; i
< st
->maxcount
; i
++ )
137 if( !st
->strings
[i
].persistent_refcount
&&
138 !st
->strings
[i
].nonpersistent_refcount
)
141 /* dynamically resize */
142 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
143 p
= msi_realloc_zero( st
->strings
, sz
* sizeof(struct msistring
) );
147 s
= msi_realloc( st
->sorted
, sz
*sizeof(UINT
) );
157 st
->freeslot
= st
->maxcount
;
159 if( st
->strings
[st
->freeslot
].persistent_refcount
||
160 st
->strings
[st
->freeslot
].nonpersistent_refcount
)
161 ERR("oops. expected freeslot to be free...\n");
165 static int find_insert_index( const string_table
*st
, UINT string_id
)
167 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
171 i
= (low
+ high
) / 2;
172 c
= strcmpW( st
->strings
[string_id
].str
, st
->strings
[st
->sorted
[i
]].str
);
179 return -1; /* already exists */
184 static void insert_string_sorted( string_table
*st
, UINT string_id
)
188 i
= find_insert_index( st
, string_id
);
192 memmove( &st
->sorted
[i
] + 1, &st
->sorted
[i
], (st
->sortcount
- i
) * sizeof(UINT
) );
193 st
->sorted
[i
] = string_id
;
197 static void set_st_entry( string_table
*st
, UINT n
, LPWSTR str
, USHORT refcount
, enum StringPersistence persistence
)
199 if (persistence
== StringPersistent
)
201 st
->strings
[n
].persistent_refcount
= refcount
;
202 st
->strings
[n
].nonpersistent_refcount
= 0;
206 st
->strings
[n
].persistent_refcount
= 0;
207 st
->strings
[n
].nonpersistent_refcount
= refcount
;
210 st
->strings
[n
].str
= str
;
212 insert_string_sorted( st
, n
);
214 if( n
< st
->maxcount
)
215 st
->freeslot
= n
+ 1;
218 static UINT
msi_string2idA( const string_table
*st
, LPCSTR buffer
, UINT
*id
)
221 UINT r
= ERROR_INVALID_PARAMETER
;
224 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
229 return ERROR_SUCCESS
;
232 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
235 str
= msi_alloc( sz
*sizeof(WCHAR
) );
237 return ERROR_NOT_ENOUGH_MEMORY
;
238 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
240 r
= msi_string2idW( st
, str
, id
);
246 static int msi_addstring( string_table
*st
, UINT n
, const CHAR
*data
, int len
, USHORT refcount
, enum StringPersistence persistence
)
257 if( st
->strings
[n
].persistent_refcount
||
258 st
->strings
[n
].nonpersistent_refcount
)
263 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
265 if (persistence
== StringPersistent
)
266 st
->strings
[n
].persistent_refcount
+= refcount
;
268 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
271 n
= st_find_free_entry( st
);
278 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
282 /* allocate a new string */
285 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
286 str
= msi_alloc( (sz
+1)*sizeof(WCHAR
) );
289 MultiByteToWideChar( st
->codepage
, 0, data
, len
, str
, sz
);
292 set_st_entry( st
, n
, str
, refcount
, persistence
);
297 int msi_addstringW( string_table
*st
, const WCHAR
*data
, int len
, USHORT refcount
, enum StringPersistence persistence
)
307 if( msi_string2idW( st
, data
, &n
) == ERROR_SUCCESS
)
309 if (persistence
== StringPersistent
)
310 st
->strings
[n
].persistent_refcount
+= refcount
;
312 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
316 n
= st_find_free_entry( st
);
320 /* allocate a new string */
323 TRACE("%s, n = %d len = %d\n", debugstr_w(data
), n
, len
);
325 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
328 memcpy( str
, data
, len
*sizeof(WCHAR
) );
331 set_st_entry( st
, n
, str
, refcount
, persistence
);
336 /* find the string identified by an id - return null if there's none */
337 const WCHAR
*msi_string_lookup_id( const string_table
*st
, UINT id
)
342 if( id
>= st
->maxcount
)
345 if( id
&& !st
->strings
[id
].persistent_refcount
&& !st
->strings
[id
].nonpersistent_refcount
)
348 return st
->strings
[id
].str
;
354 * [in] st - pointer to the string table
355 * [in] id - id of the string to retrieve
356 * [out] buffer - destination of the UTF8 string
357 * [in/out] sz - number of bytes available in the buffer on input
358 * number of bytes used on output
360 * The size includes the terminating nul character. Short buffers
361 * will be filled, but not nul terminated.
363 static UINT
msi_id2stringA( const string_table
*st
, UINT id
, LPSTR buffer
, UINT
*sz
)
369 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
371 str
= msi_string_lookup_id( st
, id
);
373 return ERROR_FUNCTION_FAILED
;
375 len
= WideCharToMultiByte( st
->codepage
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
380 return ERROR_SUCCESS
;
385 n
= strlenW( str
) + 1;
386 while( n
&& (len
> *sz
) )
387 len
= WideCharToMultiByte( st
->codepage
, 0,
388 str
, --n
, NULL
, 0, NULL
, NULL
);
393 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, n
, buffer
, len
, NULL
, NULL
);
395 return ERROR_SUCCESS
;
401 * [in] st - pointer to the string table
402 * [in] str - string to find in the string table
403 * [out] id - id of the string, if found
405 UINT
msi_string2idW( const string_table
*st
, LPCWSTR str
, UINT
*id
)
407 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
411 i
= (low
+ high
) / 2;
412 c
= strcmpW( str
, st
->strings
[st
->sorted
[i
]].str
);
421 return ERROR_SUCCESS
;
425 return ERROR_INVALID_PARAMETER
;
428 static void string_totalsize( const string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
430 UINT i
, len
, holesize
;
432 if( st
->strings
[0].str
|| st
->strings
[0].persistent_refcount
|| st
->strings
[0].nonpersistent_refcount
)
433 ERR("oops. element 0 has a string\n");
438 for( i
=1; i
<st
->maxcount
; i
++ )
440 if( !st
->strings
[i
].persistent_refcount
)
442 TRACE("[%u] nonpersistent = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
445 else if( st
->strings
[i
].str
)
447 TRACE("[%u] = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
448 len
= WideCharToMultiByte( st
->codepage
, 0,
449 st
->strings
[i
].str
, -1, NULL
, 0, NULL
, NULL
);
455 (*poolsize
) += holesize
+ 4;
461 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
464 HRESULT
msi_init_string_table( IStorage
*stg
)
466 USHORT zero
[2] = { 0, 0 };
469 /* create the StringPool stream... add the zero string to it*/
470 ret
= write_stream_data(stg
, szStringPool
, zero
, sizeof zero
, TRUE
);
471 if (ret
!= ERROR_SUCCESS
)
474 /* create the StringData stream... make it zero length */
475 ret
= write_stream_data(stg
, szStringData
, NULL
, 0, TRUE
);
476 if (ret
!= ERROR_SUCCESS
)
482 string_table
*msi_load_string_table( IStorage
*stg
, UINT
*bytes_per_strref
)
484 string_table
*st
= NULL
;
487 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
488 DWORD i
, count
, offset
, len
, n
, refs
;
490 r
= read_stream_data( stg
, szStringPool
, TRUE
, (BYTE
**)&pool
, &poolsize
);
491 if( r
!= ERROR_SUCCESS
)
493 r
= read_stream_data( stg
, szStringData
, TRUE
, (BYTE
**)&data
, &datasize
);
494 if( r
!= ERROR_SUCCESS
)
497 if ( (poolsize
> 4) && (pool
[1] & 0x8000) )
498 *bytes_per_strref
= LONG_STR_BYTES
;
500 *bytes_per_strref
= sizeof(USHORT
);
504 codepage
= pool
[0] | ( (pool
[1] & ~0x8000) << 16 );
507 st
= init_stringtable( count
, codepage
);
516 /* the string reference count is always the second word */
519 /* empty entries have two zeros, still have a string id */
520 if (pool
[i
*2] == 0 && refs
== 0)
528 * If a string is over 64k, the previous string entry is made null
529 * and its the high word of the length is inserted in the null string's
530 * reference count field.
534 len
= (pool
[i
*2+3] << 16) + pool
[i
*2+2];
543 if ( (offset
+ len
) > datasize
)
545 ERR("string table corrupt?\n");
549 r
= msi_addstring( st
, n
, data
+offset
, len
, refs
, StringPersistent
);
551 ERR("Failed to add string %d\n", n
);
556 if ( datasize
!= offset
)
557 ERR("string table load failed! (%08x != %08x), please report\n", datasize
, offset
);
559 TRACE("Loaded %d strings\n", count
);
568 UINT
msi_save_string_table( const string_table
*st
, IStorage
*storage
, UINT
*bytes_per_strref
)
570 UINT i
, datasize
= 0, poolsize
= 0, sz
, used
, r
, codepage
, n
;
571 UINT ret
= ERROR_FUNCTION_FAILED
;
577 /* construct the new table in memory first */
578 string_totalsize( st
, &datasize
, &poolsize
);
580 TRACE("%u %u %u\n", st
->maxcount
, datasize
, poolsize
);
582 pool
= msi_alloc( poolsize
);
585 WARN("Failed to alloc pool %d bytes\n", poolsize
);
588 data
= msi_alloc( datasize
);
591 WARN("Failed to alloc data %d bytes\n", poolsize
);
596 codepage
= st
->codepage
;
597 pool
[0] = codepage
& 0xffff;
598 pool
[1] = codepage
>> 16;
599 if (st
->maxcount
> 0xffff)
602 *bytes_per_strref
= LONG_STR_BYTES
;
605 *bytes_per_strref
= sizeof(USHORT
);
608 for( i
=1; i
<st
->maxcount
; i
++ )
610 if( !st
->strings
[i
].persistent_refcount
)
618 sz
= datasize
- used
;
619 r
= msi_id2stringA( st
, i
, data
+used
, &sz
);
620 if( r
!= ERROR_SUCCESS
)
622 ERR("failed to fetch string\n");
625 if( sz
&& (sz
< (datasize
- used
) ) )
629 pool
[ n
*2 + 1 ] = st
->strings
[i
].persistent_refcount
;
640 pool
[ n
*2 + 2 ] = sz
&0xffff;
641 pool
[ n
*2 + 3 ] = (sz
>>16);
645 if( used
> datasize
)
647 ERR("oops overran %d >= %d\n", used
, datasize
);
652 if( used
!= datasize
)
654 ERR("oops used %d != datasize %d\n", used
, datasize
);
658 /* write the streams */
659 r
= write_stream_data( storage
, szStringData
, data
, datasize
, TRUE
);
660 TRACE("Wrote StringData r=%08x\n", r
);
663 r
= write_stream_data( storage
, szStringPool
, pool
, poolsize
, TRUE
);
664 TRACE("Wrote StringPool r=%08x\n", r
);
677 UINT
msi_get_string_table_codepage( const string_table
*st
)
682 UINT
msi_set_string_table_codepage( string_table
*st
, UINT codepage
)
684 if (validate_codepage( codepage
))
686 st
->codepage
= codepage
;
687 return ERROR_SUCCESS
;
689 return ERROR_FUNCTION_FAILED
;