Encoding.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. public abstract class Encoding
  36. #if NET_2_0
  37. : ICloneable
  38. #endif
  39. {
  40. // Code page used by this encoding.
  41. internal int codePage;
  42. internal int windows_code_page;
  43. bool is_readonly = true;
  44. // Constructor.
  45. protected Encoding ()
  46. {
  47. }
  48. #if ECMA_COMPAT
  49. protected internal
  50. #else
  51. protected
  52. #endif
  53. Encoding (int codePage)
  54. {
  55. this.codePage = windows_code_page = codePage;
  56. #if NET_2_0
  57. switch (codePage) {
  58. default:
  59. // MS has "InternalBestFit{Decoder|Encoder}Fallback
  60. // here, but we dunno what they are for.
  61. decoder_fallback = DecoderFallback.ReplacementFallback;
  62. encoder_fallback = EncoderFallback.ReplacementFallback;
  63. break;
  64. case 20127: // ASCII
  65. case 54936: // GB18030
  66. decoder_fallback = DecoderFallback.ReplacementFallback;
  67. encoder_fallback = EncoderFallback.ReplacementFallback;
  68. break;
  69. case 1200: // UTF16
  70. case 1201: // UTF16
  71. case 12000: // UTF32
  72. case 12001: // UTF32
  73. case 65000: // UTF7
  74. case 65001: // UTF8
  75. decoder_fallback = new DecoderReplacementFallback (String.Empty);
  76. encoder_fallback = new EncoderReplacementFallback (String.Empty);
  77. break;
  78. }
  79. #endif
  80. }
  81. // until we change the callers:
  82. internal static string _ (string arg) {
  83. return arg;
  84. }
  85. #if NET_2_0
  86. DecoderFallback decoder_fallback;
  87. EncoderFallback encoder_fallback;
  88. [ComVisible (false)]
  89. public bool IsReadOnly {
  90. get { return is_readonly; }
  91. }
  92. [MonoTODO ("not used yet")]
  93. [ComVisible (false)]
  94. public DecoderFallback DecoderFallback {
  95. get {
  96. if (decoder_fallback == null)
  97. decoder_fallback = new DecoderReplacementFallback (String.Empty);
  98. return decoder_fallback;
  99. }
  100. set {
  101. if (IsReadOnly)
  102. throw new InvalidOperationException ("This Encoding is readonly.");
  103. if (value == null)
  104. throw new ArgumentNullException ();
  105. decoder_fallback = value;
  106. }
  107. }
  108. [MonoTODO ("not used yet")]
  109. [ComVisible (false)]
  110. public EncoderFallback EncoderFallback {
  111. get {
  112. if (encoder_fallback == null)
  113. encoder_fallback = new EncoderReplacementFallback (String.Empty);
  114. return encoder_fallback;
  115. }
  116. set {
  117. if (IsReadOnly)
  118. throw new InvalidOperationException ("This Encoding is readonly.");
  119. if (value == null)
  120. throw new ArgumentNullException ();
  121. encoder_fallback = value;
  122. }
  123. }
  124. internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d)
  125. {
  126. if (e != null)
  127. encoder_fallback = e;
  128. if (d != null)
  129. decoder_fallback = d;
  130. }
  131. #endif
  132. // Convert between two encodings.
  133. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  134. byte[] bytes)
  135. {
  136. if (srcEncoding == null) {
  137. throw new ArgumentNullException ("srcEncoding");
  138. }
  139. if (dstEncoding == null) {
  140. throw new ArgumentNullException ("dstEncoding");
  141. }
  142. if (bytes == null) {
  143. throw new ArgumentNullException ("bytes");
  144. }
  145. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
  146. }
  147. public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
  148. byte[] bytes, int index, int count)
  149. {
  150. if (srcEncoding == null) {
  151. throw new ArgumentNullException ("srcEncoding");
  152. }
  153. if (dstEncoding == null) {
  154. throw new ArgumentNullException ("dstEncoding");
  155. }
  156. if (bytes == null) {
  157. throw new ArgumentNullException ("bytes");
  158. }
  159. if (index < 0 || index > bytes.Length) {
  160. throw new ArgumentOutOfRangeException
  161. ("index", _("ArgRange_Array"));
  162. }
  163. if (count < 0 || (bytes.Length - index) < count) {
  164. throw new ArgumentOutOfRangeException
  165. ("count", _("ArgRange_Array"));
  166. }
  167. return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
  168. }
  169. // Determine if two Encoding objects are equal.
  170. public override bool Equals (Object obj)
  171. {
  172. Encoding enc = (obj as Encoding);
  173. if (enc != null) {
  174. return (codePage == enc.codePage);
  175. } else {
  176. return false;
  177. }
  178. }
  179. // Get the number of characters needed to encode a character buffer.
  180. public abstract int GetByteCount (char[] chars, int index, int count);
  181. // Convenience wrappers for "GetByteCount".
  182. public virtual int GetByteCount (String s)
  183. {
  184. if (s != null) {
  185. char[] chars = s.ToCharArray ();
  186. return GetByteCount (chars, 0, chars.Length);
  187. } else {
  188. throw new ArgumentNullException ("s");
  189. }
  190. }
  191. public virtual int GetByteCount (char[] chars)
  192. {
  193. if (chars != null) {
  194. return GetByteCount (chars, 0, chars.Length);
  195. } else {
  196. throw new ArgumentNullException ("chars");
  197. }
  198. }
  199. // Get the bytes that result from encoding a character buffer.
  200. public abstract int GetBytes (char[] chars, int charIndex, int charCount,
  201. byte[] bytes, int byteIndex);
  202. // Convenience wrappers for "GetBytes".
  203. public virtual int GetBytes (String s, int charIndex, int charCount,
  204. byte[] bytes, int byteIndex)
  205. {
  206. if (s == null) {
  207. throw new ArgumentNullException ("s");
  208. }
  209. return GetBytes (s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
  210. }
  211. public virtual byte[] GetBytes (String s)
  212. {
  213. if (s == null) {
  214. throw new ArgumentNullException ("s");
  215. }
  216. char[] chars = s.ToCharArray ();
  217. int numBytes = GetByteCount (chars, 0, chars.Length);
  218. byte[] bytes = new byte [numBytes];
  219. GetBytes (chars, 0, chars.Length, bytes, 0);
  220. return bytes;
  221. }
  222. public virtual byte[] GetBytes (char[] chars, int index, int count)
  223. {
  224. int numBytes = GetByteCount (chars, index, count);
  225. byte[] bytes = new byte [numBytes];
  226. GetBytes (chars, index, count, bytes, 0);
  227. return bytes;
  228. }
  229. public virtual byte[] GetBytes (char[] chars)
  230. {
  231. int numBytes = GetByteCount (chars, 0, chars.Length);
  232. byte[] bytes = new byte [numBytes];
  233. GetBytes (chars, 0, chars.Length, bytes, 0);
  234. return bytes;
  235. }
  236. // Get the number of characters needed to decode a byte buffer.
  237. public abstract int GetCharCount (byte[] bytes, int index, int count);
  238. // Convenience wrappers for "GetCharCount".
  239. public virtual int GetCharCount (byte[] bytes)
  240. {
  241. if (bytes == null) {
  242. throw new ArgumentNullException ("bytes");
  243. }
  244. return GetCharCount (bytes, 0, bytes.Length);
  245. }
  246. // Get the characters that result from decoding a byte buffer.
  247. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
  248. char[] chars, int charIndex);
  249. // Convenience wrappers for "GetChars".
  250. public virtual char[] GetChars (byte[] bytes, int index, int count)
  251. {
  252. int numChars = GetCharCount (bytes, index, count);
  253. char[] chars = new char [numChars];
  254. GetChars (bytes, index, count, chars, 0);
  255. return chars;
  256. }
  257. public virtual char[] GetChars (byte[] bytes)
  258. {
  259. if (bytes == null) {
  260. throw new ArgumentNullException ("bytes");
  261. }
  262. int numChars = GetCharCount (bytes, 0, bytes.Length);
  263. char[] chars = new char [numChars];
  264. GetChars (bytes, 0, bytes.Length, chars, 0);
  265. return chars;
  266. }
  267. // Get a decoder that forwards requests to this object.
  268. public virtual Decoder GetDecoder ()
  269. {
  270. return new ForwardingDecoder (this);
  271. }
  272. // Get an encoder that forwards requests to this object.
  273. public virtual Encoder GetEncoder ()
  274. {
  275. return new ForwardingEncoder (this);
  276. }
  277. // Loaded copy of the "I18N" assembly. We need to move
  278. // this into a class in "System.Private" eventually.
  279. private static Assembly i18nAssembly;
  280. private static bool i18nDisabled;
  281. // Invoke a specific method on the "I18N" manager object.
  282. // Returns NULL if the method failed.
  283. private static Object InvokeI18N (String name, params Object[] args)
  284. {
  285. lock (lockobj) {
  286. // Bail out if we previously detected that there
  287. // is insufficent engine support for I18N handling.
  288. if (i18nDisabled) {
  289. return null;
  290. }
  291. // Find or load the "I18N" assembly.
  292. if (i18nAssembly == null) {
  293. try {
  294. try {
  295. i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
  296. } catch (NotImplementedException) {
  297. // Assembly loading unsupported by the engine.
  298. i18nDisabled = true;
  299. return null;
  300. }
  301. if (i18nAssembly == null) {
  302. return null;
  303. }
  304. } catch (SystemException) {
  305. return null;
  306. }
  307. }
  308. // Find the "I18N.Common.Manager" class.
  309. Type managerClass;
  310. try {
  311. managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
  312. } catch (NotImplementedException) {
  313. // "GetType" is not supported by the engine.
  314. i18nDisabled = true;
  315. return null;
  316. }
  317. if (managerClass == null) {
  318. return null;
  319. }
  320. // Get the value of the "PrimaryManager" property.
  321. Object manager;
  322. try {
  323. manager = managerClass.InvokeMember
  324. ("PrimaryManager",
  325. BindingFlags.GetProperty |
  326. BindingFlags.Static |
  327. BindingFlags.Public,
  328. null, null, null, null, null, null);
  329. if (manager == null) {
  330. return null;
  331. }
  332. } catch (MissingMethodException) {
  333. return null;
  334. } catch (SecurityException) {
  335. return null;
  336. } catch (NotImplementedException) {
  337. // "InvokeMember" is not supported by the engine.
  338. i18nDisabled = true;
  339. return null;
  340. }
  341. // Invoke the requested method on the manager.
  342. try {
  343. return managerClass.InvokeMember
  344. (name,
  345. BindingFlags.InvokeMethod |
  346. BindingFlags.Instance |
  347. BindingFlags.Public,
  348. null, manager, args, null, null, null);
  349. } catch (MissingMethodException) {
  350. return null;
  351. } catch (SecurityException) {
  352. return null;
  353. }
  354. }
  355. }
  356. // Get an encoder for a specific code page.
  357. #if ECMA_COMPAT
  358. private
  359. #else
  360. public
  361. #endif
  362. static Encoding GetEncoding (int codePage)
  363. {
  364. // Check for the builtin code pages first.
  365. switch (codePage) {
  366. case 0: return Default;
  367. case ASCIIEncoding.ASCII_CODE_PAGE:
  368. return ASCII;
  369. case UTF7Encoding.UTF7_CODE_PAGE:
  370. return UTF7;
  371. case UTF8Encoding.UTF8_CODE_PAGE:
  372. return UTF8;
  373. #if NET_2_0
  374. case UTF32Encoding.UTF32_CODE_PAGE:
  375. return UTF32;
  376. #endif
  377. case UnicodeEncoding.UNICODE_CODE_PAGE:
  378. return Unicode;
  379. case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
  380. return BigEndianUnicode;
  381. case Latin1Encoding.ISOLATIN_CODE_PAGE:
  382. return ISOLatin1;
  383. default: break;
  384. }
  385. // Try to obtain a code page handler from the I18N handler.
  386. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codePage));
  387. if (enc != null) {
  388. enc.is_readonly = true;
  389. return enc;
  390. }
  391. // Build a code page class name.
  392. String cpName = "System.Text.CP" + codePage.ToString ();
  393. // Look for a code page converter in this assembly.
  394. Assembly assembly = Assembly.GetExecutingAssembly ();
  395. Type type = assembly.GetType (cpName);
  396. if (type != null) {
  397. enc = (Encoding)(Activator.CreateInstance (type));
  398. enc.is_readonly = true;
  399. return enc;
  400. }
  401. // Look in any assembly, in case the application
  402. // has provided its own code page handler.
  403. type = Type.GetType (cpName);
  404. if (type != null) {
  405. enc = (Encoding)(Activator.CreateInstance (type));
  406. enc.is_readonly = true;
  407. return enc;
  408. }
  409. // We have no idea how to handle this code page.
  410. throw new NotSupportedException
  411. (String.Format ("CodePage {0} not supported", codePage.ToString ()));
  412. }
  413. #if !ECMA_COMPAT
  414. #if NET_2_0
  415. public virtual object Clone ()
  416. {
  417. return MemberwiseClone ();
  418. }
  419. public static Encoding GetEncoding (int codePage,
  420. EncoderFallback encoderFallback, DecoderFallback decoderFallback)
  421. {
  422. if (encoderFallback == null)
  423. throw new ArgumentNullException ("encoderFallback");
  424. if (decoderFallback == null)
  425. throw new ArgumentNullException ("decoderFallback");
  426. Encoding e = GetEncoding (codePage).Clone () as Encoding;
  427. e.is_readonly = false;
  428. e.encoder_fallback = encoderFallback;
  429. e.decoder_fallback = decoderFallback;
  430. return e;
  431. }
  432. public static Encoding GetEncoding (string name,
  433. EncoderFallback encoderFallback, DecoderFallback decoderFallback)
  434. {
  435. if (encoderFallback == null)
  436. throw new ArgumentNullException ("encoderFallback");
  437. if (decoderFallback == null)
  438. throw new ArgumentNullException ("decoderFallback");
  439. Encoding e = GetEncoding (name).Clone () as Encoding;
  440. e.is_readonly = false;
  441. e.encoder_fallback = encoderFallback;
  442. e.decoder_fallback = decoderFallback;
  443. return e;
  444. }
  445. #endif
  446. // Table of builtin web encoding names and the corresponding code pages.
  447. private static readonly object[] encodings =
  448. {
  449. ASCIIEncoding.ASCII_CODE_PAGE,
  450. "ascii", "us_ascii", "us", "ansi_x3.4_1968",
  451. "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
  452. "iso_ir_6", "iso646_us", "iso_646.irv:1991",
  453. UTF7Encoding.UTF7_CODE_PAGE,
  454. "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
  455. "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
  456. "x_unicode_2_0_utf_7",
  457. UTF8Encoding.UTF8_CODE_PAGE,
  458. "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
  459. "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
  460. UnicodeEncoding.UNICODE_CODE_PAGE,
  461. "utf_16", "UTF_16LE", "ucs_2", "unicode",
  462. "iso_10646_ucs2",
  463. UnicodeEncoding.BIG_UNICODE_CODE_PAGE,
  464. "unicodefffe", "utf_16be",
  465. #if NET_2_0
  466. UTF32Encoding.UTF32_CODE_PAGE,
  467. "utf_32", "UTF_32LE", "ucs_4",
  468. UTF32Encoding.BIG_UTF32_CODE_PAGE,
  469. "UTF_32BE",
  470. #endif
  471. Latin1Encoding.ISOLATIN_CODE_PAGE,
  472. "iso_8859_1", "latin1"
  473. };
  474. // Get an encoding object for a specific web encoding name.
  475. public static Encoding GetEncoding (String name)
  476. {
  477. // Validate the parameters.
  478. if (name == null) {
  479. throw new ArgumentNullException ("name");
  480. }
  481. string converted = name.ToLowerInvariant ().Replace ('-', '_');
  482. // Search the table for a name match.
  483. int code = 0;
  484. for (int i = 0; i < encodings.Length; ++i) {
  485. object o = encodings [i];
  486. if (o is int){
  487. code = (int) o;
  488. continue;
  489. }
  490. if (converted == ((string)encodings [i]))
  491. return GetEncoding (code);
  492. }
  493. // Try to obtain a web encoding handler from the I18N handler.
  494. Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
  495. if (enc != null) {
  496. return enc;
  497. }
  498. // Build a web encoding class name.
  499. String encName = "System.Text.ENC" + converted;
  500. // Look for a code page converter in this assembly.
  501. Assembly assembly = Assembly.GetExecutingAssembly ();
  502. Type type = assembly.GetType (encName);
  503. if (type != null) {
  504. return (Encoding)(Activator.CreateInstance (type));
  505. }
  506. // Look in any assembly, in case the application
  507. // has provided its own code page handler.
  508. type = Type.GetType (encName);
  509. if (type != null) {
  510. return (Encoding)(Activator.CreateInstance (type));
  511. }
  512. // We have no idea how to handle this encoding name.
  513. throw new NotSupportedException (String.Format ("Encoding name `{0}' not supported", name));
  514. }
  515. #endif // !ECMA_COMPAT
  516. // Get a hash code for this instance.
  517. public override int GetHashCode ()
  518. {
  519. return codePage;
  520. }
  521. // Get the maximum number of bytes needed to encode a
  522. // specified number of characters.
  523. public abstract int GetMaxByteCount (int charCount);
  524. // Get the maximum number of characters needed to decode a
  525. // specified number of bytes.
  526. public abstract int GetMaxCharCount (int byteCount);
  527. // Get the identifying preamble for this encoding.
  528. public virtual byte[] GetPreamble ()
  529. {
  530. return new byte [0];
  531. }
  532. // Decode a buffer of bytes into a string.
  533. public virtual String GetString (byte[] bytes, int index, int count)
  534. {
  535. return new String (GetChars(bytes, index, count));
  536. }
  537. public virtual String GetString (byte[] bytes)
  538. {
  539. return new String (GetChars(bytes));
  540. }
  541. #if !ECMA_COMPAT
  542. internal string body_name;
  543. internal string encoding_name;
  544. internal string header_name;
  545. internal bool is_mail_news_display;
  546. internal bool is_mail_news_save;
  547. internal bool is_browser_save = false;
  548. internal bool is_browser_display = false;
  549. internal string web_name;
  550. // Get the mail body name for this encoding.
  551. public virtual String BodyName
  552. {
  553. get {
  554. return body_name;
  555. }
  556. }
  557. // Get the code page represented by this object.
  558. public virtual int CodePage
  559. {
  560. get {
  561. return codePage;
  562. }
  563. }
  564. // Get the human-readable name for this encoding.
  565. public virtual String EncodingName
  566. {
  567. get {
  568. return encoding_name;
  569. }
  570. }
  571. // Get the mail agent header name for this encoding.
  572. public virtual String HeaderName
  573. {
  574. get {
  575. return header_name;
  576. }
  577. }
  578. // Determine if this encoding can be displayed in a Web browser.
  579. public virtual bool IsBrowserDisplay
  580. {
  581. get {
  582. return is_browser_display;
  583. }
  584. }
  585. // Determine if this encoding can be saved from a Web browser.
  586. public virtual bool IsBrowserSave
  587. {
  588. get {
  589. return is_browser_save;
  590. }
  591. }
  592. // Determine if this encoding can be displayed in a mail/news agent.
  593. public virtual bool IsMailNewsDisplay
  594. {
  595. get {
  596. return is_mail_news_display;
  597. }
  598. }
  599. // Determine if this encoding can be saved from a mail/news agent.
  600. public virtual bool IsMailNewsSave
  601. {
  602. get {
  603. return is_mail_news_save;
  604. }
  605. }
  606. // Get the IANA-preferred Web name for this encoding.
  607. public virtual String WebName
  608. {
  609. get {
  610. return web_name;
  611. }
  612. }
  613. // Get the Windows code page represented by this object.
  614. public virtual int WindowsCodePage
  615. {
  616. get {
  617. // We make no distinction between normal and
  618. // Windows code pages in this implementation.
  619. return windows_code_page;
  620. }
  621. }
  622. #endif // !ECMA_COMPAT
  623. // Storage for standard encoding objects.
  624. static volatile Encoding asciiEncoding;
  625. static volatile Encoding bigEndianEncoding;
  626. static volatile Encoding defaultEncoding;
  627. static volatile Encoding utf7Encoding;
  628. static volatile Encoding utf8EncodingWithMarkers;
  629. static volatile Encoding utf8EncodingWithoutMarkers;
  630. static volatile Encoding unicodeEncoding;
  631. static volatile Encoding isoLatin1Encoding;
  632. static volatile Encoding unixConsoleEncoding;
  633. #if NET_2_0
  634. static volatile Encoding utf32Encoding;
  635. #endif
  636. static readonly object lockobj = new object ();
  637. // Get the standard ASCII encoding object.
  638. public static Encoding ASCII
  639. {
  640. get {
  641. if (asciiEncoding == null) {
  642. lock (lockobj) {
  643. if (asciiEncoding == null) {
  644. asciiEncoding = new ASCIIEncoding ();
  645. asciiEncoding.is_readonly = true;
  646. }
  647. }
  648. }
  649. return asciiEncoding;
  650. }
  651. }
  652. // Get the standard big-endian Unicode encoding object.
  653. public static Encoding BigEndianUnicode
  654. {
  655. get {
  656. if (bigEndianEncoding == null) {
  657. lock (lockobj) {
  658. if (bigEndianEncoding == null) {
  659. bigEndianEncoding = new UnicodeEncoding (true, true);
  660. bigEndianEncoding.is_readonly = true;
  661. }
  662. }
  663. }
  664. return bigEndianEncoding;
  665. }
  666. }
  667. [MethodImpl (MethodImplOptions.InternalCall)]
  668. extern internal static string InternalCodePage (ref int code_page);
  669. // Get the default encoding object.
  670. public static Encoding Default
  671. {
  672. get {
  673. if (defaultEncoding == null) {
  674. lock (lockobj) {
  675. if (defaultEncoding == null) {
  676. // See if the underlying system knows what
  677. // code page handler we should be using.
  678. int code_page = 1;
  679. string code_page_name = InternalCodePage (ref code_page);
  680. try {
  681. if (code_page == -1)
  682. defaultEncoding = GetEncoding (code_page_name);
  683. else {
  684. // map the codepage from internal to our numbers
  685. code_page = code_page & 0x0fffffff;
  686. switch (code_page){
  687. case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
  688. case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
  689. case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
  690. case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
  691. case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
  692. case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
  693. }
  694. defaultEncoding = GetEncoding (code_page);
  695. }
  696. } catch (NotSupportedException) {
  697. defaultEncoding = UTF8Unmarked;
  698. }
  699. defaultEncoding.is_readonly = true;
  700. }
  701. }
  702. }
  703. return defaultEncoding;
  704. }
  705. }
  706. // Get the ISO Latin1 encoding object.
  707. private static Encoding ISOLatin1
  708. {
  709. get {
  710. if (isoLatin1Encoding == null) {
  711. lock (lockobj) {
  712. if (isoLatin1Encoding == null) {
  713. isoLatin1Encoding = new Latin1Encoding ();
  714. isoLatin1Encoding.is_readonly = true;
  715. }
  716. }
  717. }
  718. return isoLatin1Encoding;
  719. }
  720. }
  721. // Get the standard UTF-7 encoding object.
  722. #if ECMA_COMPAT
  723. private
  724. #else
  725. public
  726. #endif
  727. static Encoding UTF7
  728. {
  729. get {
  730. if (utf7Encoding == null) {
  731. lock (lockobj) {
  732. if (utf7Encoding == null) {
  733. utf7Encoding = new UTF7Encoding ();
  734. utf7Encoding.is_readonly = true;
  735. }
  736. }
  737. }
  738. return utf7Encoding;
  739. }
  740. }
  741. // Get the standard UTF-8 encoding object.
  742. public static Encoding UTF8
  743. {
  744. get {
  745. if (utf8EncodingWithMarkers == null) {
  746. lock (lockobj) {
  747. if (utf8EncodingWithMarkers == null) {
  748. utf8EncodingWithMarkers = new UTF8Encoding (true);
  749. utf8EncodingWithMarkers.is_readonly = true;
  750. }
  751. }
  752. }
  753. return utf8EncodingWithMarkers;
  754. }
  755. }
  756. //
  757. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  758. //
  759. internal static Encoding UTF8Unmarked {
  760. get {
  761. if (utf8EncodingWithoutMarkers == null) {
  762. lock (lockobj){
  763. if (utf8EncodingWithoutMarkers == null){
  764. utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
  765. utf8EncodingWithoutMarkers.is_readonly = true;
  766. }
  767. }
  768. }
  769. return utf8EncodingWithoutMarkers;
  770. }
  771. }
  772. // Get the standard little-endian Unicode encoding object.
  773. public static Encoding Unicode
  774. {
  775. get {
  776. if (unicodeEncoding == null) {
  777. lock (lockobj) {
  778. if (unicodeEncoding == null) {
  779. unicodeEncoding = new UnicodeEncoding (false, true);
  780. unicodeEncoding.is_readonly = true;
  781. }
  782. }
  783. }
  784. return unicodeEncoding;
  785. }
  786. }
  787. #if NET_2_0
  788. // Get the standard little-endian UTF-32 encoding object.
  789. public static Encoding UTF32
  790. {
  791. get {
  792. if (utf32Encoding == null) {
  793. lock (lockobj) {
  794. if (utf32Encoding == null) {
  795. utf32Encoding = new UTF32Encoding (false, true);
  796. utf32Encoding.is_readonly = true;
  797. }
  798. }
  799. }
  800. return utf32Encoding;
  801. }
  802. }
  803. #endif
  804. // Forwarding decoder implementation.
  805. private sealed class ForwardingDecoder : Decoder
  806. {
  807. private Encoding encoding;
  808. // Constructor.
  809. public ForwardingDecoder (Encoding enc)
  810. {
  811. encoding = enc;
  812. #if NET_2_0
  813. Fallback = encoding.DecoderFallback;
  814. #endif
  815. }
  816. // Override inherited methods.
  817. public override int GetCharCount (byte[] bytes, int index, int count)
  818. {
  819. return encoding.GetCharCount (bytes, index, count);
  820. }
  821. public override int GetChars (byte[] bytes, int byteIndex,
  822. int byteCount, char[] chars,
  823. int charIndex)
  824. {
  825. return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  826. }
  827. } // class ForwardingDecoder
  828. // Forwarding encoder implementation.
  829. private sealed class ForwardingEncoder : Encoder
  830. {
  831. private Encoding encoding;
  832. // Constructor.
  833. public ForwardingEncoder (Encoding enc)
  834. {
  835. encoding = enc;
  836. #if NET_2_0
  837. Fallback = encoding.EncoderFallback;
  838. #endif
  839. }
  840. // Override inherited methods.
  841. public override int GetByteCount (char[] chars, int index, int count, bool flush)
  842. {
  843. return encoding.GetByteCount (chars, index, count);
  844. }
  845. public override int GetBytes (char[] chars, int charIndex,
  846. int charCount, byte[] bytes,
  847. int byteCount, bool flush)
  848. {
  849. return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
  850. }
  851. } // class ForwardingEncoder
  852. #if NET_2_0
  853. [CLSCompliantAttribute(false)]
  854. public unsafe virtual int GetByteCount (char *chars, int count)
  855. {
  856. if (chars == null)
  857. throw new ArgumentNullException ("chars");
  858. if (count < 0)
  859. throw new ArgumentOutOfRangeException ("count");
  860. char [] c = new char [count];
  861. for (int p = 0; p < count; p++)
  862. c [p] = chars [p];
  863. return GetByteCount (c);
  864. }
  865. [CLSCompliantAttribute(false)]
  866. public unsafe virtual int GetCharCount (byte *bytes, int count)
  867. {
  868. if (bytes == null)
  869. throw new ArgumentNullException ("bytes");
  870. if (count < 0)
  871. throw new ArgumentOutOfRangeException ("count");
  872. byte [] ba = new byte [count];
  873. for (int i = 0; i < count; i++)
  874. ba [i] = bytes [i];
  875. return GetCharCount (ba, 0, count);
  876. }
  877. [CLSCompliantAttribute(false)]
  878. public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  879. {
  880. if (bytes == null)
  881. throw new ArgumentNullException ("bytes");
  882. if (chars == null)
  883. throw new ArgumentNullException ("chars");
  884. if (charCount < 0)
  885. throw new ArgumentOutOfRangeException ("charCount");
  886. if (byteCount < 0)
  887. throw new ArgumentOutOfRangeException ("byteCount");
  888. byte [] ba = new byte [byteCount];
  889. for (int i = 0; i < byteCount; i++)
  890. ba [i] = bytes [i];
  891. char [] ret = GetChars (ba, 0, byteCount);
  892. int top = ret.Length;
  893. if (top > charCount)
  894. throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
  895. for (int i = 0; i < top; i++)
  896. chars [i] = ret [i];
  897. return top;
  898. }
  899. [CLSCompliantAttribute(false)]
  900. public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  901. {
  902. if (bytes == null)
  903. throw new ArgumentNullException ("bytes");
  904. if (chars == null)
  905. throw new ArgumentNullException ("chars");
  906. if (charCount < 0)
  907. throw new ArgumentOutOfRangeException ("charCount");
  908. if (byteCount < 0)
  909. throw new ArgumentOutOfRangeException ("byteCount");
  910. char [] c = new char [charCount];
  911. for (int i = 0; i < charCount; i++)
  912. c [i] = chars [i];
  913. byte [] b = GetBytes (c, 0, charCount);
  914. int top = b.Length;
  915. if (top > byteCount)
  916. throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
  917. for (int i = 0; i < top; i++)
  918. bytes [i] = b [i];
  919. return b.Length;
  920. }
  921. #endif
  922. }; // class Encoding
  923. }; // namespace System.Text