advapi32/tests: Allow ERROR_ACCESS_DENIED for newer Win10.
[wine.git] / dlls / msi / string.c
blob7383fddbed349e901f040e45fa2dc87ac7f0e45e
1 /*
2 * String Table Functions
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
6 * Copyright 2010 Hans Leidekker for CodeWeavers
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.
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.
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
23 #define COBJMACROS
25 #include <stdarg.h>
26 #include <assert.h>
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"
40 #include "query.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 struct msistring
46 USHORT persistent_refcount;
47 USHORT nonpersistent_refcount;
48 WCHAR *data;
49 int len;
52 struct string_table
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 */
62 static BOOL validate_codepage( UINT codepage )
64 if (codepage != CP_ACP && !IsValidCodePage( codepage ))
66 WARN("invalid codepage %u\n", codepage);
67 return FALSE;
69 return TRUE;
72 static string_table *init_stringtable( int entries, UINT codepage )
74 string_table *st;
76 if (!validate_codepage( codepage ))
77 return NULL;
79 st = msi_alloc( sizeof (string_table) );
80 if( !st )
81 return NULL;
82 if( entries < 1 )
83 entries = 1;
85 st->strings = msi_alloc_zero( sizeof(struct msistring) * entries );
86 if( !st->strings )
88 msi_free( st );
89 return NULL;
92 st->sorted = msi_alloc( sizeof (UINT) * entries );
93 if( !st->sorted )
95 msi_free( st->strings );
96 msi_free( st );
97 return NULL;
100 st->maxcount = entries;
101 st->freeslot = 1;
102 st->codepage = codepage;
103 st->sortcount = 0;
105 return st;
108 VOID msi_destroy_stringtable( string_table *st )
110 UINT i;
112 for( i=0; i<st->maxcount; i++ )
114 if( st->strings[i].persistent_refcount ||
115 st->strings[i].nonpersistent_refcount )
116 msi_free( st->strings[i].data );
118 msi_free( st->strings );
119 msi_free( st->sorted );
120 msi_free( st );
123 static int st_find_free_entry( string_table *st )
125 UINT i, sz, *s;
126 struct msistring *p;
128 TRACE("%p\n", st);
130 if( st->freeslot )
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;
137 for( i = 1; i < st->maxcount; i++ )
138 if( !st->strings[i].persistent_refcount &&
139 !st->strings[i].nonpersistent_refcount )
140 return i;
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;
148 s = msi_realloc( st->sorted, sz*sizeof(UINT) );
149 if( !s )
151 msi_free( p );
152 return -1;
155 st->strings = p;
156 st->sorted = s;
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;
166 static inline int cmp_string( const WCHAR *str1, int len1, const WCHAR *str2, int len2 )
168 if (len1 < len2) return -1;
169 else if (len1 > len2) return 1;
170 while (len1)
172 if (*str1 == *str2) { str1++; str2++; }
173 else return *str1 - *str2;
174 len1--;
176 return 0;
179 static int find_insert_index( const string_table *st, UINT string_id )
181 int i, c, low = 0, high = st->sortcount - 1;
183 while (low <= high)
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 */
195 return high + 1;
198 static void insert_string_sorted( string_table *st, UINT string_id )
200 int i;
202 i = find_insert_index( st, string_id );
203 if (i == -1)
204 return;
206 memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
207 st->sorted[i] = string_id;
208 st->sortcount++;
211 static void set_st_entry( string_table *st, UINT n, WCHAR *str, int len, USHORT refcount,
212 enum StringPersistence persistence )
214 if (persistence == StringPersistent)
216 st->strings[n].persistent_refcount = refcount;
217 st->strings[n].nonpersistent_refcount = 0;
219 else
221 st->strings[n].persistent_refcount = 0;
222 st->strings[n].nonpersistent_refcount = refcount;
225 st->strings[n].data = str;
226 st->strings[n].len = len;
228 insert_string_sorted( st, n );
230 if( n < st->maxcount )
231 st->freeslot = n + 1;
234 static UINT string2id( const string_table *st, const char *buffer, UINT *id )
236 int sz;
237 UINT r = ERROR_INVALID_PARAMETER;
238 LPWSTR str;
240 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
242 if( buffer[0] == 0 )
244 *id = 0;
245 return ERROR_SUCCESS;
248 if (!(sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 )))
249 return r;
250 str = msi_alloc( sz*sizeof(WCHAR) );
251 if( !str )
252 return ERROR_NOT_ENOUGH_MEMORY;
253 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
255 r = msi_string2id( st, str, sz - 1, id );
256 msi_free( str );
257 return r;
260 static int add_string( string_table *st, UINT n, const char *data, UINT len, USHORT refcount, enum StringPersistence persistence )
262 LPWSTR str;
263 int sz;
265 if( !data || !len )
266 return 0;
267 if( n > 0 )
269 if( st->strings[n].persistent_refcount ||
270 st->strings[n].nonpersistent_refcount )
271 return -1;
273 else
275 if (string2id( st, data, &n ) == ERROR_SUCCESS)
277 if (persistence == StringPersistent)
278 st->strings[n].persistent_refcount += refcount;
279 else
280 st->strings[n].nonpersistent_refcount += refcount;
281 return n;
283 n = st_find_free_entry( st );
284 if( n == -1 )
285 return -1;
288 if( n < 1 )
290 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
291 return -1;
294 /* allocate a new string */
295 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
296 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
297 if( !str )
298 return -1;
299 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
300 str[sz] = 0;
302 set_st_entry( st, n, str, sz, refcount, persistence );
303 return n;
306 int msi_add_string( string_table *st, const WCHAR *data, int len, enum StringPersistence persistence )
308 UINT n;
309 LPWSTR str;
311 if( !data )
312 return 0;
314 if (len < 0) len = strlenW( data );
316 if( !data[0] && !len )
317 return 0;
319 if (msi_string2id( st, data, len, &n) == ERROR_SUCCESS )
321 if (persistence == StringPersistent)
322 st->strings[n].persistent_refcount++;
323 else
324 st->strings[n].nonpersistent_refcount++;
325 return n;
328 n = st_find_free_entry( st );
329 if( n == -1 )
330 return -1;
332 /* allocate a new string */
333 TRACE( "%s, n = %d len = %d\n", debugstr_wn(data, len), n, len );
335 str = msi_alloc( (len+1)*sizeof(WCHAR) );
336 if( !str )
337 return -1;
338 memcpy( str, data, len*sizeof(WCHAR) );
339 str[len] = 0;
341 set_st_entry( st, n, str, len, 1, persistence );
342 return n;
345 /* find the string identified by an id - return null if there's none */
346 const WCHAR *msi_string_lookup( const string_table *st, UINT id, int *len )
348 if( id == 0 )
350 if (len) *len = 0;
351 return szEmpty;
353 if( id >= st->maxcount )
354 return NULL;
356 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
357 return NULL;
359 if (len) *len = st->strings[id].len;
361 return st->strings[id].data;
365 * id2string
367 * [in] st - pointer to the string table
368 * [in] id - id of the string to retrieve
369 * [out] buffer - destination of the UTF8 string
370 * [in/out] sz - number of bytes available in the buffer on input
371 * number of bytes used on output
373 * Returned string is not nul terminated.
375 static UINT id2string( const string_table *st, UINT id, char *buffer, UINT *sz )
377 int len, lenW;
378 const WCHAR *str;
380 TRACE("Finding string %d of %d\n", id, st->maxcount);
382 str = msi_string_lookup( st, id, &lenW );
383 if( !str )
384 return ERROR_FUNCTION_FAILED;
386 len = WideCharToMultiByte( st->codepage, 0, str, lenW, NULL, 0, NULL, NULL );
387 if( *sz < len )
389 *sz = len;
390 return ERROR_MORE_DATA;
392 *sz = WideCharToMultiByte( st->codepage, 0, str, lenW, buffer, *sz, NULL, NULL );
393 return ERROR_SUCCESS;
397 * msi_string2id
399 * [in] st - pointer to the string table
400 * [in] str - string to find in the string table
401 * [out] id - id of the string, if found
403 UINT msi_string2id( const string_table *st, const WCHAR *str, int len, UINT *id )
405 int i, c, low = 0, high = st->sortcount - 1;
407 if (len < 0) len = strlenW( str );
409 while (low <= high)
411 i = (low + high) / 2;
412 c = cmp_string( str, len, st->strings[st->sorted[i]].data, st->strings[st->sorted[i]].len );
414 if (c < 0)
415 high = i - 1;
416 else if (c > 0)
417 low = i + 1;
418 else
420 *id = st->sorted[i];
421 return ERROR_SUCCESS;
424 return ERROR_INVALID_PARAMETER;
427 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
429 UINT i, len, holesize;
431 if( st->strings[0].data || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
432 ERR("oops. element 0 has a string\n");
434 *poolsize = 4;
435 *datasize = 0;
436 holesize = 0;
437 for( i=1; i<st->maxcount; i++ )
439 if( !st->strings[i].persistent_refcount )
441 TRACE("[%u] nonpersistent = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
442 (*poolsize) += 4;
444 else if( st->strings[i].data )
446 TRACE("[%u] = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
447 len = WideCharToMultiByte( st->codepage, 0, st->strings[i].data, st->strings[i].len + 1,
448 NULL, 0, NULL, NULL);
449 if( len )
450 len--;
451 (*datasize) += len;
452 if (len>0xffff)
453 (*poolsize) += 4;
454 (*poolsize) += holesize + 4;
455 holesize = 0;
457 else
458 holesize += 4;
460 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
463 HRESULT msi_init_string_table( IStorage *stg )
465 USHORT zero[2] = { 0, 0 };
466 UINT ret;
468 /* create the StringPool stream... add the zero string to it*/
469 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
470 if (ret != ERROR_SUCCESS)
471 return E_FAIL;
473 /* create the StringData stream... make it zero length */
474 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
475 if (ret != ERROR_SUCCESS)
476 return E_FAIL;
478 return S_OK;
481 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
483 string_table *st = NULL;
484 CHAR *data = NULL;
485 USHORT *pool = NULL;
486 UINT r, datasize = 0, poolsize = 0, codepage;
487 DWORD i, count, offset, len, n, refs;
489 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
490 if( r != ERROR_SUCCESS)
491 goto end;
492 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
493 if( r != ERROR_SUCCESS)
494 goto end;
496 if ( (poolsize > 4) && (pool[1] & 0x8000) )
497 *bytes_per_strref = LONG_STR_BYTES;
498 else
499 *bytes_per_strref = sizeof(USHORT);
501 count = poolsize/4;
502 if( poolsize > 4 )
503 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
504 else
505 codepage = CP_ACP;
506 st = init_stringtable( count, codepage );
507 if (!st)
508 goto end;
510 offset = 0;
511 n = 1;
512 i = 1;
513 while( i<count )
515 /* the string reference count is always the second word */
516 refs = pool[i*2+1];
518 /* empty entries have two zeros, still have a string id */
519 if (pool[i*2] == 0 && refs == 0)
521 i++;
522 n++;
523 continue;
527 * If a string is over 64k, the previous string entry is made null
528 * and its the high word of the length is inserted in the null string's
529 * reference count field.
531 if( pool[i*2] == 0)
533 len = (pool[i*2+3] << 16) + pool[i*2+2];
534 i += 2;
536 else
538 len = pool[i*2];
539 i += 1;
542 if ( (offset + len) > datasize )
544 ERR("string table corrupt?\n");
545 break;
548 r = add_string( st, n, data+offset, len, refs, StringPersistent );
549 if( r != n )
550 ERR("Failed to add string %d\n", n );
551 n++;
552 offset += len;
555 if ( datasize != offset )
556 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
558 TRACE("Loaded %d strings\n", count);
560 end:
561 msi_free( pool );
562 msi_free( data );
564 return st;
567 UINT msi_save_string_table( const string_table *st, IStorage *storage, UINT *bytes_per_strref )
569 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
570 UINT ret = ERROR_FUNCTION_FAILED;
571 CHAR *data = NULL;
572 USHORT *pool = NULL;
574 TRACE("\n");
576 /* construct the new table in memory first */
577 string_totalsize( st, &datasize, &poolsize );
579 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
581 pool = msi_alloc( poolsize );
582 if( ! pool )
584 WARN("Failed to alloc pool %d bytes\n", poolsize );
585 goto err;
587 data = msi_alloc( datasize );
588 if( ! data )
590 WARN("Failed to alloc data %d bytes\n", datasize );
591 goto err;
594 used = 0;
595 codepage = st->codepage;
596 pool[0] = codepage & 0xffff;
597 pool[1] = codepage >> 16;
598 if (st->maxcount > 0xffff)
600 pool[1] |= 0x8000;
601 *bytes_per_strref = LONG_STR_BYTES;
603 else
604 *bytes_per_strref = sizeof(USHORT);
606 n = 1;
607 for( i=1; i<st->maxcount; i++ )
609 if( !st->strings[i].persistent_refcount )
611 pool[ n*2 ] = 0;
612 pool[ n*2 + 1] = 0;
613 n++;
614 continue;
617 sz = datasize - used;
618 r = id2string( st, i, data+used, &sz );
619 if( r != ERROR_SUCCESS )
621 ERR("failed to fetch string\n");
622 sz = 0;
625 if (sz)
626 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
627 else
628 pool[ n*2 + 1 ] = 0;
629 if (sz < 0x10000)
631 pool[ n*2 ] = sz;
632 n++;
634 else
636 pool[ n*2 ] = 0;
637 pool[ n*2 + 2 ] = sz&0xffff;
638 pool[ n*2 + 3 ] = (sz>>16);
639 n += 2;
641 used += sz;
642 if( used > datasize )
644 ERR("oops overran %d >= %d\n", used, datasize);
645 goto err;
649 if( used != datasize )
651 ERR("oops used %d != datasize %d\n", used, datasize);
652 goto err;
655 /* write the streams */
656 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
657 TRACE("Wrote StringData r=%08x\n", r);
658 if( r )
659 goto err;
660 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
661 TRACE("Wrote StringPool r=%08x\n", r);
662 if( r )
663 goto err;
665 ret = ERROR_SUCCESS;
667 err:
668 msi_free( data );
669 msi_free( pool );
671 return ret;
674 UINT msi_get_string_table_codepage( const string_table *st )
676 return st->codepage;
679 UINT msi_set_string_table_codepage( string_table *st, UINT codepage )
681 if (validate_codepage( codepage ))
683 st->codepage = codepage;
684 return ERROR_SUCCESS;
686 return ERROR_FUNCTION_FAILED;