ntdll: Implement DVD_SendKey() on Mac OS.
[wine/multimedia.git] / dlls / msi / string.c
blobe5b5cc2d4650a25ba91562f594279926141bf1a8
1 /*
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
22 #define COBJMACROS
24 #include <stdarg.h>
25 #include <assert.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "objbase.h"
35 #include "objidl.h"
36 #include "msipriv.h"
37 #include "winnls.h"
39 #include "query.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43 #define HASH_SIZE 0x101
44 #define LONG_STR_BYTES 3
46 typedef struct _msistring
48 int hash_next;
49 UINT persistent_refcount;
50 UINT nonpersistent_refcount;
51 LPWSTR str;
52 } msistring;
54 struct string_table
56 UINT maxcount; /* the number of strings */
57 UINT freeslot;
58 UINT codepage;
59 int hash[HASH_SIZE];
60 msistring *strings; /* an array of strings (in the tree) */
63 static UINT msistring_makehash( const WCHAR *str )
65 UINT hash = 0;
67 if (str==NULL)
68 return hash;
70 while( *str )
72 hash ^= *str++;
73 hash *= 53;
74 hash = (hash<<5) | (hash>>27);
76 return hash % HASH_SIZE;
79 static string_table *init_stringtable( int entries, UINT codepage )
81 string_table *st;
82 int i;
84 if (codepage != CP_ACP && !IsValidCodePage(codepage))
86 ERR("invalid codepage %d\n", codepage);
87 return NULL;
90 st = msi_alloc( sizeof (string_table) );
91 if( !st )
92 return NULL;
93 if( entries < 1 )
94 entries = 1;
95 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
96 if( !st->strings )
98 msi_free( st );
99 return NULL;
101 st->maxcount = entries;
102 st->freeslot = 1;
103 st->codepage = codepage;
105 for( i=0; i<HASH_SIZE; i++ )
106 st->hash[i] = -1;
108 return st;
111 VOID msi_destroy_stringtable( string_table *st )
113 UINT i;
115 for( i=0; i<st->maxcount; i++ )
117 if( st->strings[i].persistent_refcount ||
118 st->strings[i].nonpersistent_refcount )
119 msi_free( st->strings[i].str );
121 msi_free( st->strings );
122 msi_free( st );
125 static int st_find_free_entry( string_table *st )
127 UINT i, sz;
128 msistring *p;
130 TRACE("%p\n", st);
132 if( st->freeslot )
134 for( i = st->freeslot; i < st->maxcount; i++ )
135 if( !st->strings[i].persistent_refcount &&
136 !st->strings[i].nonpersistent_refcount )
137 return i;
139 for( i = 1; i < st->maxcount; i++ )
140 if( !st->strings[i].persistent_refcount &&
141 !st->strings[i].nonpersistent_refcount )
142 return i;
144 /* dynamically resize */
145 sz = st->maxcount + 1 + st->maxcount/2;
146 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
147 if( !p )
148 return -1;
149 st->strings = p;
150 st->freeslot = st->maxcount;
151 st->maxcount = sz;
152 if( st->strings[st->freeslot].persistent_refcount ||
153 st->strings[st->freeslot].nonpersistent_refcount )
154 ERR("oops. expected freeslot to be free...\n");
155 return st->freeslot;
158 static void set_st_entry( string_table *st, UINT n, LPWSTR str, UINT refcount, enum StringPersistence persistence )
160 UINT hash = msistring_makehash( str );
162 if (persistence == StringPersistent)
164 st->strings[n].persistent_refcount = refcount;
165 st->strings[n].nonpersistent_refcount = 0;
167 else
169 st->strings[n].persistent_refcount = 0;
170 st->strings[n].nonpersistent_refcount = refcount;
173 st->strings[n].str = str;
175 st->strings[n].hash_next = st->hash[hash];
176 st->hash[hash] = n;
178 if( n < st->maxcount )
179 st->freeslot = n + 1;
182 static UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
184 DWORD sz;
185 UINT r = ERROR_INVALID_PARAMETER;
186 LPWSTR str;
188 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
190 if( buffer[0] == 0 )
192 *id = 0;
193 return ERROR_SUCCESS;
196 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
197 if( sz <= 0 )
198 return r;
199 str = msi_alloc( sz*sizeof(WCHAR) );
200 if( !str )
201 return ERROR_NOT_ENOUGH_MEMORY;
202 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
204 r = msi_string2idW( st, str, id );
205 msi_free( str );
207 return r;
210 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
212 LPWSTR str;
213 int sz;
215 if( !data )
216 return 0;
217 if( !data[0] )
218 return 0;
219 if( n > 0 )
221 if( st->strings[n].persistent_refcount ||
222 st->strings[n].nonpersistent_refcount )
223 return -1;
225 else
227 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
229 if (persistence == StringPersistent)
230 st->strings[n].persistent_refcount += refcount;
231 else
232 st->strings[n].nonpersistent_refcount += refcount;
233 return n;
235 n = st_find_free_entry( st );
236 if( n == -1 )
237 return -1;
240 if( n < 1 )
242 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
243 return -1;
246 /* allocate a new string */
247 if( len < 0 )
248 len = strlen(data);
249 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
250 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
251 if( !str )
252 return -1;
253 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
254 str[sz] = 0;
256 set_st_entry( st, n, str, refcount, persistence );
258 return n;
261 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
263 LPWSTR str;
265 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
267 if( !data )
268 return 0;
269 if( !data[0] )
270 return 0;
271 if( n > 0 )
273 if( st->strings[n].persistent_refcount ||
274 st->strings[n].nonpersistent_refcount )
275 return -1;
277 else
279 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
281 if (persistence == StringPersistent)
282 st->strings[n].persistent_refcount += refcount;
283 else
284 st->strings[n].nonpersistent_refcount += refcount;
285 return n;
287 n = st_find_free_entry( st );
288 if( n == -1 )
289 return -1;
292 if( n < 1 )
294 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
295 return -1;
298 /* allocate a new string */
299 if(len<0)
300 len = strlenW(data);
301 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
303 str = msi_alloc( (len+1)*sizeof(WCHAR) );
304 if( !str )
305 return -1;
306 memcpy( str, data, len*sizeof(WCHAR) );
307 str[len] = 0;
309 set_st_entry( st, n, str, refcount, persistence );
311 return n;
314 /* find the string identified by an id - return null if there's none */
315 const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
317 if( id == 0 )
318 return szEmpty;
320 if( id >= st->maxcount )
321 return NULL;
323 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
324 return NULL;
326 return st->strings[id].str;
330 * msi_id2stringA
332 * [in] st - pointer to the string table
333 * [in] id - id of the string to retrieve
334 * [out] buffer - destination of the UTF8 string
335 * [in/out] sz - number of bytes available in the buffer on input
336 * number of bytes used on output
338 * The size includes the terminating nul character. Short buffers
339 * will be filled, but not nul terminated.
341 static UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
343 UINT len;
344 const WCHAR *str;
345 int n;
347 TRACE("Finding string %d of %d\n", id, st->maxcount);
349 str = msi_string_lookup_id( st, id );
350 if( !str )
351 return ERROR_FUNCTION_FAILED;
353 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
355 if( !buffer )
357 *sz = len;
358 return ERROR_SUCCESS;
361 if( len > *sz )
363 n = strlenW( str ) + 1;
364 while( n && (len > *sz) )
365 len = WideCharToMultiByte( st->codepage, 0,
366 str, --n, NULL, 0, NULL, NULL );
368 else
369 n = -1;
371 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
373 return ERROR_SUCCESS;
377 * msi_string2idW
379 * [in] st - pointer to the string table
380 * [in] str - string to find in the string table
381 * [out] id - id of the string, if found
383 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
385 UINT n, hash = msistring_makehash( str );
386 msistring *se = st->strings;
388 for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
390 if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
392 *id = n;
393 return ERROR_SUCCESS;
397 return ERROR_INVALID_PARAMETER;
400 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
402 UINT i, len, holesize;
404 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
405 ERR("oops. element 0 has a string\n");
407 *poolsize = 4;
408 *datasize = 0;
409 holesize = 0;
410 for( i=1; i<st->maxcount; i++ )
412 if( !st->strings[i].persistent_refcount )
414 TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
415 (*poolsize) += 4;
417 else if( st->strings[i].str )
419 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
420 len = WideCharToMultiByte( st->codepage, 0,
421 st->strings[i].str, -1, NULL, 0, NULL, NULL);
422 if( len )
423 len--;
424 (*datasize) += len;
425 if (len>0xffff)
426 (*poolsize) += 4;
427 (*poolsize) += holesize + 4;
428 holesize = 0;
430 else
431 holesize += 4;
433 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
436 static const WCHAR szStringData[] = {
437 '_','S','t','r','i','n','g','D','a','t','a',0 };
438 static const WCHAR szStringPool[] = {
439 '_','S','t','r','i','n','g','P','o','o','l',0 };
441 HRESULT msi_init_string_table( IStorage *stg )
443 USHORT zero[2] = { 0, 0 };
444 UINT ret;
446 /* create the StringPool stream... add the zero string to it*/
447 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
448 if (ret != ERROR_SUCCESS)
449 return E_FAIL;
451 /* create the StringData stream... make it zero length */
452 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
453 if (ret != ERROR_SUCCESS)
454 return E_FAIL;
456 return S_OK;
459 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
461 string_table *st = NULL;
462 CHAR *data = NULL;
463 USHORT *pool = NULL;
464 UINT r, datasize = 0, poolsize = 0, codepage;
465 DWORD i, count, offset, len, n, refs;
467 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
468 if( r != ERROR_SUCCESS)
469 goto end;
470 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
471 if( r != ERROR_SUCCESS)
472 goto end;
474 if ( (poolsize > 4) && (pool[1] & 0x8000) )
475 *bytes_per_strref = LONG_STR_BYTES;
476 else
477 *bytes_per_strref = sizeof(USHORT);
479 count = poolsize/4;
480 if( poolsize > 4 )
481 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
482 else
483 codepage = CP_ACP;
484 st = init_stringtable( count, codepage );
485 if (!st)
486 goto end;
488 offset = 0;
489 n = 1;
490 i = 1;
491 while( i<count )
493 /* the string reference count is always the second word */
494 refs = pool[i*2+1];
496 /* empty entries have two zeros, still have a string id */
497 if (pool[i*2] == 0 && refs == 0)
499 i++;
500 n++;
501 continue;
505 * If a string is over 64k, the previous string entry is made null
506 * and its the high word of the length is inserted in the null string's
507 * reference count field.
509 if( pool[i*2] == 0)
511 len = (pool[i*2+3] << 16) + pool[i*2+2];
512 i += 2;
514 else
516 len = pool[i*2];
517 i += 1;
520 if ( (offset + len) > datasize )
522 ERR("string table corrupt?\n");
523 break;
526 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
527 if( r != n )
528 ERR("Failed to add string %d\n", n );
529 n++;
530 offset += len;
533 if ( datasize != offset )
534 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
536 TRACE("Loaded %d strings\n", count);
538 end:
539 msi_free( pool );
540 msi_free( data );
542 return st;
545 UINT msi_save_string_table( const string_table *st, IStorage *storage )
547 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
548 UINT ret = ERROR_FUNCTION_FAILED;
549 CHAR *data = NULL;
550 USHORT *pool = NULL;
552 TRACE("\n");
554 /* construct the new table in memory first */
555 string_totalsize( st, &datasize, &poolsize );
557 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
559 pool = msi_alloc( poolsize );
560 if( ! pool )
562 WARN("Failed to alloc pool %d bytes\n", poolsize );
563 goto err;
565 data = msi_alloc( datasize );
566 if( ! data )
568 WARN("Failed to alloc data %d bytes\n", poolsize );
569 goto err;
572 used = 0;
573 codepage = st->codepage;
574 pool[0]=codepage&0xffff;
575 pool[1]=(codepage>>16);
576 n = 1;
577 for( i=1; i<st->maxcount; i++ )
579 if( !st->strings[i].persistent_refcount )
581 pool[ n*2 ] = 0;
582 pool[ n*2 + 1] = 0;
583 n++;
584 continue;
587 sz = datasize - used;
588 r = msi_id2stringA( st, i, data+used, &sz );
589 if( r != ERROR_SUCCESS )
591 ERR("failed to fetch string\n");
592 sz = 0;
594 if( sz && (sz < (datasize - used ) ) )
595 sz--;
597 if (sz)
598 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
599 else
600 pool[ n*2 + 1 ] = 0;
601 if (sz < 0x10000)
603 pool[ n*2 ] = sz;
604 n++;
606 else
608 pool[ n*2 ] = 0;
609 pool[ n*2 + 2 ] = sz&0xffff;
610 pool[ n*2 + 3 ] = (sz>>16);
611 n += 2;
613 used += sz;
614 if( used > datasize )
616 ERR("oops overran %d >= %d\n", used, datasize);
617 goto err;
621 if( used != datasize )
623 ERR("oops used %d != datasize %d\n", used, datasize);
624 goto err;
627 /* write the streams */
628 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
629 TRACE("Wrote StringData r=%08x\n", r);
630 if( r )
631 goto err;
632 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
633 TRACE("Wrote StringPool r=%08x\n", r);
634 if( r )
635 goto err;
637 ret = ERROR_SUCCESS;
639 err:
640 msi_free( data );
641 msi_free( pool );
643 return ret;