Encoding.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * Encoding.cs - Implementation of the "System.Text.Encoding" class.
  3. *
  4. * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
  5. * Copyright (c) 2002, Ximian, Inc.
  6. * Copyright (c) 2003, 2004 Novell, Inc.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. namespace System.Text
  27. {
  28. using System;
  29. using System.Reflection;
  30. using System.Globalization;
  31. using System.Security;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.InteropServices;
  34. [Serializable]
  35. [ComVisible (true)]
  36. public abstract class Encoding : ICloneable
  37. {
  38. // Code page used by this encoding.
  39. internal int codePage;
  40. internal int windows_code_page;
  41. bool is_readonly = true;
  42. // Constructor.
  43. protected Encoding ()
  44. {
  45. }
  46. #if ECMA_COMPAT
  47. protected internal
  48. #else
  49. protected
  50. #endif
  51. Encoding (int codePage)
  52. {
  53. this.codePage = windows_code_page = codePage;
  54. switch (codePage) {
  55. default:
  56. // MS has "InternalBestFit{Decoder|Encoder}Fallback
  57. // here, but we dunno what they are for.
  58. decoder_fallback = DecoderFallback.ReplacementFallback;
  59. encoder_fallback = EncoderFallback.ReplacementFallback;
  60. break;
  61. case 20127: // ASCII
  62. case 54936: // GB18030
  63. decoder_fallback = DecoderFallback.ReplacementFallback;
  64. encoder_fallback = EncoderFallback.ReplacementFallback;
  65. break;
  66. case 1200: // UTF16
  67. case 1201: // UTF16
  68. case 12000: // UTF32
  69. case 12001: // UTF32
  70. case 65000: // UTF7
  71. case 65001: // UTF8
  72. decoder_fallback = DecoderFallback.StandardSafeFallback;
  73. encoder_fallback = EncoderFallback.StandardSafeFallback;
  74. break;
  75. }
  76. }
  77. // until we change the callers:
  78. internal static string _ (string arg) {
  79. return arg;
  80. }
  81. DecoderFallback decoder_fallback;
  82. EncoderFallback encoder_fallback;
  83. [ComVisible (false)]
  84. public bool IsReadOnly {
  85. get { return is_readonly; }
  86. }
  87. [ComVisible (false)]
  88. public virtual bool IsSingleByte {
  89. get { return false; }
  90. }
  91. [ComVisible (false)]
  92. public DecoderFallback DecoderFallback {
  93. get { return decoder_fallback; }
  94. set {
  95. if (IsReadOnly)
  96. throw new InvalidOperationException ("This Encoding is readonly.");
  97. if (value == null)
  98. throw new ArgumentNullException ();
  99. decoder_fallback = value;
  100. }
  101. }
  102. [ComVisible (false)]
  103. public EncoderFallback EncoderFallback {
  104. get { return encoder_fallback; }
  105. set {
  106. if (IsReadOnly)
  107. throw new InvalidOperationException ("This Encoding is readonly.");
  108. if (value == null)
  109. throw new ArgumentNullException ();
  110. encoder_fallback = value;
  111. }
  112. }
  113. internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d)
  114. {
  115. if (e != null)
  116. encoder_fallback = e;
  117. if (d != null)
  118. decoder_fallback = d;
  119. }
  120. // Convert between two encodings.
  121. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  122. byte[] bytes)
  123. {
  124. if (srcEncoding == null) {
  125. throw new ArgumentNullException ("srcEncoding");
  126. }
  127. if (dstEncoding == null) {
  128. throw new ArgumentNullException ("dstEncoding");
  129. }
  130. if (bytes == null) {
  131. throw new ArgumentNullException ("bytes");
  132. }
  133. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
  134. }
  135. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  136. byte[] bytes, int index, int count)
  137. {
  138. if (srcEncoding == null) {
  139. throw new ArgumentNullException ("srcEncoding");
  140. }
  141. if (dstEncoding == null) {
  142. throw new ArgumentNullException ("dstEncoding");
  143. }
  144. if (bytes == null) {
  145. throw new ArgumentNullException ("bytes");
  146. }
  147. if (index < 0 || index > bytes.Length) {
  148. throw new ArgumentOutOfRangeException
  149. ("index", _("ArgRange_Array"));
  150. }
  151. if (count < 0 || (bytes.Length - index) < count) {
  152. throw new ArgumentOutOfRangeException
  153. ("count", _("ArgRange_Array"));
  154. }
  155. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
  156. }
  157. // Determine if two Encoding objects are equal.
  158. public override bool Equals (Object value)
  159. {
  160. Encoding enc = (value as Encoding);
  161. if (enc != null) {
  162. return codePage == enc.codePage &&
  163. DecoderFallback.Equals (enc.DecoderFallback) &&
  164. EncoderFallback.Equals (enc.EncoderFallback);
  165. } else {
  166. return false;
  167. }
  168. }
  169. // Get the number of characters needed to encode a character buffer.
  170. public abstract int GetByteCount (char[] chars, int index, int count);
  171. // Convenience wrappers for "GetByteCount".
  172. public virtual int GetByteCount (String s)
  173. {
  174. if (s == null)
  175. throw new ArgumentNullException ("s");
  176. if (s.Length == 0)
  177. return 0;
  178. unsafe {
  179. fixed (char* cptr = s) {
  180. return GetByteCount (cptr, s.Length);
  181. }
  182. }
  183. }
  184. public virtual int GetByteCount (char[] chars)
  185. {
  186. if (chars != null) {
  187. return GetByteCount (chars, 0, chars.Length);
  188. } else {
  189. throw new ArgumentNullException ("chars");
  190. }
  191. }
  192. // Get the bytes that result from encoding a character buffer.
  193. public abstract int GetBytes (char[] chars, int charIndex, int charCount,
  194. byte[] bytes, int byteIndex);
  195. // Convenience wrappers for "GetBytes".
  196. public virtual int GetBytes (String s, int charIndex, int charCount,
  197. byte[] bytes, int byteIndex)
  198. {
  199. if (s == null)
  200. throw new ArgumentNullException ("s");
  201. if (charIndex < 0 || charIndex > s.Length)
  202. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  203. if (charCount < 0 || charIndex > (s.Length - charCount))
  204. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  205. if (byteIndex < 0 || byteIndex > bytes.Length)
  206. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  207. if (charCount == 0 || bytes.Length == byteIndex)
  208. return 0;
  209. unsafe {
  210. fixed (char* cptr = s) {
  211. fixed (byte* bptr = bytes) {
  212. return GetBytes (cptr + charIndex,
  213. charCount,
  214. bptr + byteIndex,
  215. bytes.Length - byteIndex);
  216. }
  217. }
  218. }
  219. }
  220. public virtual byte[] GetBytes (String s)
  221. {
  222. if (s == null)
  223. throw new ArgumentNullException ("s");
  224. if (s.Length == 0)
  225. return EmptyArray<byte>.Value;
  226. int byteCount = GetByteCount (s);
  227. if (byteCount == 0)
  228. return EmptyArray<byte>.Value;
  229. unsafe {
  230. fixed (char* cptr = s) {
  231. byte [] bytes = new byte [byteCount];
  232. fixed (byte* bptr = bytes) {
  233. GetBytes (cptr, s.Length,
  234. bptr, byteCount);
  235. return bytes;
  236. }
  237. }
  238. }
  239. }
  240. public virtual byte[] GetBytes (char[] chars, int index, int count)
  241. {
  242. int numBytes = GetByteCount (chars, index, count);
  243. byte[] bytes = new byte [numBytes];
  244. GetBytes (chars, index, count, bytes, 0);
  245. return bytes;
  246. }
  247. public virtual byte[] GetBytes (char[] chars)
  248. {
  249. int numBytes = GetByteCount (chars, 0, chars.Length);
  250. byte[] bytes = new byte [numBytes];
  251. GetBytes (chars, 0, chars.Length, bytes, 0);
  252. return bytes;
  253. }
  254. // Get the number of characters needed to decode a byte buffer.
  255. public abstract int GetCharCount (byte[] bytes, int index, int count);
  256. // Convenience wrappers for "GetCharCount".
  257. public virtual int GetCharCount (byte[] bytes)
  258. {
  259. if (bytes == null) {
  260. throw new ArgumentNullException ("bytes");
  261. }
  262. return GetCharCount (bytes, 0, bytes.Length);
  263. }
  264. // Get the characters that result from decoding a byte buffer.
  265. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
  266. char[] chars, int charIndex);
  267. // Convenience wrappers for "GetChars".
  268. public virtual char[] GetChars (byte[] bytes, int index, int count)
  269. {
  270. int numChars = GetCharCount (bytes, index, count);
  271. char[] chars = new char [numChars];
  272. GetChars (bytes, index, count, chars, 0);
  273. return chars;
  274. }
  275. public virtual char[] GetChars (byte[] bytes)
  276. {
  277. if (bytes == null) {
  278. throw new ArgumentNullException ("bytes");
  279. }
  280. int numChars = GetCharCount (bytes, 0, bytes.Length);
  281. char[] chars = new char [numChars];
  282. GetChars (bytes, 0, bytes.Length, chars, 0);
  283. return chars;
  284. }
  285. // Get a decoder that forwards requests to this object.
  286. public virtual Decoder GetDecoder ()
  287. {
  288. return new ForwardingDecoder (this);
  289. }
  290. // Get an encoder that forwards requests to this object.
  291. public virtual Encoder GetEncoder ()
  292. {
  293. return new ForwardingEncoder (this);
  294. }
  295. // Loaded copy of the "I18N" assembly. We need to move
  296. // this into a class in "System.Private" eventually.
  297. private static Assembly i18nAssembly;
  298. private static bool i18nDisabled;
  299. // Invoke a specific method on the "I18N" manager object.
  300. // Returns NULL if the method failed.
  301. private static Object InvokeI18N (String name, params Object[] args)
  302. {
  303. lock (lockobj) {
  304. // Bail out if we previously detected that there
  305. // is insufficent engine support for I18N handling.
  306. if (i18nDisabled) {
  307. return null;
  308. }
  309. // Find or load the "I18N" assembly.
  310. if (i18nAssembly == null) {
  311. try {
  312. try {
  313. i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
  314. } catch (NotImplementedException) {
  315. // Assembly loading unsupported by the engine.
  316. i18nDisabled = true;
  317. return null;
  318. }
  319. if (i18nAssembly == null) {
  320. return null;
  321. }
  322. } catch (SystemException) {
  323. return null;
  324. }
  325. }
  326. // Find the "I18N.Common.Manager" class.
  327. Type managerClass;
  328. try {
  329. managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
  330. } catch (NotImplementedException) {
  331. // "GetType" is not supported by the engine.
  332. i18nDisabled = true;
  333. return null;
  334. }
  335. if (managerClass == null) {
  336. return null;
  337. }
  338. // Get the value of the "PrimaryManager" property.
  339. Object manager;
  340. try {
  341. manager = managerClass.InvokeMember
  342. ("PrimaryManager",
  343. BindingFlags.GetProperty |
  344. BindingFlags.Static |
  345. BindingFlags.Public,
  346. null, null, null, null, null, null);
  347. if (manager == null) {
  348. return null;
  349. }
  350. } catch (MissingMethodException) {
  351. return null;
  352. } catch (SecurityException) {
  353. return null;
  354. } catch (NotImplementedException) {
  355. // "InvokeMember" is not supported by the engine.
  356. i18nDisabled = true;
  357. return null;
  358. }
  359. // Invoke the requested method on the manager.
  360. try {
  361. return managerClass.InvokeMember
  362. (name,
  363. BindingFlags.InvokeMethod |
  364. BindingFlags.Instance |
  365. BindingFlags.Public,
  366. null, manager, args, null, null, null);
  367. } catch (MissingMethodException) {
  368. return null;
  369. } catch (SecurityException) {
  370. return null;
  371. }
  372. }
  373. }
  374. // Get an encoder for a specific code page.
  375. #if ECMA_COMPAT
  376. private
  377. #else
  378. public
  379. #endif
  380. static Encoding GetEncoding (int codepage)
  381. {
  382. if (codepage < 0 || codepage > 0xffff)
  383. throw new ArgumentOutOfRangeException ("codepage",
  384. "Valid values are between 0 and 65535, inclusive.");
  385. // Check for the builtin code pages first.
  386. switch (codepage) {
  387. case 0: return Default;
  388. case ASCIIEncoding.ASCII_CODE_PAGE:
  389. return ASCII;
  390. case UTF7Encoding.UTF7_CODE_PAGE:
  391. return UTF7;
  392. case UTF8Encoding.UTF8_CODE_PAGE:
  393. return UTF8;
  394. case UTF32Encoding.UTF32_CODE_PAGE:
  395. return UTF32;
  396. case UTF32Encoding.BIG_UTF32_CODE_PAGE:
  397. return BigEndianUTF32;
  398. case UnicodeEncoding.UNICODE_CODE_PAGE:
  399. return Unicode;
  400. case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
  401. return BigEndianUnicode;
  402. case Latin1Encoding.ISOLATIN_CODE_PAGE:
  403. return ISOLatin1;
  404. default: break;
  405. }
  406. // Try to obtain a code page handler from the I18N handler.
  407. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codepage));
  408. if (enc != null) {
  409. enc.is_readonly = true;
  410. return enc;
  411. }
  412. // Build a code page class name.
  413. String cpName = "System.Text.CP" + codepage.ToString ();
  414. // Look for a code page converter in this assembly.
  415. Assembly assembly = Assembly.GetExecutingAssembly ();
  416. Type type = assembly.GetType (cpName);
  417. if (type != null) {
  418. enc = (Encoding)(Activator.CreateInstance (type));
  419. enc.is_readonly = true;
  420. return enc;
  421. }
  422. // Look in any assembly, in case the application
  423. // has provided its own code page handler.
  424. type = Type.GetType (cpName);
  425. if (type != null) {
  426. enc = (Encoding)(Activator.CreateInstance (type));
  427. enc.is_readonly = true;
  428. return enc;
  429. }
  430. // We have no idea how to handle this code page.
  431. throw new NotSupportedException
  432. (String.Format ("CodePage {0} not supported", codepage.ToString ()));
  433. }
  434. #if !ECMA_COMPAT
  435. [ComVisible (false)]
  436. public virtual object Clone ()
  437. {
  438. Encoding e = (Encoding) MemberwiseClone ();
  439. e.is_readonly = false;
  440. return e;
  441. }
  442. public static Encoding GetEncoding (int codepage,
  443. EncoderFallback encoderFallback, DecoderFallback decoderFallback)
  444. {
  445. if (encoderFallback == null)
  446. throw new ArgumentNullException ("encoderFallback");
  447. if (decoderFallback == null)
  448. throw new ArgumentNullException ("decoderFallback");
  449. Encoding e = GetEncoding (codepage).Clone () as Encoding;
  450. e.is_readonly = false;
  451. e.encoder_fallback = encoderFallback;
  452. e.decoder_fallback = decoderFallback;
  453. return e;
  454. }
  455. public static Encoding GetEncoding (string name,
  456. EncoderFallback encoderFallback, DecoderFallback decoderFallback)
  457. {
  458. if (encoderFallback == null)
  459. throw new ArgumentNullException ("encoderFallback");
  460. if (decoderFallback == null)
  461. throw new ArgumentNullException ("decoderFallback");
  462. Encoding e = GetEncoding (name).Clone () as Encoding;
  463. e.is_readonly = false;
  464. e.encoder_fallback = encoderFallback;
  465. e.decoder_fallback = decoderFallback;
  466. return e;
  467. }
  468. static EncodingInfo [] encoding_infos;
  469. // FIXME: As everyone would agree, this implementation is so *hacky*
  470. // and could be very easily broken. But since there is a test for
  471. // this method to make sure that this method always returns
  472. // the same number and content of encoding infos, this won't
  473. // matter practically.
  474. public static EncodingInfo[] GetEncodings ()
  475. {
  476. if (encoding_infos == null) {
  477. int [] codepages = new int [] {
  478. 37, 437, 500, 708,
  479. 850, 852, 855, 857, 858, 860, 861, 862, 863,
  480. 864, 865, 866, 869, 870, 874, 875,
  481. 932, 936, 949, 950,
  482. 1026, 1047, 1140, 1141, 1142, 1143, 1144,
  483. 1145, 1146, 1147, 1148, 1149,
  484. 1200, 1201, 1250, 1251, 1252, 1253, 1254,
  485. 1255, 1256, 1257, 1258,
  486. 10000, 10079, 12000, 12001,
  487. 20127, 20273, 20277, 20278, 20280, 20284,
  488. 20285, 20290, 20297, 20420, 20424, 20866,
  489. 20871, 21025, 21866, 28591, 28592, 28593,
  490. 28594, 28595, 28596, 28597, 28598, 28599,
  491. 28605, 38598,
  492. 50220, 50221, 50222, 51932, 51949, 54936,
  493. 57002, 57003, 57004, 57005, 57006, 57007,
  494. 57008, 57009, 57010, 57011,
  495. 65000, 65001};
  496. encoding_infos = new EncodingInfo [codepages.Length];
  497. for (int i = 0; i < codepages.Length; i++)
  498. encoding_infos [i] = new EncodingInfo (codepages [i]);
  499. }
  500. return encoding_infos;
  501. }
  502. [ComVisible (false)]
  503. public bool IsAlwaysNormalized ()
  504. {
  505. return IsAlwaysNormalized (NormalizationForm.FormC);
  506. }
  507. [ComVisible (false)]
  508. public virtual bool IsAlwaysNormalized (NormalizationForm form)
  509. {
  510. // umm, ASCIIEncoding should have overriden this method, no?
  511. return form == NormalizationForm.FormC && this is ASCIIEncoding;
  512. }
  513. // Get an encoding object for a specific web encoding name.
  514. public static Encoding GetEncoding (string name)
  515. {
  516. // Validate the parameters.
  517. if (name == null) {
  518. throw new ArgumentNullException ("name");
  519. }
  520. string converted = name.ToLowerInvariant ().Replace ('-', '_');
  521. // Builtin web encoding names and the corresponding code pages.
  522. switch (converted) {
  523. case "ascii":
  524. case "us_ascii":
  525. case "us":
  526. case "ansi_x3.4_1968":
  527. case "ansi_x3.4_1986":
  528. case "cp367":
  529. case "csascii":
  530. case "ibm367":
  531. case "iso_ir_6":
  532. case "iso646_us":
  533. case "iso_646.irv:1991":
  534. return GetEncoding (ASCIIEncoding.ASCII_CODE_PAGE);
  535. case "utf_7":
  536. case "csunicode11utf7":
  537. case "unicode_1_1_utf_7":
  538. case "unicode_2_0_utf_7":
  539. case "x_unicode_1_1_utf_7":
  540. case "x_unicode_2_0_utf_7":
  541. return GetEncoding (UTF7Encoding.UTF7_CODE_PAGE);
  542. case "utf_8":
  543. case "unicode_1_1_utf_8":
  544. case "unicode_2_0_utf_8":
  545. case "x_unicode_1_1_utf_8":
  546. case "x_unicode_2_0_utf_8":
  547. return GetEncoding (UTF8Encoding.UTF8_CODE_PAGE);
  548. case "utf_16":
  549. case "utf_16le":
  550. case "ucs_2":
  551. case "unicode":
  552. case "iso_10646_ucs2":
  553. return GetEncoding (UnicodeEncoding.UNICODE_CODE_PAGE);
  554. case "unicodefffe":
  555. case "utf_16be":
  556. return GetEncoding (UnicodeEncoding.BIG_UNICODE_CODE_PAGE);
  557. case "utf_32":
  558. case "utf_32le":
  559. case "ucs_4":
  560. return GetEncoding (UTF32Encoding.UTF32_CODE_PAGE);
  561. case "utf_32be":
  562. return GetEncoding (UTF32Encoding.BIG_UTF32_CODE_PAGE);
  563. case "iso_8859_1":
  564. case "latin1":
  565. return GetEncoding (Latin1Encoding.ISOLATIN_CODE_PAGE);
  566. }
  567. // Try to obtain a web encoding handler from the I18N handler.
  568. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
  569. if (enc != null) {
  570. return enc;
  571. }
  572. // Build a web encoding class name.
  573. String encName = "System.Text.ENC" + converted;
  574. // Look for a code page converter in this assembly.
  575. Assembly assembly = Assembly.GetExecutingAssembly ();
  576. Type type = assembly.GetType (encName);
  577. if (type != null) {
  578. return (Encoding)(Activator.CreateInstance (type));
  579. }
  580. // Look in any assembly, in case the application
  581. // has provided its own code page handler.
  582. type = Type.GetType (encName);
  583. if (type != null) {
  584. return (Encoding)(Activator.CreateInstance (type));
  585. }
  586. // We have no idea how to handle this encoding name.
  587. throw new ArgumentException (String.Format ("Encoding name '{0}' not "
  588. + "supported", name), "name");
  589. }
  590. #endif // !ECMA_COMPAT
  591. // Get a hash code for this instance.
  592. public override int GetHashCode ()
  593. {
  594. return DecoderFallback.GetHashCode () << 24 + EncoderFallback.GetHashCode () << 16 + codePage;
  595. }
  596. // Get the maximum number of bytes needed to encode a
  597. // specified number of characters.
  598. public abstract int GetMaxByteCount (int charCount);
  599. // Get the maximum number of characters needed to decode a
  600. // specified number of bytes.
  601. public abstract int GetMaxCharCount (int byteCount);
  602. // Get the identifying preamble for this encoding.
  603. public virtual byte[] GetPreamble ()
  604. {
  605. return EmptyArray<byte>.Value;
  606. }
  607. // Decode a buffer of bytes into a string.
  608. public virtual String GetString (byte[] bytes, int index, int count)
  609. {
  610. return new String (GetChars(bytes, index, count));
  611. }
  612. public virtual String GetString (byte[] bytes)
  613. {
  614. if (bytes == null)
  615. throw new ArgumentNullException ("bytes");
  616. return GetString (bytes, 0, bytes.Length);
  617. }
  618. #if !ECMA_COMPAT
  619. internal bool is_mail_news_display;
  620. internal bool is_mail_news_save;
  621. internal bool is_browser_save = false;
  622. internal bool is_browser_display = false;
  623. internal string body_name;
  624. internal string encoding_name;
  625. internal string header_name;
  626. internal string web_name;
  627. // Get the mail body name for this encoding.
  628. public virtual String BodyName
  629. {
  630. get {
  631. return body_name;
  632. }
  633. }
  634. // Get the code page represented by this object.
  635. public virtual int CodePage
  636. {
  637. get {
  638. return codePage;
  639. }
  640. }
  641. // Get the human-readable name for this encoding.
  642. public virtual String EncodingName
  643. {
  644. get {
  645. return encoding_name;
  646. }
  647. }
  648. // Get the mail agent header name for this encoding.
  649. public virtual String HeaderName
  650. {
  651. get {
  652. return header_name;
  653. }
  654. }
  655. // Determine if this encoding can be displayed in a Web browser.
  656. public virtual bool IsBrowserDisplay
  657. {
  658. get {
  659. return is_browser_display;
  660. }
  661. }
  662. // Determine if this encoding can be saved from a Web browser.
  663. public virtual bool IsBrowserSave
  664. {
  665. get {
  666. return is_browser_save;
  667. }
  668. }
  669. // Determine if this encoding can be displayed in a mail/news agent.
  670. public virtual bool IsMailNewsDisplay
  671. {
  672. get {
  673. return is_mail_news_display;
  674. }
  675. }
  676. // Determine if this encoding can be saved from a mail/news agent.
  677. public virtual bool IsMailNewsSave
  678. {
  679. get {
  680. return is_mail_news_save;
  681. }
  682. }
  683. // Get the IANA-preferred Web name for this encoding.
  684. public virtual String WebName
  685. {
  686. get {
  687. return web_name;
  688. }
  689. }
  690. // Get the Windows code page represented by this object.
  691. public virtual int WindowsCodePage
  692. {
  693. get {
  694. // We make no distinction between normal and
  695. // Windows code pages in this implementation.
  696. return windows_code_page;
  697. }
  698. }
  699. #endif // !ECMA_COMPAT
  700. // Storage for standard encoding objects.
  701. static volatile Encoding asciiEncoding;
  702. static volatile Encoding bigEndianEncoding;
  703. static volatile Encoding defaultEncoding;
  704. static volatile Encoding utf7Encoding;
  705. static volatile Encoding utf8EncodingWithMarkers;
  706. static volatile Encoding utf8EncodingWithoutMarkers;
  707. static volatile Encoding unicodeEncoding;
  708. static volatile Encoding isoLatin1Encoding;
  709. static volatile Encoding utf8EncodingUnsafe;
  710. static volatile Encoding utf32Encoding;
  711. static volatile Encoding bigEndianUTF32Encoding;
  712. static readonly object lockobj = new object ();
  713. // Get the standard ASCII encoding object.
  714. public static Encoding ASCII
  715. {
  716. get {
  717. if (asciiEncoding == null) {
  718. lock (lockobj) {
  719. if (asciiEncoding == null) {
  720. asciiEncoding = new ASCIIEncoding ();
  721. // asciiEncoding.is_readonly = true;
  722. }
  723. }
  724. }
  725. return asciiEncoding;
  726. }
  727. }
  728. // Get the standard big-endian Unicode encoding object.
  729. public static Encoding BigEndianUnicode
  730. {
  731. get {
  732. if (bigEndianEncoding == null) {
  733. lock (lockobj) {
  734. if (bigEndianEncoding == null) {
  735. bigEndianEncoding = new UnicodeEncoding (true, true);
  736. // bigEndianEncoding.is_readonly = true;
  737. }
  738. }
  739. }
  740. return bigEndianEncoding;
  741. }
  742. }
  743. [MethodImpl (MethodImplOptions.InternalCall)]
  744. extern internal static string InternalCodePage (ref int code_page);
  745. // Get the default encoding object.
  746. public static Encoding Default
  747. {
  748. get {
  749. if (defaultEncoding == null) {
  750. lock (lockobj) {
  751. if (defaultEncoding == null) {
  752. #if MOBILE
  753. defaultEncoding = UTF8;
  754. #else
  755. // See if the underlying system knows what
  756. // code page handler we should be using.
  757. int code_page = 1;
  758. string code_page_name = InternalCodePage (ref code_page);
  759. try {
  760. if (code_page == -1)
  761. defaultEncoding = GetEncoding (code_page_name);
  762. else {
  763. // map the codepage from internal to our numbers
  764. code_page = code_page & 0x0fffffff;
  765. switch (code_page){
  766. case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
  767. case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
  768. case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
  769. case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
  770. case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
  771. case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
  772. }
  773. defaultEncoding = GetEncoding (code_page);
  774. }
  775. } catch (NotSupportedException) {
  776. // code_page is not supported on underlying platform
  777. defaultEncoding = UTF8Unmarked;
  778. } catch (ArgumentException) {
  779. // code_page_name is not a valid code page, or is
  780. // not supported by underlying OS
  781. defaultEncoding = UTF8Unmarked;
  782. }
  783. defaultEncoding.is_readonly = true;
  784. #endif
  785. }
  786. }
  787. }
  788. return defaultEncoding;
  789. }
  790. }
  791. // Get the ISO Latin1 encoding object.
  792. private static Encoding ISOLatin1
  793. {
  794. get {
  795. if (isoLatin1Encoding == null) {
  796. lock (lockobj) {
  797. if (isoLatin1Encoding == null) {
  798. isoLatin1Encoding = new Latin1Encoding ();
  799. // isoLatin1Encoding.is_readonly = true;
  800. }
  801. }
  802. }
  803. return isoLatin1Encoding;
  804. }
  805. }
  806. // Get the standard UTF-7 encoding object.
  807. #if ECMA_COMPAT
  808. private
  809. #else
  810. public
  811. #endif
  812. static Encoding UTF7
  813. {
  814. get {
  815. if (utf7Encoding == null) {
  816. lock (lockobj) {
  817. if (utf7Encoding == null) {
  818. utf7Encoding = new UTF7Encoding ();
  819. // utf7Encoding.is_readonly = true;
  820. }
  821. }
  822. }
  823. return utf7Encoding;
  824. }
  825. }
  826. // Get the standard UTF-8 encoding object.
  827. public static Encoding UTF8
  828. {
  829. get {
  830. if (utf8EncodingWithMarkers == null) {
  831. lock (lockobj) {
  832. if (utf8EncodingWithMarkers == null) {
  833. utf8EncodingWithMarkers = new UTF8Encoding (true);
  834. // utf8EncodingWithMarkers.is_readonly = true;
  835. }
  836. }
  837. }
  838. return utf8EncodingWithMarkers;
  839. }
  840. }
  841. //
  842. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  843. //
  844. internal static Encoding UTF8Unmarked {
  845. get {
  846. if (utf8EncodingWithoutMarkers == null) {
  847. lock (lockobj){
  848. if (utf8EncodingWithoutMarkers == null){
  849. utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
  850. // utf8EncodingWithoutMarkers.is_readonly = true;
  851. }
  852. }
  853. }
  854. return utf8EncodingWithoutMarkers;
  855. }
  856. }
  857. //
  858. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  859. //
  860. internal static Encoding UTF8UnmarkedUnsafe {
  861. get {
  862. if (utf8EncodingUnsafe == null) {
  863. lock (lockobj){
  864. if (utf8EncodingUnsafe == null){
  865. utf8EncodingUnsafe = new UTF8Encoding (false, false);
  866. utf8EncodingUnsafe.is_readonly = false;
  867. utf8EncodingUnsafe.DecoderFallback = new DecoderReplacementFallback (String.Empty);
  868. utf8EncodingUnsafe.is_readonly = true;
  869. }
  870. }
  871. }
  872. return utf8EncodingUnsafe;
  873. }
  874. }
  875. // Get the standard little-endian Unicode encoding object.
  876. public static Encoding Unicode
  877. {
  878. get {
  879. if (unicodeEncoding == null) {
  880. lock (lockobj) {
  881. if (unicodeEncoding == null) {
  882. unicodeEncoding = new UnicodeEncoding (false, true);
  883. // unicodeEncoding.is_readonly = true;
  884. }
  885. }
  886. }
  887. return unicodeEncoding;
  888. }
  889. }
  890. // Get the standard little-endian UTF-32 encoding object.
  891. public static Encoding UTF32
  892. {
  893. get {
  894. if (utf32Encoding == null) {
  895. lock (lockobj) {
  896. if (utf32Encoding == null) {
  897. utf32Encoding = new UTF32Encoding (false, true);
  898. // utf32Encoding.is_readonly = true;
  899. }
  900. }
  901. }
  902. return utf32Encoding;
  903. }
  904. }
  905. // Get the standard big-endian UTF-32 encoding object.
  906. internal static Encoding BigEndianUTF32
  907. {
  908. get {
  909. if (bigEndianUTF32Encoding == null) {
  910. lock (lockobj) {
  911. if (bigEndianUTF32Encoding == null) {
  912. bigEndianUTF32Encoding = new UTF32Encoding (true, true);
  913. // bigEndianUTF32Encoding.is_readonly = true;
  914. }
  915. }
  916. }
  917. return bigEndianUTF32Encoding;
  918. }
  919. }
  920. // Forwarding decoder implementation.
  921. private sealed class ForwardingDecoder : Decoder
  922. {
  923. private Encoding encoding;
  924. // Constructor.
  925. public ForwardingDecoder (Encoding enc)
  926. {
  927. encoding = enc;
  928. DecoderFallback fallback = encoding.DecoderFallback;
  929. if (fallback != null)
  930. Fallback = fallback;
  931. }
  932. // Override inherited methods.
  933. public override int GetCharCount (byte[] bytes, int index, int count)
  934. {
  935. return encoding.GetCharCount (bytes, index, count);
  936. }
  937. public override int GetChars (byte[] bytes, int byteIndex,
  938. int byteCount, char[] chars,
  939. int charIndex)
  940. {
  941. return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  942. }
  943. } // class ForwardingDecoder
  944. // Forwarding encoder implementation.
  945. private sealed class ForwardingEncoder : Encoder
  946. {
  947. private Encoding encoding;
  948. // Constructor.
  949. public ForwardingEncoder (Encoding enc)
  950. {
  951. encoding = enc;
  952. EncoderFallback fallback = encoding.EncoderFallback;
  953. if (fallback != null)
  954. Fallback = fallback;
  955. }
  956. // Override inherited methods.
  957. public override int GetByteCount (char[] chars, int index, int count, bool flush)
  958. {
  959. return encoding.GetByteCount (chars, index, count);
  960. }
  961. public override int GetBytes (char[] chars, int charIndex,
  962. int charCount, byte[] bytes,
  963. int byteCount, bool flush)
  964. {
  965. return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
  966. }
  967. } // class ForwardingEncoder
  968. [CLSCompliantAttribute(false)]
  969. [ComVisible (false)]
  970. public unsafe virtual int GetByteCount (char *chars, int count)
  971. {
  972. if (chars == null)
  973. throw new ArgumentNullException ("chars");
  974. if (count < 0)
  975. throw new ArgumentOutOfRangeException ("count");
  976. char [] c = new char [count];
  977. for (int p = 0; p < count; p++)
  978. c [p] = chars [p];
  979. return GetByteCount (c);
  980. }
  981. [CLSCompliantAttribute(false)]
  982. [ComVisible (false)]
  983. public unsafe virtual int GetCharCount (byte *bytes, int count)
  984. {
  985. if (bytes == null)
  986. throw new ArgumentNullException ("bytes");
  987. if (count < 0)
  988. throw new ArgumentOutOfRangeException ("count");
  989. byte [] ba = new byte [count];
  990. for (int i = 0; i < count; i++)
  991. ba [i] = bytes [i];
  992. return GetCharCount (ba, 0, count);
  993. }
  994. [CLSCompliantAttribute(false)]
  995. [ComVisible (false)]
  996. public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  997. {
  998. if (bytes == null)
  999. throw new ArgumentNullException ("bytes");
  1000. if (chars == null)
  1001. throw new ArgumentNullException ("chars");
  1002. if (charCount < 0)
  1003. throw new ArgumentOutOfRangeException ("charCount");
  1004. if (byteCount < 0)
  1005. throw new ArgumentOutOfRangeException ("byteCount");
  1006. byte [] ba = new byte [byteCount];
  1007. for (int i = 0; i < byteCount; i++)
  1008. ba [i] = bytes [i];
  1009. char [] ret = GetChars (ba, 0, byteCount);
  1010. int top = ret.Length;
  1011. if (top > charCount)
  1012. throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
  1013. for (int i = 0; i < top; i++)
  1014. chars [i] = ret [i];
  1015. return top;
  1016. }
  1017. [CLSCompliantAttribute(false)]
  1018. [ComVisible (false)]
  1019. public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  1020. {
  1021. if (bytes == null)
  1022. throw new ArgumentNullException ("bytes");
  1023. if (chars == null)
  1024. throw new ArgumentNullException ("chars");
  1025. if (charCount < 0)
  1026. throw new ArgumentOutOfRangeException ("charCount");
  1027. if (byteCount < 0)
  1028. throw new ArgumentOutOfRangeException ("byteCount");
  1029. char [] c = new char [charCount];
  1030. for (int i = 0; i < charCount; i++)
  1031. c [i] = chars [i];
  1032. byte [] b = GetBytes (c, 0, charCount);
  1033. int top = b.Length;
  1034. if (top > byteCount)
  1035. throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
  1036. for (int i = 0; i < top; i++)
  1037. bytes [i] = b [i];
  1038. return b.Length;
  1039. }
  1040. }; // class Encoding
  1041. }; // namespace System.Text