Encoding.cs 25 KB

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