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"
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
45 USHORT persistent_refcount
;
46 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
].data
);
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 if (!(p
= msi_realloc( st
->strings
, sz
* sizeof(*p
) ))) return -1;
144 memset( p
+ st
->maxcount
, 0, (sz
- st
->maxcount
) * sizeof(*p
) );
146 if (!(s
= msi_realloc( st
->sorted
, sz
* sizeof(*s
) )))
155 st
->freeslot
= st
->maxcount
;
157 if( st
->strings
[st
->freeslot
].persistent_refcount
||
158 st
->strings
[st
->freeslot
].nonpersistent_refcount
)
159 ERR("oops. expected freeslot to be free...\n");
163 static inline int cmp_string( const WCHAR
*str1
, int len1
, const WCHAR
*str2
, int len2
)
165 if (len1
< len2
) return -1;
166 else if (len1
> len2
) return 1;
169 if (*str1
== *str2
) { str1
++; str2
++; }
170 else return *str1
- *str2
;
176 static int find_insert_index( const string_table
*st
, UINT string_id
)
178 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
182 i
= (low
+ high
) / 2;
183 c
= cmp_string( st
->strings
[string_id
].data
, st
->strings
[string_id
].len
,
184 st
->strings
[st
->sorted
[i
]].data
, st
->strings
[st
->sorted
[i
]].len
);
190 return -1; /* already exists */
195 static void insert_string_sorted( string_table
*st
, UINT string_id
)
199 i
= find_insert_index( st
, string_id
);
203 memmove( &st
->sorted
[i
] + 1, &st
->sorted
[i
], (st
->sortcount
- i
) * sizeof(UINT
) );
204 st
->sorted
[i
] = string_id
;
208 static void set_st_entry( string_table
*st
, UINT n
, WCHAR
*str
, int len
, USHORT refcount
,
213 st
->strings
[n
].persistent_refcount
= refcount
;
214 st
->strings
[n
].nonpersistent_refcount
= 0;
218 st
->strings
[n
].persistent_refcount
= 0;
219 st
->strings
[n
].nonpersistent_refcount
= refcount
;
222 st
->strings
[n
].data
= str
;
223 st
->strings
[n
].len
= len
;
225 insert_string_sorted( st
, n
);
227 if( n
< st
->maxcount
)
228 st
->freeslot
= n
+ 1;
231 static UINT
string2id( const string_table
*st
, const char *buffer
, UINT
*id
)
234 UINT r
= ERROR_INVALID_PARAMETER
;
237 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
242 return ERROR_SUCCESS
;
245 if (!(sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 )))
247 str
= msi_alloc( sz
*sizeof(WCHAR
) );
249 return ERROR_NOT_ENOUGH_MEMORY
;
250 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
252 r
= msi_string2id( st
, str
, sz
- 1, id
);
257 static int add_string( string_table
*st
, UINT n
, const char *data
, UINT len
, USHORT refcount
, BOOL persistent
)
266 if( st
->strings
[n
].persistent_refcount
||
267 st
->strings
[n
].nonpersistent_refcount
)
272 if (string2id( st
, data
, &n
) == ERROR_SUCCESS
)
275 st
->strings
[n
].persistent_refcount
+= refcount
;
277 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
280 n
= st_find_free_entry( st
);
287 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
291 /* allocate a new string */
292 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
293 str
= msi_alloc( (sz
+1)*sizeof(WCHAR
) );
296 MultiByteToWideChar( st
->codepage
, 0, data
, len
, str
, sz
);
299 set_st_entry( st
, n
, str
, sz
, refcount
, persistent
);
303 int msi_add_string( string_table
*st
, const WCHAR
*data
, int len
, BOOL persistent
)
311 if (len
< 0) len
= lstrlenW( data
);
313 if( !data
[0] && !len
)
316 if (msi_string2id( st
, data
, len
, &n
) == ERROR_SUCCESS
)
319 st
->strings
[n
].persistent_refcount
++;
321 st
->strings
[n
].nonpersistent_refcount
++;
325 n
= st_find_free_entry( st
);
329 /* allocate a new string */
330 TRACE( "%s, n = %d len = %d\n", debugstr_wn(data
, len
), n
, len
);
332 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
335 memcpy( str
, data
, len
*sizeof(WCHAR
) );
338 set_st_entry( st
, n
, str
, len
, 1, persistent
);
342 /* find the string identified by an id - return null if there's none */
343 const WCHAR
*msi_string_lookup( const string_table
*st
, UINT id
, int *len
)
350 if( id
>= st
->maxcount
)
353 if( id
&& !st
->strings
[id
].persistent_refcount
&& !st
->strings
[id
].nonpersistent_refcount
)
356 if (len
) *len
= st
->strings
[id
].len
;
358 return st
->strings
[id
].data
;
364 * [in] st - pointer to the string table
365 * [in] id - id of the string to retrieve
366 * [out] buffer - destination of the UTF8 string
367 * [in/out] sz - number of bytes available in the buffer on input
368 * number of bytes used on output
370 * Returned string is not nul terminated.
372 static UINT
id2string( const string_table
*st
, UINT id
, char *buffer
, UINT
*sz
)
377 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
379 str
= msi_string_lookup( st
, id
, &lenW
);
381 return ERROR_FUNCTION_FAILED
;
383 len
= WideCharToMultiByte( st
->codepage
, 0, str
, lenW
, NULL
, 0, NULL
, NULL
);
387 return ERROR_MORE_DATA
;
389 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, lenW
, buffer
, *sz
, NULL
, NULL
);
390 return ERROR_SUCCESS
;
396 * [in] st - pointer to the string table
397 * [in] str - string to find in the string table
398 * [out] id - id of the string, if found
400 UINT
msi_string2id( const string_table
*st
, const WCHAR
*str
, int len
, UINT
*id
)
402 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
404 if (len
< 0) len
= lstrlenW( str
);
408 i
= (low
+ high
) / 2;
409 c
= cmp_string( str
, len
, st
->strings
[st
->sorted
[i
]].data
, st
->strings
[st
->sorted
[i
]].len
);
418 return ERROR_SUCCESS
;
421 return ERROR_INVALID_PARAMETER
;
424 static void string_totalsize( const string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
426 UINT i
, len
, holesize
;
428 if( st
->strings
[0].data
|| st
->strings
[0].persistent_refcount
|| st
->strings
[0].nonpersistent_refcount
)
429 ERR("oops. element 0 has a string\n");
434 for( i
=1; i
<st
->maxcount
; i
++ )
436 if( !st
->strings
[i
].persistent_refcount
)
438 TRACE("[%u] nonpersistent = %s\n", i
, debugstr_wn(st
->strings
[i
].data
, st
->strings
[i
].len
));
441 else if( st
->strings
[i
].data
)
443 TRACE("[%u] = %s\n", i
, debugstr_wn(st
->strings
[i
].data
, st
->strings
[i
].len
));
444 len
= WideCharToMultiByte( st
->codepage
, 0, st
->strings
[i
].data
, st
->strings
[i
].len
+ 1,
445 NULL
, 0, NULL
, NULL
);
451 (*poolsize
) += holesize
+ 4;
457 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
460 HRESULT
msi_init_string_table( IStorage
*stg
)
462 USHORT zero
[2] = { 0, 0 };
465 /* create the StringPool stream... add the zero string to it*/
466 ret
= write_stream_data(stg
, L
"_StringPool", zero
, sizeof zero
, TRUE
);
467 if (ret
!= ERROR_SUCCESS
)
470 /* create the StringData stream... make it zero length */
471 ret
= write_stream_data(stg
, L
"_StringData", NULL
, 0, TRUE
);
472 if (ret
!= ERROR_SUCCESS
)
478 string_table
*msi_load_string_table( IStorage
*stg
, UINT
*bytes_per_strref
)
480 string_table
*st
= NULL
;
483 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
484 DWORD i
, count
, offset
, len
, n
, refs
;
486 r
= read_stream_data( stg
, L
"_StringPool", TRUE
, (BYTE
**)&pool
, &poolsize
);
487 if( r
!= ERROR_SUCCESS
)
489 r
= read_stream_data( stg
, L
"_StringData", TRUE
, (BYTE
**)&data
, &datasize
);
490 if( r
!= ERROR_SUCCESS
)
493 if ( (poolsize
> 4) && (pool
[1] & 0x8000) )
494 *bytes_per_strref
= LONG_STR_BYTES
;
496 *bytes_per_strref
= sizeof(USHORT
);
500 codepage
= pool
[0] | ( (pool
[1] & ~0x8000) << 16 );
503 st
= init_stringtable( count
, codepage
);
512 /* the string reference count is always the second word */
515 /* empty entries have two zeros, still have a string id */
516 if (pool
[i
*2] == 0 && refs
== 0)
524 * If a string is over 64k, the previous string entry is made null
525 * and its the high word of the length is inserted in the null string's
526 * reference count field.
530 len
= (pool
[i
*2+3] << 16) + pool
[i
*2+2];
539 if ( (offset
+ len
) > datasize
)
541 ERR("string table corrupt?\n");
545 r
= add_string( st
, n
, data
+offset
, len
, refs
, TRUE
);
547 ERR( "Failed to add string %lu\n", n
);
552 if ( datasize
!= offset
)
553 ERR( "string table load failed! (%u != %lu), please report\n", datasize
, offset
);
555 TRACE( "loaded %lu strings\n", count
);
564 UINT
msi_save_string_table( const string_table
*st
, IStorage
*storage
, UINT
*bytes_per_strref
)
566 UINT i
, datasize
= 0, poolsize
= 0, sz
, used
, r
, codepage
, n
;
567 UINT ret
= ERROR_FUNCTION_FAILED
;
573 /* construct the new table in memory first */
574 string_totalsize( st
, &datasize
, &poolsize
);
576 TRACE("%u %u %u\n", st
->maxcount
, datasize
, poolsize
);
578 pool
= msi_alloc( poolsize
);
581 WARN("Failed to alloc pool %d bytes\n", poolsize
);
584 data
= msi_alloc( datasize
);
587 WARN("Failed to alloc data %d bytes\n", datasize
);
592 codepage
= st
->codepage
;
593 pool
[0] = codepage
& 0xffff;
594 pool
[1] = codepage
>> 16;
595 if (st
->maxcount
> 0xffff)
598 *bytes_per_strref
= LONG_STR_BYTES
;
601 *bytes_per_strref
= sizeof(USHORT
);
604 for( i
=1; i
<st
->maxcount
; i
++ )
606 if( !st
->strings
[i
].persistent_refcount
)
614 sz
= datasize
- used
;
615 r
= id2string( st
, i
, data
+used
, &sz
);
616 if( r
!= ERROR_SUCCESS
)
618 ERR("failed to fetch string\n");
623 pool
[ n
*2 + 1 ] = st
->strings
[i
].persistent_refcount
;
634 pool
[ n
*2 + 2 ] = sz
&0xffff;
635 pool
[ n
*2 + 3 ] = (sz
>>16);
639 if( used
> datasize
)
641 ERR("oops overran %d >= %d\n", used
, datasize
);
646 if( used
!= datasize
)
648 ERR("oops used %d != datasize %d\n", used
, datasize
);
652 /* write the streams */
653 r
= write_stream_data( storage
, L
"_StringData", data
, datasize
, TRUE
);
654 TRACE("Wrote StringData r=%08x\n", r
);
657 r
= write_stream_data( storage
, L
"_StringPool", pool
, poolsize
, TRUE
);
658 TRACE("Wrote StringPool r=%08x\n", r
);
671 UINT
msi_get_string_table_codepage( const string_table
*st
)
676 UINT
msi_set_string_table_codepage( string_table
*st
, UINT codepage
)
678 if (validate_codepage( codepage
))
680 st
->codepage
= codepage
;
681 return ERROR_SUCCESS
;
683 return ERROR_FUNCTION_FAILED
;