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
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32 /* DataBlock list element (ordinals 17-22) */
33 typedef struct tagSHLWAPI_CLIST
35 ULONG ulSize; /* Size of this list element and its data */
36 ULONG ulId; /* If 0xFFFFFFFF, The real element follows */
37 /* Item data (or a contained SHLWAPI_CLIST) follows... */
38 } SHLWAPI_CLIST, *LPSHLWAPI_CLIST;
40 typedef const SHLWAPI_CLIST* LPCSHLWAPI_CLIST;
42 /* ulId for contained SHLWAPI_CLIST items */
43 #define CLIST_ID_CONTAINER (~0U)
45 HRESULT WINAPI SHAddDataBlock(LPSHLWAPI_CLIST*,LPCSHLWAPI_CLIST);
47 /*************************************************************************
50 * Internal helper: move a DataBlock pointer to the next item.
52 inline static LPSHLWAPI_CLIST NextItem(LPSHLWAPI_CLIST lpList)
54 char* address = (char*)lpList;
55 address += lpList->ulSize;
56 return (LPSHLWAPI_CLIST)address;
59 /*************************************************************************
62 * Write a DataBlock list to an IStream object.
65 * lpStream [I] IStream object to write the list to
66 * lpList [I] List of items to write
69 * Success: S_OK. The object is written to the stream.
70 * Failure: An HRESULT error code
73 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
74 * list structure (a "DataBlock List"), which may be stored and retrieved from
77 * The exposed API consists of:
79 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
80 * - SHReadDataBlockList() - Read and create a list from a stream,
81 * - SHFreeDataBlockList() - Free a list,
82 * - SHAddDataBlock() - Insert a new item into a list,
83 * - SHRemoveDataBlock() - Remove an item from a list,
84 * - SHFindDataBlock() - Find an item in a list.
86 * The DataBlock list is stored packed into a memory array. Each element has a
87 * size and an associated ID. Elements must be less than 64k if the list is
88 * to be subsequently read from a stream.
90 * Elements are aligned on DWORD boundaries. If an elements data size is not
91 * a DWORD size multiple, the element is wrapped by inserting a surrounding
92 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
94 * These functions are slow for large objects and long lists.
96 HRESULT WINAPI SHWriteDataBlockList(IStream* lpStream, LPSHLWAPI_CLIST lpList)
99 HRESULT hRet = E_FAIL;
101 TRACE("(%p,%p)\n", lpStream, lpList);
105 while (lpList->ulSize)
107 LPSHLWAPI_CLIST lpItem = lpList;
109 if(lpList->ulId == CLIST_ID_CONTAINER)
112 hRet = IStream_Write(lpStream,lpItem,lpItem->ulSize,&ulSize);
116 if(lpItem->ulSize != ulSize)
117 return STG_E_MEDIUMFULL;
119 lpList = NextItem(lpList);
128 /* Write a terminating list entry with zero size */
129 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
135 /*************************************************************************
138 * Read and create a DataBlock list from an IStream object.
141 * lpStream [I] Stream to read the list from
142 * lppList [0] Pointer to receive the new List
146 * Failure: An HRESULT error code
149 * When read from a file, list objects are limited in size to 64k.
150 * See SHWriteDataBlockList.
152 HRESULT WINAPI SHReadDataBlockList(IStream* lpStream, LPSHLWAPI_CLIST* lppList)
154 SHLWAPI_CLIST bBuff[128]; /* Temporary storage for new list item */
155 ULONG ulBuffSize = sizeof(bBuff);
156 LPSHLWAPI_CLIST pItem = bBuff;
157 ULONG ulRead, ulSize;
160 TRACE("(%p,%p)\n", lpStream, lppList);
164 /* Free any existing list */
165 LocalFree((HLOCAL)*lppList);
171 /* Read the size of the next item */
172 hRet = IStream_Read(lpStream, &ulSize,sizeof(ulSize),&ulRead);
174 if(FAILED(hRet) || ulRead != sizeof(ulSize) || !ulSize)
175 break; /* Read failed or read zero size (the end of the list) */
179 LARGE_INTEGER liZero;
180 ULARGE_INTEGER ulPos;
184 /* Back the stream up; this object is too big for the list */
185 if(SUCCEEDED(IStream_Seek(lpStream, liZero, STREAM_SEEK_CUR, &ulPos)))
187 liZero.QuadPart = ulPos.QuadPart - sizeof(ULONG);
188 IStream_Seek(lpStream, liZero, STREAM_SEEK_SET, NULL);
192 else if (ulSize >= sizeof(SHLWAPI_CLIST))
194 /* Add this new item to the list */
195 if(ulSize > ulBuffSize)
197 /* We need more buffer space, allocate it */
198 LPSHLWAPI_CLIST lpTemp;
201 lpTemp = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT, ulSize);
203 lpTemp = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)pItem, ulSize,
204 LMEM_ZEROINIT|LMEM_MOVEABLE);
208 hRet = E_OUTOFMEMORY;
215 pItem->ulSize = ulSize;
216 ulSize -= sizeof(pItem->ulSize); /* already read this member */
218 /* Read the item Id and data */
219 hRet = IStream_Read(lpStream, &pItem->ulId, ulSize, &ulRead);
221 if(FAILED(hRet) || ulRead != ulSize)
224 SHAddDataBlock(lppList, pItem); /* Insert Item */
228 /* If we allocated space, free it */
230 LocalFree((HLOCAL)pItem);
235 /*************************************************************************
238 * Free a DataBlock list.
241 * lpList [I] List to free
247 * See SHWriteDataBlockList.
249 VOID WINAPI SHFreeDataBlockList(LPSHLWAPI_CLIST lpList)
251 TRACE("(%p)\n", lpList);
254 LocalFree((HLOCAL)lpList);
257 /*************************************************************************
260 * Insert a new item into a DataBlock list.
263 * lppList [0] Pointer to the List
264 * lpNewItem [I] The new item to add to the list
267 * Success: S_OK. The item is added to the list.
268 * Failure: An HRESULT error code.
271 * If the size of the element to be inserted is less than the size of a
272 * SHLWAPI_CLIST node, or the Id for the item is CLIST_ID_CONTAINER,
273 * the call returns S_OK but does not actually add the element.
274 * See SHWriteDataBlockList.
276 HRESULT WINAPI SHAddDataBlock(LPSHLWAPI_CLIST* lppList, LPCSHLWAPI_CLIST lpNewItem)
278 LPSHLWAPI_CLIST lpInsertAt = NULL;
281 TRACE("(%p,%p)\n", lppList, lpNewItem);
283 if(!lppList || !lpNewItem )
286 if (lpNewItem->ulSize < sizeof(SHLWAPI_CLIST) ||
287 lpNewItem->ulId == CLIST_ID_CONTAINER)
290 ulSize = lpNewItem->ulSize;
294 /* Tune size to a ULONG boundary, add space for container element */
295 ulSize = ((ulSize + 0x3) & 0xFFFFFFFC) + sizeof(SHLWAPI_CLIST);
296 TRACE("Creating container item, new size = %d\n", ulSize);
301 /* An empty list. Allocate space for terminal ulSize also */
302 *lppList = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT,
303 ulSize + sizeof(ULONG));
304 lpInsertAt = *lppList;
308 /* Append to the end of the list */
309 ULONG ulTotalSize = 0;
310 LPSHLWAPI_CLIST lpIter = *lppList;
312 /* Iterate to the end of the list, calculating the total size */
313 while (lpIter->ulSize)
315 ulTotalSize += lpIter->ulSize;
316 lpIter = NextItem(lpIter);
319 /* Increase the size of the list */
320 lpIter = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList,
321 ulTotalSize + ulSize+sizeof(ULONG),
322 LMEM_ZEROINIT | LMEM_MOVEABLE);
326 lpInsertAt = (LPSHLWAPI_CLIST)((char*)lpIter + ulTotalSize); /* At end */
332 /* Copy in the new item */
333 LPSHLWAPI_CLIST lpDest = lpInsertAt;
335 if(ulSize != lpNewItem->ulSize)
337 lpInsertAt->ulSize = ulSize;
338 lpInsertAt->ulId = CLIST_ID_CONTAINER;
341 memcpy(lpDest, lpNewItem, lpNewItem->ulSize);
343 /* Terminate the list */
344 lpInsertAt = NextItem(lpInsertAt);
345 lpInsertAt->ulSize = 0;
347 return lpNewItem->ulSize;
352 /*************************************************************************
355 * Remove an item from a DataBlock list.
358 * lppList [O] List to remove the item from
359 * ulId [I] Id of item to remove
363 * Failure: FALSE, If any parameters are invalid, or the item was not found.
366 * See SHWriteDataBlockList.
368 BOOL WINAPI SHRemoveDataBlock(LPSHLWAPI_CLIST* lppList, ULONG ulId)
370 LPSHLWAPI_CLIST lpList = 0;
371 LPSHLWAPI_CLIST lpItem = NULL;
372 LPSHLWAPI_CLIST lpNext;
375 TRACE("(%p,%d)\n", lppList, ulId);
377 if(lppList && (lpList = *lppList))
379 /* Search for item in list */
380 while (lpList->ulSize)
382 if(lpList->ulId == ulId ||
383 (lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId))
385 lpItem = lpList; /* Found */
388 lpList = NextItem(lpList);
395 lpList = lpNext = NextItem(lpItem);
397 /* Locate the end of the list */
398 while (lpList->ulSize)
399 lpList = NextItem(lpList);
401 /* Resize the list */
402 ulNewSize = LocalSize((HLOCAL)*lppList) - lpItem->ulSize;
404 /* Copy following elements over lpItem */
405 memmove(lpItem, lpNext, (char *)lpList - (char *)lpNext + sizeof(ULONG));
407 if(ulNewSize <= sizeof(ULONG))
409 LocalFree((HLOCAL)*lppList);
410 *lppList = NULL; /* Removed the last element */
414 lpList = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList, ulNewSize,
415 LMEM_ZEROINIT|LMEM_MOVEABLE);
422 /*************************************************************************
425 * Find an item in a DataBlock list.
428 * lpList [I] List to search
429 * ulId [I] Id of item to find
432 * Success: A pointer to the list item found
436 * See SHWriteDataBlockList.
438 LPSHLWAPI_CLIST WINAPI SHFindDataBlock(LPSHLWAPI_CLIST lpList, ULONG ulId)
440 TRACE("(%p,%d)\n", lpList, ulId);
444 while(lpList->ulSize)
446 if(lpList->ulId == ulId)
447 return lpList; /* Matched */
448 else if(lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId)
449 return lpList + 1; /* Contained item matches */
451 lpList = NextItem(lpList);