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