2 * SHLWAPI Compact 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
30 /* Compact list element (ordinals 17-22) */
31 typedef struct tagSHLWAPI_CLIST
33 ULONG ulSize; /* Size of this list element and its data */
34 ULONG ulId; /* If 0xFFFFFFFF, The real element follows */
35 /* Item data (or a contained SHLWAPI_CLIST) follows... */
36 } SHLWAPI_CLIST, *LPSHLWAPI_CLIST;
38 typedef const SHLWAPI_CLIST* LPCSHLWAPI_CLIST;
40 /* ulId for contained SHLWAPI_CLIST items */
41 #define CLIST_ID_CONTAINER (~0UL)
43 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST*,LPCSHLWAPI_CLIST);
45 /*************************************************************************
48 * Internal helper: move a clist pointer to the next item.
50 inline static LPSHLWAPI_CLIST NextItem(LPCSHLWAPI_CLIST lpList)
52 const char* address = (char*)lpList;
53 address += lpList->ulSize;
54 return (LPSHLWAPI_CLIST)address;
57 /*************************************************************************
60 * Write a compact list to an IStream object.
63 * lpStream [I] IStream object to write the list to
64 * lpList [I] List of items to write
67 * Success: S_OK. The object is written to the stream.
68 * Failure: An HRESULT error code
71 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
72 * list structure which may be stored and retrieved from an IStream object.
74 * The exposed API consists of:
76 * SHLWAPI_17() Write a compact list to a stream,
78 * SHLWAPI_18() Read and create a list from a stream,
80 * SHLWAPI_19() Free a list,
82 * SHLWAPI_20() Insert a new item into a list,
84 * SHLWAPI_21() Remove an item from a list,
86 * SHLWAPI_22() Find an item in a list.
88 * The compact list is stored packed into a memory array. Each element has a
89 * size and an associated ID. Elements must be less than 64k if the list is
90 * to be subsequently read from a stream.
92 * Elements are aligned on DWORD boundaries. If an elements data size is not
93 * a DWORD size multiple, the element is wrapped by inserting a surrounding
94 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
96 * These functions are slow for large objects and long lists.
98 HRESULT WINAPI SHLWAPI_17(IStream* lpStream, LPSHLWAPI_CLIST lpList)
101 HRESULT hRet = E_FAIL;
103 TRACE("(%p,%p)\n", lpStream, lpList);
107 while (lpList->ulSize)
109 LPSHLWAPI_CLIST lpItem = lpList;
111 if(lpList->ulId == CLIST_ID_CONTAINER)
114 hRet = IStream_Write(lpStream,lpItem,lpItem->ulSize,&ulSize);
118 if(lpItem->ulSize != ulSize)
119 return STG_E_MEDIUMFULL;
121 lpList = NextItem(lpList);
130 /* Write a terminating list entry with zero size */
131 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
137 /*************************************************************************
140 * Read and create a compact list from an IStream object.
143 * lpStream [I] Stream to read the list from
144 * lppList [0] Pointer to receive the new List
148 * Failure: An HRESULT error code
151 * When read from a file, list objects are limited in size to 64k.
154 HRESULT WINAPI SHLWAPI_18(IStream* lpStream, LPSHLWAPI_CLIST* lppList)
156 SHLWAPI_CLIST bBuff[128]; /* Temporary storage for new list item */
157 ULONG ulBuffSize = sizeof(bBuff);
158 LPSHLWAPI_CLIST pItem = bBuff;
159 ULONG ulRead, ulSize;
162 TRACE("(%p,%p)\n", lpStream, lppList);
166 /* Free any existing list */
167 LocalFree((HLOCAL)*lppList);
173 /* Read the size of the next item */
174 hRet = IStream_Read(lpStream, &ulSize,sizeof(ulSize),&ulRead);
176 if(FAILED(hRet) || ulRead != sizeof(ulSize) || !ulSize)
177 break; /* Read failed or read zero size (the end of the list) */
181 LARGE_INTEGER liZero;
182 ULARGE_INTEGER ulPos;
186 /* Back the stream up; this object is too big for the list */
187 if(SUCCEEDED(IStream_Seek(lpStream, liZero, STREAM_SEEK_CUR, &ulPos)))
189 liZero.QuadPart = ulPos.QuadPart - sizeof(ULONG);
190 IStream_Seek(lpStream, liZero, STREAM_SEEK_SET, NULL);
194 else if (ulSize >= sizeof(SHLWAPI_CLIST))
196 /* Add this new item to the list */
197 if(ulSize > ulBuffSize)
199 /* We need more buffer space, allocate it */
200 LPSHLWAPI_CLIST lpTemp;
203 lpTemp = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT, ulSize);
205 lpTemp = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)pItem, ulSize,
206 LMEM_ZEROINIT|LMEM_MOVEABLE);
210 hRet = E_OUTOFMEMORY;
217 pItem->ulSize = ulSize;
218 ulSize -= sizeof(pItem->ulSize); /* already read this member */
220 /* Read the item Id and data */
221 hRet = IStream_Read(lpStream, &pItem->ulId, ulSize, &ulRead);
223 if(FAILED(hRet) || ulRead != ulSize)
226 SHLWAPI_20(lppList, pItem); /* Insert Item */
230 /* If we allocated space, free it */
232 LocalFree((HLOCAL)pItem);
237 /*************************************************************************
240 * Free a compact list.
243 * lpList [I] List to free
251 VOID WINAPI SHLWAPI_19(LPSHLWAPI_CLIST lpList)
253 TRACE("(%p)\n", lpList);
256 LocalFree((HLOCAL)lpList);
259 /*************************************************************************
262 * Insert a new item into a compact list.
265 * lppList [0] Pointer to the List
266 * lpNewItem [I] The new item to add to the list
269 * Success: S_OK. The item is added to the list.
270 * Failure: An HRESULT error code.
273 * If the size of the element to be inserted is less than the size of a
274 * SHLWAPI_CLIST node, or the Id for the item is CLIST_ID_CONTAINER,
275 * the call returns S_OK but does not actually add the element.
278 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST* lppList, LPCSHLWAPI_CLIST lpNewItem)
280 LPSHLWAPI_CLIST lpInsertAt = NULL;
283 TRACE("(%p,%p)\n", lppList, lpNewItem);
285 if(!lppList || !lpNewItem )
288 if (lpNewItem->ulSize < sizeof(SHLWAPI_CLIST) ||
289 lpNewItem->ulId == CLIST_ID_CONTAINER)
292 ulSize = lpNewItem->ulSize;
296 /* Tune size to a ULONG boundary, add space for container element */
297 ulSize = ((ulSize + 0x3) & 0xFFFFFFFC) + sizeof(SHLWAPI_CLIST);
298 TRACE("Creating container item, new size = %ld\n", ulSize);
303 /* An empty list. Allocate space for terminal ulSize also */
304 *lppList = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT,
305 ulSize + sizeof(ULONG));
306 lpInsertAt = *lppList;
310 /* Append to the end of the list */
311 ULONG ulTotalSize = 0;
312 LPSHLWAPI_CLIST lpIter = *lppList;
314 /* Iterate to the end of the list, calculating the total size */
315 while (lpIter->ulSize)
317 ulTotalSize += lpIter->ulSize;
318 lpIter = NextItem(lpIter);
321 /* Increase the size of the list */
322 lpIter = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList,
323 ulTotalSize + ulSize+sizeof(ULONG),
324 LMEM_ZEROINIT | LMEM_MOVEABLE);
328 lpInsertAt = (LPSHLWAPI_CLIST)((char*)lpIter + ulTotalSize); /* At end */
334 /* Copy in the new item */
335 LPSHLWAPI_CLIST lpDest = lpInsertAt;
337 if(ulSize != lpNewItem->ulSize)
339 lpInsertAt->ulSize = ulSize;
340 lpInsertAt->ulId = CLIST_ID_CONTAINER;
343 memcpy(lpDest, lpNewItem, lpNewItem->ulSize);
345 /* Terminate the list */
346 lpInsertAt = NextItem(lpInsertAt);
347 lpInsertAt->ulSize = 0;
349 return lpNewItem->ulSize;
354 /*************************************************************************
357 * Remove an item from a compact list.
360 * lppList [O] List to remove the item from
361 * ulId [I] Id of item to remove
365 * Failure: FALSE, If any parameters are invalid, or the item was not found.
370 BOOL WINAPI SHLWAPI_21(LPSHLWAPI_CLIST* lppList, ULONG ulId)
372 LPSHLWAPI_CLIST lpList = 0;
373 LPSHLWAPI_CLIST lpItem = NULL;
374 LPSHLWAPI_CLIST lpNext;
377 TRACE("(%p,%ld)\n", lppList, ulId);
379 if(lppList && (lpList = *lppList))
381 /* Search for item in list */
382 while (lpList->ulSize)
384 if(lpList->ulId == ulId ||
385 (lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId))
387 lpItem = lpList; /* Found */
390 lpList = NextItem(lpList);
397 lpList = lpNext = NextItem(lpItem);
399 /* Locate the end of the list */
400 while (lpList->ulSize)
401 lpList = NextItem(lpList);
403 /* Resize the list */
404 ulNewSize = LocalSize((HLOCAL)*lppList) - lpItem->ulSize;
406 /* Copy following elements over lpItem */
407 memmove(lpItem, lpNext, (char *)lpList - (char *)lpNext + sizeof(ULONG));
409 if(ulNewSize <= sizeof(ULONG))
411 LocalFree((HLOCAL)*lppList);
412 *lppList = NULL; /* Removed the last element */
416 lpList = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList, ulNewSize,
417 LMEM_ZEROINIT|LMEM_MOVEABLE);
424 /*************************************************************************
427 * Find an item in a compact list.
430 * lpList [I] List to search
431 * ulId [I] Id of item to find
434 * Success: A pointer to the list item found
440 LPSHLWAPI_CLIST WINAPI SHLWAPI_22(LPSHLWAPI_CLIST lpList, ULONG ulId)
442 TRACE("(%p,%ld)\n", lpList, ulId);
446 while(lpList->ulSize)
448 if(lpList->ulId == ulId)
449 return lpList; /* Matched */
450 else if(lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId)
451 return lpList + 1; /* Contained item matches */
453 lpList = NextItem(lpList);