Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.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                 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                                 buffer.Fallback (bytes, byteIndex);
220                                 while (buffer.Remaining > 0)
221                                         chars [charIndex++] = buffer.GetNextChar ();
222                         }
223                 }
224                 return byteCount;
225         }
226
227         // Get the maximum number of bytes needed to encode a
228         // specified number of characters.
229         public override int GetMaxByteCount (int charCount)
230         {
231                 if (charCount < 0) {
232                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
233                 }
234                 return charCount;
235         }
236
237         // Get the maximum number of characters needed to decode a
238         // specified number of bytes.
239         public override int GetMaxCharCount (int byteCount)
240         {
241                 if (byteCount < 0) {
242                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
243                 }
244                 return byteCount;
245         }
246
247         // Decode a buffer of bytes into a string.
248         public override String GetString (byte[] bytes, int byteIndex, int byteCount)
249         {
250                 if (bytes == null) {
251                         throw new ArgumentNullException ("bytes");
252                 }
253                 if (byteIndex < 0 || byteIndex > bytes.Length) {
254                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
255                 }
256                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
257                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
258                 }
259                 if (byteCount == 0)
260                         return String.Empty;
261                 
262                 unsafe {
263                         fixed (byte* bytePtr = bytes) {
264                                 string s = string.InternalAllocateStr (byteCount);
265
266                                 fixed (char* charPtr = s) {
267                                         byte* currByte = bytePtr + byteIndex;
268                                         byte* lastByte = currByte + byteCount;
269                                         char* currChar = charPtr;
270
271                                         while (currByte < lastByte) {
272                                                 byte b = currByte++ [0];
273                                                 currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
274                                         }
275                                 }
276
277                                 return s;
278                         }
279                 }
280         }
281
282         [CLSCompliantAttribute (false)]
283         [ComVisible (false)]
284         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
285         {
286                 if (chars == null)
287                         throw new ArgumentNullException ("chars");
288                 if (bytes == null)
289                         throw new ArgumentNullException ("bytes");
290                 if (charCount < 0)
291                         throw new ArgumentOutOfRangeException ("charCount");
292                 if (byteCount < 0)
293                         throw new ArgumentOutOfRangeException ("byteCount");
294
295                 if (byteCount < charCount)
296                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
297
298                 for (int i = 0; i < charCount; i++){
299                         char c = chars [i];
300                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
301                 }
302                 return charCount;
303         }
304
305         [CLSCompliantAttribute(false)]
306         [ComVisible (false)]
307         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
308         {
309                 if (bytes == null)
310                         throw new ArgumentNullException ("bytes");
311                 if (chars == null) 
312                         throw new ArgumentNullException ("chars");
313                 if (charCount < 0)
314                         throw new ArgumentOutOfRangeException ("charCount");
315                 if (byteCount < 0)
316                         throw new ArgumentOutOfRangeException ("byteCount");
317                 if (charCount < byteCount)
318                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
319
320                 for (int i = 0; i < byteCount; i++){
321                         byte b = bytes [i];
322                         chars [i] = b > 127 ? '?' : (char) b;
323                 }
324                 return byteCount;
325                 
326         }
327
328         [CLSCompliantAttribute(false)]
329         [ComVisible (false)]
330         public unsafe override int GetCharCount (byte *bytes, int count)
331         {
332                 return count;
333         }
334
335         [CLSCompliantAttribute(false)]
336         [ComVisible (false)]
337         public unsafe override int GetByteCount (char *chars, int count)
338         {
339                 return count;
340         }
341
342         [ComVisible (false)]
343         public override Decoder GetDecoder ()
344         {
345                 return base.GetDecoder ();
346         }
347
348         [ComVisible (false)]
349         public override Encoder GetEncoder ()
350         {
351                 return base.GetEncoder ();
352         }
353 }; // class ASCIIEncoding
354
355 }; // namespace System.Text