2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2007 James Hawkins
5 * Copyright 2015 Hans Leidekker for CodeWeavers
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.
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.
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
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
40 #define NUM_STREAMS_COLS 2
42 typedef struct tagMSISTREAMSVIEW
49 static BOOL
streams_resize_table( MSIDATABASE
*db
, UINT size
)
51 if (!db
->num_streams_allocated
)
53 if (!(db
->streams
= msi_alloc_zero( size
* sizeof(MSISTREAM
) ))) return FALSE
;
54 db
->num_streams_allocated
= size
;
57 while (size
>= db
->num_streams_allocated
)
60 UINT new_size
= db
->num_streams_allocated
* 2;
61 if (!(tmp
= msi_realloc_zero( db
->streams
, new_size
* sizeof(MSISTREAM
) ))) return FALSE
;
63 db
->num_streams_allocated
= new_size
;
68 static UINT
STREAMS_fetch_int(struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
70 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
72 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, val
);
75 return ERROR_INVALID_PARAMETER
;
77 if (row
>= sv
->db
->num_streams
)
78 return ERROR_NO_MORE_ITEMS
;
80 *val
= sv
->db
->streams
[row
].str_index
;
85 static UINT
STREAMS_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
87 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
91 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
93 if (row
>= sv
->db
->num_streams
)
94 return ERROR_FUNCTION_FAILED
;
97 hr
= IStream_Seek( sv
->db
->streams
[row
].stream
, pos
, STREAM_SEEK_SET
, NULL
);
99 return ERROR_FUNCTION_FAILED
;
101 *stm
= sv
->db
->streams
[row
].stream
;
102 IStream_AddRef( *stm
);
104 return ERROR_SUCCESS
;
107 static UINT
STREAMS_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
109 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
111 TRACE("%p %d %p\n", sv
, row
, rec
);
113 return msi_view_get_row( sv
->db
, view
, row
, rec
);
116 static UINT
STREAMS_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
118 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
120 TRACE("(%p, %d, %p, %08x)\n", view
, row
, rec
, mask
);
122 if (row
> sv
->db
->num_streams
|| mask
>= (1 << sv
->num_cols
))
123 return ERROR_INVALID_PARAMETER
;
127 const WCHAR
*name
= MSI_RecordGetString( rec
, 1 );
129 if (!name
) return ERROR_INVALID_PARAMETER
;
130 sv
->db
->streams
[row
].str_index
= msi_add_string( sv
->db
->strings
, name
, -1, StringNonPersistent
);
138 r
= MSI_RecordGetIStream( rec
, 2, &new );
139 if (r
!= ERROR_SUCCESS
)
142 old
= sv
->db
->streams
[row
].stream
;
143 hr
= IStream_QueryInterface( new, &IID_IStream
, (void **)&sv
->db
->streams
[row
].stream
);
146 IStream_Release( new );
147 return ERROR_FUNCTION_FAILED
;
149 if (old
) IStream_Release( old
);
152 return ERROR_SUCCESS
;
155 static UINT
streams_find_row( MSISTREAMSVIEW
*sv
, MSIRECORD
*rec
, UINT
*row
)
160 str
= MSI_RecordGetString( rec
, 1 );
161 r
= msi_string2id( sv
->db
->strings
, str
, -1, &id
);
162 if (r
!= ERROR_SUCCESS
)
165 for (i
= 0; i
< sv
->db
->num_streams
; i
++)
167 STREAMS_fetch_int( &sv
->view
, i
, 1, &val
);
172 return ERROR_SUCCESS
;
176 return ERROR_FUNCTION_FAILED
;
179 static UINT
STREAMS_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
181 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
182 UINT i
, r
, num_rows
= sv
->db
->num_streams
+ 1;
184 TRACE("(%p, %p, %d, %d)\n", view
, rec
, row
, temporary
);
186 r
= streams_find_row( sv
, rec
, NULL
);
187 if (r
== ERROR_SUCCESS
)
188 return ERROR_FUNCTION_FAILED
;
190 if (!streams_resize_table( sv
->db
, num_rows
))
191 return ERROR_FUNCTION_FAILED
;
196 /* shift the rows to make room for the new row */
197 for (i
= num_rows
- 1; i
> row
; i
--)
199 sv
->db
->streams
[i
] = sv
->db
->streams
[i
- 1];
202 r
= STREAMS_set_row( view
, row
, rec
, (1 << sv
->num_cols
) - 1 );
203 if (r
== ERROR_SUCCESS
)
204 sv
->db
->num_streams
= num_rows
;
209 static UINT
STREAMS_delete_row(struct tagMSIVIEW
*view
, UINT row
)
211 FIXME("(%p %d): stub!\n", view
, row
);
212 return ERROR_SUCCESS
;
215 static UINT
STREAMS_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
217 TRACE("(%p, %p)\n", view
, record
);
218 return ERROR_SUCCESS
;
221 static UINT
STREAMS_close(struct tagMSIVIEW
*view
)
223 TRACE("(%p)\n", view
);
224 return ERROR_SUCCESS
;
227 static UINT
STREAMS_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
229 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
231 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
233 if (cols
) *cols
= sv
->num_cols
;
234 if (rows
) *rows
= sv
->db
->num_streams
;
236 return ERROR_SUCCESS
;
239 static UINT
STREAMS_get_column_info( struct tagMSIVIEW
*view
, UINT n
, LPCWSTR
*name
,
240 UINT
*type
, BOOL
*temporary
, LPCWSTR
*table_name
)
242 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
244 TRACE("(%p, %d, %p, %p, %p, %p)\n", view
, n
, name
, type
, temporary
, table_name
);
246 if (!n
|| n
> sv
->num_cols
)
247 return ERROR_INVALID_PARAMETER
;
252 if (name
) *name
= szName
;
253 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MAX_STREAM_NAME_LEN
;
257 if (name
) *name
= szData
;
258 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
261 if (table_name
) *table_name
= szStreams
;
262 if (temporary
) *temporary
= FALSE
;
263 return ERROR_SUCCESS
;
266 static UINT
streams_modify_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
268 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
271 r
= streams_find_row(sv
, rec
, &row
);
272 if (r
!= ERROR_SUCCESS
)
273 return ERROR_FUNCTION_FAILED
;
275 return STREAMS_set_row( view
, row
, rec
, (1 << sv
->num_cols
) - 1 );
278 static UINT
streams_modify_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
280 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
283 r
= streams_find_row( sv
, rec
, NULL
);
284 if (r
== ERROR_SUCCESS
)
285 return streams_modify_update(view
, rec
);
287 return STREAMS_insert_row(view
, rec
, -1, FALSE
);
290 static UINT
STREAMS_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
294 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
298 case MSIMODIFY_ASSIGN
:
299 r
= streams_modify_assign(view
, rec
);
302 case MSIMODIFY_INSERT
:
303 r
= STREAMS_insert_row(view
, rec
, -1, FALSE
);
306 case MSIMODIFY_UPDATE
:
307 r
= streams_modify_update(view
, rec
);
310 case MSIMODIFY_VALIDATE_NEW
:
311 case MSIMODIFY_INSERT_TEMPORARY
:
312 case MSIMODIFY_REFRESH
:
313 case MSIMODIFY_REPLACE
:
314 case MSIMODIFY_MERGE
:
315 case MSIMODIFY_DELETE
:
316 case MSIMODIFY_VALIDATE
:
317 case MSIMODIFY_VALIDATE_FIELD
:
318 case MSIMODIFY_VALIDATE_DELETE
:
319 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
320 r
= ERROR_CALL_NOT_IMPLEMENTED
;
324 r
= ERROR_INVALID_DATA
;
330 static UINT
STREAMS_delete(struct tagMSIVIEW
*view
)
332 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
334 TRACE("(%p)\n", view
);
337 return ERROR_SUCCESS
;
340 static UINT
STREAMS_find_matching_rows(struct tagMSIVIEW
*view
, UINT col
,
341 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
343 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
344 UINT index
= PtrToUlong(*handle
);
346 TRACE("(%p, %d, %d, %p, %p)\n", view
, col
, val
, row
, handle
);
348 if (!col
|| col
> sv
->num_cols
)
349 return ERROR_INVALID_PARAMETER
;
351 while (index
< sv
->db
->num_streams
)
353 if (sv
->db
->streams
[index
].str_index
== val
)
361 *handle
= UlongToPtr(++index
);
363 if (index
> sv
->db
->num_streams
)
364 return ERROR_NO_MORE_ITEMS
;
366 return ERROR_SUCCESS
;
369 static const MSIVIEWOPS streams_ops
=
372 STREAMS_fetch_stream
,
379 STREAMS_get_dimensions
,
380 STREAMS_get_column_info
,
383 STREAMS_find_matching_rows
,
392 static HRESULT
open_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
**stream
)
396 hr
= IStorage_OpenStream( db
->storage
, name
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, stream
);
399 MSITRANSFORM
*transform
;
401 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
403 hr
= IStorage_OpenStream( transform
->stg
, name
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, stream
);
411 static MSISTREAM
*find_stream( MSIDATABASE
*db
, const WCHAR
*name
)
415 r
= msi_string2id( db
->strings
, name
, -1, &id
);
416 if (r
!= ERROR_SUCCESS
)
419 for (i
= 0; i
< db
->num_streams
; i
++)
421 if (db
->streams
[i
].str_index
== id
) return &db
->streams
[i
];
426 static UINT
append_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
*stream
)
428 UINT i
= db
->num_streams
;
430 if (!streams_resize_table( db
, db
->num_streams
+ 1 ))
431 return ERROR_OUTOFMEMORY
;
433 db
->streams
[i
].str_index
= msi_add_string( db
->strings
, name
, -1, StringNonPersistent
);
434 db
->streams
[i
].stream
= stream
;
437 TRACE("added %s\n", debugstr_w( name
));
438 return ERROR_SUCCESS
;
441 static UINT
load_streams( MSIDATABASE
*db
)
443 WCHAR decoded
[MAX_STREAM_NAME_LEN
+ 1];
444 IEnumSTATSTG
*stgenum
;
447 UINT count
, r
= ERROR_SUCCESS
;
450 hr
= IStorage_EnumElements( db
->storage
, 0, NULL
, 0, &stgenum
);
452 return ERROR_FUNCTION_FAILED
;
457 hr
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
458 if (FAILED( hr
) || !count
)
461 /* table streams are not in the _Streams table */
462 if (stat
.type
!= STGTY_STREAM
|| *stat
.pwcsName
== 0x4840)
464 CoTaskMemFree( stat
.pwcsName
);
467 decode_streamname( stat
.pwcsName
, decoded
);
468 if (find_stream( db
, decoded
))
470 CoTaskMemFree( stat
.pwcsName
);
473 TRACE("found new stream %s\n", debugstr_w( decoded
));
475 hr
= open_stream( db
, stat
.pwcsName
, &stream
);
476 CoTaskMemFree( stat
.pwcsName
);
479 ERR("unable to open stream %08x\n", hr
);
480 r
= ERROR_FUNCTION_FAILED
;
484 r
= append_stream( db
, decoded
, stream
);
485 if (r
!= ERROR_SUCCESS
)
489 TRACE("loaded %u streams\n", db
->num_streams
);
490 IEnumSTATSTG_Release( stgenum
);
494 UINT
msi_get_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
**ret
)
501 if ((stream
= find_stream( db
, name
)))
506 hr
= IStream_Seek( stream
->stream
, pos
, STREAM_SEEK_SET
, NULL
);
508 return ERROR_FUNCTION_FAILED
;
510 *ret
= stream
->stream
;
511 IStream_AddRef( *ret
);
512 return ERROR_SUCCESS
;
515 if (!(encname
= encode_streamname( FALSE
, name
)))
516 return ERROR_OUTOFMEMORY
;
518 hr
= open_stream( db
, encname
, ret
);
521 return ERROR_FUNCTION_FAILED
;
523 r
= append_stream( db
, name
, *ret
);
524 if (r
!= ERROR_SUCCESS
)
526 IStream_Release( *ret
);
530 IStream_AddRef( *ret
);
531 return ERROR_SUCCESS
;
534 UINT
STREAMS_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
539 TRACE("(%p, %p)\n", db
, view
);
541 r
= load_streams( db
);
542 if (r
!= ERROR_SUCCESS
)
545 if (!(sv
= msi_alloc_zero( sizeof(MSISTREAMSVIEW
) )))
546 return ERROR_OUTOFMEMORY
;
548 sv
->view
.ops
= &streams_ops
;
549 sv
->num_cols
= NUM_STREAMS_COLS
;
552 *view
= (MSIVIEW
*)sv
;
554 return ERROR_SUCCESS
;
557 static HRESULT
write_stream( IStream
*dst
, IStream
*src
)
565 hr
= IStream_Stat( src
, &stat
, STATFLAG_NONAME
);
566 if (FAILED( hr
)) return hr
;
568 hr
= IStream_SetSize( dst
, stat
.cbSize
);
569 if (FAILED( hr
)) return hr
;
572 hr
= IStream_Seek( dst
, pos
, STREAM_SEEK_SET
, NULL
);
573 if (FAILED( hr
)) return hr
;
577 size
= min( sizeof(buf
), stat
.cbSize
.QuadPart
);
578 hr
= IStream_Read( src
, buf
, size
, &count
);
579 if (FAILED( hr
) || count
!= size
)
581 WARN("failed to read stream: %08x\n", hr
);
584 stat
.cbSize
.QuadPart
-= count
;
588 hr
= IStream_Write( dst
, buf
, size
, &count
);
589 if (FAILED( hr
) || count
!= size
)
591 WARN("failed to write stream: %08x\n", hr
);
595 if (!stat
.cbSize
.QuadPart
) break;
601 UINT
msi_commit_streams( MSIDATABASE
*db
)
609 TRACE("got %u streams\n", db
->num_streams
);
611 for (i
= 0; i
< db
->num_streams
; i
++)
613 name
= msi_string_lookup( db
->strings
, db
->streams
[i
].str_index
, NULL
);
614 if (!(encname
= encode_streamname( FALSE
, name
))) return ERROR_OUTOFMEMORY
;
616 hr
= IStorage_CreateStream( db
->storage
, encname
, STGM_WRITE
|STGM_SHARE_EXCLUSIVE
, 0, 0, &stream
);
619 hr
= write_stream( stream
, db
->streams
[i
].stream
);
622 ERR("failed to write stream %s (hr = %08x)\n", debugstr_w(encname
), hr
);
624 IStream_Release( stream
);
625 return ERROR_FUNCTION_FAILED
;
627 hr
= IStream_Commit( stream
, 0 );
628 IStream_Release( stream
);
631 ERR("failed to commit stream %s (hr = %08x)\n", debugstr_w(encname
), hr
);
633 return ERROR_FUNCTION_FAILED
;
636 else if (hr
!= STG_E_FILEALREADYEXISTS
)
638 ERR("failed to create stream %s (hr = %08x)\n", debugstr_w(encname
), hr
);
640 return ERROR_FUNCTION_FAILED
;
645 return ERROR_SUCCESS
;