2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2007 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
39 #define NUM_STREAMS_COLS 2
41 typedef struct tabSTREAM
47 typedef struct tagMSISTREAMSVIEW
57 static BOOL
streams_set_table_size(MSISTREAMSVIEW
*sv
, UINT size
)
59 if (size
>= sv
->max_streams
)
62 sv
->streams
= msi_realloc_zero(sv
->streams
, sv
->max_streams
* sizeof(STREAM
*));
70 static STREAM
*create_stream(MSISTREAMSVIEW
*sv
, LPCWSTR name
, BOOL encoded
, IStream
*stm
)
73 WCHAR decoded
[MAX_STREAM_NAME_LEN
];
75 stream
= msi_alloc(sizeof(STREAM
));
81 decode_streamname(name
, decoded
);
82 TRACE("stream -> %s %s\n", debugstr_w(name
), debugstr_w(decoded
));
86 stream
->str_index
= msi_addstringW(sv
->db
->strings
, name
, -1, 1, StringNonPersistent
);
91 static UINT
STREAMS_fetch_int(struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
93 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
95 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, val
);
98 return ERROR_INVALID_PARAMETER
;
100 if (row
>= sv
->num_rows
)
101 return ERROR_NO_MORE_ITEMS
;
103 *val
= sv
->streams
[row
]->str_index
;
105 return ERROR_SUCCESS
;
108 static UINT
STREAMS_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
110 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
112 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
114 if (row
>= sv
->num_rows
)
115 return ERROR_FUNCTION_FAILED
;
117 IStream_AddRef(sv
->streams
[row
]->stream
);
118 *stm
= sv
->streams
[row
]->stream
;
120 return ERROR_SUCCESS
;
123 static UINT
STREAMS_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
125 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
127 TRACE("%p %d %p\n", sv
, row
, rec
);
129 return msi_view_get_row( sv
->db
, view
, row
, rec
);
132 static UINT
STREAMS_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
134 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
138 LPWSTR encname
= NULL
, name
= NULL
;
142 UINT r
= ERROR_FUNCTION_FAILED
;
144 TRACE("(%p, %d, %p, %08x)\n", view
, row
, rec
, mask
);
146 if (row
> sv
->num_rows
)
147 return ERROR_FUNCTION_FAILED
;
149 r
= MSI_RecordGetIStream(rec
, 2, &stm
);
150 if (r
!= ERROR_SUCCESS
)
153 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
156 WARN("failed to stat stream: %08x\n", hr
);
160 if (stat
.cbSize
.QuadPart
>> 32)
162 WARN("stream too large\n");
166 data
= msi_alloc(stat
.cbSize
.QuadPart
);
170 hr
= IStream_Read(stm
, data
, stat
.cbSize
.QuadPart
, &count
);
171 if (FAILED(hr
) || count
!= stat
.cbSize
.QuadPart
)
173 WARN("failed to read stream: %08x\n", hr
);
177 name
= strdupW(MSI_RecordGetString(rec
, 1));
180 WARN("failed to retrieve stream name\n");
184 encname
= encode_streamname(FALSE
, name
);
185 msi_destroy_stream(sv
->db
, encname
);
187 r
= write_stream_data(sv
->db
->storage
, name
, data
, count
, FALSE
);
188 if (r
!= ERROR_SUCCESS
)
190 WARN("failed to write stream data: %d\n", r
);
194 stream
= create_stream(sv
, name
, FALSE
, NULL
);
198 hr
= IStorage_OpenStream(sv
->db
->storage
, encname
, 0,
199 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stream
->stream
);
202 WARN("failed to open stream: %08x\n", hr
);
206 sv
->streams
[row
] = stream
;
213 IStream_Release(stm
);
218 static UINT
STREAMS_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
220 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
223 TRACE("(%p, %p, %d, %d)\n", view
, rec
, row
, temporary
);
225 if (!streams_set_table_size(sv
, ++sv
->num_rows
))
226 return ERROR_FUNCTION_FAILED
;
229 row
= sv
->num_rows
- 1;
231 /* shift the rows to make room for the new row */
232 for (i
= sv
->num_rows
- 1; i
> row
; i
--)
234 sv
->streams
[i
] = sv
->streams
[i
- 1];
237 return STREAMS_set_row(view
, row
, rec
, 0);
240 static UINT
STREAMS_delete_row(struct tagMSIVIEW
*view
, UINT row
)
242 FIXME("(%p %d): stub!\n", view
, row
);
243 return ERROR_SUCCESS
;
246 static UINT
STREAMS_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
248 TRACE("(%p, %p)\n", view
, record
);
249 return ERROR_SUCCESS
;
252 static UINT
STREAMS_close(struct tagMSIVIEW
*view
)
254 TRACE("(%p)\n", view
);
255 return ERROR_SUCCESS
;
258 static UINT
STREAMS_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
260 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
262 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
264 if (cols
) *cols
= NUM_STREAMS_COLS
;
265 if (rows
) *rows
= sv
->num_rows
;
267 return ERROR_SUCCESS
;
270 static UINT
STREAMS_get_column_info( struct tagMSIVIEW
*view
, UINT n
, LPCWSTR
*name
,
271 UINT
*type
, BOOL
*temporary
, LPCWSTR
*table_name
)
273 TRACE("(%p, %d, %p, %p, %p, %p)\n", view
, n
, name
, type
, temporary
,
276 if (n
== 0 || n
> NUM_STREAMS_COLS
)
277 return ERROR_INVALID_PARAMETER
;
282 if (name
) *name
= szName
;
283 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MAX_STREAM_NAME_LEN
;
287 if (name
) *name
= szData
;
288 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
291 if (table_name
) *table_name
= szStreams
;
292 if (temporary
) *temporary
= FALSE
;
293 return ERROR_SUCCESS
;
296 static UINT
streams_find_row(MSISTREAMSVIEW
*sv
, MSIRECORD
*rec
, UINT
*row
)
301 str
= MSI_RecordGetString(rec
, 1);
302 r
= msi_string2idW(sv
->db
->strings
, str
, &id
);
303 if (r
!= ERROR_SUCCESS
)
306 for (i
= 0; i
< sv
->num_rows
; i
++)
308 STREAMS_fetch_int(&sv
->view
, i
, 1, &data
);
313 return ERROR_SUCCESS
;
317 return ERROR_FUNCTION_FAILED
;
320 static UINT
streams_modify_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
322 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
325 r
= streams_find_row(sv
, rec
, &row
);
326 if (r
!= ERROR_SUCCESS
)
327 return ERROR_FUNCTION_FAILED
;
329 return STREAMS_set_row(view
, row
, rec
, 0);
332 static UINT
streams_modify_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
334 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
337 r
= streams_find_row(sv
, rec
, &row
);
338 if (r
== ERROR_SUCCESS
)
339 return streams_modify_update(view
, rec
);
341 return STREAMS_insert_row(view
, rec
, -1, FALSE
);
344 static UINT
STREAMS_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
348 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
352 case MSIMODIFY_ASSIGN
:
353 r
= streams_modify_assign(view
, rec
);
356 case MSIMODIFY_INSERT
:
357 r
= STREAMS_insert_row(view
, rec
, -1, FALSE
);
360 case MSIMODIFY_UPDATE
:
361 r
= streams_modify_update(view
, rec
);
364 case MSIMODIFY_VALIDATE_NEW
:
365 case MSIMODIFY_INSERT_TEMPORARY
:
366 case MSIMODIFY_REFRESH
:
367 case MSIMODIFY_REPLACE
:
368 case MSIMODIFY_MERGE
:
369 case MSIMODIFY_DELETE
:
370 case MSIMODIFY_VALIDATE
:
371 case MSIMODIFY_VALIDATE_FIELD
:
372 case MSIMODIFY_VALIDATE_DELETE
:
373 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
374 r
= ERROR_CALL_NOT_IMPLEMENTED
;
378 r
= ERROR_INVALID_DATA
;
384 static UINT
STREAMS_delete(struct tagMSIVIEW
*view
)
386 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
389 TRACE("(%p)\n", view
);
391 for (i
= 0; i
< sv
->num_rows
; i
++)
395 if (sv
->streams
[i
]->stream
)
396 IStream_Release(sv
->streams
[i
]->stream
);
397 msi_free(sv
->streams
[i
]);
401 msi_free(sv
->streams
);
404 return ERROR_SUCCESS
;
407 static UINT
STREAMS_find_matching_rows(struct tagMSIVIEW
*view
, UINT col
,
408 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
410 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
411 UINT index
= PtrToUlong(*handle
);
413 TRACE("(%p, %d, %d, %p, %p)\n", view
, col
, val
, row
, handle
);
415 if (col
== 0 || col
> NUM_STREAMS_COLS
)
416 return ERROR_INVALID_PARAMETER
;
418 while (index
< sv
->num_rows
)
420 if (sv
->streams
[index
]->str_index
== val
)
429 *handle
= UlongToPtr(++index
);
431 if (index
> sv
->num_rows
)
432 return ERROR_NO_MORE_ITEMS
;
434 return ERROR_SUCCESS
;
437 static const MSIVIEWOPS streams_ops
=
440 STREAMS_fetch_stream
,
447 STREAMS_get_dimensions
,
448 STREAMS_get_column_info
,
451 STREAMS_find_matching_rows
,
460 static INT
add_streams_to_table(MSISTREAMSVIEW
*sv
)
462 IEnumSTATSTG
*stgenum
= NULL
;
464 STREAM
*stream
= NULL
;
466 UINT r
, count
= 0, size
;
469 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
474 sv
->streams
= msi_alloc_zero(sizeof(STREAM
*));
481 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
482 if (FAILED(hr
) || !size
)
485 if (stat
.type
!= STGTY_STREAM
)
487 CoTaskMemFree(stat
.pwcsName
);
491 /* table streams are not in the _Streams table */
492 if (*stat
.pwcsName
== 0x4840)
494 CoTaskMemFree(stat
.pwcsName
);
498 stream
= create_stream(sv
, stat
.pwcsName
, TRUE
, NULL
);
502 CoTaskMemFree(stat
.pwcsName
);
506 /* these streams appear to be unencoded */
507 if (*stat
.pwcsName
== 0x0005)
509 r
= msi_get_raw_stream(sv
->db
, stat
.pwcsName
, &stream
->stream
);
513 encname
= encode_streamname(FALSE
, stat
.pwcsName
);
514 r
= msi_get_raw_stream(sv
->db
, encname
, &stream
->stream
);
517 CoTaskMemFree(stat
.pwcsName
);
519 if (r
!= ERROR_SUCCESS
)
521 WARN("unable to get stream %u\n", r
);
526 if (!streams_set_table_size(sv
, ++count
))
532 sv
->streams
[count
- 1] = stream
;
535 IEnumSTATSTG_Release(stgenum
);
539 UINT
STREAMS_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
544 TRACE("(%p, %p)\n", db
, view
);
546 sv
= msi_alloc_zero( sizeof(MSISTREAMSVIEW
) );
548 return ERROR_FUNCTION_FAILED
;
550 sv
->view
.ops
= &streams_ops
;
552 rows
= add_streams_to_table(sv
);
556 return ERROR_FUNCTION_FAILED
;
560 *view
= (MSIVIEW
*)sv
;
562 return ERROR_SUCCESS
;