Encoding.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 !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. // 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 !MOONLIGHT
  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 !MOONLIGHT
  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 !MOONLIGHT
  772. case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
  773. #endif
  774. }
  775. defaultEncoding = GetEncoding (code_page);
  776. }
  777. } catch (NotSupportedException) {
  778. #if MOONLIGHT
  779. defaultEncoding = UTF8;
  780. #else
  781. // code_page is not supported on underlying platform
  782. defaultEncoding = UTF8Unmarked;
  783. #endif
  784. } catch (ArgumentException) {
  785. // code_page_name is not a valid code page, or is
  786. // not supported by underlying OS
  787. #if MOONLIGHT
  788. defaultEncoding = UTF8;
  789. #else
  790. defaultEncoding = UTF8Unmarked;
  791. #endif
  792. }
  793. defaultEncoding.is_readonly = true;
  794. }
  795. }
  796. }
  797. return defaultEncoding;
  798. }
  799. }
  800. #if !MOONLIGHT
  801. // Get the ISO Latin1 encoding object.
  802. private static Encoding ISOLatin1
  803. {
  804. get {
  805. if (isoLatin1Encoding == null) {
  806. lock (lockobj) {
  807. if (isoLatin1Encoding == null) {
  808. isoLatin1Encoding = new Latin1Encoding ();
  809. // isoLatin1Encoding.is_readonly = true;
  810. }
  811. }
  812. }
  813. return isoLatin1Encoding;
  814. }
  815. }
  816. #endif
  817. // Get the standard UTF-7 encoding object.
  818. #if ECMA_COMPAT
  819. private
  820. #else
  821. public
  822. #endif
  823. static Encoding UTF7
  824. {
  825. get {
  826. if (utf7Encoding == null) {
  827. lock (lockobj) {
  828. if (utf7Encoding == null) {
  829. utf7Encoding = new UTF7Encoding ();
  830. // utf7Encoding.is_readonly = true;
  831. }
  832. }
  833. }
  834. return utf7Encoding;
  835. }
  836. }
  837. // Get the standard UTF-8 encoding object.
  838. public static Encoding UTF8
  839. {
  840. get {
  841. if (utf8EncodingWithMarkers == null) {
  842. lock (lockobj) {
  843. if (utf8EncodingWithMarkers == null) {
  844. utf8EncodingWithMarkers = new UTF8Encoding (true);
  845. // utf8EncodingWithMarkers.is_readonly = true;
  846. }
  847. }
  848. }
  849. return utf8EncodingWithMarkers;
  850. }
  851. }
  852. //
  853. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  854. //
  855. internal static Encoding UTF8Unmarked {
  856. get {
  857. if (utf8EncodingWithoutMarkers == null) {
  858. lock (lockobj){
  859. if (utf8EncodingWithoutMarkers == null){
  860. utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
  861. // utf8EncodingWithoutMarkers.is_readonly = true;
  862. }
  863. }
  864. }
  865. return utf8EncodingWithoutMarkers;
  866. }
  867. }
  868. //
  869. // Only internal, to be used by the class libraries: Unmarked and non-input-validating
  870. //
  871. internal static Encoding UTF8UnmarkedUnsafe {
  872. get {
  873. if (utf8EncodingUnsafe == null) {
  874. lock (lockobj){
  875. if (utf8EncodingUnsafe == null){
  876. utf8EncodingUnsafe = new UTF8Encoding (false, false);
  877. utf8EncodingUnsafe.is_readonly = false;
  878. utf8EncodingUnsafe.DecoderFallback = new DecoderReplacementFallback (String.Empty);
  879. utf8EncodingUnsafe.is_readonly = true;
  880. }
  881. }
  882. }
  883. return utf8EncodingUnsafe;
  884. }
  885. }
  886. // Get the standard little-endian Unicode encoding object.
  887. public static Encoding Unicode
  888. {
  889. get {
  890. if (unicodeEncoding == null) {
  891. lock (lockobj) {
  892. if (unicodeEncoding == null) {
  893. unicodeEncoding = new UnicodeEncoding (false, true);
  894. // unicodeEncoding.is_readonly = true;
  895. }
  896. }
  897. }
  898. return unicodeEncoding;
  899. }
  900. }
  901. // Get the standard little-endian UTF-32 encoding object.
  902. public static Encoding UTF32
  903. {
  904. get {
  905. if (utf32Encoding == null) {
  906. lock (lockobj) {
  907. if (utf32Encoding == null) {
  908. utf32Encoding = new UTF32Encoding (false, true);
  909. // utf32Encoding.is_readonly = true;
  910. }
  911. }
  912. }
  913. return utf32Encoding;
  914. }
  915. }
  916. // Get the standard big-endian UTF-32 encoding object.
  917. internal static Encoding BigEndianUTF32
  918. {
  919. get {
  920. if (bigEndianUTF32Encoding == null) {
  921. lock (lockobj) {
  922. if (bigEndianUTF32Encoding == null) {
  923. bigEndianUTF32Encoding = new UTF32Encoding (true, true);
  924. // bigEndianUTF32Encoding.is_readonly = true;
  925. }
  926. }
  927. }
  928. return bigEndianUTF32Encoding;
  929. }
  930. }
  931. // Forwarding decoder implementation.
  932. private sealed class ForwardingDecoder : Decoder
  933. {
  934. private Encoding encoding;
  935. // Constructor.
  936. public ForwardingDecoder (Encoding enc)
  937. {
  938. encoding = enc;
  939. DecoderFallback fallback = encoding.DecoderFallback;
  940. if (fallback != null)
  941. Fallback = fallback;
  942. }
  943. // Override inherited methods.
  944. public override int GetCharCount (byte[] bytes, int index, int count)
  945. {
  946. return encoding.GetCharCount (bytes, index, count);
  947. }
  948. public override int GetChars (byte[] bytes, int byteIndex,
  949. int byteCount, char[] chars,
  950. int charIndex)
  951. {
  952. return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
  953. }
  954. } // class ForwardingDecoder
  955. // Forwarding encoder implementation.
  956. private sealed class ForwardingEncoder : Encoder
  957. {
  958. private Encoding encoding;
  959. // Constructor.
  960. public ForwardingEncoder (Encoding enc)
  961. {
  962. encoding = enc;
  963. EncoderFallback fallback = encoding.EncoderFallback;
  964. if (fallback != null)
  965. Fallback = fallback;
  966. }
  967. // Override inherited methods.
  968. public override int GetByteCount (char[] chars, int index, int count, bool flush)
  969. {
  970. return encoding.GetByteCount (chars, index, count);
  971. }
  972. public override int GetBytes (char[] chars, int charIndex,
  973. int charCount, byte[] bytes,
  974. int byteCount, bool flush)
  975. {
  976. return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
  977. }
  978. } // class ForwardingEncoder
  979. [CLSCompliantAttribute(false)]
  980. [ComVisible (false)]
  981. public unsafe virtual int GetByteCount (char *chars, int count)
  982. {
  983. if (chars == null)
  984. throw new ArgumentNullException ("chars");
  985. if (count < 0)
  986. throw new ArgumentOutOfRangeException ("count");
  987. char [] c = new char [count];
  988. for (int p = 0; p < count; p++)
  989. c [p] = chars [p];
  990. return GetByteCount (c);
  991. }
  992. [CLSCompliantAttribute(false)]
  993. [ComVisible (false)]
  994. public unsafe virtual int GetCharCount (byte *bytes, int count)
  995. {
  996. if (bytes == null)
  997. throw new ArgumentNullException ("bytes");
  998. if (count < 0)
  999. throw new ArgumentOutOfRangeException ("count");
  1000. byte [] ba = new byte [count];
  1001. for (int i = 0; i < count; i++)
  1002. ba [i] = bytes [i];
  1003. return GetCharCount (ba, 0, count);
  1004. }
  1005. [CLSCompliantAttribute(false)]
  1006. [ComVisible (false)]
  1007. public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  1008. {
  1009. if (bytes == null)
  1010. throw new ArgumentNullException ("bytes");
  1011. if (chars == null)
  1012. throw new ArgumentNullException ("chars");
  1013. if (charCount < 0)
  1014. throw new ArgumentOutOfRangeException ("charCount");
  1015. if (byteCount < 0)
  1016. throw new ArgumentOutOfRangeException ("byteCount");
  1017. byte [] ba = new byte [byteCount];
  1018. for (int i = 0; i < byteCount; i++)
  1019. ba [i] = bytes [i];
  1020. char [] ret = GetChars (ba, 0, byteCount);
  1021. int top = ret.Length;
  1022. if (top > charCount)
  1023. throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
  1024. for (int i = 0; i < top; i++)
  1025. chars [i] = ret [i];
  1026. return top;
  1027. }
  1028. [CLSCompliantAttribute(false)]
  1029. [ComVisible (false)]
  1030. public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  1031. {
  1032. if (bytes == null)
  1033. throw new ArgumentNullException ("bytes");
  1034. if (chars == null)
  1035. throw new ArgumentNullException ("chars");
  1036. if (charCount < 0)
  1037. throw new ArgumentOutOfRangeException ("charCount");
  1038. if (byteCount < 0)
  1039. throw new ArgumentOutOfRangeException ("byteCount");
  1040. char [] c = new char [charCount];
  1041. for (int i = 0; i < charCount; i++)
  1042. c [i] = chars [i];
  1043. byte [] b = GetBytes (c, 0, charCount);
  1044. int top = b.Length;
  1045. if (top > byteCount)
  1046. throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
  1047. for (int i = 0; i < top; i++)
  1048. bytes [i] = b [i];
  1049. return b.Length;
  1050. }
  1051. }; // class Encoding
  1052. }; // namespace System.Text