[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[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         // until we change the callers:
35         internal static string _ (string arg) {
36                 return arg;
37         }
38
39         // Magic number used by Windows for the ISO Latin1 code page.
40         internal const int ISOLATIN_CODE_PAGE = 28591;
41
42         // Constructor.
43         public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
44         {
45                 // Nothing to do here.
46         }
47
48         public override bool IsSingleByte {
49                 get { return true; }
50         }
51
52         public override bool IsAlwaysNormalized (NormalizationForm form)
53         {
54                 return form == NormalizationForm.FormC;
55         }
56
57         // Get the number of bytes needed to encode a character buffer.
58         public override int GetByteCount (char[] chars, int index, int count)
59         {
60                 if (chars == null) {
61                         throw new ArgumentNullException ("chars");
62                 }
63                 if (index < 0 || index > chars.Length) {
64                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
65                 }
66                 if (count < 0 || count > (chars.Length - index)) {
67                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
68                 }
69                 return count;
70         }
71
72         // Convenience wrappers for "GetByteCount".
73         public override int GetByteCount (String s)
74         {
75                 if (s == null) {
76                         throw new ArgumentNullException ("s");
77                 }
78                 return s.Length;
79         }
80
81         // Get the bytes that result from encoding a character buffer.
82         public override int GetBytes (char[] chars, int charIndex, int charCount,
83                                                                  byte[] bytes, int byteIndex)
84         {
85                 EncoderFallbackBuffer buffer = null;
86                 char [] fallback_chars = null;
87                 return GetBytes (chars, charIndex, charCount, bytes,
88                         byteIndex, ref buffer, ref fallback_chars);
89         }
90
91         int GetBytes (char[] chars, int charIndex, int charCount,
92                       byte[] bytes, int byteIndex,
93                       ref EncoderFallbackBuffer buffer,
94                       ref char [] fallback_chars)
95         {
96                 if (chars == null)
97                         throw new ArgumentNullException ("chars");
98
99                 unsafe {
100                         fixed (char *cptr = chars) {
101                                 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
102                         }
103                 }
104         }
105
106         // Convenience wrappers for "GetBytes".
107         public override int GetBytes (String s, int charIndex, int charCount,
108                                                                  byte[] bytes, int byteIndex)
109         {
110                 EncoderFallbackBuffer buffer = null;
111                 char [] fallback_chars = null;
112                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
113                         ref buffer, ref fallback_chars);
114         }
115
116         int GetBytes (String s, int charIndex, int charCount,
117                       byte[] bytes, int byteIndex,
118                       ref EncoderFallbackBuffer buffer,
119                       ref char [] fallback_chars)
120         {
121                 if (s == null)
122                         throw new ArgumentNullException ("s");
123
124                 unsafe {
125                         fixed (char *chars = s) {
126                                 return InternalGetBytes (chars, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
127                         }
128                 }
129         }
130
131         unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
132                       byte[] bytes, int byteIndex,
133                       ref EncoderFallbackBuffer buffer,
134                       ref char [] fallback_chars)
135         {
136                 if (bytes == null)
137                         throw new ArgumentNullException ("bytes");
138                 if (charIndex < 0 || charIndex > charLength)
139                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
140                 if (charCount < 0 || charCount > (charLength - charIndex))
141                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
142                 if (byteIndex < 0 || byteIndex > bytes.Length)
143                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
144                 if ((bytes.Length - byteIndex) < charCount)
145                         throw new ArgumentException (_("Arg_InsufficientSpace"));
146
147                 int count = charCount;
148                 char ch;
149                 while (count-- > 0) {
150                         ch = chars [charIndex++];
151                         if (ch < (char)0x0100) {
152                                 bytes [byteIndex++] = (byte)ch;
153                         } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
154                                 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
155                         } else {
156                                 if (buffer == null)
157                                         buffer = EncoderFallback.CreateFallbackBuffer ();
158                                 if (Char.IsSurrogate (ch) && count > 1 &&
159                                     Char.IsSurrogate (chars [charIndex]))
160                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
161                                 else
162                                         buffer.Fallback (ch, charIndex - 1);
163                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
164                                         fallback_chars = new char [buffer.Remaining];
165                                 for (int i = 0; i < fallback_chars.Length; i++)
166                                         fallback_chars [i] = buffer.GetNextChar ();
167                                 byteIndex += GetBytes (fallback_chars, 0, 
168                                         fallback_chars.Length, bytes, byteIndex,
169                                         ref buffer, ref fallback_chars);
170                         }
171                 }
172                 return charCount;
173         }
174
175         // Get the number of characters needed to decode a byte buffer.
176         public override int GetCharCount (byte[] bytes, int index, int count)
177         {
178                 if (bytes == null) {
179                         throw new ArgumentNullException ("bytes");
180                 }
181                 if (index < 0 || index > bytes.Length) {
182                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
183                 }
184                 if (count < 0 || count > (bytes.Length - index)) {
185                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
186                 }
187                 return count;
188         }
189
190         // Get the characters that result from decoding a byte buffer.
191         public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
192                                                                  char[] chars, int charIndex)
193         {
194                 if (bytes == null) {
195                         throw new ArgumentNullException ("bytes");
196                 }
197                 if (chars == null) {
198                         throw new ArgumentNullException ("chars");
199                 }
200                 if (byteIndex < 0 || byteIndex > bytes.Length) {
201                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
202                 }
203                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
204                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
205                 }
206                 if (charIndex < 0 || charIndex > chars.Length) {
207                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
208                 }
209                 if ((chars.Length - charIndex) < byteCount) {
210                         throw new ArgumentException (_("Arg_InsufficientSpace"));
211                 }
212                 int count = byteCount;
213                 while (count-- > 0) {
214                         chars [charIndex++] = (char)(bytes [byteIndex++]);
215                 }
216                 return byteCount;
217         }
218
219         // Get the maximum number of bytes needed to encode a
220         // specified number of characters.
221         public override int GetMaxByteCount (int charCount)
222         {
223                 if (charCount < 0) {
224                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
225                 }
226                 return charCount;
227         }
228
229         // Get the maximum number of characters needed to decode a
230         // specified number of bytes.
231         public override int GetMaxCharCount (int byteCount)
232         {
233                 if (byteCount < 0) {
234                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
235                 }
236                 return byteCount;
237         }
238
239         // Decode a buffer of bytes into a string.
240         public override String GetString (byte[] bytes, int index, int count)
241         {
242                 if (bytes == null) {
243                         throw new ArgumentNullException ("bytes");
244                 }
245                 if (index < 0 || index > bytes.Length) {
246                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
247                 }
248                 if (count < 0 || count > (bytes.Length - index)) {
249                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
250                 }
251                 if (count == 0)
252                     return String.Empty;
253                 unsafe {
254                         fixed (byte* bytePtr = bytes) {
255                                 string s = string.InternalAllocateStr (count);
256
257                                 fixed (char* charPtr = s) {
258                                         byte* currByte = bytePtr + index;
259                                         byte* lastByte = currByte + count;
260                                         char* currChar = charPtr;
261
262                                         while (currByte < lastByte)
263                                                 currChar++ [0] = (char) currByte++ [0];
264                                 }
265
266                                 return s;
267                         }
268                 }
269         }
270         public override String GetString (byte[] bytes)
271         {
272                 if (bytes == null) {
273                         throw new ArgumentNullException ("bytes");
274                 }
275
276                 return GetString (bytes, 0, bytes.Length);
277         }
278
279 #if !ECMA_COMPAT
280
281         // Get the mail body name for this encoding.
282         public override String BodyName
283         {
284                 get {
285                         return "iso-8859-1";
286                 }
287         }
288
289         // Get the human-readable name for this encoding.
290         public override String EncodingName
291         {
292                 get {
293                         return "Western European (ISO)";
294                 }
295         }
296
297         // Get the mail agent header name for this encoding.
298         public override String HeaderName
299         {
300                 get {
301                         return "iso-8859-1";
302                 }
303         }
304
305         // Determine if this encoding can be displayed in a Web browser.
306         public override bool IsBrowserDisplay
307         {
308                 get {
309                         return true;
310                 }
311         }
312
313         // Determine if this encoding can be saved from a Web browser.
314         public override bool IsBrowserSave
315         {
316                 get {
317                         return true;
318                 }
319         }
320
321         // Determine if this encoding can be displayed in a mail/news agent.
322         public override bool IsMailNewsDisplay
323         {
324                 get {
325                         return true;
326                 }
327         }
328
329         // Determine if this encoding can be saved from a mail/news agent.
330         public override bool IsMailNewsSave
331         {
332                 get {
333                         return true;
334                 }
335         }
336
337         // Get the IANA-preferred Web name for this encoding.
338         public override String WebName
339         {
340                 get {
341                         return "iso-8859-1";
342                 }
343         }
344
345 #endif // !ECMA_COMPAT
346
347 }; // class Latin1Encoding
348
349 }; // namespace System.Text