In .:
[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
32 [Serializable]
33 [MonoTODO ("Fix serialization compatibility with MS.NET")]
34 public class ASCIIEncoding : Encoding
35 {
36         // Magic number used by Windows for "ASCII".
37         internal const int ASCII_CODE_PAGE = 20127;
38
39         // Constructor.
40         public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
41                 body_name = header_name = web_name= "us-ascii";
42                 encoding_name = "US-ASCII";
43                 is_mail_news_display = true;
44                 is_mail_news_save = true;
45         }
46
47 #if NET_2_0
48         public override bool IsSingleByte {
49                 get { return true; }
50         }
51 #endif
52
53         // Get the number of bytes needed to encode a character buffer.
54         public override int GetByteCount (char[] chars, int index, int count)
55         {
56                 if (chars == null) {
57                         throw new ArgumentNullException ("chars");
58                 }
59                 if (index < 0 || index > chars.Length) {
60                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
61                 }
62                 if (count < 0 || count > (chars.Length - index)) {
63                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
64                 }
65                 return count;
66         }
67
68         // Convenience wrappers for "GetByteCount".
69         public override int GetByteCount (String s)
70         {
71                 if (s == null) {
72                         throw new ArgumentNullException ("s");
73                 }
74                 return s.Length;
75         }
76
77         // Get the bytes that result from encoding a character buffer.
78         public override int GetBytes (char[] chars, int charIndex, int charCount,
79                                                                  byte[] bytes, int byteIndex)
80         {
81 #if NET_2_0
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                 return GetBytes (chars, charIndex, charCount, bytes,
86                         byteIndex, 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 #endif
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 NET_2_0
121                                 if (buffer == null)
122                                         buffer = EncoderFallback.CreateFallbackBuffer ();
123                                 if (Char.IsSurrogate (ch) && count > 1 &&
124                                     Char.IsSurrogate (chars [charIndex]))
125                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
126                                 else
127                                         buffer.Fallback (ch, charIndex - 1);
128                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
129                                         fallback_chars = new char [buffer.Remaining];
130                                 for (int i = 0; i < fallback_chars.Length; i++)
131                                         fallback_chars [i] = buffer.GetNextChar ();
132                                 byteIndex += GetBytes (fallback_chars, 0, 
133                                         fallback_chars.Length, bytes, byteIndex,
134                                         ref buffer, ref fallback_chars);
135 #else
136                                 bytes [byteIndex++] = (byte)'?';
137 #endif
138                         }
139                 }
140                 return charCount;
141         }
142
143         // Convenience wrappers for "GetBytes".
144         public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
145         {
146 #if NET_2_0
147 // I know this #if is ugly, but I think it is the simplest switch.
148                 EncoderFallbackBuffer buffer = null;
149                 char [] fallback_chars = null;
150                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
151                         ref buffer, ref fallback_chars);
152         }
153
154         int GetBytes (String s, int charIndex, int charCount,
155                       byte[] bytes, int byteIndex,
156                       ref EncoderFallbackBuffer buffer,
157                       ref char [] fallback_chars)
158         {
159 #endif
160                 if (s == null) {
161                         throw new ArgumentNullException ("s");
162                 }
163                 if (bytes == null) {
164                         throw new ArgumentNullException ("bytes");
165                 }
166                 if (charIndex < 0 || charIndex > s.Length) {
167                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
168                 }
169                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
170                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
171                 }
172                 if (byteIndex < 0 || byteIndex > bytes.Length) {
173                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
174                 }
175                 if ((bytes.Length - byteIndex) < charCount) {
176                         throw new ArgumentException (_("Arg_InsufficientSpace"));
177                 }
178                 int count = charCount;
179                 char ch;
180                 while (count-- > 0) {
181                         ch = s [charIndex++];
182                         if (ch < (char)0x80) {
183                                 bytes [byteIndex++] = (byte)ch;
184                         } else {
185 #if NET_2_0
186                                 if (buffer == null)
187                                         buffer = EncoderFallback.CreateFallbackBuffer ();
188                                 if (Char.IsSurrogate (ch) && count > 1 &&
189                                     Char.IsSurrogate (s [charIndex]))
190                                         buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
191                                 else
192                                         buffer.Fallback (ch, charIndex - 1);
193                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
194                                         fallback_chars = new char [buffer.Remaining];
195                                 for (int i = 0; i < fallback_chars.Length; i++)
196                                         fallback_chars [i] = buffer.GetNextChar ();
197                                 byteIndex += GetBytes (fallback_chars, 0, 
198                                         fallback_chars.Length, bytes, byteIndex,
199                                         ref buffer, ref fallback_chars);
200 #else
201                                 bytes [byteIndex++] = (byte)'?';
202 #endif
203                         }
204                 }
205                 return charCount;
206         }
207
208         // Get the number of characters needed to decode a byte buffer.
209         public override int GetCharCount (byte[] bytes, int index, int count)
210         {
211                 if (bytes == null) {
212                         throw new ArgumentNullException ("bytes");
213                 }
214                 if (index < 0 || index > bytes.Length) {
215                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
216                 }
217                 if (count < 0 || count > (bytes.Length - index)) {
218                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
219                 }
220                 return count;
221         }
222
223         // Get the characters that result from decoding a byte buffer.
224         public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
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                                 chars [charIndex++] = '?';
247                 }
248                 return byteCount;
249         }
250
251         // Get the maximum number of bytes needed to encode a
252         // specified number of characters.
253         public override int GetMaxByteCount (int charCount)
254         {
255                 if (charCount < 0) {
256                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
257                 }
258                 return charCount;
259         }
260
261         // Get the maximum number of characters needed to decode a
262         // specified number of bytes.
263         public override int GetMaxCharCount (int byteCount)
264         {
265                 if (byteCount < 0) {
266                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
267                 }
268                 return byteCount;
269         }
270
271         // Decode a buffer of bytes into a string.
272         public override String GetString (byte[] bytes, int index, int count)
273         {
274                 if (bytes == null) {
275                         throw new ArgumentNullException ("bytes");
276                 }
277                 if (index < 0 || index > bytes.Length) {
278                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
279                 }
280                 if (count < 0 || count > (bytes.Length - index)) {
281                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
282                 }
283                 if (count == 0)
284                         return String.Empty;
285                 
286                 unsafe {
287                         fixed (byte *ss = &bytes [0]) {
288                                 return new String ((sbyte*)ss, index, count);
289                         }
290                 }
291         }
292
293 #if NET_2_0
294         [CLSCompliantAttribute (false)]
295         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
296         {
297                 if (chars == null)
298                         throw new ArgumentNullException ("chars");
299                 if (bytes == null)
300                         throw new ArgumentNullException ("bytes");
301                 if (charCount < 0)
302                         throw new ArgumentOutOfRangeException ("charCount");
303                 if (byteCount < 0)
304                         throw new ArgumentOutOfRangeException ("byteCount");
305
306                 if (byteCount < charCount)
307                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
308
309                 for (int i = 0; i < charCount; i++){
310                         char c = chars [i];
311                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
312                 }
313                 return charCount;
314         }
315
316         [CLSCompliantAttribute(false)]
317         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
318         {
319                 if (bytes == null)
320                         throw new ArgumentNullException ("bytes");
321                 if (chars == null) 
322                         throw new ArgumentNullException ("chars");
323                 if (charCount < 0)
324                         throw new ArgumentOutOfRangeException ("charCount");
325                 if (byteCount < 0)
326                         throw new ArgumentOutOfRangeException ("byteCount");
327                 if (charCount < byteCount)
328                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
329
330                 for (int i = 0; i < byteCount; i++){
331                         byte b = bytes [i];
332                         chars [i] = b > 127 ? '?' : (char) b;
333                 }
334                 return byteCount;
335                 
336         }
337
338         [CLSCompliantAttribute(false)]
339         public unsafe override int GetCharCount (byte *bytes, int count)
340         {
341                 return count;
342         }
343
344         [CLSCompliantAttribute(false)]
345         public unsafe override int GetByteCount (char *chars, int count)
346         {
347                 return count;
348         }
349 #else
350         // This routine is gone in 2.x
351         public override String GetString (byte[] bytes)
352         {
353                 if (bytes == null) {
354                         throw new ArgumentNullException ("bytes");
355                 }
356                 int count = bytes.Length;
357                 if (count == 0)
358                     return String.Empty;
359                 unsafe {
360                         fixed (byte *ss = &bytes [0]) {
361                                 return new String ((sbyte*)ss, 0, count);
362                         }
363                 }
364         }
365 #endif
366         
367 }; // class ASCIIEncoding
368
369 }; // namespace System.Text