winex11: Fix XRender_GetSrcAreaStretch compile bug for when XRender isn't around.
[wine] / dlls / msi / string.c
1 /*
2  * String Table Functions
3  *
4  * Copyright 2002-2004, Mike McCormack for CodeWeavers
5  * Copyright 2007 Robert Shearman for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #define COBJMACROS
23
24 #include <stdarg.h>
25 #include <assert.h>
26
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"
38
39 #include "query.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
42
43 #define HASH_SIZE 0x101
44 #define LONG_STR_BYTES 3
45
46 typedef struct _msistring
47 {
48     int hash_next;
49     UINT persistent_refcount;
50     UINT nonpersistent_refcount;
51     LPWSTR str;
52 } msistring;
53
54 struct string_table
55 {
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) */
61 };
62
63 static UINT msistring_makehash( const WCHAR *str )
64 {
65     UINT hash = 0;
66
67     if (str==NULL)
68         return hash;
69
70     while( *str )
71     {
72         hash ^= *str++;
73         hash *= 53;
74         hash = (hash<<5) | (hash>>27);
75     }
76     return hash % HASH_SIZE;
77 }
78
79 static string_table *init_stringtable( int entries, UINT codepage )
80 {
81     string_table *st;
82     int i;
83
84     if (codepage != CP_ACP && !IsValidCodePage(codepage))
85     {
86         ERR("invalid codepage %d\n", codepage);
87         return NULL;
88     }
89
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 )
97     {
98         msi_free( st );
99         return NULL;    
100     }
101     st->maxcount = entries;
102     st->freeslot = 1;
103     st->codepage = codepage;
104
105     for( i=0; i<HASH_SIZE; i++ )
106         st->hash[i] = -1;
107
108     return st;
109 }
110
111 VOID msi_destroy_stringtable( string_table *st )
112 {
113     UINT i;
114
115     for( i=0; i<st->maxcount; i++ )
116     {
117         if( st->strings[i].persistent_refcount ||
118             st->strings[i].nonpersistent_refcount )
119             msi_free( st->strings[i].str );
120     }
121     msi_free( st->strings );
122     msi_free( st );
123 }
124
125 static int st_find_free_entry( string_table *st )
126 {
127     UINT i, sz;
128     msistring *p;
129
130     TRACE("%p\n", st);
131
132     if( st->freeslot )
133     {
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;
138     }
139     for( i = 1; i < st->maxcount; i++ )
140         if( !st->strings[i].persistent_refcount &&
141             !st->strings[i].nonpersistent_refcount )
142             return i;
143
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;
156 }
157
158 static void set_st_entry( string_table *st, UINT n, LPWSTR str, UINT refcount, enum StringPersistence persistence )
159 {
160     UINT hash = msistring_makehash( str );
161
162     if (persistence == StringPersistent)
163     {
164         st->strings[n].persistent_refcount = refcount;
165         st->strings[n].nonpersistent_refcount = 0;
166     }
167     else
168     {
169         st->strings[n].persistent_refcount = 0;
170         st->strings[n].nonpersistent_refcount = refcount;
171     }
172
173     st->strings[n].str = str;
174
175     st->strings[n].hash_next = st->hash[hash];
176     st->hash[hash] = n;
177
178     if( n < st->maxcount )
179         st->freeslot = n + 1;
180 }
181
182 static UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
183 {
184     DWORD sz;
185     UINT r = ERROR_INVALID_PARAMETER;
186     LPWSTR str;
187
188     TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
189
190     if( buffer[0] == 0 )
191     {
192         *id = 0;
193         return ERROR_SUCCESS;
194     }
195
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 );
203
204     r = msi_string2idW( st, str, id );
205     msi_free( str );
206
207     return r;
208 }
209
210 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
211 {
212     LPWSTR str;
213     int sz;
214
215     if( !data )
216         return 0;
217     if( !data[0] )
218         return 0;
219     if( n > 0 )
220     {
221         if( st->strings[n].persistent_refcount ||
222             st->strings[n].nonpersistent_refcount )
223             return -1;
224     }
225     else
226     {
227         if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
228         {
229             if (persistence == StringPersistent)
230                 st->strings[n].persistent_refcount += refcount;
231             else
232                 st->strings[n].nonpersistent_refcount += refcount;
233             return n;
234         }
235         n = st_find_free_entry( st );
236         if( n == -1 )
237             return -1;
238     }
239
240     if( n < 1 )
241     {
242         ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
243         return -1;
244     }
245
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;
255
256     set_st_entry( st, n, str, refcount, persistence );
257
258     return n;
259 }
260
261 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
262 {
263     LPWSTR str;
264
265     /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
266
267     if( !data )
268         return 0;
269     if( !data[0] )
270         return 0;
271     if( n > 0 )
272     {
273         if( st->strings[n].persistent_refcount ||
274             st->strings[n].nonpersistent_refcount )
275             return -1;
276     }
277     else
278     {
279         if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
280         {
281             if (persistence == StringPersistent)
282                 st->strings[n].persistent_refcount += refcount;
283             else
284                 st->strings[n].nonpersistent_refcount += refcount;
285             return n;
286         }
287         n = st_find_free_entry( st );
288         if( n == -1 )
289             return -1;
290     }
291
292     if( n < 1 )
293     {
294         ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
295         return -1;
296     }
297
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 );
302
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;
308
309     set_st_entry( st, n, str, refcount, persistence );
310
311     return n;
312 }
313
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 )
316 {
317     static const WCHAR zero[] = { 0 };
318     if( id == 0 )
319         return zero;
320
321     if( id >= st->maxcount )
322         return NULL;
323
324     if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
325         return NULL;
326
327     return st->strings[id].str;
328 }
329
330 /*
331  *  msi_id2stringA
332  *
333  *  [in] st         - pointer to the string table
334  *  [in] id         - id of the string to retrieve
335  *  [out] buffer    - destination of the UTF8 string
336  *  [in/out] sz     - number of bytes available in the buffer on input
337  *                    number of bytes used on output
338  *
339  *   The size includes the terminating nul character.  Short buffers
340  *  will be filled, but not nul terminated.
341  */
342 static UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
343 {
344     UINT len;
345     const WCHAR *str;
346     int n;
347
348     TRACE("Finding string %d of %d\n", id, st->maxcount);
349
350     str = msi_string_lookup_id( st, id );
351     if( !str )
352         return ERROR_FUNCTION_FAILED;
353
354     len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
355
356     if( !buffer )
357     {
358         *sz = len;
359         return ERROR_SUCCESS;
360     }
361
362     if( len > *sz )
363     {
364         n = strlenW( str ) + 1;
365         while( n && (len > *sz) )
366             len = WideCharToMultiByte( st->codepage, 0, 
367                            str, --n, NULL, 0, NULL, NULL );
368     }
369     else
370         n = -1;
371
372     *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
373
374     return ERROR_SUCCESS;
375 }
376
377 /*
378  *  msi_string2idW
379  *
380  *  [in] st         - pointer to the string table
381  *  [in] str        - string to find in the string table
382  *  [out] id        - id of the string, if found
383  */
384 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
385 {
386     UINT n, hash = msistring_makehash( str );
387     msistring *se = st->strings;
388
389     for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
390     {
391         if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
392         {
393             *id = n;
394             return ERROR_SUCCESS;
395         }
396     }
397
398     return ERROR_INVALID_PARAMETER;
399 }
400
401 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
402 {
403     UINT i, len, holesize;
404
405     if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
406         ERR("oops. element 0 has a string\n");
407
408     *poolsize = 4;
409     *datasize = 0;
410     holesize = 0;
411     for( i=1; i<st->maxcount; i++ )
412     {
413         if( !st->strings[i].persistent_refcount )
414         {
415             TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
416             (*poolsize) += 4;
417         }
418         else if( st->strings[i].str )
419         {
420             TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
421             len = WideCharToMultiByte( st->codepage, 0,
422                      st->strings[i].str, -1, NULL, 0, NULL, NULL);
423             if( len )
424                 len--;
425             (*datasize) += len;
426             if (len>0xffff)
427                 (*poolsize) += 4;
428             (*poolsize) += holesize + 4;
429             holesize = 0;
430         }
431         else
432             holesize += 4;
433     }
434     TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
435 }
436
437 static const WCHAR szStringData[] = {
438     '_','S','t','r','i','n','g','D','a','t','a',0 };
439 static const WCHAR szStringPool[] = {
440     '_','S','t','r','i','n','g','P','o','o','l',0 };
441
442 HRESULT msi_init_string_table( IStorage *stg )
443 {
444     USHORT zero[2] = { 0, 0 };
445     UINT ret;
446
447     /* create the StringPool stream... add the zero string to it*/
448     ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
449     if (ret != ERROR_SUCCESS)
450         return E_FAIL;
451
452     /* create the StringData stream... make it zero length */
453     ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
454     if (ret != ERROR_SUCCESS)
455         return E_FAIL;
456
457     return S_OK;
458 }
459
460 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
461 {
462     string_table *st = NULL;
463     CHAR *data = NULL;
464     USHORT *pool = NULL;
465     UINT r, datasize = 0, poolsize = 0, codepage;
466     DWORD i, count, offset, len, n, refs;
467
468     r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
469     if( r != ERROR_SUCCESS)
470         goto end;
471     r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
472     if( r != ERROR_SUCCESS)
473         goto end;
474
475     if ( (poolsize > 4) && (pool[1] & 0x8000) )
476         *bytes_per_strref = LONG_STR_BYTES;
477     else
478         *bytes_per_strref = sizeof(USHORT);
479
480     count = poolsize/4;
481     if( poolsize > 4 )
482         codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
483     else
484         codepage = CP_ACP;
485     st = init_stringtable( count, codepage );
486     if (!st)
487         goto end;
488
489     offset = 0;
490     n = 1;
491     i = 1;
492     while( i<count )
493     {
494         /* the string reference count is always the second word */
495         refs = pool[i*2+1];
496
497         /* empty entries have two zeros, still have a string id */
498         if (pool[i*2] == 0 && refs == 0)
499         {
500             i++;
501             n++;
502             continue;
503         }
504
505         /*
506          * If a string is over 64k, the previous string entry is made null
507          * and its the high word of the length is inserted in the null string's
508          * reference count field.
509          */
510         if( pool[i*2] == 0)
511         {
512             len = (pool[i*2+3] << 16) + pool[i*2+2];
513             i += 2;
514         }
515         else
516         {
517             len = pool[i*2];
518             i += 1;
519         }
520
521         if ( (offset + len) > datasize )
522         {
523             ERR("string table corrupt?\n");
524             break;
525         }
526
527         r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
528         if( r != n )
529             ERR("Failed to add string %d\n", n );
530         n++;
531         offset += len;
532     }
533
534     if ( datasize != offset )
535         ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
536
537     TRACE("Loaded %d strings\n", count);
538
539 end:
540     msi_free( pool );
541     msi_free( data );
542
543     return st;
544 }
545
546 UINT msi_save_string_table( const string_table *st, IStorage *storage )
547 {
548     UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
549     UINT ret = ERROR_FUNCTION_FAILED;
550     CHAR *data = NULL;
551     USHORT *pool = NULL;
552
553     TRACE("\n");
554
555     /* construct the new table in memory first */
556     string_totalsize( st, &datasize, &poolsize );
557
558     TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
559
560     pool = msi_alloc( poolsize );
561     if( ! pool )
562     {
563         WARN("Failed to alloc pool %d bytes\n", poolsize );
564         goto err;
565     }
566     data = msi_alloc( datasize );
567     if( ! data )
568     {
569         WARN("Failed to alloc data %d bytes\n", poolsize );
570         goto err;
571     }
572
573     used = 0;
574     codepage = st->codepage;
575     pool[0]=codepage&0xffff;
576     pool[1]=(codepage>>16);
577     n = 1;
578     for( i=1; i<st->maxcount; i++ )
579     {
580         if( !st->strings[i].persistent_refcount )
581         {
582             pool[ n*2 ] = 0;
583             pool[ n*2 + 1] = 0;
584             n++;
585             continue;
586         }
587
588         sz = datasize - used;
589         r = msi_id2stringA( st, i, data+used, &sz );
590         if( r != ERROR_SUCCESS )
591         {
592             ERR("failed to fetch string\n");
593             sz = 0;
594         }
595         if( sz && (sz < (datasize - used ) ) )
596             sz--;
597
598         if (sz)
599             pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
600         else
601             pool[ n*2 + 1 ] = 0;
602         if (sz < 0x10000)
603         {
604             pool[ n*2 ] = sz;
605             n++;
606         }
607         else
608         {
609             pool[ n*2 ] = 0;
610             pool[ n*2 + 2 ] = sz&0xffff;
611             pool[ n*2 + 3 ] = (sz>>16);
612             n += 2;
613         }
614         used += sz;
615         if( used > datasize  )
616         {
617             ERR("oops overran %d >= %d\n", used, datasize);
618             goto err;
619         }
620     }
621
622     if( used != datasize )
623     {
624         ERR("oops used %d != datasize %d\n", used, datasize);
625         goto err;
626     }
627
628     /* write the streams */
629     r = write_stream_data( storage, szStringData, data, datasize, TRUE );
630     TRACE("Wrote StringData r=%08x\n", r);
631     if( r )
632         goto err;
633     r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
634     TRACE("Wrote StringPool r=%08x\n", r);
635     if( r )
636         goto err;
637
638     ret = ERROR_SUCCESS;
639
640 err:
641     msi_free( data );
642     msi_free( pool );
643
644     return ret;
645 }