Kill the MOONLIGHT define in corlib.
[mono.git] / mcs / class / corlib / Mono.Security.X509 / X509CertificateCollection.cs
1 //
2 // Based on System.Security.Cryptography.X509Certificates.X509CertificateCollection
3 //      in System assembly
4 //
5 // Authors:
6 //      Lawrence Pit (loz@cable.a2000.nl)
7 //      Sebastien Pouliot <sebastien@ximian.com>
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35
36 namespace Mono.Security.X509 {
37
38         [Serializable]
39 #if INSIDE_CORLIB
40         internal
41 #else
42         public 
43 #endif
44         class X509CertificateCollection : CollectionBase, IEnumerable {
45                 
46                 public X509CertificateCollection () 
47                 {
48                 }
49                 
50                 public X509CertificateCollection (X509Certificate [] value) 
51                 {
52                         AddRange (value);
53                 }
54                 
55                 public X509CertificateCollection (X509CertificateCollection value)
56                 {
57                         AddRange (value);
58                 }
59                 
60                 // Properties
61                 
62                 public X509Certificate this [int index] {
63                         get { return (X509Certificate) InnerList [index]; }
64                         set { InnerList [index] = value; }
65                 }
66                 
67                 // Methods
68
69                 public int Add (X509Certificate value)
70                 {
71                         if (value == null)
72                                 throw new ArgumentNullException ("value");
73                         
74                         return InnerList.Add (value);
75                 }
76                 
77                 public void AddRange (X509Certificate [] value) 
78                 {
79                         if (value == null)
80                                 throw new ArgumentNullException ("value");
81
82                         for (int i = 0; i < value.Length; i++) 
83                                 InnerList.Add (value [i]);
84                 }
85                 
86                 public void AddRange (X509CertificateCollection value)
87                 {
88                         if (value == null)
89                                 throw new ArgumentNullException ("value");
90
91                         for (int i = 0; i < value.InnerList.Count; i++) 
92                                 InnerList.Add (value [i]);
93                 }
94                 
95                 public bool Contains (X509Certificate value) 
96                 {
97                         return (IndexOf (value) != -1);
98                 }
99
100                 public void CopyTo (X509Certificate[] array, int index)
101                 {
102                         InnerList.CopyTo (array, index);
103                 }
104                 
105                 public new X509CertificateEnumerator GetEnumerator ()
106                 {
107                         return new X509CertificateEnumerator (this);
108                 }
109                 
110                 IEnumerator IEnumerable.GetEnumerator ()
111                 {
112                         return InnerList.GetEnumerator ();
113                 }
114                 
115                 public override int GetHashCode () 
116                 {
117                         return InnerList.GetHashCode ();
118                 }
119                 
120                 public int IndexOf (X509Certificate value)
121                 {
122                         if (value == null)
123                                 throw new ArgumentNullException ("value");
124
125                         byte[] hash = value.Hash;
126                         for (int i=0; i < InnerList.Count; i++) {
127                                 X509Certificate x509 = (X509Certificate) InnerList [i];
128                                 if (Compare (x509.Hash, hash))
129                                         return i;
130                         }
131                         return -1;
132                 }
133                 
134                 public void Insert (int index, X509Certificate value)
135                 {
136                         InnerList.Insert (index, value);
137                 }
138                 
139                 public void Remove (X509Certificate value)
140                 {
141                         InnerList.Remove (value);
142                 }
143
144                 // private stuff
145
146                 private bool Compare (byte[] array1, byte[] array2) 
147                 {
148                         if ((array1 == null) && (array2 == null))
149                                 return true;
150                         if ((array1 == null) || (array2 == null))
151                                 return false;
152                         if (array1.Length != array2.Length)
153                                 return false;
154                         for (int i=0; i < array1.Length; i++) {
155                                 if (array1 [i] != array2 [i])
156                                         return false;
157                         }
158                         return true;
159                 }
160
161                 // Inner Class
162                 
163                 public class X509CertificateEnumerator : IEnumerator {
164
165                         private IEnumerator enumerator;
166
167                         // Constructors
168                         
169                         public X509CertificateEnumerator (X509CertificateCollection mappings)
170                         {
171                                 enumerator = ((IEnumerable) mappings).GetEnumerator ();
172                         }
173
174                         // Properties
175                         
176                         public X509Certificate Current {
177                                 get { return (X509Certificate) enumerator.Current; }
178                         }
179                         
180                         object IEnumerator.Current {
181                                 get { return enumerator.Current; }
182                         }
183
184                         // Methods
185                         
186                         bool IEnumerator.MoveNext ()
187                         {
188                                 return enumerator.MoveNext ();
189                         }
190                         
191                         void IEnumerator.Reset () 
192                         {
193                                 enumerator.Reset ();
194                         }
195                         
196                         public bool MoveNext () 
197                         {
198                                 return enumerator.MoveNext ();
199                         }
200                         
201                         public void Reset ()
202                         {
203                                 enumerator.Reset ();
204                         }
205                 }               
206         }
207 }