msi: Avoid accessing a freed object.
[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 int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
183 {
184     LPWSTR str;
185     int sz;
186
187     if( !data )
188         return 0;
189     if( !data[0] )
190         return 0;
191     if( n > 0 )
192     {
193         if( st->strings[n].persistent_refcount ||
194             st->strings[n].nonpersistent_refcount )
195             return -1;
196     }
197     else
198     {
199         if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
200         {
201             if (persistence == StringPersistent)
202                 st->strings[n].persistent_refcount += refcount;
203             else
204                 st->strings[n].nonpersistent_refcount += refcount;
205             return n;
206         }
207         n = st_find_free_entry( st );
208         if( n < 0 )
209             return -1;
210     }
211
212     if( n < 1 )
213     {
214         ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
215         return -1;
216     }
217
218     /* allocate a new string */
219     if( len < 0 )
220         len = strlen(data);
221     sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
222     str = msi_alloc( (sz+1)*sizeof(WCHAR) );
223     if( !str )
224         return -1;
225     MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
226     str[sz] = 0;
227
228     set_st_entry( st, n, str, refcount, persistence );
229
230     return n;
231 }
232
233 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
234 {
235     LPWSTR str;
236
237     /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
238
239     if( !data )
240         return 0;
241     if( !data[0] )
242         return 0;
243     if( n > 0 )
244     {
245         if( st->strings[n].persistent_refcount ||
246             st->strings[n].nonpersistent_refcount )
247             return -1;
248     }
249     else
250     {
251         if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
252         {
253             if (persistence == StringPersistent)
254                 st->strings[n].persistent_refcount += refcount;
255             else
256                 st->strings[n].nonpersistent_refcount += refcount;
257             return n;
258         }
259         n = st_find_free_entry( st );
260         if( n < 0 )
261             return -1;
262     }
263
264     if( n < 1 )
265     {
266         ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
267         return -1;
268     }
269
270     /* allocate a new string */
271     if(len<0)
272         len = strlenW(data);
273     TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
274
275     str = msi_alloc( (len+1)*sizeof(WCHAR) );
276     if( !str )
277         return -1;
278     memcpy( str, data, len*sizeof(WCHAR) );
279     str[len] = 0;
280
281     set_st_entry( st, n, str, refcount, persistence );
282
283     return n;
284 }
285
286 /* find the string identified by an id - return null if there's none */
287 const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
288 {
289     static const WCHAR zero[] = { 0 };
290     if( id == 0 )
291         return zero;
292
293     if( id >= st->maxcount )
294         return NULL;
295
296     if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
297         return NULL;
298
299     return st->strings[id].str;
300 }
301
302 /*
303  *  msi_id2stringW
304  *
305  *  [in] st         - pointer to the string table
306  *  [in] id  - id of the string to retrieve
307  *  [out] buffer    - destination of the string
308  *  [in/out] sz     - number of bytes available in the buffer on input
309  *                    number of bytes used on output
310  *
311  *   The size includes the terminating nul character.  Short buffers
312  *  will be filled, but not nul terminated.
313  */
314 UINT msi_id2stringW( const string_table *st, UINT id, LPWSTR buffer, UINT *sz )
315 {
316     UINT len;
317     const WCHAR *str;
318
319     TRACE("Finding string %d of %d\n", id, st->maxcount);
320
321     str = msi_string_lookup_id( st, id );
322     if( !str )
323         return ERROR_FUNCTION_FAILED;
324
325     len = strlenW( str ) + 1;
326
327     if( !buffer )
328     {
329         *sz = len;
330         return ERROR_SUCCESS;
331     }
332
333     if( *sz < len )
334         *sz = len;
335     memcpy( buffer, str, (*sz)*sizeof(WCHAR) ); 
336     *sz = len;
337
338     return ERROR_SUCCESS;
339 }
340
341 /*
342  *  msi_id2stringA
343  *
344  *  [in] st         - pointer to the string table
345  *  [in] id         - id of the string to retrieve
346  *  [out] buffer    - destination of the UTF8 string
347  *  [in/out] sz     - number of bytes available in the buffer on input
348  *                    number of bytes used on output
349  *
350  *   The size includes the terminating nul character.  Short buffers
351  *  will be filled, but not nul terminated.
352  */
353 UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
354 {
355     UINT len;
356     const WCHAR *str;
357     int n;
358
359     TRACE("Finding string %d of %d\n", id, st->maxcount);
360
361     str = msi_string_lookup_id( st, id );
362     if( !str )
363         return ERROR_FUNCTION_FAILED;
364
365     len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
366
367     if( !buffer )
368     {
369         *sz = len;
370         return ERROR_SUCCESS;
371     }
372
373     if( len > *sz )
374     {
375         n = strlenW( str ) + 1;
376         while( n && (len > *sz) )
377             len = WideCharToMultiByte( st->codepage, 0, 
378                            str, --n, NULL, 0, NULL, NULL );
379     }
380     else
381         n = -1;
382
383     *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
384
385     return ERROR_SUCCESS;
386 }
387
388 /*
389  *  msi_string2idW
390  *
391  *  [in] st         - pointer to the string table
392  *  [in] str        - string to find in the string table
393  *  [out] id        - id of the string, if found
394  */
395 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
396 {
397     UINT n, hash = msistring_makehash( str );
398     msistring *se = st->strings;
399
400     for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
401     {
402         if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
403         {
404             *id = n;
405             return ERROR_SUCCESS;
406         }
407     }
408
409     return ERROR_INVALID_PARAMETER;
410 }
411
412 UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
413 {
414     DWORD sz;
415     UINT r = ERROR_INVALID_PARAMETER;
416     LPWSTR str;
417
418     TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
419
420     if( buffer[0] == 0 )
421     {
422         *id = 0;
423         return ERROR_SUCCESS;
424     }
425
426     sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
427     if( sz <= 0 )
428         return r;
429     str = msi_alloc( sz*sizeof(WCHAR) );
430     if( !str )
431         return ERROR_NOT_ENOUGH_MEMORY;
432     MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
433
434     r = msi_string2idW( st, str, id );
435     msi_free( str );
436
437     return r;
438 }
439
440 UINT msi_strcmp( const string_table *st, UINT lval, UINT rval, UINT *res )
441 {
442     const WCHAR *l_str, *r_str;
443
444     l_str = msi_string_lookup_id( st, lval );
445     if( !l_str )
446         return ERROR_INVALID_PARAMETER;
447     
448     r_str = msi_string_lookup_id( st, rval );
449     if( !r_str )
450         return ERROR_INVALID_PARAMETER;
451
452     /* does this do the right thing for all UTF-8 strings? */
453     *res = strcmpW( l_str, r_str );
454
455     return ERROR_SUCCESS;
456 }
457
458 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
459 {
460     UINT i, len, max, holesize;
461
462     if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
463         ERR("oops. element 0 has a string\n");
464
465     *poolsize = 4;
466     *datasize = 0;
467     max = 1;
468     holesize = 0;
469     for( i=1; i<st->maxcount; i++ )
470     {
471         if( !st->strings[i].persistent_refcount )
472             continue;
473         if( st->strings[i].str )
474         {
475             TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
476             len = WideCharToMultiByte( st->codepage, 0,
477                      st->strings[i].str, -1, NULL, 0, NULL, NULL);
478             if( len )
479                 len--;
480             (*datasize) += len;
481             if (len>0xffff)
482                 (*poolsize) += 4;
483             max = i + 1;
484             (*poolsize) += holesize + 4;
485             holesize = 0;
486         }
487         else
488             holesize += 4;
489     }
490     TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
491 }
492
493 static const WCHAR szStringData[] = {
494     '_','S','t','r','i','n','g','D','a','t','a',0 };
495 static const WCHAR szStringPool[] = {
496     '_','S','t','r','i','n','g','P','o','o','l',0 };
497
498 HRESULT msi_init_string_table( IStorage *stg )
499 {
500     USHORT zero[2] = { 0, 0 };
501     UINT ret;
502
503     /* create the StringPool stream... add the zero string to it*/
504     ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
505     if (ret != ERROR_SUCCESS)
506         return E_FAIL;
507
508     /* create the StringData stream... make it zero length */
509     ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
510     if (ret != ERROR_SUCCESS)
511         return E_FAIL;
512
513     return S_OK;
514 }
515
516 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
517 {
518     string_table *st = NULL;
519     CHAR *data = NULL;
520     USHORT *pool = NULL;
521     UINT r, datasize = 0, poolsize = 0, codepage;
522     DWORD i, count, offset, len, n, refs;
523
524     r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
525     if( r != ERROR_SUCCESS)
526         goto end;
527     r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
528     if( r != ERROR_SUCCESS)
529         goto end;
530
531     if ( (poolsize > 4) && (pool[1] & 0x8000) )
532         *bytes_per_strref = LONG_STR_BYTES;
533     else
534         *bytes_per_strref = sizeof(USHORT);
535
536     count = poolsize/4;
537     if( poolsize > 4 )
538         codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
539     else
540         codepage = CP_ACP;
541     st = init_stringtable( count, codepage );
542     if (!st)
543         goto end;
544
545     offset = 0;
546     n = 1;
547     i = 1;
548     while( i<count )
549     {
550         /* the string reference count is always the second word */
551         refs = pool[i*2+1];
552
553         /* empty entries have two zeros, still have a string id */
554         if (pool[i*2] == 0 && refs == 0)
555         {
556             i++;
557             n++;
558             continue;
559         }
560
561         /*
562          * If a string is over 64k, the previous string entry is made null
563          * and its the high word of the length is inserted in the null string's
564          * reference count field.
565          */
566         if( pool[i*2] == 0)
567         {
568             len = (pool[i*2+3] << 16) + pool[i*2+2];
569             i += 2;
570         }
571         else
572         {
573             len = pool[i*2];
574             i += 1;
575         }
576
577         if ( (offset + len) > datasize )
578         {
579             ERR("string table corrupt?\n");
580             break;
581         }
582
583         r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
584         if( r != n )
585             ERR("Failed to add string %d\n", n );
586         n++;
587         offset += len;
588     }
589
590     if ( datasize != offset )
591         ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
592
593     TRACE("Loaded %d strings\n", count);
594
595 end:
596     msi_free( pool );
597     msi_free( data );
598
599     return st;
600 }
601
602 UINT msi_save_string_table( const string_table *st, IStorage *storage )
603 {
604     UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
605     UINT ret = ERROR_FUNCTION_FAILED;
606     CHAR *data = NULL;
607     USHORT *pool = NULL;
608
609     TRACE("\n");
610
611     /* construct the new table in memory first */
612     string_totalsize( st, &datasize, &poolsize );
613
614     TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
615
616     pool = msi_alloc( poolsize );
617     if( ! pool )
618     {
619         WARN("Failed to alloc pool %d bytes\n", poolsize );
620         goto err;
621     }
622     data = msi_alloc( datasize );
623     if( ! data )
624     {
625         WARN("Failed to alloc data %d bytes\n", poolsize );
626         goto err;
627     }
628
629     used = 0;
630     codepage = st->codepage;
631     pool[0]=codepage&0xffff;
632     pool[1]=(codepage>>16);
633     n = 1;
634     for( i=1; i<st->maxcount; i++ )
635     {
636         if( !st->strings[i].persistent_refcount )
637             continue;
638         sz = datasize - used;
639         r = msi_id2stringA( st, i, data+used, &sz );
640         if( r != ERROR_SUCCESS )
641         {
642             ERR("failed to fetch string\n");
643             sz = 0;
644         }
645         if( sz && (sz < (datasize - used ) ) )
646             sz--;
647
648         if (sz)
649             pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
650         else
651             pool[ n*2 + 1 ] = 0;
652         if (sz < 0x10000)
653         {
654             pool[ n*2 ] = sz;
655             n++;
656         }
657         else
658         {
659             pool[ n*2 ] = 0;
660             pool[ n*2 + 2 ] = sz&0xffff;
661             pool[ n*2 + 3 ] = (sz>>16);
662             n += 2;
663         }
664         used += sz;
665         if( used > datasize  )
666         {
667             ERR("oops overran %d >= %d\n", used, datasize);
668             goto err;
669         }
670     }
671
672     if( used != datasize )
673     {
674         ERR("oops used %d != datasize %d\n", used, datasize);
675         goto err;
676     }
677
678     /* write the streams */
679     r = write_stream_data( storage, szStringData, data, datasize, TRUE );
680     TRACE("Wrote StringData r=%08x\n", r);
681     if( r )
682         goto err;
683     r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
684     TRACE("Wrote StringPool r=%08x\n", r);
685     if( r )
686         goto err;
687
688     ret = ERROR_SUCCESS;
689
690 err:
691     msi_free( data );
692     msi_free( pool );
693
694     return ret;
695 }