Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / Latin1Encoding.cs
1 /*
2  * Latin1Encoding.cs - Implementation of the
3  *                      "System.Text.Latin1Encoding" class.
4  *
5  * Copyright (c) 2002  Southern Storm Software, Pty Ltd
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 namespace System.Text
27 {
28
29 using System;
30
31 [Serializable]
32 internal class Latin1Encoding : Encoding
33 {
34         // Magic number used by Windows for the ISO Latin1 code page.
35         internal const int ISOLATIN_CODE_PAGE = 28591;
36
37         // Constructor.
38         public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
39         {
40                 // Nothing to do here.
41         }
42
43         public override bool IsSingleByte {
44                 get { return true; }
45         }
46
47         public override bool IsAlwaysNormalized (NormalizationForm form)
48         {
49                 return form == NormalizationForm.FormC;
50         }
51
52         // Get the number of bytes needed to encode a character buffer.
53         public override int GetByteCount (char[] chars, int index, int count)
54         {
55                 if (chars == null) {
56                         throw new ArgumentNullException ("chars");
57                 }
58                 if (index < 0 || index > chars.Length) {
59                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
60                 }
61                 if (count < 0 || count > (chars.Length - index)) {
62                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
63                 }
64                 return count;
65         }
66
67         // Convenience wrappers for "GetByteCount".
68         public override int GetByteCount (String s)
69         {
70                 if (s == null) {
71                         throw new ArgumentNullException ("s");
72                 }
73                 return s.Length;
74         }
75
76         // Get the bytes that result from encoding a character buffer.
77         public override int GetBytes (char[] chars, int charIndex, int charCount,
78                                                                  byte[] bytes, int byteIndex)
79         {
80                 EncoderFallbackBuffer buffer = null;
81                 char [] fallback_chars = null;
82                 return GetBytes (chars, charIndex, charCount, bytes,
83                         byteIndex, ref buffer, ref fallback_chars);
84         }
85
86         int GetBytes (char[] chars, int charIndex, int charCount,
87                       byte[] bytes, int byteIndex,
88                       ref EncoderFallbackBuffer buffer,
89                       ref char [] fallback_chars)
90         {
91                 if (chars == null)
92                         throw new ArgumentNullException ("chars");
93
94                 unsafe {
95                         fixed (char *cptr = chars) {
96                                 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
97                         }
98                 }
99         }
100
101         // Convenience wrappers for "GetBytes".
102         public override int GetBytes (String s, int charIndex, int charCount,
103                                                                  byte[] bytes, int byteIndex)
104         {
105                 EncoderFallbackBuffer buffer = null;
106                 char [] fallback_chars = null;
107                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
108                         ref buffer, ref fallback_chars);
109         }
110
111         int GetBytes (String s, int charIndex, int charCount,
112                       byte[] bytes, int byteIndex,
113                       ref EncoderFallbackBuffer buffer,
114                       ref char [] fallback_chars)
115         {
116                 if (s == null)
117                         throw new ArgumentNullException ("s");
118
119                 unsafe {
120                         fixed (char *chars = s) {
121                                 return InternalGetBytes (chars, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
122                         }
123                 }
124         }
125
126         unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
127                       byte[] bytes, int byteIndex,
128                       ref EncoderFallbackBuffer buffer,
129                       ref char [] fallback_chars)
130         {
131                 if (bytes == null)
132                         throw new ArgumentNullException ("bytes");
133                 if (charIndex < 0 || charIndex > charLength)
134                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
135                 if (charCount < 0 || charCount > (charLength - charIndex))
136                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
137                 if (byteIndex < 0 || byteIndex > bytes.Length)
138                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
139                 if ((bytes.Length - byteIndex) < charCount)
140                         throw new ArgumentException (_("Arg_InsufficientSpace"));
141
142                 int count = charCount;
143                 char ch;
144                 while (count-- > 0) {
145                         ch = chars [charIndex++];
146                         if (ch < (char)0x0100) {
147                                 bytes [byteIndex++] = (byte)ch;
148                         } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
149                                 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
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,
187                                                                  char[] chars, int charIndex)
188         {
189                 if (bytes == null) {
190                         throw new ArgumentNullException ("bytes");
191                 }
192                 if (chars == null) {
193                         throw new ArgumentNullException ("chars");
194                 }
195                 if (byteIndex < 0 || byteIndex > bytes.Length) {
196                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
197                 }
198                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
199                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
200                 }
201                 if (charIndex < 0 || charIndex > chars.Length) {
202                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
203                 }
204                 if ((chars.Length - charIndex) < byteCount) {
205                         throw new ArgumentException (_("Arg_InsufficientSpace"));
206                 }
207                 int count = byteCount;
208                 while (count-- > 0) {
209                         chars [charIndex++] = (char)(bytes [byteIndex++]);
210                 }
211                 return byteCount;
212         }
213
214         // Get the maximum number of bytes needed to encode a
215         // specified number of characters.
216         public override int GetMaxByteCount (int charCount)
217         {
218                 if (charCount < 0) {
219                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
220                 }
221                 return charCount;
222         }
223
224         // Get the maximum number of characters needed to decode a
225         // specified number of bytes.
226         public override int GetMaxCharCount (int byteCount)
227         {
228                 if (byteCount < 0) {
229                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
230                 }
231                 return byteCount;
232         }
233
234         // Decode a buffer of bytes into a string.
235         public override String GetString (byte[] bytes, int index, int count)
236         {
237                 if (bytes == null) {
238                         throw new ArgumentNullException ("bytes");
239                 }
240                 if (index < 0 || index > bytes.Length) {
241                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
242                 }
243                 if (count < 0 || count > (bytes.Length - index)) {
244                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
245                 }
246                 if (count == 0)
247                     return String.Empty;
248                 unsafe {
249                         fixed (byte* bytePtr = bytes) {
250                                 string s = string.InternalAllocateStr (count);
251
252                                 fixed (char* charPtr = s) {
253                                         byte* currByte = bytePtr + index;
254                                         byte* lastByte = currByte + count;
255                                         char* currChar = charPtr;
256
257                                         while (currByte < lastByte)
258                                                 currChar++ [0] = (char) currByte++ [0];
259                                 }
260
261                                 return s;
262                         }
263                 }
264         }
265         public override String GetString (byte[] bytes)
266         {
267                 if (bytes == null) {
268                         throw new ArgumentNullException ("bytes");
269                 }
270
271                 return GetString (bytes, 0, bytes.Length);
272         }
273
274 #if !ECMA_COMPAT
275
276         // Get the mail body name for this encoding.
277         public override String BodyName
278         {
279                 get {
280                         return "iso-8859-1";
281                 }
282         }
283
284         // Get the human-readable name for this encoding.
285         public override String EncodingName
286         {
287                 get {
288                         return "Western European (ISO)";
289                 }
290         }
291
292         // Get the mail agent header name for this encoding.
293         public override String HeaderName
294         {
295                 get {
296                         return "iso-8859-1";
297                 }
298         }
299
300         // Determine if this encoding can be displayed in a Web browser.
301         public override bool IsBrowserDisplay
302         {
303                 get {
304                         return true;
305                 }
306         }
307
308         // Determine if this encoding can be saved from a Web browser.
309         public override bool IsBrowserSave
310         {
311                 get {
312                         return true;
313                 }
314         }
315
316         // Determine if this encoding can be displayed in a mail/news agent.
317         public override bool IsMailNewsDisplay
318         {
319                 get {
320                         return true;
321                 }
322         }
323
324         // Determine if this encoding can be saved from a mail/news agent.
325         public override bool IsMailNewsSave
326         {
327                 get {
328                         return true;
329                 }
330         }
331
332         // Get the IANA-preferred Web name for this encoding.
333         public override String WebName
334         {
335                 get {
336                         return "iso-8859-1";
337                 }
338         }
339
340 #endif // !ECMA_COMPAT
341
342 }; // class Latin1Encoding
343
344 }; // namespace System.Text