2 * IAssemblyName implementation
4 * Copyright 2008 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
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37 #include "fusionpriv.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
42 IAssemblyName IAssemblyName_iface
;
60 static const WCHAR separator
[] = {',',' ',0};
61 static const WCHAR version
[] = {'V','e','r','s','i','o','n',0};
62 static const WCHAR culture
[] = {'C','u','l','t','u','r','e',0};
63 static const WCHAR pubkey
[] =
64 {'P','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
65 static const WCHAR procarch
[] = {'p','r','o','c','e','s','s','o','r',
66 'A','r','c','h','i','t','e','c','t','u','r','e',0};
68 #define CHARS_PER_PUBKEY 16
70 static inline IAssemblyNameImpl
*impl_from_IAssemblyName(IAssemblyName
*iface
)
72 return CONTAINING_RECORD(iface
, IAssemblyNameImpl
, IAssemblyName_iface
);
75 static HRESULT WINAPI
IAssemblyNameImpl_QueryInterface(IAssemblyName
*iface
,
76 REFIID riid
, LPVOID
*ppobj
)
78 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
80 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
84 if (IsEqualIID(riid
, &IID_IUnknown
) ||
85 IsEqualIID(riid
, &IID_IAssemblyName
))
87 IAssemblyName_AddRef(iface
);
88 *ppobj
= &This
->IAssemblyName_iface
;
92 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
96 static ULONG WINAPI
IAssemblyNameImpl_AddRef(IAssemblyName
*iface
)
98 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
99 ULONG refCount
= InterlockedIncrement(&This
->ref
);
101 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
106 static ULONG WINAPI
IAssemblyNameImpl_Release(IAssemblyName
*iface
)
108 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
109 ULONG refCount
= InterlockedDecrement(&This
->ref
);
111 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
115 HeapFree(GetProcessHeap(), 0, This
->path
);
116 HeapFree(GetProcessHeap(), 0, This
->displayname
);
117 HeapFree(GetProcessHeap(), 0, This
->name
);
118 HeapFree(GetProcessHeap(), 0, This
->culture
);
119 HeapFree(GetProcessHeap(), 0, This
->procarch
);
120 HeapFree(GetProcessHeap(), 0, This
);
126 static HRESULT WINAPI
IAssemblyNameImpl_SetProperty(IAssemblyName
*iface
,
131 FIXME("(%p, %d, %p, %d) stub!\n", iface
, PropertyId
, pvProperty
, cbProperty
);
135 static HRESULT WINAPI
IAssemblyNameImpl_GetProperty(IAssemblyName
*iface
,
140 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
142 TRACE("(%p, %d, %p, %p)\n", iface
, PropertyId
, pvProperty
, pcbProperty
);
144 *((LPWSTR
)pvProperty
) = '\0';
148 case ASM_NAME_NULL_PUBLIC_KEY
:
149 case ASM_NAME_NULL_PUBLIC_KEY_TOKEN
:
154 case ASM_NAME_NULL_CUSTOM
:
161 lstrcpyW(pvProperty
, name
->name
);
162 *pcbProperty
= (lstrlenW(name
->name
) + 1) * 2;
166 case ASM_NAME_MAJOR_VERSION
:
168 *((WORD
*)pvProperty
) = name
->version
[0];
169 if (name
->versize
>= 1)
170 *pcbProperty
= sizeof(WORD
);
173 case ASM_NAME_MINOR_VERSION
:
175 *((WORD
*)pvProperty
) = name
->version
[1];
176 if (name
->versize
>= 2)
177 *pcbProperty
= sizeof(WORD
);
180 case ASM_NAME_BUILD_NUMBER
:
182 *((WORD
*)pvProperty
) = name
->version
[2];
183 if (name
->versize
>= 3)
184 *pcbProperty
= sizeof(WORD
);
187 case ASM_NAME_REVISION_NUMBER
:
189 *((WORD
*)pvProperty
) = name
->version
[3];
190 if (name
->versize
>= 4)
191 *pcbProperty
= sizeof(WORD
);
194 case ASM_NAME_CULTURE
:
198 lstrcpyW(pvProperty
, name
->culture
);
199 *pcbProperty
= (lstrlenW(name
->culture
) + 1) * 2;
203 case ASM_NAME_PUBLIC_KEY_TOKEN
:
207 memcpy(pvProperty
, name
->pubkey
, sizeof(DWORD
) * 2);
208 *pcbProperty
= sizeof(DWORD
) * 2;
220 static HRESULT WINAPI
IAssemblyNameImpl_Finalize(IAssemblyName
*iface
)
222 FIXME("(%p) stub!\n", iface
);
226 static HRESULT WINAPI
IAssemblyNameImpl_GetDisplayName(IAssemblyName
*iface
,
227 LPOLESTR szDisplayName
,
228 LPDWORD pccDisplayName
,
229 DWORD dwDisplayFlags
)
231 static const WCHAR equals
[] = {'=',0};
232 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
233 WCHAR verstr
[30], *cultureval
= NULL
;
236 TRACE("(%p, %p, %p, %d)\n", iface
, szDisplayName
,
237 pccDisplayName
, dwDisplayFlags
);
239 if (dwDisplayFlags
== 0)
241 if (!name
->displayname
|| !*name
->displayname
)
242 return FUSION_E_INVALID_NAME
;
244 size
= strlenW(name
->displayname
) + 1;
246 if (*pccDisplayName
< size
)
248 *pccDisplayName
= size
;
249 return E_NOT_SUFFICIENT_BUFFER
;
252 if (szDisplayName
) strcpyW(szDisplayName
, name
->displayname
);
253 *pccDisplayName
= size
;
258 if (!name
->name
|| !*name
->name
)
259 return FUSION_E_INVALID_NAME
;
261 /* Verify buffer size is sufficient */
262 size
= lstrlenW(name
->name
) + 1;
264 if ((dwDisplayFlags
& ASM_DISPLAYF_VERSION
) && (name
->versize
> 0))
266 static const WCHAR spec
[] = {'%','d',0};
267 static const WCHAR period
[] = {'.',0};
270 wsprintfW(verstr
, spec
, name
->version
[0]);
272 for (i
= 1; i
< name
->versize
; i
++)
275 wsprintfW(value
, spec
, name
->version
[i
]);
277 lstrcatW(verstr
, period
);
278 lstrcatW(verstr
, value
);
281 size
+= lstrlenW(separator
) + lstrlenW(version
) + lstrlenW(equals
) + lstrlenW(verstr
);
284 if ((dwDisplayFlags
& ASM_DISPLAYF_CULTURE
) && (name
->culture
))
286 static const WCHAR neutral
[] = {'n','e','u','t','r','a','l', 0};
288 cultureval
= (lstrlenW(name
->culture
) == 2) ? name
->culture
: (LPWSTR
) neutral
;
289 size
+= lstrlenW(separator
) + lstrlenW(culture
) + lstrlenW(equals
) + lstrlenW(cultureval
);
292 if ((dwDisplayFlags
& ASM_DISPLAYF_PUBLIC_KEY_TOKEN
) && (name
->haspubkey
))
293 size
+= lstrlenW(separator
) + lstrlenW(pubkey
) + lstrlenW(equals
) + CHARS_PER_PUBKEY
;
295 if ((dwDisplayFlags
& ASM_DISPLAYF_PROCESSORARCHITECTURE
) && (name
->procarch
))
296 size
+= lstrlenW(separator
) + lstrlenW(procarch
) + lstrlenW(equals
) + lstrlenW(name
->procarch
);
298 if (size
> *pccDisplayName
)
301 /* Construct the string */
302 lstrcpyW(szDisplayName
, name
->name
);
304 if ((dwDisplayFlags
& ASM_DISPLAYF_VERSION
) && (name
->versize
> 0))
306 lstrcatW(szDisplayName
, separator
);
308 lstrcatW(szDisplayName
, version
);
309 lstrcatW(szDisplayName
, equals
);
310 lstrcatW(szDisplayName
, verstr
);
313 if ((dwDisplayFlags
& ASM_DISPLAYF_CULTURE
) && (name
->culture
))
315 lstrcatW(szDisplayName
, separator
);
317 lstrcatW(szDisplayName
, culture
);
318 lstrcatW(szDisplayName
, equals
);
319 lstrcatW(szDisplayName
, cultureval
);
322 if ((dwDisplayFlags
& ASM_DISPLAYF_PUBLIC_KEY_TOKEN
) && (name
->haspubkey
))
324 WCHAR pkt
[CHARS_PER_PUBKEY
+ 1];
325 static const WCHAR spec
[] = {'%','0','x','%','0','x','%','0','x',
326 '%','0','x','%','0','x','%','0','x','%','0','x','%','0','x',0};
328 lstrcatW(szDisplayName
, separator
);
330 lstrcatW(szDisplayName
, pubkey
);
331 lstrcatW(szDisplayName
, equals
);
333 wsprintfW(pkt
, spec
, name
->pubkey
[0], name
->pubkey
[1], name
->pubkey
[2],
334 name
->pubkey
[3], name
->pubkey
[4], name
->pubkey
[5], name
->pubkey
[6],
337 lstrcatW(szDisplayName
, pkt
);
340 if ((dwDisplayFlags
& ASM_DISPLAYF_PROCESSORARCHITECTURE
) && (name
->procarch
))
342 lstrcatW(szDisplayName
, separator
);
344 lstrcatW(szDisplayName
, procarch
);
345 lstrcatW(szDisplayName
, equals
);
346 lstrcatW(szDisplayName
, name
->procarch
);
349 *pccDisplayName
= size
;
353 static HRESULT WINAPI
IAssemblyNameImpl_Reserved(IAssemblyName
*iface
,
355 IUnknown
*pUnkReserved1
,
356 IUnknown
*pUnkReserved2
,
357 LPCOLESTR szReserved
,
363 TRACE("(%p, %s, %p, %p, %s, %x%08x, %p, %d, %p)\n", iface
,
364 debugstr_guid(refIID
), pUnkReserved1
, pUnkReserved2
,
365 debugstr_w(szReserved
), (DWORD
)(llReserved
>> 32), (DWORD
)llReserved
,
366 pvReserved
, cbReserved
, ppReserved
);
371 static HRESULT WINAPI
IAssemblyNameImpl_GetName(IAssemblyName
*iface
,
375 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
378 TRACE("(%p, %p, %p)\n", iface
, lpcwBuffer
, pwzName
);
381 len
= strlenW(name
->name
) + 1;
385 if (*lpcwBuffer
< len
)
388 return E_NOT_SUFFICIENT_BUFFER
;
390 if (!name
->name
) lpcwBuffer
[0] = 0;
391 else strcpyW(pwzName
, name
->name
);
397 static HRESULT WINAPI
IAssemblyNameImpl_GetVersion(IAssemblyName
*iface
,
398 LPDWORD pdwVersionHi
,
399 LPDWORD pdwVersionLow
)
401 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
403 TRACE("(%p, %p, %p)\n", iface
, pdwVersionHi
, pdwVersionLow
);
408 if (name
->versize
!= 4)
409 return FUSION_E_INVALID_NAME
;
411 *pdwVersionHi
= (name
->version
[0] << 16) + name
->version
[1];
412 *pdwVersionLow
= (name
->version
[2] << 16) + name
->version
[3];
417 static HRESULT WINAPI
IAssemblyNameImpl_IsEqual(IAssemblyName
*iface
,
418 IAssemblyName
*pName
,
421 IAssemblyNameImpl
*name1
= impl_from_IAssemblyName(iface
);
422 IAssemblyNameImpl
*name2
= impl_from_IAssemblyName(pName
);
424 TRACE("(%p, %p, 0x%08x)\n", iface
, pName
, flags
);
426 if (!pName
) return S_FALSE
;
427 if (flags
& ~ASM_CMPF_IL_ALL
) FIXME("unsupported flags\n");
429 if ((flags
& ASM_CMPF_NAME
) && strcmpW(name1
->name
, name2
->name
)) return S_FALSE
;
430 if (name1
->versize
&& name2
->versize
)
432 if ((flags
& ASM_CMPF_MAJOR_VERSION
) &&
433 name1
->version
[0] != name2
->version
[0]) return S_FALSE
;
434 if ((flags
& ASM_CMPF_MINOR_VERSION
) &&
435 name1
->version
[1] != name2
->version
[1]) return S_FALSE
;
436 if ((flags
& ASM_CMPF_BUILD_NUMBER
) &&
437 name1
->version
[2] != name2
->version
[2]) return S_FALSE
;
438 if ((flags
& ASM_CMPF_REVISION_NUMBER
) &&
439 name1
->version
[3] != name2
->version
[3]) return S_FALSE
;
441 if ((flags
& ASM_CMPF_PUBLIC_KEY_TOKEN
) &&
442 name1
->haspubkey
&& name2
->haspubkey
&&
443 memcmp(name1
->pubkey
, name2
->pubkey
, sizeof(name1
->pubkey
))) return S_FALSE
;
445 if ((flags
& ASM_CMPF_CULTURE
) &&
446 name1
->culture
&& name2
->culture
&&
447 strcmpW(name1
->culture
, name2
->culture
)) return S_FALSE
;
452 static HRESULT WINAPI
IAssemblyNameImpl_Clone(IAssemblyName
*iface
,
453 IAssemblyName
**pName
)
455 FIXME("(%p, %p) stub!\n", iface
, pName
);
459 static const IAssemblyNameVtbl AssemblyNameVtbl
= {
460 IAssemblyNameImpl_QueryInterface
,
461 IAssemblyNameImpl_AddRef
,
462 IAssemblyNameImpl_Release
,
463 IAssemblyNameImpl_SetProperty
,
464 IAssemblyNameImpl_GetProperty
,
465 IAssemblyNameImpl_Finalize
,
466 IAssemblyNameImpl_GetDisplayName
,
467 IAssemblyNameImpl_Reserved
,
468 IAssemblyNameImpl_GetName
,
469 IAssemblyNameImpl_GetVersion
,
470 IAssemblyNameImpl_IsEqual
,
471 IAssemblyNameImpl_Clone
474 /* Internal methods */
475 static inline IAssemblyNameImpl
*unsafe_impl_from_IAssemblyName(IAssemblyName
*iface
)
477 assert(iface
->lpVtbl
== &AssemblyNameVtbl
);
479 return impl_from_IAssemblyName(iface
);
482 HRESULT
IAssemblyName_SetPath(IAssemblyName
*iface
, LPCWSTR path
)
484 IAssemblyNameImpl
*name
= unsafe_impl_from_IAssemblyName(iface
);
486 name
->path
= strdupW(path
);
488 return E_OUTOFMEMORY
;
493 HRESULT
IAssemblyName_GetPath(IAssemblyName
*iface
, LPWSTR buf
, ULONG
*len
)
495 ULONG buffer_size
= *len
;
496 IAssemblyNameImpl
*name
= unsafe_impl_from_IAssemblyName(iface
);
504 *len
= lstrlenW(name
->path
) + 1;
506 if (*len
<= buffer_size
)
507 lstrcpyW(buf
, name
->path
);
509 return E_NOT_SUFFICIENT_BUFFER
;
514 static HRESULT
parse_version(IAssemblyNameImpl
*name
, LPWSTR version
)
519 for (i
= 0, beg
= version
; i
< 4; i
++)
524 end
= strchrW(beg
, '.');
526 if (end
) *end
= '\0';
527 name
->version
[i
] = atolW(beg
);
539 static HRESULT
parse_culture(IAssemblyNameImpl
*name
, LPCWSTR culture
)
541 static const WCHAR empty
[] = {0};
543 if (lstrlenW(culture
) == 2)
544 name
->culture
= strdupW(culture
);
546 name
->culture
= strdupW(empty
);
551 static BOOL
is_hex(WCHAR c
)
553 return ((c
>= 'a' && c
<= 'f') ||
554 (c
>= 'A' && c
<= 'F') ||
555 (c
>= '0' && c
<= '9'));
558 static BYTE
hextobyte(WCHAR c
)
560 if(c
>= '0' && c
<= '9')
562 if(c
>= 'A' && c
<= 'F')
564 if(c
>= 'a' && c
<= 'f')
569 static HRESULT
parse_pubkey(IAssemblyNameImpl
*name
, LPCWSTR pubkey
)
573 static const WCHAR nullstr
[] = {'n','u','l','l',0};
575 if(lstrcmpiW(pubkey
, nullstr
) == 0)
576 return FUSION_E_PRIVATE_ASM_DISALLOWED
;
578 if (lstrlenW(pubkey
) < CHARS_PER_PUBKEY
)
579 return FUSION_E_INVALID_NAME
;
581 for (i
= 0; i
< CHARS_PER_PUBKEY
; i
++)
582 if (!is_hex(pubkey
[i
]))
583 return FUSION_E_INVALID_NAME
;
585 name
->haspubkey
= TRUE
;
587 for (i
= 0; i
< CHARS_PER_PUBKEY
; i
+= 2)
589 val
= (hextobyte(pubkey
[i
]) << 4) + hextobyte(pubkey
[i
+ 1]);
590 name
->pubkey
[i
/ 2] = val
;
596 static WCHAR
*parse_value( const WCHAR
*str
, unsigned int len
)
599 const WCHAR
*p
= str
;
603 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
609 while (*p
&& *p
!= '\"') ret
[i
++] = *p
++;
610 if ((quoted
&& *p
!= '\"') || (!quoted
&& *p
== '\"'))
612 HeapFree( GetProcessHeap(), 0, ret
);
619 static HRESULT
parse_display_name(IAssemblyNameImpl
*name
, LPCWSTR szAssemblyName
)
621 LPWSTR str
, save
, ptr
, ptr2
, value
;
628 name
->displayname
= strdupW(szAssemblyName
);
629 if (!name
->displayname
)
630 return E_OUTOFMEMORY
;
632 str
= strdupW(szAssemblyName
);
640 ptr
= strchrW(str
, ',');
641 if (ptr
) *ptr
= '\0';
643 /* no ',' but ' ' only */
644 if( !ptr
&& strchrW(str
, ' ') )
646 hr
= FUSION_E_INVALID_NAME
;
650 name
->name
= strdupW(str
);
663 ptr
= strchrW(str
, '=');
666 hr
= FUSION_E_INVALID_NAME
;
673 hr
= FUSION_E_INVALID_NAME
;
677 if (!(ptr2
= strchrW(ptr
, ',')))
679 if (!(ptr2
= strchrW(ptr
, '\0')))
681 hr
= FUSION_E_INVALID_NAME
;
689 if (!(value
= parse_value( ptr
, ptr2
- ptr
)))
691 hr
= FUSION_E_INVALID_NAME
;
694 while (*str
== ' ') str
++;
696 if (!lstrcmpiW(str
, version
))
697 hr
= parse_version( name
, value
);
698 else if (!lstrcmpiW(str
, culture
))
699 hr
= parse_culture( name
, value
);
700 else if (!lstrcmpiW(str
, pubkey
))
701 hr
= parse_pubkey( name
, value
);
702 else if (!lstrcmpiW(str
, procarch
))
704 name
->procarch
= value
;
707 HeapFree( GetProcessHeap(), 0, value
);
716 HeapFree(GetProcessHeap(), 0, save
);
719 HeapFree(GetProcessHeap(), 0, name
->displayname
);
720 HeapFree(GetProcessHeap(), 0, name
->name
);
721 HeapFree(GetProcessHeap(), 0, name
->culture
);
722 HeapFree(GetProcessHeap(), 0, name
->procarch
);
727 /******************************************************************
728 * CreateAssemblyNameObject (FUSION.@)
730 HRESULT WINAPI
CreateAssemblyNameObject(LPASSEMBLYNAME
*ppAssemblyNameObj
,
731 LPCWSTR szAssemblyName
, DWORD dwFlags
,
734 IAssemblyNameImpl
*name
;
737 TRACE("(%p, %s, %08x, %p)\n", ppAssemblyNameObj
,
738 debugstr_w(szAssemblyName
), dwFlags
, pvReserved
);
740 if (!ppAssemblyNameObj
)
743 if ((dwFlags
& CANOF_PARSE_DISPLAY_NAME
) &&
744 (!szAssemblyName
|| !*szAssemblyName
))
747 name
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAssemblyNameImpl
));
749 return E_OUTOFMEMORY
;
751 name
->IAssemblyName_iface
.lpVtbl
= &AssemblyNameVtbl
;
754 hr
= parse_display_name(name
, szAssemblyName
);
757 HeapFree(GetProcessHeap(), 0, name
);
761 *ppAssemblyNameObj
= &name
->IAssemblyName_iface
;