msi: Don't terminate the string in msi_id2stringA.
[wine/multimedia.git] / dlls / msi / string.c
blob065c04a3720a00dc5b1d1fcdcdae9ae9b29e627b
1 /*
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
23 #define COBJMACROS
25 #include <stdarg.h>
26 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objbase.h"
36 #include "objidl.h"
37 #include "msipriv.h"
38 #include "winnls.h"
40 #include "query.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 struct msistring
46 USHORT persistent_refcount;
47 USHORT nonpersistent_refcount;
48 LPWSTR str;
51 struct string_table
53 UINT maxcount; /* the number of strings */
54 UINT freeslot;
55 UINT codepage;
56 UINT sortcount;
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);
66 return FALSE;
68 return TRUE;
71 static string_table *init_stringtable( int entries, UINT codepage )
73 string_table *st;
75 if (!validate_codepage( codepage ))
76 return NULL;
78 st = msi_alloc( sizeof (string_table) );
79 if( !st )
80 return NULL;
81 if( entries < 1 )
82 entries = 1;
84 st->strings = msi_alloc_zero( sizeof(struct msistring) * entries );
85 if( !st->strings )
87 msi_free( st );
88 return NULL;
91 st->sorted = msi_alloc( sizeof (UINT) * entries );
92 if( !st->sorted )
94 msi_free( st->strings );
95 msi_free( st );
96 return NULL;
99 st->maxcount = entries;
100 st->freeslot = 1;
101 st->codepage = codepage;
102 st->sortcount = 0;
104 return st;
107 VOID msi_destroy_stringtable( string_table *st )
109 UINT i;
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 );
119 msi_free( st );
122 static int st_find_free_entry( string_table *st )
124 UINT i, sz, *s;
125 struct msistring *p;
127 TRACE("%p\n", st);
129 if( st->freeslot )
131 for( i = st->freeslot; i < st->maxcount; i++ )
132 if( !st->strings[i].persistent_refcount &&
133 !st->strings[i].nonpersistent_refcount )
134 return i;
136 for( i = 1; i < st->maxcount; i++ )
137 if( !st->strings[i].persistent_refcount &&
138 !st->strings[i].nonpersistent_refcount )
139 return i;
141 /* dynamically resize */
142 sz = st->maxcount + 1 + st->maxcount/2;
143 p = msi_realloc_zero( st->strings, sz * sizeof(struct msistring) );
144 if( !p )
145 return -1;
147 s = msi_realloc( st->sorted, sz*sizeof(UINT) );
148 if( !s )
150 msi_free( p );
151 return -1;
154 st->strings = p;
155 st->sorted = s;
157 st->freeslot = st->maxcount;
158 st->maxcount = sz;
159 if( st->strings[st->freeslot].persistent_refcount ||
160 st->strings[st->freeslot].nonpersistent_refcount )
161 ERR("oops. expected freeslot to be free...\n");
162 return st->freeslot;
165 static int find_insert_index( const string_table *st, UINT string_id )
167 int i, c, low = 0, high = st->sortcount - 1;
169 while (low <= high)
171 i = (low + high) / 2;
172 c = strcmpW( st->strings[string_id].str, st->strings[st->sorted[i]].str );
174 if (c < 0)
175 high = i - 1;
176 else if (c > 0)
177 low = i + 1;
178 else
179 return -1; /* already exists */
181 return high + 1;
184 static void insert_string_sorted( string_table *st, UINT string_id )
186 int i;
188 i = find_insert_index( st, string_id );
189 if (i == -1)
190 return;
192 memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
193 st->sorted[i] = string_id;
194 st->sortcount++;
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;
204 else
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 )
220 DWORD sz;
221 UINT r = ERROR_INVALID_PARAMETER;
222 LPWSTR str;
224 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
226 if( buffer[0] == 0 )
228 *id = 0;
229 return ERROR_SUCCESS;
232 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
233 if( sz <= 0 )
234 return r;
235 str = msi_alloc( sz*sizeof(WCHAR) );
236 if( !str )
237 return ERROR_NOT_ENOUGH_MEMORY;
238 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
240 r = msi_string2idW( st, str, id );
241 msi_free( str );
243 return r;
246 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
248 LPWSTR str;
249 int sz;
251 if( !data )
252 return 0;
253 if( !data[0] )
254 return 0;
255 if( n > 0 )
257 if( st->strings[n].persistent_refcount ||
258 st->strings[n].nonpersistent_refcount )
259 return -1;
261 else
263 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
265 if (persistence == StringPersistent)
266 st->strings[n].persistent_refcount += refcount;
267 else
268 st->strings[n].nonpersistent_refcount += refcount;
269 return n;
271 n = st_find_free_entry( st );
272 if( n == -1 )
273 return -1;
276 if( n < 1 )
278 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
279 return -1;
282 /* allocate a new string */
283 if( len < 0 )
284 len = strlen(data);
285 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
286 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
287 if( !str )
288 return -1;
289 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
290 str[sz] = 0;
292 set_st_entry( st, n, str, refcount, persistence );
294 return n;
297 int msi_addstringW( string_table *st, const WCHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
299 UINT n;
300 LPWSTR str;
302 if( !data )
303 return 0;
304 if( !data[0] )
305 return 0;
307 if( msi_string2idW( st, data, &n ) == ERROR_SUCCESS )
309 if (persistence == StringPersistent)
310 st->strings[n].persistent_refcount += refcount;
311 else
312 st->strings[n].nonpersistent_refcount += refcount;
313 return n;
316 n = st_find_free_entry( st );
317 if( n == -1 )
318 return -1;
320 /* allocate a new string */
321 if(len<0)
322 len = strlenW(data);
323 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
325 str = msi_alloc( (len+1)*sizeof(WCHAR) );
326 if( !str )
327 return -1;
328 memcpy( str, data, len*sizeof(WCHAR) );
329 str[len] = 0;
331 set_st_entry( st, n, str, refcount, persistence );
333 return n;
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 )
339 if( id == 0 )
340 return szEmpty;
342 if( id >= st->maxcount )
343 return NULL;
345 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
346 return NULL;
348 return st->strings[id].str;
352 * msi_id2stringA
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 * Returned string is not nul terminated.
362 static UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
364 UINT len, lenW;
365 const WCHAR *str;
367 TRACE("Finding string %d of %d\n", id, st->maxcount);
369 str = msi_string_lookup_id( st, id );
370 if( !str )
371 return ERROR_FUNCTION_FAILED;
373 lenW = strlenW( str );
374 len = WideCharToMultiByte( st->codepage, 0, str, lenW, NULL, 0, NULL, NULL );
375 if( *sz < len )
377 *sz = len;
378 return ERROR_MORE_DATA;
380 *sz = WideCharToMultiByte( st->codepage, 0, str, lenW, buffer, *sz, NULL, NULL );
381 return ERROR_SUCCESS;
385 * msi_string2idW
387 * [in] st - pointer to the string table
388 * [in] str - string to find in the string table
389 * [out] id - id of the string, if found
391 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
393 int i, c, low = 0, high = st->sortcount - 1;
395 while (low <= high)
397 i = (low + high) / 2;
398 c = strcmpW( str, st->strings[st->sorted[i]].str );
400 if (c < 0)
401 high = i - 1;
402 else if (c > 0)
403 low = i + 1;
404 else
406 *id = st->sorted[i];
407 return ERROR_SUCCESS;
411 return ERROR_INVALID_PARAMETER;
414 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
416 UINT i, len, holesize;
418 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
419 ERR("oops. element 0 has a string\n");
421 *poolsize = 4;
422 *datasize = 0;
423 holesize = 0;
424 for( i=1; i<st->maxcount; i++ )
426 if( !st->strings[i].persistent_refcount )
428 TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
429 (*poolsize) += 4;
431 else if( st->strings[i].str )
433 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
434 len = WideCharToMultiByte( st->codepage, 0,
435 st->strings[i].str, -1, NULL, 0, NULL, NULL);
436 if( len )
437 len--;
438 (*datasize) += len;
439 if (len>0xffff)
440 (*poolsize) += 4;
441 (*poolsize) += holesize + 4;
442 holesize = 0;
444 else
445 holesize += 4;
447 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
450 HRESULT msi_init_string_table( IStorage *stg )
452 USHORT zero[2] = { 0, 0 };
453 UINT ret;
455 /* create the StringPool stream... add the zero string to it*/
456 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
457 if (ret != ERROR_SUCCESS)
458 return E_FAIL;
460 /* create the StringData stream... make it zero length */
461 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
462 if (ret != ERROR_SUCCESS)
463 return E_FAIL;
465 return S_OK;
468 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
470 string_table *st = NULL;
471 CHAR *data = NULL;
472 USHORT *pool = NULL;
473 UINT r, datasize = 0, poolsize = 0, codepage;
474 DWORD i, count, offset, len, n, refs;
476 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
477 if( r != ERROR_SUCCESS)
478 goto end;
479 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
480 if( r != ERROR_SUCCESS)
481 goto end;
483 if ( (poolsize > 4) && (pool[1] & 0x8000) )
484 *bytes_per_strref = LONG_STR_BYTES;
485 else
486 *bytes_per_strref = sizeof(USHORT);
488 count = poolsize/4;
489 if( poolsize > 4 )
490 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
491 else
492 codepage = CP_ACP;
493 st = init_stringtable( count, codepage );
494 if (!st)
495 goto end;
497 offset = 0;
498 n = 1;
499 i = 1;
500 while( i<count )
502 /* the string reference count is always the second word */
503 refs = pool[i*2+1];
505 /* empty entries have two zeros, still have a string id */
506 if (pool[i*2] == 0 && refs == 0)
508 i++;
509 n++;
510 continue;
514 * If a string is over 64k, the previous string entry is made null
515 * and its the high word of the length is inserted in the null string's
516 * reference count field.
518 if( pool[i*2] == 0)
520 len = (pool[i*2+3] << 16) + pool[i*2+2];
521 i += 2;
523 else
525 len = pool[i*2];
526 i += 1;
529 if ( (offset + len) > datasize )
531 ERR("string table corrupt?\n");
532 break;
535 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
536 if( r != n )
537 ERR("Failed to add string %d\n", n );
538 n++;
539 offset += len;
542 if ( datasize != offset )
543 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
545 TRACE("Loaded %d strings\n", count);
547 end:
548 msi_free( pool );
549 msi_free( data );
551 return st;
554 UINT msi_save_string_table( const string_table *st, IStorage *storage, UINT *bytes_per_strref )
556 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
557 UINT ret = ERROR_FUNCTION_FAILED;
558 CHAR *data = NULL;
559 USHORT *pool = NULL;
561 TRACE("\n");
563 /* construct the new table in memory first */
564 string_totalsize( st, &datasize, &poolsize );
566 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
568 pool = msi_alloc( poolsize );
569 if( ! pool )
571 WARN("Failed to alloc pool %d bytes\n", poolsize );
572 goto err;
574 data = msi_alloc( datasize );
575 if( ! data )
577 WARN("Failed to alloc data %d bytes\n", datasize );
578 goto err;
581 used = 0;
582 codepage = st->codepage;
583 pool[0] = codepage & 0xffff;
584 pool[1] = codepage >> 16;
585 if (st->maxcount > 0xffff)
587 pool[1] |= 0x8000;
588 *bytes_per_strref = LONG_STR_BYTES;
590 else
591 *bytes_per_strref = sizeof(USHORT);
593 n = 1;
594 for( i=1; i<st->maxcount; i++ )
596 if( !st->strings[i].persistent_refcount )
598 pool[ n*2 ] = 0;
599 pool[ n*2 + 1] = 0;
600 n++;
601 continue;
604 sz = datasize - used;
605 r = msi_id2stringA( st, i, data+used, &sz );
606 if( r != ERROR_SUCCESS )
608 ERR("failed to fetch string\n");
609 sz = 0;
612 if (sz)
613 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
614 else
615 pool[ n*2 + 1 ] = 0;
616 if (sz < 0x10000)
618 pool[ n*2 ] = sz;
619 n++;
621 else
623 pool[ n*2 ] = 0;
624 pool[ n*2 + 2 ] = sz&0xffff;
625 pool[ n*2 + 3 ] = (sz>>16);
626 n += 2;
628 used += sz;
629 if( used > datasize )
631 ERR("oops overran %d >= %d\n", used, datasize);
632 goto err;
636 if( used != datasize )
638 ERR("oops used %d != datasize %d\n", used, datasize);
639 goto err;
642 /* write the streams */
643 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
644 TRACE("Wrote StringData r=%08x\n", r);
645 if( r )
646 goto err;
647 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
648 TRACE("Wrote StringPool r=%08x\n", r);
649 if( r )
650 goto err;
652 ret = ERROR_SUCCESS;
654 err:
655 msi_free( data );
656 msi_free( pool );
658 return ret;
661 UINT msi_get_string_table_codepage( const string_table *st )
663 return st->codepage;
666 UINT msi_set_string_table_codepage( string_table *st, UINT codepage )
668 if (validate_codepage( codepage ))
670 st->codepage = codepage;
671 return ERROR_SUCCESS;
673 return ERROR_FUNCTION_FAILED;