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
25 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
29 USHORT persistent_refcount
;
30 USHORT nonpersistent_refcount
;
37 UINT maxcount
; /* the number of strings */
41 struct msistring
*strings
; /* an array of strings */
42 UINT
*sorted
; /* index */
45 static BOOL
validate_codepage( UINT codepage
)
47 if (codepage
!= CP_ACP
&& !IsValidCodePage( codepage
))
49 WARN("invalid codepage %u\n", codepage
);
55 static string_table
*init_stringtable( int entries
, UINT codepage
)
59 if (!validate_codepage( codepage
))
62 st
= msi_alloc( sizeof (string_table
) );
68 st
->strings
= msi_alloc_zero( sizeof(struct msistring
) * entries
);
75 st
->sorted
= msi_alloc( sizeof (UINT
) * entries
);
78 msi_free( st
->strings
);
83 st
->maxcount
= entries
;
85 st
->codepage
= codepage
;
91 VOID
msi_destroy_stringtable( string_table
*st
)
95 for( i
=0; i
<st
->maxcount
; i
++ )
97 if( st
->strings
[i
].persistent_refcount
||
98 st
->strings
[i
].nonpersistent_refcount
)
99 msi_free( st
->strings
[i
].data
);
101 msi_free( st
->strings
);
102 msi_free( st
->sorted
);
106 static int st_find_free_entry( string_table
*st
)
115 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
116 if( !st
->strings
[i
].persistent_refcount
&&
117 !st
->strings
[i
].nonpersistent_refcount
)
120 for( i
= 1; i
< st
->maxcount
; i
++ )
121 if( !st
->strings
[i
].persistent_refcount
&&
122 !st
->strings
[i
].nonpersistent_refcount
)
125 /* dynamically resize */
126 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
127 p
= msi_realloc_zero( st
->strings
, sz
* sizeof(struct msistring
) );
131 s
= msi_realloc( st
->sorted
, sz
*sizeof(UINT
) );
141 st
->freeslot
= st
->maxcount
;
143 if( st
->strings
[st
->freeslot
].persistent_refcount
||
144 st
->strings
[st
->freeslot
].nonpersistent_refcount
)
145 ERR("oops. expected freeslot to be free...\n");
149 static inline int cmp_string( const WCHAR
*str1
, int len1
, const WCHAR
*str2
, int len2
)
151 if (len1
< len2
) return -1;
152 else if (len1
> len2
) return 1;
155 if (*str1
== *str2
) { str1
++; str2
++; }
156 else return *str1
- *str2
;
162 static int find_insert_index( const string_table
*st
, UINT string_id
)
164 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
168 i
= (low
+ high
) / 2;
169 c
= cmp_string( st
->strings
[string_id
].data
, st
->strings
[string_id
].len
,
170 st
->strings
[st
->sorted
[i
]].data
, st
->strings
[st
->sorted
[i
]].len
);
176 return -1; /* already exists */
181 static void insert_string_sorted( string_table
*st
, UINT string_id
)
185 i
= find_insert_index( st
, string_id
);
189 memmove( &st
->sorted
[i
] + 1, &st
->sorted
[i
], (st
->sortcount
- i
) * sizeof(UINT
) );
190 st
->sorted
[i
] = string_id
;
194 static void set_st_entry( string_table
*st
, UINT n
, WCHAR
*str
, int len
, USHORT refcount
,
195 enum StringPersistence persistence
)
197 if (persistence
== StringPersistent
)
199 st
->strings
[n
].persistent_refcount
= refcount
;
200 st
->strings
[n
].nonpersistent_refcount
= 0;
204 st
->strings
[n
].persistent_refcount
= 0;
205 st
->strings
[n
].nonpersistent_refcount
= refcount
;
208 st
->strings
[n
].data
= str
;
209 st
->strings
[n
].len
= len
;
211 insert_string_sorted( st
, n
);
213 if( n
< st
->maxcount
)
214 st
->freeslot
= n
+ 1;
217 static UINT
msi_string2idA( const string_table
*st
, LPCSTR buffer
, UINT
*id
)
220 UINT r
= ERROR_INVALID_PARAMETER
;
223 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
228 return ERROR_SUCCESS
;
231 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
234 str
= msi_alloc( sz
*sizeof(WCHAR
) );
236 return ERROR_NOT_ENOUGH_MEMORY
;
237 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
239 r
= msi_string2id( st
, str
, sz
- 1, id
);
244 static int msi_addstring( string_table
*st
, UINT n
, const char *data
, UINT len
, USHORT refcount
, enum StringPersistence persistence
)
253 if( st
->strings
[n
].persistent_refcount
||
254 st
->strings
[n
].nonpersistent_refcount
)
259 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
261 if (persistence
== StringPersistent
)
262 st
->strings
[n
].persistent_refcount
+= refcount
;
264 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
267 n
= st_find_free_entry( st
);
274 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
278 /* 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
, sz
, refcount
, persistence
);
290 int msi_addstringW( string_table
*st
, const WCHAR
*data
, int len
, USHORT refcount
, enum StringPersistence persistence
)
298 if (len
< 0) len
= strlenW( data
);
300 if( !data
[0] && !len
)
303 if (msi_string2id( st
, data
, len
, &n
) == ERROR_SUCCESS
)
305 if (persistence
== StringPersistent
)
306 st
->strings
[n
].persistent_refcount
+= refcount
;
308 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
312 n
= st_find_free_entry( st
);
316 /* allocate a new string */
317 TRACE( "%s, n = %d len = %d\n", debugstr_wn(data
, len
), n
, len
);
319 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
322 memcpy( str
, data
, len
*sizeof(WCHAR
) );
325 set_st_entry( st
, n
, str
, len
, refcount
, persistence
);
329 /* find the string identified by an id - return null if there's none */
330 const WCHAR
*msi_string_lookup( const string_table
*st
, UINT id
, int *len
)
337 if( id
>= st
->maxcount
)
340 if( id
&& !st
->strings
[id
].persistent_refcount
&& !st
->strings
[id
].nonpersistent_refcount
)
343 if (len
) *len
= st
->strings
[id
].len
;
345 return st
->strings
[id
].data
;
351 * [in] st - pointer to the string table
352 * [in] id - id of the string to retrieve
353 * [out] buffer - destination of the UTF8 string
354 * [in/out] sz - number of bytes available in the buffer on input
355 * number of bytes used on output
357 * Returned string is not nul terminated.
359 static UINT
msi_id2stringA( const string_table
*st
, UINT id
, LPSTR buffer
, UINT
*sz
)
364 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
366 str
= msi_string_lookup( st
, id
, &lenW
);
368 return ERROR_FUNCTION_FAILED
;
370 len
= WideCharToMultiByte( st
->codepage
, 0, str
, lenW
, NULL
, 0, NULL
, NULL
);
374 return ERROR_MORE_DATA
;
376 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, lenW
, buffer
, *sz
, NULL
, NULL
);
377 return ERROR_SUCCESS
;
383 * [in] st - pointer to the string table
384 * [in] str - string to find in the string table
385 * [out] id - id of the string, if found
387 UINT
msi_string2id( const string_table
*st
, const WCHAR
*str
, int len
, UINT
*id
)
389 int i
, c
, low
= 0, high
= st
->sortcount
- 1;
391 if (len
< 0) len
= strlenW( str
);
395 i
= (low
+ high
) / 2;
396 c
= cmp_string( str
, len
, st
->strings
[st
->sorted
[i
]].data
, st
->strings
[st
->sorted
[i
]].len
);
405 return ERROR_SUCCESS
;
408 return ERROR_INVALID_PARAMETER
;
411 static void string_totalsize( const string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
413 UINT i
, len
, holesize
;
415 if( st
->strings
[0].data
|| st
->strings
[0].persistent_refcount
|| st
->strings
[0].nonpersistent_refcount
)
416 ERR("oops. element 0 has a string\n");
421 for( i
=1; i
<st
->maxcount
; i
++ )
423 if( !st
->strings
[i
].persistent_refcount
)
425 TRACE("[%u] nonpersistent = %s\n", i
, debugstr_wn(st
->strings
[i
].data
, st
->strings
[i
].len
));
428 else if( st
->strings
[i
].data
)
430 TRACE("[%u] = %s\n", i
, debugstr_wn(st
->strings
[i
].data
, st
->strings
[i
].len
));
431 len
= WideCharToMultiByte( st
->codepage
, 0, st
->strings
[i
].data
, st
->strings
[i
].len
+ 1,
432 NULL
, 0, NULL
, NULL
);
438 (*poolsize
) += holesize
+ 4;
444 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
447 HRESULT
msi_init_string_table( IStorage
*stg
)
449 USHORT zero
[2] = { 0, 0 };
452 /* create the StringPool stream... add the zero string to it*/
453 ret
= write_stream_data(stg
, szStringPool
, zero
, sizeof zero
, TRUE
);
454 if (ret
!= ERROR_SUCCESS
)
457 /* create the StringData stream... make it zero length */
458 ret
= write_stream_data(stg
, szStringData
, NULL
, 0, TRUE
);
459 if (ret
!= ERROR_SUCCESS
)
465 string_table
*msi_load_string_table( IStorage
*stg
, UINT
*bytes_per_strref
)
467 string_table
*st
= NULL
;
470 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
471 DWORD i
, count
, offset
, len
, n
, refs
;
473 r
= read_stream_data( stg
, szStringPool
, TRUE
, (BYTE
**)&pool
, &poolsize
);
474 if( r
!= ERROR_SUCCESS
)
476 r
= read_stream_data( stg
, szStringData
, TRUE
, (BYTE
**)&data
, &datasize
);
477 if( r
!= ERROR_SUCCESS
)
480 if ( (poolsize
> 4) && (pool
[1] & 0x8000) )
481 *bytes_per_strref
= LONG_STR_BYTES
;
483 *bytes_per_strref
= sizeof(USHORT
);
487 codepage
= pool
[0] | ( (pool
[1] & ~0x8000) << 16 );
490 st
= init_stringtable( count
, codepage
);
499 /* the string reference count is always the second word */
502 /* empty entries have two zeros, still have a string id */
503 if (pool
[i
*2] == 0 && refs
== 0)
511 * If a string is over 64k, the previous string entry is made null
512 * and its the high word of the length is inserted in the null string's
513 * reference count field.
517 len
= (pool
[i
*2+3] << 16) + pool
[i
*2+2];
526 if ( (offset
+ len
) > datasize
)
528 ERR("string table corrupt?\n");
532 r
= msi_addstring( st
, n
, data
+offset
, len
, refs
, StringPersistent
);
534 ERR("Failed to add string %d\n", n
);
539 if ( datasize
!= offset
)
540 ERR("string table load failed! (%08x != %08x), please report\n", datasize
, offset
);
542 TRACE("Loaded %d strings\n", count
);
551 UINT
msi_save_string_table( const string_table
*st
, IStorage
*storage
, UINT
*bytes_per_strref
)
553 UINT i
, datasize
= 0, poolsize
= 0, sz
, used
, r
, codepage
, n
;
554 UINT ret
= ERROR_FUNCTION_FAILED
;
560 /* construct the new table in memory first */
561 string_totalsize( st
, &datasize
, &poolsize
);
563 TRACE("%u %u %u\n", st
->maxcount
, datasize
, poolsize
);
565 pool
= msi_alloc( poolsize
);
568 WARN("Failed to alloc pool %d bytes\n", poolsize
);
571 data
= msi_alloc( datasize
);
574 WARN("Failed to alloc data %d bytes\n", datasize
);
579 codepage
= st
->codepage
;
580 pool
[0] = codepage
& 0xffff;
581 pool
[1] = codepage
>> 16;
582 if (st
->maxcount
> 0xffff)
585 *bytes_per_strref
= LONG_STR_BYTES
;
588 *bytes_per_strref
= sizeof(USHORT
);
591 for( i
=1; i
<st
->maxcount
; i
++ )
593 if( !st
->strings
[i
].persistent_refcount
)
601 sz
= datasize
- used
;
602 r
= msi_id2stringA( st
, i
, data
+used
, &sz
);
603 if( r
!= ERROR_SUCCESS
)
605 ERR("failed to fetch string\n");
610 pool
[ n
*2 + 1 ] = st
->strings
[i
].persistent_refcount
;
621 pool
[ n
*2 + 2 ] = sz
&0xffff;
622 pool
[ n
*2 + 3 ] = (sz
>>16);
626 if( used
> datasize
)
628 ERR("oops overran %d >= %d\n", used
, datasize
);
633 if( used
!= datasize
)
635 ERR("oops used %d != datasize %d\n", used
, datasize
);
639 /* write the streams */
640 r
= write_stream_data( storage
, szStringData
, data
, datasize
, TRUE
);
641 TRACE("Wrote StringData r=%08x\n", r
);
644 r
= write_stream_data( storage
, szStringPool
, pool
, poolsize
, TRUE
);
645 TRACE("Wrote StringPool r=%08x\n", r
);
658 UINT
msi_get_string_table_codepage( const string_table
*st
)
663 UINT
msi_set_string_table_codepage( string_table
*st
, UINT codepage
)
665 if (validate_codepage( codepage
))
667 st
->codepage
= codepage
;
668 return ERROR_SUCCESS
;
670 return ERROR_FUNCTION_FAILED
;