Encoding.cs 31 KB

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