[corlib] The hashcode of an empty struct must be constant. Fixes #17577
[mono.git] / mcs / class / corlib / System.Security.Permissions / IsolatedStorageFilePermission.cs
1 //
2 // System.Security.Permissions.IsolatedStorageFilePermission.cs
3 //
4 // Author
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.InteropServices;
31
32 namespace System.Security.Permissions {
33
34         [Serializable]
35         [ComVisible (true)]
36         public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission {
37
38                 // Constructors
39
40                 public IsolatedStorageFilePermission (PermissionState state)
41                         : base (state)
42                 {
43                 }
44
45                 // Properties
46
47                 // Methods
48
49                 public override IPermission Copy () 
50                 {
51                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
52                         p.m_userQuota = m_userQuota;
53                         p.m_machineQuota = m_machineQuota;
54                         p.m_expirationDays = m_expirationDays;
55                         p.m_permanentData = m_permanentData;
56                         p.m_allowed = m_allowed;
57                         return p;
58                 }
59
60                 public override IPermission Intersect (IPermission target) 
61                 {
62                         IsolatedStorageFilePermission isfp = Cast (target);
63                         if (isfp == null)
64                                 return null;
65                         if (IsEmpty () && isfp.IsEmpty ())
66                                 return null;
67
68                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
69                         p.m_userQuota = (m_userQuota < isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
70                         p.m_machineQuota = (m_machineQuota < isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
71                         p.m_expirationDays = (m_expirationDays < isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
72                         p.m_permanentData = (m_permanentData && isfp.m_permanentData);
73                         // UsageAllowed == Unrestricted is a special case handled by the property
74                         p.UsageAllowed = (m_allowed < isfp.m_allowed) ? m_allowed : isfp.m_allowed;
75                         return p;
76                 }
77
78                 public override bool IsSubsetOf (IPermission target) 
79                 {
80                         IsolatedStorageFilePermission isfp = Cast (target);
81                         if (isfp == null)
82                                 return IsEmpty ();
83                         if (isfp.IsUnrestricted ())
84                                 return true;
85
86                         if (m_userQuota > isfp.m_userQuota)
87                                 return false;
88                         if (m_machineQuota > isfp.m_machineQuota)
89                                 return false;
90                         if (m_expirationDays > isfp.m_expirationDays)
91                                 return false;
92                         if (m_permanentData != isfp.m_permanentData)
93                                 return false;
94                         if (m_allowed > isfp.m_allowed)
95                                 return false;
96                         return true;
97                 }
98
99                 public override IPermission Union (IPermission target)
100                 {
101                         IsolatedStorageFilePermission isfp = Cast (target);
102                         if (isfp == null)
103                                 return Copy ();
104
105                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
106                         p.m_userQuota = (m_userQuota > isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
107                         p.m_machineQuota = (m_machineQuota > isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
108                         p.m_expirationDays = (m_expirationDays > isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
109                         p.m_permanentData = (m_permanentData || isfp.m_permanentData);
110                         // UsageAllowed == Unrestricted is a special case handled by the property
111                         p.UsageAllowed = (m_allowed > isfp.m_allowed) ? m_allowed : isfp.m_allowed;
112                         return p;
113                 }
114
115                 [MonoTODO ("(2.0) new override - something must have been added ???")]
116                 [ComVisible (false)]
117                 public override SecurityElement ToXml ()
118                 {
119                         return base.ToXml ();
120                 }
121
122                 // IBuiltInPermission
123                 int IBuiltInPermission.GetTokenIndex ()
124                 {
125                         return (int) BuiltInToken.IsolatedStorageFile;
126                 }
127
128                 // helpers
129
130                 private IsolatedStorageFilePermission Cast (IPermission target)
131                 {
132                         if (target == null)
133                                 return null;
134
135                         IsolatedStorageFilePermission isfp = (target as IsolatedStorageFilePermission);
136                         if (isfp == null) {
137                                 ThrowInvalidPermission (target, typeof (IsolatedStorageFilePermission));
138                         }
139
140                         return isfp;
141                 }
142         }
143 }