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