string.c 17.3 KB
Newer Older
1
/*
2
 * String Table Functions
3
 *
4
 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5
 * Copyright 2007 Robert Shearman for CodeWeavers
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23
#define COBJMACROS

24 25 26 27 28 29 30
#include <stdarg.h>
#include <assert.h>

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wine/debug.h"
31
#include "wine/unicode.h"
32 33 34 35 36 37 38 39 40
#include "msi.h"
#include "msiquery.h"
#include "objbase.h"
#include "objidl.h"
#include "msipriv.h"
#include "winnls.h"

#include "query.h"

41
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
42

43
#define HASH_SIZE 0x101
44
#define LONG_STR_BYTES 3
45

46 47
typedef struct _msistring
{
48
    int hash_next;
49 50
    UINT persistent_refcount;
    UINT nonpersistent_refcount;
51
    LPWSTR str;
52 53 54 55
} msistring;

struct string_table
{
Mike McCormack's avatar
Mike McCormack committed
56
    UINT maxcount;         /* the number of strings */
57
    UINT freeslot;
58
    UINT codepage;
59
    int hash[HASH_SIZE];
60 61 62
    msistring *strings; /* an array of strings (in the tree) */
};

63
static UINT msistring_makehash( const WCHAR *str )
64
{
65
    UINT hash = 0;
66

67 68 69
    if (str==NULL)
        return hash;

70 71 72 73
    while( *str )
    {
        hash ^= *str++;
        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 85 86 87 88 89
    if (codepage != CP_ACP && !IsValidCodePage(codepage))
    {
        ERR("invalid codepage %d\n", codepage);
        return NULL;
    }

90
    st = msi_alloc( sizeof (string_table) );
91 92
    if( !st )
        return NULL;    
93 94
    if( entries < 1 )
        entries = 1;
95
    st->strings = msi_alloc_zero( sizeof (msistring) * entries );
96
    if( !st->strings )
97
    {
98
        msi_free( st );
99 100
        return NULL;    
    }
Mike McCormack's avatar
Mike McCormack committed
101
    st->maxcount = entries;
102
    st->freeslot = 1;
103
    st->codepage = codepage;
104

105 106 107
    for( i=0; i<HASH_SIZE; i++ )
        st->hash[i] = -1;

108 109 110 111 112 113 114
    return st;
}

VOID msi_destroy_stringtable( string_table *st )
{
    UINT i;

Mike McCormack's avatar
Mike McCormack committed
115
    for( i=0; i<st->maxcount; i++ )
116
    {
117 118
        if( st->strings[i].persistent_refcount ||
            st->strings[i].nonpersistent_refcount )
119
            msi_free( st->strings[i].str );
120
    }
121 122
    msi_free( st->strings );
    msi_free( st );
123 124 125 126
}

static int st_find_free_entry( string_table *st )
{
127
    UINT i, sz;
128
    msistring *p;
129

130 131
    TRACE("%p\n", st);

Mike McCormack's avatar
Mike McCormack committed
132 133 134
    if( st->freeslot )
    {
        for( i = st->freeslot; i < st->maxcount; i++ )
135 136
            if( !st->strings[i].persistent_refcount &&
                !st->strings[i].nonpersistent_refcount )
Mike McCormack's avatar
Mike McCormack committed
137 138 139
                return i;
    }
    for( i = 1; i < st->maxcount; i++ )
140 141
        if( !st->strings[i].persistent_refcount &&
            !st->strings[i].nonpersistent_refcount )
142 143
            return i;

144
    /* dynamically resize */
Mike McCormack's avatar
Mike McCormack committed
145
    sz = st->maxcount + 1 + st->maxcount/2;
146
    p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
147 148 149
    if( !p )
        return -1;
    st->strings = p;
Mike McCormack's avatar
Mike McCormack committed
150 151
    st->freeslot = st->maxcount;
    st->maxcount = sz;
152 153
    if( st->strings[st->freeslot].persistent_refcount ||
        st->strings[st->freeslot].nonpersistent_refcount )
154 155
        ERR("oops. expected freeslot to be free...\n");
    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 161
    UINT hash = msistring_makehash( str );

162 163 164 165 166 167 168 169 170 171 172
    if (persistence == StringPersistent)
    {
        st->strings[n].persistent_refcount = refcount;
        st->strings[n].nonpersistent_refcount = 0;
    }
    else
    {
        st->strings[n].persistent_refcount = 0;
        st->strings[n].nonpersistent_refcount = refcount;
    }

173 174 175 176 177 178 179
    st->strings[n].str = str;

    st->strings[n].hash_next = st->hash[hash];
    st->hash[hash] = n;

    if( n < st->maxcount )
        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 186
    int sz;

187 188
    if( !data )
        return 0;
189 190 191 192
    if( !data[0] )
        return 0;
    if( n > 0 )
    {
193 194
        if( st->strings[n].persistent_refcount ||
            st->strings[n].nonpersistent_refcount )
195 196 197 198 199 200
            return -1;
    }
    else
    {
        if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
        {
201 202 203 204
            if (persistence == StringPersistent)
                st->strings[n].persistent_refcount += refcount;
            else
                st->strings[n].nonpersistent_refcount += refcount;
205 206 207
            return n;
        }
        n = st_find_free_entry( st );
208
        if( n == -1 )
209 210
            return -1;
    }
211

Mike McCormack's avatar
Mike McCormack committed
212 213 214 215 216 217
    if( n < 1 )
    {
        ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
        return -1;
    }

218 219
    /* allocate a new string */
    if( len < 0 )
220 221
        len = strlen(data);
    sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
222 223
    str = msi_alloc( (sz+1)*sizeof(WCHAR) );
    if( !str )
224
        return -1;
225 226
    MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
    str[sz] = 0;
227

228
    set_st_entry( st, n, str, refcount, persistence );
229 230 231 232

    return n;
}

233
int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
234
{
235 236
    LPWSTR str;

237 238
    /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */

239 240
    if( !data )
        return 0;
241 242 243 244
    if( !data[0] )
        return 0;
    if( n > 0 )
    {
245 246
        if( st->strings[n].persistent_refcount ||
            st->strings[n].nonpersistent_refcount )
247 248 249 250
            return -1;
    }
    else
    {
251
        if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
252
        {
253 254 255 256
            if (persistence == StringPersistent)
                st->strings[n].persistent_refcount += refcount;
            else
                st->strings[n].nonpersistent_refcount += refcount;
257 258 259
            return n;
        }
        n = st_find_free_entry( st );
260
        if( n == -1 )
261 262 263
            return -1;
    }

Mike McCormack's avatar
Mike McCormack committed
264 265 266 267 268 269
    if( n < 1 )
    {
        ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
        return -1;
    }

270
    /* allocate a new string */
271 272 273 274
    if(len<0)
        len = strlenW(data);
    TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );

275 276
    str = msi_alloc( (len+1)*sizeof(WCHAR) );
    if( !str )
277
        return -1;
278 279
    memcpy( str, data, len*sizeof(WCHAR) );
    str[len] = 0;
280

281
    set_st_entry( st, n, str, refcount, persistence );
282 283 284 285

    return n;
}

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

