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
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "fusionpriv.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
43 IAssemblyName IAssemblyName_iface
;
63 static const WCHAR separator
[] = {',',' ',0};
64 static const WCHAR version
[] = {'V','e','r','s','i','o','n',0};
65 static const WCHAR culture
[] = {'C','u','l','t','u','r','e',0};
66 static const WCHAR pubkey
[] =
67 {'P','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
68 static const WCHAR procarch
[] = {'p','r','o','c','e','s','s','o','r',
69 'A','r','c','h','i','t','e','c','t','u','r','e',0};
71 #define CHARS_PER_PUBKEY 16
73 static inline IAssemblyNameImpl
*impl_from_IAssemblyName(IAssemblyName
*iface
)
75 return CONTAINING_RECORD(iface
, IAssemblyNameImpl
, IAssemblyName_iface
);
78 static HRESULT WINAPI
IAssemblyNameImpl_QueryInterface(IAssemblyName
*iface
,
79 REFIID riid
, LPVOID
*ppobj
)
81 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
83 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
87 if (IsEqualIID(riid
, &IID_IUnknown
) ||
88 IsEqualIID(riid
, &IID_IAssemblyName
))
90 IAssemblyName_AddRef(iface
);
91 *ppobj
= &This
->IAssemblyName_iface
;
95 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
99 static ULONG WINAPI
IAssemblyNameImpl_AddRef(IAssemblyName
*iface
)
101 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
102 ULONG refCount
= InterlockedIncrement(&This
->ref
);
104 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
109 static ULONG WINAPI
IAssemblyNameImpl_Release(IAssemblyName
*iface
)
111 IAssemblyNameImpl
*This
= impl_from_IAssemblyName(iface
);
112 ULONG refCount
= InterlockedDecrement(&This
->ref
);
114 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
118 HeapFree(GetProcessHeap(), 0, This
->path
);
119 HeapFree(GetProcessHeap(), 0, This
->displayname
);
120 HeapFree(GetProcessHeap(), 0, This
->name
);
121 HeapFree(GetProcessHeap(), 0, This
->culture
);
122 HeapFree(GetProcessHeap(), 0, This
->procarch
);
123 HeapFree(GetProcessHeap(), 0, This
);
129 static HRESULT WINAPI
IAssemblyNameImpl_SetProperty(IAssemblyName
*iface
,
134 FIXME("(%p, %d, %p, %d) stub!\n", iface
, PropertyId
, pvProperty
, cbProperty
);
138 static HRESULT WINAPI
IAssemblyNameImpl_GetProperty(IAssemblyName
*iface
,
143 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
146 TRACE("(%p, %d, %p, %p)\n", iface
, PropertyId
, pvProperty
, pcbProperty
);
151 case ASM_NAME_NULL_PUBLIC_KEY
:
152 case ASM_NAME_NULL_PUBLIC_KEY_TOKEN
:
157 case ASM_NAME_NULL_CUSTOM
:
164 *pcbProperty
= (lstrlenW(name
->name
) + 1) * 2;
165 if (size
< *pcbProperty
)
166 return STRSAFE_E_INSUFFICIENT_BUFFER
;
167 lstrcpyW(pvProperty
, name
->name
);
171 case ASM_NAME_MAJOR_VERSION
:
173 if (name
->versize
>= 1)
175 *pcbProperty
= sizeof(WORD
);
176 if (size
< *pcbProperty
)
177 return STRSAFE_E_INSUFFICIENT_BUFFER
;
178 *((WORD
*)pvProperty
) = name
->version
[0];
182 case ASM_NAME_MINOR_VERSION
:
184 if (name
->versize
>= 2)
186 *pcbProperty
= sizeof(WORD
);
187 if (size
< *pcbProperty
)
188 return STRSAFE_E_INSUFFICIENT_BUFFER
;
189 *((WORD
*)pvProperty
) = name
->version
[1];
193 case ASM_NAME_BUILD_NUMBER
:
195 if (name
->versize
>= 3)
197 *pcbProperty
= sizeof(WORD
);
198 if (size
< *pcbProperty
)
199 return STRSAFE_E_INSUFFICIENT_BUFFER
;
200 *((WORD
*)pvProperty
) = name
->version
[2];
204 case ASM_NAME_REVISION_NUMBER
:
206 if (name
->versize
>= 4)
208 *pcbProperty
= sizeof(WORD
);
209 if (size
< *pcbProperty
)
210 return STRSAFE_E_INSUFFICIENT_BUFFER
;
211 *((WORD
*)pvProperty
) = name
->version
[3];
215 case ASM_NAME_CULTURE
:
219 *pcbProperty
= (lstrlenW(name
->culture
) + 1) * 2;
220 if (size
< *pcbProperty
)
221 return STRSAFE_E_INSUFFICIENT_BUFFER
;
222 lstrcpyW(pvProperty
, name
->culture
);
226 case ASM_NAME_PUBLIC_KEY_TOKEN
:
230 *pcbProperty
= sizeof(DWORD
) * 2;
231 if (size
< *pcbProperty
)
232 return STRSAFE_E_INSUFFICIENT_BUFFER
;
233 memcpy(pvProperty
, name
->pubkey
, sizeof(DWORD
) * 2);
237 case ASM_NAME_ARCHITECTURE
:
239 if (name
->pekind
!= peNone
)
241 *pcbProperty
= sizeof(PEKIND
);
242 if (size
< *pcbProperty
)
243 return STRSAFE_E_INSUFFICIENT_BUFFER
;
244 *((PEKIND
*)pvProperty
) = name
->pekind
;
256 static HRESULT WINAPI
IAssemblyNameImpl_Finalize(IAssemblyName
*iface
)
258 FIXME("(%p) stub!\n", iface
);
262 static HRESULT WINAPI
IAssemblyNameImpl_GetDisplayName(IAssemblyName
*iface
,
263 LPOLESTR szDisplayName
,
264 LPDWORD pccDisplayName
,
265 DWORD dwDisplayFlags
)
267 static const WCHAR equals
[] = {'=',0};
268 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
269 WCHAR verstr
[30], *cultureval
= NULL
;
272 TRACE("(%p, %p, %p, %d)\n", iface
, szDisplayName
,
273 pccDisplayName
, dwDisplayFlags
);
275 if (dwDisplayFlags
== 0)
277 if (!name
->displayname
|| !*name
->displayname
)
278 return FUSION_E_INVALID_NAME
;
280 size
= strlenW(name
->displayname
) + 1;
282 if (*pccDisplayName
< size
)
284 *pccDisplayName
= size
;
285 return E_NOT_SUFFICIENT_BUFFER
;
288 if (szDisplayName
) strcpyW(szDisplayName
, name
->displayname
);
289 *pccDisplayName
= size
;
294 if (!name
->name
|| !*name
->name
)
295 return FUSION_E_INVALID_NAME
;
297 /* Verify buffer size is sufficient */
298 size
= lstrlenW(name
->name
) + 1;
300 if ((dwDisplayFlags
& ASM_DISPLAYF_VERSION
) && (name
->versize
> 0))
302 static const WCHAR spec
[] = {'%','d',0};
303 static const WCHAR period
[] = {'.',0};
306 wsprintfW(verstr
, spec
, name
->version
[0]);
308 for (i
= 1; i
< name
->versize
; i
++)
311 wsprintfW(value
, spec
, name
->version
[i
]);
313 lstrcatW(verstr
, period
);
314 lstrcatW(verstr
, value
);
317 size
+= lstrlenW(separator
) + lstrlenW(version
) + lstrlenW(equals
) + lstrlenW(verstr
);
320 if ((dwDisplayFlags
& ASM_DISPLAYF_CULTURE
) && (name
->culture
))
322 static const WCHAR neutral
[] = {'n','e','u','t','r','a','l', 0};
324 cultureval
= (lstrlenW(name
->culture
) == 2) ? name
->culture
: (LPWSTR
) neutral
;
325 size
+= lstrlenW(separator
) + lstrlenW(culture
) + lstrlenW(equals
) + lstrlenW(cultureval
);
328 if ((dwDisplayFlags
& ASM_DISPLAYF_PUBLIC_KEY_TOKEN
) && (name
->haspubkey
))
329 size
+= lstrlenW(separator
) + lstrlenW(pubkey
) + lstrlenW(equals
) + CHARS_PER_PUBKEY
;
331 if ((dwDisplayFlags
& ASM_DISPLAYF_PROCESSORARCHITECTURE
) && (name
->procarch
))
332 size
+= lstrlenW(separator
) + lstrlenW(procarch
) + lstrlenW(equals
) + lstrlenW(name
->procarch
);
334 if (size
> *pccDisplayName
)
336 *pccDisplayName
= size
;
337 return E_NOT_SUFFICIENT_BUFFER
;
340 /* Construct the string */
341 lstrcpyW(szDisplayName
, name
->name
);
343 if ((dwDisplayFlags
& ASM_DISPLAYF_VERSION
) && (name
->versize
> 0))
345 lstrcatW(szDisplayName
, separator
);
347 lstrcatW(szDisplayName
, version
);
348 lstrcatW(szDisplayName
, equals
);
349 lstrcatW(szDisplayName
, verstr
);
352 if ((dwDisplayFlags
& ASM_DISPLAYF_CULTURE
) && (name
->culture
))
354 lstrcatW(szDisplayName
, separator
);
356 lstrcatW(szDisplayName
, culture
);
357 lstrcatW(szDisplayName
, equals
);
358 lstrcatW(szDisplayName
, cultureval
);
361 if ((dwDisplayFlags
& ASM_DISPLAYF_PUBLIC_KEY_TOKEN
) && (name
->haspubkey
))
363 WCHAR pkt
[CHARS_PER_PUBKEY
+ 1];
364 static const WCHAR spec
[] = {'%','0','2','x','%','0','2','x','%','0','2','x',
365 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',0};
367 lstrcatW(szDisplayName
, separator
);
369 lstrcatW(szDisplayName
, pubkey
);
370 lstrcatW(szDisplayName
, equals
);
372 wsprintfW(pkt
, spec
, name
->pubkey
[0], name
->pubkey
[1], name
->pubkey
[2],
373 name
->pubkey
[3], name
->pubkey
[4], name
->pubkey
[5], name
->pubkey
[6],
376 lstrcatW(szDisplayName
, pkt
);
379 if ((dwDisplayFlags
& ASM_DISPLAYF_PROCESSORARCHITECTURE
) && (name
->procarch
))
381 lstrcatW(szDisplayName
, separator
);
383 lstrcatW(szDisplayName
, procarch
);
384 lstrcatW(szDisplayName
, equals
);
385 lstrcatW(szDisplayName
, name
->procarch
);
388 *pccDisplayName
= size
;
392 static HRESULT WINAPI
IAssemblyNameImpl_Reserved(IAssemblyName
*iface
,
394 IUnknown
*pUnkReserved1
,
395 IUnknown
*pUnkReserved2
,
396 LPCOLESTR szReserved
,
402 TRACE("(%p, %s, %p, %p, %s, %s, %p, %d, %p)\n", iface
,
403 debugstr_guid(refIID
), pUnkReserved1
, pUnkReserved2
,
404 debugstr_w(szReserved
), wine_dbgstr_longlong(llReserved
),
405 pvReserved
, cbReserved
, ppReserved
);
410 static HRESULT WINAPI
IAssemblyNameImpl_GetName(IAssemblyName
*iface
,
414 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
417 TRACE("(%p, %p, %p)\n", iface
, lpcwBuffer
, pwzName
);
420 len
= strlenW(name
->name
) + 1;
424 if (*lpcwBuffer
< len
)
427 return E_NOT_SUFFICIENT_BUFFER
;
429 if (!name
->name
) lpcwBuffer
[0] = 0;
430 else strcpyW(pwzName
, name
->name
);
436 static HRESULT WINAPI
IAssemblyNameImpl_GetVersion(IAssemblyName
*iface
,
437 LPDWORD pdwVersionHi
,
438 LPDWORD pdwVersionLow
)
440 IAssemblyNameImpl
*name
= impl_from_IAssemblyName(iface
);
442 TRACE("(%p, %p, %p)\n", iface
, pdwVersionHi
, pdwVersionLow
);
447 if (name
->versize
!= 4)
448 return FUSION_E_INVALID_NAME
;
450 *pdwVersionHi
= (name
->version
[0] << 16) + name
->version
[1];
451 *pdwVersionLow
= (name
->version
[2] << 16) + name
->version
[3];
456 static HRESULT WINAPI
IAssemblyNameImpl_IsEqual(IAssemblyName
*iface
,
457 IAssemblyName
*pName
,
460 IAssemblyNameImpl
*name1
= impl_from_IAssemblyName(iface
);
461 IAssemblyNameImpl
*name2
= impl_from_IAssemblyName(pName
);
463 TRACE("(%p, %p, 0x%08x)\n", iface
, pName
, flags
);
465 if (!pName
) return S_FALSE
;
466 if (flags
& ~ASM_CMPF_IL_ALL
) FIXME("unsupported flags\n");
468 if ((flags
& ASM_CMPF_NAME
) && strcmpW(name1
->name
, name2
->name
)) return S_FALSE
;
469 if (name1
->versize
&& name2
->versize
)
471 if ((flags
& ASM_CMPF_MAJOR_VERSION
) &&
472 name1
->version
[0] != name2
->version
[0]) return S_FALSE
;
473 if ((flags
& ASM_CMPF_MINOR_VERSION
) &&
474 name1
->version
[1] != name2
->version
[1]) return S_FALSE
;
475 if ((flags
& ASM_CMPF_BUILD_NUMBER
) &&
476 name1
->version
[2] != name2
->version
[2]) return S_FALSE
;
477 if ((flags
& ASM_CMPF_REVISION_NUMBER
) &&
478 name1
->version
[3] != name2
->version
[3]) return S_FALSE
;
480 if ((flags
& ASM_CMPF_PUBLIC_KEY_TOKEN
) &&
481 name1
->haspubkey
&& name2
->haspubkey
&&
482 memcmp(name1
->pubkey
, name2
->pubkey
, sizeof(name1
->pubkey
))) return S_FALSE
;
484 if ((flags
& ASM_CMPF_CULTURE
) &&
485 name1
->culture
&& name2
->culture
&&
486 strcmpW(name1
->culture
, name2
->culture
)) return S_FALSE
;
491 static HRESULT WINAPI
IAssemblyNameImpl_Clone(IAssemblyName
*iface
,
492 IAssemblyName
**pName
)
494 FIXME("(%p, %p) stub!\n", iface
, pName
);
498 static const IAssemblyNameVtbl AssemblyNameVtbl
= {
499 IAssemblyNameImpl_QueryInterface
,
500 IAssemblyNameImpl_AddRef
,
501 IAssemblyNameImpl_Release
,
502 IAssemblyNameImpl_SetProperty
,
503 IAssemblyNameImpl_GetProperty
,
504 IAssemblyNameImpl_Finalize
,
505 IAssemblyNameImpl_GetDisplayName
,
506 IAssemblyNameImpl_Reserved
,
507 IAssemblyNameImpl_GetName
,
508 IAssemblyNameImpl_GetVersion
,
509 IAssemblyNameImpl_IsEqual
,
510 IAssemblyNameImpl_Clone
513 /* Internal methods */
514 static inline IAssemblyNameImpl
*unsafe_impl_from_IAssemblyName(IAssemblyName
*iface
)
516 assert(iface
->lpVtbl
== &AssemblyNameVtbl
);
518 return impl_from_IAssemblyName(iface
);
521 HRESULT
IAssemblyName_SetPath(IAssemblyName
*iface
, LPCWSTR path
)
523 IAssemblyNameImpl
*name
= unsafe_impl_from_IAssemblyName(iface
);
525 name
->path
= strdupW(path
);
527 return E_OUTOFMEMORY
;
532 HRESULT
IAssemblyName_GetPath(IAssemblyName
*iface
, LPWSTR buf
, ULONG
*len
)
534 ULONG buffer_size
= *len
;
535 IAssemblyNameImpl
*name
= unsafe_impl_from_IAssemblyName(iface
);
543 *len
= lstrlenW(name
->path
) + 1;
545 if (*len
<= buffer_size
)
546 lstrcpyW(buf
, name
->path
);
548 return E_NOT_SUFFICIENT_BUFFER
;
553 static HRESULT
parse_version(IAssemblyNameImpl
*name
, LPWSTR version
)
558 for (i
= 0, beg
= version
; i
< 4; i
++)
563 end
= strchrW(beg
, '.');
565 if (end
) *end
= '\0';
566 name
->version
[i
] = atolW(beg
);
578 static HRESULT
parse_culture(IAssemblyNameImpl
*name
, LPCWSTR culture
)
580 static const WCHAR empty
[] = {0};
582 if (lstrlenW(culture
) == 2)
583 name
->culture
= strdupW(culture
);
585 name
->culture
= strdupW(empty
);
590 static BOOL
is_hex(WCHAR c
)
592 return ((c
>= 'a' && c
<= 'f') ||
593 (c
>= 'A' && c
<= 'F') ||
594 (c
>= '0' && c
<= '9'));
597 static BYTE
hextobyte(WCHAR c
)
599 if(c
>= '0' && c
<= '9')
601 if(c
>= 'A' && c
<= 'F')
603 if(c
>= 'a' && c
<= 'f')
608 static HRESULT
parse_pubkey(IAssemblyNameImpl
*name
, LPCWSTR pubkey
)
612 static const WCHAR nullstr
[] = {'n','u','l','l',0};
614 if(lstrcmpiW(pubkey
, nullstr
) == 0)
615 return FUSION_E_PRIVATE_ASM_DISALLOWED
;
617 if (lstrlenW(pubkey
) < CHARS_PER_PUBKEY
)
618 return FUSION_E_INVALID_NAME
;
620 for (i
= 0; i
< CHARS_PER_PUBKEY
; i
++)
621 if (!is_hex(pubkey
[i
]))
622 return FUSION_E_INVALID_NAME
;
624 name
->haspubkey
= TRUE
;
626 for (i
= 0; i
< CHARS_PER_PUBKEY
; i
+= 2)
628 val
= (hextobyte(pubkey
[i
]) << 4) + hextobyte(pubkey
[i
+ 1]);
629 name
->pubkey
[i
/ 2] = val
;
635 static HRESULT
parse_procarch(IAssemblyNameImpl
*name
, LPCWSTR procarch
)
637 static const WCHAR msilW
[] = {'m','s','i','l',0};
638 static const WCHAR x86W
[] = {'x','8','6',0};
639 static const WCHAR ia64W
[] = {'i','a','6','4',0};
640 static const WCHAR amd64W
[] = {'a','m','d','6','4',0};
642 if (!lstrcmpiW(procarch
, msilW
))
643 name
->pekind
= peMSIL
;
644 else if (!lstrcmpiW(procarch
, x86W
))
645 name
->pekind
= peI386
;
646 else if (!lstrcmpiW(procarch
, ia64W
))
647 name
->pekind
= peIA64
;
648 else if (!lstrcmpiW(procarch
, amd64W
))
649 name
->pekind
= peAMD64
;
652 ERR("unrecognized architecture: %s\n", wine_dbgstr_w(procarch
));
653 return FUSION_E_INVALID_NAME
;
659 static WCHAR
*parse_value( const WCHAR
*str
, unsigned int len
)
662 const WCHAR
*p
= str
;
666 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
672 while (*p
&& *p
!= '\"') ret
[i
++] = *p
++;
673 if ((quoted
&& *p
!= '\"') || (!quoted
&& *p
== '\"'))
675 HeapFree( GetProcessHeap(), 0, ret
);
682 static HRESULT
parse_display_name(IAssemblyNameImpl
*name
, LPCWSTR szAssemblyName
)
684 LPWSTR str
, save
, ptr
, ptr2
, value
;
691 name
->displayname
= strdupW(szAssemblyName
);
692 if (!name
->displayname
)
693 return E_OUTOFMEMORY
;
695 str
= strdupW(szAssemblyName
);
703 ptr
= strchrW(str
, ',');
704 if (ptr
) *ptr
= '\0';
706 /* no ',' but ' ' only */
707 if( !ptr
&& strchrW(str
, ' ') )
709 hr
= FUSION_E_INVALID_NAME
;
713 name
->name
= strdupW(str
);
726 ptr
= strchrW(str
, '=');
729 hr
= FUSION_E_INVALID_NAME
;
736 hr
= FUSION_E_INVALID_NAME
;
740 if (!(ptr2
= strchrW(ptr
, ',')))
742 if (!(ptr2
= strchrW(ptr
, '\0')))
744 hr
= FUSION_E_INVALID_NAME
;
752 if (!(value
= parse_value( ptr
, ptr2
- ptr
)))
754 hr
= FUSION_E_INVALID_NAME
;
757 while (*str
== ' ') str
++;
759 if (!lstrcmpiW(str
, version
))
760 hr
= parse_version( name
, value
);
761 else if (!lstrcmpiW(str
, culture
))
762 hr
= parse_culture( name
, value
);
763 else if (!lstrcmpiW(str
, pubkey
))
764 hr
= parse_pubkey( name
, value
);
765 else if (!lstrcmpiW(str
, procarch
))
767 name
->procarch
= value
;
770 hr
= parse_procarch( name
, name
->procarch
);
772 HeapFree( GetProcessHeap(), 0, value
);
781 HeapFree(GetProcessHeap(), 0, save
);
784 HeapFree(GetProcessHeap(), 0, name
->displayname
);
785 HeapFree(GetProcessHeap(), 0, name
->name
);
786 HeapFree(GetProcessHeap(), 0, name
->culture
);
787 HeapFree(GetProcessHeap(), 0, name
->procarch
);
792 /******************************************************************
793 * CreateAssemblyNameObject (FUSION.@)
795 HRESULT WINAPI
CreateAssemblyNameObject(IAssemblyName
**ppAssemblyNameObj
,
796 LPCWSTR szAssemblyName
, DWORD dwFlags
,
799 IAssemblyNameImpl
*name
;
802 TRACE("(%p, %s, %08x, %p)\n", ppAssemblyNameObj
,
803 debugstr_w(szAssemblyName
), dwFlags
, pvReserved
);
805 if (!ppAssemblyNameObj
)
808 if ((dwFlags
& CANOF_PARSE_DISPLAY_NAME
) &&
809 (!szAssemblyName
|| !*szAssemblyName
))
812 name
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAssemblyNameImpl
));
814 return E_OUTOFMEMORY
;
816 name
->IAssemblyName_iface
.lpVtbl
= &AssemblyNameVtbl
;
819 hr
= parse_display_name(name
, szAssemblyName
);
822 HeapFree(GetProcessHeap(), 0, name
);
826 *ppAssemblyNameObj
= &name
->IAssemblyName_iface
;