├── .gitattributes ├── .gitignore ├── Portable.Text.Encoding.sln ├── Portable.Text.Encoding ├── ASCIIEncoding.cs ├── CJK │ ├── CP51932.cs │ ├── CP932.cs │ ├── CP936.cs │ ├── CP949.cs │ ├── CP950.cs │ ├── CodeTable.cs │ ├── DbcsConvert.cs │ ├── DbcsEncoding.cs │ ├── GB18030Encoding.cs │ ├── GB18030Source.cs │ ├── ISO2022JP.cs │ ├── JISConvert.cs │ ├── big5.table │ ├── gb18030.table │ ├── gb2312.table │ ├── jis.table │ └── ks.table ├── Common │ ├── ByteEncoding.cs │ ├── MonoEncoding.cs │ └── MonoSafeEncoding.cs ├── Decoder.cs ├── DecoderExceptionFallback.cs ├── DecoderExceptionFallbackBuffer.cs ├── DecoderFallback.cs ├── DecoderFallbackBuffer.cs ├── DecoderFallbackException.cs ├── DecoderReplacementFallback.cs ├── DecoderReplacementFallbackBuffer.cs ├── Encoder.cs ├── EncoderExceptionFallback.cs ├── EncoderExceptionFallbackBuffer.cs ├── EncoderFallback.cs ├── EncoderFallbackBuffer.cs ├── EncoderFallbackException.cs ├── EncoderReplacementFallback.cs ├── EncoderReplacementFallbackBuffer.cs ├── Encoding.cs ├── EncodingInfo.cs ├── Latin1Encoding.cs ├── MidEast │ ├── CP1254.cs │ ├── CP1255.cs │ ├── CP1256.cs │ ├── CP28596.cs │ ├── CP28598.cs │ ├── CP28599.cs │ └── CP38598.cs ├── NormalizationForm.cs ├── Other │ ├── CP1251.cs │ ├── CP1257.cs │ ├── CP1258.cs │ ├── CP20866.cs │ ├── CP21866.cs │ ├── CP28594.cs │ ├── CP28595.cs │ ├── CP57002.cs │ └── CP874.cs ├── Portable.Text.Encoding.WindowsUniversal81.csproj ├── Portable.Text.Encoding.csproj ├── Properties │ └── AssemblyInfo.cs ├── Rare │ ├── CP1026.cs │ ├── CP1047.cs │ ├── CP1140.cs │ ├── CP1141.cs │ ├── CP1142.cs │ ├── CP1143.cs │ ├── CP1144.cs │ ├── CP1145.cs │ ├── CP1146.cs │ ├── CP1147.cs │ ├── CP1148.cs │ ├── CP1149.cs │ ├── CP20273.cs │ ├── CP20277.cs │ ├── CP20278.cs │ ├── CP20280.cs │ ├── CP20284.cs │ ├── CP20285.cs │ ├── CP20290.cs │ ├── CP20297.cs │ ├── CP20420.cs │ ├── CP20424.cs │ ├── CP20871.cs │ ├── CP21025.cs │ ├── CP37.cs │ ├── CP500.cs │ ├── CP708.cs │ ├── CP852.cs │ ├── CP855.cs │ ├── CP857.cs │ ├── CP858.cs │ ├── CP862.cs │ ├── CP864.cs │ ├── CP866.cs │ ├── CP869.cs │ ├── CP870.cs │ └── CP875.cs ├── UTF32Encoding.cs ├── UTF7Encoding.cs ├── UTF8Encoding.cs ├── UnicodeEncoding.cs ├── Western │ ├── CP10000.cs │ ├── CP10079.cs │ ├── CP1250.cs │ ├── CP1252.cs │ ├── CP1253.cs │ ├── CP28592.cs │ ├── CP28593.cs │ ├── CP28597.cs │ ├── CP28605.cs │ ├── CP437.cs │ ├── CP850.cs │ ├── CP860.cs │ ├── CP861.cs │ ├── CP863.cs │ └── CP865.cs └── portable.text.encoding.snk ├── README.md └── nuget └── Portable.Text.Encoding.nuspec /.gitattributes: -------------------------------------------------------------------------------- 1 | *.csproj eol=crlf 2 | *.nuspec eol=lf 3 | *.sln eol=crlf 4 | *.txt eol=lf 5 | *.cs eol=lf 6 | *.md eol=lf 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.userprefs 2 | bin 3 | obj 4 | -------------------------------------------------------------------------------- /Portable.Text.Encoding.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | VisualStudioVersion = 12.0.30324.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Portable.Text.Encoding", "Portable.Text.Encoding\Portable.Text.Encoding.csproj", "{EEE48C75-11BE-4B50-B759-F85B5052D473}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Portable.Text.Encoding.WindowsUniversal81", "Portable.Text.Encoding\Portable.Text.Encoding.WindowsUniversal81.csproj", "{B76A64F9-B00E-4243-AE89-5D024CA3B436}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {EEE48C75-11BE-4B50-B759-F85B5052D473}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {EEE48C75-11BE-4B50-B759-F85B5052D473}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {EEE48C75-11BE-4B50-B759-F85B5052D473}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {EEE48C75-11BE-4B50-B759-F85B5052D473}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(MonoDevelopProperties) = preSolution 29 | StartupItem = Portable.Text.Encoding\Portable.Text.Encoding.csproj 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/CP936.cs: -------------------------------------------------------------------------------- 1 | // 2 | // I18N.CJK.CP936.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | // (new implementation based on CP950.) 8 | // 9 | 10 | using System; 11 | 12 | namespace Portable.Text { 13 | class CP936 : DbcsEncoding 14 | { 15 | // Magic number used by Windows for the Gb2312 code page. 16 | const int GB2312_CODE_PAGE = 936; 17 | 18 | // Constructor. 19 | public CP936 () : base (GB2312_CODE_PAGE) 20 | { 21 | } 22 | 23 | internal override DbcsConvert GetConvert () 24 | { 25 | return DbcsConvert.Gb2312; 26 | } 27 | 28 | // Get the bytes that result from encoding a character buffer. 29 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 30 | { 31 | return GetBytesImpl (chars, charCount, null, 0); 32 | } 33 | 34 | // Get the bytes that result from encoding a character buffer. 35 | public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) 36 | { 37 | EncoderFallbackBuffer buffer = null; 38 | DbcsConvert gb2312 = GetConvert (); 39 | int end = charCount; 40 | int charIndex = 0; 41 | int byteIndex = 0; 42 | 43 | int origIndex = byteIndex; 44 | for (int i = charIndex; i < end; i++, charCount--) { 45 | char c = chars [i]; 46 | if (c <= 0x80 || c == 0xFF) { // ASCII 47 | int offset = byteIndex++; 48 | if (bytes != null) 49 | bytes [offset] = (byte)c; 50 | continue; 51 | } 52 | byte b1 = gb2312.u2n [((int)c) * 2 + 1]; 53 | byte b2 = gb2312.u2n [((int)c) * 2]; 54 | if (b1 == 0 && b2 == 0) { 55 | HandleFallback (ref buffer, chars, ref i, ref charCount, bytes, ref byteIndex, ref byteCount, null); 56 | } else { 57 | if (bytes != null) { 58 | bytes [byteIndex++] = b1; 59 | bytes [byteIndex++] = b2; 60 | } else { 61 | byteIndex += 2; 62 | } 63 | } 64 | } 65 | return byteIndex - origIndex; 66 | } 67 | 68 | // Get the characters that result from decoding a byte buffer. 69 | public override int GetCharCount (byte[] bytes, int index, int count) 70 | { 71 | return GetDecoder ().GetCharCount (bytes, index, count); 72 | } 73 | 74 | // Get the characters that result from decoding a byte buffer. 75 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 76 | { 77 | return GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex); 78 | } 79 | 80 | // Get a decoder that handles a rolling Gb2312 state. 81 | public override 82 | #if STANDALONE 83 | Decoder 84 | #else 85 | System.Text.Decoder 86 | #endif 87 | GetDecoder () 88 | { 89 | return new CP936Decoder (GetConvert ()); 90 | } 91 | 92 | // Get the mail body name for this encoding. 93 | public override string BodyName { 94 | get { return "gb2312"; } 95 | } 96 | 97 | // Get the human-readable name for this encoding. 98 | public override string EncodingName { 99 | get { return "Chinese Simplified (GB2312)"; } 100 | } 101 | 102 | // Get the mail agent header name for this encoding. 103 | public override string HeaderName { 104 | get { return "gb2312"; } 105 | } 106 | 107 | // Determine if this encoding can be displayed in a Web browser. 108 | public override bool IsBrowserDisplay { 109 | get { return true; } 110 | } 111 | 112 | // Determine if this encoding can be saved from a Web browser. 113 | public override bool IsBrowserSave { 114 | get { return true; } 115 | } 116 | 117 | // Determine if this encoding can be displayed in a mail/news agent. 118 | public override bool IsMailNewsDisplay { 119 | get { return true; } 120 | } 121 | 122 | // Determine if this encoding can be saved from a mail/news agent. 123 | public override bool IsMailNewsSave { 124 | get { return true; } 125 | } 126 | 127 | // Get the IANA-preferred Web name for this encoding. 128 | public override string WebName { 129 | get { return "gb2312"; } 130 | } 131 | } 132 | 133 | // Decoder that handles a rolling Gb2312 state. 134 | sealed class CP936Decoder : DbcsEncoding.DbcsDecoder 135 | { 136 | int last_byte_count, last_byte_bytes; 137 | 138 | // Constructor. 139 | public CP936Decoder (DbcsConvert convert) : base (convert) 140 | { 141 | } 142 | 143 | // Get the characters that result from decoding a byte buffer. 144 | public override int GetCharCount (byte[] bytes, int index, int count) 145 | { 146 | return GetCharCount (bytes, index, count, false); 147 | } 148 | 149 | public override int GetCharCount (byte[] bytes, int index, int count, bool flush) 150 | { 151 | CheckRange (bytes, index, count); 152 | 153 | int lastByte = last_byte_count; 154 | last_byte_count = 0; 155 | int length = 0; 156 | 157 | while (count-- > 0) { 158 | int b = bytes [index++]; 159 | if (lastByte == 0) { 160 | if (b <= 0x80 || b == 0xFF) { // ASCII 161 | length++; 162 | continue; 163 | } else { 164 | lastByte = b; 165 | continue; 166 | } 167 | } 168 | length++; 169 | lastByte = 0; 170 | } 171 | 172 | if (lastByte != 0) { 173 | if (flush) { 174 | length++; 175 | last_byte_count = 0; 176 | } else 177 | last_byte_count = lastByte; 178 | } 179 | 180 | return length; 181 | } 182 | 183 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 184 | { 185 | return GetChars (bytes, byteIndex, byteCount, chars, charIndex, false); 186 | } 187 | 188 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush) 189 | { 190 | CheckRange (bytes, byteIndex, byteCount, chars, charIndex); 191 | 192 | int origIndex = charIndex; 193 | int lastByte = last_byte_bytes; 194 | last_byte_bytes = 0; 195 | while (byteCount-- > 0) { 196 | int b = bytes [byteIndex++]; 197 | if (lastByte == 0) { 198 | if (b <= 0x80 || b == 0xFF) { // ASCII 199 | chars [charIndex++] = (char)b; 200 | continue; 201 | } else if (b < 0x81 || b >= 0xFF) { 202 | continue; 203 | } else { 204 | lastByte = b; 205 | continue; 206 | } 207 | } 208 | int ord = ((lastByte - 0x81) * 191 + b - 0x40) * 2; 209 | char c1 = ord < 0 || ord >= convert.n2u.Length ? 210 | '\0' : (char)(convert.n2u [ord] + convert.n2u [ord + 1] * 256); 211 | if (c1 == 0) 212 | chars [charIndex++] = '?'; 213 | else 214 | chars [charIndex++] = c1; 215 | lastByte = 0; 216 | } 217 | 218 | if (lastByte != 0) { 219 | if (flush) { 220 | // FIXME: handle fallback 221 | chars [charIndex++] = '?'; 222 | last_byte_bytes = 0; 223 | } else { 224 | last_byte_bytes = lastByte; 225 | } 226 | } 227 | 228 | return charIndex - origIndex; 229 | } 230 | } 231 | 232 | class ENCgb2312 : CP936 233 | { 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/CP950.cs: -------------------------------------------------------------------------------- 1 | // 2 | // I18N.CJK.CP950 3 | // 4 | // Author: 5 | // Alan Tam Siu Lung (Tam@SiuLung.com) 6 | // Atsushi Enomoto 7 | // 8 | 9 | using System; 10 | 11 | namespace Portable.Text { 12 | class CP950 : DbcsEncoding 13 | { 14 | // Magic number used by Windows for the Big5 code page. 15 | const int BIG5_CODE_PAGE = 950; 16 | 17 | // Constructor. 18 | public CP950 () : base (BIG5_CODE_PAGE) 19 | { 20 | } 21 | 22 | internal override DbcsConvert GetConvert () 23 | { 24 | return DbcsConvert.Big5; 25 | } 26 | 27 | // Get the bytes that result from encoding a character buffer. 28 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 29 | { 30 | DbcsConvert convert = GetConvert (); 31 | int index = 0; 32 | int length = 0; 33 | 34 | while (charCount-- > 0) { 35 | char c = chars [index++]; 36 | if (c <= 0x80 || c == 0xFF) { // ASCII 37 | length++; 38 | continue; 39 | } 40 | byte b1 = convert.u2n [((int)c) * 2 + 1]; 41 | byte b2 = convert.u2n [((int)c) * 2]; 42 | if (b1 == 0 && b2 == 0) { 43 | // FIXME: handle fallback for GetByteCountImpl(). 44 | length++; 45 | } else { 46 | length += 2; 47 | } 48 | } 49 | return length; 50 | } 51 | 52 | // Get the bytes that result from encoding a character buffer. 53 | public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) 54 | { 55 | EncoderFallbackBuffer buffer = null; 56 | DbcsConvert convert = GetConvert (); 57 | int end = charCount; 58 | int charIndex = 0; 59 | int byteIndex = 0; 60 | 61 | int origIndex = byteIndex; 62 | for (int i = charIndex; i < end; i++, charCount--) { 63 | char c = chars [i]; 64 | if (c <= 0x80 || c == 0xFF) { // ASCII 65 | bytes [byteIndex++] = (byte)c; 66 | continue; 67 | } 68 | byte b1 = convert.u2n [((int)c) * 2 + 1]; 69 | byte b2 = convert.u2n [((int)c) * 2]; 70 | if (b1 == 0 && b2 == 0) { 71 | HandleFallback (ref buffer, chars, ref i, ref charCount, bytes, ref byteIndex, ref byteCount, null); 72 | } else { 73 | bytes [byteIndex++] = b1; 74 | bytes [byteIndex++] = b2; 75 | } 76 | } 77 | return byteIndex - origIndex; 78 | } 79 | 80 | // Get the characters that result from decoding a byte buffer. 81 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 82 | { 83 | return GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex); 84 | } 85 | 86 | // Get a decoder that handles a rolling Big5 state. 87 | public override 88 | #if STANDALONE 89 | Decoder 90 | #else 91 | System.Text.Decoder 92 | #endif 93 | GetDecoder () 94 | { 95 | return new CP950Decoder (GetConvert ()); 96 | } 97 | 98 | // Get the mail body name for this encoding. 99 | public override String BodyName { 100 | get { return "big5"; } 101 | } 102 | 103 | // Get the human-readable name for this encoding. 104 | public override String EncodingName { 105 | get { return "Chinese Traditional (Big5)"; } 106 | } 107 | 108 | // Get the mail agent header name for this encoding. 109 | public override String HeaderName { 110 | get { return "big5"; } 111 | } 112 | 113 | // Get the IANA-preferred Web name for this encoding. 114 | public override String WebName { 115 | get { return "big5"; } 116 | } 117 | 118 | /* 119 | // Get the Windows code page represented by this object. 120 | public override int WindowsCodePage { 121 | get { return BIG5_PAGE; } 122 | } 123 | */ 124 | 125 | // Decoder that handles a rolling Big5 state. 126 | sealed class CP950Decoder : DbcsDecoder 127 | { 128 | int last_byte_count, last_byte_conv; 129 | 130 | // Constructor. 131 | public CP950Decoder (DbcsConvert convert) : base (convert) 132 | { 133 | } 134 | 135 | public override int GetCharCount (byte[] bytes, int index, int count) 136 | { 137 | return GetCharCount (bytes, index, count, false); 138 | } 139 | 140 | public override int GetCharCount (byte[] bytes, int index, int count, bool flush) 141 | { 142 | CheckRange (bytes, index, count); 143 | 144 | int lastByte = last_byte_count; 145 | last_byte_count = 0; 146 | int length = 0; 147 | 148 | while (count-- > 0) { 149 | int b = bytes [index++]; 150 | if (lastByte == 0) { 151 | if (b <= 0x80 || b == 0xFF) { // ASCII 152 | length++; 153 | } else if (b < 0xA1 || b >= 0xFA) { 154 | // incorrect first byte. 155 | length++; 156 | count--; // cut one more byte. 157 | } else { 158 | lastByte = b; 159 | } 160 | continue; 161 | } 162 | int ord = ((lastByte - 0xA1) * 191 + b - 0x40) * 2; 163 | char c1 = ord < 0 || ord > convert.n2u.Length ? 164 | '\0' : 165 | (char)(convert.n2u [ord] + convert.n2u [ord + 1] * 256); 166 | if (c1 == 0) 167 | // FIXME: fallback 168 | length++; 169 | else 170 | length++; 171 | lastByte = 0; 172 | } 173 | 174 | if (lastByte != 0) { 175 | if (flush) 176 | // FIXME: fallback 177 | length++; 178 | else 179 | last_byte_count = lastByte; 180 | } 181 | return length; 182 | } 183 | 184 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 185 | { 186 | return GetChars (bytes, byteIndex, byteCount, chars, charIndex, false); 187 | } 188 | 189 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush) 190 | { 191 | CheckRange (bytes, byteIndex, byteCount, chars, charIndex); 192 | 193 | int origIndex = charIndex; 194 | int lastByte = last_byte_conv; 195 | last_byte_conv = 0; 196 | 197 | while (byteCount-- > 0) { 198 | int b = bytes [byteIndex++]; 199 | if (lastByte == 0) { 200 | if (b <= 0x80 || b == 0xFF) { // ASCII 201 | chars [charIndex++] = (char)b; 202 | } else if (b < 0xA1 || b >= 0xFA) { 203 | // incorrect first byte. 204 | chars [charIndex++] = '?'; 205 | byteCount--; // cut one more byte. 206 | } else { 207 | lastByte = b; 208 | } 209 | continue; 210 | } 211 | 212 | int ord = ((lastByte - 0xA1) * 191 + b - 0x40) * 2; 213 | char c1 = ord < 0 || ord > convert.n2u.Length ? 214 | '\0' : 215 | (char)(convert.n2u [ord] + convert.n2u [ord + 1] * 256); 216 | 217 | if (c1 == 0) 218 | chars [charIndex++] = '?'; 219 | else 220 | chars [charIndex++] = c1; 221 | lastByte = 0; 222 | } 223 | 224 | if (lastByte != 0) { 225 | if (flush) 226 | chars [charIndex++] = '?'; 227 | else 228 | last_byte_conv = lastByte; 229 | } 230 | 231 | return charIndex - origIndex; 232 | } 233 | } 234 | } 235 | 236 | class ENCbig5 : CP950 237 | { 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/CodeTable.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CodeTable.cs - Implementation of the "System.Text.CodeTable" class. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | using System; 26 | using System.IO; 27 | using System.Reflection; 28 | 29 | namespace Portable.Text { 30 | // This class assists encoding classes for the large CJK character 31 | // sets by providing pointer access to table data in the resource 32 | // section of the current assembly. 33 | // 34 | // Code tables are named by their resource (e.g. "jis.table") and 35 | // contain one or more sections. Each section has an 8-byte header, 36 | // consisting of a 32-bit section number and a 32-bit section length. 37 | // The alignment of the data in the table is not guaranteed. 38 | sealed class CodeTable : IDisposable 39 | { 40 | // Internal state. 41 | Stream stream; 42 | 43 | // Load a code table from the resource section of this assembly. 44 | public CodeTable (string name) 45 | { 46 | stream = typeof (CodeTable).GetTypeInfo ().Assembly.GetManifestResourceStream (name); 47 | 48 | if (stream == null) 49 | throw new NotSupportedException ("Encoding not supported."); 50 | } 51 | 52 | // Implement the IDisposable interface. 53 | public void Dispose () 54 | { 55 | if (stream != null) { 56 | stream.Dispose (); 57 | stream = null; 58 | } 59 | } 60 | 61 | public byte[] GetSection (int num) 62 | { 63 | // If the table has been disposed, then bail out. 64 | if (stream == null) 65 | return null; 66 | 67 | // Scan through the stream looking for the section. 68 | byte[] header = new byte [8]; 69 | long length = stream.Length; 70 | int sectNum, sectLen; 71 | long posn = 0; 72 | 73 | while ((posn + 8) <= length) { 74 | // Read the next header block. 75 | stream.Position = posn; 76 | if (stream.Read (header, 0, 8) != 8) 77 | break; 78 | 79 | // Decode the fields in the header block. 80 | sectNum = ((int)(header [0])) | 81 | (((int)(header [1])) << 8) | 82 | (((int)(header [2])) << 16) | 83 | (((int)(header [3])) << 24); 84 | sectLen = ((int)(header [4])) | 85 | (((int)(header [5])) << 8) | 86 | (((int)(header [6])) << 16) | 87 | (((int)(header [7])) << 24); 88 | 89 | // Is this the section we are looking for? 90 | if (sectNum == num) { 91 | byte[] buf = new byte [sectLen]; 92 | if (stream.Read (buf, 0, sectLen) != sectLen) 93 | break; 94 | 95 | return buf; 96 | } 97 | 98 | // Advance to the next section. 99 | posn += 8 + sectLen; 100 | } 101 | 102 | // We were unable to find the requested section. 103 | return null; 104 | } 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/DbcsConvert.cs: -------------------------------------------------------------------------------- 1 | // 2 | // I18N.CJK.DbcsConvert 3 | // 4 | // Author: 5 | // Alan Tam Siu Lung (Tam@SiuLung.com) 6 | // Atsushi Enomoto 7 | // 8 | 9 | namespace Portable.Text 10 | { 11 | // This class assists other DBCS encoding classes in converting back 12 | // and forth between JIS character sets and Unicode. It uses 13 | // several large tables to do this, some of which are stored in 14 | // the resource section of the assembly for efficient access. 15 | class DbcsConvert 16 | { 17 | internal static readonly DbcsConvert Gb2312 = new DbcsConvert ("gb2312.table"); 18 | internal static readonly DbcsConvert Big5 = new DbcsConvert ("big5.table"); 19 | internal static readonly DbcsConvert KS = new DbcsConvert ("ks.table"); 20 | 21 | // Public access to the conversion tables. 22 | public byte[] n2u; 23 | public byte[] u2n; 24 | 25 | // Constructor. 26 | internal DbcsConvert (string fileName) 27 | { 28 | using (var table = new CodeTable (fileName)) { 29 | n2u = table.GetSection(1); 30 | u2n = table.GetSection(2); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/DbcsEncoding.cs: -------------------------------------------------------------------------------- 1 | // 2 | // I18N.CJK.DbcsEncoding 3 | // 4 | // Author: 5 | // Alan Tam Siu Lung (Tam@SiuLung.com) 6 | // 7 | 8 | using System; 9 | using System.Text; 10 | 11 | namespace Portable.Text 12 | { 13 | abstract class DbcsEncoding : MonoEncoding 14 | { 15 | protected DbcsEncoding (int codePage) : this (codePage, 0) 16 | { 17 | } 18 | 19 | protected DbcsEncoding (int codePage, int windowsCodePage) 20 | : base (codePage, windowsCodePage) 21 | { 22 | } 23 | 24 | internal abstract DbcsConvert GetConvert (); 25 | 26 | // Get the number of bytes needed to encode a character buffer. 27 | public override int GetByteCount (char[] chars, int index, int count) 28 | { 29 | if (chars == null) 30 | throw new ArgumentNullException ("chars"); 31 | 32 | if (index < 0 || index > chars.Length) 33 | throw new ArgumentOutOfRangeException ("index"); 34 | 35 | if (count < 0 || index + count > chars.Length) 36 | throw new ArgumentOutOfRangeException ("count"); 37 | 38 | byte[] buffer = new byte[count * 2]; 39 | 40 | return GetBytes(chars, index, count, buffer, 0); 41 | } 42 | 43 | // Get the number of characters needed to decode a byte buffer. 44 | public override int GetCharCount (byte[] bytes, int index, int count) 45 | { 46 | if (bytes == null) 47 | throw new ArgumentNullException ("bytes"); 48 | 49 | if (index < 0 || index > bytes.Length) 50 | throw new ArgumentOutOfRangeException ("index"); 51 | 52 | if (count < 0 || index + count > bytes.Length) 53 | throw new ArgumentOutOfRangeException ("count"); 54 | 55 | char[] buffer = new char[count]; 56 | 57 | return GetChars(bytes, index, count, buffer, 0); 58 | } 59 | 60 | // Get the characters that result from decoding a byte buffer. 61 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 62 | { 63 | if (bytes == null) 64 | throw new ArgumentNullException ("bytes"); 65 | 66 | if (chars == null) 67 | throw new ArgumentNullException ("chars"); 68 | 69 | if (byteIndex < 0 || byteIndex > bytes.Length) 70 | throw new ArgumentOutOfRangeException ("byteIndex"); 71 | 72 | if (byteCount < 0 || byteIndex + byteCount > bytes.Length) 73 | throw new ArgumentOutOfRangeException ("byteCount"); 74 | 75 | if (charIndex < 0 || charIndex > chars.Length) 76 | throw new ArgumentOutOfRangeException ("charIndex"); 77 | 78 | return 0; // For subclasses to implement 79 | } 80 | 81 | // Get the maximum number of bytes needed to encode a 82 | // specified number of characters. 83 | public override int GetMaxByteCount (int charCount) 84 | { 85 | if (charCount < 0) 86 | throw new ArgumentOutOfRangeException ("charCount"); 87 | 88 | return charCount * 2; 89 | } 90 | 91 | // Get the maximum number of characters needed to decode a 92 | // specified number of bytes. 93 | public override int GetMaxCharCount (int byteCount) 94 | { 95 | if (byteCount < 0) 96 | throw new ArgumentOutOfRangeException ("byteCount"); 97 | 98 | return byteCount; 99 | } 100 | 101 | // Determine if this encoding can be displayed in a Web browser. 102 | public override bool IsBrowserDisplay { 103 | get { return true; } 104 | } 105 | 106 | // Determine if this encoding can be saved from a Web browser. 107 | public override bool IsBrowserSave { 108 | get { return true; } 109 | } 110 | 111 | // Determine if this encoding can be displayed in a mail/news agent. 112 | public override bool IsMailNewsDisplay { 113 | get { return true; } 114 | } 115 | 116 | // Determine if this encoding can be saved from a mail/news agent. 117 | public override bool IsMailNewsSave { 118 | get { return true; } 119 | } 120 | 121 | // Decoder that handles a rolling state. 122 | internal abstract class DbcsDecoder : Decoder 123 | { 124 | protected DbcsConvert convert; 125 | 126 | // Constructor. 127 | public DbcsDecoder (DbcsConvert convert) 128 | { 129 | this.convert = convert; 130 | } 131 | 132 | internal void CheckRange (byte[] bytes, int index, int count) 133 | { 134 | if (bytes == null) 135 | throw new ArgumentNullException ("bytes"); 136 | 137 | if (index < 0 || index > bytes.Length) 138 | throw new ArgumentOutOfRangeException ("index"); 139 | 140 | if (count < 0 || count > (bytes.Length - index)) 141 | throw new ArgumentOutOfRangeException ("count"); 142 | } 143 | 144 | internal void CheckRange (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 145 | { 146 | if (bytes == null) 147 | throw new ArgumentNullException ("bytes"); 148 | 149 | if (chars == null) 150 | throw new ArgumentNullException ("chars"); 151 | 152 | if (byteIndex < 0 || byteIndex > bytes.Length) 153 | throw new ArgumentOutOfRangeException ("byteIndex"); 154 | 155 | if (byteCount < 0 || byteIndex + byteCount > bytes.Length) 156 | throw new ArgumentOutOfRangeException ("byteCount"); 157 | 158 | if (charIndex < 0 || charIndex > chars.Length) 159 | throw new ArgumentOutOfRangeException ("charIndex"); 160 | } 161 | } 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/GB18030Source.cs: -------------------------------------------------------------------------------- 1 | // 2 | // GB18030Encoding.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | using System; 8 | using System.Reflection; 9 | 10 | namespace Portable.Text 11 | { 12 | unsafe class GB18030Source 13 | { 14 | class GB18030Map 15 | { 16 | public readonly int UStart; 17 | public readonly int UEnd; 18 | public readonly long GStart; 19 | public readonly long GEnd; 20 | public readonly bool Dummy; // This range is actually not usable. 21 | 22 | public GB18030Map (int ustart, int uend, long gstart, long gend, bool dummy) 23 | { 24 | this.UStart = ustart; 25 | this.UEnd = uend; 26 | this.GStart = gstart; 27 | this.GEnd = gend; 28 | this.Dummy = dummy; 29 | } 30 | } 31 | 32 | GB18030Source () 33 | { 34 | } 35 | 36 | static readonly byte[] gbx2uni; 37 | static readonly byte[] uni2gbx; 38 | 39 | static GB18030Source () 40 | { 41 | var assembly = typeof (GB18030Source).GetTypeInfo ().Assembly; 42 | 43 | using (var stream = assembly.GetManifestResourceStream ("gb18030.table")) { 44 | var buf = new byte[4]; 45 | int size; 46 | 47 | stream.Read (buf, 0, buf.Length); 48 | size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3]); 49 | 50 | gbx2uni = new byte[size]; 51 | stream.Read (gbx2uni, 0, size); 52 | 53 | stream.Read (buf, 0, buf.Length); 54 | size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3]); 55 | 56 | uni2gbx = new byte[size]; 57 | stream.Read (uni2gbx, 0, size); 58 | } 59 | } 60 | 61 | static readonly long gbxBase = FromGBXRaw (0x81, 0x30, 0x81, 0x30, false); 62 | static readonly long gbxSuppBase = FromGBXRaw (0x90, 0x30, 0x81, 0x30, false); 63 | 64 | // See http://icu.sourceforge.net/docs/papers/gb18030.html 65 | // and referenced XML mapping table. 66 | static readonly GB18030Map [] ranges = { 67 | // rawmap: 0x0080-0x0451 68 | new GB18030Map (0x0452, 0x200F, FromGBXRaw (0x81, 0x30, 0xD3, 0x30, false), FromGBXRaw (0x81, 0x36, 0xA5, 0x31, false), false), 69 | // rawmap: 0x2010-0x2642 70 | new GB18030Map (0x2643, 0x2E80, FromGBXRaw (0x81, 0x37, 0xA8, 0x39, false), FromGBXRaw (0x81, 0x38, 0xFD, 0x38, false), false), 71 | // rawmap: 0x2E81-0x361A 72 | new GB18030Map (0x361B, 0x3917, FromGBXRaw (0x82, 0x30, 0xA6, 0x33, false), FromGBXRaw (0x82, 0x30, 0xF2, 0x37, false), false), 73 | // rawmap: 0x3918-0x3CE0 74 | new GB18030Map (0x3CE1, 0x4055, FromGBXRaw (0x82, 0x31, 0xD4, 0x38, false), FromGBXRaw (0x82, 0x32, 0xAF, 0x32, false), false), 75 | // rawmap: 0x4056-0x415F 76 | new GB18030Map (0x4160, 0x4336, FromGBXRaw (0x82, 0x32, 0xC9, 0x37, false), FromGBXRaw (0x82, 0x32, 0xF8, 0x37, false), false), 77 | // rawmap: 4337-0x44D6 78 | new GB18030Map (0x44D7, 0x464B, FromGBXRaw (0x82, 0x33, 0xA3, 0x39, false), FromGBXRaw (0x82, 0x33, 0xC9, 0x31, false), false), 79 | // rawmap: 0x464C-0x478D 80 | new GB18030Map (0x478E, 0x4946, FromGBXRaw (0x82, 0x33, 0xE8, 0x38, false), FromGBXRaw (0x82, 0x34, 0x96, 0x38, false), false), 81 | // rawmap: 0x4947-0x49B7 82 | new GB18030Map (0x49B8, 0x4C76, FromGBXRaw (0x82, 0x34, 0xA1, 0x31, false), FromGBXRaw (0x82, 0x34, 0xE7, 0x33, false), false), 83 | // rawmap: 0x4C77-0x4DFF 84 | 85 | // 4E00-9FA5 are all mapped in GB2312 86 | new GB18030Map (0x4E00, 0x9FA5, 0, 0, true), 87 | 88 | new GB18030Map (0x9FA6, 0xD7FF, FromGBXRaw (0x82, 0x35, 0x8F, 0x33, false), FromGBXRaw (0x83, 0x36, 0xC7, 0x38, false), false), 89 | 90 | // D800-DFFF are ignored (surrogate) 91 | // E000-E76B are all mapped in GB2312. 92 | new GB18030Map (0xD800, 0xE76B, 0, 0, true), 93 | 94 | // rawmap: 0xE76C-E884 95 | new GB18030Map (0xE865, 0xF92B, FromGBXRaw (0x83, 0x36, 0xD0, 0x30, false), FromGBXRaw (0x84, 0x30, 0x85, 0x34, false), false), 96 | // rawmap: 0xF92C-FA29 97 | new GB18030Map (0xFA2A, 0xFE2F, FromGBXRaw (0x84, 0x30, 0x9C, 0x38, false), FromGBXRaw (0x84, 0x31, 0x85, 0x37, false), false), 98 | // rawmap: 0xFE30-FFE5 99 | new GB18030Map (0xFFE6, 0xFFFF, FromGBXRaw (0x84, 0x31, 0xA2, 0x34, false), FromGBXRaw (0x84, 0x31, 0xA4, 0x39, false), false), 100 | }; 101 | 102 | public static void Unlinear (byte [] bytes, int start, long gbx) 103 | { 104 | fixed (byte* bptr = bytes) { 105 | Unlinear (bptr + start, gbx); 106 | } 107 | } 108 | 109 | public static unsafe void Unlinear (byte* bytes, long gbx) 110 | { 111 | bytes [3] = (byte) (gbx % 10 + 0x30); 112 | gbx /= 10; 113 | bytes [2] = (byte) (gbx % 126 + 0x81); 114 | gbx /= 126; 115 | bytes [1] = (byte) (gbx % 10 + 0x30); 116 | gbx /= 10; 117 | bytes [0] = (byte) (gbx + 0x81); 118 | } 119 | 120 | // negative (invalid) or positive (valid) 121 | public static long FromGBX (byte [] bytes, int start) 122 | { 123 | byte b1 = bytes [start]; 124 | byte b2 = bytes [start + 1]; 125 | byte b3 = bytes [start + 2]; 126 | byte b4 = bytes [start + 3]; 127 | 128 | if (b1 < 0x81 || b1 == 0xFF) 129 | return -1; 130 | if (b2 < 0x30 || b2 > 0x39) 131 | return -2; 132 | if (b3 < 0x81 || b3 == 0xFF) 133 | return -3; 134 | if (b4 < 0x30 || b4 > 0x39) 135 | return -4; 136 | 137 | if (b1 >= 0x90) 138 | return FromGBXRaw (b1, b2, b3, b4, true); 139 | 140 | long linear = FromGBXRaw (b1, b2, b3, b4, false); 141 | 142 | long rawOffset = 0; 143 | long startIgnore = 0; 144 | 145 | foreach (var range in ranges) { 146 | if (linear < range.GStart) 147 | return ToUcsRaw ((int)(linear - startIgnore + rawOffset)); 148 | 149 | if (linear <= range.GEnd) 150 | return linear - gbxBase - range.GStart + range.UStart; 151 | 152 | if (range.GStart != 0) { 153 | rawOffset += range.GStart - startIgnore; 154 | startIgnore = range.GEnd + 1; 155 | } 156 | } 157 | 158 | // return ToUcsRaw ((int) (linear - gbxBase)); 159 | throw new Exception (string.Format ("GB18030 INTERNAL ERROR (should not happen): GBX {0:x02} {1:x02} {2:x02} {3:x02}", b1, b2, b3, b4)); 160 | } 161 | 162 | public static long FromUCSSurrogate (int cp) 163 | { 164 | return cp + gbxSuppBase; 165 | } 166 | 167 | public static long FromUCS (int cp) 168 | { 169 | long startIgnore = 0x80; 170 | long rawOffset = 0; 171 | 172 | foreach (var range in ranges) { 173 | if (cp < range.UStart) 174 | return ToGbxRaw ((int) (cp - startIgnore + rawOffset)); 175 | 176 | if (cp <= range.UEnd) 177 | return cp - range.UStart + range.GStart; 178 | 179 | if (range.GStart != 0) { 180 | rawOffset += range.UStart - startIgnore; 181 | startIgnore = range.UEnd + 1; 182 | } 183 | } 184 | 185 | throw new Exception (String.Format ("GB18030 INTERNAL ERROR (should not happen): UCS {0:x06}", cp)); 186 | } 187 | 188 | static long FromGBXRaw (byte b1, byte b2, byte b3, byte b4, bool supp) 189 | { 190 | // 126 = 0xFE - 0x80 191 | return (((b1 - (supp ? 0x90 : 0x81)) * 10 + 192 | (b2 - 0x30)) * 126 + 193 | (b3 - 0x81)) * 10 + 194 | b4 - 0x30 + (supp ? 0x10000 : 0); 195 | } 196 | 197 | static int ToUcsRaw (int idx) 198 | { 199 | return gbx2uni[idx * 2] * 0x100 + gbx2uni[idx * 2 + 1]; 200 | } 201 | 202 | static long ToGbxRaw (int idx) 203 | { 204 | if (idx < 0 || idx * 2 + 1 >= uni2gbx.Length) 205 | return -1; 206 | 207 | return gbxBase + uni2gbx[idx * 2] * 0x100 + uni2gbx[idx * 2 + 1]; 208 | } 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/JISConvert.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * JISConvert.cs - Implementation of the "System.Text.JISConvert" class. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | namespace Portable.Text { 26 | // This class assists other encoding classes in converting back 27 | // and forth between JIS character sets and Unicode. It uses 28 | // several large tables to do this, some of which are stored in 29 | // the resource section of the assembly for efficient access. 30 | sealed class JISConvert 31 | { 32 | // Table identifiers. 33 | const int JISX0208_To_Unicode = 1; 34 | const int JISX0212_To_Unicode = 2; 35 | const int CJK_To_JIS = 3; 36 | const int Greek_To_JIS = 4; 37 | const int Extra_To_JIS = 5; 38 | 39 | // Public access to the conversion tables. 40 | public byte[] Jisx0208ToUnicode; 41 | public byte[] Jisx0212ToUnicode; 42 | public byte[] CjkToJis; 43 | public byte[] GreekToJis; 44 | public byte[] ExtraToJis; 45 | 46 | // Constructor. 47 | JISConvert () 48 | { 49 | // Load the conversion tables. 50 | using (var table = new CodeTable ("jis.table")) { 51 | Jisx0208ToUnicode = table.GetSection (JISX0208_To_Unicode); 52 | Jisx0212ToUnicode = table.GetSection (JISX0212_To_Unicode); 53 | CjkToJis = table.GetSection (CJK_To_JIS); 54 | GreekToJis = table.GetSection (Greek_To_JIS); 55 | ExtraToJis = table.GetSection (Extra_To_JIS); 56 | } 57 | } 58 | 59 | // The one and only JIS conversion object in the system. 60 | static JISConvert convert; 61 | 62 | static readonly object lockobj = new object (); 63 | 64 | // Get the primary JIS conversion object. 65 | public static JISConvert Convert { 66 | get { 67 | lock (lockobj) { 68 | if (convert != null) { 69 | return convert; 70 | } else { 71 | convert = new JISConvert (); 72 | return convert; 73 | } 74 | } 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/big5.table: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/CJK/big5.table -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/gb18030.table: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/CJK/gb18030.table -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/gb2312.table: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/CJK/gb2312.table -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/jis.table: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/CJK/jis.table -------------------------------------------------------------------------------- /Portable.Text.Encoding/CJK/ks.table: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/CJK/ks.table -------------------------------------------------------------------------------- /Portable.Text.Encoding/Common/ByteEncoding.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * ByteEncoding.cs - Implementation of the "I18N.Common.ByteEncoding" class. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | using System; 26 | 27 | namespace Portable.Text { 28 | // This class provides an abstract base for encodings that use a single 29 | // byte per character. The bulk of the work is done in this class, with 30 | // subclasses providing implementations of the "ToBytes" methods to perform 31 | // the char->byte conversion. 32 | abstract class ByteEncoding : MonoEncoding 33 | { 34 | // static byte[] isNormalized; 35 | // static byte[] isNormalizedComputed; 36 | // static byte[] normalization_bytes; 37 | 38 | // Internal state. 39 | protected char[] toChars; 40 | protected string encodingName; 41 | protected string bodyName; 42 | protected string headerName; 43 | protected string webName; 44 | protected bool isBrowserDisplay; 45 | protected bool isBrowserSave; 46 | protected bool isMailNewsDisplay; 47 | protected bool isMailNewsSave; 48 | protected int windowsCodePage; 49 | 50 | // Constructor. 51 | protected ByteEncoding (int codePage, char[] toChars, 52 | string encodingName, string bodyName, 53 | string headerName, string webName, 54 | bool isBrowserDisplay, bool isBrowserSave, 55 | bool isMailNewsDisplay, bool isMailNewsSave, 56 | int windowsCodePage) 57 | : base (codePage) 58 | { 59 | if (toChars.Length != byte.MaxValue + 1) 60 | throw new ArgumentException ("toChars"); 61 | 62 | this.toChars = toChars; 63 | this.encodingName = encodingName; 64 | this.bodyName = bodyName; 65 | this.headerName = headerName; 66 | this.webName = webName; 67 | this.isBrowserDisplay = isBrowserDisplay; 68 | this.isBrowserSave = isBrowserSave; 69 | this.isMailNewsDisplay = isMailNewsDisplay; 70 | this.isMailNewsSave = isMailNewsSave; 71 | this.windowsCodePage = windowsCodePage; 72 | } 73 | 74 | public override bool IsAlwaysNormalized (NormalizationForm form) 75 | { 76 | // if (form != NormalizationForm.FormC) 77 | // return false; 78 | // 79 | // if (isNormalized == null) 80 | // isNormalized = new byte [0x10000 / 8]; 81 | // 82 | // if (isNormalizedComputed == null) 83 | // isNormalizedComputed = new byte [0x10000 / 8]; 84 | // 85 | // if (normalization_bytes == null) { 86 | // normalization_bytes = new byte [0x100]; 87 | // lock (normalization_bytes) { 88 | // for (int i = 0; i < 0x100; i++) 89 | // normalization_bytes [i] = (byte)i; 90 | // } 91 | // } 92 | // 93 | // byte offset = (byte)(1 << (CodePage % 8)); 94 | // if ((isNormalizedComputed [CodePage / 8] & offset) == 0) { 95 | // Encoding e = Clone () as Encoding; 96 | // e.DecoderFallback = new DecoderReplacementFallback (""); 97 | // string s = e.GetString (normalization_bytes); 98 | // // note that the flag only stores FormC information. 99 | // if (s != s.Normalize (form)) 100 | // isNormalized [CodePage / 8] |= offset; 101 | // isNormalizedComputed [CodePage / 8] |= offset; 102 | // } 103 | // 104 | // return (isNormalized [CodePage / 8] & offset) == 0; 105 | return false; 106 | } 107 | 108 | public override bool IsSingleByte { 109 | get { return true; } 110 | } 111 | 112 | public override int GetByteCount (String s) 113 | { 114 | if (s == null) { 115 | throw new ArgumentNullException ("s"); 116 | } 117 | return s.Length; 118 | } 119 | 120 | // Get the number of bytes needed to encode a character buffer. 121 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 122 | { 123 | return charCount; 124 | } 125 | 126 | // Convert an array of characters into a byte buffer, 127 | // once the parameters have been validated. 128 | protected unsafe abstract void ToBytes (char* chars, int charCount, byte* bytes, int byteCount); 129 | 130 | // Convert an array of characters into a byte buffer, 131 | // once the parameters have been validated. 132 | protected unsafe virtual void ToBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) 133 | { 134 | // When it is not overriden, use ToBytes() with pointers 135 | // (this is the ideal solution) 136 | if (charCount == 0 || bytes.Length == byteIndex) 137 | return; 138 | 139 | if (charIndex < 0 || charIndex > chars.Length) 140 | throw new ArgumentOutOfRangeException ("charIndex"); 141 | 142 | if (byteIndex < 0 || byteIndex > bytes.Length) 143 | throw new ArgumentOutOfRangeException ("byteIndex"); 144 | 145 | if (charCount < 0 || charIndex + charCount > chars.Length || byteIndex + charCount > bytes.Length) 146 | throw new ArgumentOutOfRangeException ("charCount"); 147 | 148 | fixed (char* cptr = chars) { 149 | fixed (byte* bptr = bytes) { 150 | ToBytes (cptr + charIndex, charCount, bptr + byteIndex, bytes.Length - byteIndex); 151 | } 152 | } 153 | } 154 | 155 | public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) 156 | { 157 | ToBytes (chars, charCount, bytes, byteCount); 158 | return charCount; 159 | } 160 | 161 | // Get the number of characters needed to decode a byte buffer. 162 | public override int GetCharCount (byte[] bytes, int index, int count) 163 | { 164 | if (bytes == null) 165 | throw new ArgumentNullException ("bytes"); 166 | 167 | if (index < 0 || index > bytes.Length) 168 | throw new ArgumentOutOfRangeException ("index"); 169 | 170 | if (count < 0 || count > (bytes.Length - index)) 171 | throw new ArgumentOutOfRangeException ("count"); 172 | 173 | return count; 174 | } 175 | 176 | // Get the characters that result from decoding a byte buffer. 177 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 178 | { 179 | if (bytes == null) 180 | throw new ArgumentNullException ("bytes"); 181 | 182 | if (chars == null) 183 | throw new ArgumentNullException ("chars"); 184 | 185 | if (byteIndex < 0 || byteIndex > bytes.Length) 186 | throw new ArgumentOutOfRangeException ("byteIndex"); 187 | 188 | if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 189 | throw new ArgumentOutOfRangeException ("byteCount"); 190 | 191 | if (charIndex < 0 || charIndex > chars.Length) 192 | throw new ArgumentOutOfRangeException ("charIndex"); 193 | 194 | if ((chars.Length - charIndex) < byteCount) 195 | throw new ArgumentException ("Insufficient space available.", "chars"); 196 | 197 | int count = byteCount; 198 | char[] cvt = toChars; 199 | 200 | while (count-- > 0) 201 | chars [charIndex++] = cvt [(int)(bytes [byteIndex++])]; 202 | 203 | return byteCount; 204 | } 205 | 206 | // Get the maximum number of bytes needed to encode a 207 | // specified number of characters. 208 | public override int GetMaxByteCount (int charCount) 209 | { 210 | if (charCount < 0) 211 | throw new ArgumentOutOfRangeException ("charCount"); 212 | 213 | return charCount; 214 | } 215 | 216 | // Get the maximum number of characters needed to decode a 217 | // specified number of bytes. 218 | public override int GetMaxCharCount (int byteCount) 219 | { 220 | if (byteCount < 0) 221 | throw new ArgumentOutOfRangeException ("byteCount"); 222 | 223 | return byteCount; 224 | } 225 | 226 | // Decode a buffer of bytes into a string. 227 | public unsafe override string GetString (byte[] bytes, int index, int count) 228 | { 229 | if (bytes == null) 230 | throw new ArgumentNullException ("bytes"); 231 | 232 | if (index < 0 || index > bytes.Length) 233 | throw new ArgumentOutOfRangeException ("index"); 234 | 235 | if (count < 0 || count > (bytes.Length - index)) 236 | throw new ArgumentOutOfRangeException ("count"); 237 | 238 | if (count == 0) 239 | return string.Empty; 240 | 241 | string s = new string ((char) 0, count); 242 | 243 | fixed (byte* bytePtr = bytes) { 244 | fixed (char* charPtr = s) { 245 | fixed (char* cvt = toChars) { 246 | byte* b = bytePtr + index; 247 | char* c = charPtr; 248 | 249 | while (count-- != 0) 250 | *(c++) = cvt [*(b++)]; 251 | } 252 | } 253 | } 254 | 255 | return s; 256 | } 257 | 258 | public override String GetString (byte[] bytes) 259 | { 260 | if (bytes == null) 261 | throw new ArgumentNullException ("bytes"); 262 | 263 | return GetString (bytes, 0, bytes.Length); 264 | } 265 | 266 | // Get the mail body name for this encoding. 267 | public override string BodyName { 268 | get { return bodyName; } 269 | } 270 | 271 | // Get the human-readable name for this encoding. 272 | public override string EncodingName { 273 | get { return encodingName; } 274 | } 275 | 276 | // Get the mail agent header name for this encoding. 277 | public override string HeaderName { 278 | get { return headerName; } 279 | } 280 | 281 | // Determine if this encoding can be displayed in a Web browser. 282 | public override bool IsBrowserDisplay { 283 | get { return isBrowserDisplay; } 284 | } 285 | 286 | // Determine if this encoding can be saved from a Web browser. 287 | public override bool IsBrowserSave { 288 | get { return isBrowserSave; } 289 | } 290 | 291 | // Determine if this encoding can be displayed in a mail/news agent. 292 | public override bool IsMailNewsDisplay { 293 | get { return isMailNewsDisplay; } 294 | } 295 | 296 | // Determine if this encoding can be saved from a mail/news agent. 297 | public override bool IsMailNewsSave { 298 | get { return isMailNewsSave; } 299 | } 300 | 301 | // Get the IANA-preferred Web name for this encoding. 302 | public override string WebName { 303 | get { return webName; } 304 | } 305 | // Get the Windows code page represented by this object. 306 | public override int WindowsCodePage { 307 | get { return windowsCodePage; } 308 | } 309 | } 310 | } 311 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Common/MonoEncoding.cs: -------------------------------------------------------------------------------- 1 | // 2 | // MonoEncoding.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | // Copyright (C) 2005 Novell, Inc. http://www.novell.com 8 | // 9 | 10 | using System; 11 | 12 | namespace Portable.Text { 13 | abstract class MonoEncoding : Encoding 14 | { 15 | readonly int win_code_page; 16 | 17 | protected MonoEncoding (int codePage) : this (codePage, 0) 18 | { 19 | } 20 | 21 | protected MonoEncoding (int codePage, int windowsCodePage) : base (codePage) 22 | { 23 | win_code_page = windowsCodePage; 24 | } 25 | 26 | public override int WindowsCodePage { 27 | get { return win_code_page != 0 ? win_code_page : base.WindowsCodePage; } 28 | } 29 | 30 | /// 31 | /// GetBytes method used internally by state-full encoders/encodings. 32 | /// 33 | /// The chars. 34 | /// Index of the char. 35 | /// The char count. 36 | /// The bytes. 37 | /// Index of the byte. 38 | /// if set to true [flush]. 39 | /// The encoding class to use (or null if state-less). 40 | /// 41 | /// 42 | /// Only state-full encoders need to implement this method (ie. ISO-2022-JP) 43 | /// 44 | protected unsafe virtual int GetBytesInternal (char* chars, int charCount, byte* bytes, int byteCount, bool flush, object state) 45 | { 46 | throw new NotImplementedException ("Statefull encoding is not implemented (yet?) by this encoding class."); 47 | } 48 | 49 | public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer, 50 | char* chars, ref int charIndex, ref int charCount, 51 | byte* bytes, ref int byteIndex, ref int byteCount, object state) 52 | { 53 | if (buffer == null) 54 | buffer = EncoderFallback.CreateFallbackBuffer (); 55 | 56 | if (charCount > 1 && (Char.IsSurrogate (chars [charIndex]) && Char.IsSurrogate (chars [charIndex + 1]))) { 57 | buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex); 58 | charIndex++; 59 | charCount--; 60 | } else { 61 | buffer.Fallback (chars [charIndex], charIndex); 62 | } 63 | 64 | char[] tmp = new char [buffer.Remaining]; 65 | int idx = 0; 66 | while (buffer.Remaining > 0) 67 | tmp [idx++] = buffer.GetNextChar (); 68 | 69 | fixed (char* tmparr = tmp) { 70 | var outbytes = bytes == null ? null : bytes + byteIndex; 71 | var len = state == null ? 72 | GetBytes (tmparr, tmp.Length, outbytes, byteCount) 73 | : GetBytesInternal (tmparr, tmp.Length, outbytes, byteCount, true, state); 74 | 75 | byteIndex += len; 76 | byteCount -= len; 77 | } 78 | } 79 | 80 | public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer, 81 | char* chars, ref int charIndex, ref int charCount, 82 | byte* bytes, ref int byteIndex, ref int byteCount) 83 | { 84 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, 85 | bytes, ref byteIndex, ref byteCount, null); 86 | } 87 | 88 | // Get the bytes that result from encoding a character buffer. 89 | public override int GetByteCount (char[] chars, int index, int count) 90 | { 91 | if (chars == null) 92 | throw new ArgumentNullException ("chars"); 93 | 94 | if (index < 0 || index > chars.Length) 95 | throw new ArgumentOutOfRangeException ("index"); 96 | 97 | if (count < 0 || count > (chars.Length - index)) 98 | throw new ArgumentOutOfRangeException ("count"); 99 | 100 | if (count == 0) 101 | return 0; 102 | 103 | unsafe { 104 | fixed (char* cptr = chars) { 105 | return GetByteCountImpl ( 106 | cptr + index, count); 107 | } 108 | } 109 | } 110 | 111 | // Get the bytes that result from encoding a character buffer. 112 | public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) 113 | { 114 | if (chars == null) 115 | throw new ArgumentNullException ("chars"); 116 | 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | 120 | if (charIndex < 0 || charIndex > chars.Length) 121 | throw new ArgumentOutOfRangeException ("charIndex"); 122 | 123 | if (charCount < 0 || charCount > (chars.Length - charIndex)) 124 | throw new ArgumentOutOfRangeException ("charCount"); 125 | 126 | if (byteIndex < 0 || byteIndex > bytes.Length) 127 | throw new ArgumentOutOfRangeException ("byteIndex"); 128 | 129 | if (bytes.Length - byteIndex < charCount) 130 | throw new ArgumentException ("Insufficient space available.", "bytes"); 131 | 132 | if (charCount == 0) 133 | return 0; 134 | 135 | unsafe { 136 | fixed (char* cptr = chars) { 137 | fixed (byte* bptr = bytes) { 138 | return GetBytesImpl ( 139 | cptr + charIndex, 140 | charCount, 141 | bptr + byteIndex, 142 | bytes.Length - byteIndex); 143 | } 144 | } 145 | } 146 | } 147 | // Convenience wrappers for "GetBytes". 148 | public override int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex) 149 | { 150 | // Validate the parameters. 151 | if (s == null) 152 | throw new ArgumentNullException ("s"); 153 | 154 | if (bytes == null) 155 | throw new ArgumentNullException ("bytes"); 156 | 157 | if (charIndex < 0 || charIndex > s.Length) 158 | throw new ArgumentOutOfRangeException ("charIndex"); 159 | 160 | if (charCount < 0 || charCount > (s.Length - charIndex)) 161 | throw new ArgumentOutOfRangeException ("charCount"); 162 | 163 | if (byteIndex < 0 || byteIndex > bytes.Length) 164 | throw new ArgumentOutOfRangeException ("byteIndex"); 165 | 166 | if ((bytes.Length - byteIndex) < charCount) 167 | throw new ArgumentException ("Insufficient space available.", "bytes"); 168 | 169 | if (charCount == 0 || bytes.Length == byteIndex) 170 | return 0; 171 | 172 | unsafe { 173 | fixed (char* cptr = s) { 174 | fixed (byte* bptr = bytes) { 175 | return GetBytesImpl ( 176 | cptr + charIndex, 177 | charCount, 178 | bptr + byteIndex, 179 | bytes.Length - byteIndex); 180 | } 181 | } 182 | } 183 | } 184 | 185 | public unsafe override int GetByteCount (char* chars, int count) 186 | { 187 | return GetByteCountImpl (chars, count); 188 | } 189 | 190 | public unsafe override int GetBytes (char* chars, int charCount, 191 | byte* bytes, int byteCount) 192 | { 193 | return GetBytesImpl (chars, charCount, bytes, byteCount); 194 | } 195 | 196 | public unsafe abstract int GetByteCountImpl (char* chars, int charCount); 197 | 198 | public unsafe abstract int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount); 199 | } 200 | 201 | abstract class MonoEncoder : Encoder 202 | { 203 | readonly MonoEncoding encoding; 204 | 205 | protected MonoEncoder (MonoEncoding encoding) 206 | { 207 | this.encoding = encoding; 208 | } 209 | 210 | public override int GetByteCount (char[] chars, int index, int count, bool flush) 211 | { 212 | if (chars == null) 213 | throw new ArgumentNullException ("chars"); 214 | 215 | if (index < 0 || index > chars.Length) 216 | throw new ArgumentOutOfRangeException ("index"); 217 | 218 | if (count < 0 || count > (chars.Length - index)) 219 | throw new ArgumentOutOfRangeException ("count"); 220 | 221 | if (count == 0) 222 | return 0; 223 | 224 | unsafe { 225 | fixed (char* cptr = chars) { 226 | return GetByteCountImpl (cptr + index, count, flush); 227 | } 228 | } 229 | } 230 | 231 | public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) 232 | { 233 | if (chars == null) 234 | throw new ArgumentNullException ("chars"); 235 | 236 | if (bytes == null) 237 | throw new ArgumentNullException ("bytes"); 238 | 239 | if (charIndex < 0 || charIndex > chars.Length) 240 | throw new ArgumentOutOfRangeException ("charIndex"); 241 | 242 | if (charCount < 0 || charCount > (chars.Length - charIndex)) 243 | throw new ArgumentOutOfRangeException ("charCount"); 244 | 245 | if (byteIndex < 0 || byteIndex > bytes.Length) 246 | throw new ArgumentOutOfRangeException ("byteIndex"); 247 | 248 | if (bytes.Length - byteIndex < charCount) 249 | throw new ArgumentException ("Insufficient space available.", "bytes"); 250 | 251 | if (charCount == 0) 252 | return 0; 253 | 254 | unsafe { 255 | fixed (char* cptr = chars) { 256 | fixed (byte* bptr = bytes) { 257 | return GetBytesImpl (cptr + charIndex, charCount, bptr + byteIndex, bytes.Length - byteIndex, flush); 258 | } 259 | } 260 | } 261 | } 262 | 263 | public unsafe abstract int GetByteCountImpl (char* chars, int charCount, bool refresh); 264 | 265 | public unsafe abstract int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount, bool refresh); 266 | 267 | public unsafe void HandleFallback ( 268 | char* chars, ref int charIndex, ref int charCount, 269 | byte* bytes, ref int byteIndex, ref int byteCount, object state) 270 | { 271 | EncoderFallbackBuffer buffer = FallbackBuffer; 272 | encoding.HandleFallback (ref buffer, 273 | chars, ref charIndex, ref charCount, 274 | bytes, ref byteIndex, ref byteCount, state); 275 | } 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Common/MonoSafeEncoding.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/Common/MonoSafeEncoding.cs -------------------------------------------------------------------------------- /Portable.Text.Encoding/Decoder.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Decoder.cs - Implementation of the "System.Text.Decoder" class. 3 | * 4 | * Copyright (c) 2001 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | using System; 26 | 27 | namespace Portable.Text 28 | { 29 | public abstract class Decoder 30 | #if !STANDALONE 31 | : System.Text.Decoder 32 | #endif 33 | { 34 | DecoderFallback fallback = new DecoderReplacementFallback (); 35 | DecoderFallbackBuffer fallback_buffer; 36 | 37 | // Constructor. 38 | protected Decoder () 39 | { 40 | } 41 | 42 | public DecoderFallback Fallback { 43 | get { return fallback; } 44 | set { 45 | if (value == null) 46 | throw new ArgumentNullException (); 47 | fallback = value; 48 | fallback_buffer = null; 49 | } 50 | } 51 | 52 | public DecoderFallbackBuffer FallbackBuffer { 53 | get { 54 | if (fallback_buffer == null) 55 | fallback_buffer = fallback.CreateFallbackBuffer (); 56 | return fallback_buffer; 57 | } 58 | } 59 | 60 | #if STANDALONE 61 | // Get the number of characters needed to decode a buffer. 62 | public abstract int GetCharCount (byte[] bytes, int index, int count); 63 | 64 | // Get the characters that result from decoding a buffer. 65 | public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex); 66 | #endif 67 | 68 | #if STANDALONE 69 | public virtual 70 | #else 71 | public override 72 | #endif 73 | int GetCharCount (byte[] bytes, int index, int count, bool flush) 74 | { 75 | if (flush) 76 | Reset (); 77 | 78 | return GetCharCount (bytes, index, count); 79 | } 80 | 81 | #if STANDALONE 82 | public virtual 83 | #else 84 | public override 85 | #endif 86 | int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush) 87 | { 88 | CheckArguments (bytes, byteIndex, byteCount); 89 | CheckArguments (chars, charIndex); 90 | 91 | if (flush) 92 | Reset (); 93 | 94 | return GetChars (bytes, byteIndex, byteCount, chars, charIndex); 95 | } 96 | 97 | #if STANDALONE 98 | public virtual 99 | #else 100 | public override 101 | #endif 102 | void Reset () 103 | { 104 | if (fallback_buffer != null) 105 | fallback_buffer.Reset (); 106 | } 107 | 108 | #if STANDALONE 109 | public virtual 110 | #else 111 | public override 112 | #endif 113 | void Convert (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed) 114 | { 115 | CheckArguments (bytes, byteIndex, byteCount); 116 | 117 | if (chars == null) 118 | throw new ArgumentNullException ("chars"); 119 | 120 | if (charIndex < 0) 121 | throw new ArgumentOutOfRangeException ("charIndex"); 122 | 123 | if (charCount < 0 || chars.Length < charIndex + charCount) 124 | throw new ArgumentOutOfRangeException ("charCount"); 125 | 126 | bytesUsed = byteCount; 127 | while (true) { 128 | charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush); 129 | if (charsUsed <= charCount) 130 | break; 131 | 132 | flush = false; 133 | bytesUsed >>= 1; 134 | } 135 | 136 | completed = bytesUsed == byteCount; 137 | charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush); 138 | } 139 | 140 | static void CheckArguments (char[] chars, int charIndex) 141 | { 142 | if (chars == null) 143 | throw new ArgumentNullException ("chars"); 144 | 145 | if (charIndex < 0 || chars.Length < charIndex) 146 | throw new ArgumentOutOfRangeException ("charIndex"); 147 | } 148 | 149 | static void CheckArguments (byte[] bytes, int byteIndex, int byteCount) 150 | { 151 | if (bytes == null) 152 | throw new ArgumentNullException ("bytes"); 153 | 154 | if (byteIndex < 0) 155 | throw new ArgumentOutOfRangeException ("byteIndex"); 156 | 157 | if (byteCount < 0 || bytes.Length < byteIndex + byteCount) 158 | throw new ArgumentOutOfRangeException ("byteCount"); 159 | } 160 | 161 | static unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount) 162 | { 163 | if (chars == null) 164 | throw new ArgumentNullException ("chars"); 165 | 166 | if (bytes == null) 167 | throw new ArgumentNullException ("bytes"); 168 | 169 | if (charCount < 0) 170 | throw new ArgumentOutOfRangeException ("charCount"); 171 | 172 | if (byteCount < 0) 173 | throw new ArgumentOutOfRangeException ("byteCount"); 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderExceptionFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderExceptionFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | namespace Portable.Text 31 | { 32 | public sealed class DecoderExceptionFallback : DecoderFallback 33 | { 34 | public DecoderExceptionFallback () 35 | { 36 | } 37 | 38 | public override int MaxCharCount { 39 | get { return 0; } 40 | } 41 | 42 | public override DecoderFallbackBuffer CreateFallbackBuffer () 43 | { 44 | return new DecoderExceptionFallbackBuffer (); 45 | } 46 | 47 | public override bool Equals (object obj) 48 | { 49 | return (obj is DecoderExceptionFallback); 50 | } 51 | 52 | public override int GetHashCode () 53 | { 54 | return 0; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderExceptionFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderExceptionFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer 34 | { 35 | public DecoderExceptionFallbackBuffer () 36 | { 37 | } 38 | 39 | public override int Remaining { 40 | get { return 0; } 41 | } 42 | 43 | public override bool Fallback (byte[] bytesUnknown, int index) 44 | { 45 | throw new DecoderFallbackException (null, bytesUnknown, index); 46 | } 47 | 48 | public override char GetNextChar () 49 | { 50 | return char.MinValue; 51 | } 52 | 53 | public override bool MovePrevious () 54 | { 55 | return false; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public abstract class DecoderFallback 34 | { 35 | static readonly DecoderFallback exception_fallback = new DecoderExceptionFallback (); 36 | static readonly DecoderFallback replacement_fallback = new DecoderReplacementFallback (); 37 | static readonly DecoderFallback standard_safe_fallback = new DecoderReplacementFallback ("\uFFFD"); 38 | 39 | protected DecoderFallback () 40 | { 41 | } 42 | 43 | public static DecoderFallback ExceptionFallback { 44 | get { return exception_fallback; } 45 | } 46 | 47 | public abstract int MaxCharCount { get; } 48 | 49 | public static DecoderFallback ReplacementFallback { 50 | get { return replacement_fallback; } 51 | } 52 | 53 | internal static DecoderFallback StandardSafeFallback { 54 | get { return standard_safe_fallback; } 55 | } 56 | 57 | public abstract DecoderFallbackBuffer CreateFallbackBuffer (); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public abstract class DecoderFallbackBuffer 34 | { 35 | protected DecoderFallbackBuffer () 36 | { 37 | } 38 | 39 | public abstract int Remaining { get; } 40 | 41 | public abstract bool Fallback (byte[] bytesUnknown, int index); 42 | 43 | public abstract char GetNextChar (); 44 | 45 | public abstract bool MovePrevious (); 46 | 47 | public virtual void Reset () 48 | { 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderFallbackException.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderFallbackException.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | using System; 32 | 33 | namespace Portable.Text 34 | { 35 | public sealed class DecoderFallbackException : ArgumentException 36 | { 37 | public DecoderFallbackException () 38 | : this (null) 39 | { 40 | } 41 | 42 | public DecoderFallbackException (string message) 43 | : base (message) 44 | { 45 | } 46 | 47 | public DecoderFallbackException (string message, Exception innerException) 48 | : base (message, innerException) 49 | { 50 | } 51 | 52 | public DecoderFallbackException (string message, byte[] bytesUnknown, int index) : base (message) 53 | { 54 | bytes_unknown = bytesUnknown; 55 | this.index = index; 56 | } 57 | 58 | byte[] bytes_unknown; 59 | int index = - 1; 60 | 61 | public byte[] BytesUnknown { 62 | get { return bytes_unknown; } 63 | } 64 | 65 | public int Index { 66 | get { return index; } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderReplacementFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderReplacementFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | using System; 32 | 33 | namespace Portable.Text 34 | { 35 | public sealed class DecoderReplacementFallback : DecoderFallback 36 | { 37 | readonly string replacement; 38 | 39 | public DecoderReplacementFallback () : this ("?") 40 | { 41 | } 42 | 43 | public DecoderReplacementFallback (string replacement) 44 | { 45 | if (replacement == null) 46 | throw new ArgumentNullException (); 47 | 48 | // FIXME: check replacement validity (invalid surrogate) 49 | 50 | this.replacement = replacement; 51 | } 52 | 53 | public string DefaultString { 54 | get { return replacement; } 55 | } 56 | 57 | public override int MaxCharCount { 58 | get { return replacement.Length; } 59 | } 60 | 61 | public override DecoderFallbackBuffer CreateFallbackBuffer () 62 | { 63 | return new DecoderReplacementFallbackBuffer (this); 64 | } 65 | 66 | public override bool Equals (object obj) 67 | { 68 | var fallback = obj as DecoderReplacementFallback; 69 | 70 | return fallback != null && replacement == fallback.replacement; 71 | } 72 | 73 | public override int GetHashCode () 74 | { 75 | return replacement.GetHashCode (); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/DecoderReplacementFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // DecoderReplacementFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | using System; 32 | 33 | namespace Portable.Text 34 | { 35 | // This DecoderFallbackBuffer is simple. It ignores the input buffers. 36 | // DecoderFallbackBuffer users could implement their own complex 37 | // fallback buffers. 38 | 39 | public sealed class DecoderReplacementFallbackBuffer : DecoderFallbackBuffer 40 | { 41 | bool fallback_assigned; 42 | int current; 43 | string replacement; 44 | 45 | public DecoderReplacementFallbackBuffer ( 46 | DecoderReplacementFallback fallback) 47 | { 48 | if (fallback == null) 49 | throw new ArgumentNullException ("fallback"); 50 | replacement = fallback.DefaultString; 51 | current = 0; 52 | } 53 | 54 | public override int Remaining { 55 | get { return fallback_assigned ? replacement.Length - current : 0; } 56 | } 57 | 58 | public override bool Fallback (byte[] bytesUnknown, int index) 59 | { 60 | if (bytesUnknown == null) 61 | throw new ArgumentNullException ("bytesUnknown"); 62 | 63 | if (fallback_assigned && Remaining != 0) 64 | throw new ArgumentException ("Reentrant Fallback method invocation occured. It might be because either this FallbackBuffer is incorrectly shared by multiple threads, invoked inside Encoding recursively, or Reset invocation is forgotten."); 65 | 66 | if (index < 0 || bytesUnknown.Length < index) 67 | throw new ArgumentOutOfRangeException ("index"); 68 | 69 | fallback_assigned = true; 70 | current = 0; 71 | 72 | return replacement.Length > 0; 73 | } 74 | 75 | public override char GetNextChar () 76 | { 77 | if (!fallback_assigned) 78 | return '\0'; 79 | 80 | if (current >= replacement.Length) 81 | return char.MinValue; 82 | 83 | return replacement [current++]; 84 | } 85 | 86 | public override bool MovePrevious () 87 | { 88 | if (current == 0) 89 | return false; 90 | 91 | current--; 92 | 93 | return true; 94 | } 95 | 96 | public override void Reset () 97 | { 98 | fallback_assigned = false; 99 | current = 0; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Encoder.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * Encoder.cs - Implementation of the "System.Text.Encoder" class. 3 | * 4 | * Copyright (c) 2001 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | using System; 26 | 27 | namespace Portable.Text 28 | { 29 | public abstract class Encoder 30 | #if !STANDALONE 31 | : System.Text.Encoder 32 | #endif 33 | { 34 | EncoderFallback fallback = new EncoderReplacementFallback (); 35 | EncoderFallbackBuffer fallback_buffer; 36 | 37 | // Constructor. 38 | protected Encoder () 39 | { 40 | } 41 | 42 | public EncoderFallback Fallback { 43 | get { return fallback; } 44 | set { 45 | if (value == null) 46 | throw new ArgumentNullException (); 47 | fallback = value; 48 | fallback_buffer = null; 49 | } 50 | } 51 | 52 | public EncoderFallbackBuffer FallbackBuffer { 53 | get { 54 | if (fallback_buffer == null) 55 | fallback_buffer = Fallback.CreateFallbackBuffer (); 56 | return fallback_buffer; 57 | } 58 | } 59 | 60 | #if STANDALONE 61 | // Get the number of bytes needed to encode a buffer. 62 | public abstract int GetByteCount(char[] chars, int index, int count, bool flush); 63 | 64 | // Get the bytes that result from decoding a buffer. 65 | public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush); 66 | #endif 67 | 68 | public virtual void Reset () 69 | { 70 | if (fallback_buffer != null) 71 | fallback_buffer.Reset (); 72 | } 73 | 74 | #if STANDALONE 75 | public virtual 76 | #else 77 | public override 78 | #endif 79 | void Convert (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed) 80 | { 81 | if (chars == null) 82 | throw new ArgumentNullException ("chars"); 83 | if (bytes == null) 84 | throw new ArgumentNullException ("bytes"); 85 | if (charIndex < 0) 86 | throw new ArgumentOutOfRangeException ("charIndex"); 87 | if (charCount < 0 || chars.Length < charIndex + charCount) 88 | throw new ArgumentOutOfRangeException ("charCount"); 89 | if (byteIndex < 0) 90 | throw new ArgumentOutOfRangeException ("byteIndex"); 91 | if (byteCount < 0 || bytes.Length < byteIndex + byteCount) 92 | throw new ArgumentOutOfRangeException ("byteCount"); 93 | 94 | charsUsed = charCount; 95 | while (true) { 96 | bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush); 97 | if (bytesUsed <= byteCount) 98 | break; 99 | 100 | flush = false; 101 | charsUsed >>= 1; 102 | } 103 | 104 | completed = charsUsed == charCount; 105 | bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush); 106 | } 107 | 108 | static unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount) 109 | { 110 | if (chars == null) 111 | throw new ArgumentNullException ("chars"); 112 | 113 | if (bytes == null) 114 | throw new ArgumentNullException ("bytes"); 115 | 116 | if (charCount < 0) 117 | throw new ArgumentOutOfRangeException ("charCount"); 118 | 119 | if (byteCount < 0) 120 | throw new ArgumentOutOfRangeException ("byteCount"); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderExceptionFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderExceptionFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public sealed class EncoderExceptionFallback : EncoderFallback 34 | { 35 | public EncoderExceptionFallback () 36 | { 37 | } 38 | 39 | public override int MaxCharCount { 40 | get { return 0; } 41 | } 42 | 43 | public override EncoderFallbackBuffer CreateFallbackBuffer () 44 | { 45 | return new EncoderExceptionFallbackBuffer (); 46 | } 47 | 48 | public override bool Equals (object obj) 49 | { 50 | return (obj is EncoderExceptionFallback); 51 | } 52 | 53 | public override int GetHashCode () 54 | { 55 | return 0; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderExceptionFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderExceptionFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public sealed class EncoderExceptionFallbackBuffer : EncoderFallbackBuffer 34 | { 35 | public EncoderExceptionFallbackBuffer () 36 | { 37 | } 38 | 39 | public override int Remaining { 40 | get { return 0; } 41 | } 42 | 43 | public override bool Fallback (char charUnknown, int index) 44 | { 45 | throw new EncoderFallbackException (charUnknown, index); 46 | } 47 | 48 | public override bool Fallback (char charUnknownHigh, char charUnknownLow, int index) 49 | { 50 | throw new EncoderFallbackException (charUnknownHigh, charUnknownLow, index); 51 | } 52 | 53 | public override char GetNextChar () 54 | { 55 | return char.MinValue; 56 | } 57 | 58 | public override bool MovePrevious () 59 | { 60 | return false; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public abstract class EncoderFallback 34 | { 35 | static readonly EncoderFallback exception_fallback = new EncoderExceptionFallback (); 36 | static readonly EncoderFallback replacement_fallback = new EncoderReplacementFallback (); 37 | static readonly EncoderFallback standard_safe_fallback = new EncoderReplacementFallback ("\uFFFD"); 38 | 39 | protected EncoderFallback () 40 | { 41 | } 42 | 43 | public static EncoderFallback ExceptionFallback { 44 | get { return exception_fallback; } 45 | } 46 | 47 | public abstract int MaxCharCount { get; } 48 | 49 | public static EncoderFallback ReplacementFallback { 50 | get { return replacement_fallback; } 51 | } 52 | 53 | internal static EncoderFallback StandardSafeFallback { 54 | get { return standard_safe_fallback; } 55 | } 56 | 57 | public abstract EncoderFallbackBuffer CreateFallbackBuffer (); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public abstract class EncoderFallbackBuffer 34 | { 35 | protected EncoderFallbackBuffer () 36 | { 37 | } 38 | 39 | public abstract int Remaining { get; } 40 | 41 | public abstract bool Fallback (char charUnknown, int index); 42 | 43 | public abstract bool Fallback (char charUnknownHigh, char charUnknownLow, int index); 44 | 45 | public abstract char GetNextChar (); 46 | 47 | public abstract bool MovePrevious (); 48 | 49 | public virtual void Reset () 50 | { 51 | char c; 52 | 53 | do { 54 | c = GetNextChar (); 55 | } while (c != '\0'); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderFallbackException.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderFallbackException.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | using System; 32 | 33 | namespace Portable.Text 34 | { 35 | public sealed class EncoderFallbackException : ArgumentException 36 | { 37 | char char_unknown, char_unknown_high, char_unknown_low; 38 | int index = - 1; 39 | 40 | public EncoderFallbackException () : this (null) 41 | { 42 | } 43 | 44 | public EncoderFallbackException (string message) : base (message) 45 | { 46 | } 47 | 48 | public EncoderFallbackException (string message, Exception innerException) : base (message, innerException) 49 | { 50 | } 51 | 52 | internal EncoderFallbackException (char charUnknown, int index) : base (null) 53 | { 54 | char_unknown = charUnknown; 55 | this.index = index; 56 | } 57 | 58 | internal EncoderFallbackException (char charUnknownHigh, char charUnknownLow, int index) : base (null) 59 | { 60 | char_unknown_high = charUnknownHigh; 61 | char_unknown_low = charUnknownLow; 62 | this.index = index; 63 | } 64 | 65 | public char CharUnknown { 66 | get { return char_unknown; } 67 | } 68 | 69 | public char CharUnknownHigh { 70 | get { return char_unknown_high; } 71 | } 72 | 73 | public char CharUnknownLow { 74 | get { return char_unknown_low; } 75 | } 76 | 77 | public int Index { 78 | get { return index; } 79 | } 80 | 81 | public bool IsUnknownSurrogate () 82 | { 83 | throw new NotImplementedException (); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderReplacementFallback.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderReplacementFallback.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | using System; 32 | 33 | namespace Portable.Text 34 | { 35 | public sealed class EncoderReplacementFallback : EncoderFallback 36 | { 37 | readonly string replacement; 38 | 39 | public EncoderReplacementFallback () : this ("?") 40 | { 41 | } 42 | 43 | public EncoderReplacementFallback (string replacement) 44 | { 45 | if (replacement == null) 46 | throw new ArgumentNullException (); 47 | 48 | // FIXME: check replacement validity (invalid surrogate) 49 | 50 | this.replacement = replacement; 51 | } 52 | 53 | public string DefaultString { 54 | get { return replacement; } 55 | } 56 | 57 | public override int MaxCharCount { 58 | get { return replacement.Length; } 59 | } 60 | 61 | public override EncoderFallbackBuffer CreateFallbackBuffer () 62 | { 63 | return new EncoderReplacementFallbackBuffer (this); 64 | } 65 | 66 | public override bool Equals (object obj) 67 | { 68 | var fallback = obj as EncoderReplacementFallback; 69 | 70 | return fallback != null && replacement == fallback.replacement; 71 | } 72 | 73 | public override int GetHashCode () 74 | { 75 | return replacement.GetHashCode (); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncoderReplacementFallbackBuffer.cs: -------------------------------------------------------------------------------- 1 | // 2 | // EncoderReplacementFallbackBuffer.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // Copyright 2011 Xamarin Inc (http://www.xamarin.com). 11 | // 12 | // Permission is hereby granted, free of charge, to any person obtaining 13 | // a copy of this software and associated documentation files (the 14 | // "Software"), to deal in the Software without restriction, including 15 | // without limitation the rights to use, copy, modify, merge, publish, 16 | // distribute, sublicense, and/or sell copies of the Software, and to 17 | // permit persons to whom the Software is furnished to do so, subject to 18 | // the following conditions: 19 | // 20 | // The above copyright notice and this permission notice shall be 21 | // included in all copies or substantial portions of the Software. 22 | // 23 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 27 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 28 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 29 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | // 31 | 32 | using System; 33 | 34 | namespace Portable.Text 35 | { 36 | // This EncoderFallbackBuffer is simple. It ignores the input buffers. 37 | // EncoderFallbackBuffer users could implement their own complex 38 | // fallback buffers. 39 | 40 | public sealed class EncoderReplacementFallbackBuffer : EncoderFallbackBuffer 41 | { 42 | bool fallback_assigned; 43 | string replacement; 44 | int current; 45 | 46 | public EncoderReplacementFallbackBuffer (EncoderReplacementFallback fallback) 47 | { 48 | if (fallback == null) 49 | throw new ArgumentNullException ("fallback"); 50 | 51 | replacement = fallback.DefaultString; 52 | current = 0; 53 | } 54 | 55 | public override int Remaining { 56 | get { return replacement.Length - current; } 57 | } 58 | 59 | public override bool Fallback (char charUnknown, int index) 60 | { 61 | return Fallback (index); 62 | } 63 | 64 | public override bool Fallback (char charUnknownHigh, char charUnknownLow, int index) 65 | { 66 | return Fallback (index); 67 | } 68 | 69 | // hmm, what is this index for??? 70 | bool Fallback (int index) 71 | { 72 | if (fallback_assigned && Remaining != 0) 73 | throw new ArgumentException ("Reentrant Fallback method invocation occured. It might be because either this FallbackBuffer is incorrectly shared by multiple threads, invoked inside Encoding recursively, or Reset invocation is forgotten."); 74 | 75 | if (index < 0) 76 | throw new ArgumentOutOfRangeException ("index"); 77 | 78 | fallback_assigned = true; 79 | current = 0; 80 | 81 | return replacement.Length > 0; 82 | } 83 | 84 | public override char GetNextChar () 85 | { 86 | if (current >= replacement.Length) 87 | return char.MinValue; 88 | 89 | return replacement [current++]; 90 | } 91 | 92 | public override bool MovePrevious () 93 | { 94 | if (current == 0) 95 | return false; 96 | 97 | current--; 98 | 99 | return true; 100 | } 101 | 102 | public override void Reset () 103 | { 104 | fallback_assigned = false; 105 | current = 0; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/EncodingInfo.cs: -------------------------------------------------------------------------------- 1 | // 2 | // System.Text.EncodingInfo.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2006 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | 32 | namespace Portable.Text 33 | { 34 | public sealed class EncodingInfo 35 | { 36 | readonly int codepage; 37 | Encoding encoding; 38 | 39 | internal EncodingInfo (int cp) 40 | { 41 | codepage = cp; 42 | } 43 | 44 | public int CodePage { 45 | get { return codepage; } 46 | } 47 | 48 | public string DisplayName { 49 | get { return Name; } 50 | } 51 | 52 | public string Name { 53 | get { 54 | if (encoding == null) 55 | encoding = GetEncoding (); 56 | return encoding.WebName; 57 | } 58 | } 59 | 60 | public override bool Equals (object obj) 61 | { 62 | var info = obj as EncodingInfo; 63 | 64 | return info != null && info.codepage == codepage; 65 | } 66 | 67 | public override int GetHashCode () 68 | { 69 | return codepage; 70 | } 71 | 72 | public Encoding GetEncoding () 73 | { 74 | return Encoding.GetEncoding (codepage); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/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 | using System; 27 | 28 | namespace Portable.Text 29 | { 30 | class Latin1Encoding : Encoding 31 | { 32 | // Magic number used by Windows for the ISO Latin1 code page. 33 | internal const int ISOLATIN_CODE_PAGE = 28591; 34 | 35 | // Constructor. 36 | public Latin1Encoding () : base (ISOLATIN_CODE_PAGE) 37 | { 38 | // Nothing to do here. 39 | } 40 | 41 | public override bool IsSingleByte { 42 | get { return true; } 43 | } 44 | 45 | public override bool IsAlwaysNormalized (NormalizationForm form) 46 | { 47 | return form == NormalizationForm.FormC; 48 | } 49 | 50 | // Get the number of bytes needed to encode a character buffer. 51 | public override int GetByteCount (char[] chars, int index, int count) 52 | { 53 | if (chars == null) 54 | throw new ArgumentNullException ("chars"); 55 | 56 | if (index < 0 || index > chars.Length) 57 | throw new ArgumentOutOfRangeException ("index"); 58 | 59 | if (count < 0 || count > (chars.Length - index)) 60 | throw new ArgumentOutOfRangeException ("count"); 61 | 62 | return count; 63 | } 64 | 65 | // Convenience wrappers for "GetByteCount". 66 | public override int GetByteCount (string s) 67 | { 68 | if (s == null) 69 | throw new ArgumentNullException ("s"); 70 | 71 | return s.Length; 72 | } 73 | 74 | // Get the bytes that result from encoding a character buffer. 75 | public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) 76 | { 77 | EncoderFallbackBuffer buffer = null; 78 | char[] fallback_chars = null; 79 | 80 | return GetBytes (chars, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars); 81 | } 82 | 83 | int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, ref EncoderFallbackBuffer buffer, ref char[] fallback_chars) 84 | { 85 | if (chars == null) 86 | throw new ArgumentNullException ("chars"); 87 | 88 | unsafe { 89 | fixed (char *cptr = chars) { 90 | return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars); 91 | } 92 | } 93 | } 94 | 95 | // Convenience wrappers for "GetBytes". 96 | public override int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex) 97 | { 98 | EncoderFallbackBuffer buffer = null; 99 | char[] fallback_chars = null; 100 | 101 | return GetBytes (s, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars); 102 | } 103 | 104 | int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex, ref EncoderFallbackBuffer buffer, ref char[] fallback_chars) 105 | { 106 | if (s == null) 107 | throw new ArgumentNullException ("s"); 108 | 109 | unsafe { 110 | fixed (char *cptr = s) { 111 | return InternalGetBytes (cptr, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars); 112 | } 113 | } 114 | } 115 | 116 | unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount, byte[] bytes, int byteIndex, ref EncoderFallbackBuffer buffer, ref char[] fallback_chars) 117 | { 118 | if (bytes == null) 119 | throw new ArgumentNullException ("bytes"); 120 | 121 | if (charIndex < 0 || charIndex > charLength) 122 | throw new ArgumentOutOfRangeException ("charIndex"); 123 | 124 | if (charCount < 0 || charCount > (charLength - charIndex)) 125 | throw new ArgumentOutOfRangeException ("charCount"); 126 | 127 | if (byteIndex < 0 || byteIndex > bytes.Length) 128 | throw new ArgumentOutOfRangeException ("byteIndex"); 129 | 130 | if ((bytes.Length - byteIndex) < charCount) 131 | throw new ArgumentException ("Insufficient space available."); 132 | 133 | int count = charCount; 134 | char ch; 135 | 136 | while (count-- > 0) { 137 | ch = chars [charIndex++]; 138 | if (ch < (char) 0x0100) { 139 | bytes [byteIndex++] = (byte)ch; 140 | } else if (ch >= '\uFF01' && ch <= '\uFF5E') { 141 | bytes [byteIndex++] = (byte)(ch - 0xFEE0); 142 | } else { 143 | if (buffer == null) 144 | buffer = EncoderFallback.CreateFallbackBuffer (); 145 | 146 | if (Char.IsSurrogate (ch) && count > 1 && Char.IsSurrogate (chars [charIndex])) 147 | buffer.Fallback (ch, chars [charIndex], charIndex++ - 1); 148 | else 149 | buffer.Fallback (ch, charIndex - 1); 150 | 151 | if (fallback_chars == null || fallback_chars.Length < buffer.Remaining) 152 | fallback_chars = new char [buffer.Remaining]; 153 | 154 | for (int i = 0; i < fallback_chars.Length; i++) 155 | fallback_chars [i] = buffer.GetNextChar (); 156 | 157 | byteIndex += GetBytes (fallback_chars, 0, fallback_chars.Length, bytes, byteIndex, ref buffer, ref fallback_chars); 158 | } 159 | } 160 | 161 | return charCount; 162 | } 163 | 164 | // Get the number of characters needed to decode a byte buffer. 165 | public override int GetCharCount (byte[] bytes, int index, int count) 166 | { 167 | if (bytes == null) 168 | throw new ArgumentNullException ("bytes"); 169 | 170 | if (index < 0 || index > bytes.Length) 171 | throw new ArgumentOutOfRangeException ("index"); 172 | 173 | if (count < 0 || count > (bytes.Length - index)) 174 | throw new ArgumentOutOfRangeException ("count"); 175 | 176 | return count; 177 | } 178 | 179 | // Get the characters that result from decoding a byte buffer. 180 | public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 181 | { 182 | if (bytes == null) 183 | throw new ArgumentNullException ("bytes"); 184 | 185 | if (chars == null) 186 | throw new ArgumentNullException ("chars"); 187 | 188 | if (byteIndex < 0 || byteIndex > bytes.Length) 189 | throw new ArgumentOutOfRangeException ("byteIndex"); 190 | 191 | if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 192 | throw new ArgumentOutOfRangeException ("byteCount"); 193 | 194 | if (charIndex < 0 || charIndex > chars.Length) 195 | throw new ArgumentOutOfRangeException ("charIndex"); 196 | 197 | if ((chars.Length - charIndex) < byteCount) 198 | throw new ArgumentException ("Insufficient space available."); 199 | 200 | int count = byteCount; 201 | while (count-- > 0) 202 | chars [charIndex++] = (char) bytes [byteIndex++]; 203 | 204 | return byteCount; 205 | } 206 | 207 | // Get the maximum number of bytes needed to encode a 208 | // specified number of characters. 209 | public override int GetMaxByteCount (int charCount) 210 | { 211 | if (charCount < 0) 212 | throw new ArgumentOutOfRangeException ("charCount"); 213 | 214 | return charCount; 215 | } 216 | 217 | // Get the maximum number of characters needed to decode a 218 | // specified number of bytes. 219 | public override int GetMaxCharCount (int byteCount) 220 | { 221 | if (byteCount < 0) 222 | throw new ArgumentOutOfRangeException ("byteCount"); 223 | 224 | return byteCount; 225 | } 226 | 227 | // Decode a buffer of bytes into a string. 228 | public override string GetString (byte[] bytes, int index, int count) 229 | { 230 | if (bytes == null) 231 | throw new ArgumentNullException ("bytes"); 232 | 233 | if (index < 0 || index > bytes.Length) 234 | throw new ArgumentOutOfRangeException ("index"); 235 | 236 | if (count < 0 || count > (bytes.Length - index)) 237 | throw new ArgumentOutOfRangeException ("count"); 238 | 239 | if (count == 0) 240 | return string.Empty; 241 | 242 | unsafe { 243 | fixed (byte* bytePtr = bytes) { 244 | var chars = new char[count]; 245 | 246 | fixed (char* charPtr = chars) { 247 | byte* currByte = bytePtr + index; 248 | byte* lastByte = currByte + count; 249 | char* currChar = charPtr; 250 | 251 | while (currByte < lastByte) 252 | *currChar++ = (char) *currByte++; 253 | } 254 | 255 | return new string (chars); 256 | } 257 | } 258 | } 259 | 260 | public override string GetString (byte[] bytes) 261 | { 262 | if (bytes == null) 263 | throw new ArgumentNullException ("bytes"); 264 | 265 | return GetString (bytes, 0, bytes.Length); 266 | } 267 | 268 | // Get the mail body name for this encoding. 269 | public override string BodyName { 270 | get { return "iso-8859-1"; } 271 | } 272 | 273 | // Get the human-readable name for this encoding. 274 | public override string EncodingName { 275 | get { return "Western European (ISO)"; } 276 | } 277 | 278 | // Get the mail agent header name for this encoding. 279 | public override string HeaderName { 280 | get { return "iso-8859-1"; } 281 | } 282 | 283 | // Determine if this encoding can be displayed in a Web browser. 284 | public override bool IsBrowserDisplay { 285 | get { return true; } 286 | } 287 | 288 | // Determine if this encoding can be saved from a Web browser. 289 | public override bool IsBrowserSave { 290 | get { return true; } 291 | } 292 | 293 | // Determine if this encoding can be displayed in a mail/news agent. 294 | public override bool IsMailNewsDisplay { 295 | get { return true; } 296 | } 297 | 298 | // Determine if this encoding can be saved from a mail/news agent. 299 | public override bool IsMailNewsSave { 300 | get { return true; } 301 | } 302 | 303 | // Get the IANA-preferred Web name for this encoding. 304 | public override string WebName { 305 | get { return "iso-8859-1"; } 306 | } 307 | } 308 | } 309 | 310 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/MidEast/CP1255.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP1255.cs - Hebrew (Windows) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-5351.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP1255 : ByteEncoding 34 | { 35 | public CP1255 () 36 | : base (1255, ToChars, "Hebrew (Windows)", 37 | "windows-1255", "windows-1255", "windows-1255", 38 | true, true, true, true, 1255) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u20AC', '\u0081', '\u201A', '\u0192', 65 | '\u201E', '\u2026', '\u2020', '\u2021', '\u02C6', '\u2030', 66 | '\u008A', '\u2039', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u2018', '\u2019', '\u201C', '\u201D', '\u2022', 68 | '\u2013', '\u2014', '\u02DC', '\u2122', '\u009A', '\u203A', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u00A1', 70 | '\u00A2', '\u00A3', '\u20AA', '\u00A5', '\u00A6', '\u00A7', 71 | '\u00A8', '\u00A9', '\u00D7', '\u00AB', '\u00AC', '\u00AD', 72 | '\u00AE', '\u00AF', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', 74 | '\u00F7', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u00BF', 75 | '\u05B0', '\u05B1', '\u05B2', '\u05B3', '\u05B4', '\u05B5', 76 | '\u05B6', '\u05B7', '\u05B8', '\u05B9', '\u003F', '\u05BB', 77 | '\u05BC', '\u05BD', '\u05BE', '\u05BF', '\u05C0', '\u05C1', 78 | '\u05C2', '\u05C3', '\u05F0', '\u05F1', '\u05F2', '\u05F3', 79 | '\u05F4', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 80 | '\u003F', '\u003F', '\u05D0', '\u05D1', '\u05D2', '\u05D3', 81 | '\u05D4', '\u05D5', '\u05D6', '\u05D7', '\u05D8', '\u05D9', 82 | '\u05DA', '\u05DB', '\u05DC', '\u05DD', '\u05DE', '\u05DF', 83 | '\u05E0', '\u05E1', '\u05E2', '\u05E3', '\u05E4', '\u05E5', 84 | '\u05E6', '\u05E7', '\u05E8', '\u05E9', '\u05EA', '\u003F', 85 | '\u003F', '\u200E', '\u200F', '\u003F', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int count) 89 | { 90 | if (this.EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, count, null, 0); 93 | } else { 94 | return count; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (this.EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 128) 135 | switch (ch) { 136 | case 0x0081: 137 | case 0x008A: 138 | case 0x008C: 139 | case 0x008D: 140 | case 0x008E: 141 | case 0x008F: 142 | case 0x0090: 143 | case 0x009A: 144 | case 0x009C: 145 | case 0x009D: 146 | case 0x009E: 147 | case 0x009F: 148 | case 0x00A0: 149 | case 0x00A1: 150 | case 0x00A2: 151 | case 0x00A3: 152 | case 0x00A5: 153 | case 0x00A6: 154 | case 0x00A7: 155 | case 0x00A8: 156 | case 0x00A9: 157 | case 0x00AB: 158 | case 0x00AC: 159 | case 0x00AD: 160 | case 0x00AE: 161 | case 0x00AF: 162 | case 0x00B0: 163 | case 0x00B1: 164 | case 0x00B2: 165 | case 0x00B3: 166 | case 0x00B4: 167 | case 0x00B5: 168 | case 0x00B6: 169 | case 0x00B7: 170 | case 0x00B8: 171 | case 0x00B9: 172 | case 0x00BB: 173 | case 0x00BC: 174 | case 0x00BD: 175 | case 0x00BE: 176 | case 0x00BF: 177 | break; 178 | case 0x00D7: 179 | ch = 0xAA; 180 | break; 181 | case 0x00F7: 182 | ch = 0xBA; 183 | break; 184 | case 0x0192: 185 | ch = 0x83; 186 | break; 187 | case 0x02C6: 188 | ch = 0x88; 189 | break; 190 | case 0x02DC: 191 | ch = 0x98; 192 | break; 193 | case 0x05B0: 194 | case 0x05B1: 195 | case 0x05B2: 196 | case 0x05B3: 197 | case 0x05B4: 198 | case 0x05B5: 199 | case 0x05B6: 200 | case 0x05B7: 201 | case 0x05B8: 202 | case 0x05B9: 203 | ch -= 0x04F0; 204 | break; 205 | case 0x05BB: 206 | case 0x05BC: 207 | case 0x05BD: 208 | case 0x05BE: 209 | case 0x05BF: 210 | case 0x05C0: 211 | case 0x05C1: 212 | case 0x05C2: 213 | case 0x05C3: 214 | ch -= 0x04F0; 215 | break; 216 | case 0x05D0: 217 | case 0x05D1: 218 | case 0x05D2: 219 | case 0x05D3: 220 | case 0x05D4: 221 | case 0x05D5: 222 | case 0x05D6: 223 | case 0x05D7: 224 | case 0x05D8: 225 | case 0x05D9: 226 | case 0x05DA: 227 | case 0x05DB: 228 | case 0x05DC: 229 | case 0x05DD: 230 | case 0x05DE: 231 | case 0x05DF: 232 | case 0x05E0: 233 | case 0x05E1: 234 | case 0x05E2: 235 | case 0x05E3: 236 | case 0x05E4: 237 | case 0x05E5: 238 | case 0x05E6: 239 | case 0x05E7: 240 | case 0x05E8: 241 | case 0x05E9: 242 | case 0x05EA: 243 | ch -= 0x04F0; 244 | break; 245 | case 0x05F0: 246 | case 0x05F1: 247 | case 0x05F2: 248 | case 0x05F3: 249 | case 0x05F4: 250 | ch -= 0x051C; 251 | break; 252 | case 0x200E: 253 | ch = 0xFD; 254 | break; 255 | case 0x200F: 256 | ch = 0xFE; 257 | break; 258 | case 0x2013: 259 | ch = 0x96; 260 | break; 261 | case 0x2014: 262 | ch = 0x97; 263 | break; 264 | case 0x2018: 265 | ch = 0x91; 266 | break; 267 | case 0x2019: 268 | ch = 0x92; 269 | break; 270 | case 0x201A: 271 | ch = 0x82; 272 | break; 273 | case 0x201C: 274 | ch = 0x93; 275 | break; 276 | case 0x201D: 277 | ch = 0x94; 278 | break; 279 | case 0x201E: 280 | ch = 0x84; 281 | break; 282 | case 0x2020: 283 | ch = 0x86; 284 | break; 285 | case 0x2021: 286 | ch = 0x87; 287 | break; 288 | case 0x2022: 289 | ch = 0x95; 290 | break; 291 | case 0x2026: 292 | ch = 0x85; 293 | break; 294 | case 0x2030: 295 | ch = 0x89; 296 | break; 297 | case 0x2039: 298 | ch = 0x8B; 299 | break; 300 | case 0x203A: 301 | ch = 0x9B; 302 | break; 303 | case 0x20AA: 304 | ch = 0xA4; 305 | break; 306 | case 0x20AC: 307 | ch = 0x80; 308 | break; 309 | case 0x2122: 310 | ch = 0x99; 311 | break; 312 | default: 313 | { 314 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 315 | ch -= 0xFEE0; 316 | } else { 317 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 318 | continue; 319 | } 320 | } 321 | break; 322 | } 323 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 324 | if (bytes != null) 325 | bytes [byteIndex] = (byte)ch; 326 | byteIndex++; 327 | byteCount--; 328 | } 329 | return byteIndex; 330 | } 331 | } 332 | 333 | // class CP1255 334 | class ENCwindows_1255 : CP1255 335 | { 336 | public ENCwindows_1255 () : base () 337 | { 338 | } 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/MidEast/CP28598.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28598.cs - Hebrew (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-916.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28598 : ByteEncoding 34 | { 35 | public CP28598 () 36 | : base (28598, ToChars, "Hebrew (ISO)", 37 | "iso-8859-8", "iso-8859-8", "iso-8859-8", 38 | true, true, true, true, 1255) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u003F', 70 | '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', 71 | '\u00A8', '\u00A9', '\u00D7', '\u00AB', '\u00AC', '\u00AD', 72 | '\u00AE', '\u203E', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u00B4', '\u00B5', '\u00B6', '\u2022', '\u00B8', '\u00B9', 74 | '\u00F7', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u003F', 75 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 76 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 77 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 78 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 79 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 80 | '\u003F', '\u2017', '\u05D0', '\u05D1', '\u05D2', '\u05D3', 81 | '\u05D4', '\u05D5', '\u05D6', '\u05D7', '\u05D8', '\u05D9', 82 | '\u05DA', '\u05DB', '\u05DC', '\u05DD', '\u05DE', '\u05DF', 83 | '\u05E0', '\u05E1', '\u05E2', '\u05E3', '\u05E4', '\u05E5', 84 | '\u05E6', '\u05E7', '\u05E8', '\u05E9', '\u05EA', '\u003F', 85 | '\u003F', '\u003F', '\u003F', '\u003F', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int count) 89 | { 90 | if (this.EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, count, null, 0); 93 | } else { 94 | return count; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (this.EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 161) 135 | switch (ch) { 136 | case 0x00A2: 137 | case 0x00A3: 138 | case 0x00A4: 139 | case 0x00A5: 140 | case 0x00A6: 141 | case 0x00A7: 142 | case 0x00A8: 143 | case 0x00A9: 144 | case 0x00AB: 145 | case 0x00AC: 146 | case 0x00AD: 147 | case 0x00AE: 148 | case 0x00B0: 149 | case 0x00B1: 150 | case 0x00B2: 151 | case 0x00B3: 152 | case 0x00B4: 153 | case 0x00B5: 154 | case 0x00B6: 155 | case 0x00B8: 156 | case 0x00B9: 157 | case 0x00BB: 158 | case 0x00BC: 159 | case 0x00BD: 160 | case 0x00BE: 161 | break; 162 | case 0x00D7: 163 | ch = 0xAA; 164 | break; 165 | case 0x00F7: 166 | ch = 0xBA; 167 | break; 168 | case 0x05D0: 169 | case 0x05D1: 170 | case 0x05D2: 171 | case 0x05D3: 172 | case 0x05D4: 173 | case 0x05D5: 174 | case 0x05D6: 175 | case 0x05D7: 176 | case 0x05D8: 177 | case 0x05D9: 178 | case 0x05DA: 179 | case 0x05DB: 180 | case 0x05DC: 181 | case 0x05DD: 182 | case 0x05DE: 183 | case 0x05DF: 184 | case 0x05E0: 185 | case 0x05E1: 186 | case 0x05E2: 187 | case 0x05E3: 188 | case 0x05E4: 189 | case 0x05E5: 190 | case 0x05E6: 191 | case 0x05E7: 192 | case 0x05E8: 193 | case 0x05E9: 194 | case 0x05EA: 195 | ch -= 0x04F0; 196 | break; 197 | case 0x2017: 198 | ch = 0xDF; 199 | break; 200 | case 0x2022: 201 | ch = 0xB7; 202 | break; 203 | case 0x203E: 204 | ch = 0xAF; 205 | break; 206 | default: 207 | { 208 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 209 | ch -= 0xFEE0; 210 | } else { 211 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 212 | continue; 213 | } 214 | } 215 | break; 216 | } 217 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 218 | if (bytes != null) 219 | bytes [byteIndex] = (byte)ch; 220 | byteIndex++; 221 | byteCount--; 222 | } 223 | return byteIndex; 224 | } 225 | } 226 | 227 | // class CP28598 228 | class ENCiso_8859_8 : CP28598 229 | { 230 | public ENCiso_8859_8 () : base () 231 | { 232 | } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/MidEast/CP28599.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28599.cs - Latin 5 (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-920.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28599 : ByteEncoding 34 | { 35 | public CP28599 () 36 | : base (28599, ToChars, "Latin 5 (ISO)", 37 | "iso-8859-9", "iso-8859-9", "iso-8859-9", 38 | true, true, true, true, 1254) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u00A1', 70 | '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', 71 | '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', 72 | '\u00AE', '\u00AF', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', 74 | '\u00BA', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u00BF', 75 | '\u00C0', '\u00C1', '\u00C2', '\u00C3', '\u00C4', '\u00C5', 76 | '\u00C6', '\u00C7', '\u00C8', '\u00C9', '\u00CA', '\u00CB', 77 | '\u00CC', '\u00CD', '\u00CE', '\u00CF', '\u011E', '\u00D1', 78 | '\u00D2', '\u00D3', '\u00D4', '\u00D5', '\u00D6', '\u00D7', 79 | '\u00D8', '\u00D9', '\u00DA', '\u00DB', '\u00DC', '\u0130', 80 | '\u015E', '\u00DF', '\u00E0', '\u00E1', '\u00E2', '\u00E3', 81 | '\u00E4', '\u00E5', '\u00E6', '\u00E7', '\u00E8', '\u00E9', 82 | '\u00EA', '\u00EB', '\u00EC', '\u00ED', '\u00EE', '\u00EF', 83 | '\u011F', '\u00F1', '\u00F2', '\u00F3', '\u00F4', '\u00F5', 84 | '\u00F6', '\u00F7', '\u00F8', '\u00F9', '\u00FA', '\u00FB', 85 | '\u00FC', '\u0131', '\u015F', '\u00FF', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int count) 89 | { 90 | if (this.EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, count, null, 0); 93 | } else { 94 | return count; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (this.EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 208) 135 | switch (ch) { 136 | case 0x00D1: 137 | case 0x00D2: 138 | case 0x00D3: 139 | case 0x00D4: 140 | case 0x00D5: 141 | case 0x00D6: 142 | case 0x00D7: 143 | case 0x00D8: 144 | case 0x00D9: 145 | case 0x00DA: 146 | case 0x00DB: 147 | case 0x00DC: 148 | case 0x00DF: 149 | case 0x00E0: 150 | case 0x00E1: 151 | case 0x00E2: 152 | case 0x00E3: 153 | case 0x00E4: 154 | case 0x00E5: 155 | case 0x00E6: 156 | case 0x00E7: 157 | case 0x00E8: 158 | case 0x00E9: 159 | case 0x00EA: 160 | case 0x00EB: 161 | case 0x00EC: 162 | case 0x00ED: 163 | case 0x00EE: 164 | case 0x00EF: 165 | case 0x00F1: 166 | case 0x00F2: 167 | case 0x00F3: 168 | case 0x00F4: 169 | case 0x00F5: 170 | case 0x00F6: 171 | case 0x00F7: 172 | case 0x00F8: 173 | case 0x00F9: 174 | case 0x00FA: 175 | case 0x00FB: 176 | case 0x00FC: 177 | case 0x00FF: 178 | break; 179 | case 0x011E: 180 | ch = 0xD0; 181 | break; 182 | case 0x011F: 183 | ch = 0xF0; 184 | break; 185 | case 0x0130: 186 | ch = 0xDD; 187 | break; 188 | case 0x0131: 189 | ch = 0xFD; 190 | break; 191 | case 0x015E: 192 | ch = 0xDE; 193 | break; 194 | case 0x015F: 195 | ch = 0xFE; 196 | break; 197 | case 0x203E: 198 | ch = 0xAF; 199 | break; 200 | default: 201 | { 202 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 203 | ch -= 0xFEE0; 204 | } else { 205 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 206 | continue; 207 | } 208 | } 209 | break; 210 | } 211 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 212 | if (bytes != null) 213 | bytes [byteIndex] = (byte)ch; 214 | byteIndex++; 215 | byteCount--; 216 | } 217 | return byteIndex; 218 | } 219 | } 220 | 221 | // class CP28599 222 | class ENCiso_8859_9 : CP28599 223 | { 224 | public ENCiso_8859_9 () : base () 225 | { 226 | } 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/MidEast/CP38598.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP38598.cs - Hebrew (ISO Alternative) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-916.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP38598 : ByteEncoding 34 | { 35 | public CP38598 () 36 | : base (38598, ToChars, "Hebrew (ISO Alternative)", 37 | "iso-8859-8", "windows-38598", "windows-38598", 38 | true, true, true, true, 1255) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u003F', 70 | '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', 71 | '\u00A8', '\u00A9', '\u00D7', '\u00AB', '\u00AC', '\u00AD', 72 | '\u00AE', '\u203E', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u00B4', '\u00B5', '\u00B6', '\u2022', '\u00B8', '\u00B9', 74 | '\u00F7', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u003F', 75 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 76 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 77 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 78 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 79 | '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', '\u003F', 80 | '\u003F', '\u2017', '\u05D0', '\u05D1', '\u05D2', '\u05D3', 81 | '\u05D4', '\u05D5', '\u05D6', '\u05D7', '\u05D8', '\u05D9', 82 | '\u05DA', '\u05DB', '\u05DC', '\u05DD', '\u05DE', '\u05DF', 83 | '\u05E0', '\u05E1', '\u05E2', '\u05E3', '\u05E4', '\u05E5', 84 | '\u05E6', '\u05E7', '\u05E8', '\u05E9', '\u05EA', '\u003F', 85 | '\u003F', '\u003F', '\u003F', '\u003F', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int count) 89 | { 90 | if (this.EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, count, null, 0); 93 | } else { 94 | return count; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (this.EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 161) 135 | switch (ch) { 136 | case 0x00A2: 137 | case 0x00A3: 138 | case 0x00A4: 139 | case 0x00A5: 140 | case 0x00A6: 141 | case 0x00A7: 142 | case 0x00A8: 143 | case 0x00A9: 144 | case 0x00AB: 145 | case 0x00AC: 146 | case 0x00AD: 147 | case 0x00AE: 148 | case 0x00B0: 149 | case 0x00B1: 150 | case 0x00B2: 151 | case 0x00B3: 152 | case 0x00B4: 153 | case 0x00B5: 154 | case 0x00B6: 155 | case 0x00B8: 156 | case 0x00B9: 157 | case 0x00BB: 158 | case 0x00BC: 159 | case 0x00BD: 160 | case 0x00BE: 161 | break; 162 | case 0x00D7: 163 | ch = 0xAA; 164 | break; 165 | case 0x00F7: 166 | ch = 0xBA; 167 | break; 168 | case 0x05D0: 169 | case 0x05D1: 170 | case 0x05D2: 171 | case 0x05D3: 172 | case 0x05D4: 173 | case 0x05D5: 174 | case 0x05D6: 175 | case 0x05D7: 176 | case 0x05D8: 177 | case 0x05D9: 178 | case 0x05DA: 179 | case 0x05DB: 180 | case 0x05DC: 181 | case 0x05DD: 182 | case 0x05DE: 183 | case 0x05DF: 184 | case 0x05E0: 185 | case 0x05E1: 186 | case 0x05E2: 187 | case 0x05E3: 188 | case 0x05E4: 189 | case 0x05E5: 190 | case 0x05E6: 191 | case 0x05E7: 192 | case 0x05E8: 193 | case 0x05E9: 194 | case 0x05EA: 195 | ch -= 0x04F0; 196 | break; 197 | case 0x2017: 198 | ch = 0xDF; 199 | break; 200 | case 0x2022: 201 | ch = 0xB7; 202 | break; 203 | case 0x203E: 204 | ch = 0xAF; 205 | break; 206 | default: 207 | { 208 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 209 | ch -= 0xFEE0; 210 | } else { 211 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 212 | continue; 213 | } 214 | } 215 | break; 216 | } 217 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 218 | if (bytes != null) 219 | bytes [byteIndex] = (byte)ch; 220 | byteIndex++; 221 | byteCount--; 222 | } 223 | return byteIndex; 224 | } 225 | } 226 | 227 | // class CP38598 228 | class ENCwindows_38598 : CP38598 229 | { 230 | public ENCwindows_38598 () : base () 231 | { 232 | } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/NormalizationForm.cs: -------------------------------------------------------------------------------- 1 | // 2 | // System.Text.NormalizationForm.cs 3 | // 4 | // Author: 5 | // Atsushi Enomoto 6 | // 7 | 8 | // 9 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining 12 | // a copy of this software and associated documentation files (the 13 | // "Software"), to deal in the Software without restriction, including 14 | // without limitation the rights to use, copy, modify, merge, publish, 15 | // distribute, sublicense, and/or sell copies of the Software, and to 16 | // permit persons to whom the Software is furnished to do so, subject to 17 | // the following conditions: 18 | // 19 | // The above copyright notice and this permission notice shall be 20 | // included in all copies or substantial portions of the Software. 21 | // 22 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | // 30 | 31 | namespace Portable.Text 32 | { 33 | public enum NormalizationForm { 34 | FormC = 1, 35 | FormD = 2, 36 | FormKC = 5, 37 | FormKD = 6 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Other/CP28595.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28595.cs - Cyrillic (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-915.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28595 : ByteEncoding 34 | { 35 | public CP28595 () 36 | : base (28595, ToChars, "Cyrillic (ISO)", 37 | "iso-8859-5", "iso-8859-5", "iso-8859-5", 38 | true, true, true, true, 1251) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u0401', 70 | '\u0402', '\u0403', '\u0404', '\u0405', '\u0406', '\u0407', 71 | '\u0408', '\u0409', '\u040A', '\u040B', '\u040C', '\u00AD', 72 | '\u040E', '\u040F', '\u0410', '\u0411', '\u0412', '\u0413', 73 | '\u0414', '\u0415', '\u0416', '\u0417', '\u0418', '\u0419', 74 | '\u041A', '\u041B', '\u041C', '\u041D', '\u041E', '\u041F', 75 | '\u0420', '\u0421', '\u0422', '\u0423', '\u0424', '\u0425', 76 | '\u0426', '\u0427', '\u0428', '\u0429', '\u042A', '\u042B', 77 | '\u042C', '\u042D', '\u042E', '\u042F', '\u0430', '\u0431', 78 | '\u0432', '\u0433', '\u0434', '\u0435', '\u0436', '\u0437', 79 | '\u0438', '\u0439', '\u043A', '\u043B', '\u043C', '\u043D', 80 | '\u043E', '\u043F', '\u0440', '\u0441', '\u0442', '\u0443', 81 | '\u0444', '\u0445', '\u0446', '\u0447', '\u0448', '\u0449', 82 | '\u044A', '\u044B', '\u044C', '\u044D', '\u044E', '\u044F', 83 | '\u2116', '\u0451', '\u0452', '\u0453', '\u0454', '\u0455', 84 | '\u0456', '\u0457', '\u0458', '\u0459', '\u045A', '\u045B', 85 | '\u045C', '\u00A7', '\u045E', '\u045F', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int count) 89 | { 90 | if (this.EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, count, null, 0); 93 | } else { 94 | return count; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (this.EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 161) 135 | switch (ch) { 136 | case 0x00AD: 137 | break; 138 | case 0x00A7: 139 | ch = 0xFD; 140 | break; 141 | case 0x0401: 142 | case 0x0402: 143 | case 0x0403: 144 | case 0x0404: 145 | case 0x0405: 146 | case 0x0406: 147 | case 0x0407: 148 | case 0x0408: 149 | case 0x0409: 150 | case 0x040A: 151 | case 0x040B: 152 | case 0x040C: 153 | ch -= 0x0360; 154 | break; 155 | case 0x040E: 156 | case 0x040F: 157 | case 0x0410: 158 | case 0x0411: 159 | case 0x0412: 160 | case 0x0413: 161 | case 0x0414: 162 | case 0x0415: 163 | case 0x0416: 164 | case 0x0417: 165 | case 0x0418: 166 | case 0x0419: 167 | case 0x041A: 168 | case 0x041B: 169 | case 0x041C: 170 | case 0x041D: 171 | case 0x041E: 172 | case 0x041F: 173 | case 0x0420: 174 | case 0x0421: 175 | case 0x0422: 176 | case 0x0423: 177 | case 0x0424: 178 | case 0x0425: 179 | case 0x0426: 180 | case 0x0427: 181 | case 0x0428: 182 | case 0x0429: 183 | case 0x042A: 184 | case 0x042B: 185 | case 0x042C: 186 | case 0x042D: 187 | case 0x042E: 188 | case 0x042F: 189 | case 0x0430: 190 | case 0x0431: 191 | case 0x0432: 192 | case 0x0433: 193 | case 0x0434: 194 | case 0x0435: 195 | case 0x0436: 196 | case 0x0437: 197 | case 0x0438: 198 | case 0x0439: 199 | case 0x043A: 200 | case 0x043B: 201 | case 0x043C: 202 | case 0x043D: 203 | case 0x043E: 204 | case 0x043F: 205 | case 0x0440: 206 | case 0x0441: 207 | case 0x0442: 208 | case 0x0443: 209 | case 0x0444: 210 | case 0x0445: 211 | case 0x0446: 212 | case 0x0447: 213 | case 0x0448: 214 | case 0x0449: 215 | case 0x044A: 216 | case 0x044B: 217 | case 0x044C: 218 | case 0x044D: 219 | case 0x044E: 220 | case 0x044F: 221 | ch -= 0x0360; 222 | break; 223 | case 0x0451: 224 | case 0x0452: 225 | case 0x0453: 226 | case 0x0454: 227 | case 0x0455: 228 | case 0x0456: 229 | case 0x0457: 230 | case 0x0458: 231 | case 0x0459: 232 | case 0x045A: 233 | case 0x045B: 234 | case 0x045C: 235 | ch -= 0x0360; 236 | break; 237 | case 0x045E: 238 | ch = 0xFE; 239 | break; 240 | case 0x045F: 241 | ch = 0xFF; 242 | break; 243 | case 0x2116: 244 | ch = 0xF0; 245 | break; 246 | default: 247 | { 248 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 249 | ch -= 0xFEE0; 250 | } else { 251 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 252 | continue; 253 | } 254 | } 255 | break; 256 | } 257 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 258 | if (bytes != null) 259 | bytes [byteIndex] = (byte)ch; 260 | byteIndex++; 261 | byteCount--; 262 | } 263 | return byteIndex; 264 | } 265 | } 266 | 267 | // class CP28595 268 | class ENCiso_8859_5 : CP28595 269 | { 270 | public ENCiso_8859_5 () : base () 271 | { 272 | } 273 | } 274 | } 275 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Portable.Text.Encoding.WindowsUniversal81.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | {B76A64F9-B00E-4243-AE89-5D024CA3B436} 7 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 8 | Library 9 | Portable.Text 10 | Portable.Text.Encoding 11 | v4.5 12 | Profile111 13 | 10.0 14 | 15 | 16 | true 17 | full 18 | false 19 | ..\bin\Debug\Profile111 20 | obj\Debug\Profile111 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | ..\bin\Release\Profile111 31 | obj\Release\Profile111 32 | TRACE 33 | prompt 34 | 4 35 | false 36 | true 37 | 38 | 39 | true 40 | 41 | 42 | portable.text.encoding.snk 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | big5.table 158 | 159 | 160 | gb18030.table 161 | 162 | 163 | gb2312.table 164 | 165 | 166 | jis.table 167 | 168 | 169 | ks.table 170 | 171 | 172 | 173 | 174 | 175 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Portable.Text.Encoding.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {EEE48C75-11BE-4B50-B759-F85B5052D473} 7 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 8 | Library 9 | Portable.Text 10 | Portable.Text.Encoding 11 | v4.5 12 | Profile7 13 | 10.0 14 | 15 | 16 | true 17 | full 18 | false 19 | ..\bin\Debug\Profile7 20 | obj\Debug\Profile7 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | true 26 | 27 | 28 | pdbonly 29 | true 30 | ..\bin\Release\Profile7 31 | obj\Release\Profile7 32 | TRACE 33 | prompt 34 | 4 35 | false 36 | true 37 | 38 | 39 | true 40 | 41 | 42 | portable.text.encoding.snk 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | big5.table 158 | 159 | 160 | gb18030.table 161 | 162 | 163 | gb2312.table 164 | 165 | 166 | jis.table 167 | 168 | 169 | ks.table 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // 2 | // AssemblyInfo.cs 3 | // 4 | // Author: Jeffrey Stedfast 5 | // 6 | // Copyright (c) 2014-2015 Xamarin Inc. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | using System.Reflection; 28 | 29 | // Information about this assembly is defined by the following attributes. 30 | // Change them to the values specific to your project. 31 | [assembly: AssemblyTitle ("Portable.Text.Encoding")] 32 | [assembly: AssemblyDescription ("A Portable Implementation of System.Text.Encoding.")] 33 | [assembly: AssemblyConfiguration ("")] 34 | [assembly: AssemblyCompany ("Xamarin Inc.")] 35 | [assembly: AssemblyProduct ("")] 36 | [assembly: AssemblyCopyright ("Copyright © 2014-2015 Xamarin Inc. (www.xamarin.com)")] 37 | [assembly: AssemblyTrademark ("Xamarin Inc.")] 38 | [assembly: AssemblyCulture ("")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Micro Version 45 | // Build Number 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | // 51 | // Note: AssemblyVersion is what the CLR matches against at runtime, so be careful 52 | // about updating it. The AssemblyFileVersion is the official release version while 53 | // the AssemblyInformationalVersion is just used as a display version. 54 | // 55 | // Based on my current understanding, AssemblyVersion is essentially the "API Version" 56 | // and so should only be updated when the API changes. The other 2 Version attributes 57 | // represent the "Release Version". 58 | // 59 | // Making releases: 60 | // 61 | // If any classes, methods, or enum values have been added, bump the Micro Version 62 | // in all version attributes and set the Build Number back to 0. 63 | // 64 | // If there have only been bug fixes, bump the Micro Version and/or the Build Number 65 | // in the AssemblyFileVersion attribute. 66 | [assembly: AssemblyInformationalVersion ("0.8.3")] 67 | [assembly: AssemblyFileVersion ("0.8.3.0")] 68 | [assembly: AssemblyVersion ("0.8.3.0")] 69 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Western/CP28593.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28593.cs - Latin 3 (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-913.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28593 : ByteEncoding 34 | { 35 | public CP28593 () 36 | : base (28593, ToChars, "Latin 3 (ISO)", 37 | "iso-8859-3", "iso-8859-3", "iso-8859-3", 38 | true, true, true, true, 28593) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u0126', 70 | '\u02D8', '\u00A3', '\u00A4', '\u003F', '\u0124', '\u00A7', 71 | '\u00A8', '\u0130', '\u015E', '\u011E', '\u0134', '\u00AD', 72 | '\u003F', '\u017B', '\u00B0', '\u0127', '\u00B2', '\u00B3', 73 | '\u00B4', '\u00B5', '\u0125', '\u00B7', '\u00B8', '\u0131', 74 | '\u015F', '\u011F', '\u0135', '\u00BD', '\u003F', '\u017C', 75 | '\u00C0', '\u00C1', '\u00C2', '\u003F', '\u00C4', '\u010A', 76 | '\u0108', '\u00C7', '\u00C8', '\u00C9', '\u00CA', '\u00CB', 77 | '\u00CC', '\u00CD', '\u00CE', '\u00CF', '\u003F', '\u00D1', 78 | '\u00D2', '\u00D3', '\u00D4', '\u0120', '\u00D6', '\u00D7', 79 | '\u011C', '\u00D9', '\u00DA', '\u00DB', '\u00DC', '\u016C', 80 | '\u015C', '\u00DF', '\u00E0', '\u00E1', '\u00E2', '\u003F', 81 | '\u00E4', '\u010B', '\u0109', '\u00E7', '\u00E8', '\u00E9', 82 | '\u00EA', '\u00EB', '\u00EC', '\u00ED', '\u00EE', '\u00EF', 83 | '\u003F', '\u00F1', '\u00F2', '\u00F3', '\u00F4', '\u0121', 84 | '\u00F6', '\u00F7', '\u011D', '\u00F9', '\u00FA', '\u00FB', 85 | '\u00FC', '\u016D', '\u015D', '\u02D9', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 89 | { 90 | if (EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, charCount, null, 0); 93 | } else { 94 | return charCount; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 161) 135 | switch (ch) { 136 | case 0x00A3: 137 | case 0x00A4: 138 | case 0x00A7: 139 | case 0x00A8: 140 | case 0x00AD: 141 | case 0x00B0: 142 | case 0x00B2: 143 | case 0x00B3: 144 | case 0x00B4: 145 | case 0x00B5: 146 | case 0x00B7: 147 | case 0x00B8: 148 | case 0x00BD: 149 | case 0x00C0: 150 | case 0x00C1: 151 | case 0x00C2: 152 | case 0x00C4: 153 | case 0x00C7: 154 | case 0x00C8: 155 | case 0x00C9: 156 | case 0x00CA: 157 | case 0x00CB: 158 | case 0x00CC: 159 | case 0x00CD: 160 | case 0x00CE: 161 | case 0x00CF: 162 | case 0x00D1: 163 | case 0x00D2: 164 | case 0x00D3: 165 | case 0x00D4: 166 | case 0x00D6: 167 | case 0x00D7: 168 | case 0x00D9: 169 | case 0x00DA: 170 | case 0x00DB: 171 | case 0x00DC: 172 | case 0x00DF: 173 | case 0x00E0: 174 | case 0x00E1: 175 | case 0x00E2: 176 | case 0x00E4: 177 | case 0x00E7: 178 | case 0x00E8: 179 | case 0x00E9: 180 | case 0x00EA: 181 | case 0x00EB: 182 | case 0x00EC: 183 | case 0x00ED: 184 | case 0x00EE: 185 | case 0x00EF: 186 | case 0x00F1: 187 | case 0x00F2: 188 | case 0x00F3: 189 | case 0x00F4: 190 | case 0x00F6: 191 | case 0x00F7: 192 | case 0x00F9: 193 | case 0x00FA: 194 | case 0x00FB: 195 | case 0x00FC: 196 | break; 197 | case 0x0108: 198 | ch = 0xC6; 199 | break; 200 | case 0x0109: 201 | ch = 0xE6; 202 | break; 203 | case 0x010A: 204 | ch = 0xC5; 205 | break; 206 | case 0x010B: 207 | ch = 0xE5; 208 | break; 209 | case 0x011C: 210 | ch = 0xD8; 211 | break; 212 | case 0x011D: 213 | ch = 0xF8; 214 | break; 215 | case 0x011E: 216 | ch = 0xAB; 217 | break; 218 | case 0x011F: 219 | ch = 0xBB; 220 | break; 221 | case 0x0120: 222 | ch = 0xD5; 223 | break; 224 | case 0x0121: 225 | ch = 0xF5; 226 | break; 227 | case 0x0124: 228 | ch = 0xA6; 229 | break; 230 | case 0x0125: 231 | ch = 0xB6; 232 | break; 233 | case 0x0126: 234 | ch = 0xA1; 235 | break; 236 | case 0x0127: 237 | ch = 0xB1; 238 | break; 239 | case 0x0130: 240 | ch = 0xA9; 241 | break; 242 | case 0x0131: 243 | ch = 0xB9; 244 | break; 245 | case 0x0134: 246 | ch = 0xAC; 247 | break; 248 | case 0x0135: 249 | ch = 0xBC; 250 | break; 251 | case 0x015C: 252 | ch = 0xDE; 253 | break; 254 | case 0x015D: 255 | ch = 0xFE; 256 | break; 257 | case 0x015E: 258 | ch = 0xAA; 259 | break; 260 | case 0x015F: 261 | ch = 0xBA; 262 | break; 263 | case 0x016C: 264 | ch = 0xDD; 265 | break; 266 | case 0x016D: 267 | ch = 0xFD; 268 | break; 269 | case 0x017B: 270 | ch = 0xAF; 271 | break; 272 | case 0x017C: 273 | ch = 0xBF; 274 | break; 275 | case 0x02D8: 276 | ch = 0xA2; 277 | break; 278 | case 0x02D9: 279 | ch = 0xFF; 280 | break; 281 | default: 282 | { 283 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 284 | ch -= 0xFEE0; 285 | } else { 286 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 287 | continue; 288 | } 289 | } 290 | break; 291 | } 292 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 293 | if (bytes != null) 294 | bytes [byteIndex] = (byte)ch; 295 | byteIndex++; 296 | byteCount--; 297 | } 298 | return byteIndex; 299 | } 300 | } 301 | 302 | // class CP28593 303 | class ENCiso_8859_3 : CP28593 304 | { 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Western/CP28597.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28597.cs - Greek (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-4909.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28597 : ByteEncoding 34 | { 35 | public CP28597 () 36 | : base (28597, ToChars, "Greek (ISO)", 37 | "iso-8859-7", "iso-8859-7", "iso-8859-7", 38 | true, true, true, true, 1253) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u2018', 70 | '\u2019', '\u00A3', '\u20AC', '\u003F', '\u00A6', '\u00A7', 71 | '\u00A8', '\u00A9', '\u003F', '\u00AB', '\u00AC', '\u00AD', 72 | '\u003F', '\u2015', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u00B4', '\u0385', '\u0386', '\u0387', '\u0388', '\u0389', 74 | '\u038A', '\u00BB', '\u038C', '\u00BD', '\u038E', '\u038F', 75 | '\u0390', '\u0391', '\u0392', '\u0393', '\u0394', '\u0395', 76 | '\u0396', '\u0397', '\u0398', '\u0399', '\u039A', '\u039B', 77 | '\u039C', '\u039D', '\u039E', '\u039F', '\u03A0', '\u03A1', 78 | '\u003F', '\u03A3', '\u03A4', '\u03A5', '\u03A6', '\u03A7', 79 | '\u03A8', '\u03A9', '\u03AA', '\u03AB', '\u03AC', '\u03AD', 80 | '\u03AE', '\u03AF', '\u03B0', '\u03B1', '\u03B2', '\u03B3', 81 | '\u03B4', '\u03B5', '\u03B6', '\u03B7', '\u03B8', '\u03B9', 82 | '\u03BA', '\u03BB', '\u03BC', '\u03BD', '\u03BE', '\u03BF', 83 | '\u03C0', '\u03C1', '\u03C2', '\u03C3', '\u03C4', '\u03C5', 84 | '\u03C6', '\u03C7', '\u03C8', '\u03C9', '\u03CA', '\u03CB', 85 | '\u03CC', '\u03CD', '\u03CE', '\u003F', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 89 | { 90 | if (EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, charCount, null, 0); 93 | } else { 94 | return charCount; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 161) 135 | switch (ch) { 136 | case 0x00A3: 137 | case 0x00A6: 138 | case 0x00A7: 139 | case 0x00A8: 140 | case 0x00A9: 141 | case 0x00AB: 142 | case 0x00AC: 143 | case 0x00AD: 144 | case 0x00B0: 145 | case 0x00B1: 146 | case 0x00B2: 147 | case 0x00B3: 148 | case 0x00B4: 149 | case 0x00B7: 150 | case 0x00BB: 151 | case 0x00BD: 152 | break; 153 | case 0x0385: 154 | case 0x0386: 155 | case 0x0387: 156 | case 0x0388: 157 | case 0x0389: 158 | case 0x038A: 159 | ch -= 0x02D0; 160 | break; 161 | case 0x038C: 162 | ch = 0xBC; 163 | break; 164 | case 0x038E: 165 | case 0x038F: 166 | case 0x0390: 167 | case 0x0391: 168 | case 0x0392: 169 | case 0x0393: 170 | case 0x0394: 171 | case 0x0395: 172 | case 0x0396: 173 | case 0x0397: 174 | case 0x0398: 175 | case 0x0399: 176 | case 0x039A: 177 | case 0x039B: 178 | case 0x039C: 179 | case 0x039D: 180 | case 0x039E: 181 | case 0x039F: 182 | case 0x03A0: 183 | case 0x03A1: 184 | ch -= 0x02D0; 185 | break; 186 | case 0x03A3: 187 | case 0x03A4: 188 | case 0x03A5: 189 | case 0x03A6: 190 | case 0x03A7: 191 | case 0x03A8: 192 | case 0x03A9: 193 | case 0x03AA: 194 | case 0x03AB: 195 | case 0x03AC: 196 | case 0x03AD: 197 | case 0x03AE: 198 | case 0x03AF: 199 | case 0x03B0: 200 | case 0x03B1: 201 | case 0x03B2: 202 | case 0x03B3: 203 | case 0x03B4: 204 | case 0x03B5: 205 | case 0x03B6: 206 | case 0x03B7: 207 | case 0x03B8: 208 | case 0x03B9: 209 | case 0x03BA: 210 | case 0x03BB: 211 | case 0x03BC: 212 | case 0x03BD: 213 | case 0x03BE: 214 | case 0x03BF: 215 | case 0x03C0: 216 | case 0x03C1: 217 | case 0x03C2: 218 | case 0x03C3: 219 | case 0x03C4: 220 | case 0x03C5: 221 | case 0x03C6: 222 | case 0x03C7: 223 | case 0x03C8: 224 | case 0x03C9: 225 | case 0x03CA: 226 | case 0x03CB: 227 | case 0x03CC: 228 | case 0x03CD: 229 | case 0x03CE: 230 | ch -= 0x02D0; 231 | break; 232 | case 0x03D5: 233 | ch = 0xF6; 234 | break; 235 | case 0x2015: 236 | ch = 0xAF; 237 | break; 238 | case 0x2018: 239 | ch = 0xA1; 240 | break; 241 | case 0x2019: 242 | ch = 0xA2; 243 | break; 244 | case 0x20AC: 245 | ch = 0xA4; 246 | break; 247 | default: 248 | { 249 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 250 | ch -= 0xFEE0; 251 | } else { 252 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 253 | continue; 254 | } 255 | } 256 | break; 257 | } 258 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 259 | if (bytes != null) 260 | bytes [byteIndex] = (byte)ch; 261 | byteIndex++; 262 | byteCount--; 263 | } 264 | return byteIndex; 265 | } 266 | } 267 | 268 | // class CP28597 269 | class ENCiso_8859_7 : CP28597 270 | { 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/Western/CP28605.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * CP28605.cs - Latin 9 (ISO) code page. 3 | * 4 | * Copyright (c) 2002 Southern Storm Software, Pty Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the "Software"), 8 | * to deal in the Software without restriction, including without limitation 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included 14 | * in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | * OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | // Generated from "ibm-923.ucm". 26 | 27 | // WARNING: Modifying this file directly might be a bad idea. 28 | // You should edit the code generator tools/ucm2cp.c instead for your changes 29 | // to appear in all relevant classes. 30 | using System; 31 | 32 | namespace Portable.Text { 33 | class CP28605 : ByteEncoding 34 | { 35 | public CP28605 () 36 | : base (28605, ToChars, "Latin 9 (ISO)", 37 | "iso-8859-15", "iso-8859-15", "iso-8859-15", 38 | false, true, true, true, 1252) 39 | { 40 | } 41 | 42 | private static readonly char[] ToChars = { 43 | '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', 44 | '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', 45 | '\u000C', '\u000D', '\u000E', '\u000F', '\u0010', '\u0011', 46 | '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', 47 | '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', 48 | '\u001E', '\u001F', '\u0020', '\u0021', '\u0022', '\u0023', 49 | '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', 50 | '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F', 51 | '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', 52 | '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', 53 | '\u003C', '\u003D', '\u003E', '\u003F', '\u0040', '\u0041', 54 | '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', 55 | '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', 56 | '\u004E', '\u004F', '\u0050', '\u0051', '\u0052', '\u0053', 57 | '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', 58 | '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F', 59 | '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', 60 | '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', 61 | '\u006C', '\u006D', '\u006E', '\u006F', '\u0070', '\u0071', 62 | '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', 63 | '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', 64 | '\u007E', '\u007F', '\u0080', '\u0081', '\u0082', '\u0083', 65 | '\u0084', '\u0085', '\u0086', '\u0087', '\u0088', '\u0089', 66 | '\u008A', '\u008B', '\u008C', '\u008D', '\u008E', '\u008F', 67 | '\u0090', '\u0091', '\u0092', '\u0093', '\u0094', '\u0095', 68 | '\u0096', '\u0097', '\u0098', '\u0099', '\u009A', '\u009B', 69 | '\u009C', '\u009D', '\u009E', '\u009F', '\u00A0', '\u00A1', 70 | '\u00A2', '\u00A3', '\u20AC', '\u00A5', '\u0160', '\u00A7', 71 | '\u0161', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', 72 | '\u00AE', '\u00AF', '\u00B0', '\u00B1', '\u00B2', '\u00B3', 73 | '\u017D', '\u00B5', '\u00B6', '\u00B7', '\u017E', '\u00B9', 74 | '\u00BA', '\u00BB', '\u0152', '\u0153', '\u0178', '\u00BF', 75 | '\u00C0', '\u00C1', '\u00C2', '\u00C3', '\u00C4', '\u00C5', 76 | '\u00C6', '\u00C7', '\u00C8', '\u00C9', '\u00CA', '\u00CB', 77 | '\u00CC', '\u00CD', '\u00CE', '\u00CF', '\u00D0', '\u00D1', 78 | '\u00D2', '\u00D3', '\u00D4', '\u00D5', '\u00D6', '\u00D7', 79 | '\u00D8', '\u00D9', '\u00DA', '\u00DB', '\u00DC', '\u00DD', 80 | '\u00DE', '\u00DF', '\u00E0', '\u00E1', '\u00E2', '\u00E3', 81 | '\u00E4', '\u00E5', '\u00E6', '\u00E7', '\u00E8', '\u00E9', 82 | '\u00EA', '\u00EB', '\u00EC', '\u00ED', '\u00EE', '\u00EF', 83 | '\u00F0', '\u00F1', '\u00F2', '\u00F3', '\u00F4', '\u00F5', 84 | '\u00F6', '\u00F7', '\u00F8', '\u00F9', '\u00FA', '\u00FB', 85 | '\u00FC', '\u00FD', '\u00FE', '\u00FF', 86 | }; 87 | // Get the number of bytes needed to encode a character buffer. 88 | public unsafe override int GetByteCountImpl (char* chars, int charCount) 89 | { 90 | if (EncoderFallback != null) { 91 | //Calculate byte count by actually doing encoding and discarding the data. 92 | return GetBytesImpl (chars, charCount, null, 0); 93 | } else { 94 | return charCount; 95 | } 96 | } 97 | // Get the number of bytes needed to encode a character buffer. 98 | public override int GetByteCount (String s) 99 | { 100 | if (EncoderFallback != null) { 101 | //Calculate byte count by actually doing encoding and discarding the data. 102 | unsafe { 103 | fixed (char *s_ptr = s) { 104 | return GetBytesImpl (s_ptr, s.Length, null, 0); 105 | } 106 | } 107 | } else { 108 | //byte count equals character count because no EncoderFallback set 109 | return s.Length; 110 | } 111 | } 112 | //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count 113 | protected unsafe override void ToBytes (char* chars, int charCount, 114 | byte* bytes, int byteCount) 115 | { 116 | //Calling ToBytes with null destination buffer doesn't make any sense 117 | if (bytes == null) 118 | throw new ArgumentNullException ("bytes"); 119 | GetBytesImpl (chars, charCount, bytes, byteCount); 120 | } 121 | 122 | public unsafe override int GetBytesImpl (char* chars, int charCount, 123 | byte* bytes, int byteCount) 124 | { 125 | int ch; 126 | int charIndex = 0; 127 | int byteIndex = 0; 128 | EncoderFallbackBuffer buffer = null; 129 | 130 | while (charCount > 0) { 131 | ch = (int)(chars [charIndex]); 132 | charIndex++; 133 | charCount--; 134 | if (ch >= 164) 135 | switch (ch) { 136 | case 0x00A5: 137 | case 0x00A7: 138 | case 0x00A9: 139 | case 0x00AA: 140 | case 0x00AB: 141 | case 0x00AC: 142 | case 0x00AD: 143 | case 0x00AE: 144 | case 0x00AF: 145 | case 0x00B0: 146 | case 0x00B1: 147 | case 0x00B2: 148 | case 0x00B3: 149 | case 0x00B5: 150 | case 0x00B6: 151 | case 0x00B7: 152 | case 0x00B9: 153 | case 0x00BA: 154 | case 0x00BB: 155 | case 0x00BF: 156 | case 0x00C0: 157 | case 0x00C1: 158 | case 0x00C2: 159 | case 0x00C3: 160 | case 0x00C4: 161 | case 0x00C5: 162 | case 0x00C6: 163 | case 0x00C7: 164 | case 0x00C8: 165 | case 0x00C9: 166 | case 0x00CA: 167 | case 0x00CB: 168 | case 0x00CC: 169 | case 0x00CD: 170 | case 0x00CE: 171 | case 0x00CF: 172 | case 0x00D0: 173 | case 0x00D1: 174 | case 0x00D2: 175 | case 0x00D3: 176 | case 0x00D4: 177 | case 0x00D5: 178 | case 0x00D6: 179 | case 0x00D7: 180 | case 0x00D8: 181 | case 0x00D9: 182 | case 0x00DA: 183 | case 0x00DB: 184 | case 0x00DC: 185 | case 0x00DD: 186 | case 0x00DE: 187 | case 0x00DF: 188 | case 0x00E0: 189 | case 0x00E1: 190 | case 0x00E2: 191 | case 0x00E3: 192 | case 0x00E4: 193 | case 0x00E5: 194 | case 0x00E6: 195 | case 0x00E7: 196 | case 0x00E8: 197 | case 0x00E9: 198 | case 0x00EA: 199 | case 0x00EB: 200 | case 0x00EC: 201 | case 0x00ED: 202 | case 0x00EE: 203 | case 0x00EF: 204 | case 0x00F0: 205 | case 0x00F1: 206 | case 0x00F2: 207 | case 0x00F3: 208 | case 0x00F4: 209 | case 0x00F5: 210 | case 0x00F6: 211 | case 0x00F7: 212 | case 0x00F8: 213 | case 0x00F9: 214 | case 0x00FA: 215 | case 0x00FB: 216 | case 0x00FC: 217 | case 0x00FD: 218 | case 0x00FE: 219 | case 0x00FF: 220 | break; 221 | case 0x0152: 222 | ch = 0xBC; 223 | break; 224 | case 0x0153: 225 | ch = 0xBD; 226 | break; 227 | case 0x0160: 228 | ch = 0xA6; 229 | break; 230 | case 0x0161: 231 | ch = 0xA8; 232 | break; 233 | case 0x0178: 234 | ch = 0xBE; 235 | break; 236 | case 0x017D: 237 | ch = 0xB4; 238 | break; 239 | case 0x017E: 240 | ch = 0xB8; 241 | break; 242 | case 0x20AC: 243 | ch = 0xA4; 244 | break; 245 | default: 246 | { 247 | if (ch >= 0xFF01 && ch <= 0xFF5E) { 248 | ch -= 0xFEE0; 249 | } else { 250 | HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); 251 | continue; 252 | } 253 | } 254 | break; 255 | } 256 | //Write encoded byte to buffer, if buffer is defined and fallback was not used 257 | if (bytes != null) 258 | bytes [byteIndex] = (byte)ch; 259 | byteIndex++; 260 | byteCount--; 261 | } 262 | return byteIndex; 263 | } 264 | } 265 | 266 | // class CP28605 267 | class ENCiso_8859_15 : CP28605 268 | { 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /Portable.Text.Encoding/portable.text.encoding.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstedfast/Portable.Text.Encoding/74147c65fff30b872ea585fccf2c384b8d708353/Portable.Text.Encoding/portable.text.encoding.snk -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Portable.Text.Encoding 2 | ====================== 3 | 4 | A Portable Implementation of System.Text.Encoding. 5 | 6 | ## Goals 7 | 8 | The main goal of this project is to supply a portable alternative to System.Text.Encoding that can be used 9 | from other PCL libraries. 10 | 11 | Platforms such as Windows Phone currently do not provide support for text encodings other than a limited subset 12 | of the Unicode encodings. This can be a real show-stopper when you need to work with legacy text encodings. 13 | 14 | For example, while trying to make a PCL version of [MimeKit](https://github.com/jstedfast/MimeKit) available 15 | for developers targetting Windows Phone, the biggest road block that I ran into was a lack of support for 16 | charset conversion. While more and more mail software is moving toward using UTF-8, it's still common for 17 | users to receive mail in other encodings. As you might imagine, a mail client that only supported receiving 18 | mail that was encoded in UTF-8 would have very limited value to most users. 19 | 20 | ## Supported Text Encodings 21 | 22 | Currently supported encodings include: 23 | * ASCII 24 | * Unicode (including UTF-8 and UTF-7) 25 | * Western Encodings 26 | * IBM437 27 | * IBM850 28 | * IBM860 29 | * IBM861 30 | * IBM862 31 | * IBM863 32 | * IBM865 33 | * ISO-8859-1 34 | * ISO-8859-2 35 | * ISO-8869-3 36 | * ISO-8859-7 37 | * ISO-8859-15 38 | * Windows-1250 39 | * Windows-1252 40 | * Windows-1253 41 | * Macintosh and Mac-Icelandic 42 | * Middle Eastern Encodings 43 | * ISO-8859-6 44 | * ISO-8859-8 45 | * ISO-8859-9 46 | * Windows-1254 47 | * Windows-1255 48 | * Windows-1256 49 | * Windows-38598 50 | * Chinese/Japanese/Korean Encodings 51 | * Big5 52 | * EUC-JP 53 | * EUC-KR 54 | * GB2312 55 | * GB18030 56 | * ISO-2022-JP 57 | * SHIFT-JIS 58 | * UHC 59 | * Other Encodings 60 | * ISO-8859-4 61 | * ISO-8859-5 62 | * KOI8-R 63 | * KOI8-U 64 | * Windows-874 65 | * Windows-1251 66 | * Windows-1257 67 | * Windows-1258 68 | * And many more... 69 | 70 | ## TODO 71 | 72 | Add a few more optionally-included assemblies for whatever other encodings that may be desired. 73 | 74 | ## License Information 75 | 76 | Portable.Text.Encoding is Copyright (C) 2014 Xamarin Inc. and is licensed under the MIT license: 77 | 78 | Permission is hereby granted, free of charge, to any person obtaining a copy 79 | of this software and associated documentation files (the "Software"), to deal 80 | in the Software without restriction, including without limitation the rights 81 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 82 | copies of the Software, and to permit persons to whom the Software is 83 | furnished to do so, subject to the following conditions: 84 | 85 | The above copyright notice and this permission notice shall be included in 86 | all copies or substantial portions of the Software. 87 | 88 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 89 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 90 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 91 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 92 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 93 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 94 | THE SOFTWARE. 95 | 96 | ## Installing via NuGet 97 | 98 | The easiest way to install Portable.Text.Encoding is via 99 | [NuGet](https://www.nuget.org/packages/Portable.Text.Encoding/). 100 | 101 | In Visual Studio's [Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console), 102 | simply enter the following command: 103 | 104 | Install-Package Portable.Text.Encoding 105 | 106 | ## Building 107 | 108 | Just open up Portable.Text.Encoding.sln in your favorite IDE (Visual Studio, Xamarin Studio, SharpDevelop, etc) and hit "Build", it's that easy! 109 | 110 | ## Contributing 111 | 112 | The first thing you'll need to do is fork Portable.Text.Encoding to your own GitHub repository. Once you do that, 113 | 114 | git clone git@github.com//Portable.Text.Encoding.git 115 | 116 | Now you are ready to start hacking away on the code. When you're satisfied with your set of patches, commit 117 | them to your GitHub fork and then send me a Pull Request and I'll review and merge your changes into the 118 | official repo. 119 | 120 | ### Adding Support for Other Text Encodings 121 | 122 | So far, all of the code has been taken directly from [Mono](https://github.com/mono/mono) and it should be fairly 123 | straight-forward to "port" the [I18N](https://github.com/mono/mono/tree/master/mcs/class/I18N) assemblies over to 124 | Portable.Text.Encoding. 125 | 126 | I think the best coarse of action is to follow the same overall design of the I18N assemblies - in other words: 127 | create a new PCL project/assembly for each "region" (West, CJK, MidEast, Rare, Other). Once you've added all the 128 | sources (and tables for those that have them) and changed the namespace from `System.Text` to `Portable.Text`, 129 | just try building. If there aren't any errors, you're done! 130 | 131 | ## Reporting Bugs 132 | 133 | Have a bug or a feature request? [Please open a new issue](https://github.com/jstedfast/Portable.Text.Encoding/issues). 134 | 135 | Before opening a new issue, please search for existing issues to avoid submitting duplicates. 136 | 137 | ## Documentation 138 | 139 | There realistically doesn't need to be any real API documentation as the API is exactly the same as 140 | System.Text.Encoding API, but I'm open to contributions in this area. 141 | -------------------------------------------------------------------------------- /nuget/Portable.Text.Encoding.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Portable.Text.Encoding 5 | 0.8.3.0 6 | Portable.Text.Encoding: A Portable Implementation of System.Text.Encoding. 7 | Jeffrey Stedfast,Atsushi Enomoto,and Mono contributors 8 | Jeffrey Stedfast 9 | http://opensource.org/licenses/MIT 10 | http://github.com/jstedfast/Portable.Text.Encoding 11 | false 12 | The main goal of this project is to supply a portable alternative to System.Text.Encoding that can be used from other PCL libraries. 13 | 14 | Platforms such as Windows Phone currently do not provide support for text encodings other than a limited subset of the Unicode encodings. This can be a real show-stopper when you need to work with legacy text encodings. 15 | A Portable Implementation of System.Text.Encoding. 16 | * Updated to work with Xamarin.iOS. 17 | Xamarin Inc. 18 | en-US 19 | pcl net45 win8 win81 wp80 wpa81 monodroid monotouch xamarin 20 | 21 | 22 | 23 | 24 | 25 | --------------------------------------------------------------------------------