Mike McCormack's avatar
Mike McCormack committed
293
    if( id >= st->maxcount )
294 295
        return NULL;

296
    if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
297 298 299 300 301 302 303 304 305
        return NULL;

    return st->strings[id].str;
}

/*
 *  msi_id2stringW
 *
 *  [in] st         - pointer to the string table
306
 *  [in] id  - id of the string to retrieve
307 308 309 310 311 312 313
 *  [out] buffer    - destination of the string
 *  [in/out] sz     - number of bytes available in the buffer on input
 *                    number of bytes used on output
 *
 *   The size includes the terminating nul character.  Short buffers
 *  will be filled, but not nul terminated.
 */
314
UINT msi_id2stringW( const string_table *st, UINT id, LPWSTR buffer, UINT *sz )
315 316
{
    UINT len;
317
    const WCHAR *str;
318

Mike McCormack's avatar
Mike McCormack committed
319
    TRACE("Finding string %d of %d\n", id, st->maxcount);
320

321
    str = msi_string_lookup_id( st, id );
322
    if( !str )
323 324
        return ERROR_FUNCTION_FAILED;

325
    len = strlenW( str ) + 1;
326 327 328

    if( !buffer )
    {
329
        *sz = len;
330 331 332
        return ERROR_SUCCESS;
    }

333 334 335 336
    if( *sz < len )
        *sz = len;
    memcpy( buffer, str, (*sz)*sizeof(WCHAR) ); 
    *sz = len;
337 338 339 340

    return ERROR_SUCCESS;
}

341 342 343 344
/*
 *  msi_id2stringA
 *
 *  [in] st         - pointer to the string table
345
 *  [in] id         - id of the string to retrieve
346 347 348 349 350 351 352
 *  [out] buffer    - destination of the UTF8 string
 *  [in/out] sz     - number of bytes available in the buffer on input
 *                    number of bytes used on output
 *
 *   The size includes the terminating nul character.  Short buffers
 *  will be filled, but not nul terminated.
 */
