Encoding.cs 27 KB

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