2009-11-02 Miguel de Icaza <miguel@novell.com>
[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                 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
83                 EncoderFallbackBuffer buffer = null;
84                 char [] fallback_chars = null;
85                 
86                 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
87                                  ref buffer, ref fallback_chars);
88         }
89
90         int GetBytes (char[] chars, int charIndex, int charCount,
91                       byte[] bytes, int byteIndex,
92                       ref EncoderFallbackBuffer buffer,
93                       ref char [] fallback_chars)
94         {
95                 if (chars == null) 
96                         throw new ArgumentNullException ("chars");
97
98                 if (bytes == null) 
99                         throw new ArgumentNullException ("bytes");
100
101                 if (charIndex < 0 || charIndex > chars.Length) 
102                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
103
104                 if (charCount < 0 || charCount > (chars.Length - charIndex)) 
105                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
106
107                 if (byteIndex < 0 || byteIndex > bytes.Length) 
108                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
109
110                 if ((bytes.Length - byteIndex) < charCount) 
111                         throw new ArgumentException (_("Arg_InsufficientSpace"));
112
113                 int count = charCount;
114                 char ch;
115                 while (count-- > 0) {
116                         ch = chars [charIndex++];
117                         if (ch < (char)0x80) {
118                                 bytes [byteIndex++] = (byte)ch;
119                         } else {
120                                 if (buffer == null)
121                                         buffer = EncoderFallback.CreateFallbackBuffer ();
122                                 if (Char.IsSurrogate (ch) && count > 1 &&
123                                     Char.IsSurrogate (chars [charIndex]))
124                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
125                                 else
126                                         buffer.Fallback (ch, charIndex - 1);
127                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
128                                         fallback_chars = new char [buffer.Remaining];
129                                 for (int i = 0; i < fallback_chars.Length; i++)
130                                         fallback_chars [i] = buffer.GetNextChar ();
131                                 byteIndex += GetBytes (fallback_chars, 0, 
132                                         fallback_chars.Length, bytes, byteIndex,
133                                         ref buffer, ref fallback_chars);
134                         }
135                 }
136                 return charCount;
137         }
138
139         // Convenience wrappers for "GetBytes".
140         public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
141         {
142 // I know this #if is ugly, but I think it is the simplest switch.
143                 EncoderFallbackBuffer buffer = null;
144                 char [] fallback_chars = null;
145                 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
146                         ref buffer, ref fallback_chars);
147         }
148
149         int GetBytes (String chars, int charIndex, int charCount,
150                       byte[] bytes, int byteIndex,
151                       ref EncoderFallbackBuffer buffer,
152                       ref char [] fallback_chars)
153         {
154                 if (chars == null) {
155                         throw new ArgumentNullException ("chars");
156                 }
157                 if (bytes == null) {
158                         throw new ArgumentNullException ("bytes");
159                 }
160                 if (charIndex < 0 || charIndex > chars.Length) {
161                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
162                 }
163                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
164                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
165                 }
166                 if (byteIndex < 0 || byteIndex > bytes.Length) {
167                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
168                 }
169                 if ((bytes.Length - byteIndex) < charCount) {
170                         throw new ArgumentException (_("Arg_InsufficientSpace"));
171                 }
172                 int count = charCount;
173                 char ch;
174                 while (count-- > 0) {
175                         ch = chars [charIndex++];
176                         if (ch < (char)0x80) {
177                                 bytes [byteIndex++] = (byte)ch;
178                         } else {
179                                 if (buffer == null)
180                                         buffer = EncoderFallback.CreateFallbackBuffer ();
181                                 if (Char.IsSurrogate (ch) && count > 1 &&
182                                     Char.IsSurrogate (chars [charIndex]))
183                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
184                                 else
185                                         buffer.Fallback (ch, charIndex - 1);
186                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
187                                         fallback_chars = new char [buffer.Remaining];
188                                 for (int i = 0; i < fallback_chars.Length; i++)
189                                         fallback_chars [i] = buffer.GetNextChar ();
190                                 byteIndex += GetBytes (fallback_chars, 0, 
191                                         fallback_chars.Length, bytes, byteIndex,
192                                         ref buffer, ref fallback_chars);
193                         }
194                 }
195                 return charCount;
196         }
197
198         // Get the number of characters needed to decode a byte buffer.
199         public override int GetCharCount (byte[] bytes, int index, int count)
200         {
201                 if (bytes == null) {
202                         throw new ArgumentNullException ("bytes");
203                 }
204                 if (index < 0 || index > bytes.Length) {
205                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
206                 }
207                 if (count < 0 || count > (bytes.Length - index)) {
208                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
209                 }
210                 return count;
211         }
212
213         // Get the characters that result from decoding a byte buffer.
214         public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
215         {
216 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
217                 DecoderFallbackBuffer buffer = null;
218                 return GetChars (bytes, byteIndex, byteCount, chars,
219                         charIndex, ref buffer);
220         }
221
222         int GetChars (byte[] bytes, int byteIndex, int byteCount,
223                       char[] chars, int charIndex,
224                       ref DecoderFallbackBuffer buffer)
225         {
226                 if (bytes == null)
227                         throw new ArgumentNullException ("bytes");
228                 if (chars == null) 
229                         throw new ArgumentNullException ("chars");
230                 if (byteIndex < 0 || byteIndex > bytes.Length) 
231                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
232                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 
233                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
234                 if (charIndex < 0 || charIndex > chars.Length) 
235                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
236
237                 if ((chars.Length - charIndex) < byteCount) 
238                         throw new ArgumentException (_("Arg_InsufficientSpace"));
239
240                 int count = byteCount;
241                 while (count-- > 0) {
242                         char c = (char) bytes [byteIndex++];
243                         if (c < '\x80')
244                                 chars [charIndex++] = c;
245                         else {
246                                 if (buffer == null)
247                                         buffer = DecoderFallback.CreateFallbackBuffer ();
248                                 buffer.Fallback (bytes, byteIndex);
249                                 while (buffer.Remaining > 0)
250                                         chars [charIndex++] = buffer.GetNextChar ();
251                         }
252                 }
253                 return byteCount;
254         }
255
256         // Get the maximum number of bytes needed to encode a
257         // specified number of characters.
258         public override int GetMaxByteCount (int charCount)
259         {
260                 if (charCount < 0) {
261                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
262                 }
263                 return charCount;
264         }
265
266         // Get the maximum number of characters needed to decode a
267         // specified number of bytes.
268         public override int GetMaxCharCount (int byteCount)
269         {
270                 if (byteCount < 0) {
271                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
272                 }
273                 return byteCount;
274         }
275
276         // Decode a buffer of bytes into a string.
277         public override String GetString (byte[] bytes, int byteIndex, int byteCount)
278         {
279                 if (bytes == null) {
280                         throw new ArgumentNullException ("bytes");
281                 }
282                 if (byteIndex < 0 || byteIndex > bytes.Length) {
283                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
284                 }
285                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
286                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
287                 }
288                 if (byteCount == 0)
289                         return String.Empty;
290                 
291                 unsafe {
292                         fixed (byte* bytePtr = bytes) {
293                                 string s = string.InternalAllocateStr (byteCount);
294
295                                 fixed (char* charPtr = s) {
296                                         byte* currByte = bytePtr + byteIndex;
297                                         byte* lastByte = currByte + byteCount;
298                                         char* currChar = charPtr;
299
300                                         while (currByte < lastByte) {
301                                                 byte b = currByte++ [0];
302                                                 currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
303                                         }
304                                 }
305
306                                 return s;
307                         }
308                 }
309         }
310
311         [CLSCompliantAttribute (false)]
312         [ComVisible (false)]
313         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
314         {
315                 if (chars == null)
316                         throw new ArgumentNullException ("chars");
317                 if (bytes == null)
318                         throw new ArgumentNullException ("bytes");
319                 if (charCount < 0)
320                         throw new ArgumentOutOfRangeException ("charCount");
321                 if (byteCount < 0)
322                         throw new ArgumentOutOfRangeException ("byteCount");
323
324                 if (byteCount < charCount)
325                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
326
327                 for (int i = 0; i < charCount; i++){
328                         char c = chars [i];
329                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
330                 }
331                 return charCount;
332         }
333
334         [CLSCompliantAttribute(false)]
335         [ComVisible (false)]
336         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
337         {
338                 if (bytes == null)
339                         throw new ArgumentNullException ("bytes");
340                 if (chars == null) 
341                         throw new ArgumentNullException ("chars");
342                 if (charCount < 0)
343                         throw new ArgumentOutOfRangeException ("charCount");
344                 if (byteCount < 0)
345                         throw new ArgumentOutOfRangeException ("byteCount");
346                 if (charCount < byteCount)
347                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
348
349                 for (int i = 0; i < byteCount; i++){
350                         byte b = bytes [i];
351                         chars [i] = b > 127 ? '?' : (char) b;
352                 }
353                 return byteCount;
354                 
355         }
356
357         [CLSCompliantAttribute(false)]
358         [ComVisible (false)]
359         public unsafe override int GetCharCount (byte *bytes, int count)
360         {
361                 return count;
362         }
363
364         [CLSCompliantAttribute(false)]
365         [ComVisible (false)]
366         public unsafe override int GetByteCount (char *chars, int count)
367         {
368                 return count;
369         }
370
371         [ComVisible (false)]
372         public override Decoder GetDecoder ()
373         {
374                 return base.GetDecoder ();
375         }
376
377         [ComVisible (false)]
378         public override Encoder GetEncoder ()
379         {
380                 return base.GetEncoder ();
381         }
382 }; // class ASCIIEncoding
383
384 }; // namespace System.Text