353
UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
354 355
{
    UINT len;
356
    const WCHAR *str;
Mike McCormack's avatar
Mike McCormack committed
357
    int n;
358

Mike McCormack's avatar
Mike McCormack committed
359
    TRACE("Finding string %d of %d\n", id, st->maxcount);
360

361
    str = msi_string_lookup_id( st, id );
362
    if( !str )
363 364
        return ERROR_FUNCTION_FAILED;

365
    len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
366 367 368 369 370 371 372

    if( !buffer )
    {
        *sz = len;
        return ERROR_SUCCESS;
    }

Mike McCormack's avatar
Mike McCormack committed
373 374 375 376 377 378 379 380 381 382 383
    if( len > *sz )
    {
        n = strlenW( str ) + 1;
        while( n && (len > *sz) )
            len = WideCharToMultiByte( st->codepage, 0, 
                           str, --n, NULL, 0, NULL, NULL );
    }
    else
        n = -1;

    *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
384 385 386 387

    return ERROR_SUCCESS;
}

388
/*
389
 *  msi_string2idW
390 391
 *
 *  [in] st         - pointer to the string table
392
 *  [in] str        - string to find in the string table
393 394
 *  [out] id        - id of the string, if found
 */
395
UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
396
{
397 398
    UINT n, hash = msistring_makehash( str );
    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 405
            *id = n;
            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 415
{
    DWORD sz;
    UINT r = ERROR_INVALID_PARAMETER;
416
    LPWSTR str;
417

418
    TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
419

420 421 422 423 424 425
    if( buffer[0] == 0 )
    {
        *id = 0;
        return ERROR_SUCCESS;
    }

426
    sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
427 428
    if( sz <= 0 )
        return r;
429
    str = msi_alloc( sz*sizeof(WCHAR) );
430 431
    if( !str )
        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 438

    return r;
}
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 445 446 447 448 449 450 451 452

    l_str = msi_string_lookup_id( st, lval );
    if( !l_str )
        return ERROR_INVALID_PARAMETER;
    
    r_str = msi_string_lookup_id( st, rval );
    if( !r_str )
        return ERROR_INVALID_PARAMETER;

    /* does this do the right thing for all UTF-8 strings? */
453
    *res = strcmpW( l_str, r_str );
454 455 456

    return ERROR_SUCCESS;
}
457

458
static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
459
{
460
    UINT i, len, holesize;
461

462
    if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
Mike McCormack's avatar
Mike McCormack committed
463
        ERR("oops. element 0 has a string\n");
464 465 466 467

    *poolsize = 4;
    *datasize = 0;
    holesize = 0;
Mike McCormack's avatar
Mike McCormack committed
468
    for( i=1; i<st->maxcount; i++ )
469
    {
470
        if( !st->strings[i].persistent_refcount )
471 472 473 474 475
        {
            TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
            (*poolsize) += 4;
        }
        else if( st->strings[i].str )
476
        {
Mike McCormack's avatar
Mike McCormack committed
477
            TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
478 479 480 481
            len = WideCharToMultiByte( st->codepage, 0,
                     st->strings[i].str, -1, NULL, 0, NULL, NULL);
            if( len )
                len--;
482 483 484 485 486
            (*datasize) += len;
            if (len>0xffff)
                (*poolsize) += 4;
            (*poolsize) += holesize + 4;
            holesize = 0;
487
        }
488 489
        else
            holesize += 4;
490
    }
491
    TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
Mike McCormack's avatar
Mike McCormack committed
492
}
493 494 495 496 497 498 499 500 501 502 503 504

static const WCHAR szStringData[] = {
    '_','S','t','r','i','n','g','D','a','t','a',0 };
static const WCHAR szStringPool[] = {
    '_','S','t','r','i','n','g','P','o','o','l',0 };

HRESULT msi_init_string_table( IStorage *stg )
{
    USHORT zero[2] = { 0, 0 };
    UINT ret;

    /* create the StringPool stream... add the zero string to it*/
505
    ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
506 507 508 509
    if (ret != ERROR_SUCCESS)
        return E_FAIL;

    /* create the StringData stream... make it zero length */
510
    ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
511 512 513 514 515 516
    if (ret != ERROR_SUCCESS)
        return E_FAIL;

    return S_OK;
}

