Encoding.cs 30 KB

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