[corlib] The hashcode of an empty struct must be constant. Fixes #17577
[mono.git] / mcs / class / corlib / System.Text / ASCIIEncoding.cs
1 /*
2  * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
3  *
4  * Copyright (c) 2001  Southern Storm Software, Pty Ltd
5  * Copyright (C) 2003 Novell, Inc.
6  * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 namespace System.Text
28 {
29
30 using System;
31 using System.Runtime.InteropServices;
32
33 [Serializable]
34 [ComVisible (true)]
35 [MonoLimitation ("Serialization format not compatible with .NET")]
36 public class ASCIIEncoding : Encoding
37 {
38         // Magic number used by Windows for "ASCII".
39         internal const int ASCII_CODE_PAGE = 20127;
40
41         // Constructor.
42         public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
43                 body_name = header_name = web_name= "us-ascii";
44                 encoding_name = "US-ASCII";
45                 is_mail_news_display = true;
46                 is_mail_news_save = true;
47         }
48
49         [ComVisible (false)]
50         public override bool IsSingleByte {
51                 get { return true; }
52         }
53
54         // Get the number of bytes needed to encode a character buffer.
55         public override int GetByteCount (char[] chars, int index, int count)
56         {
57                 if (chars == null) {
58                         throw new ArgumentNullException ("chars");
59                 }
60                 if (index < 0 || index > chars.Length) {
61                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
62                 }
63                 if (count < 0 || count > (chars.Length - index)) {
64                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
65                 }
66                 return count;
67         }
68
69         // Convenience wrappers for "GetByteCount".
70         public override int GetByteCount (String chars)
71         {
72                 if (chars == null) {
73                         throw new ArgumentNullException ("chars");
74                 }
75                 return chars.Length;
76         }
77
78         // Get the bytes that result from encoding a character buffer.
79         public override int GetBytes (char[] chars, int charIndex, int charCount,
80                                       byte[] bytes, int byteIndex)
81         {
82                 EncoderFallbackBuffer buffer = null;
83                 char [] fallback_chars = null;
84                 
85                 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
86                                  ref buffer, ref fallback_chars);
87         }
88
89         int GetBytes (char[] chars, int charIndex, int charCount,
90                       byte[] bytes, int byteIndex,
91                       ref EncoderFallbackBuffer buffer,
92                       ref char [] fallback_chars)
93         {
94                 if (chars == null)
95                         throw new ArgumentNullException ("chars");
96
97                 unsafe {
98                         fixed (char *cptr = chars) {
99                                 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
100                         }
101                 }
102         }
103
104         // Convenience wrappers for "GetBytes".
105         public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
106         {
107                 EncoderFallbackBuffer buffer = null;
108                 char [] fallback_chars = null;
109                 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
110                         ref buffer, ref fallback_chars);
111         }
112
113         int GetBytes (String chars, int charIndex, int charCount,
114                       byte[] bytes, int byteIndex,
115                       ref EncoderFallbackBuffer buffer,
116                       ref char [] fallback_chars)
117         {
118                 if (chars == null)
119                         throw new ArgumentNullException ("chars");
120
121                 unsafe {
122                         fixed (char *cptr = chars) {
123                                 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
124                         }
125                 }
126         }
127
128         unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
129                       byte[] bytes, int byteIndex,
130                       ref EncoderFallbackBuffer buffer,
131                       ref char [] fallback_chars)
132         {
133                 if (bytes == null)
134                         throw new ArgumentNullException ("bytes");
135                 if (charIndex < 0 || charIndex > charLength)
136                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
137                 if (charCount < 0 || charCount > (charLength - charIndex))
138                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
139                 if (byteIndex < 0 || byteIndex > bytes.Length)
140                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
141                 if ((bytes.Length - byteIndex) < charCount)
142                         throw new ArgumentException (_("Arg_InsufficientSpace"));
143
144                 int count = charCount;
145                 char ch;
146                 while (count-- > 0) {
147                         ch = chars [charIndex++];
148                         if (ch < (char)0x80) {
149                                 bytes [byteIndex++] = (byte)ch;
150                         } else {
151                                 if (buffer == null)
152                                         buffer = EncoderFallback.CreateFallbackBuffer ();
153                                 if (Char.IsSurrogate (ch) && count > 1 &&
154                                     Char.IsSurrogate (chars [charIndex]))
155                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
156                                 else
157                                         buffer.Fallback (ch, charIndex - 1);
158                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
159                                         fallback_chars = new char [buffer.Remaining];
160                                 for (int i = 0; i < fallback_chars.Length; i++)
161                                         fallback_chars [i] = buffer.GetNextChar ();
162                                 byteIndex += GetBytes (fallback_chars, 0, 
163                                         fallback_chars.Length, bytes, byteIndex,
164                                         ref buffer, ref fallback_chars);
165                         }
166                 }
167                 return charCount;
168         }
169
170         // Get the number of characters needed to decode a byte buffer.
171         public override int GetCharCount (byte[] bytes, int index, int count)
172         {
173                 if (bytes == null) {
174                         throw new ArgumentNullException ("bytes");
175                 }
176                 if (index < 0 || index > bytes.Length) {
177                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
178                 }
179                 if (count < 0 || count > (bytes.Length - index)) {
180                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
181                 }
182                 return count;
183         }
184
185         // Get the characters that result from decoding a byte buffer.
186         public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
187         {
188                 DecoderFallbackBuffer buffer = null;
189                 return GetChars (bytes, byteIndex, byteCount, chars,
190                         charIndex, ref buffer);
191         }
192
193         int GetChars (byte[] bytes, int byteIndex, int byteCount,
194                       char[] chars, int charIndex,
195                       ref DecoderFallbackBuffer buffer)
196         {
197                 if (bytes == null)
198                         throw new ArgumentNullException ("bytes");
199                 if (chars == null) 
200                         throw new ArgumentNullException ("chars");
201                 if (byteIndex < 0 || byteIndex > bytes.Length) 
202                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
203                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 
204                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
205                 if (charIndex < 0 || charIndex > chars.Length) 
206                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
207
208                 if ((chars.Length - charIndex) < byteCount) 
209                         throw new ArgumentException (_("Arg_InsufficientSpace"));
210
211                 int count = byteCount;
212                 while (count-- > 0) {
213                         char c = (char) bytes [byteIndex++];
214                         if (c < '\x80')
215                                 chars [charIndex++] = c;
216                         else {
217                                 if (buffer == null)
218                                         buffer = DecoderFallback.CreateFallbackBuffer ();
219                                 var thisByte = new byte[] { bytes [byteIndex-1] };
220                                 buffer.Fallback (thisByte, 0);
221                                 while (buffer.Remaining > 0) {
222                                         if (charIndex < chars.Length) {
223                                                 chars [charIndex++] = buffer.GetNextChar ();
224                                                 continue;
225                                         }
226                                         throw new ArgumentException (
227                                                         "The output char buffer is too small to contain the " +
228                                                         "decoded characters.");
229                                 }
230                         }
231                 }
232                 return byteCount;
233         }
234
235         // Get the maximum number of bytes needed to encode a
236         // specified number of characters.
237         public override int GetMaxByteCount (int charCount)
238         {
239                 if (charCount < 0) {
240                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
241                 }
242                 return charCount;
243         }
244
245         // Get the maximum number of characters needed to decode a
246         // specified number of bytes.
247         public override int GetMaxCharCount (int byteCount)
248         {
249                 if (byteCount < 0) {
250                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
251                 }
252                 return byteCount;
253         }
254
255         // Decode a buffer of bytes into a string.
256         public override String GetString (byte[] bytes, int byteIndex, int byteCount)
257         {
258                 if (bytes == null) {
259                         throw new ArgumentNullException ("bytes");
260                 }
261                 if (byteIndex < 0 || byteIndex > bytes.Length) {
262                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
263                 }
264                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
265                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
266                 }
267                 if (byteCount == 0)
268                         return String.Empty;
269                 
270                 unsafe {
271                         fixed (byte* bytePtr = bytes) {
272                                 string s = string.InternalAllocateStr (byteCount);
273
274                                 fixed (char* charPtr = s) {
275                                         byte* currByte = bytePtr + byteIndex;
276                                         byte* lastByte = currByte + byteCount;
277                                         char* currChar = charPtr;
278
279                                         while (currByte < lastByte) {
280                                                 byte b = currByte++ [0];
281                                                 currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
282                                         }
283                                 }
284
285                                 return s;
286                         }
287                 }
288         }
289
290         [CLSCompliantAttribute (false)]
291         [ComVisible (false)]
292         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
293         {
294                 if (chars == null)
295                         throw new ArgumentNullException ("chars");
296                 if (bytes == null)
297                         throw new ArgumentNullException ("bytes");
298                 if (charCount < 0)
299                         throw new ArgumentOutOfRangeException ("charCount");
300                 if (byteCount < 0)
301                         throw new ArgumentOutOfRangeException ("byteCount");
302
303                 if (byteCount < charCount)
304                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
305
306                 for (int i = 0; i < charCount; i++){
307                         char c = chars [i];
308                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
309                 }
310                 return charCount;
311         }
312
313         [CLSCompliantAttribute(false)]
314         [ComVisible (false)]
315         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
316         {
317                 if (bytes == null)
318                         throw new ArgumentNullException ("bytes");
319                 if (chars == null) 
320                         throw new ArgumentNullException ("chars");
321                 if (charCount < 0)
322                         throw new ArgumentOutOfRangeException ("charCount");
323                 if (byteCount < 0)
324                         throw new ArgumentOutOfRangeException ("byteCount");
325                 if (charCount < byteCount)
326                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
327
328                 for (int i = 0; i < byteCount; i++){
329                         byte b = bytes [i];
330                         chars [i] = b > 127 ? '?' : (char) b;
331                 }
332                 return byteCount;
333                 
334         }
335
336         [CLSCompliantAttribute(false)]
337         [ComVisible (false)]
338         public unsafe override int GetCharCount (byte *bytes, int count)
339         {
340                 return count;
341         }
342
343         [CLSCompliantAttribute(false)]
344         [ComVisible (false)]
345         public unsafe override int GetByteCount (char *chars, int count)
346         {
347                 return count;
348         }
349
350         [ComVisible (false)]
351         public override Decoder GetDecoder ()
352         {
353                 return base.GetDecoder ();
354         }
355
356         [ComVisible (false)]
357         public override Encoder GetEncoder ()
358         {
359                 return base.GetEncoder ();
360         }
361 }; // class ASCIIEncoding
362
363 }; // namespace System.Text