517
string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
518 519 520 521 522 523 524
{
    string_table *st = NULL;
    CHAR *data = NULL;
    USHORT *pool = NULL;
    UINT r, datasize = 0, poolsize = 0, codepage;
    DWORD i, count, offset, len, n, refs;

525
    r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
526 527
    if( r != ERROR_SUCCESS)
        goto end;
528
    r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
529 530 531
    if( r != ERROR_SUCCESS)
        goto end;

532
    if ( (poolsize > 4) && (pool[1] & 0x8000) )
533 534 535 536
        *bytes_per_strref = LONG_STR_BYTES;
    else
        *bytes_per_strref = sizeof(USHORT);

537
    count = poolsize/4;
538 539
    if( poolsize > 4 )
        codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
540 541
    else
        codepage = CP_ACP;
542
    st = init_stringtable( count, codepage );
543 544
    if (!st)
        goto end;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

    offset = 0;
    n = 1;
    i = 1;
    while( i<count )
    {
        /* the string reference count is always the second word */
        refs = pool[i*2+1];

        /* empty entries have two zeros, still have a string id */
        if (pool[i*2] == 0 && refs == 0)
        {
            i++;
            n++;
            continue;
        }

        /*
         * If a string is over 64k, the previous string entry is made null
         * and its the high word of the length is inserted in the null string's
         * reference count field.
         */
        if( pool[i*2] == 0)
        {
            len = (pool[i*2+3] << 16) + pool[i*2+2];
            i += 2;
        }
        else
        {
            len = pool[i*2];
            i += 1;
        }

        if ( (offset + len) > datasize )
        {
            ERR("string table corrupt?\n");
            break;
        }

        r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
        if( r != n )
            ERR("Failed to add string %d\n", n );
        n++;
        offset += len;
    }

    if ( datasize != offset )
        ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );

    TRACE("Loaded %d strings\n", count);

end:
    msi_free( pool );
    msi_free( data );

    return st;
}

603
UINT msi_save_string_table( const string_table *st, IStorage *storage )
604
{
605
    UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
606 607 608 609 610 611 612
    UINT ret = ERROR_FUNCTION_FAILED;
    CHAR *data = NULL;
    USHORT *pool = NULL;

    TRACE("\n");

    /* construct the new table in memory first */
613
    string_totalsize( st, &datasize, &poolsize );
614

615
    TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    pool = msi_alloc( poolsize );
    if( ! pool )
    {
        WARN("Failed to alloc pool %d bytes\n", poolsize );
        goto err;
    }
    data = msi_alloc( datasize );
    if( ! data )
    {
        WARN("Failed to alloc data %d bytes\n", poolsize );
        goto err;
    }

    used = 0;
631
    codepage = st->codepage;
632 633 634
    pool[0]=codepage&0xffff;
    pool[1]=(codepage>>16);
    n = 1;
635
    for( i=1; i<st->maxcount; i++ )
636
    {
637
        if( !st->strings[i].persistent_refcount )
638 639 640 641
        {
            pool[ n*2 ] = 0;
            pool[ n*2 + 1] = 0;
            n++;
642
            continue;
643 644
        }

645 646 647 648 649 650 651 652 653 654 655
        sz = datasize - used;
        r = msi_id2stringA( st, i, data+used, &sz );
        if( r != ERROR_SUCCESS )
        {
            ERR("failed to fetch string\n");
            sz = 0;
        }
        if( sz && (sz < (datasize - used ) ) )
            sz--;

        if (sz)
656
            pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
        else
            pool[ n*2 + 1 ] = 0;
        if (sz < 0x10000)
        {
            pool[ n*2 ] = sz;
            n++;
        }
        else
        {
            pool[ n*2 ] = 0;
            pool[ n*2 + 2 ] = sz&0xffff;
            pool[ n*2 + 3 ] = (sz>>16);
            n += 2;
        }
        used += sz;
        if( used > datasize  )
        {
            ERR("oops overran %d >= %d\n", used, datasize);
            goto err;
        }
    }

    if( used != datasize )
    {
        ERR("oops used %d != datasize %d\n", used, datasize);
        goto err;
    }

    /* write the streams */
686
    r = write_stream_data( storage, szStringData, data, datasize, TRUE );
687 688 689
    TRACE("Wrote StringData r=%08x\n", r);
    if( r )
        goto err;
690
    r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
691 692 693 694 695 696 697 698 699 700 701 702
    TRACE("Wrote StringPool r=%08x\n", r);
    if( r )
        goto err;

    ret = ERROR_SUCCESS;

err:
    msi_free( data );
    msi_free( pool );

    return ret;
}