2 * SHLWAPI DataBlock List functions
4 * Copyright 2002 Jon Griffiths
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
20 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
31 //#include "winuser.h"
32 //#include "objbase.h"
34 #include <wine/debug.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
38 /* dwSignature for contained DATABLOCK_HEADER items */
39 #define CLIST_ID_CONTAINER (~0U)
41 /*************************************************************************
44 * Internal helper: move a DataBlock pointer to the next item.
46 static inline LPDATABLOCK_HEADER
NextItem(LPDBLIST lpList
)
48 char* address
= (char*)lpList
;
49 address
+= lpList
->cbSize
;
50 return (LPDATABLOCK_HEADER
)address
;
53 /*************************************************************************
56 * Insert a new item into a DataBlock list.
59 * lppList [0] Pointer to the List
60 * lpNewItem [I] The new item to add to the list
63 * Success: S_OK. The item is added to the list.
64 * Failure: An HRESULT error code.
67 * If the size of the element to be inserted is less than the size of a
68 * DATABLOCK_HEADER node, or the Id for the item is CLIST_ID_CONTAINER,
69 * the call returns S_OK but does not actually add the element.
70 * See SHWriteDataBlockList.
72 HRESULT WINAPI
SHAddDataBlock(LPDBLIST
* lppList
, const DATABLOCK_HEADER
*lpNewItem
)
74 LPDATABLOCK_HEADER lpInsertAt
= NULL
;
77 TRACE("(%p,%p)\n", lppList
, lpNewItem
);
79 if(!lppList
|| !lpNewItem
)
82 if (lpNewItem
->cbSize
< sizeof(DATABLOCK_HEADER
) ||
83 lpNewItem
->dwSignature
== CLIST_ID_CONTAINER
)
86 ulSize
= lpNewItem
->cbSize
;
90 /* Tune size to a ULONG boundary, add space for container element */
91 ulSize
= ((ulSize
+ 0x3) & 0xFFFFFFFC) + sizeof(DATABLOCK_HEADER
);
92 TRACE("Creating container item, new size = %d\n", ulSize
);
97 /* An empty list. Allocate space for terminal ulSize also */
98 *lppList
= LocalAlloc(LMEM_ZEROINIT
, ulSize
+ sizeof(ULONG
));
99 lpInsertAt
= *lppList
;
103 /* Append to the end of the list */
104 ULONG ulTotalSize
= 0;
105 LPDATABLOCK_HEADER lpIter
= *lppList
;
107 /* Iterate to the end of the list, calculating the total size */
108 while (lpIter
->cbSize
)
110 ulTotalSize
+= lpIter
->cbSize
;
111 lpIter
= NextItem(lpIter
);
114 /* Increase the size of the list */
115 lpIter
= LocalReAlloc(*lppList
, ulTotalSize
+ ulSize
+sizeof(ULONG
),
116 LMEM_ZEROINIT
| LMEM_MOVEABLE
);
120 lpInsertAt
= (LPDATABLOCK_HEADER
)((char*)lpIter
+ ulTotalSize
); /* At end */
126 /* Copy in the new item */
127 LPDATABLOCK_HEADER lpDest
= lpInsertAt
;
129 if(ulSize
!= lpNewItem
->cbSize
)
131 lpInsertAt
->cbSize
= ulSize
;
132 lpInsertAt
->dwSignature
= CLIST_ID_CONTAINER
;
135 memcpy(lpDest
, lpNewItem
, lpNewItem
->cbSize
);
137 /* Terminate the list */
138 lpInsertAt
= NextItem(lpInsertAt
);
139 lpInsertAt
->cbSize
= 0;
141 return lpNewItem
->cbSize
;
146 /*************************************************************************
149 * Write a DataBlock list to an IStream object.
152 * lpStream [I] IStream object to write the list to
153 * lpList [I] List of items to write
156 * Success: S_OK. The object is written to the stream.
157 * Failure: An HRESULT error code
160 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
161 * list structure (a "DataBlock List"), which may be stored and retrieved from
164 * The exposed API consists of:
166 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
167 * - SHReadDataBlockList() - Read and create a list from a stream,
168 * - SHFreeDataBlockList() - Free a list,
169 * - SHAddDataBlock() - Insert a new item into a list,
170 * - SHRemoveDataBlock() - Remove an item from a list,
171 * - SHFindDataBlock() - Find an item in a list.
173 * The DataBlock list is stored packed into a memory array. Each element has a
174 * size and an associated ID. Elements must be less than 64k if the list is
175 * to be subsequently read from a stream.
177 * Elements are aligned on DWORD boundaries. If an elements data size is not
178 * a DWORD size multiple, the element is wrapped by inserting a surrounding
179 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
181 * These functions are slow for large objects and long lists.
183 HRESULT WINAPI
SHWriteDataBlockList(IStream
* lpStream
, LPDBLIST lpList
)
188 TRACE("(%p,%p)\n", lpStream
, lpList
);
192 while (lpList
->cbSize
)
194 LPDATABLOCK_HEADER lpItem
= lpList
;
196 if(lpList
->dwSignature
== CLIST_ID_CONTAINER
)
199 hRet
= IStream_Write(lpStream
,lpItem
,lpItem
->cbSize
,&ulSize
);
203 if(lpItem
->cbSize
!= ulSize
)
204 return STG_E_MEDIUMFULL
;
206 lpList
= NextItem(lpList
);
215 /* Write a terminating list entry with zero size */
216 hRet
= IStream_Write(lpStream
, &ulSize
,sizeof(ulSize
),&ulDummy
);
222 /*************************************************************************
225 * Read and create a DataBlock list from an IStream object.
228 * lpStream [I] Stream to read the list from
229 * lppList [0] Pointer to receive the new List
233 * Failure: An HRESULT error code
236 * When read from a file, list objects are limited in size to 64k.
237 * See SHWriteDataBlockList.
239 HRESULT WINAPI
SHReadDataBlockList(IStream
* lpStream
, LPDBLIST
* lppList
)
241 DATABLOCK_HEADER bBuff
[128]; /* Temporary storage for new list item */
242 ULONG ulBuffSize
= sizeof(bBuff
);
243 LPDATABLOCK_HEADER pItem
= bBuff
;
244 ULONG ulRead
, ulSize
;
247 TRACE("(%p,%p)\n", lpStream
, lppList
);
251 /* Free any existing list */
258 /* Read the size of the next item */
259 hRet
= IStream_Read(lpStream
, &ulSize
,sizeof(ulSize
),&ulRead
);
261 if(FAILED(hRet
) || ulRead
!= sizeof(ulSize
) || !ulSize
)
262 break; /* Read failed or read zero size (the end of the list) */
266 LARGE_INTEGER liZero
;
267 ULARGE_INTEGER ulPos
;
271 /* Back the stream up; this object is too big for the list */
272 if(SUCCEEDED(IStream_Seek(lpStream
, liZero
, STREAM_SEEK_CUR
, &ulPos
)))
274 liZero
.QuadPart
= ulPos
.QuadPart
- sizeof(ULONG
);
275 IStream_Seek(lpStream
, liZero
, STREAM_SEEK_SET
, NULL
);
279 else if (ulSize
>= sizeof(DATABLOCK_HEADER
))
281 /* Add this new item to the list */
282 if(ulSize
> ulBuffSize
)
284 /* We need more buffer space, allocate it */
285 LPDATABLOCK_HEADER lpTemp
;
288 lpTemp
= LocalAlloc(LMEM_ZEROINIT
, ulSize
);
290 lpTemp
= LocalReAlloc(pItem
, ulSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
294 hRet
= E_OUTOFMEMORY
;
301 pItem
->cbSize
= ulSize
;
302 ulSize
-= sizeof(pItem
->cbSize
); /* already read this member */
304 /* Read the item Id and data */
305 hRet
= IStream_Read(lpStream
, &pItem
->dwSignature
, ulSize
, &ulRead
);
307 if(FAILED(hRet
) || ulRead
!= ulSize
)
310 SHAddDataBlock(lppList
, pItem
); /* Insert Item */
314 /* If we allocated space, free it */
321 /*************************************************************************
324 * Free a DataBlock list.
327 * lpList [I] List to free
333 * See SHWriteDataBlockList.
335 VOID WINAPI
SHFreeDataBlockList(LPDBLIST lpList
)
337 TRACE("(%p)\n", lpList
);
343 /*************************************************************************
346 * Remove an item from a DataBlock list.
349 * lppList [O] List to remove the item from
350 * dwSignature [I] Id of item to remove
354 * Failure: FALSE, If any parameters are invalid, or the item was not found.
357 * See SHWriteDataBlockList.
359 BOOL WINAPI
SHRemoveDataBlock(LPDBLIST
* lppList
, DWORD dwSignature
)
361 LPDATABLOCK_HEADER lpList
= 0;
362 LPDATABLOCK_HEADER lpItem
= NULL
;
363 LPDATABLOCK_HEADER lpNext
;
366 TRACE("(%p,%d)\n", lppList
, dwSignature
);
368 if(lppList
&& (lpList
= *lppList
))
370 /* Search for item in list */
371 while (lpList
->cbSize
)
373 if(lpList
->dwSignature
== dwSignature
||
374 (lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
))
376 lpItem
= lpList
; /* Found */
379 lpList
= NextItem(lpList
);
386 lpList
= lpNext
= NextItem(lpItem
);
388 /* Locate the end of the list */
389 while (lpList
->cbSize
)
390 lpList
= NextItem(lpList
);
392 /* Resize the list */
393 ulNewSize
= LocalSize(*lppList
) - lpItem
->cbSize
;
395 /* Copy following elements over lpItem */
396 memmove(lpItem
, lpNext
, (char *)lpList
- (char *)lpNext
+ sizeof(ULONG
));
398 if(ulNewSize
<= sizeof(ULONG
))
401 *lppList
= NULL
; /* Removed the last element */
405 lpList
= LocalReAlloc(*lppList
, ulNewSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
412 /*************************************************************************
415 * Find an item in a DataBlock list.
418 * lpList [I] List to search
419 * dwSignature [I] Id of item to find
422 * Success: A pointer to the list item found
426 * See SHWriteDataBlockList.
428 DATABLOCK_HEADER
* WINAPI
SHFindDataBlock(LPDBLIST lpList
, DWORD dwSignature
)
430 TRACE("(%p,%d)\n", lpList
, dwSignature
);
434 while(lpList
->cbSize
)
436 if(lpList
->dwSignature
== dwSignature
)
437 return lpList
; /* Matched */
438 else if(lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
)
439 return lpList
+ 1; /* Contained item matches */
441 lpList
= NextItem(lpList
);