Fix the FilterGraph CLSID declaration.
[wine/multimedia.git] / dlls / shlwapi / clist.c
blobf0f858e4d7897fa6e722262bab22a44fbdeb57d0
1 /*
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
20 #include <string.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "objbase.h"
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 -1, 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 static const ULONG CLIST_ID_CONTAINER = -1u;
43 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST*,LPCSHLWAPI_CLIST);
45 /*************************************************************************
46 * NextItem
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 /*************************************************************************
58 * @ [SHLWAPI.17]
60 * Write a compact list to an IStream object.
62 * PARAMS
63 * lpStream [I] IStream object to write the list to
64 * lpList [I] List of items to write
66 * RETURNS
67 * Success: S_OK. The object is written to the stream.
68 * Failure: An HRESULT error code
70 * NOTES
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 -1, 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)
100 ULONG ulSize;
101 HRESULT hRet = E_FAIL;
103 TRACE("(%p,%p)\n", lpStream, lpList);
105 if(lpList)
107 while (lpList->ulSize)
109 LPSHLWAPI_CLIST lpItem = lpList;
111 if(lpList->ulId == CLIST_ID_CONTAINER)
112 lpItem++;
114 hRet = IStream_Write(lpStream,lpItem,lpItem->ulSize,&ulSize);
115 if (FAILED(hRet))
116 return hRet;
118 if(lpItem->ulSize != ulSize)
119 return STG_E_MEDIUMFULL;
121 lpList = NextItem(lpList);
125 if(SUCCEEDED(hRet))
127 ULONG ulDummy;
128 ulSize = 0;
130 /* Write a terminating list entry with zero size */
131 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
134 return hRet;
137 /*************************************************************************
138 * @ [SHLWAPI.18]
140 * Read and create a compact list from an IStream object.
142 * PARAMS
143 * lpStream [I] Stream to read the list from
144 * lppList [0] Pointer to recieve the new List
146 * RETURNS
147 * Success: S_OK
148 * Failure: An HRESULT error code
150 * NOTES
151 * When read from a file, list objects are limited in size to 64k.
152 * See SHLWAPI_17.
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;
160 HRESULT hRet = S_OK;
162 TRACE("(%p,%p)\n", lpStream, lppList);
164 if(*lppList)
166 /* Free any existing list */
167 LocalFree((HLOCAL)*lppList);
168 *lppList = NULL;
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) */
179 if(ulSize > 0xFFFF)
181 LARGE_INTEGER liZero;
182 ULARGE_INTEGER ulPos;
184 liZero.QuadPart = 0;
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);
192 break;
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;
202 if (pItem == bBuff)
203 lpTemp = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT, ulSize);
204 else
205 lpTemp = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)pItem, ulSize,
206 LMEM_ZEROINIT|LMEM_MOVEABLE);
208 if(!lpTemp)
210 hRet = E_OUTOFMEMORY;
211 break;
213 ulBuffSize = ulSize;
214 pItem = lpTemp;
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)
224 break;
226 SHLWAPI_20(lppList, pItem); /* Insert Item */
228 } while(1);
230 /* If we allocated space, free it */
231 if(pItem != bBuff)
232 LocalFree((HLOCAL)pItem);
234 return hRet;
237 /*************************************************************************
238 * @ [SHLWAPI.19]
240 * Free a compact list.
242 * PARAMS
243 * lpList [I] List to free
245 * RETURNS
246 * Nothing.
248 * NOTES
249 * See SHLWAPI_17.
251 VOID WINAPI SHLWAPI_19(LPSHLWAPI_CLIST lpList)
253 TRACE("(%p)\n", lpList);
255 if (lpList)
256 LocalFree((HLOCAL)lpList);
259 /*************************************************************************
260 * @ [SHLWAPI.20]
262 * Insert a new item into a compact list.
264 * PARAMS
265 * lppList [0] Pointer to the List
266 * lpNewItem [I] The new item to add to the list
268 * RETURNS
269 * Success: S_OK. The item is added to the list.
270 * Failure: An HRESULT error code.
272 * NOTES
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.
276 * See SHLWAPI_17.
278 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST* lppList, LPCSHLWAPI_CLIST lpNewItem)
280 LPSHLWAPI_CLIST lpInsertAt = NULL;
281 ULONG ulSize;
283 TRACE("(%p,%p)\n", lppList, lpNewItem);
285 if(!lppList || !lpNewItem )
286 return E_INVALIDARG;
288 if (lpNewItem->ulSize < sizeof(SHLWAPI_CLIST) ||
289 lpNewItem->ulId == CLIST_ID_CONTAINER)
290 return S_OK;
292 ulSize = lpNewItem->ulSize;
294 if(ulSize & 0x3)
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);
301 if(!*lppList)
303 /* An empty list. Allocate space for terminal ulSize also */
304 *lppList = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT,
305 ulSize + sizeof(ULONG));
306 lpInsertAt = *lppList;
308 else
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);
325 if(lpIter)
327 *lppList = lpIter;
328 lpInsertAt = (LPSHLWAPI_CLIST)((char*)lpIter + ulTotalSize); /* At end */
332 if(lpInsertAt)
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;
341 lpDest++;
343 memcpy(lpDest, lpNewItem, lpNewItem->ulSize);
345 /* Terminate the list */
346 lpInsertAt = NextItem(lpInsertAt);
347 lpInsertAt->ulSize = 0;
349 return lpNewItem->ulSize;
351 return S_OK;
354 /*************************************************************************
355 * @ [SHLWAPI.21]
357 * Remove an item from a compact list.
359 * PARAMS
360 * lppList [O] List to remove the item from
361 * ulId [I] Id of item to remove
363 * RETURNS
364 * Success: TRUE.
365 * Failure: FALSE, If any parameters are invalid, or the item was not found.
367 * NOTES
368 * See SHLWAPI_17.
370 BOOL WINAPI SHLWAPI_21(LPSHLWAPI_CLIST* lppList, ULONG ulId)
372 LPSHLWAPI_CLIST lpList = 0;
373 LPSHLWAPI_CLIST lpItem = NULL;
374 LPSHLWAPI_CLIST lpNext;
375 ULONG ulNewSize;
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 */
388 break;
390 lpList = NextItem(lpList);
394 if(!lpItem)
395 return FALSE;
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 */
414 else
416 lpList = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList, ulNewSize,
417 LMEM_ZEROINIT|LMEM_MOVEABLE);
418 if(lpList)
419 *lppList = lpList;
421 return TRUE;
424 /*************************************************************************
425 * @ [SHLWAPI.22]
427 * Find an item in a compact list.
429 * PARAMS
430 * lpList [I] List to search
431 * ulId [I] Id of item to find
433 * RETURNS
434 * Success: A pointer to the list item found
435 * Failure: NULL
437 * NOTES
438 * See SHLWAPI_17.
440 LPSHLWAPI_CLIST WINAPI SHLWAPI_22(LPSHLWAPI_CLIST lpList, ULONG ulId)
442 TRACE("(%p,%ld)\n", lpList, ulId);
444 if(lpList)
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);
456 return NULL;